Skip to content
Python

fix(observability): keep build_app's signature; read a saved window after the call (OPE-244)

Devika Verma0cb2d9fb2ba1
diff --git a/coworker/observability/provider_view.py b/coworker/observability/provider_view.py
index abdc78f4..a2b7ecac 100644
--- a/coworker/observability/provider_view.py
+++ b/coworker/observability/provider_view.py
@@ -194,14 +194,17 @@ def _provider_title(provider: str) -> Optional[str]:
return None
-def _context_window(model: str) -> Optional[int]:
+def _context_window(model: str, *, catalogue_only: bool = False) -> Optional[int]:
"""The window the engine compacts against (`TurnEngine._compaction_config`): the
catalogue, else the user's saved size, else an Ollama model's `num_ctx`. A local
- server's window is left out: finding it may mean a request to that server."""
+ server's window is left out: finding it may mean a request to that server. With
+ `catalogue_only` (a call's start) nothing is read from disk, so the request is not held."""
try:
from ..providers.matrix import model_context_windows
window = model_context_windows().get(model)
+ if catalogue_only:
+ return int(window) if window else None
if not window:
from .. import model_config
@@ -288,7 +291,7 @@ class _ModelCall:
attrs={
"purpose": tags.purpose, "provider": provider,
"provider_title": _provider_title(provider), "model": model,
- "stream": streamed, "context_window": _context_window(model),
+ "stream": streamed, "context_window": _context_window(model, catalogue_only=True),
"tools_offered": len(tools or []), "tools_hash": tools_hash(tools),
},
)
@@ -297,7 +300,18 @@ class _ModelCall:
return None
if step_id is None:
return None
- return cls(tags, sink, step_id, streamed)
+ call = cls(tags, sink, step_id, streamed)
+ call.model = model
+ return call
+
+ def _window(self, attrs: dict[str, Any]) -> None:
+ """A window the catalogue lacks (the user's saved size, Ollama's num_ctx), looked up
+ once the call is over so recording never delays the request."""
+ model = getattr(self, "model", "")
+ if model and not _context_window(model, catalogue_only=True):
+ window = _context_window(model)
+ if window:
+ attrs["context_window"] = window
def saw(self, chunk: Any) -> None:
# The first streamed piece of the reply: text or thinking (§7.2).
@@ -334,6 +348,7 @@ class _ModelCall:
more, content = self._exchange()
attrs.update(more)
_fill_from_facts(attrs)
+ self._window(attrs)
self.sink.end_step(
self.step_id, status="ok", attrs=attrs, content=content,
wait_for_reply_message=self.tags.purpose == "main",
@@ -350,6 +365,7 @@ class _ModelCall:
more, content = self._exchange()
attrs: dict[str, Any] = {"finish_reason": None, "ttft_ms": self.ttft_ms if self.streamed else None,
"usage": {"reported": False}, **more}
+ self._window(attrs)
text = str(root_cause(exc)).strip()[:200] or None
self.sink.end_step(self.step_id, status="error", attrs=attrs, content=content,
error={"type": classify_error(exc), "text": text})
diff --git a/coworker/server/run.py b/coworker/server/run.py
index b765fbc3..6741b7fb 100644
--- a/coworker/server/run.py
+++ b/coworker/server/run.py
@@ -99,14 +99,14 @@ def _watch_parent_windows(parent: int) -> None:
threading.Thread(target=watch, daemon=True).start()
-def build_app(workspace: str | None, model: str, mode: str, *, owns_state: bool = False):
+def build_app(workspace: str | None, model: str, mode: str):
manager = SessionManager(
workspace=Path(workspace).expanduser().resolve() if workspace else None,
data_dir=state_dir(),
model=model,
mode=Mode(mode),
)
- if owns_state:
+ if _ENGINE_LOCK is not None:
# Turns an earlier run left open (crash, restart mid-turn) are closed as unknown
# (observability TD-12). Only with the engine lock: a second server on the same
# folder may have turns running right now.
@@ -227,7 +227,7 @@ def main(argv=None) -> None:
_exit_when_orphaned()
_warn_if_state_shared()
- app = build_app(args.cwd, args.model, args.mode, owns_state=_ENGINE_LOCK is not None)
+ app = build_app(args.cwd, args.model, args.mode)
uvicorn.run(
app, host=args.host, port=args.port, ws_max_size=_WS_MAX_FRAME_BYTES
)
diff --git a/tests/test_observability_model_steps.py b/tests/test_observability_model_steps.py
index 8a3cf761..3220d7fb 100644
--- a/tests/test_observability_model_steps.py
+++ b/tests/test_observability_model_steps.py
@@ -303,3 +303,20 @@ def test_the_context_window_is_the_one_the_engine_compacts_against(monkeypatch):
assert _context_window("openrouter:model-b") == 64_000
assert _context_window("ollama:model-c") == 40_960
assert _context_window("together:model-d") is None
+
+
+def test_a_saved_window_is_read_after_the_call_not_before(tmp_path, monkeypatch):
+ # Reading the user's saved size before the request delayed it (a title call then raced
+ # ahead in test_server). The step starts with the catalogue's value only and gets the
+ # saved size once the call is over.
+ from coworker import model_config
+
+ monkeypatch.setattr(model_config, "context_size_for", lambda model: 64_000)
+ engine, rec = _engine(tmp_path, [AssistantTurn(text="hi", finish_reason="stop", usage=None)])
+ events = []
+ rec.add_listener(lambda ev: events.append(ev))
+ _run(engine, "hello")
+ [started] = [e for e in events if e.get("type") == "step_started" and e.get("kind") == "model"]
+ assert started["attrs"]["context_window"] is None # not in the catalogue; nothing read yet
+ [step] = [s for s in rec.as_dict()["steps"] if s["kind"] == "model"]
+ assert step["attrs"]["context_window"] == 64_000