Skip to content

[business-logic] #4232 Case 3 不完整修复: session-timeout 中止的 run 仍执行 reflection/ratchet/failure-attribution #4248

Closed

Description

@topcheer

现象

#4232 (587e547) 提交信息 Case 3 明确宣称:

"isCancelled now also honors context.DeadlineExceeded so session-timeout-aborted runs skip reflection/failure-attribution (noisy insights, wasted 30s LLM call)."

但实现未覆盖 session-timeout 的主路径,被超时中止的 run 仍会执行完整的 post-run reflection/ratchet/failure-attribution 链。

根因

internal/agent/agent.go L1698-1699:

isCancelled := errors.Is(err, context.Canceled) ||
    (err == nil && ctx.Err() != nil && (errors.Is(ctx.Err(), context.Canceled) || errors.Is(ctx.Err(), context.DeadlineExceeded)))

新增的 DeadlineExceeded 检查被锁死在 err == nil && ctx.Err() != nil 子句内,只覆盖"run 正常返回 nil 但外部 ctx 恰好到期"的竞态。而 session-timeout 的两条主路径返回的都是非 nil 错误:

  1. 硬停路径: sessionTimedOut = true → break → L5963-5971 return ErrSessionTimeout。ErrSessionTimeout 是不包装任何 context 错误的裸哨兵 (internal/agent/session_timeout.go:16) → 两个分支都不命中 → isCancelled = false
  2. L2229 路径: return ctx.Err() 返回 context.DeadlineExceeded(非 nil)→ err == nil 为 false → 同样不命中

后果(isCancelled=false 时 L1700 起的 post-run 链全部照跑)

  • maybeReflect → reflection handler + recordPlaybook(失败 degrade 策略 SuccessRate)+ runRatchet(context.Background + 30s 不可取消 LLM 调用)在被截断的 run 上提炼"教训"入库
  • recordFailureAttribution 把"墙钟到点"错误归因为某个无辜 tool/file 的 terminal causal suspect 并持久化
  • trajIntel.recordInjectionOutcome(false) / recordHoldoutOutcome(false) 因超时惩罚被注入的 learning(L1728-1731 注释明确写了 cancelled run 必须跳过正是为了防止这种惩罚)
  • r445 的 MarkInterrupted 快照没有打上——超时 run 恰恰是最需要续跑上下文的场景

超时最常发生在无人值守的 autopilot/cron 长 run 上,恰好是最依赖学习库质量的场景,学习污染有跨 session 复合效应。

证据

修复方向(最小改动)

isCancelled := errors.Is(err, context.Canceled) ||
    errors.Is(err, context.DeadlineExceeded) ||
    errors.Is(err, ErrSessionTimeout) ||
    (err == nil && ctx.Err() != nil && (errors.Is(ctx.Err(), context.Canceled) || errors.Is(ctx.Err(), context.DeadlineExceeded)))

或让 ErrSessionTimeout 包装 context.DeadlineExceeded。并补 session-timeout 路径的测试。

(来源: 业务逻辑审查账本轮 agent.go 深读 + 独立复核 subagent 确认)

Activity

  1. topcheer commented on Oct 16, 2026

    @topcheer
    OwnerAuthor

    复核坐实+已修:PR 见上(err 链双臂——你的两主路径枚举逐条对应:哨兵臂+ctx.Err 臂)。与 #4239 在途件的关系已注明(rebase 以本版为准)。

  2. added a commit that references this issue on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions