Skip to content

feat(agent): re-check session rollover inside the turn loop (#549) - #550

Merged
edenreich merged 2 commits into
mainfrom
feat/agent-rollover-in-loop-549
May 29, 2026
Merged

edenreich merged 2 commits into
mainfrom
feat/agent-rollover-in-loop-549

Conversation

@edenreich

@edenreich edenreich commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #549.

Summary

  • infer agent previously evaluated SessionRolloverManager.ShouldRollover only 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.
  • Chat mode already re-evaluates rollover before every user message at 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.
  • The trigger logic that chat and agent now share is lifted onto *services.SessionRolloverManager as a new MaybeRollover(ctx, model, groupKey) (newID string, fired bool) method so both modes go through the same ShouldRollover → PerformRollover → warn-on-fail path.

Notable design decisions

  • s.completedTurns is preserved across rollover. --max-turns N is a user-set hard ceiling; resetting on every rollover would silently extend it and also disrupt the cadence of injectSystemReminderIfDue. Documented in the doc comment on maybeRollover.
  • The in-loop call sits before injectSystemReminderIfDue. A rollover swaps s.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.
  • The idle trigger is structurally a no-op inside an active loop because every preceding executeTurn appends 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

File Change
internal/services/session_rollover_manager.go New MaybeRollover method
internal/handlers/chat_message_processor.go Consume MaybeRollover; behavior unchanged
cmd/agent.go Two new AgentSession fields (rolloverManager, groupKey); maybeRolloverSession package function becomes (*AgentSession).maybeRollover; new s.maybeRollover() call at the top of the turn-loop body
cmd/agent_test.go TestMaybeRolloverInLoop — four cases covering the issue's acceptance criteria
internal/services/session_rollover_manager_test.go Four cases for the new MaybeRollover method

Known limitations (out of scope for #549, mentioned for follow-up)

  • BackgroundTasksWaiter retains the pre-rollover session id. Constructed once at cmd/agent.go:181-187 with the original newSessionID. 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.
  • Async persistence race window. addMessage at cmd/agent.go:993-1005 kicks off a goroutine for conversationRepo.AddMessage. If a goroutine from turn N is still in-flight when the loop iterates back into maybeRollover and that rollover calls repo.StartNewConversation, the late-arriving message may land in the new conversation file. This race exists pre-change for /compact flows too; no new exposure.

Docs follow-up

Per the org's docs follow-up rule: this is a public-surface change (new MaybeRollover method + new agent runtime behavior). A [DOCS] ticket for inference-gateway/docs will reference this PR.

@edenreich
edenreich requested a review from a team as a code owner May 29, 2026 01:56
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
edenreich force-pushed the feat/agent-rollover-in-loop-549 branch from a029b6f to 23497a7 Compare May 29, 2026 01:56
Comment thread cmd/agent_test.go Resolved
@edenreich
edenreich merged commit a26b9ba into main May 29, 2026
@edenreich
edenreich deleted the feat/agent-rollover-in-loop-549 branch May 29, 2026 11:23
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)
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.114.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Re-check session rollover inside the 'infer agent' turn loop

1 participant