fix(issue-10,11,13,15): complete agy purge, orchestrator purge, workspace-anchored registry
Issue #10 - agy purge_artifacts left resumable pieces behind: it now also removes the SQLite sidecars (-wal/-shm/-journal), the title annotation and the presence lock (the lock only once no live process references the conversation), and residual_artifacts() reports anything under ~/.gemini/antigravity-cli still carrying the uuid. Issue #13 - the orchestrator has no herdr_sessions row, so its conversation could not be purged by any skill. `lib_py.agents purge <agent|auto> <uuid>` resolves the adapter from what is on disk (orchestrator_uuids stores bare uuids), refuses while a live process names the uuid (conversation_in_use: process list minus the caller's own lineage, plus lsof), and prints what it purged. `orc_onboard.sh --remove <uuid> --purge-conversation --yes [--agent]` runs it before the registry update so a refusal changes nothing (exit 4 without --yes, 5 while in use). Issue #11 - "Successfully removeed": conjugate add/remove explicitly. Issue #15 - the job registry and audit log followed the caller's cwd, so the pytest suite (create_session.sh with its default onboarding job, run from the repo root) had left 593 test records in the real .mam/jobs. Defaults now resolve MAM_JOBS_DIR / DELEGATE_JOB_LOGS_DIR, then $WORKSPACE_ROOT/.mam/..., then the cwd (mqtt_common.default_registry_dir / get_logs_dir; registry.py, publish_event.py, service.py, create_session.sh, stop_session.sh); the sandbox fixture pins both. The tier suites that used to leak now leave the real registry untouched (881 records before and after). Tests: test_issue10_13_purge.py (13), test_issue15_registry_isolation.py (7); T-O13 pins the corrected message; the R1 helper test and B-9 logs-dir test now assert the workspace-anchored contract; publish_event snapshot updated. Refs #10 #11 #13 #15 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCgu9Fa9iT7TbJUSzdq4Ff
This commit is contained in:
1 parent
bd3df890dc
commit
c82ea75a15
20 files changed
+723
-33
No files matched your search
@@ -1,11 +1,48 @@
|
||||
# __main__.py — CLI bridge for lib_py.agents facts and resolution (N4)
|
||||
|
||||
import sys, json, shlex
|
||||
import os, sys, json, shlex
|
||||
from lib_py.agents.registry import get_adapter, own_key, agent_of_row
|
||||
|
||||
# `purge auto` probes adapters in this order; opencode last because its
|
||||
# artifact path is one shared DB that exists for every uuid.
|
||||
PURGE_PROBE_ORDER = ('agy', 'claude', 'hermes', 'grok', 'codex', 'opencode')
|
||||
|
||||
|
||||
def _resolve_purge_target(agent_hint: str, uuid: str, workspace: str):
|
||||
"""(agent, adapter, ctx) whose on-disk artifact carries `uuid`, else None.
|
||||
|
||||
An explicit agent is trusted as long as its artifact exists. `auto` tries
|
||||
each adapter's own verification first (revalidate mode: an orchestrator
|
||||
conversation is not in any workspace cache), then a plain path check for
|
||||
adapters whose artifact path embeds the uuid.
|
||||
"""
|
||||
from lib_py.agents.base import DiscoveryContext
|
||||
names = [agent_hint] if agent_hint and agent_hint != 'auto' else list(PURGE_PROBE_ORDER)
|
||||
probes = []
|
||||
for name in names:
|
||||
adapter = get_adapter(name)
|
||||
if not adapter:
|
||||
continue
|
||||
probes.append((name, adapter, DiscoveryContext(workspace, name, mode="revalidate")))
|
||||
for name, adapter, ctx in probes:
|
||||
try:
|
||||
if adapter.verify_artifact(uuid, ctx):
|
||||
return name, adapter, ctx
|
||||
except Exception:
|
||||
pass
|
||||
for name, adapter, ctx in probes:
|
||||
try:
|
||||
path = adapter.artifact_path(uuid, ctx)
|
||||
except Exception:
|
||||
continue
|
||||
if uuid in path and os.path.exists(path):
|
||||
return name, adapter, ctx
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python -m lib_py.agents <facts|resolve> [args...]", file=sys.stderr)
|
||||
print("Usage: python -m lib_py.agents <facts|resolve|purge> [args...]", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
cmd = sys.argv[1]
|
||||
@@ -83,6 +120,31 @@ def main():
|
||||
print('/exit')
|
||||
sys.exit(0)
|
||||
|
||||
elif cmd == 'purge':
|
||||
# purge <agent|auto> <uuid> [workspace] (Issue #13)
|
||||
# Deletes a conversation's on-disk artifacts through its adapter and
|
||||
# prints one JSON object: {"agent", "purged", "residual", "in_use"}.
|
||||
# Exit 0 purged; 4 refused, conversation still in use (nothing
|
||||
# removed); 5 no adapter has an artifact for this uuid.
|
||||
agent_hint = sys.argv[2] if len(sys.argv) > 2 else 'auto'
|
||||
uuid = sys.argv[3] if len(sys.argv) > 3 else ''
|
||||
workspace = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else os.getcwd()
|
||||
if not uuid:
|
||||
print("ERROR: purge requires <agent|auto> <uuid>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
target = _resolve_purge_target(agent_hint, uuid, workspace)
|
||||
if not target:
|
||||
print(json.dumps({"agent": None, "purged": [], "residual": [], "in_use": False}))
|
||||
sys.exit(5)
|
||||
name, adapter, ctx = target
|
||||
if adapter.conversation_in_use(uuid, ctx):
|
||||
print(json.dumps({"agent": name, "purged": [], "residual": [], "in_use": True}))
|
||||
sys.exit(4)
|
||||
purged = adapter.purge_artifacts(uuid, ctx)
|
||||
residual = adapter.residual_artifacts(uuid, ctx)
|
||||
print(json.dumps({"agent": name, "purged": purged, "residual": residual, "in_use": False}))
|
||||
sys.exit(0)
|
||||
|
||||
else:
|
||||
print(f"ERROR: Unknown CLI command {cmd!r}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import os, json, sqlite3, shutil
|
||||
import os, sys, json, sqlite3, shutil
|
||||
from typing import Optional, Any
|
||||
from lib_py.agents.base import BaseAgentAdapter, DiscoveryContext
|
||||
|
||||
@@ -71,18 +71,55 @@ class AgyAgentAdapter(BaseAgentAdapter):
|
||||
return False
|
||||
return True
|
||||
|
||||
def _cli_root(self, ctx: DiscoveryContext) -> str:
|
||||
return f"{ctx.home_dir}/.gemini/antigravity-cli"
|
||||
|
||||
def purge_artifacts(self, uuid: str, ctx: DiscoveryContext) -> list:
|
||||
# Issue #10: a purge must leave nothing resumable behind. Besides the
|
||||
# conversation DB and the brain dir, agy keeps SQLite sidecars (-wal /
|
||||
# -shm / -journal, created whenever the DB is opened - the stop path
|
||||
# itself does so while capturing the id, and an unflushed WAL can still
|
||||
# hold conversation content), the title annotation and a presence lock.
|
||||
purged = []
|
||||
db = self.artifact_path(uuid, ctx)
|
||||
if os.path.exists(db):
|
||||
os.remove(db)
|
||||
purged.append(db)
|
||||
brain = f"{ctx.home_dir}/.gemini/antigravity-cli/brain/{uuid}"
|
||||
for path in [db] + [f"{db}{suffix}" for suffix in ("-wal", "-shm", "-journal")]:
|
||||
if os.path.exists(path):
|
||||
os.remove(path)
|
||||
purged.append(path)
|
||||
root = self._cli_root(ctx)
|
||||
brain = f"{root}/brain/{uuid}"
|
||||
if os.path.isdir(brain):
|
||||
shutil.rmtree(brain, ignore_errors=True)
|
||||
purged.append(brain)
|
||||
annotation = f"{root}/annotations/{uuid}.pbtxt"
|
||||
if os.path.exists(annotation):
|
||||
os.remove(annotation)
|
||||
purged.append(annotation)
|
||||
# The presence lock marks a conversation some process has open; only
|
||||
# remove it once nothing references the conversation any more.
|
||||
lock = f"{root}/presence/{uuid}.lock"
|
||||
if os.path.exists(lock):
|
||||
if self.conversation_in_use(uuid, ctx):
|
||||
print(f"WARN: presence lock kept, conversation {uuid} is still in use: {lock}", file=sys.stderr)
|
||||
else:
|
||||
os.remove(lock)
|
||||
purged.append(lock)
|
||||
return purged
|
||||
|
||||
def residual_artifacts(self, uuid: str, ctx: DiscoveryContext) -> list:
|
||||
"""Anything under the agy CLI tree still carrying this conversation id."""
|
||||
root = self._cli_root(ctx)
|
||||
if not os.path.isdir(root):
|
||||
return []
|
||||
found = []
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
for name in dirnames + filenames:
|
||||
if uuid in name:
|
||||
found.append(os.path.join(dirpath, name))
|
||||
# Do not descend into a matched directory; it is reported as a whole.
|
||||
dirnames[:] = [d for d in dirnames if uuid not in d]
|
||||
return sorted(found)
|
||||
|
||||
def spawn_spec(self, binary: str, session_uuid: str = "", use_wrapper: bool = False) -> str:
|
||||
return f"{binary} --dangerously-skip-permissions"
|
||||
|
||||
|
||||
@@ -1,10 +1,53 @@
|
||||
# base.py — BaseAgentAdapter abstract base class, SpawnSpec, and DiscoveryContext
|
||||
|
||||
import os, json, sqlite3
|
||||
import os, json, sqlite3, shutil, subprocess
|
||||
from typing import Optional, Dict, Any, List
|
||||
from lib_py.paths import resolve_home
|
||||
from lib_py.verify_session import verify_session_uuid, workspace_key
|
||||
|
||||
|
||||
def conversation_in_use(uuid: str, paths=()) -> bool:
|
||||
"""A live process still references the conversation (Issues #10, #13).
|
||||
|
||||
Either its id is on a command line other than this process's own lineage
|
||||
(`claude -r <uuid>`, `agy --conversation <uuid>`, ... - the purge command
|
||||
itself names the uuid, so its ancestors are excluded), or one of `paths`
|
||||
is held open (lsof, where available). A purge must not touch a
|
||||
conversation that is in use.
|
||||
"""
|
||||
try:
|
||||
out = subprocess.run(["ps", "-axo", "pid=,ppid=,command="],
|
||||
capture_output=True, text=True, timeout=10).stdout
|
||||
except Exception:
|
||||
out = ""
|
||||
parents: Dict[int, int] = {}
|
||||
holders: List[int] = []
|
||||
for line in out.splitlines():
|
||||
parts = line.split(None, 2)
|
||||
if len(parts) < 2 or not (parts[0].isdigit() and parts[1].isdigit()):
|
||||
continue
|
||||
pid, ppid = int(parts[0]), int(parts[1])
|
||||
parents[pid] = ppid
|
||||
if len(parts) > 2 and uuid in parts[2]:
|
||||
holders.append(pid)
|
||||
if holders:
|
||||
lineage = set()
|
||||
p = os.getpid()
|
||||
while p and p not in lineage:
|
||||
lineage.add(p)
|
||||
p = parents.get(p, 0)
|
||||
if any(h not in lineage for h in holders):
|
||||
return True
|
||||
existing = [p for p in paths if p and os.path.exists(p)]
|
||||
if existing and shutil.which("lsof"):
|
||||
try:
|
||||
r = subprocess.run(["lsof", "-t"] + existing, capture_output=True, text=True, timeout=10)
|
||||
if r.stdout.strip():
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
class SpawnSpec:
|
||||
def __init__(self, agent_name: str, session_name: str, command: str, env: Optional[Dict[str, str]] = None):
|
||||
self.agent_name = agent_name
|
||||
@@ -107,6 +150,15 @@ class BaseAgentAdapter:
|
||||
def purge_artifacts(self, uuid: str, ctx: DiscoveryContext) -> list:
|
||||
raise NotImplementedError
|
||||
|
||||
def residual_artifacts(self, uuid: str, ctx: DiscoveryContext) -> list:
|
||||
"""Paths still carrying `uuid` after a purge. Adapters that know their
|
||||
on-disk tree override this; the default has nothing to scan."""
|
||||
return []
|
||||
|
||||
def conversation_in_use(self, uuid: str, ctx: DiscoveryContext) -> bool:
|
||||
"""True while a live process still references the conversation."""
|
||||
return conversation_in_use(uuid, [self.artifact_path(uuid, ctx)])
|
||||
|
||||
def spawn_spec(self, binary: str, session_uuid: str = "", use_wrapper: bool = False) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -137,6 +137,31 @@ DEFAULT_REGISTRY_DIR = ".mam/jobs"
|
||||
DEFAULT_TOPIC_ROOT = "python/mqtt/jobs"
|
||||
LOCK_FILENAME = ".lock"
|
||||
|
||||
|
||||
def _workspace_mam_dir(leaf: str) -> Optional[str]:
|
||||
"""`$WORKSPACE_ROOT/.mam/<leaf>` when lib.sh has told us which workspace we
|
||||
serve; None otherwise."""
|
||||
root = os.environ.get("WORKSPACE_ROOT")
|
||||
if root and root.strip() and os.path.isdir(root):
|
||||
return os.path.join(root, ".mam", leaf)
|
||||
return None
|
||||
|
||||
|
||||
def default_registry_dir() -> str:
|
||||
"""Registry root, resolved at call time (Issue #15).
|
||||
|
||||
``MAM_JOBS_DIR`` wins; otherwise the workspace lib.sh exported as
|
||||
``WORKSPACE_ROOT``; otherwise the historical cwd-relative ``.mam/jobs``.
|
||||
Anchoring on the workspace is what keeps a test sandbox (or any caller
|
||||
whose cwd is not the workspace) from registering jobs into whichever
|
||||
``.mam/jobs`` happens to sit under the current directory - the pytest
|
||||
suite used to leave hundreds of onboarding records in the real registry.
|
||||
"""
|
||||
env = os.environ.get("MAM_JOBS_DIR")
|
||||
if env and env.strip():
|
||||
return env
|
||||
return _workspace_mam_dir("jobs") or DEFAULT_REGISTRY_DIR
|
||||
|
||||
# Persistent audit-log layout: .mam/delegate_job_logs/<job_id>/{meta,events,status}.
|
||||
# This is a *separate* artifact from the registry: the registry is the live job
|
||||
# record (mutated in place), the audit log is an append-only history that
|
||||
@@ -150,14 +175,16 @@ def get_logs_dir() -> str:
|
||||
"""Audit-log root, resolved at call time (B-9).
|
||||
|
||||
Overridable with ``DELEGATE_JOB_LOGS_DIR``; otherwise
|
||||
``<cwd>/.mam/delegate_job_logs``. Resolved per call rather than at import
|
||||
so a chdir after import cannot strand the audit trail in the old tree —
|
||||
the same reason ``DEFAULT_REGISTRY_DIR`` stays a relative string.
|
||||
``$WORKSPACE_ROOT/.mam/delegate_job_logs`` (Issue #15, see
|
||||
``default_registry_dir``); otherwise ``<cwd>/.mam/delegate_job_logs``.
|
||||
Resolved per call rather than at import so a chdir after import cannot
|
||||
strand the audit trail in the old tree — the same reason
|
||||
``DEFAULT_REGISTRY_DIR`` stays a relative string.
|
||||
"""
|
||||
env = os.environ.get("DELEGATE_JOB_LOGS_DIR")
|
||||
if env and env.strip():
|
||||
return env
|
||||
return os.path.join(os.getcwd(), ".mam", "delegate_job_logs")
|
||||
return _workspace_mam_dir("delegate_job_logs") or os.path.join(os.getcwd(), ".mam", "delegate_job_logs")
|
||||
|
||||
|
||||
def __getattr__(name: str): # PEP 562 (3.7+)
|
||||
|
||||
@@ -35,6 +35,7 @@ from lib_py.delegation.mqtt_common import (
|
||||
DEFAULT_REGISTRY_DIR,
|
||||
SCHEMA_VERSION,
|
||||
broker_config_from_job,
|
||||
default_registry_dir,
|
||||
events_topic_for,
|
||||
load_job,
|
||||
next_seq,
|
||||
@@ -242,7 +243,7 @@ def main(argv=None) -> int:
|
||||
parser.add_argument("--data", default=None, help="optional JSON object string")
|
||||
parser.add_argument("--retained", action="store_true",
|
||||
help="force retain=True (auto for completed/error)")
|
||||
parser.add_argument("--registry-dir", default=DEFAULT_REGISTRY_DIR)
|
||||
parser.add_argument("--registry-dir", default=default_registry_dir())
|
||||
parser.add_argument("--attempts", type=int, default=3)
|
||||
parser.add_argument("--finalize-report", default=None, metavar="PATH",
|
||||
help="atomically finalize this report and declare its "
|
||||
|
||||
@@ -28,6 +28,7 @@ from lib_py.delegation.mqtt_common import (
|
||||
_atomic_write_record,
|
||||
_utcnow,
|
||||
broker_config_from_env,
|
||||
default_registry_dir,
|
||||
load_job,
|
||||
registry_lock,
|
||||
topic_prefix_for,
|
||||
@@ -266,7 +267,7 @@ def get_feedback(job_id: str, registry_dir: str = DEFAULT_REGISTRY_DIR) -> str:
|
||||
# --------------------------------------------------------------------------
|
||||
def _build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="multi-agent-mux-delegate-job registry CLI")
|
||||
parser.add_argument("--registry-dir", default=DEFAULT_REGISTRY_DIR)
|
||||
parser.add_argument("--registry-dir", default=default_registry_dir())
|
||||
sub = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
p_reg = sub.add_parser("register", help="create a pending job; prints the job id")
|
||||
|
||||
@@ -18,7 +18,7 @@ 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.registry_dir = registry_dir or mqtt_common.default_registry_dir()
|
||||
self.persistent = persistent
|
||||
self._publisher = None
|
||||
|
||||
|
||||
Reference in new issue
Block a user