Skip to content

Self-improving AI Employees for everyone

fix(approvals): the watchdog's round expires a gate's draft too (1b.10)

trohitg780d9ba92d3d
diff --git a/docs-internal/chat_protocol.md b/docs-internal/chat_protocol.md
index c97a6a55..600da556 100644
--- a/docs-internal/chat_protocol.md
+++ b/docs-internal/chat_protocol.md
@@ -617,9 +617,10 @@ What an employee sends to someone waits for the owner's OK while its workflow as
cancels only the drafts that still wait (see [Branches](#branches)), so a failure shows again on the way back.
- **Expiry.** A gate's draft expires after the node's `timeout_hours` (168 by default), and the waiting gate ends it
`expired` and lets nothing through. A held call's draft expires 168 hours after it was made
- (`DEFAULT_TIMEOUT_HOURS`); the chat watchdog ends it on its next round (`reconcile.expire_due`), announces it and
- tells the employee. A decision that reaches a draft past its expiry ends it the same way and answers `expired`.
- Rows are kept until their workflow is deleted.
+ (`DEFAULT_TIMEOUT_HOURS`). The chat watchdog ends every draft past its expiry on its next round
+ (`reconcile.expire_due`) and announces it: a held call's, telling the employee, and a gate's, so one no gate waits
+ on any more does not stay waiting. A decision that reaches a draft past its expiry ends it the same way and answers
+ `expired`. Rows are kept until their workflow is deleted.
- **Generations that wait for the owner** (RFC-0007, `approval_wait_version` 1, frozen per generation). Start does
not write it yet, so every generation works as above until it does. In such a generation:
- **What asks** is the workflow's mode (`workflow_rules.mode`), applied to the call's class
diff --git a/server/services/approvals/reconcile.py b/server/services/approvals/reconcile.py
index 3e84a7fa..c5e386c6 100644
--- a/server/services/approvals/reconcile.py
+++ b/server/services/approvals/reconcile.py
@@ -23,8 +23,9 @@ outcome, since it may have gone out.
Orphaned gate rows are ended once per process, the first time the drafts
are listed; local rows created by this process are never touched.
-Expiry: a held call waits on nothing, so the chat watchdog ends one past
-its expiry every round (:func:`expire_due`). Rows are kept until their
+Expiry: the chat watchdog ends every draft past its expiry each round
+(:func:`expire_due`): a held call waits on nothing, and a gate's draft ends
+even when its gate no longer waits on it. Rows are kept until their
workflow is deleted.
Waits: a decided row a turn waits on is nudged again from the same round
@@ -133,20 +134,20 @@ async def recover_sends(database: Any, *, now: datetime | None = None) -> List[s
async def expire_due(database: Any, *, now: datetime | None = None) -> List[str]:
- """End the held calls past their expiry (``decisions.expire_if_due``,
- which announces each). A gate's row is its own node's to end."""
+ """End the drafts past their expiry (``decisions.expire_if_due``, which
+ announces each and tells the employee about a held call): a gate's
+ too, so one no gate waits on any more does not wait forever. A gate
+ that still waits reads it expired and lets nothing through."""
from services.approvals.decisions import expire_if_due
now = now or datetime.now(timezone.utc)
expired: List[str] = []
for row in await store.list_pending(database):
- if row.kind != "tool_call":
- continue
settled = await expire_if_due(database, row, now)
if settled.status == "expired":
expired.append(settled.id)
if expired:
- logger.info("Ended held drafts past their expiry", count=len(expired))
+ logger.info("Ended drafts past their expiry", count=len(expired))
return expired
diff --git a/server/tests/services/approvals/test_approval_gate.py b/server/tests/services/approvals/test_approval_gate.py
index 970f3b94..d7ad1713 100644
--- a/server/tests/services/approvals/test_approval_gate.py
+++ b/server/tests/services/approvals/test_approval_gate.py
@@ -165,6 +165,17 @@ async def test_an_expired_draft_lets_nothing_through(harness):
assert result["status"] == "expired" and result["approved"] is False
+async def test_the_round_expires_a_gate_draft_and_the_gate_lets_nothing_through(harness):
+ # 1b.10: the chat watchdog's round ends a gate's draft past its expiry;
+ # a gate still waiting on it reads it expired.
+ task = asyncio.ensure_future(run_gate(harness))
+ (row,) = await pending(harness)
+ later = store.aware(row.expires_at) + timedelta(minutes=1)
+ assert await reconcile.expire_due(harness.database, now=later) == [row.id]
+ result = (await asyncio.wait_for(task, 5))["result"]
+ assert result["status"] == "expired" and result["approved"] is False
+
+
async def test_a_shutting_down_worker_hands_the_wait_back(harness, monkeypatch):
monkeypatch.setattr(harness.gate, "_worker_shutting_down", lambda: True)
result = await run_gate(harness)
diff --git a/server/tests/services/approvals/test_tool_calls.py b/server/tests/services/approvals/test_tool_calls.py
index 818fadfe..95c4fbd7 100644
--- a/server/tests/services/approvals/test_tool_calls.py
+++ b/server/tests/services/approvals/test_tool_calls.py
@@ -149,7 +149,7 @@ async def test_a_held_call_past_its_expiry_ends_on_its_own(harness, nodes_loaded
await rules.set_ask_first(harness.database, "wf", True)
await tool_calls.check(call(), get_node_class("whatsappSend"))
(held,) = await store.list_approvals(harness.database, status="pending")
- # A gate's draft past its expiry is its own node's to end.
+ # A gate's draft no gate waits on any more ends too (1b.10).
gate, _ = await store.get_or_create(
harness.database,
idempotency_key="g:due",
@@ -158,11 +158,12 @@ async def test_a_held_call_past_its_expiry_ends_on_its_own(harness, nodes_loaded
later = store.aware(held.expires_at) + timedelta(hours=1)
harness.broadcaster.frames.clear()
- assert await reconcile.expire_due(harness.database, now=later) == [held.id]
+ assert set(await reconcile.expire_due(harness.database, now=later)) == {held.id, gate.id}
assert (await store.get(harness.database, held.id)).status == "expired"
- assert (await store.get(harness.database, gate.id)).status == "pending"
+ assert (await store.get(harness.database, gate.id)).status == "expired"
lifecycle = [frame["data"]["type"] for frame in harness.broadcaster.frames if frame["type"] == "approval_lifecycle"]
- assert lifecycle == ["com.opencompany.approval.expired"]
+ assert lifecycle == ["com.opencompany.approval.expired"] * 2
+ # Only a held call's employee is told; a gate's run reads its own row.
(note,) = await notes.claim_notes(harness.database, session_id="wf", run_id="r_next")
assert note.kind == "update" and "expired unsent" in note.text
# Ended once.