Skip to content

refactor: make context counters model-specific (1M-context models) + re-sync model catalog (#1521) - #1524

Merged
vybe merged 2 commits into
devfrom
feature/1521-model-context-windows
Jul 8, 2026
Merged

vybe merged 2 commits into
devfrom
feature/1521-model-context-windows

Conversation

@vybe

@vybe vybe commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • The % context used gauge assumed a flat 200K window, reading up to ~5× too high on 1M-context models (Opus 4.8/4.7, Sonnet 5, Fable 5 = 1M; Gemini = 1M; Codex = 272K). This consolidates ~15 scattered 200000 literals + the 5 disagreeing agent-server resolvers into one model→context-window catalog.
  • The runtime-reported modelUsage.contextWindow stays primary (only it knows the effective plan/auth/beta window); the catalog is the fallback — bare Claude → 200K safe floor, [1m] → 1M, Gemini → 1M, Codex → 272K, unknown → 200K + logged warning.
  • Why the bare-Claude floor is 200K, not 1M: the effective window is auth/plan-dependent (a Pro-without-credits Opus turn is genuinely 200K). On the fallback path we have no tier info, so failing "too full" is safe; a fixed 1M fallback would hide an imminent 200K compaction wall. Real 1M is delivered by the primary value.

Changes

  • New src/backend/services/model_context.py — pure-stdlib catalog (resolve_context_window + DEFAULT_CONTEXT_WINDOW), vendored byte-identically into docker/base-image/agent_server/model_context.py (Invariant Setup improvements #5, parity-tested).
  • Critical fix: ClaudeCodeRuntime.get_context_window was dead code — the real Claude fallback was the static ExecutionMetadata.context_window = 200000 default. Now the catalog seeds metadata.context_window at construction in the Claude + headless paths (the stream value overrides it), guarded by a regression test.
  • Unified 5 resolvers (state.py, claude_code, gemini_runtime, codex_runtime, ABC-now-non-abstract); fixed state.py init-ordering so a [1m] agent reports 1M pre-first-turn.
  • Backend downstream: 13 or 200000 → or DEFAULT_CONTEXT_WINDOW.
  • Re-synced ModelSelector.vue presets (added Fable 5, Sonnet 5) + agent-server alias note.
  • Docs: architecture.md (Context-window catalog note), model-selection.md, learnings.md.
  • No schema change — columns/DDL unchanged; only written values become model-correct.

Test Plan

  • New: pytest tests/unit/test_model_context_catalog.py tests/unit/test_model_context_parity.py — 35 pass (resolution table incl. [1m]/unknown/None, byte-parity, Claude-metadata seed regression, AgentState init-ordering)
  • Full unit suite: 3611 passed (4 pre-existing unrelated flakes in fork_to_own/capacity_manager — pass in isolation, don't touch this code)
  • No secrets in diff; scope clean; /review → 0 critical

Fixes #1521

🤖 Generated with Claude Code

Eugene Vyborov and others added 2 commits July 8, 2026 10:36
…nc model catalog (#1521)

Replace ~15 scattered hardcoded 200000 context-window denominators with one
model→context-window catalog so the "% context used" gauge is correct for
1M-context models (Opus 4.8/4.7, Sonnet 5, Fable 5, Gemini = 1M; Codex = 272K).
The runtime-reported modelUsage.contextWindow stays PRIMARY; the catalog is the
FALLBACK only — bare Claude → 200K safe floor, [1m] → 1M, Gemini → 1M,
Codex → 272K, unknown → 200K + logged warning. The bare-Claude floor is
deliberate: the effective window is auth/plan-dependent, so a fixed 1M fallback
would hide an imminent 200K compaction wall.

- New pure-stdlib services/model_context.py, vendored byte-identically into the
  agent server (Invariant #5, parity-tested).
- Unify 5 disagreeing agent-server resolvers (state.py, claude_code, gemini,
  codex, the ABC) via the catalog. Seed metadata.context_window at construction
  in the Claude + headless paths — the real Claude fallback, since
  get_context_window is dead code for Claude and the flat Pydantic default was
  the actual fallback. Fix state.py init-ordering so a [1m] agent reports 1M
  before its first turn. The non-abstract ABC default means a new runtime can't
  ship a silently-wrong 200K.
- Backend downstream fallbacks use the shared DEFAULT_CONTEXT_WINDOW constant.
- Re-sync ModelSelector presets (add Fable 5, Sonnet 5) + agent-server alias note.
- No schema change (columns/DDL unchanged; only written values become correct).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…text-windows

# Conflicts:
#	tests/registry.json
@dolho

dolho commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review (direct). Consolidates scattered 200000 literals + disagreeing per-runtime resolvers into one model_context.resolve_context_window catalog for the % context denominator.

Strengths: total function (bounded output, never raises); the safe-floor invariant is the right call — bare Claude ids resolve to the guaranteed 200K floor (over-report usage = safe) rather than the 1M ceiling (which would hide a 200K compaction wall), with [1m] as the explicit 1M opt-in and the runtime-reported value as the authority. Byte-identical vendored copy (Invariant #5) guarded by test_model_context_parity.py.

Notes (non-blocking):

  • Documented tradeoff: a bare sonnet-5/fable-5 on the fallback path resolves to 200K even though they're natively 1M — safe (fuller bar), and the docstring calls it out (decision A). Just flagging it's intentional, not a bug.
  • _FAMILY_PREFIX_WINDOWS is startswith-ordered most-specific-first; fine today, but a future claude-… id that should differ from the 200K floor would need the runtime value (which it has). OK.

Merge conflict in tests/registry.json (both sides appended test entries) resolved — kept all three entries, JSON validated. CI re-running post-rebase. ✅ once green.

@vybe
vybe merged commit 09ffab2 into dev Jul 8, 2026
20 checks passed
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.

2 participants