Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/web/src/components/chat/blocks/subagentParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export function foldSubagentResultBlocks(blocks: Block[]): Block[] {
out.push({
kind: "worker_result",
sessionId: r.sessionId,
msgId: r.msgId || undefined,
from: r.from || undefined,
status: r.status || undefined,
goal: r.goal || undefined,
Expand Down Expand Up @@ -305,3 +306,30 @@ export function foldSubagentResultBlocks(blocks: Block[]): Block[] {
}
return out;
}

// A worker session can send a series of digests and a separate terminal result.
// Older envelopes lack a message ID; only identical legacy reports are replays.
function resultKey(block: Block): string | undefined {
if (block.kind === "worker_result") {
if (block.msgId) return JSON.stringify([block.kind, block.sessionId, block.msgId]);
if (block.sessionId) {
return JSON.stringify([
block.kind, block.sessionId, block.digest, block.status, block.summary, block.filePath,
]);
}
}
if (block.kind === "subagent_result" && block.sessionId) {
return JSON.stringify([block.kind, block.sessionId]);
}
}

export function omitRepeatedResults(blocks: Block[], existing: Block[]): Block[] {
const seen = new Set(existing.map(resultKey));
return blocks.filter((block) => {
const key = resultKey(block);
if (!key) return true;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
2 changes: 1 addition & 1 deletion apps/web/src/components/chat/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export type Block =
// ---- worker 协议(worker-protocol-design.md §7,P1 web 侧)----
// 员工回传卡:watch 订阅的终态/ digest 注入 <ginno_worker_result> 信封
// 折成专用卡(区别于子代理结果卡:带派发来源 from 与 digest 标识)。
| { kind: "worker_result"; sessionId: string; from?: string; status?: string;
| { kind: "worker_result"; sessionId: string; msgId?: string; from?: string; status?: string;
goal?: string; summary: string; digest?: boolean;
viaFile?: boolean; filePath?: string }
// 员工任务卡:assign_worker 注入的 <ginno_worker_assign> 信封(worker
Expand Down
21 changes: 3 additions & 18 deletions apps/web/src/components/chat/engine/handlers/02-turn-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import type { EngineCtx } from "../ctx";
import type { ServerEvent } from "./types";
import { omitRepeatedResults } from "../../blocks/subagentParse";
import { foldSubagentResultBlocks } from "@/components/chat/blocks";
import type { ChatMsg } from "../../streamCore";
import { mid } from "../../streamCore";
Expand Down Expand Up @@ -84,29 +85,13 @@ export function onTurnStart(ctx: EngineCtx, sid: string, ev: ServerEvent) {
if (srvTurn && (echoText || echoImgs.length)) {
const store = ctx.storeRef.current[sid] ?? [];
if (!store.some((m) => m.role === "user" && m.turnId === srvTurn)) {
// 信封折卡(<ginno_subagent_result>/<ginno_worker_result> 等)与
// steer.absorbed 路径按 sessionId 互斥(2026-11-05 双卡修复:子代理
// 回传先经 steer 注入折卡,wake turn echo 再折一次 → 同一结果
// 两张卡;absorb 侧已有同款幂等,这里对齐——已存在同 sessionId 的
// 结果卡时,echo 只保留非信封内容(纯文本渠道回显不受影响))。
const echoBlocks = foldSubagentResultBlocks([
const echoBlocks = omitRepeatedResults(foldSubagentResultBlocks([
...(echoText ? [{ kind: "text" as const, text: echoText }] : []),
...echoImgs.map((i) => ({
kind: "image" as const,
url: `data:${i.media_type || "image/png"};base64,${i.data}`,
})),
]).filter((b) => {
const csid = (b as { sessionId?: string }).sessionId;
if (!csid) return true;
if (b.kind !== "subagent_result" && b.kind !== "worker_result") return true;
return !store.some((m) =>
m.blocks.some(
(ob) =>
(ob.kind === "subagent_result" || ob.kind === "worker_result") &&
(ob as { sessionId?: string }).sessionId === csid,
),
);
});
]), store.flatMap((m) => m.blocks));
if (echoBlocks.length) {
const echo: ChatMsg = {
id: srvTurn,
Expand Down
15 changes: 2 additions & 13 deletions apps/web/src/components/chat/engine/handlers/06-steer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import type { EngineCtx } from "../ctx";
import type { ServerEvent } from "./types";
import { omitRepeatedResults } from "../../blocks/subagentParse";
import { foldSubagentResultBlocks, type Block } from "@/components/chat/blocks";
import { mid } from "../../streamCore";
import { settleRecalled } from "../steer";
Expand Down Expand Up @@ -91,19 +92,7 @@ export function onSteerAbsorbed(ctx: EngineCtx, sid: string, ev: ServerEvent) {
]).filter((b) => b.kind !== "text");
if (foldedCards.length) {
const store = ctx.storeRef.current[sid] ?? [];
// 幂等:同一 child 的回传卡已在 store(absorb 事件重放/重连补发)
// 不重复插;无 sessionId 的卡(plan/delegation)不做去重。
const fresh = foldedCards.filter((b) => {
const csid = (b as { sessionId?: string }).sessionId;
if (!csid) return true;
return !store.some((m) =>
m.blocks.some(
(ob) =>
(ob.kind === "subagent_result" || ob.kind === "worker_result") &&
(ob as { sessionId?: string }).sessionId === csid,
),
);
});
const fresh = omitRepeatedResults(foldedCards, store.flatMap((m) => m.blocks));
if (fresh.length) {
ctx.storeRef.current[sid] = [
...store,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/ginno_runtime/worker_bus/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import time
import uuid
from typing import Any

from .. import paths
Expand Down Expand Up @@ -209,7 +210,7 @@ async def notify_settled(target_id: str) -> None:
_session_meta_patch(slug, target_id, {"subscribers": keep})
return
_session_meta_patch(slug, target_id, {"subscribers": keep})
text = _worker_result_text(target_id, meta, st)
text = _worker_result_text(target_id, meta, st, msg_id=f"result-{uuid.uuid4().hex}")
_log.info("worker_notify_settled target=%s status=%s subs=%s",
target_id, st, ",".join(fire))
for sid in fire:
Expand Down Expand Up @@ -274,7 +275,7 @@ async def on_turn_settled_hook(session_id: str) -> None:
)
return
due = list(digests)
_digest_msg_id = f"digest-{session_id[:8]}-{now}"
_digest_msg_id = f"digest-{uuid.uuid4().hex}"
via_file = False
file_path = ""
if len(excerpt) > DIGEST_CAP:
Expand Down
31 changes: 31 additions & 0 deletions packages/runtime/tests/e2e/test_packaged_ui_worker_result_dedup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Distinct Worker messages from one session remain visible across delivery paths."""

import pytest
from playwright.sync_api import expect
from test_packaged_ui_transcript_event_rows import (
app as app,
capture_socket,
open_session,
send_frame,
wait_for_session_socket,
)

pytestmark = pytest.mark.e2e


def test_worker_terminal_is_distinct_from_digest(app):
page, port, sid = app
capture_socket(page)
open_session(page, port, sid)
wait_for_session_socket(page, sid)
digest = ('<ginno_worker_result session="worker-fixture" msg_id="msg-digest" from="AuditWorker" '
'status="working" kind="digest">digest body</ginno_worker_result>')
terminal = ('<ginno_worker_result session="worker-fixture" msg_id="msg-terminal" from="AuditWorker" '
'status="done">terminal body</ginno_worker_result>')
send_frame(page, sid, "steer.absorbed", steer_id="dedup-digest", text=digest)
expect(page.get_by_text("Worker progress: AuditWorker", exact=True)).to_have_count(1)
send_frame(page, sid, "turn.start", turn_id="worker-wake", agent_id="dev", text=terminal)
send_frame(page, sid, "steer.absorbed", steer_id="dedup-terminal", text=terminal)
send_frame(page, sid, "steer.absorbed", steer_id="dedup-digest-replay", text=digest)
expect(page.get_by_text("Worker return: AuditWorker", exact=True)).to_have_count(1)
expect(page.get_by_text("Worker progress: AuditWorker", exact=True)).to_have_count(1)
1 change: 1 addition & 0 deletions packages/runtime/tests/unit/test_worker_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async def test_notify_settled_fires_once_and_skips_ancestors(inject_calls):
assert len(inject_calls) == 1 and inject_calls[0]["sub"] == "SUB"
assert "<ginno_worker_result" in inject_calls[0]["text"]
assert 'status="done"' in inject_calls[0]["text"]
assert 'msg_id="result-' in inject_calls[0]["text"]
assert inject_calls[0]["child"] == "W1"
assert inject_calls[0]["extra"]["ginno_worker_msg"]["from"] == "W1"
meta = next(m for m in _session_meta_list(SLUG) if m["id"] == "W1")
Expand Down
Loading