Skip to content

fix(agent): prevent Host stack overflow during history replay - #7019

Merged
M4n5ter merged 2 commits into
apache:rust-previewfrom
sunrioa:codex/fix-bot-history-replay-stack
Nov 19, 2026
Merged

M4n5ter merged 2 commits into
apache:rust-previewfrom
sunrioa:codex/fix-bot-history-replay-stack

Conversation

@sunrioa

@sunrioa sunrioa commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

The main Bot could crash the Rust Host while replaying earlier assistant messages to prepare its next model request. In the observed incident, a discovery worker had already delivered its report. The Host aborted before the Bot made a model call. Each restart picked up the same pending run and hit the same stack overflow, leaving the desktop reconnecting.

Heap-pin the supervised driver phase once per run in Owner::run_with_capabilities. The enclosing cleanup and queue futures now hold a pointer to that state. The driver still runs on the same Tokio task, with the existing cancellation and lease cleanup. The fix works with the default 2 MiB worker stack.

Verification

Check Result
Clean upstream reproduction On macOS arm64 with a debug build of rust-preview at 98a12eb26, the existing replay test aborted on a Tokio worker. Its local model fixture reproduced the same assistant-history replay path seen in the real Host crash. The same test passed at 1572c6798.
Stack use Across 102 matching frames on the crash path, the estimated stack allocation in the debug binaries fell from 2,062.5 KiB to 1,871.2 KiB.
Original workspace recovery The patched desktop reopened the original workspace. The same pending Bot run completed about 50 seconds after launch, with two successful DeepSeek calls and one successful SendUser. The Bot conversation opened normally.
Related checks All 24 Agent engine tests passed. Of 46 selected native tests, 43 passed on the first run. One continuation test timed out and later passed in an isolated comparison on both revisions, bringing the patched result to 44/46 across runs. Formatting and the macOS desktop build passed.

To reproduce on the upstream commit or verify the fix, run the existing regression test on its default worker stack.

cargo test --locked -p maka-agent-native --lib \
  tail_replay_keeps_reasoning_with_tool_calls_and_resets_the_step_producer \
  -- --nocapture

The two remaining native failures are chat_history_reads_a_turns_steps_as_one_growing_group and work_groups_page_cleanly_across_skipped_leading_steps. Both also overflow on clean upstream in the separate code that groups history rows for display.

AI use

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

OpenAI Codex investigated the crash, authored the fix, ran the verification, and drafted this description.

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
中文说明

修改内容

主 Bot 在回放此前的助手消息、准备下一次模型请求时,可能耗尽线程栈并导致 Rust Host 退出。在实际遇到的那一轮中,主动发现 worker 已经把报告交给 Bot。Bot 尚未调用模型,Host 就崩溃了。重启后,Host 会恢复同一条未完成的任务,再次触发崩溃,桌面因此持续重连。

修复在 Owner::run_with_capabilities 中将每轮的驱动执行状态固定在堆上,让外层的清理和队列只持有指针。驱动仍在原 Tokio 任务中执行,沿用现有的取消和租约清理逻辑。默认 2 MiB 工作线程栈即可完成回放。

验证结果

  • 干净上游复现。 在 macOS arm64 的调试构建上,对 rust-preview 提交 98a12eb26 运行上面的既有测试,本机模拟模型即可复现,与真实 Host 崩溃时的助手历史回放路径一致。同一测试在修复提交 1572c6798 上通过。
  • 栈占用。 对比调试二进制里崩溃路径上的 102 个匹配栈帧,估算栈分配从 2,062.5 KiB 降至 1,871.2 KiB。
  • 原工作区恢复。 修复版启动约 50 秒后,先前挂起的同一条 Bot 任务完成。两次 DeepSeek 调用成功,SendUser 成功一次,主 Bot 对话可以正常打开。
  • 相关测试。 Agent 引擎 24 项全部通过。46 项原生用例首次通过 43 项,其中一项续跑用例曾超时,在上游和修复版分别单独复验均通过,修复版累计通过 44 项。剩余两项在干净上游也会栈溢出,发生在界面读取并分组显示历史记录的路径。格式检查和 macOS 桌面构建通过。

sunrioa and others added 2 commits November 19, 2026 12:20
Heap-pin the supervised driver phase once per run so the cleanup and queue futures do not carry its entire state inline. Keep preparation, cancellation and lease cleanup on the original task.

Generated-by: Codex
…ames

Spawn DriverPorts::prepare_step on NativeDriver's tracked worker pool
(self.workers) with an abort-on-drop guard so history replay starts on a
clean Tokio worker stack instead of stacking beneath wake_run, drive_run,
run_with_capabilities, and Engine::drive (~812 KiB in debug builds).

Also box the non-Idle action dispatch and completion polling in
Engine::drive, Engine::drive polling in run_with_capabilities, Phase 1
base resolution + Base::Request in DriverModel::project_history, and the
steering/notification/prompt-preparation phases in prepare_main_step so
neither the orchestration stack nor the replay stack carries mutually
exclusive phases inline.

Measured on macOS arm64 debug (2 MiB Tokio worker stack):
- Replay stack at flush_assistant: 1,423.0 KiB -> 438.9 KiB (-69.2%)
- Outer orchestration stack at reconcile::next: 812.4 KiB -> 445.7 KiB (-45.1%)
- Engine::drive frame: 293.9 KiB -> 114.8 KiB (-60.9%)
- DriverModel::project_history frame: 158.1 KiB -> 55.6 KiB (-64.8%)
@M4n5ter

M4n5ter commented Nov 19, 2026

Copy link
Copy Markdown
Member
English

Pushed follow-up commit 8f3be5357 on top of 1572c6798 to address both underlying causes of the 2 MiB worker stack overflow (the original Box::pin in driver_assembly.rs only reduced the debug replay stack from ~2,062.5 KiB to ~1,871.2 KiB because polling still ran inline beneath ~812 KiB of outer orchestration frames):

  1. Isolate step preparation onto NativeDriver's tracked worker pool (self.workers): DriverPorts::prepare_step now takes self: Arc<Self> and owned RunView, and NativeDriver::prepare_step spawns onto self.workers with an AbortOnDrop guard. History replay now starts on a fresh Tokio worker stack while preserving existing self.cancellation and workers lease-cleanup guarantees.
  2. Shrink oversized async fn frames across both stacks: Boxed non-Idle action dispatch and complete inside Engine::drive, engine.drive polling inside run_with_capabilities, Phase 1 base resolution + Base::Request in DriverModel::project_history, and steering/notification/prompt-prep phases in prepare_main_step / project_step_input.

Measured with lldb on macOS arm64 debug (tail_replay_keeps_reasoning_with_tool_calls_and_resets_the_step_producer, 2 MiB worker stack):

Stack / Frame Before After (8f3be5357) Delta
Replay worker stack at flush_assistant 1,423.0 KiB 438.9 KiB -984.1 KiB (-69.2%)
Outer orchestration stack at reconcile::next 812.4 KiB 445.7 KiB -366.7 KiB (-45.1%)
Engine::drive frame 293.9 KiB 114.8 KiB -179.1 KiB (-60.9%)
DriverModel::project_history frame 158.1 KiB 55.6 KiB -102.5 KiB (-64.8%)
DriverModel::prepare_main_step frame 158.8 KiB 107.0 KiB -51.8 KiB (-32.6%)

Verified locally: cargo fmt --check, maka-agent-engine lib tests (24/24), and maka-agent-native tail_replay + source_recovery suites (7/7).

简体中文

已在 1572c6798 之上追加提交 8f3be5357,从根因上解决 2 MiB worker 线程栈溢出问题(原 driver_assembly.rs 的 Box::pin 只缩小了外层枚举,poll 时仍压在 ~812 KiB 外层编排栈之下,debug 下仅从 ~2,062.5 KiB 降到 ~1,871.2 KiB):

  1. 把 prepare_step 隔离到 NativeDriver 既有的受管 worker 池(self.workers): DriverPorts::prepare_step 改为接收 self: Arc<Self> 与 owned RunView,NativeDriver::prepare_step 通过 self.workers.spawn 配合 AbortOnDrop 守卫在独立 Tokio worker 栈上执行历史回放,完整保留现有的 self.cancellation 与 workers 租约清理保证。
  2. 拆瘦两端巨型 async fn 栈帧: 将 Engine::drive 里的非 Idle 动作分发与 complete、run_with_capabilities 里的 engine.drive、DriverModel::project_history 里的阶段一 base 解析与 Base::Request,以及 prepare_main_step / project_step_input 里的互斥重型阶段分别装箱隔离。

macOS arm64 debug 下用 lldb 实测(tail_replay_keeps_reasoning_with_tool_calls_and_resets_the_step_producer,2 MiB worker 栈):

调用栈 / 栈帧 修复前 修复后(8f3be5357) 降幅
回放 worker 栈(flush_assistant 处) 1,423.0 KiB 438.9 KiB -984.1 KiB (-69.2%)
外层编排栈(reconcile::next 处) 812.4 KiB 445.7 KiB -366.7 KiB (-45.1%)
Engine::drive 单帧 293.9 KiB 114.8 KiB -179.1 KiB (-60.9%)
DriverModel::project_history 单帧 158.1 KiB 55.6 KiB -102.5 KiB (-64.8%)
DriverModel::prepare_main_step 单帧 158.8 KiB 107.0 KiB -51.8 KiB (-32.6%)

本地已通过:cargo fmt --check、maka-agent-engine 全部单测(24/24)、maka-agent-native 的 tail_replay 与 source_recovery 回归套件(7/7)。

@M4n5ter
M4n5ter merged commit 0ce742e into apache:rust-preview Nov 19, 2026
7 of 8 checks passed
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.

2 participants