Skip to content
Python

fix(engine): time model calls, tools and waits with perf_counter, which ticks every millisecond on Windows (OPE-245)

Devika Vermaaddd78f3b9ed
diff --git a/coworker/engine.py b/coworker/engine.py
index 716c262b..351e8eac 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -484,11 +484,11 @@ class TurnEngine:
cancel_wait.cancel()
async def _wait_tool(self, tool_call, coro, interrupted):
- started, clock = time.time(), time.monotonic()
+ started, clock = time.time(), time.perf_counter()
try:
return await self._interruptible(coro, interrupted)
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)
def _timed_result(self, tool_call, result):
message = _tool_result_message(tool_call, result)
@@ -802,7 +802,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:
@@ -879,7 +879,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],
@@ -2283,7 +2283,7 @@ class TurnEngine:
def _execute_sync(self, tool_call: ToolCall) -> tuple[Any, str]:
"""Execute one authorized call (runs in a worker thread)."""
- 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):
@@ -2302,7 +2302,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)
diff --git a/tests/test_timing_clock.py b/tests/test_timing_clock.py
new file mode 100644
index 00000000..fbd55585
--- /dev/null
+++ b/tests/test_timing_clock.py
@@ -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