Repository navigation
acp: run PasClaw as an Agent Client Protocol agent (pasclaw acp) - #630
Conversation
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
| 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; |
There was a problem hiding this comment.
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 👍 / 👎.
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
…apzcp acp: fix the PR #630 review findings
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 acpuses the same loop, tools,permissionsrules, sessions, modes and providers aspasclaw agent. Only the driver changes.Use it
Zed
settings.json:JetBrains IDEs use the same shape in
~/.jetbrains/acp.json. Flags areagent's (--model,--provider,--mode,--system,--no-mcp,--profile,--backend).--max-iterationsdefaults to 50 here, since an editor turn is a working session. Seedocs/acp.md.What the editor gets
agent_message_chunktool_call: kind (read, edit, execute, search, fetch, ...), a title such asRead src/a.pasor the command itself, the file as a location, and the raw argumentstool_call_update, completed or failed. A call a gate refused (permissions, a declined ask, plan or space mode) is failed, live and on replaywrite_file/edit_file/append_filerewrotetodo_write's checklistplanpermissionsrule with"effect":"ask"session/request_permissionwith Allow once / Allow for this session / Deny. It is bounded bypermissions_ask_timeout_sec; at the limit it counts as a no and is retracted with$/cancel_requestsession/cancel: the loop stops at its next safe point, and the turn answersstopReason: "cancelled"build,plan,improve,spaceviasession/set_modesession/loadreplays prompts, replies, tool calls and resultscwdis the session's project: tools resolve relative paths there, and the shell runs there.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.Cmd.Agenthosts it (TACPLoopHost).RunSingleTurnbuilds it.cwdas 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.RunInteractivekeeps them.acp, as it is formcp stdio, and logs go to stderr.src/pkg/acpwas added to the.dprojsearch 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:make test-acp-e2e(acp_e2e_test.py) drives the real binary over stdio against a fake OpenAI-compatible model:slugifyplus a test, then run the tests.make testpasses on FPC 3.2.2.Not supported yet
Listed in
docs/acp.md:env(logged as a warning);fs/*,terminal/*), so unsaved buffers are not seen;Generated by Claude Code