From a3e15b64646fff492f4b8c09a91f4642a9b120e1 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 15 Nov 2026 08:58:41 -0800 Subject: [PATCH 1/3] fix(gateway): a state trigger checks once more when a write lands while its check runs A watched write found a state trigger already evaluating and asked nothing of it, so a check that had read the state from before the write could end unmatched and leave the trigger armed although its condition now held, until some later write. A write that lands while the check runs now marks it for a re-check, which runs once the check ends unmatched, and a match is detected as that write's. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/gateway/triggers.py | 35 ++++++++++++++++++-------- tst/unit/env/gateway/triggers_test.py | 36 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/agent_env/env/gateway/triggers.py b/src/agent_env/env/gateway/triggers.py index 0108f534f..76f6f49a9 100644 --- a/src/agent_env/env/gateway/triggers.py +++ b/src/agent_env/env/gateway/triggers.py @@ -506,9 +506,10 @@ def register(self, body: Any) -> dict: self._config_set = True for tid, spec in to_add: # `running` counts its fires in flight, `alongside` holds those run beside the first (concurrent_fires), - # `waits` each fire waiting out a delay or its executor's hours, `calls` each executor call in progress. + # `waits` each fire waiting out a delay or its executor's hours, `calls` each executor call in progress, + # `recheck` the write that asked a state check under way to run again. self._triggers[tid] = {"spec": spec, "status": "armed", "detected_at": None, - "fired_at": None, "evaluating": False, + "fired_at": None, "evaluating": False, "recheck": None, "next_mark": None, "resolved": False, "fire_count": 0, "failure_count": 0, "last_failure_at": None, "rng": _make_rng(spec), "mark_gen": None, @@ -1065,10 +1066,16 @@ def _detect(self, matched: list[tuple[str, dict]], provoking: dict, return pending, timeout_s def _reevaluate_states(self, provokable: list[tuple[str, dict]], provoking: dict) -> None: + """Each armed state trigger ``provoking`` can provoke checked: once more after the check under way, which may + have read the state from before this write, when one is.""" for tid, trig in provokable: - if trig["status"] == "armed" and trig["spec"]["when"]["type"] == "state" and not trig["evaluating"]: - trig["evaluating"] = True - self._schedule(self._eval_state(tid, trig, provoking)) + if trig["status"] != "armed" or trig["spec"]["when"]["type"] != "state": + continue + if trig["evaluating"]: + trig["recheck"] = provoking + continue + trig["evaluating"] = True + self._schedule(self._eval_state(tid, trig, provoking)) def may_hold_website_request(self, role: str, app: str, method: str, path: str, status: int, op: str | None = None) -> bool: @@ -1398,13 +1405,21 @@ def _emit(self, kind: str, trigger_id: str | None, **payload) -> dict: return event async def _eval_state(self, tid: str, trig: dict, provoking: dict) -> None: + """``trig``'s check, run again while a write asked for it as it ran and it hasn't matched; a match is + detected as the write that asked for that check's.""" try: - matched = await self._run_check(trig["spec"]["when"]["check"]) - except Exception as e: - self._emit("eval_error", tid, detail=str(e)[:300]) - matched = False + while True: + trig["recheck"] = None + try: + matched = await self._run_check(trig["spec"]["when"]["check"]) + except Exception as e: + self._emit("eval_error", tid, detail=str(e)[:300]) + matched = False + if matched or trig["recheck"] is None or trig["status"] != "armed": + break + provoking = trig["recheck"] finally: - trig["evaluating"] = False + trig["evaluating"], trig["recheck"] = False, None if matched and trig["status"] == "armed": self._mark_detected(tid, trig, provoking) diff --git a/tst/unit/env/gateway/triggers_test.py b/tst/unit/env/gateway/triggers_test.py index fe0285ad8..edbaeb9c6 100644 --- a/tst/unit/env/gateway/triggers_test.py +++ b/tst/unit/env/gateway/triggers_test.py @@ -456,6 +456,42 @@ async def test_an_executor_write_prompts_only_the_state_triggers_that_list_it(en assert {tid: t["status"] for tid, t in engine._triggers.items()} == {"listed": "fired", "unlisted": "armed"} +@pytest.mark.asyncio +@pytest.mark.parametrize("second_write_holds", [True, False], ids=["it-now-holds", "it-still-doesnt"]) +async def test_a_write_landing_while_a_state_check_runs_has_it_checked_once_more(engine, monkeypatch, + second_write_holds): + """A check that read the state from before a write, while that write's own re-check was asked for, runs again: so + the trigger fires as the write that made it hold, and stays armed when nothing does.""" + reads, slow = [], asyncio.Event() + read = TriggerEngine._internal_call + + async def read_then_answer_late_once(self, tool_name, arguments, role=None): + result = await read(self, tool_name, arguments, role=role) + reads.append(result.content[0].text) + if len(reads) == 1: + await slow.wait() + return result + + monkeypatch.setattr(TriggerEngine, "_internal_call", read_then_answer_late_once) + check = {"steps": [{"tool": "gdocs_get_document_text", "args": {"documentId": "doc-1"}, "extract": "text", + "predicate": {"regex": "Budget Fit"}}]} + engine.register({"triggers": [{"id": "x", "when": {"type": "state", "check": check}, "actions": []}]}) + engine.on_tool_call("default", "gdocs_batch_update", {"documentId": "doc-1"}, _result()) + await asyncio.sleep(0.05) + if second_write_holds: + engine._test_state["doc_text"] = "Budget Fit: fine." + engine.on_tool_call("default", "gdocs_append_text", {"documentId": "doc-1"}, _result()) + await asyncio.sleep(0.05) + slow.set() + await asyncio.sleep(0.2) + + assert len(reads) == 2 and "Budget Fit" not in reads[0], reads + assert engine._triggers["x"]["status"] == ("fired" if second_write_holds else "armed") + assert not engine._triggers["x"]["evaluating"] and engine._triggers["x"]["recheck"] is None + detected = [e for e in engine._events if e["kind"] == "detected"] + assert [e["provoking"]["tool"] for e in detected] == (["gdocs_append_text"] if second_write_holds else []) + + @pytest.mark.asyncio async def test_a_delay_waits_its_virtual_time_before_the_action(engine, executor_calls, virtual_time): engine.register({"executors": {"bob": {"a2a_url": "http://bob", "role": "bob"}}, From caf0fa8bccfaf22d9697f1dbe5febaaebbb5e2e3 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 15 Nov 2026 09:02:20 -0800 Subject: [PATCH 2/3] fix(gateway): a state trigger removed while its check runs is neither checked again nor detected A removed trigger kept its armed status, so a re-check asked for before the removal could run it again and detect it, and so could its own slow check that matched afterwards, as on main. A check now repeats and detects only while the engine still holds that trigger, armed; one re-added under the same id is a new trigger. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/gateway/triggers.py | 11 ++++++--- tst/unit/env/gateway/triggers_test.py | 33 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/agent_env/env/gateway/triggers.py b/src/agent_env/env/gateway/triggers.py index 76f6f49a9..d88d4abf1 100644 --- a/src/agent_env/env/gateway/triggers.py +++ b/src/agent_env/env/gateway/triggers.py @@ -1406,7 +1406,8 @@ def _emit(self, kind: str, trigger_id: str | None, **payload) -> dict: async def _eval_state(self, tid: str, trig: dict, provoking: dict) -> None: """``trig``'s check, run again while a write asked for it as it ran and it hasn't matched; a match is - detected as the write that asked for that check's.""" + detected as the write that asked for that check's. A trigger removed meanwhile, or replaced by one of its id, + is neither checked again nor detected.""" try: while True: trig["recheck"] = None @@ -1415,14 +1416,18 @@ async def _eval_state(self, tid: str, trig: dict, provoking: dict) -> None: except Exception as e: self._emit("eval_error", tid, detail=str(e)[:300]) matched = False - if matched or trig["recheck"] is None or trig["status"] != "armed": + if matched or trig["recheck"] is None or not self._armed(tid, trig): break provoking = trig["recheck"] finally: trig["evaluating"], trig["recheck"] = False, None - if matched and trig["status"] == "armed": + if matched and self._armed(tid, trig): self._mark_detected(tid, trig, provoking) + def _armed(self, tid: str, trig: dict) -> bool: + """Whether ``trig`` is still the engine's trigger ``tid``, and armed.""" + return self._triggers.get(tid) is trig and trig["status"] == "armed" + async def _run_check(self, check: dict, ctx: dict | None = None, role: str | None = None) -> bool: bindings: dict[str, Any] = {} for step in check["steps"]: diff --git a/tst/unit/env/gateway/triggers_test.py b/tst/unit/env/gateway/triggers_test.py index edbaeb9c6..0689a0f91 100644 --- a/tst/unit/env/gateway/triggers_test.py +++ b/tst/unit/env/gateway/triggers_test.py @@ -492,6 +492,39 @@ async def read_then_answer_late_once(self, tool_name, arguments, role=None): assert [e["provoking"]["tool"] for e in detected] == (["gdocs_append_text"] if second_write_holds else []) +@pytest.mark.asyncio +@pytest.mark.parametrize("rechecked", [True, False], ids=["with-a-recheck-asked-for", "on-its-own-check"]) +async def test_a_state_trigger_removed_while_its_check_runs_is_neither_checked_again_nor_detected(engine, monkeypatch, + rechecked): + reads, slow = [], asyncio.Event() + read = TriggerEngine._internal_call + + async def read_then_answer_late_once(self, tool_name, arguments, role=None): + result = await read(self, tool_name, arguments, role=role) + reads.append(result.content[0].text) + if len(reads) == 1: + await slow.wait() + return result + + monkeypatch.setattr(TriggerEngine, "_internal_call", read_then_answer_late_once) + check = {"steps": [{"tool": "gdocs_get_document_text", "args": {"documentId": "doc-1"}, "extract": "text", + "predicate": {"regex": "Budget Fit"}}]} + engine.register({"triggers": [{"id": "x", "when": {"type": "state", "check": check}, "actions": []}]}) + if not rechecked: + engine._test_state["doc_text"] = "Budget Fit: fine." + engine.on_tool_call("default", "gdocs_batch_update", {"documentId": "doc-1"}, _result()) + await asyncio.sleep(0.05) + engine._test_state["doc_text"] = "Budget Fit: fine." + if rechecked: + engine.on_tool_call("default", "gdocs_append_text", {"documentId": "doc-1"}, _result()) + engine.remove({"ids": ["x"]}) + slow.set() + await asyncio.sleep(0.2) + + assert len(reads) == 1, reads + assert not [e for e in engine._events if e["kind"] == "detected"] + + @pytest.mark.asyncio async def test_a_delay_waits_its_virtual_time_before_the_action(engine, executor_calls, virtual_time): engine.register({"executors": {"bob": {"a2a_url": "http://bob", "role": "bob"}}, From 1ed1bd234156be231dd44f02d07b08f174d2005b Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 15 Nov 2026 09:06:21 -0800 Subject: [PATCH 3/3] fix(gateway): a state check's match is detected as the last write before the check that matched A write landing after a state check was scheduled but before it started asked for a recheck that the check's start cleared, so a match was detected as the earlier write. Each check now starts from the latest write that asked for it. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/gateway/triggers.py | 3 +-- tst/unit/env/gateway/triggers_test.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/agent_env/env/gateway/triggers.py b/src/agent_env/env/gateway/triggers.py index d88d4abf1..4388ed14f 100644 --- a/src/agent_env/env/gateway/triggers.py +++ b/src/agent_env/env/gateway/triggers.py @@ -1410,7 +1410,7 @@ async def _eval_state(self, tid: str, trig: dict, provoking: dict) -> None: is neither checked again nor detected.""" try: while True: - trig["recheck"] = None + provoking, trig["recheck"] = trig["recheck"] or provoking, None try: matched = await self._run_check(trig["spec"]["when"]["check"]) except Exception as e: @@ -1418,7 +1418,6 @@ async def _eval_state(self, tid: str, trig: dict, provoking: dict) -> None: matched = False if matched or trig["recheck"] is None or not self._armed(tid, trig): break - provoking = trig["recheck"] finally: trig["evaluating"], trig["recheck"] = False, None if matched and self._armed(tid, trig): diff --git a/tst/unit/env/gateway/triggers_test.py b/tst/unit/env/gateway/triggers_test.py index 0689a0f91..f014363ec 100644 --- a/tst/unit/env/gateway/triggers_test.py +++ b/tst/unit/env/gateway/triggers_test.py @@ -492,6 +492,20 @@ async def read_then_answer_late_once(self, tool_name, arguments, role=None): assert [e["provoking"]["tool"] for e in detected] == (["gdocs_append_text"] if second_write_holds else []) +@pytest.mark.asyncio +async def test_a_write_landing_before_a_scheduled_state_check_starts_is_the_one_it_is_detected_as(engine): + check = {"steps": [{"tool": "gdocs_get_document_text", "args": {"documentId": "doc-1"}, "extract": "text", + "predicate": {"regex": "Budget Fit"}}]} + engine.register({"triggers": [{"id": "x", "when": {"type": "state", "check": check}, "actions": []}]}) + engine.on_tool_call("default", "gdocs_batch_update", {"documentId": "doc-1"}, _result()) + engine._test_state["doc_text"] = "Budget Fit: fine." + engine.on_tool_call("default", "gdocs_append_text", {"documentId": "doc-1"}, _result()) + await asyncio.sleep(0.2) + + assert engine._triggers["x"]["status"] == "fired" + assert [e["provoking"]["tool"] for e in engine._events if e["kind"] == "detected"] == ["gdocs_append_text"] + + @pytest.mark.asyncio @pytest.mark.parametrize("rechecked", [True, False], ids=["with-a-recheck-asked-for", "on-its-own-check"]) async def test_a_state_trigger_removed_while_its_check_runs_is_neither_checked_again_nor_detected(engine, monkeypatch,