Skip to content

fix(stream): 断流检测 + 会话连续 + 思考签名 + 文档映射 + 协议完整性加固 - #141

Closed
asuan-dev wants to merge 6 commits into
Quorinex:mainfrom
asuan-dev:fix/stream-integrity-and-ide-fingerprint
Closed

fix(stream): 断流检测 + 会话连续 + 思考签名 + 文档映射 + 协议完整性加固#141
asuan-dev wants to merge 6 commits into
Quorinex:mainfrom
asuan-dev:fix/stream-integrity-and-ide-fingerprint

Conversation

@asuan-dev

@asuan-dev asuan-dev commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

本 PR 修复 Kiro-Go 代理的几类真实线上问题,并补齐与官方协议更接近的流完整性/身份/文档路径:

  • 上游空流 / 截断流被当成成功
  • 并行 tool 丢失、重复、或半套泄漏
  • 多轮 Claude 思考签名无法精确回贴
  • 会话 conversation / continuation ID 每轮乱跳
  • document / file 附件被丢弃
  • endpoint 身份与 stop reason 映射分散
  • eventstream CRC / 官方可选事件 / 残缺 tool 参数伪造

当前 HEAD:cd836d8

1. 断流 / 空流(核心)

  • 解析上游 metadataEvent.stopReason
  • 空响应 / 截断响应分类;同账号有限次重试后再换号
  • 已向客户端 flush 部分内容后不再伪造 end_turn / stop / completed,改为报错事件
  • 六条路径统一走 runKiroWithIntegrityRetry
    • Claude / OpenAI / Responses × stream / non-stream
    • 以及 web-search 混合轮次
  • 引入 streamIntegrityState,由状态对象统一观测 content / tools / stopReason / reasoning,避免各路径手搓四元组

2. 并行 tool 完整性

  • toolUseId 状态机累计,保留到达顺序
  • 整组并行 tool 校验通过后才 flush;合法 sibling 不再先泄漏给客户端
  • 残缺 input / 非法 JSON 不再伪造 {},作为 hard integrity error
  • 同账号重试耗尽后,请求内 excluded 正确排除该账号(不误伤 health 记罚)

3. 客户端指纹

  • 默认 KiroIDE 版本 0.11.1071.0.212
  • endpoint 解析统一到 resolveKiroEndpoint:URL / headers / content-type / agent-mode 单一来源
  • API Key 走 CLI runtime 身份;OAuth 保留 preferred endpoint 链

4. 思考签名按次精确回贴

  • 上游 reasoningContentEvent 解析 signature / redacted
  • 服务端按 apiKey + model + conversationId + turn fingerprint 精确存储
  • 仅成功完整回合 seal;仅 exact hit 回贴
  • 不按账号/模型模糊匹配,避免串会话

5. 会话连续性

  • conversationId / agentContinuationId 不再每请求乱造或只靠 opener hash
  • 成功回合后 seal 客户端将回放的 transcript 前缀
  • 下一请求前缀 exact match 才复用;否则 mint 新 ID
  • seal 指纹使用 inbound Claude messages(含 thinking 展示形态),与客户端实际存储一致

6. 文档 / 附件

  • Claude document / file / input_document → Kiro Documents
  • 支持 base64 与 raw-text (source.type=text + text/plain)
  • MIME / magic / 名称清洗 / 会话内去重 / 最多 5 个
  • extractClaudeUserContent 单次解析返回校验错误,避免 validate 与 translate 各解一遍还吞错

7. 协议帧与 stop reason

  • 新增 eventstream CRC 校验(prelude + message)
  • 接受官方可选 stream 事件;未知/坏帧有明确错误而非静默成功
  • stop reason 中心分类(含 content_filtered 等),统一映射到:
    • Claude:end_turn / max_tokens / refusal / tool_use ...
    • OpenAI:stop / length / content_filter / tool_calls
    • Responses:completed / incomplete + reason

8. Web Search 生命周期

  • 混合 tool 轮次共用 integrity 状态
  • 成功终端才 seal 会话与 reasoning
  • 搜索结果继续进入下一轮 tool_result 反馈

Test plan

  • go test ./proxy -count=1
  • go test ./... -count=1
  • 覆盖:空流/截断流重试、flush 后禁止重试、并行残缺 tool 不泄漏、session exact reuse、reasoning exact reattach、document raw-text / magic / limit、endpoint CLI/OAuth 身份、CRC mismatch 拒绝
  • 真实 Kiro 账号联调:断流重试、并行 tool、thinking 多轮、document 附件、web_search 混合
  • 多实例部署场景:确认 session / reasoning 内存态不可跨进程共享(已知限制)

Residual risks

  1. 当前以 unit / mock 为主,真实上游限流与长流式断点仍需手测
  2. session continuity 与 reasoning stamp 存在进程内存,水平扩展不共享
  3. Claude web_search 块仍有部分 stringly 形状,上游格式变化时可能还要再收口

Commits

  • 80c2d98 fix(protocol): align default client version with current Kiro IDE
  • 45b8478 fix(stream): parse stopReason and stop losing parallel tool calls
  • 599e56b fix(stream): surface empty/truncated upstream streams instead of faking success
  • 17e1087 fix(stream): reattach thinking signatures by exact turn key
  • 26259cb feat(proxy): map Claude document blocks to Kiro documents
  • b4e5748 fix(session): reuse conversation and continuation ids across a proven chain
  • 3a5ac64 fix(session): seal the assistant turn the client actually stores
  • ce74db6 merge: reconcile web search with stream and session fixes
  • cd836d8 fix(proxy): harden stream integrity and protocol parity

@asuan-dev asuan-dev changed the title fix(stream): detect truncated streams, stop duplicating tool calls fix(stream): 断流检测 + 会话连续 + 思考签名回贴 + 文档映射 Jul 25, 2026
@asuan-dev asuan-dev changed the title fix(stream): 断流检测 + 会话连续 + 思考签名回贴 + 文档映射 fix(stream): 断流检测 + 会话连续 + 思考签名 + 文档映射 + 协议完整性加固 Jul 26, 2026
Default User-Agent / x-amz-user-agent still advertised 0.11.107. Bump the
fallback to the current desktop build and keep the per-endpoint header
builders explicit so OAuth and API Key paths stay distinguishable.
Parse AWS event-stream messages with prelude/message CRC validation, keep
official optional events, and accumulate parallel tool_use frames by id.
Flush only after the full tool set validates so a malformed sibling cannot
leak a partial tool call to the client.
Add same-account integrity retry for empty/truncated streams and reject
incomplete tool arguments instead of forging {}. Successful-turn sealing
is gated on terminal outcomes that are safe to continue from.
Mint conversation/continuation ids once and reuse them only on an exact
client-transcript match. Store thinking signatures by exact turn key and
reattach on the next request from the handler path, without fuzzy matching.
Translate document/file blocks into Kiro documents (including raw text),
validate MIME/magic/name/limit rules, and classify upstream stop reasons
once for Claude, OpenAI, and Responses mappings.
…r paths

Route Claude/OpenAI/Responses through integrity retry, keep ClaudeToKiro
pure with handler-side attachStoredReasoning, and restore web-search
lifecycle seals: intermediate rounds seal the raw upstream tool shape so
the next loop reuses ids; final flush seals client-visible content.
@asuan-dev
asuan-dev force-pushed the fix/stream-integrity-and-ide-fingerprint branch from 1573c30 to b784418 Compare July 26, 2026 05:17
@asuan-dev

Copy link
Copy Markdown
Contributor Author

Closing this PR. Will not continue the changes here.

@asuan-dev asuan-dev closed this Jul 26, 2026
@asuan-dev
asuan-dev deleted the fix/stream-integrity-and-ide-fingerprint branch July 26, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant