Python
Add Claude 5 rows to the model matrix; warn on the 128k context fallback (OPE-170 p1)
Devika Verma2a5c169ac526
diff --git a/coworker/engine.py b/coworker/engine.pyindex e472df8e..24b26be2 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -12,6 +12,8 @@ engine says `needs_user`, the engine emits `PERMISSION_REQUIRED` and awaits thefrom __future__ import annotations+import logging+import asyncioimport jsonimport time@@ -40,6 +42,8 @@ from .providers.openai_provider import looks_like_unparsed_tool_callfrom .tools import ToolRegistry+logger = logging.getLogger(__name__)+class ApprovalOutcome(str, Enum):ONCE = "once"ALWAYS_TOOL = "always_tool"@@ -243,6 +247,7 @@ class TurnEngine:# Whether the latest assistant turn hit the output-token limit — decides which# diagnosis a mangled (unparseable-args) tool call gets answered with.self._turn_truncated = False+ self._warned_context_fallback = False# Each pending steering message: (text, optional MessageSource sidecar dict).self._steering: list[tuple[str, Optional[dict[str, Any]]]] = []# tool_call.id → the standing rule that auto-allowed it ("tool → target"), so the@@ -615,6 +620,19 @@ class TurnEngine:from .providers.matrix import model_context_windowscfg["context_window"] = model_context_windows().get(self.model)+ if not cfg["context_window"] and not self._warned_context_fallback:+ # OPE-170: an unlisted model compacts on the 128k guess, which for a+ # 1M-window model means compacting at a tenth of the window and+ # rebuilding the prompt cache each time. Say so once per engine.+ self._warned_context_fallback = True+ logger.warning(+ "model %s has no context window in the model matrix; assuming "+ "%d tokens (compaction at %d). Add a matrix row or set "+ "context_window in the compaction settings.",+ self.model,+ _compaction.DEFAULT_CONTEXT_WINDOW,+ _compaction.trigger_tokens(None),+ )cfg.setdefault("threshold_pct", _compaction.DEFAULT_THRESHOLD_PCT)cfg.setdefault("cap_tokens", _compaction.DEFAULT_CAP_TOKENS)return cfgdiff --git a/coworker/providers/matrix.py b/coworker/providers/matrix.pyindex 052a19ff..af250fe1 100644--- a/coworker/providers/matrix.py+++ b/coworker/providers/matrix.py@@ -107,8 +107,21 @@ MATRIX: dict[str, ModelEntry] = {"openai-codex:gpt-5.1-codex-mini": ModelEntry("GPT-5.1 Codex Mini · ChatGPT plan", _AGENTIC, 400_000),- # Fable 5 (2026-06-09) is GA; its Mythos 5 sibling is approved-orgs-only, so it- # stays out of a picker meant for the public.+ # Claude 5 generation (platform.claude.com/docs/en/models/overview, read 2026-09-08):+ # 1M context is the default and only size; 128K max output; adaptive thinking.+ # Fable 5.1 (2026-09-01) is GA; its Mythos 5.1 sibling is approved-orgs-only, so it+ # stays out of a picker meant for the public. Missing rows fell back to the 128k+ # compaction guess and compacted at 102,400 tokens (OPE-170).+ "anthropic:claude-fable-5-1": ModelEntry(+ "Claude Fable 5.1 · Anthropic", _AGENTIC_VISION, 1_000_000+ ),+ "anthropic:claude-opus-5": ModelEntry(+ "Claude Opus 5 · Anthropic", _AGENTIC_VISION, 1_000_000+ ),+ "anthropic:claude-sonnet-5": ModelEntry(+ "Claude Sonnet 5 · Anthropic", _AGENTIC_VISION, 1_000_000+ ),+ # Fable 5 (2026-06-09) stays listed as the registry's recommended model."anthropic:claude-fable-5": ModelEntry("Claude Fable 5 · Anthropic", _AGENTIC_VISION, 1_000_000),diff --git a/tests/test_matrix_current_models.py b/tests/test_matrix_current_models.pynew file mode 100644index 00000000..8f69d94a--- /dev/null+++ b/tests/test_matrix_current_models.py@@ -0,0 +1,172 @@+"""OPE-170 problem 1: the model matrix knows the current models' context windows.++A model missing from the matrix falls back to `DEFAULT_CONTEXT_WINDOW` (128,000), so+auto-compaction fires at 102,400 tokens — half or a tenth of what the newest models have+(observed on Claude Sonnet 5: two compactions in one 33-minute run, each rebuilding the+prompt cache). These tests pin the current Anthropic rows, keep every model the product+recommends or has evaluated in the table, and make the fallback visible in the log.+"""++from __future__ import annotations++import asyncio+import logging+import re+from pathlib import Path++import aisuite as ai+import pytest+from coworker.compaction import DEFAULT_CONTEXT_WINDOW, trigger_tokens+from coworker.engine import TurnEngine+from coworker.permissions import PermissionEngine+from coworker.providers import AssistantTurn, ModelCapabilities, ProviderClient+from coworker.providers.matrix import MATRIX, model_context_windows+from coworker.providers.registry import DESCRIPTORS+from coworker.tools import ToolRegistry++REPO = Path(__file__).resolve().parents[1]+++# -- rows ---------------------------------------------------------------------------+++ "model,window",+ [+ # platform.claude.com/docs/en/models/overview, read 2026-09-08: 1M is the default+ # and only context size on the Claude 5 generation; Haiku 4.5 stays at 200K.+ ("anthropic:claude-fable-5-1", 1_000_000),+ ("anthropic:claude-opus-5", 1_000_000),+ ("anthropic:claude-sonnet-5", 1_000_000),+ ("anthropic:claude-fable-5", 1_000_000),+ ("anthropic:claude-haiku-4-5", 200_000),+ ("together:moonshotai/Kimi-K3", 1_000_000),+ ],+)+def test_current_models_have_verified_windows(model, window):+ assert model_context_windows()[model] == window+++def test_claude_5_rows_are_vision_capable_and_labelled():+ for mid in ("anthropic:claude-fable-5-1", "anthropic:claude-opus-5", "anthropic:claude-sonnet-5"):+ entry = MATRIX[mid]+ assert entry.caps.tools and entry.caps.vision and entry.caps.pdf, mid+ assert "Anthropic" in entry.label, mid+++def _matrix_key(provider: str, model: str) -> str:+ # OpenAI rows are stored bare (bare ids route to the OpenAI default).+ return model if provider == "openai" else f"{provider}:{model}"+++# Recommended models whose vendor spec has not been re-checked: the matrix keeps their+# window at None on purpose (the GUI meter hides rather than show a made-up number) and+# they compact on the 128k guess. Verify the spec, fill the row, and shrink this set.+_KNOWN_UNVERIFIED_RECOMMENDATIONS = {"minimax:MiniMax-M2.5", "meta:muse-spark-1.1"}+++def test_every_recommended_model_is_in_the_matrix():+ """The Settings pane suggests each provider's `recommended_model`; every one must be a+ curated row (Ollama excepted: a user-local model name, not a vendor catalog id)."""+ missing = [+ _matrix_key(d.name, d.recommended_model)+ for d in DESCRIPTORS+ if d.name != "ollama" and d.recommended_model+ and _matrix_key(d.name, d.recommended_model) not in MATRIX+ ]+ assert not missing, f"recommended models without a matrix row: {missing}"+++def test_recommended_models_without_a_window_are_exactly_the_known_set():+ """A recommendation without a window compacts on the 128k guess. New ones must not+ slip in silently; verified ones must leave the allowlist."""+ windows = model_context_windows()+ without = {+ _matrix_key(d.name, d.recommended_model)+ for d in DESCRIPTORS+ if d.name != "ollama" and d.recommended_model+ and _matrix_key(d.name, d.recommended_model) not in windows+ }+ assert without == _KNOWN_UNVERIFIED_RECOMMENDATIONS+++def test_every_model_in_the_reviewer_eval_reports_has_a_window():+ """`reports/reviewer-eval-*.md` are the models the product has been evaluated on."""+ reports = sorted((REPO / "reports").glob("reviewer-eval-*.md"))+ if not reports:+ pytest.skip("no reviewer-eval reports in this checkout")+ pattern = re.compile(r"\b(anthropic|openai|together|gemini|zai|deepseek|fireworks|openrouter):([A-Za-z0-9._/-]+)")+ ids = set()+ for path in reports:+ for provider, model in pattern.findall(path.read_text(encoding="utf-8")):+ ids.add(_matrix_key(provider, model))+ assert ids, "no model ids found in the reports"+ windows = model_context_windows()+ missing = sorted(m for m in ids if m not in windows)+ assert not missing, f"evaluated models without a matrix window: {missing}"+++# -- trigger arithmetic (documents what the rows buy) -------------------------------+++def test_trigger_for_a_1m_model_is_the_cap_not_the_128k_guess():+ assert trigger_tokens(None) == int(0.8 * DEFAULT_CONTEXT_WINDOW) # 102,400+ assert trigger_tokens(1_000_000) == 250_000+ assert trigger_tokens(200_000) == 160_000+++# -- fallback is visible ------------------------------------------------------------+++class _OneTurn(ProviderClient):+ def complete(self, *, model, messages, tools=None, **settings):+ return AssistantTurn(text="done", finish_reason="stop")++ def capabilities(self, model):+ return ModelCapabilities()+++def _engine(tmp_path, model):+ registry = ToolRegistry()+ registry.register_all(ai.toolkits.files(root=str(tmp_path), allow_write=True))+ return TurnEngine(+ provider=_OneTurn(),+ registry=registry,+ permissions=PermissionEngine(workspace_root=tmp_path),+ model=model,+ )+++def _run(engine):+ async def _collect():+ return [ev async for ev in engine.run("hello")]++ return asyncio.run(_collect())+++def test_unlisted_model_logs_the_128k_fallback_once(tmp_path, caplog):+ engine = _engine(tmp_path, "anthropic:claude-unlisted-9")+ with caplog.at_level(logging.WARNING, logger="coworker.engine"):+ _run(engine)+ _run(engine) # second turn: no repeat+ hits = [r for r in caplog.records if "context window" in r.getMessage()]+ assert len(hits) == 1, [r.getMessage() for r in caplog.records]+ msg = hits[0].getMessage()+ assert "anthropic:claude-unlisted-9" in msg and str(DEFAULT_CONTEXT_WINDOW) in msg+++def test_listed_model_does_not_warn(tmp_path, caplog):+ engine = _engine(tmp_path, "anthropic:claude-sonnet-5")+ with caplog.at_level(logging.WARNING, logger="coworker.engine"):+ _run(engine)+ assert not [r for r in caplog.records if "context window" in r.getMessage()]+++def test_compaction_settings_override_silences_the_warning(tmp_path, caplog):+ """A caller that supplies its own context_window (the GUI's per-model settings) has+ made the choice explicitly; nothing to warn about."""+ engine = _engine(tmp_path, "anthropic:claude-unlisted-9")+ engine.compaction_settings = lambda: {"context_window": 400_000}+ with caplog.at_level(logging.WARNING, logger="coworker.engine"):+ _run(engine)+ assert not [r for r in caplog.records if "context window" in r.getMessage()]