Round-2 review of952657e(reviewer-grok-01): R1's disk-heartbeat evidence comes from events.log, but publish_event appended to it only after with_retry(_publish_once) returned - after the broker ACK or all of its retries. With the public broker at ~5 s per publish, an agent's heartbeat reached the disk after the grace window and the live job was closed as deadline_timeout (test_wrapper_idle_timeout_grace_accepts_disk_heartbeat failed 2/2 on952657ehere, as it did for the reviewer). - publish_event: new record_local_event appends the prepared payload to events.log right after prepare_event (validation and seq allocation first) and before the wire publish; record_outcome takes local_event_recorded=True so the line is not written twice, while the audit "published" entry and the status sync stay after the publish - DelegationService.publish_event: the same local-first order - wrapper comment and VERSIONS.md v4.4.2 item 6 now say the heartbeat is on disk before the publish; new-test count 21 - tests: the heartbeat is already in events.log when the publish starts (and is recorded once), and a publish failing on every retry leaves one local record with published=False in the audit; both fail on952657e. The wrapper disk-heartbeat test keeps the real publish_event path and now passes 3/3 at the same broker latency Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HCGpJXhSZasJLU38ksDghe
108 lines
4.5 KiB
Python
108 lines
4.5 KiB
Python
"""DelegationService — one façade over registering, publishing and supervising.
|
|
|
|
Phase 1 keeps every behaviour in the existing entry points; this only groups
|
|
them so a long-running caller (the phase 2a engine) has a single object to hold,
|
|
optionally with a persistent MQTT connection. CLI and shell callers always use
|
|
`persistent=False`, because a process that exits after one publish gains nothing
|
|
from connection reuse.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from lib_py.delegation import daemon, mqtt_common, publish_event, registry
|
|
|
|
logger = logging.getLogger("delegate_job.service")
|
|
|
|
|
|
class DelegationService(object):
|
|
def __init__(self, registry_dir=None, persistent=False):
|
|
self.registry_dir = registry_dir or mqtt_common.DEFAULT_REGISTRY_DIR
|
|
self.persistent = persistent
|
|
self._publisher = None
|
|
|
|
# -- lifecycle ---------------------------------------------------------
|
|
def __enter__(self):
|
|
if self.persistent:
|
|
from lib_py.delegation.publisher import PersistentPublisher
|
|
self._publisher = PersistentPublisher()
|
|
self._publisher.open()
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc, tb):
|
|
self.close()
|
|
return False
|
|
|
|
def close(self):
|
|
if self._publisher is not None:
|
|
self._publisher.close()
|
|
self._publisher = None
|
|
|
|
# -- operations --------------------------------------------------------
|
|
def submit_job(self, prompt, agent="claude-code", agent_session="herdr:claude", **kw):
|
|
return registry.register_job(
|
|
prompt=prompt, agent=agent, agent_session=agent_session,
|
|
registry_dir=self.registry_dir, **kw
|
|
)
|
|
|
|
def publish_event(self, job_id, event, detail="", data=None, attempts=3,
|
|
expect_dispatch_token=None, attempt_id=None):
|
|
"""Publish one event. Returns the same exit status as the CLI (0 / 2 / 4)."""
|
|
eff_dispatch_token = expect_dispatch_token
|
|
eff_attempt_id = attempt_id
|
|
if data and isinstance(data, dict):
|
|
if eff_dispatch_token is None:
|
|
eff_dispatch_token = data.get("dispatch_token")
|
|
if eff_attempt_id is None:
|
|
eff_attempt_id = data.get("attempt_id")
|
|
|
|
try:
|
|
rec = self.load_job(job_id)
|
|
actual_tok = rec.get("dispatch_token")
|
|
rec_attempt = rec.get("attempt_id", rec.get("iteration", 1))
|
|
except FileNotFoundError:
|
|
rec = None
|
|
actual_tok = None
|
|
rec_attempt = 1
|
|
|
|
if rec is not None and actual_tok is not None:
|
|
if eff_dispatch_token is None and eff_attempt_id is None:
|
|
if event in ("completed", "error", "cancelled") and rec_attempt > 1:
|
|
logger.error("dispatch token required: redispatched job %s carries a dispatch token "
|
|
"but none was provided", job_id)
|
|
return 4
|
|
elif eff_dispatch_token is not None and actual_tok != eff_dispatch_token:
|
|
return 4
|
|
elif eff_attempt_id is not None and rec_attempt != eff_attempt_id:
|
|
return 4
|
|
elif eff_dispatch_token is not None and actual_tok != eff_dispatch_token:
|
|
return 4
|
|
|
|
prepared = publish_event.prepare_event(
|
|
job_id=job_id, event=event, detail=detail,
|
|
data=data, registry_dir=self.registry_dir,
|
|
dispatch_token=eff_dispatch_token, attempt_id=eff_attempt_id,
|
|
)
|
|
# Local first, as in the CLI: the event reaches events.log before the
|
|
# publish can block on the broker (see publish_event.record_local_event).
|
|
publish_event.record_local_event(prepared, self.registry_dir)
|
|
publisher = self._publisher
|
|
if publisher is None:
|
|
from lib_py.delegation.publisher import OneShotPublisher
|
|
publisher = OneShotPublisher()
|
|
ok, error = publisher.publish_with_retry(prepared, attempts=attempts)
|
|
publish_event.record_outcome(
|
|
prepared, self.registry_dir, ok, error,
|
|
expected_dispatch_token=eff_dispatch_token,
|
|
expected_attempt=eff_attempt_id,
|
|
local_event_recorded=True,
|
|
)
|
|
return 0 if ok else 2
|
|
|
|
def ensure_subscriber_daemon(self, job_id, workdir=None):
|
|
return daemon.ensure_subscriber_daemon(job_id, workdir)
|
|
|
|
def load_job(self, job_id) -> Dict[str, Any]:
|
|
return mqtt_common.load_job(job_id, self.registry_dir)
|