Repository navigation
fix(agent): compact and continue after a context overflow with partial output - #7055
Conversation
M4n5ter
left a comment
There was a problem hiding this comment.
Review: 3 live-reproduced bugs on OpenAI Responses (item_acceptance == true) and proactive compaction adoption
English
Consolidating the 6 scattered inline overflow checks into Attempt::compactable_overflow() / overflow_ended() is a very clean simplification (net -9 production lines). However, dropping !attempt.semantic broke the implicit invariant that overflowed attempts leave no Fact::ModelObserved rows in the canonical log, causing three live-reproduced regressions on OpenAI Responses and proactive compaction paths:
1. [Blocker] On OpenAI Responses (supports_item_acceptance() == true), the "dropped" partial output leaks back into subsequent history replays (same-Run ToolContinuation or Turn 3) (live-reproduced)
- Locations:
summary_repair.rs#L101,compacted.rs#L431-L439,replay.rs#L262-L264. - Root cause & Repro: With
supports_item_acceptance() == true,receive()persistsFact::ModelObservedas soon as anoutput_item.donearrives. When the stream then fails withcontext_length_exceeded,prepare_frozen_summaryreusessource_capture.through_sequencefrom before the overflowed attemptM1asContextCompacted's covered watermark. While the immediateAfterCompactionrequest excludesM1, any subsequentToolContinuationin the same Run or Turn 3 replayssemantic_tailafterthrough_sequence, wherereplay.rs:262includesM1and re-injects itsModelObserved(Text)right beforeM2's real reply (andservice/history/projection.rs:497also renders the supposedly undelivered draft). - Reproduction: Real
Rig+ OpenAI Responses fixture emittingDROPPED_PARTIAL_OUTPUT_LEAK_PROBEthenresponse.failed(context_length_exceeded):summary_has_leak=false, m2_has_leak=false, m2_has_note=true, then on Turn 3:t3_has_leak=true, t3_has_second=true. - Suggested fix: Skip attempts that have
compactable_overflow()(and were superseded by compaction) indriver_model/replay.rs(and alignservice/history/projection.rsif the cut-off draft should not appear as delivered).
2. [Major] Adopting a ready background proactive summary (Action::AdoptCandidate) silently drops the [Host] Your previous response was cut off... note (live-reproduced)
- Location:
compacted.rs#L294-L302. - Root cause & Repro:
compacted.rs:294checks onlysummary_request.compaction_recovery. When a session is near the window limit (the most common situation for mid-response overflow), a background proactive summary is often already ready andreconcile.rs:1545takesAction::AdoptCandidate. Proactive summaries always havecompaction_recovery == None(sql_compaction.rs:69) and may also come from a prior Run (producer != view.run), socut_offevaluates tofalse. - Reproduction: Real
RigwithcompactionThreshold: 5000where Turn 2 triggered proactive summary completion before Main overflowed with partial text:has_summary=true, has_cutoff_note=false, has_partial=false, final_state={"kind":"completed"}. - Suggested fix: When
summary_request.compaction_recovery.filter(|_| producer == view.run)isNoneandcurrent_request.accounting.purpose == ModelPurpose::Main, inspectview.current_attempt's recovery directly.
3. [Major] On OpenAI Responses, a tool call emitted before overflow is actually executed (with side effects) before the Run fails (live-reproduced)
- Location:
reconcile.rs#L1099-L1231. - Root cause & Repro: Allowing
AttemptRecovery::ContextOverflowonModelClosed(Outcome::Error)lets any already-persistedModelObserved(ToolCall)hit the tool-scheduling loop inreconcile.rs:1099-1231before line 1531's overflow branch. Becausetool_incompleteonly checksOutcome::Success + FinishReason::Length, the driver approves and executes the tool (ToolDecided(Approved) -> ToolAdmitted -> LocalToolReturned -> ToolResultAccepted(Succeeded)) and then terminates the Run asFaileddiscarding the result. - Suggested fix: Reject unadmitted tool calls in
reconcile.rswhenever their attempt closed withOutcome::Error+AttemptRecovery::ContextOverflow(or any non-recoverableOutcome::Error) instead of executing them before failing the turn.
4. Tests
- The new
preset_acceptance.rstest (+84 lines) is a solid end-to-end behavioral test (no full rewrite needed); just reuse existingquiescent_messagehelpers and add regressions for the three cases above (Responses Turn 3 non-leak,AdoptCandidatecut-off note, Responses tool-call-before-overflow non-execution).
简体中文
把分散在 state / engine / native 6 处的内联判断收敛成 Attempt::compactable_overflow() / overflow_ended()(生产代码净删 9 行)非常干净。但去掉 !attempt.semantic 后打破了“overflow attempt 不会在 canonical log 留 Fact::ModelObserved”的隐含前提,在 OpenAI Responses 与后台 proactive summary 路径实跑复现出 3 个回归:
1. [Blocker] OpenAI Responses(supports_item_acceptance() == true)上被宣称“丢弃”的 partial output 会在下一次历史重放(同 Run ToolContinuation 或 Turn 3)重新泄漏进 prompt(实跑复现)
- 位置:
summary_repair.rs#L101、compacted.rs#L431-L439、replay.rs#L262-L264。 - 根因与复现:Responses 模式下收到
output_item.done就会立即落盘Fact::ModelObserved。随后触发context_length_exceeded时,prepare_frozen_summary复用了溢出 attemptM1之前的through_sequence作为压缩覆盖水位线。紧接着的AfterCompaction虽不含M1,但同 Run 内任意ToolContinuation或用户发 Turn 3 从through_sequence之后重放semantic_tail时,replay.rs:262会把M1的ModelObserved(Text)原封不动重新插回 prompt(service/history/projection.rs:497也会渲染出来)。实跑复现:summary_has_leak=false, m2_has_leak=false, m2_has_note=true,随后 Turn 3 测得t3_has_leak=true, t3_has_second=true。 - 最小修法:在
driver_model/replay.rs跳过已记录compactable_overflow()且被后续压缩取代的失败 attempt(若不希望未送达草稿展示在历史里,同步对齐projection.rs)。
2. [Major] 命中后台 proactive summary 采纳路径(Action::AdoptCandidate)时静默丢失 [Host] Your previous response was cut off... 提示(实跑复现)
- 位置:
compacted.rs#L294-L302。 - 根因与复现:长会话逼近窗口上限时后台 proactive summary 往往已就绪并走
Action::AdoptCandidate,其compaction_recovery恒为None(且跨 Run 复用时producer != view.run),导致cut_off恒为false。实跑compactionThreshold: 5000测得has_summary=true, has_cutoff_note=false, has_partial=false, final_state={"kind":"completed"}。 - 最小修法:
summary_request.compaction_recovery.filter(|_| producer == view.run)为None且当前主请求为ModelPurpose::Main时,直接检查view.current_attempt的recovery。
3. [Major] OpenAI Responses 上在 overflow 前已发出的 tool call 会先被真实执行(产生副作用),再把 Run 判 Failed 扔掉结果(实跑复现)
- 位置:
reconcile.rs#L1099-L1231。 - 根因与复现:放宽
ModelClosed(Outcome::Error)写ContextOverflow后,已落盘的ModelObserved(ToolCall)先落入reconcile.rs:1099-1231的工具调度循环(排在 line 1531 之前),而tool_incomplete只拦截Outcome::Success + FinishReason::Length。实跑 OpenAI Responses 先发function_call再发context_length_exceeded,事实序列实锤先执行了ToolDecided(Approved) -> ToolAdmitted -> LocalToolReturned -> ToolResultAccepted(Succeeded),随后才RunTerminated(Failed)。 - 最小修法:在
reconcile.rs工具调度入口对已以Outcome::Error+ContextOverflow(或任意不可恢复Outcome::Error)关闭的 attempt 拒绝未 admit 的 tool call,而非先执行再丢弃结果。
4. 测试建议
- 新增的
preset_acceptance.rs测试本身是扎实的行为测试(无需整套重写),建议复用已有quiescent_message辅助精简样板,并补上上述 3 个场景的回归断言/用例。
6bc471d to
86d633b
Compare
|
Thanks — rebased onto current
|
…l output Since #7083 a main-model context overflow interrupts the attempt. When the attempt's partial output never reached the canonical log (no semantic output, no tool calls), compact and continue instead of failing the turn: - Consolidate the safe-overflow check in Attempt::compactable_overflow() across state, engine, and native; OpenAI Responses attempts that already persisted semantic output or tool calls still end the turn. - Reject unadmitted tool calls from ContextOverflow attempts (and wait for Assess while an interrupted attempt has no recovery yet) so doomed overflowed attempts never execute side-effecting tools before failing. - Emit the Host cut-off note on both on-demand overflow summaries and adopted background proactive summaries (Action::AdoptCandidate). - Route Anthropic stream model_context_window_exceeded to ProviderError so it takes the ContextOverflow compaction path instead of output-length continuation.
86d633b to
31c991a
Compare
Summary
#7083 made a Main context overflow always interrupt the attempt. This PR compacts and continues after a mid-response context overflow whenever it is safe:
Attempt::compactable_overflow()checks that the attempt was interrupted byContextOverflow, made no tool calls, and persisted no semantic output to the canonical log — replacing scattered inline checks across state, engine, and native.Action::AdoptCandidate) append the Host cut-off note so the model knows its previous partial text was not delivered.reconcile.rsnow waits for recovery assessment before dispatching tools on an interrupted attempt and rejects unadmitted tool calls onContextOverflowattempts without executing them.model_context_window_exceedednow emitEvent::ProviderErrorso they enterModelError::ContextOverflowcompaction recovery instead of output-length continuation.Verification
cargo fmt --checkcargo test -p maka-provider-protocol --lib(32/32 passed, includinga_stop_at_a_full_context_window_is_a_provider_error)cargo test -p maka-agent-state --lib(51/51 passed)cargo test -p maka-agent-engine --lib(32/32 passed, includinga_tool_call_from_an_overflowed_attempt_is_rejected_without_execution)cargo test -p maka-agent-native --lib context_overflow_after_partial_output_compacts_and_continues(1/1 passed: both background-summary adoption and on-demand overflow summary paths)