Skip to content

fix: self-hosted model discovery ignores top-level context_size/context_length field#109368

Open
Mohl wants to merge 2 commits into
openclaw:mainfrom
Mohl:fix/self-hosted-context-window-field-fallback
Open

fix: self-hosted model discovery ignores top-level context_size/context_length field#109368
Mohl wants to merge 2 commits into
openclaw:mainfrom
Mohl:fix/self-hosted-context-window-field-fallback

Conversation

@Mohl

@Mohl Mohl commented Jul 16, 2026

Copy link
Copy Markdown

Closes #109367

What Problem This Solves

Resolves a problem where users configuring a self-hosted/custom OpenAI-compatible provider (e.g. Cortecs.ai) would see their model's context window massively under-reported by OpenClaw's catalog. discoverOpenAICompatibleLocalModels only read a llama.cpp-specific nested field (meta.n_ctx_train) for context length, so any provider advertising context length as a top-level field on the /v1/models row (a common convention outside the llama.cpp ecosystem) fell through to a hardcoded 128k default. Concretely: Cortecs' glm-5.2 advertises context_size: 1048576 (1M tokens, matching Z.ai's published spec), but OpenClaw's catalog reported it as ~131k. This caused premature/unnecessary auto-compaction and, in one observed case, a session-ending compaction timeout on a session that would have fit comfortably in the model's real context window.

Why This Change Was Made

Added lookups for the common top-level field-name variants (context_length, context_window, context_size) on the discovered model row, checked after the existing meta.n_ctx_train llama.cpp lookup (unchanged priority for that ecosystem) and before the hardcoded SELF_HOSTED_DEFAULT_CONTEXT_WINDOW fallback. This is additive only — no existing behavior for llama.cpp or providers without any of these fields changes.

User Impact

Users configuring self-hosted/custom OpenAI-compatible providers that advertise context length as a top-level /v1/models field (Cortecs and similar cloud-aggregator-style APIs) now get the correct context window in OpenClaw's catalog, instead of a silently-wrong 128k default. This avoids unnecessary/premature auto-compaction and the associated risk of compaction timeouts on large-context models.

Evidence

  • Added two new unit tests in src/plugins/provider-self-hosted-setup.test.ts:
    • reads provider-advertised context_size when meta.n_ctx_train is absent — reproduces the Cortecs/glm-5.2 case (context_size: 1048576contextWindow: 1048576).
    • prefers context_length over context_window and context_size when multiple are present — pins the read priority when a row advertises more than one variant.
  • Full existing test file still passes:
    $ npx vitest run src/plugins/provider-self-hosted-setup.test.ts
     Test Files  1 passed (1)
          Tests  15 passed (15)
    
  • npx tsc --noEmit and npx oxlint clean on both changed files.

Real-behavior proof (post-rebase, requested by review)

Rebased onto current upstream/main (HEAD now 06b4d56d5), reinstalled dependencies, and reran the focused test command:

$ node scripts/run-vitest.mjs src/plugins/provider-self-hosted-setup.test.ts
 Test Files  1 passed (1)
      Tests  15 passed (15)

Then called the patched discoverOpenAICompatibleLocalModels directly against the live Cortecs API (https://api.cortecs.ai/v1) with a real (redacted) Cortecs API key, no mocking:

{
  "id": "glm-5.2",
  "contextWindow": 1048576,
  "maxTokens": 8192
}
total models discovered: 74

Before this fix, glm-5.2 would have fallen through to the hardcoded SELF_HOSTED_DEFAULT_CONTEXT_WINDOW (128k/131k) since its context length is only advertised as a top-level context_size field, not meta.n_ctx_train. With the fix, the catalog now correctly resolves the real 1,048,576-token (1M) window advertised by the provider.


AI-assisted PR — implemented by an OpenClaw agent session while investigating a real compaction-timeout incident against Cortecs' GLM-5.2. Session context: the agent observed openclaw capability model inspect --model cortecs/glm-5.2 reporting contextWindow: 131000 while Cortecs' own /v1/models API reports context_size: 1048576 for the same model, traced the discrepancy to this code path, and verified the fix with the tests above.

@Mohl
Mohl force-pushed the fix/self-hosted-context-window-field-fallback branch from f421609 to 5fcff30 Compare July 17, 2026 00:21
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. labels Jul 17, 2026
@clawsweeper

clawsweeper Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 21, 2026, 3:10 AM ET / 07:10 UTC.

Summary
The branch reads top-level context_length, context_window, and context_size fields from OpenAI-compatible model rows before falling back to the existing default, with focused coverage for context_size and precedence.

PR surface: Source +18, Tests +79. Total +97 across 2 files.

Reproducibility: yes. from source and supplied live evidence: a /v1/models row with only a positive top-level context_size previously bypasses meta.n_ctx_train and reaches the default; the PR's Cortecs run reports the corrected discovered value.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: src/plugins/provider-self-hosted-setup.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #109367
Summary: This PR is the linked candidate fix for the reported self-hosted discovery defect; the fallback-selection report is related context but addresses a separate decision path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

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

Rank-up moves:

  • [P2] Refresh the branch against current main and confirm the required merge-result checks for the exact head.

Risk before merge

  • [P1] The branch is behind current main; refresh the actual merge result and required checks against the current base before landing.

Maintainer options:

  1. Decide the mitigation before merge
    Rebase or merge current main, retain the existing precedence and regression tests, then land if the refreshed merge result and required checks remain clean.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P2] No mechanical repair is identified; the remaining action is a normal current-base merge-result refresh and maintainer landing review.

Security
Cleared: The diff only parses additional untrusted response fields through the existing positive-integer helper and adds tests; it introduces no dependency, permission, secret, or execution-path change.

Review details

Best possible solution:

Rebase or merge current main, retain the existing precedence and regression tests, then land if the refreshed merge result and required checks remain clean.

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

Yes from source and supplied live evidence: a /v1/models row with only a positive top-level context_size previously bypasses meta.n_ctx_train and reaches the default; the PR's Cortecs run reports the corrected discovered value.

Is this the best way to solve the issue?

Yes. Reading common provider-advertised fields at the existing discovery boundary, after the established llama.cpp field and before the default, is narrower and less error-prone than provider-specific special cases.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P1: Incorrectly defaulting an affected provider's context window can cause premature compaction and failed agent turns for real self-hosted users.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR includes focused test output and a redacted after-fix live Cortecs discovery result showing glm-5.2 resolves to the advertised 1,048,576-token window.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR includes focused test output and a redacted after-fix live Cortecs discovery result showing glm-5.2 resolves to the advertised 1,048,576-token window.
Evidence reviewed

PR surface:

Source +18, Tests +79. Total +97 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 19 1 +18
Tests 1 79 0 +79
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 98 1 +97

What I checked:

  • Changed discovery precedence: The proposed discovery path preserves the explicit override and llama.cpp meta.n_ctx_train precedence, then accepts positive top-level context field variants before SELF_HOSTED_DEFAULT_CONTEXT_WINDOW. (src/plugins/provider-self-hosted-setup.ts:259, 06b4d56d5cbe)
  • Regression coverage: The PR adds coverage for a context_size: 1048576 model row and for the declared top-level field priority, directly covering the reported provider shape and preventing a precedence regression. (src/plugins/provider-self-hosted-setup.test.ts:465, 06b4d56d5cbe)
  • Real provider proof: The PR body reports an after-fix direct call against the live Cortecs /v1 API that discovers glm-5.2 with contextWindow: 1048576, alongside focused test output; this is stronger than mock-only evidence. (0b7e79d3f274)
  • Current review state: The PR remains unmerged and GitHub reports it as mergeable but behind main; current main therefore has not yet incorporated this candidate fix. (f4b37db18df4)
  • Related issue boundary: The linked report describes the same discovery defect, while the separately linked fallback-selection report concerns candidate choice after discovery and remains a distinct follow-up area. (src/plugins/provider-self-hosted-setup.ts:259, 06b4d56d5cbe)

Likely related people:

  • Mohl: Provided the concrete Cortecs reproduction, live after-fix provider output, and the focused patch; this establishes close connection to the behavior but not current-main ownership. (role: reporter and proposed-fix author; confidence: low; commits: 06b4d56d5cbe, 0b7e79d3f274; files: src/plugins/provider-self-hosted-setup.ts, src/plugins/provider-self-hosted-setup.test.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.
Review history (2 earlier review cycles)
  • reviewed 2026-07-17T00:28:59.116Z sha 5fcff30 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-20T06:36:29.659Z sha 06b4d56 :: needs maintainer review before merge. :: none

…ze on self-hosted model discovery

discoverOpenAICompatibleLocalModels only read contextWindow from
model.meta.n_ctx_train (llama.cpp-specific), falling back to the
hardcoded 128k default for any provider that advertises context length
as a top-level field instead. Cortecs.ai's /v1/models response for
glm-5.2 reports context_size: 1048576 (the model's real 1M-token
window), but that field was ignored, so the catalog under-reported it
as 128k/131k. This caused unnecessarily aggressive/premature
compaction on sessions that had far more real headroom than the
catalog believed.

Fixes openclaw#109367

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@Mohl
Mohl force-pushed the fix/self-hosted-context-window-field-fallback branch from 5fcff30 to 06b4d56 Compare July 20, 2026 06:32
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

2 participants