fix(session): prune turns after each runTurn to prevent heap OOM - #214
Open
yablokolabs wants to merge 1 commit into
Open
fix(session): prune turns after each runTurn to prevent heap OOM#214yablokolabs wants to merge 1 commit into
yablokolabs wants to merge 1 commit into
Conversation
…mory Issue AtomicBot-ai#121: TUI crashes with heap OOM after 25+ tool-call steps because SessionState.turns grows unbounded — every assistant_tool_call, tool_result, and assistant_reply is appended forever. Large tool outputs (file reads, web fetches) push long sessions past the V8 heap limit. The TUI side (feed, messages, reasoning) was already bounded via pushRing with ringBufferSize. The real culprit was turns[]. Add pruneSessionTurns() which applies the same packConversation token-based windowing at the persistence boundary: after runTurn returns, old turns beyond the conversationMaxTokens budget are dropped and replaced with a compact summary line. The next prompt build sees the pruned state and packConversation computes a fresh summary from what remains. 5 new tests covering: within-budget passthrough, over-budget pruning, empty input, immutability guarantee, large-turn-list pruning.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #121.
SessionState.turns[]grows unbounded — everyassistant_tool_call,tool_result, andassistant_replyis appended forever. Large tool outputs (file reads, web fetches) in a 25+ step session push the V8 heap past 4 GB.The TUI side (
feed,messages,reasoning) was already bounded viapushRingwithringBufferSize. The real culprit wasturns[].Fix
packConversationalready computes the token-budgeted visible window at prompt-build time. This PR applies the same windowing at the persistence boundary: afterrunTurnreturns,pruneSessionTurns()drops old turns beyondconversationMaxTokensand replaces them with a compact summary line. The next prompt build sees the pruned state;packConversationcomputes a fresh summary from what remains.The pruning is zero-cost when turns already fit within budget (returns a copy of the input array). It only activates when the accumulated turns exceed the token cap.
Changes
src/session/conversation-turn.ts— newpruneSessionTurns(turns, maxTokens)pure functionsrc/runtime/bootstrap.ts— callpruneSessionTurnsbeforesessionStore.saveinexecuteTurnsrc/session/index.ts— export the new functionTest coverage
5 new tests:
Verification
npm run lintcleansend-message-concurrency,llm-health-poller)