Skip to content

fix: cap session context overrides by model window#90889

Merged
steipete merged 11 commits into
mainfrom
clawsweeper/gitcrawl-13603-switching-to-a-smaller-context-model-mid-session-causes-hard-cra
Jun 14, 2026
Merged

fix: cap session context overrides by model window#90889
steipete merged 11 commits into
mainfrom
clawsweeper/gitcrawl-13603-switching-to-a-smaller-context-model-mid-session-causes-hard-cra

Conversation

@clawsweeper

@clawsweeper clawsweeper Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Verification

  • node scripts/run-vitest.mjs src/agents/command/session-store.test.ts src/agents/context.lookup.test.ts src/agents/context.test.ts src/agents/embedded-agent-runner/model.static-catalog.test.ts src/auto-reply/reply/directive-handling.model.test.ts src/auto-reply/reply/model-selection.test.ts src/auto-reply/status.test.ts src/commands/status.scan-overview.test.ts src/commands/status.scan.config-shared.test.ts src/commands/status.summary.runtime.test.ts src/commands/status.summary.test.ts src/commands/status.test.ts src/config/io.best-effort.test.ts src/gateway/server-reload-handlers.test.ts (441 assertions)
  • pnpm tsgo
  • pnpm check:test-types
  • node scripts/run-oxlint.mjs $(git diff --name-only origin/main...HEAD -- '*.ts')
  • AWS Crabbox changed gate and build: run run_27f365e25fd2, lease cbx_66c09737cd1a (golden-crayfish), exit 0

Real behavior proof

Behavior addressed: Oversized context overrides and stale model metadata could survive model switches/reloads and make persisted or displayed session context exceed the selected model's known window.

Real environment tested: macOS with official OpenAI Responses and Anthropic Messages APIs; isolated local OpenClaw agent/session/status flow; AWS Linux Crabbox/Testbox.

Exact steps or command run after this patch: Forced one official tool call plus follow-up text with OpenAI gpt-5.5 and Anthropic claude-sonnet-4-6; ran OPENCLAW_LIVE_TEST=1 node scripts/test-live.mjs --no-quiet src/agents/models.profiles.live.test.ts; ran an isolated OpenClaw turn with a 64,000-token configured ceiling and inspected persisted session metadata plus status; ran the AWS changed gate and build listed above.

Evidence after fix: Both official-provider forced tool calls returned the requested noop call and follow-up text; all 37 live profile tests passed for both models, including prompt, file-read, and image probes; persisted and status context both resolved to 64,000; AWS changed gate and build completed successfully.

Observed result after fix: Effective context never exceeds the selected model's known window, model switches carry current metadata, and official OpenAI/Anthropic tool calling remains functional.

What was not tested: Live APIs for other providers and native cross-OS application flows. Their metadata paths are covered by focused unit/catalog tests, TypeScript checks, lint, and the Linux remote build.

Dependency proof

  • Anthropic registration and fixed-window handling checked in extensions/anthropic/register.runtime.ts and extensions/anthropic-vertex/provider-catalog.ts.
  • OpenAI Codex model-list context-window behavior checked directly in sibling Codex source at codex-rs/codex-api/src/endpoint/models.rs on b89ce9a2bc.

Fixes #39857.
Related source PR: #39859.

Co-authored-by: Kros Dai [email protected]

@clawsweeper clawsweeper Bot added agents Agent runtime and tooling clawsweeper Tracked by ClawSweeper automation labels Jun 6, 2026
@clawsweeper

clawsweeper Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Codex review: needs changes before merge. Reviewed June 13, 2026, 9:23 PM ET / 01:23 UTC.

Summary
Caps effective session context overrides by known model windows and refactors provider-aware context metadata resolution across agent persistence, status, config snapshots, model catalogs, and gateway reloads.

PR surface: Source +494, Tests +1473. Total +1967 across 34 files.

Reproducibility: yes. Source inspection of current main shows resolveContextTokensForModel returns a positive override before model-window lookup, which can overstate a known smaller model window.

Review metrics: 3 noteworthy metrics.

  • PR Surface: +2724/-757 across 34 files. The fix now spans resolver, config, status, session persistence, auto-reply, catalog, and gateway reload paths, so review needs whole-surface evidence rather than a narrow diff read.
  • Config Snapshot Surface: 1 read-only snapshot facade added, 0 user config keys. The PR changes config-loading semantics without adding a user option; maintainers should verify authored-source projection stays read-only.
  • Runtime Entry State: 1 runtime entry now unused. Exact-head dependency proof shows a published runtime entry became unreferenced after the refactor and must be removed or intentionally preserved.

Stored data model
Persistent data-model change detected: persistent cache schema: src/agents/context-resolution.ts, persistent cache schema: src/agents/context-runtime-state.ts, persistent cache schema: src/agents/context.test.ts, persistent cache schema: src/agents/context.ts, persistent cache schema: src/auto-reply/reply/model-selection.test.ts, persistent cache schema: src/auto-reply/status.test.ts, and 6 more. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦐 gold shrimp
Proof: 🦞 diamond lobster
Patch quality: 🦐 gold shrimp
Result: needs maintainer review before merge.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Remove or intentionally restore the agents/models-config.runtime runtime boundary and rerun the dependency/deadcode gate.
  • [P2] Keep the current live/session proof attached to the exact repaired head before merge.

Risk before merge

  • [P1] Existing users with an explicit context-token override above a known selected-model limit will see a smaller effective value in status and persisted session metadata after merge.
  • [P1] The PR currently leaves a runtime entry published but unused; landing without resolving it keeps the dependency gate red and leaves unclear runtime-boundary intent.
  • [P1] The OpenGrep failure points at a pre-existing test helper pattern in a changed file, so it should be handled separately unless maintainers want this PR to clean the helper too.

Maintainer options:

  1. Fix The Runtime Entry Blocker (recommended)
    Remove the stale agents/models-config.runtime entry and tests if no shipped runtime contract remains, or restore a real production boundary before merge.
  2. Accept The Context Cap Change
    Maintainers can intentionally accept that configured oversized context ceilings will now be reduced to known model windows in status and persisted sessions.
  3. Pause For Override Policy
    Pause the PR if maintainers want explicit product policy for preserving old oversized override semantics instead of treating this as a correctness fix.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Fix the dependency/deadcode failure introduced by this PR. The refactor removed the production `modelsConfigRuntimeLoader` import/use, leaving `src/agents/models-config.runtime.ts` only referenced by packaging/tests. If no shipped runtime contract remains for `agents/models-config.runtime`, delete the stale runtime file, remove the dist entry from `tsdown.config.ts`, and update `src/infra/tsdown-config.test.ts` plus related snapshot coverage. If the stable runtime boundary is still required, restore a real production import/use instead. Preserve the context-window cap behavior and validate the dependency/deadcode check.

Next step before merge

  • [P2] A bounded repair can address the stale runtime entry and dependency failure without changing the core context-window behavior.

Security
Cleared: No new production security or supply-chain concern was found; the OpenGrep hit is an unchanged test-helper env override pattern, not a diff-added secret or permission path.

Review findings

  • [P2] Remove or restore the stale models-config runtime — src/agents/context-runtime-state.ts:15-22
Review details

Best possible solution:

Resolve the stale runtime entry, then land the shared-resolver fix with explicit maintainer acceptance that oversized overrides are ceilings, not forced session windows.

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

Yes. Source inspection of current main shows resolveContextTokensForModel returns a positive override before model-window lookup, which can overstate a known smaller model window.

Is this the best way to solve the issue?

Mostly yes. The shared resolver is the right owner for status and persistence, but the branch is not merge-ready until the stale runtime entry/dependency failure is resolved.

Full review comments:

  • [P2] Remove or restore the stale models-config runtime — src/agents/context-runtime-state.ts:15-22
    This refactor removes the modelsConfigRuntimeLoader production import/use, but agents/models-config.runtime is still a published tsdown entry and now only appears in snapshot/config tests. The exact-head dependency check reports src/agents/models-config.runtime.ts as an unexpected unused file, so the branch cannot land until the runtime entry is either deleted from packaging/tests or deliberately kept via a real production boundary.
    Confidence: 0.93

Overall correctness: patch is incorrect
Overall confidence: 0.88

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. PR body and maintainer comment provide after-fix live provider, isolated session/status, and AWS Crabbox proof for the current head.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): PR body and maintainer comment provide after-fix live provider, isolated session/status, and AWS Crabbox proof for the current head.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.
  • remove status: needs maintainer proof decision: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P2: This is a normal-priority correctness fix for inaccurate effective context accounting with limited blast radius, but it has a concrete merge blocker.
  • merge-risk: 🚨 compatibility: Existing configurations with an oversized context override will observe a lower effective value once a smaller selected-model window is known.
  • merge-risk: 🚨 session-state: The corrected effective window is persisted to session metadata and consumed by status and downstream session accounting.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (live_output): PR body and maintainer comment provide after-fix live provider, isolated session/status, and AWS Crabbox proof for the current head.
  • proof: sufficient: Contributor real behavior proof is sufficient. PR body and maintainer comment provide after-fix live provider, isolated session/status, and AWS Crabbox proof for the current head.
Evidence reviewed

PR surface:

Source +494, Tests +1473. Total +1967 across 34 files.

View PR surface stats
Area Files Added Removed Net
Source 20 1140 646 +494
Tests 14 1584 111 +1473
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 34 2724 757 +1967

Acceptance criteria:

  • [P1] pnpm deadcode:dependencies.
  • [P1] pnpm deadcode:unused-files.
  • [P1] pnpm check:test-types.
  • [P1] node scripts/run-vitest.mjs src/agents/context.test.ts src/agents/context.lookup.test.ts src/commands/status.summary.runtime.test.ts src/gateway/server-reload-handlers.test.ts.

What I checked:

Likely related people:

  • steipete: Authored several PR commits that reshape provider-owned model context metadata and provided current-head live proof. (role: recent area contributor and PR maintainer; confidence: high; commits: 44c322773448, 24c1bc4ef1a1, 528ab00eb37b; files: src/agents/context-resolution.ts, src/agents/context.ts, src/agents/command/session-store.ts)
  • xdanger: Opened the source issue and source PR and authored the first commit preserved in this replacement branch. (role: source reporter and repair contributor; confidence: medium; commits: d283354a9d21; files: src/agents/context.ts, src/agents/command/session-store.ts)
  • RomneyDa: Recent merged work changed context-cache warmup and lazy loading behavior in src/agents/context.ts, which overlaps this PR's cache/reload boundary. (role: adjacent context-cache contributor; confidence: medium; commits: b79effefee92, d91ef6bb171d; files: src/agents/context.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. 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. status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jun 6, 2026
@clawsweeper

clawsweeper Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

ClawSweeper status: this ClawSweeper-authored replacement PR is blocked on real behavior proof.

Reviewed head: 0ef28694fb56a03c49f3239d39d3f8f290d9e3cf
Proof status: not_applicable
Updated: 2026-06-12T11:20:38.140Z

Maintainer decision needed:

  • capture real behavior proof for this head
  • apply proof: override if the proof requirement should be waived
  • pause or close the replacement if proof should not be pursued

@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. status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jun 10, 2026
@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. labels Jun 12, 2026
@clawsweeper

clawsweeper Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

ClawSweeper 🐠 automerge status

ClawSweeper took another look; no safe branch change was available on this pass.

Executor outcome: source PR branch changed while the repair worker was preparing its push; requeue against the latest head.
Worker summary: Make this PR merge-ready for ClawSweeper automerge. Rebase onto latest main, address PR comments and review findings, fix CI/check failures, preserve release-note context, and validate before returning.

Worker actions:

  • build_fix_artifact on this PR: planned - Maintainer opted this PR into ClawSweeper automerge/autofix repair; run the direct Codex edit loop after live hydration instead of a separate read-only planning pass.

ClawSweeper left the PR as-is: no push, no rebase, no replacement PR, no merge, and no fresh ClawSweeper pass.

fish notes: reasoning high.

Automerge progress:

  • 2026-06-12 15:38:13 UTC review queued 349b05bc25a0 (after repair)
  • 2026-06-14 01:23:48 UTC review requested repair 1235135c6472 (structured ClawSweeper needs-human verdict with repairable P-severity findings...)

@clawsweeper clawsweeper Bot added rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jun 12, 2026
@clawsweeper
clawsweeper Bot force-pushed the clawsweeper/gitcrawl-13603-switching-to-a-smaller-context-model-mid-session-causes-hard-cra branch from 0ef2869 to 349b05b Compare June 12, 2026 15:38
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 12, 2026
@steipete steipete self-assigned this Jun 13, 2026
@steipete
steipete force-pushed the clawsweeper/gitcrawl-13603-switching-to-a-smaller-context-model-mid-session-causes-hard-cra branch from 349b05b to 1235135 Compare June 14, 2026 01:07
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime commands Command implementations size: XL and removed size: S labels Jun 14, 2026
@steipete

steipete commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Land-ready proof for SHA fa4fc7d3292a00fefecb22832ea757bee7e9bb60:

  • Rebased cleanly onto current origin/main.
  • Focused regression suite: 14 files, 441 assertions, all passing.
  • pnpm tsgo, pnpm check:test-types, changed TypeScript oxlint, git diff --check, and full pnpm build: passing.
  • Live official provider proof: forced noop tool calls plus follow-up text passed with OpenAI gpt-5.5 and Anthropic claude-sonnet-4-6.
  • Live profile suite: 37 tests passed across those two models, including prompt, file-read, and image probes.
  • Isolated OpenClaw agent/session/status proof: configured ceiling 64,000; persisted context 64,000; status context 64,000.
  • AWS Crabbox changed gate + build: run run_27f365e25fd2, lease cbx_66c09737cd1a (golden-crayfish), exit 0, lease stopped.
  • Direct contracts checked: Anthropic registration/Vertex catalog in this repo; Codex model-list context-window behavior in codex-rs/codex-api/src/endpoint/models.rs at sibling Codex SHA b89ce9a2bc.
  • The first CI pass exposed a TUI teardown race after the broader context catalog import. The test now stubs the context warmup boundary; the exact TUI lane passes 23 files / 489 tests.
  • Final GitHub CI is fully green: CI run 27484950708, CodeQL run 27484950717, critical-quality run 27484950707, OpenGrep run 27484950706, and TUI PTY run 27484950712.

Known gap: no live calls to other providers or native cross-OS app flows. Those paths have focused unit/catalog coverage plus Linux remote type/lint/build proof.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. labels Jun 14, 2026
@steipete
steipete merged commit 399f5bc into main Jun 14, 2026
164 checks passed
@steipete
steipete deleted the clawsweeper/gitcrawl-13603-switching-to-a-smaller-context-model-mid-session-causes-hard-cra branch June 14, 2026 01:48
samson910022 pushed a commit to samson910022/openclaw that referenced this pull request Jun 14, 2026
…t window resolution

When a session entry has model="openrouter/anthropic/claude-sonnet-4" but
entry.modelProvider is empty, buildStatusMessage() previously set
contextLookupProvider = undefined, causing resolveContextTokensForModel()
to skip the provider-qualified cache key lookup.

Now extract the embedded provider (first slash segment) and pass it to the
resolution chain so the correct context window is found.

Combined with PR openclaw#92424's resolveFreshModel() fix and PR openclaw#92709's
slash-guard removal in resolveContextTokensForModel(), this gives
comprehensive coverage for proxy model context window resolution.

Refs: openclaw#39857, openclaw#90889, openclaw#92424, openclaw#92709
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper Tracked by ClawSweeper automation commands Command implementations gateway Gateway runtime 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. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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.

Status/session context window can over-report the selected model's actual window

2 participants