Skip to content

fix(agent): hand a tool call with an unknown result back to the model - #7043

Merged
M4n5ter merged 1 commit into
rust-previewfrom
fix/unknown-tool-result-to-model
Nov 20, 2026
Merged

M4n5ter merged 1 commit into
rust-previewfrom
fix/unknown-tool-result-to-model

Conversation

@UncertaintyDeterminesYou4ndMe

@UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a tool call ended but its effect was unknown (a Host operation that reported Unknown with confirmed cleanup, or a local call that returned OutcomeUnknown), the Run stopped on a blocker and failed. The model never saw what happened, and the user saw a failed turn.

Now such a call is handed back to the model as its result: "The call ended, but its result is unknown: . It may or may not have taken effect. Check the current state before deciding what to do next; do not simply repeat the call." The call is never run again.

  • The call is accepted with a new result kind, AcceptedResult::OutcomeUnknown. The model-facing result is AcceptedToolResult::OutcomeUnknown { diagnostic }, and the model gets the diagnostic as error text. The fold accepts it only with evidence: an Unknown receipt with confirmed cleanup, or a local Unknown return for a call without an original Cell. Acceptance resolves an open OperationUnknown blocker for the call.
  • After acceptance the call no longer counts as unsettled for effect admission, the next model step, follow-up messages or the session catalog. The live projection and a catalog rebuild agree on unknown_effects. History and Recall show the diagnostic text.
  • A local call that returns OutcomeUnknown is first recorded durably as a returned call with settlement Unknown, then its resources are released.
  • This also applies after a Stop: a cancelled tool that returns OutcomeUnknown is settled, so it does not block follow-up messages.
  • Unchanged: a call whose end is not confirmed (cleanup unconfirmed, or its start was unknown) keeps the existing blocker, because it may still be running. Its diagnostic now says so. A code cell's exec keeps its own recovery. blocks_followup and blocks_continuation are unchanged.

Trade-offs

  • A failed Cell wait records the same fact as a local call that returned Unknown (LocalToolReturned with settlement Unknown). The stored fact alone cannot tell them apart. So the engine settles a local call only when the dispatch in the current activation recorded that return. If the Host restarts between the return and the settlement, the call falls back to the existing recovery and the Run fails as it does today. A durable signal would close this gap, but it needs a new settlement value or a check on the reserved tool name wait.
  • DispatchSource is now Ordinary | CellExec | CellWait, so the Host can tell an exec from an ordinary tool without reading the frontier.

Not covered

A remote MCP call whose result is unknown still invalidates the connection, so every later Run that uses it fails (separate issue).

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code (Claude Opus 5.5) found the failure path, wrote the change and the tests, and made the review changes.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

@github-actions github-actions Bot added the effort/L Under 1000 readable lines label Nov 20, 2026

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: reusing RiskAdjudicated causes 4 live-reproduced state/UI/recovery bugs; recommend a dedicated AcceptedResult variant

English

The goal of handing confirmed-ended unknown tool calls back to the model is solid, and answer to Review Question #1 is a clear yes—use a dedicated AcceptedResult / AcceptedToolResult variant rather than reusing Fact::Blocked + Fact::RiskAdjudicated(RecordExternalObservation) + magic JSON key "unknownOutcome". Overloading RiskAdjudicated and LocalToolReturned(Unknown) causes four concrete, live-reproduced bugs:

1. [Major] session_catalog live projection vs rebuild diverges and permanently marks completed Runs as unknown_effects = true (live-reproduced)

  • Locations: index.rs#L117-L127, fold.rs#L162-L181, patches.rs#L352.
  • Path & Repro: After Action::SettleUnknown, tool_pending becomes false (agent_tools.pending = 0), so the next live fact loads only pending = 1 tools and flips agent_catalog_runs.unknown_effects back to 0. On store.rebuild(run) or catalog.rebuild(session), full replay reloads the settled tool, and session_catalog/index.rs:117-127 matches OperationOutcome::Unknown without excluding t.unknown_outcome_accepted(). Repro on real RootStore/Store/SessionCatalog printed after_settle=true, after_complete=false, after_run_rebuild=true, after_cat_rebuild=true—permanently blocking owner/patches.rs:352 and polluting session list status.

2. [Major] Reusing RiskAdjudicated leaks raw internal JSON and unresolved sha256: refs to UI/SessionRead/maka://.../tool-results/... and leaves Recall empty (live-reproduced)

  • Locations: driver.rs#L2214-L2241, tool_result_source.rs#L761-L768, index.rs#L391.
  • Path & Repro: ToolResultAccepted outranks LocalToolReturned / OperationObserved in history pairing (service/history/source.rs:886-910), and tool_result_source.rs:761 passes "risk_adjudicated" straight through without dereferencing decision.evidence. Repro via ToolResultReader::live returned raw internal JSON containing "digest":"sha256:..." with no diagnostic text; recall/index.rs:391 also skips RiskAdjudicated.

3. [Major] Failed CellWait during driver retirement (retiring == true) is misclassified as an ended local tool call and triggers bogus Action::SettleUnknown (live-reproduced)

  • Locations: driver_effects.rs#L568-L590, driver.rs#L1841, reconcile.rs#L385-L399.
  • Path & Repro: view.original_cell(&wait_tool) is always None (Cells belong to exec, not wait). When CellWait hits OutcomeUnknown while retiring == true, driver.rs:1841 skips writing Blocked(OriginalAdmissionUnknown), and settle_all deliberately publishes LocalToolReturned(Unknown) to preserve logical Cell uncertainty (driver_cells.rs:1441). On recovery, reconcile::unknown_outcome sees no OriginalAdmissionUnknown and no original_cell, returning Action::SettleUnknown (reproduced by running the PR's own driver/tests.rs scenario with retiring = true).

4. [Major] User Stop during a local tool returning OutcomeUnknown permanently locks the session (blocks_followup() == true) (live-reproduced)

  • Locations: reconcile.rs#L364, fold.rs#L531-L590.
  • Path & Repro: Real local tools (e.g. CUA click/scroll and stdio MCP after process teardown) return ToolError::OutcomeUnknown on cancellation. Because reconcile.rs:364 rejects SettleUnknown when view.stopped == true, the tool falls into Lookup -> OriginalAdmissionUnknown -> Terminal::Failed. fold.rs:531-590 only exempts Host Unknown + Confirmed, so blocks_followup() stays true and permanently rejects follow-up messages with OriginalOperationUnsettled.

Design & Test Quality

  • Design: Add a dedicated AcceptedResult + AcceptedToolResult variant carrying text diagnostics (mirroring ProviderOutcomeUnknown and Rejected { diagnostic }) and unwrap it in tool_result_source.rs and recall/index.rs. Split DispatchSource into Ordinary | CellExec | CellWait so driver_effects.rs doesn't re-query state.frontier or overload LocalToolReturned(Unknown).
  • Tests (673 / 927 = 72.6%): Heavy scaffolding and white-box implementation mirroring—driver/tests.rs (+330) copies internal driver.rs lines and opens 5 pub(in crate::driver) holes in witness.rs, while the new driver_model.rs branch has 0 test coverage (contradicting the PR Verification note). Recommend deleting driver/tests.rs, slimming reconcile/tests.rs, and writing focused behavioral tests covering the state transitions + an end-to-end Rig test through DriverModel, ToolResultReader::live, and store.rebuild/catalog.rebuild.
简体中文

把已确认结束但结果未知的工具调用交回给模型这个方向是对的,针对 PR Review Question #1,答案很明确:应该新增独立的 AcceptedResult / AcceptedToolResult 变体,不要为了避开 facts schema 改动而硬借 Fact::Blocked + Fact::RiskAdjudicated(RecordExternalObservation) + 魔法 JSON key "unknownOutcome"。借壳复用与一词两用直接带出了 4 个实跑复现的可达 bug:

1. [Major] session_catalog live 投影与 rebuild 分歧,重建后把已完成 Run 永久污染成 unknown_effects = true(实跑复现)

  • 位置:index.rs#L117-L127、fold.rs#L162-L181、patches.rs#L352。
  • 路径与复现:Action::SettleUnknown 后 agent_tools.pending = 0,后续 live fact 只加载 pending = 1 工具把 unknown_effects 翻回 0;但一旦触发 store.rebuild 或 catalog.rebuild,全量重放重新载入该 tool,而 session_catalog/index.rs:117-127 匹配 OperationOutcome::Unknown 时没排除 t.unknown_outcome_accepted()。在真实 RootStore/Store/SessionCatalog 实跑输出 after_settle=true, after_complete=false, after_run_rebuild=true, after_cat_rebuild=true,直接卡死 owner/patches.rs:352 并污染会话列表状态。

2. [Major] 复用 RiskAdjudicated 导致 UI/SessionRead/maka://.../tool-results/... 吐出裸内部 JSON 与 sha256: 指针,且 Recall 索引留空(实跑复现)

  • 位置:driver.rs#L2214-L2241、tool_result_source.rs#L761-L768、index.rs#L391。
  • 路径与复现:历史配对中 ToolResultAccepted 优先级压过 LocalToolReturned / OperationObserved,而 tool_result_source.rs:761 对 "risk_adjudicated" 不解引用 decision.evidence。实跑 ToolResultReader::live 直接返回含 "digest":"sha256:..." 的原始内部 JSON(不含诊断文案);recall/index.rs:391 也直接跳过 RiskAdjudicated。

3. [Major] Driver 退场(retiring == true)期间失败的 CellWait 会被误判为普通已结束本地调用并误触发 Action::SettleUnknown(实跑复现)

  • 位置:driver_effects.rs#L568-L590、driver.rs#L1841、reconcile.rs#L385-L399。
  • 路径与复现:Cell 归属于 exec 而非 wait,view.original_cell(&wait_tool) 恒为 None。retiring == true 时 driver.rs:1841 跳过写 OriginalAdmissionUnknown,随后 settle_all 有意落盘 LocalToolReturned(Unknown) 保留 Cell 逻辑不确定性;下次恢复时 reconcile::unknown_outcome 两个启发式条件全被绕过,直接返回 Action::SettleUnknown(用本 PR 的 driver/tests.rs 场景改为 retiring = true 已实跑复现)。

4. [Major] 用户点 Stop 时本地工具取消返回 OutcomeUnknown 会永久锁死会话(blocks_followup() == true)(实跑复现)

  • 位置:reconcile.rs#L364、fold.rs#L531-L590。
  • 路径与复现:CUA 原生点击/滚动与 stdio MCP 在取消后都会返回 ToolError::OutcomeUnknown。reconcile.rs:364 在 view.stopped 时拒绝 SettleUnknown 退回 Lookup -> OriginalAdmissionUnknown -> Failed,而 fold.rs:531-590 只放行 Host Unknown + Confirmed,导致 blocks_followup() == true 永久拒收后续消息。

设计与测试建议

  • 设计:仿照现成的 AcceptedResult::ProviderOutcomeUnknown 和 AcceptedToolResult::Rejected { diagnostic } 加专用变体并在 tool_result_source.rs / recall/index.rs 解包诊断文本;将 DispatchSource 拆为 Ordinary | CellExec | CellWait,省掉 driver_effects.rs 的异步 frontier 反查并彻底消掉 LocalToolReturned(Unknown) 的语义重载。
  • 测试(673 / 927 = 72.6%):driver/tests.rs(+330 行)大量复刻私有实现并给 witness.rs 开了 5 处可见性后门,而 driver_model.rs 新增代码实际为 0 覆盖(与 PR Verification 描述不符)。建议删掉 driver/tests.rs、精简 reconcile/tests.rs,重构为状态转移表 + 一条真正跑通 DriverModel、ToolResultReader::live 与 store/catalog.rebuild 的端到端行为测试。

@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Thanks — rebased onto current rust-preview and reworked along your suggestions:

  • Design (Q1): added AcceptedResult::OutcomeUnknown and AcceptedToolResult::OutcomeUnknown { diagnostic }; dropped the Blocked + RiskAdjudicated + unknownOutcome reuse. The fold accepts it only with evidence (an Unknown receipt with confirmed cleanup, or a local Unknown return without an original Cell), and acceptance resolves the open OperationUnknown blocker.
  • 1 (catalog after rebuild): run_flags skips accepted OutcomeUnknown calls, so the live projection and a rebuild agree on unknown_effects.
  • 2 (raw JSON / empty Recall): tool_result_source and Recall read the diagnostic; the model gets it as error text.
  • 3 (failed CellWait while retiring): the engine settles a local call only when this activation's dispatch recorded the unknown return (Live.ended_unknown). A bare LocalToolReturned(Unknown) from the store is ambiguous, so after a restart the call keeps its existing recovery. DispatchSource is now Ordinary | CellExec | CellWait; the frontier read is gone.
  • 4 (Stop): settlement no longer skips stopped Runs, so a cancelled tool that returns OutcomeUnknown does not block follow-ups.
  • Tests: removed driver/tests.rs and the witness.rs visibility changes; two table-style reconcile tests; one native test that runs the real complete(), then checks the restored next model request, ToolResultReader::live, and the store and catalog rebuilds.

A call that ended without a known result is accepted as OutcomeUnknown and
goes back to the model as error text with its diagnostic; it is never run
again. A Host operation has ended once its cleanup is confirmed. A local
call counts as ended only when this activation's dispatch recorded the
unknown return, because a failed Cell wait records the same fact; after a
restart such a call keeps its existing recovery.

Accepting the result resolves the open OperationUnknown blocker, settles
the call for admission, the catalog and Recall, and also applies after a
Stop, so follow-up messages are not blocked.

Generated-by: Claude Code (Claude Opus 5.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants