Skip to content

fix(executor): harden cancel flow and adopt typed stream part routing#81

Merged
liujuanjuan1984 merged 9 commits intomainfrom
dev/2026-02-14
Feb 15, 2026
Merged

fix(executor): harden cancel flow and adopt typed stream part routing#81
liujuanjuan1984 merged 9 commits intomainfrom
dev/2026-02-14

Conversation

@liujuanjuan1984
Copy link
Copy Markdown
Collaborator

@liujuanjuan1984 liujuanjuan1984 commented Feb 14, 2026

变更背景

本 PR 已从早期的“取消链路 + 内嵌标记解析”演进为:

目标是让流式输出在单 artifact 下保持类型稳定、顺序稳定,并消除 reasoning/text/tool 误路由与重复/丢块问题。

模块一:任务取消链路(Executor / Cancel)

主要改动

  • cancel() 不再强制关闭外部 event_queue,避免取消路径阻塞。
  • 新增运行中请求索引(request task / stop_event / identity)。
  • cancel() 能命中并中断正在执行的 execute()execute()finally 清理注册状态。

相关文件

  • src/opencode_a2a_serve/agent.py
  • tests/test_cancellation.py
  • tests/test_agent_errors.py

模块二:流式输出模型重构(Single Artifact + BlockType)

主要改动

  • 废弃物理 Channel 拆分,统一为单 artifact 流({task_id}:stream)。
  • 元数据统一使用 opencode.block_typetext / reasoning / tool_call)。
  • 引入 opencode.sequence 保持跨块时序。
  • 移除 content_type 冗余字段。

相关文件

  • src/opencode_a2a_serve/agent.py
  • tests/test_streaming_output_contract.py

模块三:强类型 part 状态机与乱序恢复(Schema-first)

主要改动

  • part.type + part_id 为主路由,建立 part_id 状态机(buffer / role / message_id / block_type)。
  • 支持 message.part.deltamessage.part.updated 乱序:
    • delta 先到时缓存,updated 到达后回放。
  • 增加结构化 tool part 输出(tool/call_id/status/...)。
  • 保留单 artifact append-only 发射规则,修复 reasoning 重复追加场景。

相关文件

  • src/opencode_a2a_serve/agent.py
  • tests/test_streaming_output_contract.py

模块四:文档同步

主要改动

  • 更新流式契约说明:单 artifact、block_typesequence、最终快照策略。
  • 明确 schema-first 路由与 delta 乱序回放,不再以内嵌 marker parser 作为主路径。

相关文件

  • docs/guide.md
  • docs/deployment.md

回归验证

  • uv run pre-commit run --all-files
  • uv run pytest ✅(当前 59 passed)

风险与边界

  • 本 PR 保证服务内取消链路中断;上游推理是否可中止仍取决于 OpenCode abort 能力(未在本 PR 内对接)。
  • part.updated 缺失 part_id 时使用回退键归并,仅用于兼容异常事件形态。

关联关系

@liujuanjuan1984 liujuanjuan1984 changed the title fix: 任务取消链路与流式嵌入频道解析稳定性修复 fix: harden task cancellation flow and embedded stream channel parsing Feb 14, 2026
@liujuanjuan1984
Copy link
Copy Markdown
Collaborator Author

基于本轮审查,已修复并补测以下关键风险:

  1. tool_call 解析提前闭合风险
  • 问题:原扫描逻辑仅按 [/] 计数,未感知字符串与转义;当 JSON 字符串中出现 ] 时会提前闭合,造成 tool_call/final_answer 频道污染。
  • 修复:在 _parse_embedded_channelstool_call 扫描中增加字符串状态机(in_string/escaped/quote_char),仅在非字符串上下文处理方括号深度。
  1. 回归测试补充
  • 新增 test_streaming_parses_tool_call_with_bracket_in_json_string,覆盖 ] 出现在 JSON 字符串且含数组字段的场景,验证:
    • tool_call 完整保留:[tool_call: {"text":"a]b","arr":[1,2]}]
    • final_answer 不被污染。

验证结果:

  • uv run pre-commit run --all-files 通过
  • uv run pytest 通过(当前 53 passed)

对应 commit:e4595a1

@liujuanjuan1984 liujuanjuan1984 changed the title fix: harden task cancellation flow and embedded stream channel parsing fix(executor): harden cancel flow and adopt typed stream part routing Feb 15, 2026
@liujuanjuan1984 liujuanjuan1984 marked this pull request as ready for review February 15, 2026 07:07
@liujuanjuan1984 liujuanjuan1984 merged commit 8d631fb into main Feb 15, 2026
1 check passed
@liujuanjuan1984 liujuanjuan1984 deleted the dev/2026-02-14 branch February 15, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment