Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(approvals): held drafts expire on their own, and say so
trohitg9a2ee511eaf6
diff --git a/server/services/approvals/decisions.py b/server/services/approvals/decisions.pyindex 3418583d..cef9aebb 100644--- a/server/services/approvals/decisions.py+++ b/server/services/approvals/decisions.py@@ -16,6 +16,10 @@Each decision is a compare-and-swap on the row's revision, recorded with its``decision_key`` in the same transaction (``approval_decisions``): the samekey again returns the row as it is. Never imports ``nodes/``.++A pending row past its expiry ends ``expired`` (:func:`expire_if_due`) when+a decision reaches it, and a held call's row also when the chat watchdog+sweeps (``reconcile.expire_due``); whoever ends it announces it."""from __future__ import annotations@@ -26,6 +30,7 @@ from typing import Any, Dict, Optional, Tuplefrom models.approvals import ApprovalRequestfrom services.approvals import storefrom services.approvals.contract import DEFAULT_TIMEOUT_HOURS, RESTORE_SECONDS, UNDO_SECONDS+from services.approvals.listeners import change_of, notify_approval_changedDECISIONS = ("send", "undo", "discard", "restore", "retry")#: Why a decision on a row that already moved on was refused.@@ -57,15 +62,23 @@ def _settled(row: ApprovalRequest) -> DecisionRefused:return DecisionRefused(SETTLED_ERRORS.get(row.status, "already_decided"))-async def _expire_if_due(database: Any, row: ApprovalRequest, now: datetime) -> ApprovalRequest:- """A pending row past its expiry ends ``expired`` (expiry is lazy)."""+async def expire_if_due(database: Any, row: ApprovalRequest, now: datetime) -> ApprovalRequest:+ """A pending row past its expiry ends ``expired``. The caller that ends+ it announces it, and tells the employee when it held a tool call; one+ another caller ended meanwhile comes back as it is."""expires = store.aware(row.expires_at)if row.status != "pending" or expires is None or expires > now:return rowtry:- return await store.settle(database, row.id, expected_revision=row.revision, status="expired")+ expired = await store.settle(database, row.id, expected_revision=row.revision, status="expired")except store.ApprovalConflict:return await store.get(database, row.id) or row+ await notify_approval_changed(change_of(expired, "expired"))+ if expired.kind == "tool_call":+ from services.approvals.execution import tell_employee++ await tell_employee(database, expired)+ return expireddef _send_values(row: ApprovalRequest, text: Any, subject: Any, now: datetime) -> Dict[str, Any]:@@ -119,7 +132,7 @@ async def decide(if await store.find_decision(database, row.id, decision_key) is not None:return (await store.get(database, row.id)) or row, Truenow = now or _utcnow()- row = await _expire_if_due(database, row, now)+ row = await expire_if_due(database, row, now)record = (decision, decision_key, actor)kind = row.kind or "gate"if decision == "send":diff --git a/server/services/approvals/reconcile.py b/server/services/approvals/reconcile.pyindex 0cf29a60..042423b4 100644--- a/server/services/approvals/reconcile.py+++ b/server/services/approvals/reconcile.py@@ -19,6 +19,10 @@ since it may have gone out.Runs once per process, the first time the drafts are listed; local rowscreated 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+workflow is deleted."""from __future__ import annotations@@ -106,6 +110,24 @@ async def recover_sends(database: Any, *, now: datetime | None = None) -> List[sreturn touched+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."""+ 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))+ return expired++async def reconcile_once(database: Any) -> None:global _doneif _done:@@ -124,4 +146,4 @@ def reset_for_tests() -> None:_done = False-__all__ = ["PROCESS_STARTED", "cancel_orphaned_pending", "reconcile_once", "recover_sends"]+__all__ = ["PROCESS_STARTED", "cancel_orphaned_pending", "expire_due", "reconcile_once", "recover_sends"]diff --git a/server/services/chat/watchdog.py b/server/services/chat/watchdog.pyindex 23b5dcf2..8cb8028d 100644--- a/server/services/chat/watchdog.py+++ b/server/services/chat/watchdog.py@@ -97,6 +97,16 @@ class ChatRunWatchdog:raiseexcept Exception: # noqa: BLE001 - the next sweep tries againlogger.warning("Chat run sweep failed", exc_info=True)+ try:+ # Drafts held for the owner wait on nothing, so they end here+ # once past their expiry (services/approvals/reconcile.py).+ from services.approvals.reconcile import expire_due++ await expire_due(self.database)+ except asyncio.CancelledError:+ raise+ except Exception: # noqa: BLE001 - the next round tries again+ logger.warning("Held draft expiry failed", exc_info=True)await asyncio.sleep(self.interval)diff --git a/server/tests/services/approvals/test_tool_calls.py b/server/tests/services/approvals/test_tool_calls.pyindex 791230fb..7b506cd5 100644--- a/server/tests/services/approvals/test_tool_calls.py+++ b/server/tests/services/approvals/test_tool_calls.py@@ -124,6 +124,35 @@ async def test_tools_that_cannot_wait_are_refused_or_restricted(harness, nodes_lassert await store.list_approvals(harness.database) == []+async def test_a_held_call_past_its_expiry_ends_on_its_own(harness, nodes_loaded):+ from datetime import timedelta++ from services.approvals import reconcile+ from services.chat import notes++ 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.+ gate, _ = await store.get_or_create(+ harness.database,+ idempotency_key="g:due",+ fields={"owner_id": "owner", "workflow_id": "wf", "node_id": "wf:approvalGate:1", "draft_text": "hi", "expires_at": held.expires_at},+ )+ 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 (await store.get(harness.database, held.id)).status == "expired"+ assert (await store.get(harness.database, gate.id)).status == "pending"+ lifecycle = [frame["data"]["type"] for frame in harness.broadcaster.frames if frame["type"] == "approval_lifecycle"]+ assert lifecycle == ["com.opencompany.approval.expired"]+ (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.+ assert await reconcile.expire_due(harness.database, now=later) == []++class Ran:"""Stands in for a plugin's execute_as_tool: records what ran."""diff --git a/server/tests/services/chat/test_watchdog.py b/server/tests/services/chat/test_watchdog.pyindex 567bb5b6..c0bfc331 100644--- a/server/tests/services/chat/test_watchdog.py+++ b/server/tests/services/chat/test_watchdog.py@@ -41,6 +41,32 @@ async def test_it_sweeps_at_once_and_keeps_sweeping_after_a_failure(monkeypatch)assert len(sweeps) == count, "the loop kept running after stop"+async def test_it_ends_held_drafts_every_round_and_survives_a_failure(monkeypatch):+ from services.approvals import reconcile++ async def sweep(database, *, temporal_status, temporal_cancel):+ return []++ rounds = []++ async def expire_due(database):+ rounds.append(database)+ if len(rounds) == 1:+ raise RuntimeError("database is locked")+ return []++ monkeypatch.setattr(watchdog.ledger, "sweep", sweep)+ monkeypatch.setattr(reconcile, "expire_due", expire_due)+ dog = watchdog.ChatRunWatchdog("db", interval=0.01)+ dog.start()+ for _ in range(50):+ if len(rounds) >= 3:+ break+ await asyncio.sleep(0.01)+ await dog.stop()+ assert len(rounds) >= 3 and set(rounds) == {"db"}++def _container(monkeypatch, client):import core.container as container_module