Skip to content

The agent that grows with you

fix(compression): propagate timing hooks to the protected-call worker

kshitijk4poord24e6a34d2c1
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index fa67c0b3f2b..ba0baa87401 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -484,29 +484,35 @@ def _aux_progress_active() -> bool:
@contextlib.contextmanager
-def aux_progress_hook(hook):
- """Install *hook* as the current thread's aux forward-progress callback.
+def _aux_thread_local_hook(local: threading.local, hook):
+ """Install one thread-local hook callback and restore its prior value.
- ``hook=None`` is a no-op passthrough so callers can wire it
- unconditionally. Re-entrant-safe: restores the previous hook on exit.
+ ``hook=None`` (or any non-callable) is a no-op passthrough so callers can
+ wire it unconditionally. Re-entrant-safe: restores the previous hook on
+ exit. Shared by the forward-progress hook and the content-free timing
+ hooks — one save/restore implementation, three thread-local slots.
"""
- prev = getattr(_aux_progress, "hook", None)
- _aux_progress.hook = hook if callable(hook) else prev
+ previous = getattr(local, "hook", None)
+ local.hook = hook if callable(hook) else previous
try:
yield
finally:
- _aux_progress.hook = prev
+ local.hook = previous
@contextlib.contextmanager
-def _aux_timing_hook(local: threading.local, hook):
- """Install one content-free timing hook and restore its prior value."""
- previous = getattr(local, "hook", None)
- local.hook = hook if callable(hook) else previous
- try:
+def aux_progress_hook(hook):
+ """Install *hook* as the current thread's aux forward-progress callback.
+
+ ``hook=None`` is a no-op passthrough so callers can wire it
+ unconditionally. Re-entrant-safe: restores the previous hook on exit.
+ """
+ with _aux_thread_local_hook(_aux_progress, hook):
yield
- finally:
- local.hook = previous
+
+
+# Back-compat alias — the timing hooks were introduced with this name.
+_aux_timing_hook = _aux_thread_local_hook
def _run_protected_sync_provider_call(
@@ -540,14 +546,24 @@ def _run_protected_sync_provider_call(
raise AuxiliaryExplicitCancellation()
progress_hook = getattr(_aux_progress, "hook", None)
+ # Timing hooks ride along with the progress hook: _create_with_progress
+ # fires _notify_aux_dispatch/_notify_aux_provider_response from whichever
+ # thread runs the provider callback, so an owner-thread-only install would
+ # silently drop provider_dispatch_ms / time_to_first_progress_ms whenever
+ # the protected daemon path is taken.
+ dispatch_hook = getattr(_aux_dispatch, "hook", None)
+ provider_response_hook = getattr(_aux_provider_response, "hook", None)
provider_context = contextvars.copy_context()
done = threading.Event()
outcome: dict[str, Any] = {}
def _provider_worker() -> None:
try:
- with aux_progress_hook(progress_hook), aux_interrupt_protection(
- cancel_check=cancel_check
+ with (
+ aux_progress_hook(progress_hook),
+ _aux_thread_local_hook(_aux_dispatch, dispatch_hook),
+ _aux_thread_local_hook(_aux_provider_response, provider_response_hook),
+ aux_interrupt_protection(cancel_check=cancel_check),
):
outcome["result"] = callback(kwargs)
except BaseException as exc:
diff --git a/tests/agent/test_fast_compression_lane.py b/tests/agent/test_fast_compression_lane.py
index 6072702331c..8326dd66e75 100644
--- a/tests/agent/test_fast_compression_lane.py
+++ b/tests/agent/test_fast_compression_lane.py
@@ -399,3 +399,44 @@ def test_reasoning_effort_aliases_certify_like_none():
lane = _resolve({**base, "reasoning_effort": not_disabled})
assert lane.certified_non_reasoning is False, not_disabled
assert lane.max_tokens is None, not_disabled
+
+
+def test_timing_hooks_propagate_to_protected_call_worker_thread():
+ """The protected daemon path must carry the timing hooks across threads.
+
+ _run_protected_sync_provider_call runs the provider callback on a daemon
+ worker. The dispatch/provider-response hooks are threading.local, so
+ without explicit propagation provider_dispatch_ms and
+ time_to_first_progress_ms silently vanish whenever compression takes the
+ protected path (the common case: aux_interrupt_protection + hard-cancel
+ source both active).
+ """
+ from agent.auxiliary_client import (
+ _aux_timing_hook,
+ _aux_dispatch,
+ _aux_provider_response,
+ _notify_aux_dispatch,
+ _notify_aux_provider_response,
+ _run_protected_sync_provider_call,
+ aux_interrupt_protection,
+ )
+
+ seen = []
+
+ def _callback(_kwargs):
+ # Runs on the daemon worker thread — both notifies must reach the
+ # hooks installed on the owner thread.
+ _notify_aux_dispatch()
+ _notify_aux_provider_response()
+ return "ok"
+
+ with (
+ _aux_timing_hook(_aux_dispatch, lambda: seen.append("dispatch")),
+ _aux_timing_hook(_aux_provider_response, lambda: seen.append("response")),
+ aux_interrupt_protection(cancel_check=lambda: False),
+ ):
+ result = _run_protected_sync_provider_call(_callback, {})
+
+ assert result == "ok"
+ assert "dispatch" in seen
+ assert "response" in seen