Skip to content

fix(card): 失败卡不再把「重试可能有用」当成「什么都没执行」 - #1132

Merged
deepcoldy merged 1 commit into
masterfrom
fix/retryable-is-not-proof-nothing-ran
Aug 31, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/retryable-is-not-proof-nothing-ran

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

问题:卡上每一句都是假的

用户报告「明明没报错却显示本轮执行失败」。实际是真失败了,但失败卡说的内容全是假的:

⚠️ Claude 本轮执行失败
错误码:`provider_server_error`
这一轮的输入没有送达 CLI,没有任何已执行的操作,可以安全重试。
[🔄 重试这一轮]  ← 提交的是「原话重发」

线上实证:出这张卡的那一轮,会话已经查过 npm registry、读过 origin/master、发出了多条 IM 消息。所以:

  1. 「输入没有送达 CLI」— 假
  2. 「没有任何已执行的操作」— 假
  3. 「可以安全重试」— 危险:按钮是重发原话,点下去会重复那些副作用

根因:把两个语义压进了同一个档位

turnRetryOffer()(src/services/turn-failure-notice.ts)里:

if (turn.retryable === true) return 'safe';   // ← 删掉的就是这行

而 safe 档位在卡上渲染的文案(i18n/zh.ts 的 card.turn_failed.retry_safe)明确断言「没执行过」。

但 CLI 给的 retryable 回答的是「重试有没有可能成功」,不是「有没有东西已经跑了」。 provider_server_error 的判定条件(claude-transcript.ts:130)正好说明这点:

if (code === 'server_error'
  || (status >= 500 && status <= 599)
  || hasApiErrorSignature(ev, /connection\s+(reset|lost|closed)|econnreset
      |http2:\s*client\s+connection\s+lost|closed\s+mid-response|…/i)) {
  return { status: 'failed', errorCode: 'provider_server_error', retryable: true };
}

closed mid-response、connection reset、http2 client connection lost —— 全都是「模型已经在跑、连接中途断掉」,副作用完全可能已经落地。

改法

只有「失败发生在哪」能证明没跑过,那正是 PRE_EXECUTION_ERROR_CODES 枚举的(输入连 CLI 都没写进去)。所以:

  • PRE_EXECUTION_ERROR_CODES 成为通向 safe 的唯一路径
  • retryable 只决定「值不值得给重试按钮」,不再决定「有没有副作用风险」
  • retryable === false 的显式拒绝仍然优先(那些 pre-execution 码由 worker.ts 发出时不带 retryable 标志,所以不会被这条吞掉 —— 已核)

连带修正按钮语义:caveated 提交的不再是原话,而是
[BOTMUX_CONTINUE] 上一轮被异常中断,可能已完成了一部分。请确认现场后从中断处继续,不要重复已完成的操作;无法安全判断则停下并请求人工决策。

修后行为(8 个码逐条实测)

错误码 修前 修后
provider_server_error ❌ safe(谎称没跑) ✅ caveated
provider_unexpected_eof ❌ safe ✅ caveated
cli_exit caveated caveated
write_input_threw / adopt_write_input_threw / raw_input_write_failed / zmx_recovery_blocked_before_write / terminal_bridge_unavailable safe ✅ 仍是 safe
provider_invalid_request 等显式拒绝 none none

⚠️ 原有测试把缺陷写成了期望

原用例 offers a plain retry when the CLI explicitly authorised it 断言的正是
turnRetryOffer({errorCode:'provider_server_error', retryable:true}) === 'safe'
—— 正是它让整个套件在用户看到假卡片时保持绿色。

已将其反转为「retryable: true 不得读成『没执行』」,并新增反方向守卫:5 个 pre-execution 码必须仍是 safe(否则过度警告会把真安全的轮也降级成 continue,那是另一个方向的回归)。

验证

  • 反变异 2/2 全红:① 还原 if (turn.retryable === true) return 'safe' → 1 红 ② 去掉 pre-execution 的 safe 通道(过度警告)→ 2 红
  • 变异后逐次 cmp 核对还原为 byte-identical
  • bun run build ✅、tsc --noEmit ✅
  • test/turn-failure-card.test.ts 48/48;加上 worker-structured-lifecycle-status 共 78/78
  • 影响面:turnRetryOffer 的消费点只有 worker-pool.ts:1283(构卡)与 card-builder.ts(渲染 + 按钮动作),两者共用同一 offer,不存在「卡上说能安全重试、按钮却给 continue」的分裂

遗留

  • 本 PR 不动 provider_server_error 的判定条件本身(把 5xx 与 mid-response 断连合并成一个码是既有设计)。若要区分「请求根本没发出」与「响应中途断」,需要 provider 侧更细的信号,属另一件事。

失败卡对 `provider_server_error` 打出「这一轮的输入**没有送达 CLI**,**没有任何已执行
的操作**,可以安全重试」——三句话全是假的,而且按钮还是「重发原话」,点下去会**重复
已经产生的副作用**。

根因在 `turnRetryOffer()`:
```ts
if (turn.retryable === true) return 'safe';   // ← 删掉的就是这行
```
`safe` 档位的文案断言「没执行过」,但 CLI 给的 `retryable` 回答的是「**重试有没有可能
成功**」,不是「有没有东西已经跑了」。而 `provider_server_error` 恰恰覆盖
`closed mid-response` / `connection reset` / `http2: client connection lost` / HTTP 5xx
(见 `claude-transcript.ts`)——**全都是模型已经在跑、连接中途断掉**。

线上实证:一轮已经查过 registry、读过 master、发过多条 IM 消息的会话以
`provider_server_error` 结束,卡片却宣称没有任何已执行的操作。

改法:只有「失败发生在**哪**」能证明没跑过,那正是 `PRE_EXECUTION_ERROR_CODES`
(输入连 CLI 都没写进去)。现在**只有该集合通向 `safe`**;`retryable` 只决定值不值得
给重试按钮。`retryable === false` 的显式拒绝仍然优先(那些码不带 retryable 标志发出,
不会被吞)。

连带修正按钮语义:`caveated` 提交的是 `[BOTMUX_CONTINUE] …不要重复已完成的操作…`
而不是原话重发。

⚠️ 原有用例「offers a plain retry when the CLI explicitly authorised it」断言的正是
`provider_server_error → safe`,**把缺陷写成了期望**,所以套件一直绿。已将其反转为
「`retryable: true` 不得读成『没执行』」,并新增一条守住反方向(5 个 pre-execution 码
必须仍是 `safe`,避免过度警告把真安全的轮也降级成 continue)。

验证:8 个错误码逐条核对;反变异 2/2 全红(还原那行 → 1 红;去掉 pre-execution 的 safe
通道 → 2 红);`bun run build`、`tsc --noEmit` 通过;相关测试 78/78。
@deepcoldy

Copy link
Copy Markdown
Owner Author

Review (pi bot, automated). ⚠️ Same shared-identity caveat: this account is the PR author, so this is a comment, not an approval.

LGTM. Independently verified:

  1. Order (point 1): the current order is correct — do NOT reverse it. I traced all 5 pre-execution codes to their emitTurnTerminal call sites: every one is emitted WITHOUT the retryable parameter (it's the 6th arg, all pass ≤4 args), so turn.retryable === undefined, not false. The retryable === false check cannot swallow them today. If a future call site pairs a pre-execution code with retryable: false, the CLI is explicitly saying "retry won't help" — respecting that (none) is more correct than offering a retry button that won't work. Pre-execution-first would offer retry even when the CLI refused it.

  2. Completeness (point 2): the 5 codes are the clear-cut cases. I checked the other error codes that reach turnRetryOffer:

    • submit_impossible:* — likely pre-execution but dynamic (reason suffix), can't be in a static Set. Potential follow-up for a prefix match.
    • submit_usage_limit — ambiguous (limit may be detected before or during submission); caveated is the safe choice.
    • submit_unconfirmed — genuinely ambiguous (write may have completed without confirmation); caveated is correct.
    • adopt_generation_changed* — concurrency signals; caveated is reasonable.

    The set errs toward caveated (over-cautious) rather than safe (under-cautious) — the right failure mode. No false safe (the dangerous direction).

  3. provider_unexpected_eof → caveated (point 3): agreed. EOF can occur mid-response; the code at claude-transcript.ts:128 returns retryable: true for it, same class as provider_server_error.

  4. Card rendering + handler: verified the full chain — safe → primary "retry" button (mode: 'resend', original input); caveated → default "continue" button (mode: 'continue', buildTurnContinuePrompt()). Old cards without a mode field default to continue (fail-safe). Renderer and handler read the same retryOffer, so card semantics match execution.

  5. Tests: 48/48 on both vitest and bun test. The reversed test ("retryable: true must NOT be read as nothing executed") and the counterweight (pre-execution codes still yield safe) are well-designed.

@deepcoldy
deepcoldy merged commit a22f1c3 into master Aug 31, 2026
8 of 9 checks passed
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

🚀 Released in v3.18.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