Files
multi-agent-mux/.agents/skills/lib_py/agents/__main__.py
T
GodopuandClaude Opus 5 5ba04a27ca feat(issue-24): startup dialogs come from the agent adapter and are handled while waiting for the TUI
handle_startup_dialogs was a claude-only string ladder called only when
AGENT=claude, and wait_for_tui_ready does not treat a dialog screen as ready.
So agy, grok and codex sat on their directory-trust prompt for the full
readiness timeout, the session was recorded as tui-ready-timeout, and
onboarding injection was disabled (create_session.sh:313-314) - a human had to
accept each dialog by hand.

The signatures and accept keys move to the adapters as `startup_dialogs`
(pattern, keys, kind), published through the existing `facts` eval contract as
MAM_STARTUP_DIALOGS. claude's ladder moves over verbatim; agy takes Enter, grok
y, codex Enter, and codex's update notice takes 2 (Skip), which blocks a start
just as hard while changing nothing. kind is `trust` or `notice`:
--no-auto-trust leaves trust dialogs to a human and still answers notices,
since trusting a directory makes codex load its project-local config, hooks and
exec policy.

Re-sending is gated by time (MAM_DIALOG_RESEND_S, default 5s), not by frames.
#24's comment reports that `--source visible` keeps showing an accepted dialog
for a while, and _pane_capture hardcodes that source, so "still on screen" is
not evidence the keys were missed; acceptance is judged by the ready tokens
alone.

Two existing tests asserted the old shape and are updated, not weakened:
b19's "Escape branch stays" now looks for it in claude's adapter, and c1b's
"Enter once per iteration" becomes "never twice for one frame", which the
cooldown satisfies strictly more tightly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 04:22:09 +00:00

155 lines
6.4 KiB
Python

# __main__.py — CLI bridge for lib_py.agents facts and resolution (N4)
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 the adapter's own
`has_artifacts` - which, for agy, also accepts orphaned sidecars, the
annotation or the presence lock left behind after the DB is gone.
"""
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:
if adapter.has_artifacts(uuid, ctx):
return name, adapter, ctx
except Exception:
continue
return None
def main():
if len(sys.argv) < 2:
print("Usage: python -m lib_py.agents <facts|resolve|purge> [args...]", file=sys.stderr)
sys.exit(1)
cmd = sys.argv[1]
if cmd == 'facts':
agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
adapter = get_adapter(agent_name)
if not adapter:
print(f"ERROR: Unknown agent {agent_name!r}", file=sys.stderr)
sys.exit(1)
# Emit shell-eval friendly facts
q = shlex.quote
print(f"MAM_AGENT_NAME={q(adapter.name)}")
print(f"MAM_OWN_KEY={q(adapter.own_key)}")
print(f"MAM_INPUT_PROMPT={q(adapter.input_prompt or '')}")
print(f"MAM_INPUT_PLACEHOLDER={q(adapter.input_placeholder or '')}")
print(f"MAM_INPUT_RULE_PATTERN={q(adapter.input_rule_pattern or '')}")
print(f"MAM_READY_TOKENS={q(adapter.ready_tokens)}")
print(f"MAM_STRONG_READY_TOKENS={q(adapter.strong_ready_tokens)}")
print(f"MAM_WEAK_READY_TOKENS={q(adapter.weak_ready_tokens)}")
print(f"MAM_MODAL_TOKENS={q(adapter.modal_tokens or '')}")
print(f"MAM_STARTUP_DIALOGS={q(json.dumps(adapter.startup_dialogs))}")
print(f"MAM_EXIT_KEY={q(adapter.exit_key)}")
print(f"MAM_DELEGATE_AGENT_KEY={q(adapter.delegate_agent_key)}")
elif cmd == 'resolve':
name = sys.argv[2] if len(sys.argv) > 2 else ''
resolved = agent_of_row({}, session_name=name)
if resolved:
print(resolved)
sys.exit(0)
sys.exit(1)
elif cmd == 'input-region':
agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
pane_text = sys.stdin.read()
from lib_py.agents.input_region import extract_input_region
try:
region = extract_input_region(pane_text, agent_name)
print(region)
sys.exit(0)
except Exception as ex:
print(f"ERROR: {ex}", file=sys.stderr)
sys.exit(5)
elif cmd == 'spawn-spec':
agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
binary = sys.argv[3] if len(sys.argv) > 3 else agent_name
uuid = sys.argv[4] if len(sys.argv) > 4 else ''
use_wrapper = (sys.argv[5].lower() in ('1', 'true', 'yes')) if len(sys.argv) > 5 else False
adapter = get_adapter(agent_name)
if not adapter:
sys.exit(1)
print(adapter.spawn_spec(binary, uuid, use_wrapper=use_wrapper))
sys.exit(0)
elif cmd == 'resume-spec':
agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
binary = sys.argv[3] if len(sys.argv) > 3 else agent_name
uuid = sys.argv[4] if len(sys.argv) > 4 else ''
workspace = sys.argv[5] if len(sys.argv) > 5 else ''
adapter = get_adapter(agent_name)
if not adapter:
sys.exit(1)
from lib_py.agents.base import DiscoveryContext
ctx = DiscoveryContext(workspace, agent_name) if workspace else None
mat = adapter.verify_artifact(uuid, ctx) if (ctx and uuid) else False
print(adapter.resume_spec(binary, uuid, materialized=mat))
sys.exit(0)
elif cmd == 'exit-key':
agent_name = sys.argv[2] if len(sys.argv) > 2 else ''
adapter = get_adapter(agent_name)
if adapter:
print(adapter.exit_key)
sys.exit(0)
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)
if __name__ == '__main__':
main()