Skip to content

refactor(config): consolidate context budget to one per-model knob - #124665

Merged
steipete merged 10 commits into
mainfrom
steipete/context-tokens-one-knob
Aug 16, 2026
Merged

steipete merged 10 commits into
mainfrom
steipete/context-tokens-one-knob

Conversation

@steipete

@steipete steipete commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

OpenClaw had six configuration paths that could set a model's effective context budget: provider-level contextTokens and contextWindow, per-model contextTokens and contextWindow, agents.defaults.contextTokens, and agents.entries.<id>.contextTokens. One number, six knobs. The resolution ladder merging them needed exact-vs-normalized provider matching, a sourceCfg side-channel solely to distinguish operator-authored contextWindow values from the 200k default that normalization used to bake into every model entry, and agent-cap override plumbing threaded through eight production files.

Two latent bugs lived in that complexity, fixed here:

  • Status reporting could cap a runtime-discovered context window (e.g. Codex-reported 272k) with the generic 200k fallback because the default was applied before runtime candidates were exhausted.
  • The native slash-command fast path passed agentCfg?.contextTokens ?? 0 — literally zero context tokens — whenever no agent-level cap was configured.

Why This Change Was Made

Maintainer decision (steipete): consolidate to one knob pair. Every comparable product (Claude Code, Codex CLI, opencode, LiteLLM) exposes exactly one per-model context limit; none has a multi-level ladder.

  • Kept: models.providers.<p>.models[].contextTokens (active-input cap) and models.providers.<p>.models[].contextWindow (native window declaration).
  • Removed: provider-level contextTokens/contextWindow, agents.defaults.contextTokens, per-agent-entry contextTokens.
  • Normalization no longer bakes a 200k contextWindow into model entries, which deletes the sourceCfg authored-vs-filled hack outright.
  • New resolution order: authored per-model cap (bounded by the Anthropic fixed contract) → fixed contract → discovered/runtime values → authored window → caller fallback.

Old configs keep booting: a raw pre-validation migration (same load-boundary pattern as the persisted-roster migration) bakes provider-level values into explicit model entries and strips retired keys in-memory before strict Zod validation. openclaw doctor --fix persists the same migration to disk, reporting every moved/removed key, and the retirement is registered in the doctor deprecation-compat inventory.

User Impact

  • Operators with provider-level context values and explicit model entries: behavior preserved (values are baked into the entries by doctor/load migration).
  • Operators with provider-level values but no explicit model entries, or agent-level caps: the key is removed with a warning naming the replacement (models.providers.<p>.models[].contextTokens). This capability narrowing is an explicit maintainer decision.
  • No gateway startup breakage: un-migrated configs load normally; doctor persists the cleanup.
  • Docs updated across model-providers, config-agents, config-tools, local-models, ollama, openai, and hooks pages.

Evidence

  • Focused suites: 326 files / 5,435 tests passed (agents context, config, doctor, gateway session-utils, auto-reply lanes) via node scripts/run-vitest.mjs.
  • Migration tests cover: bake case (budget identical pre/post), remove+warn cases, all three agent surfaces, idempotency, and old-config-loads-inert pre-doctor via the normal load path.
  • pnpm build pass; pnpm docs:check-config-examples 768 files / 1,174 fences pass; pnpm tsgo:core:test pass.
  • Fresh autoreview (Codex gpt-5.6-sol, high): clean, no accepted findings, overall 0.97.
  • LOC: production/docs +396/−556 (net −160); tests +715/−1,099 (net −384); total net −544.
  • check:assertion-safety note: the server.auth.control-ui.pairing.suite.ts: 6 < 11 line is a shrink advisory (exit 0) from main's refactor: replace runtime mock seams with dependency injection #124634 refactor; file and baseline untouched here.
  • check-lint red on earlier runs was main-owned breakage from fix: avoid nesting dedicated Linux workers #124636 (scripts/check-changed.mts boolean-literal-compare), fixed in-flight by fix(config): correct compaction mode help text; repair main lint + qa-lab flake #124656 and picked up here via rebase — not part of this diff.

@clawsweeper

clawsweeper Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime cli CLI command changes commands Command implementations agents Agent runtime and tooling extensions: lmstudio labels Aug 16, 2026
@steipete
steipete marked this pull request as ready for review August 16, 2026 15:25
@openclaw-barnacle openclaw-barnacle Bot added size: XL maintainer Maintainer-authored PR labels Aug 16, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 16, 2026
@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 16, 2026, 12:46 PM ET / 16:46 UTC.

ClawSweeper review

What this changes

The branch consolidates context-budget configuration around per-model context windows and migrates retired provider- and agent-level caps.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open: the refactor makes a package-exported Plugin SDK field optional without a versioned compatibility path, which can break existing provider plugins that treat it as a number.

Priority: P1
Reviewed head: bbe8dcb9fe5fe4cc89e44c865d2757ce5f18092c
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The refactor has substantial validation claims, but its public SDK compatibility break must be resolved before it is quality-ready.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored internal refactor is outside the external-contributor real-behavior-proof gate.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored internal refactor is outside the external-contributor real-behavior-proof gate.
Evidence reviewed 4 items Published required contract: Current main declares ModelDefinitionConfig.contextWindow as required, and the supplied PR diff changes this exact field to optional.
Public SDK exposure: The public provider-onboard and provider-model-types entrypoints re-export ModelDefinitionConfig; provider-onboard is package-exported.
SDK compatibility policy: The applicable SDK guide requires owner acceptance before making a shipped type source-incompatible and requires versioned/documented breaks for public third-party APIs.
Findings 1 actionable finding [P1] Preserve the required Plugin SDK context-window contract
Security None None.

How this fits together

Provider and model configuration feeds context-budget resolution, model catalogs, and status output. External provider plugins can consume the same model-definition type through OpenClaw’s Plugin SDK.

flowchart LR
  Config[Operator and plugin model config] --> Loader[Config loading and migration]
  Loader --> Resolver[Context budget resolution]
  Resolver --> Runtime[Agent runtime and status]
  Config --> SDK[Published Plugin SDK types]
  SDK --> Plugins[External provider plugins]
Loading

Decision needed

Question Recommendation
Should external Plugin SDK consumers retain a required contextWindow, or should OpenClaw intentionally publish an optional replacement with a versioned migration path? Preserve the required SDK field: Normalize omitted transport-only input before it reaches the exported model-definition contract.

Why: This changes a package-exported SDK type, so compatibility policy cannot safely be inferred from the internal refactor alone.

Before merge

  • Preserve the required Plugin SDK context-window contract (P1) - The branch changes the package-exported ModelDefinitionConfig.contextWindow from number to optional. provider-onboard and provider-model-types re-export this type, so existing external plugins that consume the field as a number become source-incompatible and may receive undefined. Keep the public projection required or add a versioned, documented migration path.
  • Resolve merge risk (P1) - Merging the optional public field can make external provider plugins fail typechecking or receive an absent context window where their current contract guarantees a number.

Findings

  • [P1] Preserve the required Plugin SDK context-window contract — src/config/types.models.ts:185
Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Production versus test LOC production/docs net -160; tests net -384 The PR body reports a net reduction, but the exported SDK type change remains compatibility-sensitive.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/config/io.compat.test.ts, migration/backfill/repair: src/config/legacy.context-budget.test.ts, persistent cache schema: src/agents/context.test.ts, serialized state: src/agents/command/session-store.ts, serialized state: src/config/io.compat.test.ts, serialized state: src/gateway/session-utils-model.ts, and 6 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Restore the stable SDK contract (recommended)
    Keep contextWindow required in the package-exported model definition and contain optionality in an internal or input-only type.
  2. Publish a versioned migration
    If optionality is intentional, document and version the public contract before external plugin consumers must adapt.

Technical review

Best possible solution:

Keep the external model-definition projection required, or introduce a separately versioned optional input contract with documented migration support.

Do we have a high-confidence way to reproduce the issue?

Yes—an external plugin that assigns model.contextWindow to a number is source-reproducible as broken once this exported field becomes optional.

Is this the best way to solve the issue?

No—the internal simplification should not silently weaken a public Plugin SDK contract; preserve the required projection or version the new contract.

Full review comments:

  • [P1] Preserve the required Plugin SDK context-window contract — src/config/types.models.ts:185
    The branch changes the package-exported ModelDefinitionConfig.contextWindow from number to optional. provider-onboard and provider-model-types re-export this type, so existing external plugins that consume the field as a number become source-incompatible and may receive undefined. Keep the public projection required or add a versioned, documented migration path.
    Confidence: 0.96

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against eb63cb148115.

Labels

Label changes:

  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored internal refactor is outside the external-contributor real-behavior-proof gate.
  • remove status: 👀 ready for maintainer look: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P1: The published model-definition type can break existing external provider plugins at compile time and weaken their runtime contract.
  • merge-risk: 🚨 compatibility: Changing a package-exported required field to optional alters an existing third-party Plugin SDK contract.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored internal refactor is outside the external-contributor real-behavior-proof gate.

Evidence

Acceptance criteria:

  • [P1] Compile a representative external Plugin SDK consumer against the preserved or versioned model definition.
  • [P1] Verify transport-only model input is normalized before any public runtime consumer receives it.

What I checked:

Likely related people:

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Keep the public field required or publish a versioned migration contract.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (3 earlier review cycles)
  • reviewed 2026-08-16T15:41:12.629Z sha a24cdb322146817a038fdba1ff61f8ad39003170 :: found issues before merge. :: [P1] Preserve catalog context metadata for transport-only inline models | [P1] Preserve mixed per-model/provider context precedence | [P2] Emit the retirement warning during normal config loads
  • reviewed 2026-08-16T16:04:58.232Z sha 0a43376 :: needs changes before merge. :: [P1] Preserve catalog context metadata for transport-only inline models | [P2] Emit retirement warnings during normal configuration loads
  • reviewed 2026-08-16T16:36:38.580Z sha d046a8d :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the steipete/context-tokens-one-knob branch from 85c72a4 to 0a43376 Compare August 16, 2026 15:57
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 16, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 16, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 16, 2026
@steipete
steipete merged commit 715c379 into main Aug 16, 2026
217 checks passed
@steipete
steipete deleted the steipete/context-tokens-one-knob branch August 16, 2026 17:05
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 17, 2026
…penclaw#124665)

* refactor(config): consolidate context budget settings

* test(config): type legacy context fixtures

* test(config): align context budget fixtures

* fix(status): honor runtime context discovery

* docs(config): clarify context budget fallbacks

* fix(ci): resolve context budget lint failures

* test(ci): align context budget shard fixtures

* fix(models): preserve catalog context metadata

* fix(config): surface context migration diagnostics

* test(plugin-sdk): keep live catalog coverage focused
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling cli CLI command changes commands Command implementations docs Improvements or additions to documentation extensions: github-copilot extensions: lmstudio gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XL status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant