Skip to content

context-window-guard: strict modelId match drops user contextTokens for provider/model form (1M Copilot models capped at 128k) #76532

Closed

Description

@jiahzhu1989

Version

OpenClaw 2026.5.2

Summary

When models.providers.<provider>.models[].id is configured as a bare model id (e.g. claude-opus-4.7-1m-internal) but the runtime resolves the model id in provider/model form (e.g. github-copilot/claude-opus-4.7-1m-internal), resolveContextWindowInfo fails to find the configured entry due to strict m.id === modelId equality. It then silently falls back to the plugin's default contextWindow (128k for github-copilot), and that 128k is propagated through agentMeta.contextTokens into sessions.json, capping the session at 128k even though the user configured 1M / 936k.

Repro

~/.openclaw/openclaw.json:

{
  "models": {
    "providers": {
      "github-copilot": {
        "models": [
          {
            "id": "claude-opus-4.7-1m-internal",
            "api": "anthropic-messages",
            "contextWindow": 1000000,
            "contextTokens": 936000,
            "maxTokens": 64000
          }
        ]
      }
    }
  },
  "agents": {
    "list": [
      {
        "id": "my-agent",
        "model": { "primary": "github-copilot/claude-opus-4.7-1m-internal" }
      }
    ]
  }
}
  • openclaw models list correctly shows Ctx 914k/977k (cache + resolveContextTokensForModel work fine).
  • After any agent run, agents/<agentId>/sessions/sessions.json entry has contextTokens: 128000.
  • Status card / /status shows Context: xx/128k.

Root cause

src/agents/pi-embedded-runner/run/setup.ts:126 calls:

const ctxInfo = resolveContextWindowInfo({
  cfg: params.cfg,
  provider: params.provider,
  modelId: params.modelId,                                // can be "github-copilot/claude-opus-4.7-1m-internal"
  modelContextTokens: readPiModelContextTokens(params.runtimeModel),
  modelContextWindow: params.runtimeModel.contextWindow,  // plugin default 128_000
  defaultTokens: DEFAULT_CONTEXT_TOKENS,
});

params.modelId may be in provider/model form (it is set in pi-embedded-runner/run.ts and forwarded as-is by callers like auto-reply/reply/agent-runner-execution.ts and cron/isolated-agent/run-executor.ts).

src/agents/context-window-guard.ts resolveContextWindowInfo:

const match = models.find((m) => m?.id === params.modelId);

This strict equality fails when m.id is bare and params.modelId is provider/model. It then falls back to params.modelContextWindow (the plugin default 128k), which gets stored as agentMeta.contextTokens and persisted into sessions.json via persistRunSessionUsage.

pi-embedded-runner/model.ts already uses matchesProviderScopedModelId for the inverse direction (candidate has provider/ prefix, requested is bare). The two helpers should be symmetric.

Suggested fix

src/agents/context-window-guard.ts, treat provider/model and bare ids as equivalent under the matching provider:

-    const match = models.find((m) => m?.id === params.modelId);
+    const providerPrefix = params.provider ? `${params.provider}/` : '';
+    const stripPrefix = (id: string) =>
+      providerPrefix && id.startsWith(providerPrefix) ? id.slice(providerPrefix.length) : id;
+    const requestedBare = stripPrefix(params.modelId);
+    const match = models.find((m) => {
+      if (!m?.id) return false;
+      if (m.id === params.modelId) return true;
+      return stripPrefix(m.id) === requestedBare;
+    });

This restores the user-configured contextTokens=936000 regardless of whether the runtime forwards a bare or provider-scoped id.

Workaround (no source patch)

Duplicate each large-context model entry under both <id> and <provider>/<id> form in models.providers.<provider>.models[] until upstream is fixed:

{ "id": "claude-opus-4.7-1m-internal", "contextWindow": 1000000, "contextTokens": 936000, ... },
{ "id": "github-copilot/claude-opus-4.7-1m-internal", "contextWindow": 1000000, "contextTokens": 936000, ... }

Verified to restore Context: xx/936k end-to-end on 2026.5.2 without dist changes.

Related code paths

  • src/agents/context-window-guard.ts — resolveContextWindowInfo strict match
  • src/agents/pi-embedded-runner/run/setup.ts:126 — call site
  • src/agents/pi-embedded-runner/run.ts:~507 — resolveEffectiveRuntimeModel({ modelId })
  • src/auto-reply/reply/agent-runner.runtime — contextTokensUsed = runResult.meta.agentMeta.contextTokens ?? resolveContextTokensForModel(...), then persistRunSessionUsage
  • src/agents/pi-embedded-runner/model.ts — matchesProviderScopedModelId (the existing helper for the inverse direction)

Activity

  1. clawsweeper commented on May 3, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed May 31, 2026, 1:00 AM ET / 05:00 UTC.

    Summary
    Codex review failed: codex execution failed.

    Reproducibility: unclear. The review failed before ClawSweeper could establish a reproduction path.

    Ways to help us reproduce this

    • Add a screenshot or short recording showing the behavior.
    • Add expected vs actual behavior.

    Next step
    Review did not complete, so no work-lane recommendation was made.

    Review details

    Best possible solution:

    Retry the Codex review after fixing the execution failure.

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

    Unclear. The review failed before ClawSweeper could establish a reproduction path.

    Is this the best way to solve the issue?

    Unclear. Retry the review first so ClawSweeper can evaluate the actual issue and fix direction.

    AGENTS.md: unclear because the file could not be read completely.

    Remaining risk / open question:

    • No close action taken because the review did not complete.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against e1a98171417c.

    Label changes

    Label changes:

    • add issue-rating: 🦪 silver shellfish: Current issue advisory state selects this label.
    • add clawsweeper:needs-info: Current issue advisory state selects this label.
    • remove P2: Current review triage priority is none.
    • remove clawsweeper:no-new-fix-pr: Current issue advisory state no longer selects this label.
    • remove clawsweeper:source-repro: Current issue advisory state no longer selects this label.
    • remove clawsweeper:linked-pr-open: Current issue advisory state no longer selects this label.
    • remove impact:session-state: Current review selected no impact labels.
    • remove impact:auth-provider: Current review selected no impact labels.
    • remove issue-rating: 🦞 diamond lobster: Current issue advisory state no longer selects this label.
    Evidence reviewed

    What I checked:

    • failure reason: codex execution failed.
    • codex failure detail: Codex review failed for this issue with exit 1.
    • codex stdout: Per-item Codex failure; continuing with the rest of the shard.

    Likely related people:

    • unknown: Codex failed before it could trace repository history. (role: review did not complete; confidence: low)
    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.
  2. barnacle-openclaw commented on May 30, 2026

    @barnacle-openclaw

    This issue has been automatically marked as stale due to inactivity.
    Please add updates or it will be closed.

  3. added
    stale
    on May 30, 2026
  4. added
    P2
    impact:session-state
    impact:auth-provider
    issue-rating: 🦞 diamond lobster
    clawsweeper:source-repro
    clawsweeper:linked-pr-open
    clawsweeper:no-new-fix-pr
    on May 30, 2026
  5. removed
    stale
    on May 31, 2026
  6. steipete commented on Jun 1, 2026

    @steipete
    Contributor

    Fixed on current main by f18fd20 (fix(agents): match provider-scoped context ids). resolveContextWindowInfo now matches bare configured model IDs against provider-scoped runtime IDs, and the inverse form, before falling back to the provider default window.

    Proof run on branch inference at 7562afd:

    node scripts/run-vitest.mjs src/agents/model-catalog.test.ts src/agents/context-window-guard.test.ts extensions/github-copilot/models.test.ts extensions/github-copilot/index.test.ts

    Result: 3 Vitest shards passed. The regression tests cover both tiny vs openrouter/tiny and openrouter/tiny vs tiny, preserving the configured contextTokens path instead of capping at the default. Closing as fixed on main.

  7. locked as resolved and limited conversation to collaborators on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2clawsweeper:linked-pr-openclawsweeper:no-new-fix-prclawsweeper:source-reproimpact:auth-providerimpact:session-stateissue-rating: 🦞 diamond lobster

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions