Repository navigation
fix(daemon): centralize mid-turn event constant + recover timed-out drains - #5266
Conversation
…rains Follow-up to QwenLM#5175 addressing two post-merge /review suggestions. Centralize the `mid_turn_message_injected` SSE event `type`: it was a bare literal in the daemon publisher (acp-bridge), the SDK validator/reducer, and the browser consumer (webui), so a rename in one could silently break browser-side dedup. It now lives once in acp-bridge's dependency-free `daemonEventTypes` module (lightweight like `mcpTimeouts`, so the SDK re-exports it via its build-time devDep without dragging acp-bridge's type graph into the SDK bundle), and bridgeClient / the SDK / webui all import the single binding. Close the drain-timeout message-loss window: the daemon splices + SSE-publishes (browser dedupes) before the ACP child's response lands, so if the child's 2s drain timeout fires first, the late response was discarded — losing the messages from both queues (silent, one-turn loss). The child now recovers that late response and injects it on the next batch instead of dropping it. Tests: drain-timeout recovery (Session), and a rename-safety assertion pinning the shared event constant to the wire literal.
…nParts - Emit a `debugLogger.debug` line when a timed-out drain is recovered (session id + count), guarded on a non-empty payload, so the recovery path is correlatable in production logs. - Rename `#formatMidTurnParts` → `#buildMidTurnParts`: the method records to the chat transcript, so a "format" verb understated its side effect.
origin/main refactored `#drainMidTurnUserMessages` (it now takes an `abortSignal`, parses rich text/structured drain items via `parseMidTurnDrainResponse`, and resolves them through `prefixMidTurnUserMessageParts`), but kept the splice-then-discard timeout path. Re-applied the drain-timeout recovery on top of that structure: the late response is now parsed into `DrainedMidTurnMessage[]`, buffered, and re-injected on the next batch through the shared `#buildMidTurnParts` loop. Also folds in the latest review feedback: - `#recoverLateDrain` bounds the wait with a 30s secondary deadline (unref'd) so a response arriving long after the turn is dropped, not injected into an unrelated context — and swallows a late rejection. - no variable shadowing in the recovery handler. - removed the orphaned constant doc-blocks in bridgeClient.ts; the import site carries a one-line note instead.
✅ Local verification — centralized mid-turn event constant @
|
|
@qwen-code /triage |
doudouOUC
left a comment
There was a problem hiding this comment.
LGTM — 两个聚焦修复,正确性和测试覆盖都到位。
常量集中化
daemonEventTypes.ts无依赖模块 → SDK re-export → webui 消费,与mcpTimeouts.ts同模式,改名编译时全链路失败export const保留字面量类型,switch窄化和DaemonEventEnvelope类型参数不受影响- 防改名测试
expect(MID_TURN_MESSAGE_INJECTED_EVENT).toBe('mid_turn_message_injected')钉住 wire 值
Drain 超时恢复
#recoverLateDrain用Promise.race+ 30s 二级截止时间,Symbol哨兵区分超时与真实响应,pending.catch+ try/catch 双重吞掉迟到 rejection- recovered buffer 在
midTurnDrainUnavailable检查之前清空 → 即使 drain 永久禁用,之前暂存的消息仍会注入,不会丢失 #buildMidTurnParts提取逐行对比原内联代码完全保真,纯机械提取
建议(非阻塞)
dispose() 中未清理 midTurnRecoveredMessages。功能上无害(dispose 后不再调 #drainMidTurnUserMessages,timer 用了 unref()),但与 notificationQueue = [] / cronQueue = [] 的 dispose 模式不一致。建议加一行 this.midTurnRecoveredMessages = []; 保持防御性 cleanup。
What this PR does
A follow-up to #5175 that addresses two
/reviewsuggestions left on that (now merged) PR. First, it makes themid_turn_message_injectedSSE eventtypea single shared constant instead of a bare string literal duplicated across three packages. Second, it closes a narrow window where a mid-turn message could be silently lost for one turn when the ACP child's drain request times out after the daemon has already drained and broadcast it.Why it's needed
The mid-turn drain feature touches the same wire string
'mid_turn_message_injected'in three independent places: the daemon publisher (acp-bridge/bridgeClient.ts), the SDK validator/reducer (sdk-typescript/daemon/events.ts), and the browser consumer (webui/midTurnInjectedSidechannel.ts). A rename or typo in any one — with the others unchanged — silently breaks browser-side dedup: the drain still works, but injected messages are no longer removed from the browser's pending queue and get double-delivered next turn. The siblingcraft/drainMidTurnQueuemethod string was already centralized; this brings the event type to parity.Separately, the daemon answers the child's drain by splicing its in-memory queue and publishing the
mid_turn_message_injectedSSE frame (which the browser uses to dedup) before the response travels back to the child. The child races that response against a 2 s timeout. When the timeout fires first, the child discarded the late response — so the messages were gone from both the daemon queue and the browser queue, yet never reached the model: a silent one-turn loss. Localhost IPC rarely approaches 2 s, but the impact is silent data loss.Reviewer Test Plan
How to verify
Constant centralization: the event type now lives once in
acp-bridge/src/daemonEventTypes.ts— a dependency-free module (likemcpTimeouts.ts) so the SDK re-exports it through its build-time devDep on acp-bridge without dragging acp-bridge's type graph into the SDK's bundled.d.ts.bridgeClient.ts, the SDK, and webui all import the single binding, so a rename either propagates everywhere or fails to compile.cd packages/sdk-typescript && npx vitest run test/unit/daemonEvents.test.ts test/unit/daemon-public-surface.test.ts→ 102 pass, incl. the newMID_TURN_MESSAGE_INJECTED_EVENTrename-safety assertion (value === wire literal, registered in the known-event set).cd packages/acp-bridge && npx vitest run src/bridgeClient.test.ts→ 23 pass.cd packages/webui && npx vitest run src/daemon→ 141 pass.Drain-timeout recovery: on a timed-out drain the child now recovers the late response (the daemon already drained + broadcast it) and injects it on the next tool batch instead of discarding it.
cd packages/cli && npx vitest run src/acp-integration/session/Session.test.ts→ 146 pass, incl. the newrecovers a drain that timed out and injects it on the next batch(times out batch 1, resolves the daemon's answer late, asserts it is injected on batch 2 and recorded exactly once).Typecheck:
npx tsc --noEmitclean inacp-bridge/sdk-typescript/webui/cli. Builds:npm run buildclean foracp-bridge→sdk-typescript→webui; the event const inlines into the SDK's published bundle (index.cjs/mjs,daemon/index.js/cjs), so there is no new runtime acp-bridge dependency.Evidence (Before & After)
N/A — non-user-visible (a shared constant plus a daemon/child drain-recovery code path; no UI change). Behavior is pinned by the unit tests above.
Tested on
tsc --noEmit+ package buildsEnvironment (optional)
Unit tests +
tsc --noEmit+npm run build(acp-bridge,sdk-typescript,webui) on macOS. No live daemon/browser session.Risk & Scope
daemonEventTypessubpath to acp-bridge; the SDK's value is inlined at build time (same mechanism asmcpTimeouts), so there is no new runtime dependency and no SDK-bundle bloat.MID_TURN_MESSAGE_INJECTED_EVENTis additively exported from@qwen-code/sdk/daemon; the wire string is unchanged.Linked Issues
Follow-up to #5175 (post-merge review comments).
中文说明
这个 PR 做了什么
这是 #5175(已合并)的后续,处理该 PR 上
/review留下的两条建议。其一,把mid_turn_message_injected这个 SSE 事件type从三个包里各自硬编码的字符串字面量,收敛为单一共享常量。其二,堵住一个狭窄窗口:当 ACP 子进程的 drain 请求在 daemon 已经排空并广播之后才超时,mid-turn 消息可能被静默丢失一轮。为什么需要
mid-turn drain 特性在三个独立位置用到同一个 wire 字符串
'mid_turn_message_injected':daemon 发布方(acp-bridge/bridgeClient.ts)、SDK 校验/归约(sdk-typescript/daemon/events.ts)、浏览器消费方(webui/midTurnInjectedSidechannel.ts)。任何一处改名/打错而其它处不变,都会静默破坏浏览器侧去重:drain 仍工作,但注入的消息不再从浏览器待发队列移除,下一轮被重复投递。兄弟方法字符串craft/drainMidTurnQueue已经集中化;本 PR 让事件类型对齐。另外,daemon 应答子进程的 drain 时,会先 splice 其内存队列并发布
mid_turn_message_injectedSSE 帧(浏览器据此去重),然后响应才回传给子进程。子进程用 2 秒超时与该响应竞速。若超时先触发,子进程会丢弃这个迟到的响应——于是消息从 daemon 队列和浏览器队列都消失了,却从未到达模型:静默丢一轮。本地 IPC 极少接近 2 秒,但影响是静默数据丢失。审阅者测试计划
如何验证
常量集中化:事件类型现在只在
acp-bridge/src/daemonEventTypes.ts定义——一个无依赖模块(类似mcpTimeouts.ts),这样 SDK 通过其对 acp-bridge 的构建期 devDep 再导出它,而不会把 acp-bridge 的类型图拖进 SDK 打包后的.d.ts。bridgeClient.ts、SDK、webui 都 import 这唯一绑定,改名要么处处传播、要么编译失败。cd packages/sdk-typescript && npx vitest run test/unit/daemonEvents.test.ts test/unit/daemon-public-surface.test.ts→ 102 通过,含新增MID_TURN_MESSAGE_INJECTED_EVENT防改名断言(值 === wire 字面量、且登记在 known-event 集合)。cd packages/acp-bridge && npx vitest run src/bridgeClient.test.ts→ 23 通过。cd packages/webui && npx vitest run src/daemon→ 141 通过。Drain 超时恢复:drain 超时后,子进程现在恢复迟到的响应(daemon 已排空+广播)并在下一批 tool 注入,而不是丢弃。
cd packages/cli && npx vitest run src/acp-integration/session/Session.test.ts→ 146 通过,含新增recovers a drain that timed out and injects it on the next batch(第 1 批超时,迟到 resolve daemon 的应答,断言在第 2 批注入且恰好记录一次)。类型检查:
npx tsc --noEmit在acp-bridge/sdk-typescript/webui/cli干净。构建:npm run build在acp-bridge→sdk-typescript→webui干净;事件常量在构建时内联进 SDK 发布包(index.cjs/mjs、daemon/index.js/cjs),因此没有新的运行时 acp-bridge 依赖。证据(前后对比)
N/A——非用户可见(共享常量 + daemon/子进程的 drain 恢复代码路径;无 UI 变化)。行为由上述单测钉住。
测试平台
tsc --noEmit+ 打包环境(可选)
macOS 上的单测 +
tsc --noEmit+npm run build(acp-bridge、sdk-typescript、webui)。无 live daemon/浏览器会话。风险与范围
daemonEventTypes子路径;SDK 的值在构建时内联(与mcpTimeouts同机制),因此无新增运行时依赖、不增大 SDK 包。MID_TURN_MESSAGE_INJECTED_EVENT是从@qwen-code/sdk/daemon增量导出;wire 字符串不变。关联 Issue
#5175 的后续(合并后评审意见)。