fix(stop): a purge waits for the agent process, not only the herdr session (Issue #31)

A closed herdr session is not an exited agent, and the `herdr already dead`
path ran no check at all, so stop could remove the files of a live claude
and then its row. New `python -m lib_py.agents in-use <agent> <uuid> [ws]
[--wait s]` gives the purge CLI's own verdict; stop asks it outside the
registry lock and, unless it answers free, exits 4 with nothing changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GodopuandClaude Opus 5.5 committed 2026-10-15 15:16:06 +00:00
1 parent 48cda51d85
commit ae98cf18c4
2 files changed
+50 -1

No files matched your search

+33 -1
View File
@@ -1,6 +1,6 @@
# __main__.py — CLI bridge for lib_py.agents facts and resolution (N4)
import os, sys, json, shlex
import os, sys, json, shlex, time
from lib_py.agents.registry import get_adapter, own_key, agent_of_row
# `purge auto` probes adapters in this order; opencode last because its
@@ -151,6 +151,38 @@ def main():
print(json.dumps({"agent": name, "purged": purged, "residual": residual, "in_use": False}))
sys.exit(0)
elif cmd == 'in-use':
# in-use <agent> <uuid> [workspace] [--wait <sec>] (Issue #31)
# The purge CLI's own in-use verdict, for stop_session.sh to ask
# before it purges. Re-checks every 0.25 s for up to --wait seconds.
# Prints {"agent", "in_use"}. Exit 0 free; 4 still in use; 1 bad args.
args = sys.argv[2:]
wait = 0.0
if '--wait' in args:
i = args.index('--wait')
try:
wait = float(args[i + 1])
except (IndexError, ValueError):
print("ERROR: --wait requires seconds", file=sys.stderr)
sys.exit(1)
del args[i:i + 2]
agent_name = args[0] if args else ''
uuid = args[1] if len(args) > 1 else ''
workspace = args[2] if len(args) > 2 and args[2] else os.getcwd()
adapter = get_adapter(agent_name)
if not adapter or not uuid:
print("ERROR: in-use requires <agent> <uuid>", file=sys.stderr)
sys.exit(1)
from lib_py.agents.base import DiscoveryContext
ctx = DiscoveryContext(workspace, agent_name)
deadline = time.monotonic() + wait
in_use = adapter.conversation_in_use(uuid, ctx)
while in_use and time.monotonic() < deadline:
time.sleep(0.25)
in_use = adapter.conversation_in_use(uuid, ctx)
print(json.dumps({"agent": agent_name, "in_use": in_use}))
sys.exit(4 if in_use else 0)
else:
print(f"ERROR: Unknown CLI command {cmd!r}", file=sys.stderr)
sys.exit(1)