feat(acp): session rotation on context window exhaustion#155
Merged
wesbillman merged 3 commits intomainfrom Mar 22, 2026
Merged
feat(acp): session rotation on context window exhaustion#155wesbillman merged 3 commits intomainfrom
wesbillman merged 3 commits intomainfrom
Conversation
When an agent hits MaxTokens or MaxTurnRequests, the channel session is now invalidated so the next prompt creates a fresh one instead of reusing the bloated session. Also adds proactive turn-based rotation via SPROUT_ACP_MAX_TURNS_PER_SESSION (default 0 = disabled). When set, sessions are rotated after N successful turns before hitting the context limit. Refactored session invalidation into OwnedAgent::invalidate_session() and invalidate_all_sessions() helpers for consistency across all code paths. Changes: - pool.rs: Session rotation logic in run_prompt_task Ok(Ok()) arm, turn counters on OwnedAgent, invalidation helpers, PromptContext field - config.rs: New max_turns_per_session CLI arg and Config field - main.rs: Wire PromptContext and OwnedAgent construction sites
Fix 4 post-prompt error paths that cleared sessions without clearing turn counters, causing stale counts to leak across session lifetimes. Extract SessionState from OwnedAgent so the session/turn-counter state machine is testable without spawning a real agent subprocess. All session mutation now goes through SessionState methods — no raw field access outside the impl. Methods: - invalidate(source) — clear one channel or heartbeat - invalidate_channel(id) -> bool — clear one channel, return whether it existed - invalidate_all() — clear everything (agent exit) Also fix: handle_prompt_result's channel-removal now uses invalidate_channel() instead of raw retain(). 8 unit tests cover all SessionState methods and edge cases.
Collaborator
|
Pushed a fix commit on top: What changed: The PR correctly migrated the pre-prompt error paths in The fix:
This comment was written by AI (goose). |
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
When an ACP agent hits
MaxTokensorMaxTurnRequests(context window exhaustion), the session was previously returned to the pool unchanged — subsequent prompts would re-enter the bloated session and fail or degrade. This PR adds automatic session rotation to handle context window exhaustion gracefully.Changes
Reactive Session Rotation (
pool.rs)StopReason::MaxTokensorStopReason::MaxTurnRequestsis returned, the channel session is invalidated so the next prompt creates a fresh oneProactive Turn-Based Rotation (
config.rs,pool.rs,main.rs)SPROUT_ACP_MAX_TURNS_PER_SESSION(CLI:--max-turns-per-session, default:0= disabled)1for "fresh session per task" mode — agents use channel history (context_message_limit) as context instead of accumulating in-session stateRefactored Session Invalidation (
pool.rs)OwnedAgent::invalidate_session()andOwnedAgent::invalidate_all_sessions()helper methodssessions.remove()/sessions.clear()/heartbeat_session = Nonepatterns throughoutrun_prompt_task()with the new helpersLogging
MaxTokensandMaxTurnRequestslog messages now note upcoming session rotationinfolevel with source and stop reasonFiles Changed
crates/sprout-acp/src/pool.rs— rotation logic, turn counters, invalidation helpers,PromptContextfieldcrates/sprout-acp/src/config.rs— new CLI arg, Config field, summary stringcrates/sprout-acp/src/main.rs— wired new fields into all 3OwnedAgentconstruction sites andPromptContextTesting
max_turns_per_session=0preserves existing behavior exactly