Skip to content

feat(core): compact each agent at its own window - #61698

Open
cevr wants to merge 1 commit into
anomalyco:devfrom
cevr:feat/agent-compaction-window
Open

cevr wants to merge 1 commit into
anomalyco:devfrom
cevr:feat/agent-compaction-window

Conversation

@cevr

@cevr cevr commented Nov 4, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #61696

Type of change

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

What does this PR do?

Automatic compaction can now plan for an agent's own window. An agent may carry request.settings.compactionWindow (tokens), which a plugin sets in agent.transform. The ceiling for that agent's sessions is reckoned from the smaller of it and the model's window, with the usual buffer. It never raises the model's window and leaves the summary request alone. Agents that share a model can then compact at different sizes.

The change is one function, autoLimit, used where the automatic trigger computes its ceiling.

How did you verify your code works?

  • New test in packages/core/test/session-compaction.test.ts: an agent's window lowers the automatic ceiling for its sessions only. It fails without the change.
  • bun test test/session-compaction.test.ts (36 pass) and bun run typecheck in packages/core.
  • I've run it daily on 2.2.5 with planner/implementor/verifier agents at 400k and workers at 200k on one model.

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Related: #43703, #56744

Automatic compaction plans for the model's window. An agent can now carry
its own, set by a plugin as request.settings.compactionWindow (tokens): the
ceiling for that agent's sessions is reckoned from the smaller of the two,
with the usual reserve. Agents that share a model compact at different sizes.

The trigger runs before any plugin hook in a step, so a plugin that calls
session.compact lands a step late; this keeps the decision in one place.

@opencodejr opencodejr Bot 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.

The change works as described. With compactionWindow: 50000 on an agent, automatic compaction is skipped at 33,999 tokens and runs at 34,000. Without the key, or with a window bigger than the model's, the model's own ceiling still applies. I checked this with the new test, which fails against the base compaction.ts and passes with the change. The full session-compaction suite passes (36 tests), and so does tsc -b for packages/core.

Public API: no typed or documented API changes. The change adds one new key that agent.request.settings accepts, compactionWindow (a positive integer, in tokens), which only lowers the automatic ceiling. It is backwards compatible: agents without the key behave as before. Only plugins (agent.transform) can set it; agent request blocks in config files copy only headers and body.

Main concern: how the setting is exposed (see the inline comment). Otherwise the logic looks sound: it only lowers the limit, leaves the summary request and native compaction alone, and also applies to the background-precompute ceiling.

Related: #43703, #56744

* size to the model's limits.
*/
const autoLimit = (context: SessionContext.Loaded): SessionContext.Loaded["model"]["limit"] => {
const window = context.agent.info.request?.settings?.["compactionWindow"]

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.

request.settings is the provider-request settings bag (Provider.Settings). For models, these settings are passed to the SDK as provider options, and the schema already has a typed compaction field there. Putting an untyped compactionWindow key in the same bag has some downsides:

  • It isn't in any schema or docs, so it can only be found by reading this function.
  • Config can't set it: config/plugin/agent.ts copies only an agent's headers and body.
  • If agent request settings are ever merged into the model's settings, the way headers and body are meant to be, this key would be sent to the provider.

A typed setting would avoid all three. That could be a field on the agent (for example compaction: { window } in the agent schema and config) or a typed entry next to Provider.Settings.compaction. Maintainers should decide which shape they want before this is merged.

This branch has not been deployed

No deployments
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.

[FEATURE]: per-agent compaction window

1 participant