Merge origin/main into the observability branch

Conflicts: the session title call keeps main's retry for models that
cannot turn reasoning off (OPE-201) and the recording's "title" tag on
both calls. The engine's waits and tool calls keep the recording code and
time with perf_counter, as main now does (OPE-245).
This commit is contained in:
Devika Verma
2026-11-04 14:03:20 +05:30
9 changed files with 200 additions and 41 deletions
+13 -8
View File
@@ -651,7 +651,7 @@ class TurnEngine:
cancel_wait.cancel()
async def _wait_tool(self, tool_call, coro, interrupted, kind: Optional[str] = None):
started, clock = time.time(), time.monotonic()
started, clock = time.time(), time.perf_counter()
# Observability E20: a wait step from the moment the card is shown to the answer, so
# a slow answer is never mistaken for a slow agent (§5.4).
kind = kind or _WAIT_KINDS.get(tool_call.name, "approval")
@@ -675,7 +675,7 @@ class TurnEngine:
failed = True
raise
finally:
self._tool_timings.setdefault(tool_call.id, {}).update(waited_started=started, waited_ms=(time.monotonic() - clock) * 1000)
self._tool_timings.setdefault(tool_call.id, {}).update(waited_started=started, waited_ms=(time.perf_counter() - clock) * 1000)
self._obs.end_wait(tool_call.id)
if step_id is not None:
who = ""
@@ -1066,9 +1066,14 @@ class TurnEngine:
# The reviewer judges with the session's own model (§1.5: "if it's trusted to
# drive the agent, it's strong enough to review it"). Bound once at session build,
# it would otherwise keep the OLD model for the rest of the session after a
# switch — silently reviewing with a model the user moved away from.
# switch — silently reviewing with a model the user moved away from. A
# decision-model reviewer (OpenAI Decisions, Jev) judges with its own service's
# model, which a session switch must not change (its `model` is read-only).
if self.reviewer is not None:
self.reviewer.model = model
from .decision_reviewer import DecisionReviewer
if not isinstance(self.reviewer, DecisionReviewer):
self.reviewer.model = model
if not had_history:
return None
from .providers.matrix import model_labels
@@ -1316,7 +1321,7 @@ class TurnEngine:
reasoning="".join(streamed_reasoning) or None,
)
model_started, model_clock = time.time(), time.monotonic()
model_started, model_clock = time.time(), time.perf_counter()
try:
async for chunk in self._astream():
if chunk.reasoning_delta:
@@ -1393,7 +1398,7 @@ class TurnEngine:
effort_setting=self.model_settings.get("reasoning_effort"),
)
)
self.messages[-1]["timing"] = {"model_started": model_started, "model_ms": (time.monotonic() - model_clock) * 1000}
self.messages[-1]["timing"] = {"model_started": model_started, "model_ms": (time.perf_counter() - model_clock) * 1000}
payload: dict[str, Any] = {
"text": turn.text,
"tool_calls": [tc.name for tc in turn.tool_calls],
@@ -3067,7 +3072,7 @@ class TurnEngine:
}
def _execute_call(self, tool_call: ToolCall) -> tuple[Any, str]:
started, clock = time.time(), time.monotonic()
started, clock = time.time(), time.perf_counter()
try:
delegated = self._authorized_delegates.pop(tool_call.id, None)
if delegated and delegated != self._delegated_context(tool_call):
@@ -3086,7 +3091,7 @@ class TurnEngine:
except Exception as exc:
return {"error": str(exc), "error_type": type(exc).__name__}, "error"
finally:
self._tool_timings.setdefault(tool_call.id, {}).update(tool_started=started, tool_ms=(time.monotonic() - clock) * 1000)
self._tool_timings.setdefault(tool_call.id, {}).update(tool_started=started, tool_ms=(time.perf_counter() - clock) * 1000)
def _record_result(self, tool_call: ToolCall, result: Any, status: str) -> Event:
spec = self.registry.get(tool_call.name)
+2 -1
View File
@@ -29,7 +29,8 @@ _lock = threading.Lock()
def native_base(base_url: Optional[str]) -> str:
base = (base_url or "http://localhost:11434").strip().rstrip("/")
# The same default as `registry.DEFAULT_OLLAMA_URL` (127.0.0.1, OPE-229).
base = (base_url or "http://127.0.0.1:11434").strip().rstrip("/")
return base[: -len("/v1")] if base.endswith("/v1") else base
+1 -1
View File
@@ -237,7 +237,7 @@ class OpenAIProvider(ProviderClient):
# inject a `client` directly, bypassing all of this.
#
# `base_url` points the same OpenAI SDK at any OpenAI-compatible endpoint — used by the
# provider router for Ollama (`http://localhost:11434/v1`, with a placeholder key) and,
# provider router for Ollama (`http://127.0.0.1:11434/v1`, with a placeholder key) and,
# later, other OpenAI-shaped backends. When None, behavior is identical to stock OpenAI.
#
# `http_client` is an optional httpx.Client handed to the SDK. Ollama uses it to carry
+4 -1
View File
@@ -32,7 +32,10 @@ from .openai_provider import OpenAIProvider
from .openai_responses import OpenAIResponsesProvider
from .vertex_provider import VertexProvider
DEFAULT_OLLAMA_URL = "http://localhost:11434"
# 127.0.0.1, not localhost: on Windows `localhost` tries ::1 first, where Ollama does not
# listen, and every new connection waits about 2 s before falling back (OPE-229). An
# address the user typed is used as typed.
DEFAULT_OLLAMA_URL = "http://127.0.0.1:11434"
# The index deliberately lives in the SecretStore too: it names profiles which contain
# credentials, and keeping the two together makes a future secure-store backend a drop-in.
CUSTOM_PROVIDERS_PROFILE = "providers:custom"
+51 -28
View File
@@ -99,7 +99,7 @@ from ..providers import (
verify_provider_key,
)
from ..providers.local_server import LOCAL_SERVERS
from ..providers.registry import CUSTOM_PROVIDERS_PROFILE
from ..providers.registry import CUSTOM_PROVIDERS_PROFILE, DEFAULT_OLLAMA_URL
from ..secrets import SecretStore, state_dir
from ..sessions import SessionRecord, usage_totals
from ..teams import Actor as TeamActor
@@ -4756,7 +4756,7 @@ class SessionManager:
if cached and now - cached[0] < 30:
return cached[1]
profile = self.secrets.get("provider:ollama") or {}
base = (profile.get("base_url") or "http://localhost:11434").strip().rstrip("/")
base = (profile.get("base_url") or DEFAULT_OLLAMA_URL).strip().rstrip("/")
if base.endswith("/v1"):
base = base[: -len("/v1")]
try:
@@ -4771,7 +4771,7 @@ class SessionManager:
def _ollama_models(self) -> list[str]:
"""Live list of models pulled into the configured Ollama server (via its native
`/api/tags`), as `ollama:<name>` so they're directly selectable. Empty if Ollama is
unreachable — best-effort, never raises. No stored profile → the default localhost
unreachable — best-effort, never raises. No stored profile → the default 127.0.0.1
endpoint, same as `_ollama_alive` (keyless Ollama needs no saved form)."""
from ..providers import ollama_facts
@@ -8421,6 +8421,9 @@ class SessionManager:
# instruction ("4-5 word title") yields English titles for Chinese/Japanese/…
# sessions. Same-language also relaxes the word count to a length that is natural
# for the opener's script (CJK titles are a few characters, not 4-5 "words").
# A title asked again without an effort setting (OPE-201): room for the reasoning a
# model that cannot turn it off spends before the few words of the title.
_AUTOTITLE_REASONING_MAX_TOKENS = 1024
_AUTOTITLE_PROMPT = (
"You title chat sessions. Given the user's opening message(s) — and, when "
"present, the assistant's first reply for context — reply with ONLY a short "
@@ -8523,31 +8526,51 @@ class SessionManager:
fallback stays; the small-talk sentinel leaves auto_title unset so the turn-2
retry can run."""
try:
turn = await asyncio.to_thread(
tag_provider(engine.provider, purpose="title", turn=turn_id).complete,
model=engine.model,
messages=[
{"role": "system", "content": self._AUTOTITLE_PROMPT},
{
"role": "user",
"content": "\n\n".join(openers)
+ (
f"\n\n[the assistant's first reply]\n{assistant}"
if assistant
else ""
),
},
],
temperature=0.2,
# Reasoning-routed models spend hidden tokens BEFORE emitting text; a
# tight cap plus default effort yields an empty completion and a silent
# no-op. Effort "none" means "no thinking": every provider's effort
# mapper handles it (OpenAI-vocabulary endpoints get it on the wire,
# Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves
# headroom either way.
max_tokens=64,
reasoning_effort="none",
)
messages = [
{"role": "system", "content": self._AUTOTITLE_PROMPT},
{
"role": "user",
"content": "\n\n".join(openers)
+ (
f"\n\n[the assistant's first reply]\n{assistant}"
if assistant
else ""
),
},
]
# Tagged so the recording files these calls under "title" (observability).
titler = tag_provider(engine.provider, purpose="title", turn=turn_id)
try:
turn = await asyncio.to_thread(
titler.complete,
model=engine.model,
messages=messages,
temperature=0.2,
# Reasoning-routed models spend hidden tokens BEFORE emitting text; a
# tight cap plus default effort yields an empty completion and a silent
# no-op. Effort "none" means "no thinking": every provider's effort
# mapper handles it (OpenAI-vocabulary endpoints get it on the wire,
# Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves
# headroom either way.
max_tokens=64,
reasoning_effort="none",
)
except Exception as exc:
# OPE-201: some endpoints cannot turn reasoning off and refuse "none"
# (OpenRouter GLM 5.3: 400 "Reasoning is mandatory for this endpoint and
# cannot be disabled"). Ask once more with the model's own effort, and
# room for the reasoning it spends before the few words of the title.
text = str(exc).lower()
if "reasoning" not in text and "effort" not in text:
raise
logger.info("autotitle for %s: retrying without reasoning effort (%s)", session_id, exc)
turn = await asyncio.to_thread(
titler.complete,
model=engine.model,
messages=messages,
temperature=0.2,
max_tokens=self._AUTOTITLE_REASONING_MAX_TOKENS,
)
raw = (getattr(turn, "text", None) or "").strip()
# Sanitize: surrounding quotes off, whitespace collapsed, capped at 60.
title = " ".join(raw.strip("\"'“”‘’`").split())
+17
View File
@@ -1091,6 +1091,23 @@ def test_reviewer_follows_a_model_switch(tmp_path):
assert engine.reviewer.model == "anthropic:claude-sonnet-5"
def test_a_decision_model_reviewer_keeps_its_model_on_a_switch(tmp_path):
# With OpenAI Decisions or Jev as the reviewer, picking a model in a session raised
# AttributeError (DecisionReviewer.model has no setter) and closed the session's socket.
# Those reviewers judge with their own service's model, so a switch leaves it alone.
from coworker.decision_reviewer import build_decision_reviewer
engine, _rows, _approvals = _engine(tmp_path, [AssistantTurn(text="ok", finish_reason="stop")])
engine.reviewer = build_decision_reviewer("jev", api_key="test-key-not-real")
before = engine.reviewer.model
engine.messages.append({"role": "user", "content": "hi"})
engine.switch_model("openai:model-b") # must not raise
assert engine.model == "openai:model-b"
assert engine.reviewer.model == before
def test_model_switch_with_no_reviewer_attached_still_works(tmp_path):
engine, _rows, _approvals = _engine(tmp_path, [AssistantTurn(text="ok", finish_reason="stop")])
engine.reviewer = None
+36
View File
@@ -208,3 +208,39 @@ async def test_non_english_small_talk_still_hits_the_sentinel(tmp_path):
await _turn(mgr, "lang3", "帮我整理季度汇报")
assert len(provider.title_calls) == 2
assert mgr.session_store.load("lang3").title == "季度汇报整理"
class _SettingsRecorder(TitleAwareProvider):
def __init__(self, chat_turns, titles):
super().__init__(chat_turns, titles)
self.title_settings: list[dict] = []
def complete(self, *, model, messages, tools=None, **settings):
if messages and "title chat sessions" in str(messages[0].get("content", "")):
self.title_settings.append(dict(settings))
return super().complete(model=model, messages=messages, tools=tools, **settings)
async def test_a_model_that_cannot_turn_reasoning_off_still_gets_a_title(tmp_path):
# OPE-201: an endpoint whose reasoning is mandatory refuses effort "none" (OpenRouter
# GLM 5.3 answers 400). The title is asked once more without the effort, with room for
# the reasoning the model spends first.
refused = RuntimeError("Error code: 400 - {'error': {'message': 'Reasoning is mandatory for "
"this endpoint and cannot be disabled.', 'code': 400}}")
provider = _SettingsRecorder([_text("sure")], [refused, "Release Notes Draft"])
mgr = SessionManager(workspace=tmp_path, provider=provider)
await _turn(mgr, "s20", "draft the release notes")
first, second = provider.title_settings
assert first["reasoning_effort"] == "none" and first["max_tokens"] == 64
assert "reasoning_effort" not in second
assert second["max_tokens"] == SessionManager._AUTOTITLE_REASONING_MAX_TOKENS
assert mgr.session_store.title_state("s20")["auto_title"] == "Release Notes Draft"
async def test_other_title_failures_are_not_retried(tmp_path):
provider = _SettingsRecorder([_text("ok")], [RuntimeError("model down"), "Never Used"])
mgr = SessionManager(workspace=tmp_path, provider=provider)
await _turn(mgr, "s21", "summarize this doc")
assert len(provider.title_settings) == 1
assert mgr.session_store.title_state("s21")["auto_title"] is None
+14 -2
View File
@@ -52,12 +52,24 @@ def test_base_url_omitted_when_none(monkeypatch):
# -- ollama URL normalization ---------------------------------------------------
def test_normalize_ollama_url():
assert _normalize_ollama_url(None) == "http://localhost:11434/v1"
# The default is 127.0.0.1 (OPE-229); an address the user typed is kept as typed.
assert _normalize_ollama_url(None) == "http://127.0.0.1:11434/v1"
assert (
_normalize_ollama_url("http://localhost:11434") == "http://localhost:11434/v1"
)
assert _normalize_ollama_url("http://h:1/v1/") == "http://h:1/v1"
assert _normalize_ollama_url(" ") == "http://localhost:11434/v1"
assert _normalize_ollama_url(" ") == "http://127.0.0.1:11434/v1"
def test_ollama_defaults_never_use_localhost():
"""OPE-229: on Windows `localhost` tries ::1 first and every new Ollama connection
waits about 2 s. Every built-in default must be 127.0.0.1."""
from coworker.providers import ollama_facts
from coworker.providers.registry import DEFAULT_OLLAMA_URL
assert DEFAULT_OLLAMA_URL == "http://127.0.0.1:11434"
assert ollama_facts.native_base(None) == "http://127.0.0.1:11434"
assert ollama_facts.native_base("http://localhost:11434/v1") == "http://localhost:11434"
def test_build_ollama_client_uses_base_url(monkeypatch):
+62
View File
@@ -0,0 +1,62 @@
"""OPE-245: the `timing` sidecar measures with a millisecond clock on every OS.
On Windows `time.monotonic()` ticks every 15.625 ms (GetTickCount64), so a tool that took
4 ms was saved as `tool_ms` 0 or 15.6. `time.perf_counter()` is monotonic too and ticks in
fractions of a microsecond everywhere."""
from __future__ import annotations
import asyncio
import time
from coworker.engine import TurnEngine
from coworker.permissions import PermissionEngine
from coworker.providers import AssistantTurn, ModelCapabilities, ProviderClient, ToolCall
from coworker.tools import ToolRegistry
class _Provider(ProviderClient):
def __init__(self, turns):
self._turns = list(turns)
def complete(self, *, model, messages, tools=None, **settings):
return self._turns.pop(0)
def capabilities(self, model):
return ModelCapabilities()
class _ReadOnly:
requires_approval = False
risk_level = "low"
def four_ms() -> dict:
"""Busy for about 4 ms."""
end = time.perf_counter() + 0.004
while time.perf_counter() < end:
pass
return {"ok": True}
def test_tool_ms_has_millisecond_resolution(tmp_path):
registry = ToolRegistry()
registry.register(four_ms, metadata=_ReadOnly())
engine = TurnEngine(
provider=_Provider([
AssistantTurn(tool_calls=[ToolCall(id="c1", name="four_ms", arguments={})],
finish_reason="tool_calls"),
AssistantTurn(text="done", finish_reason="stop"),
]),
registry=registry,
permissions=PermissionEngine(workspace_root=tmp_path),
model="gpt-5.5",
)
async def go():
return [ev async for ev in engine.run("go")]
asyncio.run(go())
tool = next(m for m in engine.messages if m.get("role") == "tool")
# 15.6 ms ticks would give 0 or ~15.6 here.
assert 3.5 <= tool["timing"]["tool_ms"] < 15