Skip to content

M66 (TASK-123-FREE): Local CLI Discovery (verity discover) & Unified ExecutionBackend + ACP

1. Purpose & Architectural Decisions

  • Problem (P2.1): verity-redteam-core cannot discover or drive locally authenticated coding CLIs (claude, codex, gemini, opencode, aider, cursor-agent, agy) or ACP-capable agents under one contract.
  • Discovery Substrate: verity discover [--format text|json] probes PATH plus fallback bins (~/.local/bin, ~/.npm-global/bin, ~/.cargo/bin, ~/go/bin) concurrently with --version (1,500 ms timeout), Ollama GET /api/tags (250 ms timeout), and non-secret env/auth-file checks.
  • State Machine & Cache: TargetStatus (NOT_INSTALLED, BROKEN_SHIM, NEEDS_AUTH, READY_UNVERIFIED, VERIFIED, AUTH_FAILED, HANG_PRONE) with a TargetProbe dataclass cached in .verity/probe_cache.json.
  • Unified Backend Contract: TurnRecord, SessionBudget, UnifiedRequest (target e.g. agent:claude, model:ollama/..., sandbox="read-only|workspace-write|full"), StreamEvent, and the ExecutionBackend protocol (can_handle, run).
  • ACP Backend: AcpExecutionBackend drives agent-client-protocol (Apache-2.0) stdio JSON-RPC (initialize, session/new, session/prompt, session/cancel on max_internal_steps).
  • CLI Backend Hardening: HeadlessCliBackend adapters use stdin=DEVNULL, start_new_session=True, a pty.openpty() fallback (TERM=dumb, NO_COLOR=1, CI=1) for agy/hang-prone Ink CLIs, a two-tier watchdog (immediate auth/interactive-prompt fail-fast plus first-byte 25s / idle 60s / turn 120s timers), and guaranteed os.killpg teardown in finally:.
  • Reusable Standalone Python Component: Package discovery + ExecutionBackend (AcpExecutionBackend + HeadlessCliBackend) as a single unified, reusable Python library/component (verity_agent_bridge / decoupled verity.backends with zero imports from verity.cli, verity.scanner, or verity.mcp_server) so it can be reused independently across verity-redteam-core, verity-lite, and external Python tools.
  • Multi-OS Support (macOS, Linux, Windows):
  • Cross-platform discovery: shutil.which + OS-aware fallback bins (~/.local/bin, ~/.npm-global/bin, ~/.cargo/bin, ~/go/bin on POSIX; %LOCALAPPDATA%\Programs, %APPDATA%\npm, PATHEXT .exe/.cmd shims on Windows).
  • Cross-platform process isolation & teardown: start_new_session=True + os.killpg on POSIX; subprocess.CREATE_NEW_PROCESS_GROUP + tree kill (taskkill /T /F) on Windows; pty.openpty() on POSIX with pipe-safe fallback on Windows where pty is unavailable.
  • Free/Open-Core Boundary: this Free substrate is consumed by verity-lite (TASK-124-PRO) for closed-loop verity fix --code; zero Pro imports land in this repository.

2. Target Files

File Change
pyproject.toml Add agent-client-protocol (Apache-2.0) dependency
src/verity/backends/__init__.py Export the ExecutionBackend protocol and backend implementations
src/verity/backends/models.py TurnRecord, SessionBudget, UnifiedRequest, StreamEvent, ExecutionBackend
src/verity/backends/acp.py AcpExecutionBackend over agent-client-protocol stdio JSON-RPC lifecycle + session/cancel
src/verity/backends/cli_adapter.py HeadlessCliBackend adapters for claude, codex, gemini, opencode, aider, cursor-agent, agy with pty fallback, two-tier watchdog, os.killpg teardown
src/verity/discovery/cli_agents.py Concurrent CLI/Ollama probing, TargetStatus state machine, TargetProbe, .verity/probe_cache.json
src/verity/cli.py Add verity discover [--format text|json]
tests/unit/test_discover_and_execution_backend.py Unit tests for discovery/caching, ACP cancellation, and CLI backend isolation/watchdogs

3. ATDD Traceability Matrix

AC ID Acceptance Criterion Paired Benign / Negative Control Verification Tier Test Target
AC-66.1 verity discover --format json probes local CLI agents and Ollama concurrently, classifies NOT_INSTALLED / BROKEN_SHIM / NEEDS_AUTH / READY_UNVERIFIED without leaking secrets or invoking Keychain popups, and caches in .verity/probe_cache.json. Negative control: absent binary classifies NOT_INSTALLED; no /usr/bin/security invocation and zero secret values in JSON output. unit tests/unit/test_discover_and_execution_backend.py::test_discover_json_probes_and_caches_targets
AC-66.2 AcpExecutionBackend handles the ACP lifecycle (session/new, session/prompt) and triggers session/cancel when max_internal_steps is reached. Benign control: request within max_internal_steps completes without emitting session/cancel. unit tests/unit/test_discover_and_execution_backend.py::test_acp_backend_cancels_on_max_internal_steps
AC-66.3 HeadlessCliBackend isolates subprocesses (stdin=DEVNULL, start_new_session=True), uses the pty.openpty() fallback when configured, kills process groups immediately on auth/prompt fail-fast patterns (not logged in, login required, press enter) or idle timeout, and leaves zero orphan processes. Benign control: streaming JSON turn completes cleanly as StreamEvent items with zero orphan processes. unit tests/unit/test_discover_and_execution_backend.py::test_headless_cli_backend_isolation_watchdog_and_streaming
AC-66.4 Reusable component has zero imports from verity.cli/verity.scanner/verity.mcp_server, and discovery/process-isolation adapters handle both POSIX (os.killpg, pty.openpty) and Windows (CREATE_NEW_PROCESS_GROUP, PATHEXT, no pty import crash). Negative control: importing verity.backends on Windows (sys.platform == "win32") succeeds without ModuleNotFoundError: No module named 'pty'. unit tests/unit/test_discover_and_execution_backend.py::test_reusable_component_boundary_and_multi_os_support
AC-66.5 Ruff linter and full unit test suite pass cleanly. Clean workspace yields zero lint errors. ruff uv run ruff check src tests && uv run pytest tests/

4. Verification Commands

uv run pytest tests/unit/test_discover_and_execution_backend.py
uv run pytest tests/
uv run ruff check src tests

5. Execution Checklist

  • [ ] AC-66.1 implemented (verity discover concurrent probing, TargetStatus, .verity/probe_cache.json)
  • [ ] AC-66.2 implemented (AcpExecutionBackend lifecycle + session/cancel on max_internal_steps)
  • [ ] AC-66.3 implemented (HeadlessCliBackend isolation, pty fallback, two-tier watchdog, os.killpg teardown)
  • [ ] AC-66.4 implemented (standalone reusable component boundary + POSIX/Windows multi-OS support)
  • [ ] AC-66.5 verified green (ruff + full pytest)
  • [ ] Board #5 item PVTI_lAHOD-xmQM4Br351zhOMELc synced & status updated