Skip to content

The agent that grows with you

fix(delegation): an interim task-failure notice never claims, acknowledges or dedups against the batch's final result

Teknium70214e9ab8ee
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index fc356f608c6..c5ce3cb4b2a 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -950,7 +950,14 @@ class GatewayNotificationsMixin:
evt_type = str(evt.get("type") or "")
if evt_type == "async_delegation":
producer_id = str(evt.get("delegation_id") or "")
- return (evt_type, producer_id, "") if producer_id else None
+ if not producer_id:
+ return None
+ if evt.get("task_failure_notice"):
+ # An interim per-task notice is its own producer event: it must not mark the
+ # batch's final result as already delivered, nor a sibling's notice.
+ task_idx = ((evt.get("results") or [{}])[0] or {}).get("task_index", "")
+ return (evt_type, producer_id, f"task_failure:{task_idx}")
+ return (evt_type, producer_id, "")
if evt_type == "completion":
producer_id = str(evt.get("session_id") or "")
started_at = evt.get("started_at")
@@ -1035,7 +1042,9 @@ class GatewayNotificationsMixin:
"""
claim = self._CompletionClaim()
evt_type = evt.get("type")
- if evt_type == "async_delegation":
+ # An interim per-task notice shares the batch's delegation_id but is not the durable
+ # completion; claiming that row here would acknowledge the FINAL result before it exists.
+ if evt_type == "async_delegation" and not evt.get("task_failure_notice"):
claim.delegation_id = str(evt.get("delegation_id") or "")
if claim.delegation_id:
try:
diff --git a/tests/tools/test_async_batch_task_failure_notice.py b/tests/tools/test_async_batch_task_failure_notice.py
index e1b9cce8961..427855cab31 100644
--- a/tests/tools/test_async_batch_task_failure_notice.py
+++ b/tests/tools/test_async_batch_task_failure_notice.py
@@ -46,3 +46,24 @@ def test_notice_is_not_sent_for_a_finished_batch_and_does_not_dedup_against_the_
notice = {"type": "async_delegation", "delegation_id": "deleg_x", "task_failure_notice": True, "results": [{"task_index": 2}]}
final = {"type": "async_delegation", "delegation_id": "deleg_x", "is_batch": True, "results": []}
assert _notification_event_dedup_key(notice) != _notification_event_dedup_key(final)
+
+
+def test_interim_notice_never_claims_or_acknowledges_the_batch_final_row(tmp_path, monkeypatch):
+ """Independent-review witness: a busy parent that drained the notice first acknowledged the FINAL
+ result's durable row, and the consolidated result was never delivered (nor replayed after restart)."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hh"))
+ notice = {"type": "async_delegation", "delegation_id": "deleg_x", "task_failure_notice": True, "results": [{"task_index": 0}]}
+ final = {"type": "async_delegation", "delegation_id": "deleg_x", "is_batch": True, "results": []}
+ # The notice is a non-durable event: empty token, no row touched.
+ assert ad.claim_event_delivery(notice, "tui-poller") == ""
+ ad.complete_event_delivery(notice, "")
+ assert ad.is_interim_delegation_event(notice) and not ad.is_interim_delegation_event(final)
+
+
+def test_gateway_dedup_identity_separates_notices_from_the_final_and_from_each_other():
+ from gateway.run_notifications import GatewayNotificationsMixin
+ ident = GatewayNotificationsMixin._completion_delivery_identity
+ n0 = {"type": "async_delegation", "delegation_id": "d", "task_failure_notice": True, "results": [{"task_index": 0}]}
+ n1 = {"type": "async_delegation", "delegation_id": "d", "task_failure_notice": True, "results": [{"task_index": 1}]}
+ final = {"type": "async_delegation", "delegation_id": "d", "is_batch": True, "results": []}
+ assert len({ident(n0), ident(n1), ident(final)}) == 3
diff --git a/tools/async_delegation.py b/tools/async_delegation.py
index 6fba6071da3..97a0df13d4f 100644
--- a/tools/async_delegation.py
+++ b/tools/async_delegation.py
@@ -323,8 +323,17 @@ def claim_completion_delivery(delegation_id: str, claim_id: str) -> bool:
return cur.rowcount == 1
+def is_interim_delegation_event(evt: Dict[str, Any]) -> bool:
+ """An early per-task notice for a batch that is still running. It shares the batch's
+ ``delegation_id`` but is NOT the durable completion: it must never claim, acknowledge or
+ dedup against the final result's row (independent review reproduced exactly that loss)."""
+ return evt.get("type") == "async_delegation" and bool(evt.get("task_failure_notice"))
+
+
def claim_event_delivery(evt: Dict[str, Any], consumer: str) -> Optional[str]:
- """Claim a durable delegation event; non-durable events need no token."""
+ """Claim a durable delegation event; non-durable events (and interim notices) need no token."""
+ if is_interim_delegation_event(evt):
+ return ""
delegation_id = str(evt.get("delegation_id") or "") if evt.get("type") == "async_delegation" else ""
if not delegation_id:
return ""