Skip to content

feat(acp): serve ACP over stdio with free-mode admission and host fixes - #1789

Open
GekkeTovie wants to merge 25 commits into
CodebuffAI:mainfrom
GekkeTovie:feature/acp-t3-fixes-r2
Open

GekkeTovie wants to merge 25 commits into
CodebuffAI:mainfrom
GekkeTovie:feature/acp-t3-fixes-r2

Conversation

@GekkeTovie

@GekkeTovie GekkeTovie commented Dec 2, 2026 •

Copy link
Copy Markdown

What

This adds a Freebuff --acp provider (ACP v1 over stdio) for local editor hosts such as T3 Code and Zed, and hardens the in-tree ACP branch for real hosts. It carries forward the ACP work from @espetro (#1611) and @rodriveiga01 (feature/acp-agent) with the review fixes below.

  • ACP v1 stdio server: initialize, session/new, session/prompt, session/cancel and session/set_mode; newline-delimited JSON-RPC; stdout is protocol-only.
  • One free-mode admission path: acpFreebuffSession.ensure -> callFreebuffSession, reused per model and released on shutdown, SIGINT, SIGTERM and stdin close.
  • Session modes: Freebuff model picker plus agent modes; session/set_mode emits current_mode_update and tracks the picker id.
  • Tool approval and permissions: a fail-closed toolApproval seam in the SDK; ACP asks before side-effecting and MCP tools, supports allow-always, cancel/deny, and never runs a malformed request.
  • Prompt content blocks: text and resource links are mapped instead of dropped; malformed blocks are rejected.
  • Failure statuses: refusal reasons go out as one agent_message_chunk plus stopReason: refusal (deduped, never also thrown); transient admission failures use a bounded model fallback and poll.
  • Host fixes: lowercase CLI user-agent, --acp implies trustAgents, an early --acp branch before TTY/OSC, startup logs to stderr, and bounded shutdown/child cleanup.
  • Tests: the ACP-specific cli/src/acp files are unchanged from bbcb4030c, where the ACP suite (60 tests) and SDK approval/web-fetch tests (12) passed. The merge brings upstream changes to shared sdk/src/run.ts, cli/src/cli-args.ts and cli/src/index.tsx; those 60/12 results are pre-merge evidence, not a post-merge test, and I did not re-run ACP locally after the merge because of the no-ACP constraint. The CI check on the merge head is green. A compiled Windows build exposes --acp; --version, --smoke-tree-sitter, ACP initialize and session/new exit 0, with protocol 1 and only JSON on stdout.

Why

T3 Code/Zed Local ACP hosts need a headless agent that uses the same free-mode admission, instance metadata and device signing as the TUI. External wrappers are refused by the server's client allowlist, so this surface has to live in the official CLI. Review of the first cut found real host bugs (constructor shape, hardcoded agent/cost mode, refusal duplication, missing prompt blocks/permissions, stdout pollution); this branch fixes them.

Known limitation: live free-mode turn

A self-built or dev client is still refused by the free-mode allowlist (third_party_client_warning); the official freebuff@0.2.36 has no --acp yet. No live free-mode completion is claimed and none was re-run for this PR. The handshake, session creation, modes and compiled-binary ACP transport are verified; a live model turn and T3/Zed reply E2E need an official allowlisted build with --acp. This branch adds no workaround for the allowlist.

Scope and commit notes

  • 25 commits total: the ACP feature and host fixes, a docs commit that corrects NOTES.md and adds docs/acp-free-mode-ads.md (an opt-in _meta.freebuff.ad proposal for Feature request: headless/print mode + SDK support for freebuff authToken (for third-party orchestrators) #947), a final docs-only cleanup that removes the internal PR draft and raw live-evidence file and neutralizes the account-specific strike wording, a small .gitignore cleanup that drops the stale exception for that removed evidence file, and a merge of current main that resolves the bun.lock conflict. The ad proposal is documentation only, nothing is implemented, and it can be dropped if out of scope.
  • The branch started at a70c9a865; it is now merged with current main. The only conflict was bun.lock, resolved by keeping current main's lockfile and adding this branch's @agentclientprotocol/sdk dependency (2.7.0). No rebase or force-push; the ACP-specific cli/src/acp files are unchanged, while the merge brings upstream changes to shared sdk/src/run.ts, cli/src/cli-args.ts and cli/src/index.tsx. The 60/12 results are pre-merge evidence on bbcb4030c and were not re-run locally.
  • scripts/acp-*.ts are local harness/readiness tools from the investigation, not production code.

Credits

espetro and others added 25 commits November 16, 2026 13:11
Fixes the ACP half of CodebuffAI#319. `freebuff --acp` serves ACP v1 (stdio
JSON-RPC, newline-delimited) using the official
\@agentclientprotocol/sdk, so editors such as Zed can drive the CLI as
an agent subprocess.

The ACP surface is a second front end over the same conversation
machinery the TUI uses: `getCodebuffClient()` + the SDK's `run()` with
PrintModeEvent handlers. Free mode's session admission, instance
metadata and device signing are unchanged, so the server's client
allowlist still sees an official Freebuff CLI - the third-party-refusal
gate that keeps an external SDK wrapper from driving free mode (seen
while prototyping) never trips.

Mapping: text -> agent_message_chunk, reasoning_delta ->
agent_thought_chunk, tool_call -> tool_call (pending), tool_result ->
tool_call_update (completed), error -> notice, finish -> the
session/prompt response (end_turn). session/cancel aborts the in-flight
SDK run; tool calls left open by a turn end failed/completed so clients
never spin forever.

Tests cover the event mapping, the protocol lifecycle in-process, and
the real stdio transport (spawn the CLI, initialize, session/new).
The ACP entry point had no way to claim a Freebuff session: admission lives in
the React store, so a headless process sent every turn without an instance id
and had it refused before it ran. Claim the hour through the same
callFreebuffSession the TUI uses, reuse it per model, and release it at
shutdown, with serveAcp as the single owner of process lifetime.

A refused admission is reported as an ACP notice plus a refusal stop reason so
the editor keeps the session, and the hardcoded base2-free/LITE pair is replaced
by the mode mapping that is correct in both Freebuff and Codebuff builds.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Restore test/setup-scm-loader.ts (referenced by cli/bunfig but missing from
this public snapshot) so bun test --cwd cli src/acp is self-contained, and
commit the redacted live harness transcript plus blocker notes showing the
third_party_client_warning gate.

Co-authored-by: Cursor <cursoragent@cursor.com>
Capture keep/port/drop, conflict resolution, test evidence, and the live
allowlist blocker so the draft PR can be opened once workflow-scoped push works.

Co-authored-by: Cursor <cursoragent@cursor.com>
After rebasing onto origin/main, detectTerminalTheme runs via a background promise rather than await; update the host-fix contract and refresh T3 evidence notes in the draft PR body.

Co-authored-by: Cursor <cursoragent@cursor.com>
T3/Zed need session/update current_mode_update after set_mode, and
currentModeId must reflect the last selected model or agent mode.

Co-authored-by: Cursor <cursoragent@cursor.com>
Record that set_mode emits current_mode_update and that the compiled
binary cancel harness passed, while npm 0.2.36 still lacks --acp.

Co-authored-by: Cursor <cursoragent@cursor.com>
Keep the PR body aligned with current T3/handshake checks and the
still-blocked official --acp allowlist gate.

Co-authored-by: Cursor <cursoragent@cursor.com>
Poll npm/binaries for --acp without live prompts, and support
--handshake-only/--set-mode so T3 E2E can start the moment allowlist ships.

Co-authored-by: Cursor <cursoragent@cursor.com>
Watch/poll npm or a binary path, then run handshake + two live turns
only after acp-official-ready passes (strike-safe until then).

Co-authored-by: Cursor <cursoragent@cursor.com>
Note that Freebuff Desktop has no --acp CLI and point reviewers at the
npm poller that runs live E2E the moment official --acp ships.

Co-authored-by: Cursor <cursoragent@cursor.com>
…lowlist

Cite espetro on CodebuffAI#319: in-tree flag in an official binary is the free-mode
path; self-built binaries stay third_party.

Co-authored-by: Cursor <cursoragent@cursor.com>
Hosts drop the non-standard { sessionUpdate: 'notice', notice } update: T3 Code shows 'Unsupported ACP update: notice' and discards the text, so a refused turn ended with no visible reason. Emit the reason as a standard agent_message_chunk instead, once per turn, on a new paragraph when text was already streamed. stopReason stays 'refusal'.

fallbackModelFor used DEFAULT_FREEBUFF_MODEL_ID, which upstream moved to DeepSeek V4.1 Flash, so DeepSeek fell back to itself. Use named ids.
The console guard was installed inside serveAcp, after the early branch had
already run auth, analytics and direnv setup, so a log from any of those
reached stdout and corrupted the JSON-RPC stream. Install it as the first
statement of the --acp branch and keep the idempotent call for direct
embedders. Note that host mcpServers are not supported yet.

A session DELETE that never settles could also hold the process open after
stdin EOF; the release now runs under a two-second timeout. The stdio E2E
test closes stdin and asserts exit 0.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
The toolApproval option receives the tool name, input, toolCallId and whether
an MCP server will run the tool before it runs, and answers allow or deny. A
deny returns the SDK's normal errorMessage tool result instead of executing.
The seam is fail-closed: a handler that throws, or returns anything but
"allow", leaves the tool unexecuted and reports why, so a host bug can never
run a tool without consent. When unset, every run behaves exactly as before.

The option is declared on the client and on the per-run options; a per-run
handler overrides the client default. Tests cover a denied call never reaching
the network, a throwing or non-answering handler denying the tool, the real
write_file path for client and per-run handlers, the override, and the MCP
flag.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Ask the host before tools that leave the session: run_terminal_command,
write_file, str_replace and apply_patch, plus any MCP tool, send
session/request_permission with allow_once / allow_always / reject_once.
allow_always is cached per session, a cancelled turn denies without asking,
and only an RPC failure falls back to running the tool; a malformed or
unknown outcome denies. A denied tool is reported failed, as are errorMessage
results, shell commands that exit non-zero (except exit 1 with no output,
which is grep's "no match"), and a background process marked error.

Prompt blocks are mapped too: file:// resource_links become platform paths.
Free-mode refusals (third_party_client_warning, prompt_policy, session limits)
end as one readable chunk plus stopReason "refusal" whether admission or the
run reports them, and a failure reported both as an event and as the run
output is sent once. Shutdown now swallows a rejected hour release, the ACP
run and its session updates are typechecked without `as never` casts, and the
resource_link tests construct platform-native file URLs so they pass on
Windows.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
NOTES.md said ACP v1 has no `notice` update. It does, but it is
fire-and-forget and only allowed when the client advertises `session.notices`,
so this agent keeps errors on `agent_message_chunk`. Also record that a
refused admission is one agent message, not a notice.

Add the opt-in `_meta.freebuff.ad` proposal for CodebuffAI#947: return an auction result
on the `session/prompt` response instead of conversation content or a new
`sessionUpdate` kind, and never count an impression the host cannot draw.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
The public upstream PR should not carry a stale internal PR draft or raw
live-refusal evidence with a local path, session id and account strike
count. Drop those files and keep the public refusal code in the blocker
note without the account-specific details. No runtime code changes.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Resolve the bun.lock conflict by keeping current main's lockfile and
adding this branch's @agentclientprotocol/sdk dependency. No ACP runtime
code changed.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
The raw live-evidence transcript is no longer in the tree, so do not
un-ignore its path. Also drop the trailing blank line at EOF. No runtime
or ACP code changes.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>

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.

4 participants