Repository navigation
feat(agent): re-check session rollover inside the turn loop (#549) - #550
Merged
Merged
Conversation
Previously SessionRolloverManager.ShouldRollover was only evaluated at session start. A long agent run could cross both the token-fill (compact.auto_at) and idle (compact.rollover_on_idle_minutes) thresholds without ever producing a new session file with a summary. Chat mode already re-evaluates per user message; this brings agent mode in line. - New SessionRolloverManager.MaybeRollover combines the ShouldRollover gate with PerformRollover and a uniform warn-on-fail fallback that both chat and agent now consume. - maybeRolloverSession becomes a method on AgentSession and is called both at startup and at the top of every turn-loop iteration (before injectSystemReminderIfDue so that on rollover the next reminder lands in the new conversation rather than being summarised away). - s.completedTurns is preserved across rollover so --max-turns stays a hard ceiling.
edenreich
force-pushed
the
feat/agent-rollover-in-loop-549
branch
from
May 29, 2026 01:56
a029b6f to
23497a7
Compare
edenreich
commented
May 29, 2026
edenreich
commented
May 29, 2026
edenreich
commented
May 29, 2026
Co-authored-by: Eden Reich <[email protected]>
inference-gateway-releaser Bot
pushed a commit
that referenced
this pull request
May 29, 2026
## [0.114.0](v0.113.0...v0.114.0) (2026-05-29) ### 🚀 Features * add system reminders to agent mode ([#548](#548)) ([36a44a8](36a44a8)) * **agent:** re-check session rollover inside the turn loop ([#549](#549)) ([#550](#550)) ([a26b9ba](a26b9ba)) ### 🐛 Bug Fixes * chat rollover async ux ([#551](#551)) ([af5fac9](af5fac9)), closes [#549](#549)
Contributor
|
🎉 This PR is included in version 0.114.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Closed
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.
Closes #549.
Summary
infer agentpreviously evaluatedSessionRolloverManager.ShouldRolloveronly at session start (cmd/agent.go:195). A long agent run could cross both the token-fill (compact.auto_at) and idle (compact.rollover_on_idle_minutes) thresholds without ever producing a new session file with a summary — the persisted conversation grew monotonically across e.g. a 50-turn run.internal/handlers/chat_message_processor.go:239-248. This PR brings agent mode in line by re-evaluating rollover at the top of every turn-loop iteration.*services.SessionRolloverManageras a newMaybeRollover(ctx, model, groupKey) (newID string, fired bool)method so both modes go through the sameShouldRollover → PerformRollover → warn-on-failpath.Notable design decisions
s.completedTurnsis preserved across rollover.--max-turns Nis a user-set hard ceiling; resetting on every rollover would silently extend it and also disrupt the cadence ofinjectSystemReminderIfDue. Documented in the doc comment onmaybeRollover.injectSystemReminderIfDue. A rollover swapss.conversation; if the reminder were injected first, it would be summarised away by the rollover that immediately follows. Placing the rollover first means the next reminder cycle lands cleanly in the new conversation.executeTurnappends a fresh-timestamped message — the asymmetry is documented near the call site rather than guarded with dead-code branching, per the issue's "confirm and document the asymmetry, or guard explicitly" criterion.Code changes
internal/services/session_rollover_manager.goMaybeRollovermethodinternal/handlers/chat_message_processor.goMaybeRollover; behavior unchangedcmd/agent.goAgentSessionfields (rolloverManager,groupKey);maybeRolloverSessionpackage function becomes(*AgentSession).maybeRollover; news.maybeRollover()call at the top of the turn-loop bodycmd/agent_test.goTestMaybeRolloverInLoop— four cases covering the issue's acceptance criteriainternal/services/session_rollover_manager_test.goMaybeRollovermethodKnown limitations (out of scope for #549, mentioned for follow-up)
BackgroundTasksWaiterretains the pre-rollover session id. Constructed once atcmd/agent.go:181-187with the originalnewSessionID. After mid-loop rollover, in-flight A2A and background-bash tasks continue to be tracked under the pre-rollover id. Will file a follow-up issue if needed.addMessageatcmd/agent.go:993-1005kicks off a goroutine forconversationRepo.AddMessage. If a goroutine from turn N is still in-flight when the loop iterates back intomaybeRolloverand that rollover callsrepo.StartNewConversation, the late-arriving message may land in the new conversation file. This race exists pre-change for/compactflows too; no new exposure.Docs follow-up
Per the org's docs follow-up rule: this is a public-surface change (new
MaybeRollovermethod + new agent runtime behavior). A[DOCS]ticket forinference-gateway/docswill reference this PR.