Lifts the RFC 010 § Deferred restriction that the server had to launch in the
workspace ("cwd must equal the launch directory"). An editor can now open any
project folder, and N concurrent sessions over one connection can each target a
different directory.
- packages/acp: drop the `cwd === process.cwd()` guard in validateWorkspaceParams
(keep "must be absolute" — the cwd becomes the session header / bash workdir),
and drop the persisted-cwd-vs-launch-dir check in session/load (a resumed
session keeps its original header.cwd, so its bash tools run in its workspace).
- packages/tool-bash: the missing link — default the bash workdir to the calling
agent's session cwd (`exec.agent.session.header.cwd`) via a new resolveWorkdir
helper. An explicit model `workdir` still wins; a relative one resolves against
the session cwd. This is the only correct spot for multi-session: N sessions
share one ctx.bash executor, so the workdir must come per-call from exec.agent,
not executor config. Falls back to the executor default when no session cwd is
available (preserves non-ACP behavior).
- Trust: the cwd originates from the ACP client (the user's editor) at
session/new — same trust level as the old launch dir; no new untrusted-input
path. `additionalDirectories` (scope widening / sandbox) stays rejected.
- Tests: bridge accepts any absolute cwd + records it on the header; session/load
honors the persisted cwd; bash defaults to / resolves relative against the
session cwd; two sessions with different cwds each run bash in their own dir;
non-absolute cwd still rejected. 100% per-file coverage maintained.
- Docs: RFC 010 status + § Deferred cwd bullet marked RESOLVED; acp README adds a
Per-session cwd section; tool-bash + example READMEs and e2e comments updated.
145 lines
6.0 KiB
TypeScript
145 lines
6.0 KiB
TypeScript
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
|
|
import { Readable, Writable } from 'node:stream'
|
|
import { mkdtemp, rm, readFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
import {
|
|
ClientSideConnection,
|
|
ndJsonStream,
|
|
PROTOCOL_VERSION,
|
|
type Agent as AcpAgent,
|
|
type Client,
|
|
type RequestPermissionRequest,
|
|
type RequestPermissionResponse,
|
|
type SessionNotification,
|
|
} from '@agentclientprotocol/sdk'
|
|
|
|
/**
|
|
* End-to-end: boot examples/acp-agent as a real subprocess speaking ACP over
|
|
* its stdio, drive it with a real ClientSideConnection, send a real prompt, and
|
|
* verify the WORLD (a file the agent wrote), not the agent's self-report. Owns
|
|
* and disposes the subprocess in afterEach. Key-gated.
|
|
*
|
|
* Also asserts stdout purity (only framed JSON-RPC on stdout) — that one runs
|
|
* WITHOUT a key, since it only needs the server to boot and answer initialize.
|
|
*/
|
|
|
|
const startScript = fileURLToPath(new URL('../start.ts', import.meta.url))
|
|
// Resolve tsx's loader to an ABSOLUTE path: the subprocess runs with cwd set to
|
|
// a temp workdir (this test launches there and uses it as the session cwd; the
|
|
// bridge no longer requires cwd === the launch dir, but a temp dir keeps the
|
|
// test hermetic), where a bare `--import tsx` would not resolve from
|
|
// node_modules. import.meta.resolve gives the worktree's tsx regardless of cwd.
|
|
const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
|
|
|
|
interface Spawned {
|
|
child: ChildProcessWithoutNullStreams
|
|
client: ClientSideConnection
|
|
updates: SessionNotification['update'][]
|
|
stderr: string[]
|
|
}
|
|
|
|
function spawnAcpAgent(cwd: string): Spawned {
|
|
const child = spawn(
|
|
process.execPath,
|
|
['--import', tsxLoader, startScript],
|
|
{ cwd, env: { ...process.env }, stdio: ['pipe', 'pipe', 'pipe'] },
|
|
)
|
|
const stderr: string[] = []
|
|
child.stderr.setEncoding('utf8')
|
|
child.stderr.on('data', (chunk: string) => stderr.push(chunk))
|
|
|
|
const updates: SessionNotification['update'][] = []
|
|
const stream = ndJsonStream(
|
|
Writable.toWeb(child.stdin) as WritableStream<Uint8Array>,
|
|
Readable.toWeb(child.stdout) as ReadableStream<Uint8Array>,
|
|
)
|
|
const makeClient = (_agent: AcpAgent): Client => ({
|
|
sessionUpdate(params: SessionNotification): Promise<void> {
|
|
updates.push(params.update)
|
|
return Promise.resolve()
|
|
},
|
|
requestPermission(_params: RequestPermissionRequest): Promise<RequestPermissionResponse> {
|
|
// Permission gate is deferred (TODO(rfc010-permission-gate)); the bridge
|
|
// never requests permission yet, so just allow if it ever does.
|
|
return Promise.resolve({ outcome: { outcome: 'cancelled' } })
|
|
},
|
|
})
|
|
const client = new ClientSideConnection(makeClient, stream)
|
|
return { child, client, updates, stderr }
|
|
}
|
|
|
|
let spawned: Spawned | undefined
|
|
let workdir: string | undefined
|
|
|
|
afterEach(async () => {
|
|
if (spawned) {
|
|
spawned.child.kill('SIGKILL')
|
|
spawned = undefined
|
|
}
|
|
if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
|
|
workdir = undefined
|
|
})
|
|
|
|
describe('acp-agent stdout purity (no key required)', () => {
|
|
it('emits only framed JSON-RPC on stdout', async () => {
|
|
workdir = await mkdtemp(join(tmpdir(), 'acp-e2e-'))
|
|
// Collect raw stdout bytes directly (bypass the SDK framing) to inspect.
|
|
// A dummy key lets the deepseek adapter APPLY (it only checks the key is
|
|
// present at boot, not valid — the key is used only on a real model call,
|
|
// which this purity test never triggers). So this runs WITHOUT real creds.
|
|
const child = spawn(process.execPath, ['--import', tsxLoader, startScript], {
|
|
cwd: workdir,
|
|
env: { ...process.env, DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot' },
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
})
|
|
const out: string[] = []
|
|
child.stdout.setEncoding('utf8')
|
|
child.stdout.on('data', (c: string) => out.push(c))
|
|
|
|
// Send a single initialize request as a newline-delimited JSON-RPC frame.
|
|
const req = JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} } })
|
|
child.stdin.write(req + '\n')
|
|
|
|
// Give it a moment to boot + reply, then inspect stdout.
|
|
await new Promise(r => setTimeout(r, 4000))
|
|
child.kill('SIGKILL')
|
|
|
|
const lines = out.join('').split('\n').filter(l => l.trim().length > 0)
|
|
expect(lines.length).toBeGreaterThan(0)
|
|
for (const line of lines) {
|
|
// Every stdout line MUST parse as JSON (a JSON-RPC frame). A non-JSON
|
|
// line means a logger/print leaked onto the protocol channel.
|
|
expect(() => JSON.parse(line) as unknown).not.toThrow()
|
|
}
|
|
}, 30_000)
|
|
})
|
|
|
|
describe.skipIf(!process.env.DEEPSEEK_API_KEY)('acp-agent e2e: real prompt over ACP', () => {
|
|
it('runs a real turn and the agent writes the requested file (verified on disk)', async () => {
|
|
workdir = await mkdtemp(join(tmpdir(), 'acp-e2e-'))
|
|
spawned = spawnAcpAgent(workdir)
|
|
const { client, updates } = spawned
|
|
|
|
await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
// Any absolute cwd is honored now; use the temp `workdir` as this session's
|
|
// workspace (the bash tool will run there) — it need not equal the launch dir.
|
|
const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] })
|
|
|
|
const res = await client.prompt({
|
|
sessionId,
|
|
prompt: [{ type: 'text', text: 'Use the bash tool to write the exact text ACP_OK into a file named proof.txt in the current directory. Then stop.' }],
|
|
})
|
|
expect(['end_turn', 'max_tokens']).toContain(res.stopReason)
|
|
|
|
// Verify the WORLD, not the agent's self-report: read the file from disk.
|
|
const proof = await readFile(join(workdir, 'proof.txt'), 'utf8')
|
|
expect(proof).toContain('ACP_OK')
|
|
|
|
// And the client saw tool-call activity stream through.
|
|
expect(updates.some(u => u.sessionUpdate === 'tool_call')).toBe(true)
|
|
}, 180_000)
|
|
})
|