DSH / Atlas
2026-11-24implementedarchitecture

The inbox returns claims for an unopened step

Inbox 放回尚未开启的步骤所领取的消息

AgentLoop claims inbox input before prompt assembly and `agent/pre-step`. If either fails, the user stops the Agent, or the process exits before `step/start`, the batch has already left the inbox and no `user/message` records it. #5655 reports the user case: Stop during the compaction a second prompt triggered loses that prompt. #8700 and #8721 report the producer cases: subagent results, scheduled reminders, ACP pro

Source compatibility: Chinese title uses a compatible non-canonical header

English

Problem

AgentLoop claims inbox input before prompt assembly and agent/pre-step. If either fails, the user stops the Agent, or the process exits before step/start, the batch has already left the inbox and no user/message records it. #5655 reports the user case: Stop during the compaction a second prompt triggered loses that prompt. #8700 and #8721 report the producer cases: subagent results, scheduled reminders, ACP prompts, and goal rounds disappear the same way. Every inbox producer shares one recovery owner, the inbox itself, because headless, ACP, SDK, and guest producers keep no copy of what they queued.

Decision

The inbox projection folds queues and claims from the same splices. A claim splice records start and removedCount but not the removed messages, so the fold that reconstructs the queues is the only fold that can name the claimed batch. The projection state is { queues: { 'next-turn', 'next-step' }, claim: { pending, interrupted } }. queues stays one object because the wire view returns it by reference, which lets claim-only changes republish nothing. claim mirrors that grouping. The transitions are:

EventBefore this changeNow
agent/inbox/spliced, queue updateValidate start and removedCount, splice the target, reject an identity pending twiceUnchanged
agent/inbox/spliced, removal without outcomeQueue update onlyThe removed messages append to claim.pending in queue order
agent/inbox/spliced, outcome: 'canceled'Queue update onlyThe removed identities leave claim.pending and claim.interrupted unless the same splice reinserts them
step/startIgnoredclaim.pending empties
turn/endIgnoredclaim.pending empties; an interrupted reason moves it to claim.interrupted, any other reason empties both
session/end-seed with inherited: trueIgnoredBoth claim lists empty: a fork inherits its parent's queue, never a claim the parent's turn left unopened
Other eventsSame referenceSame reference
Wire viewThe statequeues, by reference

The Host checkpoint stores the full state under stateVersion 3; a version 1 or 2 row replays from the log.

The inbox owns the return. When a turn ends before step/start, the loop calls inbox.returnClaims() before recording turn/end, and the inbox decides: clear() marks the current claim discarded and claim() resets the mark, so a Stop that clears the queue drops the batch while a Stop that keeps the queue (keepInbox) returns it to the head of next-turn in claim order, skipping identities already queued. A return the Session refuses is reported through agent/error; turn/end still records the original failure and settles the claim. The loop keeps no return state of its own.

Resume restores the interrupted claim before agent/created. After announce() opens the Session for writes, AgentLoop calls inbox.restoreInterrupted() without consulting the creation source: claim.interrupted is non-empty only after the resume repair recorded turn/end as interrupted, so a fresh Session is a no-op. A fork is also a no-op: a cold read of a crashed parent closes its open turn as interrupted inside the inherited prefix, and the fold drops both claim lists at the inherited session/end-seed, so the child keeps the parent's queue but not the input the parent had taken out of it. A session/created listener cannot own this because the Session is read-only during that dispatch.

Producers that own a request or a retry withdraw their returned input. ACP withdraws the prompt of a settled request, the goal driver withdraws a round without a live reservation, and schedule recovery leaves a returned reminder queued instead of moving it or holding its slot. The subagent manager treats a child whose only queued input the latest turn returned as settled rather than parked.

Alternatives considered

A parallel projection holding only pending and interrupted. It must replay the queues itself to learn which messages a claim removed, so each Session would carry two queue folds and two queue copies in its checkpoint, and a splice change would touch both. Carrying the removed messages in the splice event instead would duplicate every message in the log.

A flat projection state. Four parallel fields read better but the wire view must then build a new object per state change, so each step/start and turn/end would republish an identical inbox to every client; a reference cache in the view costs more than the grouping.

An error hook or user/message before preparation. A hook cannot recover a claim after a process exit. Committing user/message before request preparation leaves an unanswered prompt in history when the user stops during preparation.

Return only on failure, drop on every cancellation. Stop during compaction is #5655's own report; treating it as a decision against the message keeps that message lost. keepInbox already distinguishes a Stop from a clear.

Return input at each producer (#8741). It restores Web prompts and nothing else: headless, ACP, SDK, and guests keep no copy of their input. Returning in the inbox makes recovery the default and lets producers with their own handling withdraw.

Return each message to the queue it left. A steer returned to next-step is invisible to the queue dock and runs with the next message without a chance to edit or remove it; the step it was steering ended with its turn. Next-turn is where the owner sees it.

A return flag on the loop's running phase. The loop recorded whether cancellation kept the inbox and consulted it in its turn-ending catch. The fact belongs to the inbox, which already sees clear() and claim(), and the flag was used as state although it is consumed once per driver. Wrapping the step loop in an inbox-owned recovery function was considered for the same reason; it moves the one call into a closure without moving the decision, at the cost of re-indenting the loop and routing its exits through a return value.

Verification

  • agent-loop/tests/inbox-projection.spec.ts: the transition table above, one case per row, plus same-reference results, view reference reuse across claim-only changes, rejected splices, and checkpoint replay from versions 1 and 2.
  • agent-loop/tests/inbox-settlement.spec.ts: a queue-keeping Stop and a failing pre-step return the whole batch without waking the Agent; a clearing Stop discards it; a failure after step/start leaves the batch settled; a refused return is reported and the turn still closes with the original failure; a fresh Agent records no return; a fork of a crashed parent keeps the parent's queue without restoring its claim.
  • agent-loop/tests/cancel.spec.ts, loop.spec.ts, interception.spec.ts: the returned prompt sits ahead of later input and the next wake runs it first.
  • agent-loop/tests/resume.spec.ts: a resumed log whose interrupted turn claimed a prompt returns it before agent/created, once, ahead of input queued later.
  • subagent tests cover idle children staying resident with returned input. ACP tests cover rejected prompts running on the next wake. Headless and SDK tests cover next-wake handling of returned child results and SDK prompts. SDK resume tests cover a stale goal round leaving the first prompt queued.

Consequences

  • A failure before step/start leaves the complete claimed batch at the next-turn head; the queue dock shows it and the next wake runs it in order. Once the step starts, the batch is settled even when request preparation fails before the messages enter history.
  • A message that fails before step start on every wake stays at the queue head until its cause is fixed or its owner removes it, and input arriving meanwhile is claimed and returned with it.
  • Schedule recovery can repeatedly wake a returned reminder while a later occurrence stays due.
  • The loop's turn code holds one returnClaims() call and no inbox recovery state.

中文

问题

AgentLoop 在提示词组装和 agent/pre-step 之前领取 inbox 输入。如果其中任一环节失败、用户停止 Agent,或者进程在 step/start 之前退出,这批消息已经离开 inbox,也没有 user/message 记录它。#5655 报告的是用户场景:第二条提示触发压缩时按 Stop,那条提示就丢了。#8700 和 #8721 报告的是投递方场景:子 Agent 结果、定时提醒、ACP 提示和 goal 回合以同样的方式消失。所有 inbox 投递方共用一个恢复责任方,也就是 inbox 本身,因为 headless、ACP、SDK 和访客投递方都不保留自己排入的输入副本。

决策

inbox 投影从同一组 splice 折叠出队列和领取记录。 领取时写入的 splice 只记录 start 和 removedCount,不记录被移除的消息,所以只有重建队列的那份折叠逻辑能说出领取的是哪一批消息。投影状态为 { queues: { 'next-turn', 'next-step' }, claim: { pending, interrupted } }。queues 保持为一个对象,因为 wire view 按引用返回它,只改领取记录时就不会重复发布。claim 采用同样的分组。状态转移如下:

事件本次修改前现在
agent/inbox/spliced,队列更新校验 start 和 removedCount,对目标队列做 splice,拒绝同一身份重复排队不变
agent/inbox/spliced,移除且没有 outcome只更新队列被移除的消息按队列顺序追加到 claim.pending
agent/inbox/spliced,outcome: 'canceled'只更新队列被移除的身份从 claim.pending 和 claim.interrupted 中移出,除非同一条 splice 又把它插回
step/start忽略清空 claim.pending
turn/end忽略清空 claim.pending;reason 为 interrupted 时把它移到 claim.interrupted,其他 reason 两者都清空
带 inherited: true 的 session/end-seed忽略两个领取列表都清空:fork 继承父会话的队列,但不继承父会话轮次留下的未开启领取
其他事件同一引用同一引用
Wire view整个状态按引用返回 queues

Host 检查点以 stateVersion 3 保存完整状态;版本 1 或 2 的行从日志重放。

由 inbox 负责放回。 轮次在 step/start 之前结束时,循环在记录 turn/end 之前调用 inbox.returnClaims(),由 inbox 决定:clear() 把当前领取记为已放弃,claim() 重置这个标记,因此清空队列的 Stop 会丢弃这批消息,保留队列的 Stop(keepInbox)会把它们按领取顺序放回 next-turn 队首,跳过已经在队列中的身份。Session 拒绝放回时通过 agent/error 报告;turn/end 仍记录原始失败并落定这次领取。循环自己不持有任何放回状态。

恢复时在 agent/created 之前放回被中断的领取。 announce() 让 Session 可写之后,AgentLoop 调用 inbox.restoreInterrupted(),不看创建来源:只有恢复修复把 turn/end 记为 interrupted 之后 claim.interrupted 才非空,所以新建 Session 时这一步什么也不做。fork 时同样什么也不做:冷读崩溃的父会话会在继承前缀内把未关闭的轮次记为 interrupted,折叠逻辑在带 inherited 标记的 session/end-seed 处清空两个领取列表,子会话因此保留父会话的队列,但不保留父会话已经取出的输入。session/created 监听器不能承担这件事,因为派发期间 Session 是只读的。

拥有请求或重试的投递方撤回自己被放回的输入。 ACP 撤回已结算请求的提示,goal 驱动撤回没有有效预约的回合,schedule 恢复让被放回的提醒留在队列里,不移动它也不占用它的时段。子 Agent 管理器把只剩放回输入的子 Agent 视为已结算,而非停放。

考虑过的替代方案

只保存 pending 和 interrupted 的平行投影。 它必须自己重放队列才能知道一次领取移除了哪些消息,于是每个 Session 都有两份队列折叠逻辑和两份队列检查点,修改 splice 时两处都要改。改为让 splice 事件携带被移除的消息,则每条消息都会在日志里重复一份。

平铺的投影状态。 四个并列字段更好读,但 wire view 必须在每次状态变化时新建对象,于是每次 step/start 和 turn/end 都会向所有客户端重复发布内容相同的 inbox;在 view 里缓存引用的代价比分组更大。

错误钩子,或在准备前写 user/message。 钩子无法在进程退出后恢复领取。在请求准备前提交 user/message,用户在准备期间停止时历史里会留下一条没有回答的提示。

只在失败时放回,任何取消都丢弃。 压缩期间按 Stop 正是 #5655 报告的情形;把它当作对消息的否决会让那条消息继续丢失。keepInbox 已经把 Stop 和清空分开。

由各投递方放回(#8741)。 这个方案只能放回 Web 提示:headless、ACP、SDK 和访客都不保留输入副本。由 inbox 放回时,恢复成为默认行为,有自己处理方式的投递方再撤回。

放回各自离开的队列。 放回 next-step 的 steer 在队列面板里看不见,会随下一条消息一起运行而没有编辑或删除的机会;它要插入的那一步已经随轮次结束。next-turn 是拥有者看得见的地方。

在循环的运行阶段上放一个放回标志。 循环曾记录取消是否保留 inbox,并在结束轮次的 catch 里读取它。这个事实属于 inbox,它本来就能看到 clear() 和 claim();而且这个标志每个 driver 只消费一次,却被当作状态使用。出于同样的理由也考虑过用 inbox 持有的恢复函数包住步骤循环;它只是把这一次调用挪进闭包,决策并没有挪动,代价是整个循环重新缩进,循环的出口也要改走返回值。

验证

  • agent-loop/tests/inbox-projection.spec.ts:上表每行一个用例,另有同一引用的结果、只改领取记录时 view 复用引用、拒绝非法 splice,以及从版本 1 和 2 的检查点重放。
  • agent-loop/tests/inbox-settlement.spec.ts:保留队列的 Stop 和失败的 pre-step 放回整批且不唤醒 Agent;清空队列的 Stop 丢弃它;step/start 之后失败时整批保持落定;拒绝放回时报告该失败,轮次仍以原始失败关闭;新建 Agent 不记录放回;fork 崩溃的父会话时保留父会话的队列而不放回其领取。
  • agent-loop/tests/cancel.spec.ts、loop.spec.ts、interception.spec.ts:放回的提示排在后来输入之前,下次唤醒先运行它。
  • agent-loop/tests/resume.spec.ts:被中断轮次领取了提示的恢复日志在 agent/created 之前放回它一次,排在之后排队的输入前面。
  • 子 Agent 测试 覆盖空闲子级携带放回输入继续驻留。ACP 测试 覆盖返回失败的提示在下次唤醒时运行。Headless 和 SDK 测试覆盖下次唤醒处理放回的子 Agent 结果和 SDK 提示。SDK 恢复测试 覆盖首条提示因过时的 goal 回合而继续排队。

影响

  • step/start 之前失败时,已领取的整批消息留在 next-turn 队首;队列面板显示它,下次唤醒按顺序运行。步骤一旦开始,这批消息即已落定,即使请求准备在消息进入历史前失败也一样。
  • 每次唤醒都在步骤开始前失败的消息留在队首,直到原因修复或拥有者移除它,期间到达的输入会和它一起被领取并放回。
  • 后续一次投递持续处于到期状态时,schedule 恢复逻辑可能反复唤醒已放回的提醒。
  • 循环的轮次代码只包含一次 returnClaims() 调用,不持有 inbox 恢复状态。