Skip to content

The agent that grows with you

fix: an approval leader publishes its timeout before a follower can find it

kshitijk4poor8acb7f65801f
diff --git a/tests/tools/test_approval_gateway_wait_late_choice.py b/tests/tools/test_approval_gateway_wait_late_choice.py
index 8062fa2accd..6b3fd723900 100644
--- a/tests/tools/test_approval_gateway_wait_late_choice.py
+++ b/tests/tools/test_approval_gateway_wait_late_choice.py
@@ -134,6 +134,28 @@ def test_coalesced_follower_waits_the_leaders_captured_timeout(monkeypatch):
assert seen == [3600]
+def test_leader_publishes_its_timeout_before_the_pre_request_hooks(monkeypatch):
+ """A follower can find the leader while its ``pre_approval_request`` hooks run, before the prompt
+ is sent; the leader's timeout must already be on the entry by then."""
+ _clear()
+ monkeypatch.setattr(wait_mod._ctx, "_get_approval_timeout", lambda: 3600)
+ published = []
+
+ def hook(name, **kw):
+ if name == "pre_approval_request":
+ published.append(mod._gateway_queues[SESSION_KEY][0].data.get("timeout_s"))
+
+ monkeypatch.setattr(wait_mod._ctx, "_fire_approval_hook", hook)
+ seen = []
+ monkeypatch.setattr(wait_mod, "_poll_event",
+ lambda event, session_key, *, interrupt_log, timeout_s=None: seen.append(timeout_s) or "timeout")
+
+ wait_mod._await_gateway_decision(SESSION_KEY, lambda data: None, dict(APPROVAL))
+
+ assert published == [3600]
+ assert seen == [3600]
+
+
def test_resolve_commits_the_choice_before_releasing_the_approval_lock(monkeypatch):
"""``_drop_entry`` reads ``entry.result`` and leaves the queue in one ``_lock`` section, which only
closes the race if ``resolve_gateway_approval`` commits ``result``/``event`` INSIDE the section that
diff --git a/tools/approval_gateway_wait.py b/tools/approval_gateway_wait.py
index 55802c14540..398ee417e23 100644
--- a/tools/approval_gateway_wait.py
+++ b/tools/approval_gateway_wait.py
@@ -165,6 +165,9 @@ def _await_gateway_decision(session_key: str, notify_cb, approval_data: dict, *,
}
keys = list(approval_data.get("pattern_keys") or [])
entry = _ApprovalEntry(approval_data)
+ # Captured before the entry is published: a coalesced follower can find it while the pre-request
+ # hooks still run, and must wait this same timeout rather than re-read a since-edited config.
+ timeout_s = entry.data["timeout_s"] = _ctx._get_approval_timeout()
def _enqueue() -> None:
register_prepared_approval(session_key, entry)
@@ -233,8 +236,6 @@ def _await_gateway_decision(session_key: str, notify_cb, approval_data: dict, *,
_ctx._fire_approval_hook("pre_approval_request", **payload)
# Bridges sync agent thread → async gateway.
try:
- timeout_s = _ctx._get_approval_timeout()
- entry.data["timeout_s"] = timeout_s
notify_cb(dict(entry.data))
approval_published()
except Exception as exc: