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
26 changes: 25 additions & 1 deletion packages/webui/acp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,44 @@ export class McodeAcpClient extends EventEmitter {
// (含截图工具的 base64 rawOutput)都被 push 进数组且无任何消费者,
// 自迭代长任务把堆撑到 GB 级直到 heap OOM。thinking/answer 累积
// 保留(finalize 有消费者)。
const result = { thinking: '', answer: '', messageIds: new Set(), stopReason: null }
// session-isolation/07: thinking/answer are PER-SEGMENT, not
// turn-long. The discriminator mirrors streamAcpPrompt's
// lastChunkKind (session-isolation/06): chunks of the same kind
// append (streaming growth), a kind change resets — so
// result.answer / result.thinking carry the LAST segment when
// the prompt settles, the same value the live `●` / `▲` lines
// hold. The old turn-long concatenation leaked into the [send]
// result log, the ● finalize rewrite, the no-usage token
// estimate and the empty-turn note.
const result = { thinking: '', answer: '', messageIds: new Set(), stopReason: null, lastChunkKind: null }
const onUpdate = (u) => {
if (u.sessionUpdate === 'agent_thought_chunk' && u.content?.type === 'text') {
if (result.lastChunkKind !== 'thought') result.thinking = ''
result.thinking += u.content.text
result.lastChunkKind = 'thought'
if (u.messageId) result.messageIds.add(u.messageId)
try { onChunk?.({ kind: 'thought', text: u.content.text }) } catch {}
} else if (u.sessionUpdate === 'agent_message_chunk' && u.content?.type === 'text') {
if (result.lastChunkKind !== 'message') result.answer = ''
result.answer += u.content.text
result.lastChunkKind = 'message'
if (u.messageId) result.messageIds.add(u.messageId)
try { onChunk?.({ kind: 'message', text: u.content.text }) } catch {}
} else if (u.sessionUpdate === 'tool_call') {
// session-isolation/07 (acceptance alignment): ONLY
// chat-line-breaking tool events reset the per-segment
// accumulator — the same rule as streamAcpPrompt's
// lastChunkKind (server/lib/mcode-acp.js; keep the two in
// sync). Non-rendering events (usage_update, plan_update,
// session_info_update, mode/goal/config updates) can
// interleave MID-segment and must NOT reset: an early reset
// would truncate result.answer before streamAcpPrompt's
// settle merge consumes it.
result.lastChunkKind = 'tool_call'
// payload: {toolCallId, title, name, status, rawInput, ...}
try { onChunk?.({ kind: 'tool_call', update: u }) } catch {}
} else if (u.sessionUpdate === 'tool_call_update') {
result.lastChunkKind = 'tool_call_update'
try { onChunk?.({ kind: 'tool_update', update: u }) } catch {}
} else if (u.sessionUpdate === 'usage_update') {
// payload: {used, size, cost} — cumulative values for the
Expand Down
15 changes: 15 additions & 0 deletions packages/webui/server/lib/mcode-acp.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ function streamAcpPrompt(
// plan_update, error, anything else) breaks the same-prefix
// chain. Without this update, the next message chunk would
// see lastChunkKind === "message" and skip the reset.
// session-isolation/07: this reset set is the canonical one —
// keep acp.mjs#prompt's result.lastChunkKind in sync (only
// chat-line-breaking tool events reset there; usage / plan /
// session_info events must NOT, they can interleave
// MID-segment and an early reset would truncate
// result.answer).
r.lastChunkKind = "tool_call";
} else if (c.kind === "tool_update" && c.update) {
applyToolUpdate(r, cs, c.update);
Expand Down Expand Up @@ -968,6 +974,15 @@ function streamAcpPrompt(
pushStateFor(cid);
})
.then((result) => {
// session-isolation/07: the transport's result.answer/thinking
// carry the LAST segment (per-segment discriminator in acp.mjs,
// mirroring this callback's lastChunkKind resets) — not a
// turn-long concatenation. `|| r.answer` keeps the
// callback-derived value when the transport saw no chunks of
// that kind; both sides now agree on the same segment
// semantics, so the empty-turn note, the no-usage token
// estimate, the ● finalize rewrite and the [send] result log
// all read the final segment.
r.answer = result.answer || r.answer;
r.thinking = result.thinking || r.thinking;
r.stopReason = result.stopReason;
Expand Down
Loading
Loading