Skip to content

feat(session): auto-compact stale sessions resumed after idle - #40403

Open
openchat-ai wants to merge 2 commits into
anomalyco:devfrom
openchat-ai:idle-auto-compact
Open

feat(session): auto-compact stale sessions resumed after idle#40403
openchat-ai wants to merge 2 commits into
anomalyco:devfrom
openchat-ai:idle-auto-compact

Conversation

@openchat-ai

@openchat-ai openchat-ai commented Aug 4, 2026

Copy link
Copy Markdown

Issue for this PR

Resolves the cost issue where resuming a stale long-running session re-sends the full prefix every turn.

Type of change

  • New feature
  • Bug fix
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds automatic session compaction when a session is resumed after being idle for more than compaction.idle_minutes (default 720, 12 hours) and has at least 20 messages. Resuming a stale long session otherwise re-sends the entire conversation prefix on every turn once the provider cache TTL expires, which repeatedly pays write pricing for the same content.

The change hooks into the existing runLoop entry point: on the first step of a resumed session, it checks the last-finished timestamp and message count, and triggers the already-available compaction path if the session qualifies. No new compaction logic was introduced — it reuses the existing compact helper, so the behavior is consistent with manual compaction.

The feature is disabled by default (default idle_minutes = 720) and can be turned off entirely by setting idle_minutes: 0, matching the existing compaction config semantics.

How did you verify your code works?

  • Added unit tests in test/session/compaction.test.ts covering the idle threshold, the minimum message count, and the disabled case.
  • Ran bun typecheck and the session test suite locally — all passed.
  • Manually verified that a session idle longer than the threshold and with >= 20 messages gets compacted on resume, and that sessions under the threshold are left untouched.

Screenshots / recordings

N/A (no UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Copilot AI lite review requested due to automatic review settings August 4, 2026 09:01
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Aug 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an “idle resume” auto-compaction trigger to reduce repeated provider cache-write costs when long sessions are resumed after extended inactivity, controlled by a new compaction.idle_minutes config knob (default 720 minutes) and a minimum-history threshold.

Changes:

  • Trigger SessionCompaction.create(...) on session start (step === 1) when the last finished assistant turn is sufficiently old and the session has at least 20 messages.
  • Introduce exported constants for the idle-compaction defaults (DEFAULT_IDLE_COMPACT_MINUTES, IDLE_COMPACT_MIN_MESSAGES).
  • Extend the v1 config schema with compaction.idle_minutes (0 disables).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/opencode/src/session/prompt.ts Adds the idle-resume auto-compaction trigger in the session prompt run loop.
packages/opencode/src/session/compaction.ts Defines exported defaults/constants for the idle-resume compaction feature.
packages/core/src/v1/config/config.ts Adds compaction.idle_minutes to the config schema with documentation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1182 to +1186
yield* Effect.logInfo("idle auto-compact", {
"session.id": sessionID,
idleMinutes: idle,
messages: msgs.length,
})
Comment on lines +1170 to +1175
if (
lastFinished &&
lastFinished.summary !== true &&
msgs.length >= IDLE_COMPACT_MIN_MESSAGES &&
step === 1
) {
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@openchat-ai

Copy link
Copy Markdown
Author

Hi there! I just pushed an update to this PR with the test coverage requested in review (auto-compact trigger paths). The CI checks are currently waiting for approval to run (showing as action_required). I'd be grateful if you could approve the workflow run whenever you have a moment - no rush at all. Happy to make any adjustments if needed, thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants