diff --git a/src/agent_env/env/gateway/triggers.py b/src/agent_env/env/gateway/triggers.py index 0108f534f..4388ed14f 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,16 +1405,28 @@ 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. A trigger removed meanwhile, or replaced by one of its id, + is neither checked again nor detected.""" 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: + provoking, trig["recheck"] = trig["recheck"] or provoking, 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 not self._armed(tid, trig): + break finally: - trig["evaluating"] = False - if matched and trig["status"] == "armed": + trig["evaluating"], trig["recheck"] = False, None + 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 fe0285ad8..f014363ec 100644 --- a/tst/unit/env/gateway/triggers_test.py +++ b/tst/unit/env/gateway/triggers_test.py @@ -456,6 +456,89 @@ 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_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, + 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"}},