2026-07-14 14:11:33 +08:00
|
|
|
import { mkdtemp, readFile } from 'node:fs/promises'
|
2026-06-16 11:10:25 +08:00
|
|
|
import { tmpdir } from 'node:os'
|
|
|
|
|
import { join } from 'node:path'
|
|
|
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
2026-07-14 00:18:00 +08:00
|
|
|
import { PROTOCOL_VERSION } from '@agentclientprotocol/sdk'
|
2026-06-16 11:10:25 +08:00
|
|
|
import {
|
2026-07-14 00:18:00 +08:00
|
|
|
launchAcpTestAgent,
|
|
|
|
|
type AgentUnderTest,
|
|
|
|
|
type LaunchedAcpTestAgent,
|
|
|
|
|
} from '@deepseek-ai/dsh-acp-snapshot'
|
2026-07-14 14:11:33 +08:00
|
|
|
import { cleanupAcpExampleTest } from './cleanup.ts'
|
2026-06-16 11:10:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-07-14 00:18:00 +08:00
|
|
|
const AGENT: AgentUnderTest = {
|
2026-07-15 16:57:26 +08:00
|
|
|
binScript: fileURLToPath(new URL('../../../packages/examples/acp-demo/src/bin.ts', import.meta.url)),
|
2026-07-14 00:18:00 +08:00
|
|
|
configPath: fileURLToPath(new URL('../cordis.yml', import.meta.url)),
|
|
|
|
|
tsconfigPath: fileURLToPath(new URL('../../../tsconfig.json', import.meta.url)),
|
2026-06-16 11:10:25 +08:00
|
|
|
}
|
2026-07-14 11:53:45 +08:00
|
|
|
const DANGER_FULL_ACCESS_ENV = { DSH_PERMISSION_MODE: 'danger-full-access' }
|
2026-06-16 11:10:25 +08:00
|
|
|
|
2026-07-14 00:18:00 +08:00
|
|
|
let spawned: LaunchedAcpTestAgent | undefined
|
2026-06-16 11:10:25 +08:00
|
|
|
let workdir: string | undefined
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2026-07-14 14:11:33 +08:00
|
|
|
const ownedSpawned = spawned
|
|
|
|
|
const ownedWorkdir = workdir
|
|
|
|
|
spawned = undefined
|
|
|
|
|
workdir = undefined
|
|
|
|
|
await cleanupAcpExampleTest(ownedSpawned, ownedWorkdir)
|
2026-06-16 11:10:25 +08:00
|
|
|
})
|
|
|
|
|
|
fix(acp): server crashed on connect — drop `export default`, read optional service cwd-independently
Two independent bugs made the ACP server crash the moment an editor (Zed)
connected, despite 178 green unit tests at 100% coverage:
1. `session/new` threw `cannot get property "agents" without inject`. Root
cause: a stray `export default apply` made the cordis Loader's
`unwrapExports` (`exports.default ?? exports`) collapse the module to the
bare `apply` function, discarding the sibling `inject`/`name`/`Config`
named exports. The plugin fiber was built with empty `inject`, so every
`ctx.<service>` read in `apply` threw at load. Fix: remove the default
export so the Loader uses the namespace.
2. `session/load` threw `cannot get property "sessionPersistence" without
inject`. `AgentLoop.resume` read `this.ctx.sessionPersistence` (a service
it deliberately does NOT inject); the property proxy's ancestor-only fiber
walk fails through the bridge's traceable shadow. Fix: read it via
`this.ctx.get('sessionPersistence', false)`, the topology-independent
global-store lookup.
Why the suite missed both: every test mounted the plugin by hand
(`ctx.plugin({name,inject,apply})`), bypassing `unwrapExports` entirely, and
the only test driving these RPCs was key-gated (skipped in CI). Added a no-key
`session/new` e2e that boots the real example through the real Loader — it
fails loudly on bug #1 without an API key. Set `TSX_TSCONFIG_PATH` in the e2e
spawn so the subprocess resolves workspace `paths` from a temp cwd (it was
silently falling back to a stale built `lib/`).
Docs: post-mortem 0001; AGENTS.md "line coverage is not behavior coverage" +
with-key/smoke-test philosophy; packages/AGENTS.md plugin-export-shape and
ctx.get rules; dsh-code-review SKILL checks.
2026-06-18 03:12:37 +08:00
|
|
|
describe('acp-agent over real stdio (no key required)', () => {
|
2026-06-16 11:10:25 +08:00
|
|
|
it('emits only framed JSON-RPC on stdout', async () => {
|
|
|
|
|
workdir = await mkdtemp(join(tmpdir(), 'acp-e2e-'))
|
2026-07-14 00:18:00 +08:00
|
|
|
// Inspect the launcher's raw-byte tee in addition to driving its SDK client.
|
2026-06-16 11:10:25 +08:00
|
|
|
// 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.
|
2026-07-14 00:18:00 +08:00
|
|
|
spawned = launchAcpTestAgent({
|
|
|
|
|
agent: AGENT,
|
2026-06-16 11:10:25 +08:00
|
|
|
cwd: workdir,
|
2026-07-05 18:33:27 +08:00
|
|
|
env: {
|
|
|
|
|
DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot',
|
2026-07-14 11:53:45 +08:00
|
|
|
...DANGER_FULL_ACCESS_ENV,
|
2026-07-05 18:33:27 +08:00
|
|
|
},
|
2026-06-16 11:10:25 +08:00
|
|
|
})
|
2026-07-14 00:18:00 +08:00
|
|
|
await spawned.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
2026-06-16 11:10:25 +08:00
|
|
|
|
2026-07-14 00:18:00 +08:00
|
|
|
const lines = spawned.rawStdout().split('\n').filter(line => line.trim().length > 0)
|
2026-06-16 11:10:25 +08:00
|
|
|
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)
|
fix(acp): server crashed on connect — drop `export default`, read optional service cwd-independently
Two independent bugs made the ACP server crash the moment an editor (Zed)
connected, despite 178 green unit tests at 100% coverage:
1. `session/new` threw `cannot get property "agents" without inject`. Root
cause: a stray `export default apply` made the cordis Loader's
`unwrapExports` (`exports.default ?? exports`) collapse the module to the
bare `apply` function, discarding the sibling `inject`/`name`/`Config`
named exports. The plugin fiber was built with empty `inject`, so every
`ctx.<service>` read in `apply` threw at load. Fix: remove the default
export so the Loader uses the namespace.
2. `session/load` threw `cannot get property "sessionPersistence" without
inject`. `AgentLoop.resume` read `this.ctx.sessionPersistence` (a service
it deliberately does NOT inject); the property proxy's ancestor-only fiber
walk fails through the bridge's traceable shadow. Fix: read it via
`this.ctx.get('sessionPersistence', false)`, the topology-independent
global-store lookup.
Why the suite missed both: every test mounted the plugin by hand
(`ctx.plugin({name,inject,apply})`), bypassing `unwrapExports` entirely, and
the only test driving these RPCs was key-gated (skipped in CI). Added a no-key
`session/new` e2e that boots the real example through the real Loader — it
fails loudly on bug #1 without an API key. Set `TSX_TSCONFIG_PATH` in the e2e
spawn so the subprocess resolves workspace `paths` from a temp cwd (it was
silently falling back to a stale built `lib/`).
Docs: post-mortem 0001; AGENTS.md "line coverage is not behavior coverage" +
with-key/smoke-test philosophy; packages/AGENTS.md plugin-export-shape and
ctx.get rules; dsh-code-review SKILL checks.
2026-06-18 03:12:37 +08:00
|
|
|
|
|
|
|
|
it('session/new succeeds over real stdio (no model call)', async () => {
|
2026-07-24 01:40:25 +08:00
|
|
|
// REGRESSION GUARD (this exact RPC exposed the missing-inject Loader bug):
|
|
|
|
|
// `session/new` drives the
|
fix(acp): server crashed on connect — drop `export default`, read optional service cwd-independently
Two independent bugs made the ACP server crash the moment an editor (Zed)
connected, despite 178 green unit tests at 100% coverage:
1. `session/new` threw `cannot get property "agents" without inject`. Root
cause: a stray `export default apply` made the cordis Loader's
`unwrapExports` (`exports.default ?? exports`) collapse the module to the
bare `apply` function, discarding the sibling `inject`/`name`/`Config`
named exports. The plugin fiber was built with empty `inject`, so every
`ctx.<service>` read in `apply` threw at load. Fix: remove the default
export so the Loader uses the namespace.
2. `session/load` threw `cannot get property "sessionPersistence" without
inject`. `AgentLoop.resume` read `this.ctx.sessionPersistence` (a service
it deliberately does NOT inject); the property proxy's ancestor-only fiber
walk fails through the bridge's traceable shadow. Fix: read it via
`this.ctx.get('sessionPersistence', false)`, the topology-independent
global-store lookup.
Why the suite missed both: every test mounted the plugin by hand
(`ctx.plugin({name,inject,apply})`), bypassing `unwrapExports` entirely, and
the only test driving these RPCs was key-gated (skipped in CI). Added a no-key
`session/new` e2e that boots the real example through the real Loader — it
fails loudly on bug #1 without an API key. Set `TSX_TSCONFIG_PATH` in the e2e
spawn so the subprocess resolves workspace `paths` from a temp cwd (it was
silently falling back to a stale built `lib/`).
Docs: post-mortem 0001; AGENTS.md "line coverage is not behavior coverage" +
with-key/smoke-test philosophy; packages/AGENTS.md plugin-export-shape and
ctx.get rules; dsh-code-review SKILL checks.
2026-06-18 03:12:37 +08:00
|
|
|
// full bridge → `ctx.agents.create({sessionId, meta:{cwd}})` → AgentLoop →
|
|
|
|
|
// registry/persistence path, ALL of which run from the JSON-RPC read loop
|
|
|
|
|
// OUTSIDE the bridge plugin's injection scope. A lazy `ctx.<service>` read
|
|
|
|
|
// on that path throws and the RPC fails with an Internal error — yet the
|
|
|
|
|
// call never touches the model, so this reproduces WITHOUT a key. The
|
|
|
|
|
// key-gated prompt test below never caught it (it needs real creds); the
|
|
|
|
|
// initialize-only purity test never caught it (initialize does not reach
|
|
|
|
|
// the factory). This closes that gap: boot the real subprocess and create a
|
|
|
|
|
// session, asserting the RPC RESOLVES (not rejects with an inject error).
|
|
|
|
|
workdir = await mkdtemp(join(tmpdir(), 'acp-e2e-'))
|
|
|
|
|
// A dummy key lets the deepseek adapter boot (it only checks presence, not
|
|
|
|
|
// validity, at apply time); no model call is made, so the key is never used.
|
2026-07-14 00:18:00 +08:00
|
|
|
spawned = launchAcpTestAgent({
|
|
|
|
|
agent: AGENT,
|
|
|
|
|
cwd: workdir,
|
2026-07-14 11:53:45 +08:00
|
|
|
env: {
|
|
|
|
|
DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'sk-dummy-for-boot',
|
|
|
|
|
...DANGER_FULL_ACCESS_ENV,
|
|
|
|
|
},
|
2026-07-14 00:18:00 +08:00
|
|
|
})
|
fix(acp): server crashed on connect — drop `export default`, read optional service cwd-independently
Two independent bugs made the ACP server crash the moment an editor (Zed)
connected, despite 178 green unit tests at 100% coverage:
1. `session/new` threw `cannot get property "agents" without inject`. Root
cause: a stray `export default apply` made the cordis Loader's
`unwrapExports` (`exports.default ?? exports`) collapse the module to the
bare `apply` function, discarding the sibling `inject`/`name`/`Config`
named exports. The plugin fiber was built with empty `inject`, so every
`ctx.<service>` read in `apply` threw at load. Fix: remove the default
export so the Loader uses the namespace.
2. `session/load` threw `cannot get property "sessionPersistence" without
inject`. `AgentLoop.resume` read `this.ctx.sessionPersistence` (a service
it deliberately does NOT inject); the property proxy's ancestor-only fiber
walk fails through the bridge's traceable shadow. Fix: read it via
`this.ctx.get('sessionPersistence', false)`, the topology-independent
global-store lookup.
Why the suite missed both: every test mounted the plugin by hand
(`ctx.plugin({name,inject,apply})`), bypassing `unwrapExports` entirely, and
the only test driving these RPCs was key-gated (skipped in CI). Added a no-key
`session/new` e2e that boots the real example through the real Loader — it
fails loudly on bug #1 without an API key. Set `TSX_TSCONFIG_PATH` in the e2e
spawn so the subprocess resolves workspace `paths` from a temp cwd (it was
silently falling back to a stale built `lib/`).
Docs: post-mortem 0001; AGENTS.md "line coverage is not behavior coverage" +
with-key/smoke-test philosophy; packages/AGENTS.md plugin-export-shape and
ctx.get rules; dsh-code-review SKILL checks.
2026-06-18 03:12:37 +08:00
|
|
|
const { client } = spawned
|
|
|
|
|
|
|
|
|
|
await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
|
|
|
const { sessionId } = await client.newSession({ cwd: workdir, mcpServers: [] })
|
|
|
|
|
expect(typeof sessionId).toBe('string')
|
|
|
|
|
expect(sessionId.length).toBeGreaterThan(0)
|
|
|
|
|
}, 60_000)
|
2026-06-16 11:10:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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-'))
|
2026-07-14 11:53:45 +08:00
|
|
|
spawned = launchAcpTestAgent({ agent: AGENT, cwd: workdir, env: DANGER_FULL_ACCESS_ENV })
|
2026-06-16 11:10:25 +08:00
|
|
|
const { client, updates } = spawned
|
|
|
|
|
|
|
|
|
|
await client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
2026-06-17 10:01:18 +08:00
|
|
|
// 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.
|
2026-06-16 11:10:25 +08:00
|
|
|
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)
|
|
|
|
|
|
2026-07-25 13:02:37 +08:00
|
|
|
// Assert the filesystem effect independently of the model response.
|
2026-06-16 11:10:25 +08:00
|
|
|
const proof = await readFile(join(workdir, 'proof.txt'), 'utf8')
|
|
|
|
|
expect(proof).toContain('ACP_OK')
|
|
|
|
|
|
2026-07-24 01:40:25 +08:00
|
|
|
// The transport exposes only committed assistant text; tool execution is
|
|
|
|
|
// proved by the world effect above and remains session-log data.
|
|
|
|
|
expect(updates.length).toBeGreaterThan(0)
|
|
|
|
|
expect(updates.every(update => update.sessionUpdate === 'agent_message_chunk')).toBe(true)
|
2026-06-16 11:10:25 +08:00
|
|
|
}, 180_000)
|
|
|
|
|
})
|