Lifts the RFC 010 single-session-per-connection cap: the bridge now runs N concurrent sessions over one connection, each mapped to its own LoopAgent. - packages/acp: live sessions held in a Map<sessionId, SessionRecord> with an agent→sessionId reverse WeakMap so agent/* events (which carry only the Agent) demux in O(1). Every session/event and agent/status is routed strictly to its owning record — concurrent sessions never cross-settle or interleave their session/update notifications. Per-session state: one in-flight prompt each, session/cancel aborts+settles only its own agent/prompt, session/load reserves a per-id load slot (distinct ids load concurrently; re-loading a live id is rejected), and disposal drains every live session in parallel to quiescence. - packages/tool-bash: record each background task's owning agent at spawn and keep it for the executor's lifetime (NOT cleared on completion). bash_output/bash_kill reject a task owned by a different agent (a task with no owner is open; a no-agent caller can't access an owned task). Task ids are global and predictable, so this is the fence that stops one session's agent from reading/killing another session's background task. - Per-session permission ownership and a per-agent disposer seam stay deferred (depend on the deferred permission gate); the reverse map the gate will route through is in place. RFC 011 stays `proposed`. - Tests: two sessions stream concurrently without interleave; cross-session cancel isolation; per-session in-flight enforcement; dispose-all-to-quiescence; bash cross-session read/kill rejected (+ no-agent and unowned-task cases). - Docs: RFC 011 implementation-status note; acp + tool-bash READMEs; example MVP-limitations updated. 100% per-file coverage maintained.
158 lines
7.9 KiB
TypeScript
158 lines
7.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { PROTOCOL_VERSION } from '@agentclientprotocol/sdk'
|
|
import { makeBridgeHarness, textResponse, type BridgeHarness } from './harness.ts'
|
|
|
|
/**
|
|
* End-to-end bridge specs over an in-memory transport: a real
|
|
* ClientSideConnection drives the bridge's AgentSideConnection, so every
|
|
* assertion exercises actual JSON-RPC framing and the harness event taxonomy.
|
|
*/
|
|
describe('acp bridge', () => {
|
|
let storageDir: string
|
|
let harness: BridgeHarness | undefined
|
|
|
|
beforeEach(async () => {
|
|
storageDir = await mkdtemp(join(tmpdir(), 'acp-test-'))
|
|
})
|
|
|
|
afterEach(async () => {
|
|
// e2e/integration tests own their resources (AGENTS.md): dispose even on
|
|
// failure so a flaky run never leaks a context or persistence dir.
|
|
if (harness) await harness.dispose()
|
|
harness = undefined
|
|
await rm(storageDir, { recursive: true, force: true })
|
|
})
|
|
|
|
it('initialize negotiates the protocol version and advertises capabilities', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
const res = await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
expect(res.protocolVersion).toBe(PROTOCOL_VERSION)
|
|
expect(res.agentCapabilities?.loadSession).toBe(true)
|
|
expect(res.agentCapabilities?.promptCapabilities).toMatchObject({ image: false, audio: false })
|
|
expect(res.agentInfo?.name).toBe('deepseek-harness-acp')
|
|
})
|
|
|
|
it('session/new creates a session and a full prompt turn streams text then settles end_turn', async () => {
|
|
harness = await makeBridgeHarness({ storageDir, script: [textResponse('hello there')] })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
expect(sessionId).toBeTruthy()
|
|
|
|
const res = await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'hi' }] })
|
|
expect(res.stopReason).toBe('end_turn')
|
|
|
|
// The streamed text arrived as agent_message_chunk updates.
|
|
const text = harness.updates
|
|
.filter(u => u.sessionUpdate === 'agent_message_chunk')
|
|
.map(u => (u.content.type === 'text' ? u.content.text : ''))
|
|
.join('')
|
|
expect(text).toBe('hello there')
|
|
})
|
|
|
|
it('allows multiple concurrent sessions, each with a distinct id', async () => {
|
|
harness = await makeBridgeHarness({ storageDir, script: [] })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
const a = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
const b = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
expect(a.sessionId).toBeTruthy()
|
|
expect(b.sessionId).toBeTruthy()
|
|
expect(a.sessionId).not.toBe(b.sessionId)
|
|
// Both agents are live and independently registered.
|
|
expect(harness.ctx.agents.get(a.sessionId)).toBeDefined()
|
|
expect(harness.ctx.agents.get(b.sessionId)).toBeDefined()
|
|
})
|
|
|
|
it('rejects a non-absolute cwd and a cwd that differs from the launch dir', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
await expect(harness.client.newSession({ cwd: 'relative/path', mcpServers: [] }))
|
|
.rejects.toThrow(/absolute/)
|
|
await expect(harness.client.newSession({ cwd: '/some/other/dir', mcpServers: [] }))
|
|
.rejects.toThrow(/launch directory/)
|
|
})
|
|
|
|
it('rejects non-empty additionalDirectories', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
await expect(harness.client.newSession({ cwd: process.cwd(), mcpServers: [], additionalDirectories: ['/x'] }))
|
|
.rejects.toThrow(/additionalDirectories/)
|
|
})
|
|
|
|
it('rejects an empty prompt without hanging', async () => {
|
|
harness = await makeBridgeHarness({ storageDir, script: [] })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
await expect(harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: ' ' }] }))
|
|
.rejects.toThrow(/empty prompt/)
|
|
})
|
|
|
|
it('rejects image content in a prompt (text-only capabilities)', async () => {
|
|
harness = await makeBridgeHarness({ storageDir, script: [] })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
await expect(harness.client.prompt({
|
|
sessionId,
|
|
prompt: [{ type: 'image', mimeType: 'image/png', data: 'AA==' }],
|
|
})).rejects.toThrow(/text/)
|
|
})
|
|
|
|
it('rejects a prompt carrying a non-text block alongside text (no silent context loss)', async () => {
|
|
// A text + resource_link prompt must be rejected, not run text-only with the
|
|
// resource silently dropped — that would feed the model an incomplete prompt.
|
|
harness = await makeBridgeHarness({ storageDir, script: [] })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
await expect(harness.client.prompt({
|
|
sessionId,
|
|
prompt: [
|
|
{ type: 'text', text: 'fix the bug in' },
|
|
{ type: 'resource_link', uri: 'file:///x.ts', name: 'x.ts' },
|
|
],
|
|
})).rejects.toThrow(/text/)
|
|
})
|
|
|
|
it('rejects a prompt for an unknown session', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
await expect(harness.client.prompt({ sessionId: 'nope', prompt: [{ type: 'text', text: 'hi' }] }))
|
|
.rejects.toThrow(/unknown session/)
|
|
})
|
|
|
|
it('negotiates an unsupported protocol version down to the supported one', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
const res = await harness.client.initialize({ protocolVersion: 999, clientCapabilities: {} })
|
|
expect(res.protocolVersion).toBe(PROTOCOL_VERSION)
|
|
})
|
|
|
|
it('a cancel for an unknown/absent session is a silent no-op', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
// No session created yet — cancel must not throw.
|
|
await expect(harness.client.cancel({ sessionId: 'nope' })).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('authenticate is a no-op (no auth methods advertised)', async () => {
|
|
harness = await makeBridgeHarness({ storageDir })
|
|
await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
await expect(harness.client.authenticate({ methodId: 'whatever' })).resolves.toBeDefined()
|
|
})
|
|
|
|
it('honors agentName/agentVersion/systemPrompt config', async () => {
|
|
harness = await makeBridgeHarness({
|
|
storageDir,
|
|
script: [textResponse('ok')],
|
|
config: { agentName: 'custom-agent', agentVersion: '9.9.9', systemPrompt: 'be terse' },
|
|
})
|
|
const res = await harness.client.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} })
|
|
expect(res.agentInfo).toMatchObject({ name: 'custom-agent', version: '9.9.9' })
|
|
// Create + prompt so the systemPrompt config flows through agentOptions and
|
|
// reaches the model request.
|
|
const { sessionId } = await harness.client.newSession({ cwd: process.cwd(), mcpServers: [] })
|
|
await harness.client.prompt({ sessionId, prompt: [{ type: 'text', text: 'hi' }] })
|
|
expect(harness.adapter.requests[0]?.system).toContain('be terse')
|
|
})
|
|
})
|