Skip to content

feat(compaction): enforce 384k active budget ceiling and sticky generation latch for request-shape stability - #1095

Open
codeg-dev wants to merge 7 commits into
code-yeongyu:mainfrom
codeg-dev:feat/context-budget-policy
Open

feat(compaction): enforce 384k active budget ceiling and sticky generation latch for request-shape stability#1095
codeg-dev wants to merge 7 commits into
code-yeongyu:mainfrom
codeg-dev:feat/context-budget-policy

Conversation

@codeg-dev

@codeg-dev codeg-dev commented Aug 23, 2026

Copy link
Copy Markdown

Summary

Separates physical context window from operational working-set budget (ContextBudgetPolicy) in core compaction and builtin compaction extensions, enforcing a 384k active ceiling for 1M models and a sticky generation latch to prevent request prefix shape oscillations.

Key Changes

  1. ContextBudgetPolicy (384k Active Ceiling & 288k Warmup):

    • Sets initial operational active ceiling maxActiveContextTokens = 384_000 for 1M-class models (contextWindow >= 500_000), regardless of adaptive ratio resulting in 650k.
    • Speculative warmup begins at 384_000 * 0.75 ≈ 288_000 tokens.
    • 1M model default keepRecentTokens = 35_000 (explicit user settings take precedence).
    • Preserves standard behavior for 200k/smaller models without forced 384k ceiling.
  2. Request-Shape Sticky Generation Latch:

    • Once context reduction engages (e.g. at 50% usage), the transform stays latched across the current compaction generation.
    • Usage oscillations (e.g. 501k → 499k → 505k) do not alternate the historical request prefix shape, protecting prompt cache hits.
    • Latch releases only on accepted & persisted session_compact or session tree reload/reset.
  3. Context Pressure Metric Interface:

    • Introduces typed ContextPressure interface with activeTokens, requestBodyBytes, and generation for non-token metric tracking.

Verification

  • packages/coding-agent/test/compaction/policy.test.ts: 5/5 PASS (200k compatibility, 1M 384k/288k matrix, user overrides).
  • packages/coding-agent/test/compaction/context-reduction.test.ts: 9/9 PASS (collapse, shrink, clear, sticky generation latch & prefix stability).
  • packages/coding-agent/test/compaction.test.ts: 43/43 PASS.
  • Full compaction test suite: 57/57 files PASS.
  • npm run check: 0 errors (biome, shrinkwrap, install-lock, tsc --noEmit, browser-smoke).
  • Full monorepo build: npm run build PASS.

Summary by cubic

Enforces a 384k active-context ceiling for 1M-class models and adds a branch-scoped sticky context-reduction latch to stabilize request prefix shape and preserve cache hits. Previously we used a 0.65 ratio (~650k on 1M) and reduction could oscillate; now blocking caps at 384k, speculative warmup starts at 288k, and reduction stays latched until compaction is accepted or the model or session branch changes.

  • Policy: ContextBudgetPolicy decouples physical window and active budget. 1M defaults to maxActiveContextTokens=384k, keepRecentTokens=35k, warmup at 75%, reserveTokens=16,384; standard models keep keepRecentTokens=20k. Users can override via settings compaction.maxContextTokens and compaction.keepRecentTokens; explicit overrides (including zero) are preserved.
  • Threshold plumbing and final admission: core and harness shouldCompact() honor the active ceiling; computeEffectiveBlockingThresholdTokens() caps adaptive thresholds; the final provider admission path now enforces the same ceiling before dispatch; speculative warmup derives from the policy.
  • Sticky latch: builtin compaction engages once over the gate and remains active; resets on accepted session_compact, session_tree, and model_select. Provider-native compaction stays excluded.
  • Migration: to keep 20k retention on 1M models, set compaction.keepRecentTokens: 20000. To lower the active ceiling, set compaction.maxContextTokens.

Written for commit 2cf8c81. Summary will update on new commits.

Review in cubic

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31f29200ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/coding-agent/src/core/extensions/builtin/compaction/policy.ts Outdated
Comment thread packages/agent/src/harness/compaction/compaction.ts
@codeg-dev

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31f29200ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/coding-agent/src/core/compaction/compaction.ts
@codeg-dev

Copy link
Copy Markdown
Author

@codex review

@codeg-dev

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c9837c5acd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/coding-agent/src/core/settings-manager.ts Outdated
Comment thread packages/coding-agent/src/core/extensions/builtin/compaction/policy.ts Outdated
…eat/context-budget-policy

# Conflicts:
#	packages/agent/src/harness/compaction/compaction.ts
#	packages/coding-agent/src/core/extensions/builtin/compaction/index.ts
#	packages/coding-agent/src/core/extensions/builtin/compaction/policy.ts
#	packages/coding-agent/src/core/settings-manager.ts
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.

1 participant