Repository navigation
fix(agent): prevent Host stack overflow during history replay - #7019
Conversation
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%)
EnglishPushed follow-up commit
Measured with
Verified locally: 简体中文已在
macOS arm64 debug 下用
本地已通过: |
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
rust-previewat98a12eb26, 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 at1572c6798.SendUser. The Bot conversation opened normally.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 \ -- --nocaptureThe two remaining native failures are
chat_history_reads_a_turns_steps_as_one_growing_groupandwork_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
OpenAI Codex investigated the crash, authored the fix, ran the verification, and drafted this description.
Checklist
Does this PR entail a change in behavior?
中文说明
修改内容
主 Bot 在回放此前的助手消息、准备下一次模型请求时,可能耗尽线程栈并导致 Rust Host 退出。在实际遇到的那一轮中,主动发现 worker 已经把报告交给 Bot。Bot 尚未调用模型,Host 就崩溃了。重启后,Host 会恢复同一条未完成的任务,再次触发崩溃,桌面因此持续重连。
修复在
Owner::run_with_capabilities中将每轮的驱动执行状态固定在堆上,让外层的清理和队列只持有指针。驱动仍在原 Tokio 任务中执行,沿用现有的取消和租约清理逻辑。默认 2 MiB 工作线程栈即可完成回放。验证结果
rust-preview提交98a12eb26运行上面的既有测试,本机模拟模型即可复现,与真实 Host 崩溃时的助手历史回放路径一致。同一测试在修复提交1572c6798上通过。SendUser成功一次,主 Bot 对话可以正常打开。