Skip to content

feat(core): allow per-model compaction buffer - #56443

Open
PzaThief wants to merge 2 commits into
anomalyco:devfrom
PzaThief:compaction-model-buffer
Open

PzaThief wants to merge 2 commits into
anomalyco:devfrom
PzaThief:compaction-model-buffer

Conversation

@PzaThief

@PzaThief PzaThief commented Oct 17, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #43703

Type of change

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

What does this PR do?

Adds an optional buffer to the existing per-provider/model compaction policy:

{
  "providers": {
    "openai": {
      "models": {
        "gpt-4.1": { "settings": { "compaction": { "buffer": 40000 } } }
      }
    }
  }
}

The effective buffer is the model setting, then the provider setting, then the top-level compaction.buffer. This lets models with different context windows keep independent headroom instead of sharing one global value. buffer remains an absolute token count; percentage scaling was intentionally not added.

How did you verify your code works?

  • bun test test/config/provider.test.ts test/config/compaction.test.ts test/session-compaction.test.ts in packages/core (49 pass)
  • bun test test/model.test.ts in packages/schema (9 pass)
  • bun run typecheck in packages/core, packages/schema, and packages/client
  • bunx oxlint on changed files
  • Regenerated packages/client types and packages/protocol/openapi.json; bun run check:generated in packages/protocol passes.
  • The pre-push full bun run check fails on packages/app/src/custom-elements.d.ts because this Windows checkout has symlinks disabled, unrelated to this change.

Screenshots / recordings

N/A — not a UI change.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Oct 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent opencode-agent 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.

This works as described, but one generated file needs updating before it's ready. I checked it with the core config, compaction and shared-schema tests and the schema model tests (all pass) and with type checks of packages/core and packages/schema (pass). I also read through the settings merge: Provider.mergeOverlay merges provider and model settings deeply, so a model { buffer } keeps the provider's type, and the order model → provider → top-level compaction.buffer holds. Using the session model's buffer for the "is it due" check and the compaction model's buffer for the summary budget is consistent, since each is measured against that model's own window.

Public API:

  • Config: adds settings.compaction.buffer (non-negative integer) to providers and models. Existing configs still validate.
  • Schema and SDK: Provider.Compaction / ProviderCompaction changes from { type: "summary" } | { type: "native" } to { type?: "summary" | "native"; buffer?: number }. Configs are backwards compatible, and a missing type still means summary. TypeScript code that read compaction.type as always set now has to handle undefined.
  • The OpenAPI document wasn't regenerated (see the inline comment).

Concern: packages/protocol/openapi.json is stale, so bun run check:generated in packages/protocol fails with this change but passes on the base.

Comment thread packages/schema/src/provider.ts Resolved
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Oct 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@pma1999

pma1999 commented Nov 5, 2026

Copy link
Copy Markdown

Tested this branch locally against head 926ae54 (Windows, Bun 1.4.0):

  • packages/schema: full suite 69 pass / 0 fail; tsgo --noEmit clean.
  • packages/core: session-compaction.test.ts + config/provider.test.ts 48 pass / 0 fail; tsgo -b clean.

The model-level buffer overrides the global value in due() and survives the provider/model settings merge, which covers the mixed-window case (1M model with buffer: 550000 compacts at 450K; 200K model with buffer: 50000 compacts at 150K).

One rebase note: current dev has moved the buffer into selectModel(...), so this needs a refresh before it can merge (mergeable_state: dirty).

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-model compaction threshold (reserved/buffer)

2 participants