Skip to content

acp: run PasClaw as an Agent Client Protocol agent (pasclaw acp) - #630

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pasclaw-desktop-plan-uapzcp
Nov 12, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pasclaw-desktop-plan-uapzcp

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Adds pasclaw acp, which runs PasClaw as an Agent Client Protocol agent. ACP is how an editor (Zed, JetBrains IDEs, ACP plugins for Neovim and Emacs) drives a coding agent it did not build: newline-delimited JSON-RPC 2.0 over the agent's stdin and stdout.

pasclaw acp uses the same loop, tools, permissions rules, sessions, modes and providers as pasclaw agent. Only the driver changes.

Use it

Zed settings.json:

"agent_servers": {
  "PasClaw": { "type": "custom", "command": "/path/to/pasclaw", "args": ["acp"], "env": {} }
}

JetBrains IDEs use the same shape in ~/.jetbrains/acp.json. Flags are agent's (--model, --provider, --mode, --system, --no-mcp, --profile, --backend). --max-iterations defaults to 50 here, since an editor turn is a working session. See docs/acp.md.

What the editor gets

PasClaw ACP
the model's text, one message per model round agent_message_chunk
a tool call tool_call: kind (read, edit, execute, search, fetch, ...), a title such as Read src/a.pas or the command itself, the file as a location, and the raw arguments
its result tool_call_update, completed or failed. A call a gate refused (permissions, a declined ask, plan or space mode) is failed, live and on replay
a file write_file / edit_file / append_file rewrote a diff, before and after
todo_write's checklist plan
a permissions rule with "effect":"ask" session/request_permission with Allow once / Allow for this session / Deny. It is bounded by permissions_ask_timeout_sec; at the limit it counts as a no and is retracted with $/cancel_request
the stop button session/cancel: the loop stops at its next safe point, and the turn answers stopReason: "cancelled"
modes build, plan, improve, space via session/set_mode
a saved session session/load replays prompts, replies, tool calls and results
  • Project. The editor's cwd is the session's project: tools resolve relative paths there, and the shell runs there.
  • MCP. MCP servers the editor passes (stdio) are connected once per name.
  • Sessions. Each ACP session is an ordinary PasClaw session, saved after every turn and resumable with pasclaw resume <id>.

How it's built

  • PasClaw.ACP (src/pkg/acp/) is the protocol with no model behind it: framing, request/response bookkeeping both ways, sessions, cancellation, and the translation above.
    • A turn runs on its own thread, so cancel and the editor's answers get through while it works.
    • Output goes through one lock, one message per line.
    • Anything that isn't a selected allow (reject, cancelled, an option never offered, no answer) counts as a no.
  • Cmd.Agent hosts it (TACPLoopHost).
    • One registry is built per process, the way RunSingleTurn builds it.
    • Per turn it sets the editor's cwd as the loop's workspace; the session id as the steering, background and cache key (and where allow for this session is kept); and the mode.
    • History, compaction carry-over and working state are kept the way RunInteractive keeps them.
  • stdout is protocol only. The banner is suppressed for acp, as it is for mcp stdio, and logs go to stderr.
  • Delphi: src/pkg/acp was added to the .dproj search path. Not compiled with Delphi (dcc64 is not available here).

Tests

  • make test-acp (acp_tests.pas) tests the protocol layer with a scripted host:
    • translation of kinds, titles, plans, prompt blocks, paths and modes;
    • ids echoed as received, parse errors, initialize-first, unknown methods and sessions;
    • a turn, with a diff for an edit and none for a read that names a file;
    • an ask round trip, the ask limit, and cancel releasing a pending ask;
    • one turn per session, load replaying in order before the response, and a raising host returning an error.
  • make test-acp-e2e (acp_e2e_test.py) drives the real binary over stdio against a fake OpenAI-compatible model:
    • a plan and a diff, with the file landing in the editor's project rather than PasClaw's workspace;
    • an allowed and a denied ask, plan mode, and cancel;
    • an editor-supplied MCP server;
    • load in a fresh process, with every stdout line valid JSON-RPC.
  • Revert-checked: 20 checks, each undoing one piece, and every one fails a test.
  • Official SDK: the official ACP TypeScript SDK (2.6.0), used as the client, completed full sessions. Every message PasClaw sent validated against the spec's v1 JSON schema, with 0 invalid.
  • Live session, model played by hand. The SDK client acted as the editor; the task was to add slugify plus a test, then run the tests.
    • What happened: a plan, diffs, an allow-listed command that didn't ask, an ask the editor approved, a plan-mode refusal, and the session reloaded in a new process.
    • It found one bug, fixed here: refused calls showed as completed.
  • make test passes on FPC 3.2.2.

Not supported yet

Listed in docs/acp.md:

  • images and audio in prompts;
  • HTTP and SSE MCP servers from the editor, and per-server env (logged as a warning);
  • the editor's file system and terminals (fs/*, terminal/*), so unsaved buffers are not seen;
  • token-by-token streaming;
  • authentication methods.

Generated by Claude Code

ACP is how an editor such as Zed, a JetBrains IDE or an ACP plugin for
Neovim or Emacs drives a coding agent it did not build: newline-delimited
JSON-RPC 2.0 over the agent's stdin and stdout. `pasclaw acp` puts PasClaw
on that wire, using the same loop, tools, permissions rules, sessions,
modes and providers as `pasclaw agent`.

- PasClaw.ACP (src/pkg/acp) is the protocol, with no model behind it.
  It handles initialize, session/new, session/load (replays the
  conversation), session/prompt, session/set_mode and session/cancel. A
  turn runs on its own thread, so cancel and the editor's answers get
  through while it runs. Every message is one line, behind one lock.
- The loop's events map onto ACP updates:
  - text becomes agent_message_chunk;
  - each tool call becomes tool_call with a kind, a title, a location
    and the raw arguments, then tool_call_update with the result;
  - a file an edit tool rewrote gets a diff;
  - todo_write becomes a plan;
  - a permissions "ask" rule becomes session/request_permission (once /
    this session / no). It is bounded by permissions_ask_timeout_sec and
    retracted with $/cancel_request.
  - a call a gate refused is shown as failed, live and on replay.
- Cmd.Agent hosts it: one registry for the process, and per turn the
  editor's cwd as the loop's workspace, the session id as the
  steering, background and cache key, and the mode. MCP servers the
  editor supplies are connected (stdio). Sessions are saved after
  every turn. The banner is suppressed and logs go to stderr.
- Tests:
  - acp_tests: the protocol layer with a scripted host;
  - acp_e2e_test.py: the real binary over stdio against a fake model
    (make test-acp, test-acp-e2e).
  - 19 revert checks each fail a test.
  - Checked by hand with the official TypeScript SDK as the client:
    every message validates against the v1 schema. A live session with
    the model played by hand, reloaded in a new process, found the
    refused-call status bug fixed here.
- Docs: docs/acp.md (Zed and JetBrains setup, the mapping,
  permissions, sessions, limits), commands, README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T20:29:16.913615Z a5ee414 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a5ee4146c1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

try
Handlers.Quiet := True;
Handlers.MetaRef := @St.Session.Meta;
LoopCfg := BuildLoopConfig(FCfg, FProvider, FReg, FModel, A, Handlers, PromptText);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Set the session workspace before building the system prompt

When the editor's cwd differs from the agent's launch/configured workspace, BuildLoopConfig builds project context for the wrong directory. BuildSystemPrompt uses CurrentWorkspace to load AGENTS.md, configured instruction files and the workspace description, but assigning LoopCfg.Workspace only changes tool dispatches later. Consequently, tools edit the editor's project without its project rules being injected, potentially following another project's rules instead. Set and restore the thread-local sandbox workspace around prompt construction as well.

Useful? React with 👍 / 👎.

Comment on lines +2444 to +2448
if not RunToolLoop(LoopCfg, Msgs, Loop) then
begin
if Turn.Cancelled then Exit;
Result.Error := Format('the model call failed (HTTP %d)', [Loop.LastResp.StatusCode]);
Exit;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Surface provider failures instead of returning a successful empty turn

If a configured provider exhausts retries/fallbacks and returns an error status, RunToolLoop deliberately returns True with the failure in Loop.Content and Loop.LastResp (PasClaw.Tools.ToolLoop.pas:2494–2513), before invoking OnText. This check therefore misses ordinary authentication, rate-limit and connection failures: the error is saved as an assistant message but never sent to the editor, which receives only stopReason: "end_turn". Inspect the returned response status and report the failure through Result.Error or an explicit turn update.

Useful? React with 👍 / 👎.

@FMXExpress
FMXExpress merged commit f06628a into main Nov 12, 2026
FMXExpress pushed a commit that referenced this pull request Nov 12, 2026
Fixes the code review of `pasclaw acp` (#630) and two Codex review
comments on it.

The host (PasClaw.Cmd.Agent):
- A provider that cannot answer is now the prompt's JSON-RPC error,
  with the provider's message. The tool loop hands a failure back as
  Content with a non-2xx status; it was sent as end_turn with no text
  and saved as the assistant's reply. ProviderRefused moves from the
  gateway into PasClaw.Tools.ToolLoop so both use the same check.
- session/set_mode reaches the system prompt after the first turn. The
  kept prompt (it can carry a compaction summary) was reused whole, so
  the gate changed and the model was not told. New
  RetargetModeSection (PasClaw.Agent.Prompt) swaps the mode directive;
  the REPL's /mode had the same bug and uses it too.
- The editor's project is the thread's workspace for the whole turn, so
  the system prompt is built for it (its AGENTS.md, instruction files,
  workspace section), not only the tool dispatches.
- The docker shell backend works: each turn sets the backend's session
  id (it refused every command with an empty one), and turns run one at
  a time when the backend keeps per-session state. Containers already
  stop at exit (the backend reaps the ones it started); a reloaded
  session keeps its container.
- Editor-supplied MCP servers connect through the new
  ConnectMCPServerList (PasClaw.MCP.Bridge) instead of swapping the
  shared config's server list while other sessions' turns read it, and
  without re-registering tool_search's disclosure from the editor's
  servers alone.
- The session records the editor's project as its location, so
  `pasclaw resume <id>` works there.
- session/load refuses a session created under another profile unless
  --profile or $PASCLAW_PROFILE chose this one, naming the command that
  opens it, so a `security` session is not run without its sandbox.
- An explicit --max-iterations is kept, 8 included (it became 50).

The protocol layer (PasClaw.ACP):
- Output cut for display, titles and replayed results stay valid UTF-8
  (Utf8SafeTruncate moves to PasClaw.Utils, surrogate-safe on Delphi).
- Calls a turn announced but never finished are shown failed before
  the turn answers, and no longer take a later same-named result.
- Each tool call's arguments are parsed once, not about a dozen times.
- Ids come from the new JsonIdLiteral (PasClaw.JSON), shared with the
  MCP server; a null id is echoed as null. Its Delphi branch tests
  TJSONNumber before TJSONString (the former descends from the latter).

Tests: unit tests for open calls, the UTF-8 cut, null ids and
RetargetModeSection; e2e checks for each host fix (a fake docker CLI
for the backend), an event-driven cancel instead of a 0.5 s sleep, and
the unit test's scratch directory is removed. Each behavior fix was
reverted to confirm a test fails without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
FMXExpress added a commit that referenced this pull request Nov 12, 2026
@FMXExpress FMXExpress mentioned this pull request Nov 13, 2026
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