Skip to content

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

Description

@edenreich

Summary

infer agent only evaluates SessionRolloverManager.ShouldRollover once, at session start (cmd/agent.go:195 → maybeRolloverSession at cmd/agent.go:247). The multi-turn loop at cmd/agent.go:370 never re-checks, so a long agent run can 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 does the right thing — it re-evaluates rollover on every user message at internal/handlers/chat_message_processor.go:239-248. Agent mode should mirror that.

Why this matters

There are two compaction tiers and only one currently survives the agent loop:

  1. ConversationOptimizer.OptimizeMessages (internal/agent/agent.go:321-323) trims what the LLM sees on every Run. ✅ Works in agent mode — keeps the LLM under context.
  2. SessionRolloverManager.PerformRollover produces a summary and starts a new session file. ❌ Only fires at agent startup; never inside the loop.

Consequence: the LLM stays under context (tier 1), but the persisted conversation file grows monotonically across a 50-turn run, the idle threshold is never enforced mid-run, and the user never gets the fresh-session-with-summary behavior they get in chat mode for the same workload.

Proposed behavior

  • Add a rollover check inside the agent loop in cmd/agent.go:370 — natural placement is right before executeTurn() (parallel to the system-reminder injection added in [FEATURE] Add 'infer conversations show <id>' subcommand #547's branch).
  • Use the same SessionRolloverManager instance and the same groupKey resolved at startup so the new session id is linked to the same group.
  • On rollover: replace s.sessionID, clear s.conversation, reload via s.initializeSession(newID) — same flow maybeRolloverSession already implements.
  • Decide whether to also reset s.completedTurns after a rollover (probably yes, since the rolled-over session is conceptually a fresh start with the summary as turn-0 context).
  • Idempotent and cheap: ShouldRollover is a token estimate + a timestamp check, safe to call every turn.

Acceptance Criteria

  • SessionRolloverManager.ShouldRollover is re-evaluated inside the agent turn loop in cmd/agent.go.
  • When it fires mid-loop, PerformRollover runs, the session id is swapped, the conversation is reloaded, and the loop continues against the new session.
  • Token-trigger rollover fires within ~1 turn of crossing compact.auto_at percent of the model's context window.
  • Idle-trigger rollover never fires inside an agent loop (the agent is actively running, so the idle check is structurally a no-op there) — confirm and document the asymmetry, or guard explicitly to skip the idle path.
  • s.completedTurns behavior across rollover is documented (reset vs. preserved) and matches chat-mode semantics.
  • Test covers: rollover fires mid-loop on token threshold; loop continues against new session id; group key is preserved.

Activity

  1. added
    enhancement
    on May 29, 2026
  2. inference-gateway-releaser commented on May 29, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.114.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions