Skip to content

feat(witan): add harness adapters with ACP session updates as the event model - #745

Open
blarghmatey wants to merge 4 commits into
mainfrom
witan-harness-adapters
Open

blarghmatey wants to merge 4 commits into
mainfrom
witan-harness-adapters

Conversation

@blarghmatey

Copy link
Copy Markdown
Member

What are the relevant tickets?

N/A on GitHub. Witan task tk-harness-adapter-interface-with-acp-session-updat-66b325 ("Harness adapter interface with ACP session updates as the event model, plus native Claude Code and Pi adapters").

Description (What does it do?)

Fan-out runners need to start a worker on whichever harness a host has, watch it, learn its session id and resume it later. Today witan run starts an agent in the foreground with subprocess.run and never learns its session id.

This adds witan.harness:

  • A HarnessAdapter/AgentSession interface with declared capabilities (oneshot, resume, events, live input, attach), so a streaming or networked mode can be added later without reshaping it. SessionRef records harness, session id, host and cwd, because both harnesses keep transcripts per host and per working directory, and resuming from another host or cwd raises ResumeUnavailable.
  • ClaudeAdapter (claude -p --output-format stream-json --verbose, --session-id to start, --resume to continue) and PiAdapter (pi -p --mode json, --session-id to start, --session to continue).
  • Every codec emits ACP v1 session updates, and each session appends them to a JSONL event log with witan's fields under _meta, so one reader covers every harness and a future ACP adapter can relay an agent's own updates unchanged.
  • agent_environ moves from cli/run_helpers to harness/transport so both launch paths build the worker environment the same way without the harness importing the CLI.

The SessionRef field names match the harness and harness_session_ref fields the WorkflowSession fan-out change adds, but nothing here imports WorkflowSession or needs that change to land first.

Implementation details

The ACP models are hand-written pydantic (schema at agentclientprotocol/agent-client-protocol 7855bab, protocol version 1) rather than the agent-client-protocol SDK. Its 1.0.0 wheel only needs pydantic, but the native adapters use ten small update types, and the event log is a format witan keeps reading after the SDK moves on. The ACP adapter, which needs the SDK's JSON-RPC client, can decide on it then.

The module lives in witan rather than beside agent-config-kit's per-harness adapters. Those write config files and have no runtime, while this spawns processes and owns a log format, so putting it there would turn a config library into a process supervisor and add a floor bump on every change.

A pump thread writes the log as output arrives, because a worker nobody is reading would otherwise stall on a full stdout pipe and leave its log stale for fan-out status.

Prompts go on stdin for both harnesses. In argv, Pi reads a prompt that starts with @ as a file to attach, and either harness fails to launch with one over the kernel's per-argument limit. Pi resumes with --session, which exits 1 for an unknown id, where --session-id would silently create a new session in the wrong directory.

A harness can exit 0 after a failed model call (Pi does on a provider 429), so Exit.ok also requires no reported error and an end_turn stop reason.

Permissions come from launch flags scoped to the worktree. Claude runs with --permission-mode acceptEdits by default, no --add-dir, and --permission-prompts none so anything that would prompt fails instead of hanging. Its user and project settings still load (witan's context hooks run through them), so a permissions.allow rule covering a directory of worktrees lets one worker edit a sibling's. Pi has no flag to confine its bash tool to the worktree; the worktree is only its working directory. Passing allowed_tools (e.g. read,edit,write) becomes Pi's --tools allowlist for a worker that must not run commands.

How can this be tested?

  • uv run --isolated --package witan-council --group test pytest mcp/servers/witan/tests/test_harness.py: 24 passed, 2 skipped. The codec tests replay scrubbed recordings of a real start and resume of each harness (Claude Code 2.1.332, pi 1.5.2) and a Pi run that hit a provider 429. The session tests drive both adapters against a stand-in executable.
  • The 2 skipped are test_live_claude and test_live_pi, which run the real CLIs when WITAN_HARNESS_LIVE=1 (model overrides via WITAN_HARNESS_LIVE_CLAUDE_MODEL / WITAN_HARNESS_LIVE_PI_MODEL).
  • tests/test_launch_spec.py (34 passed) covers the witan run launch path that now imports agent_environ from the harness.
  • The flags were checked against claude --help and pi --help on those versions, and pi -p --session <unknown id> exits 1 with "No session found".

https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m

Fan-out runners need to start a worker on whichever harness a host has,
watch it, learn its session id and resume it later, and today witan can
only run an agent in the foreground with no session id. This adds
witan.harness: a HarnessAdapter/AgentSession interface with declared
capabilities, a stdio transport, a codec per wire format and the session
lifecycle, plus native adapters for Claude Code (claude -p --output-format
stream-json) and Pi (pi -p --mode json). Flags were checked against claude
2.1.332 and pi 1.5.2.

Every codec emits ACP v1 session updates (schema at agent-client-protocol
7855bab, protocol version 1), and each session appends them to a JSONL
event log with witan's fields under _meta, so one reader covers every
harness and an ACP adapter can relay an agent's own updates as they are.
A pump thread writes the log as output arrives, because a worker nobody is
reading would otherwise stall on a full stdout pipe and leave its log
stale for fanout status.

The models are hand-written pydantic rather than the agent-client-protocol
SDK. Its 1.0.0 wheel only needs pydantic, but the native adapters use ten
small update types, and the log is a format witan keeps reading after the
SDK moves on. The ACP adapter, which needs the SDK's JSON-RPC client, can
decide on it then.

The module lives in witan, not beside agent-config-kit's per-harness
adapters. Those write config files and have no runtime, while this spawns
processes and owns a log format; putting it there would turn a config
library into a process supervisor and add a floor bump on every change.
agent_environ moves here from cli/run_helpers so both launch paths build
the worker environment the same way without the harness importing the CLI.

Prompts go on stdin for both harnesses: in argv, Pi reads a prompt that
starts with @ as a file to attach, and either harness fails on one over
the kernel's per-argument limit. Pi resumes with --session, which exits 1
for an unknown id, where --session-id would silently start a new session.

Tests replay scrubbed recordings of a real start and resume of each
harness (and a Pi run that hit a provider 429, which Pi reports and then
exits 0) through the codecs, and drive both adapters against a stand-in
executable. test_live_claude and test_live_pi run the real CLIs when
WITAN_HARNESS_LIVE=1.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:00

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Adds harness adapters that start, resume, and log Claude Code and Pi sessions.

  • Adds: harness session interfaces with capabilities, session references, lifecycle results, and resume validation.
  • Adds: Claude Code and Pi adapters that start and resume sessions using their native command-line protocols.
  • Adds: ACP v1 session updates and flushed JSONL logs for normalized worker output, metadata, usage, and lifecycle events.

Exported API changed: agent_environ (moved from mcp/servers/witan/witan/cli/run_helpers.py)

Details for reviewers

Risks

  • Claude Code settings permissions can allow a worker to edit sibling worktrees because the adapter loads user and project settings.
  • Malformed Claude tool input can terminate the pump because _tool_call() calls .get() without confirming the input is a mapping.

Core files: mcp/servers/witan/witan/harness/transport.py, mcp/servers/witan/witan/harness/claude.py, mcp/servers/witan/witan/harness/pi.py

More changes

  • Changes: agent_environ to shared harness transport code for consistent worker environments. (transport.py, run_helpers.py)
  • Adds: background stdout and stderr draining so workers continue while callers consume events or wait for completion. (transport.py)
  • Adds: adapter codec tests and harness output fixtures covering starts, resumes, errors, plans, clipping, and usage. (test_harness.py, pi_start.jsonl, claude_start.jsonl and 3 more)
  • Changes: the changelog to document harness adapters and ACP session updates. (20261125_070245_blarghmatey_witan_harness_adapters.md)

Unchanged

  • Both native adapters remain one-shot processes with live input unsupported.
  • Session transcripts remain tied to their host and working directory.

Summary of a6a53a8 · React 👍 or 👎 on my comments to tune what I raise · Commands · Review config

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: Needs Changes 🔧

Blocking Issues:

  • Keep draining stdout after malformed events and clean up on fatal pump failures.
  • Clean up workers when event-log initialization fails.
  • Finalize workers and publish their exit without requiring a caller to invoke wait().
  • Restrict event-log access to its owner.
  • Read JSONL records by physical line rather than Unicode line boundaries.
  • Stream events from the log instead of retaining every update in memory.

📌 Reviewed up to the latest commit · ⏱️ Review completed in 8 minutes

  • 🔄 re-review this PR

A record the codec rejected (e.g. a TodoWrite with a status outside ACP's)
raised out of the pump, whose generator close shut the harness's stdout, so
the worker died of a broken pipe. It is now an error notice and draining
continues; a failure the pump can't continue past (the log can't be written)
kills and reaps the harness and wait() raises it.

The pump now reaps the harness and writes the exited update itself, so a
log reader sees completion without the runner calling wait(). The event log
opens, and rejects meta that sets seq or ts, before the harness launches, so
neither failure leaves a worker running with no handle. read_event_log reads
physical lines, because splitlines() also split records on U+2028 inside
JSON strings.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
The log carries the prompt and clipped tool input and output, and was
created with the umask's mode (0644 under 022), readable by other local
users. It is now created 0600, and appending to an existing log with any
group or other bits raises PermissionError naming the path and mode, before
the harness launches.

Every notification was also kept in an unbounded in-memory queue for
events(), so a run whose runner only calls wait() accumulated its whole
stream. The queue is gone: events() follows the log file from the offset
the run started at, woken by a Condition on each write, and stops where the
run closed the log, so a resume appending to the same file doesn't extend an
earlier run's events.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m
GitHub kept #745's head at 7741c96 after 9a5861f was pushed to the branch
during a GraphQL outage, and no CI ran for it.

Claude-Session: https://claude.ai/code/session_01FGZebERkUorXxXDu66F91m

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants