feat(issue-5): DelegationService, cli router, ensure_subscriber_daemon; lib.sh helpers delegate to python
Adds the python side of the delegation helpers and switches lib.sh over to it.
daemon.py port of start_watchdog. Same monitor path, same pgrep pattern, so
a daemon started by either implementation is found by the other;
same stderr wording and exit 1 when reconcile.sh is missing; same
"print the PID" contract, multiple pgrep matches included, since
the shell echoed them all and trimming would be a silent change.
start_new_session detaches it, and SIGHUP is ignored across the
exec so it outlives its parent exactly as nohup did.
cli.py thin router. submit/publish forward to the existing main() of the
module that already owned the behaviour, so exit codes, stdout
shape and side effects cannot drift between the shell path and
the script path. Only ensure-daemon carries logic of its own.
service.py DelegationService facade, with an optional persistent connection
for the phase 2a engine. CLI and shell always use one-shot.
lib.sh keeps the three legacy bodies verbatim as _*_legacy behind
MAM_DELEGATION_PY=0, which is the instant rollback switch. PYTHONPATH is passed
explicitly so `-m` resolves lib_py from the tree this lib.sh belongs to, which
is what makes a frozen loop run frozen delegation code. start_watchdog survives
as an alias for ensure_subscriber_daemon.
The wrapper and run_loop.sh are untouched; reconcile.sh gains only the
ensure_env_loaded line from the previous commit.
Verified: C-01..C-09 pass against both the legacy and the python bodies, the
daemon dedups and survives its parent, the cli reproduces the legacy failure
line exactly, and publish failure semantics (status synced, seq consumed,
audit records published=false) are identical through the shim and the cli.
tier1+tier2 109 passed; bash -n clean on all six shell files.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UrDNJkHmm18uu7n7JwV5Qd
This commit is contained in:
1 parent
fb3498e23b
commit
78cb9ecfa9
7 files changed
+423
-3
No files matched your search
@@ -0,0 +1,67 @@
|
||||
"""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):
|
||||
"""Publish one event. Returns the same exit status as the CLI (0 / 2)."""
|
||||
prepared = publish_event.prepare_event(
|
||||
job_id=job_id, event=event, detail=detail,
|
||||
data=data, registry_dir=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)
|
||||
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)
|
||||
Reference in new issue
Block a user