| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- import { describe, expect, test } from "bun:test"
- import { ACP } from "../../src/acp/agent"
- import type { AgentSideConnection } from "@agentclientprotocol/sdk"
- import type { Event } from "@opencode-ai/sdk/v2"
- import { Instance } from "../../src/project/instance"
- import { tmpdir } from "../fixture/fixture"
- type SessionUpdateParams = Parameters<AgentSideConnection["sessionUpdate"]>[0]
- type RequestPermissionParams = Parameters<AgentSideConnection["requestPermission"]>[0]
- type RequestPermissionResult = Awaited<ReturnType<AgentSideConnection["requestPermission"]>>
- type GlobalEventEnvelope = {
- directory?: string
- payload?: Event
- }
- type EventController = {
- push: (event: GlobalEventEnvelope) => void
- close: () => void
- }
- function createEventStream() {
- const queue: GlobalEventEnvelope[] = []
- const waiters: Array<(value: GlobalEventEnvelope | undefined) => void> = []
- const state = { closed: false }
- const push = (event: GlobalEventEnvelope) => {
- const waiter = waiters.shift()
- if (waiter) {
- waiter(event)
- return
- }
- queue.push(event)
- }
- const close = () => {
- state.closed = true
- for (const waiter of waiters.splice(0)) {
- waiter(undefined)
- }
- }
- const stream = async function* (signal?: AbortSignal) {
- while (true) {
- if (signal?.aborted) return
- const next = queue.shift()
- if (next) {
- yield next
- continue
- }
- if (state.closed) return
- const value = await new Promise<GlobalEventEnvelope | undefined>((resolve) => {
- waiters.push(resolve)
- if (!signal) return
- signal.addEventListener("abort", () => resolve(undefined), { once: true })
- })
- if (!value) return
- yield value
- }
- }
- return { controller: { push, close } satisfies EventController, stream }
- }
- function createFakeAgent() {
- const updates = new Map<string, string[]>()
- const chunks = new Map<string, string>()
- const record = (sessionId: string, type: string) => {
- const list = updates.get(sessionId) ?? []
- list.push(type)
- updates.set(sessionId, list)
- }
- const connection = {
- async sessionUpdate(params: SessionUpdateParams) {
- const update = params.update
- const type = update?.sessionUpdate ?? "unknown"
- record(params.sessionId, type)
- if (update?.sessionUpdate === "agent_message_chunk") {
- const content = update.content
- if (content?.type !== "text") return
- if (typeof content.text !== "string") return
- chunks.set(params.sessionId, (chunks.get(params.sessionId) ?? "") + content.text)
- }
- },
- async requestPermission(_params: RequestPermissionParams): Promise<RequestPermissionResult> {
- return { outcome: { outcome: "selected", optionId: "once" } } as RequestPermissionResult
- },
- } as unknown as AgentSideConnection
- const { controller, stream } = createEventStream()
- const calls = {
- eventSubscribe: 0,
- sessionCreate: 0,
- }
- const sdk = {
- global: {
- event: async (opts?: { signal?: AbortSignal }) => {
- calls.eventSubscribe++
- return { stream: stream(opts?.signal) }
- },
- },
- session: {
- create: async (_params?: any) => {
- calls.sessionCreate++
- return {
- data: {
- id: `ses_${calls.sessionCreate}`,
- time: { created: new Date().toISOString() },
- },
- }
- },
- get: async (_params?: any) => {
- return {
- data: {
- id: "ses_1",
- time: { created: new Date().toISOString() },
- },
- }
- },
- messages: async () => {
- return { data: [] }
- },
- message: async () => {
- return {
- data: {
- info: {
- role: "assistant",
- },
- },
- }
- },
- },
- permission: {
- respond: async () => {
- return { data: true }
- },
- },
- config: {
- providers: async () => {
- return {
- data: {
- providers: [
- {
- id: "opencode",
- name: "opencode",
- models: {
- "big-pickle": { id: "big-pickle", name: "big-pickle" },
- },
- },
- ],
- },
- }
- },
- },
- app: {
- agents: async () => {
- return {
- data: [
- {
- name: "build",
- description: "build",
- mode: "agent",
- },
- ],
- }
- },
- },
- command: {
- list: async () => {
- return { data: [] }
- },
- },
- mcp: {
- add: async () => {
- return { data: true }
- },
- },
- } as any
- const agent = new ACP.Agent(connection, {
- sdk,
- defaultModel: { providerID: "opencode", modelID: "big-pickle" },
- } as any)
- const stop = () => {
- controller.close()
- ;(agent as any).eventAbort.abort()
- }
- return { agent, controller, calls, updates, chunks, stop, sdk, connection }
- }
- describe("acp.agent event subscription", () => {
- test("routes message.part.updated by the event sessionID (no cross-session pollution)", async () => {
- await using tmp = await tmpdir()
- await Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const { agent, controller, updates, stop } = createFakeAgent()
- const cwd = "/tmp/opencode-acp-test"
- const sessionA = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- const sessionB = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- controller.push({
- directory: cwd,
- payload: {
- type: "message.part.updated",
- properties: {
- part: {
- sessionID: sessionB,
- messageID: "msg_1",
- type: "text",
- synthetic: false,
- },
- delta: "hello",
- },
- },
- } as any)
- await new Promise((r) => setTimeout(r, 10))
- expect((updates.get(sessionA) ?? []).includes("agent_message_chunk")).toBe(false)
- expect((updates.get(sessionB) ?? []).includes("agent_message_chunk")).toBe(true)
- stop()
- },
- })
- })
- test("keeps concurrent sessions isolated when message.part.updated events are interleaved", async () => {
- await using tmp = await tmpdir()
- await Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const { agent, controller, chunks, stop } = createFakeAgent()
- const cwd = "/tmp/opencode-acp-test"
- const sessionA = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- const sessionB = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- const tokenA = ["ALPHA_", "111", "_X"]
- const tokenB = ["BETA_", "222", "_Y"]
- const push = (sessionId: string, messageID: string, delta: string) => {
- controller.push({
- directory: cwd,
- payload: {
- type: "message.part.updated",
- properties: {
- part: {
- sessionID: sessionId,
- messageID,
- type: "text",
- synthetic: false,
- },
- delta,
- },
- },
- } as any)
- }
- push(sessionA, "msg_a", tokenA[0])
- push(sessionB, "msg_b", tokenB[0])
- push(sessionA, "msg_a", tokenA[1])
- push(sessionB, "msg_b", tokenB[1])
- push(sessionA, "msg_a", tokenA[2])
- push(sessionB, "msg_b", tokenB[2])
- await new Promise((r) => setTimeout(r, 20))
- const a = chunks.get(sessionA) ?? ""
- const b = chunks.get(sessionB) ?? ""
- expect(a).toContain(tokenA.join(""))
- expect(b).toContain(tokenB.join(""))
- for (const part of tokenB) expect(a).not.toContain(part)
- for (const part of tokenA) expect(b).not.toContain(part)
- stop()
- },
- })
- })
- test("does not create additional event subscriptions on repeated loadSession()", async () => {
- await using tmp = await tmpdir()
- await Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const { agent, calls, stop } = createFakeAgent()
- const cwd = "/tmp/opencode-acp-test"
- const sessionId = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- await agent.loadSession({ sessionId, cwd, mcpServers: [] } as any)
- await agent.loadSession({ sessionId, cwd, mcpServers: [] } as any)
- await agent.loadSession({ sessionId, cwd, mcpServers: [] } as any)
- await agent.loadSession({ sessionId, cwd, mcpServers: [] } as any)
- expect(calls.eventSubscribe).toBe(1)
- stop()
- },
- })
- })
- test("permission.asked events are handled and replied", async () => {
- await using tmp = await tmpdir()
- await Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const permissionReplies: string[] = []
- const { agent, controller, stop, sdk } = createFakeAgent()
- sdk.permission.reply = async (params: any) => {
- permissionReplies.push(params.requestID)
- return { data: true }
- }
- const cwd = "/tmp/opencode-acp-test"
- const sessionA = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- controller.push({
- directory: cwd,
- payload: {
- type: "permission.asked",
- properties: {
- id: "perm_1",
- sessionID: sessionA,
- permission: "bash",
- patterns: ["*"],
- metadata: {},
- always: [],
- },
- },
- } as any)
- await new Promise((r) => setTimeout(r, 20))
- expect(permissionReplies).toContain("perm_1")
- stop()
- },
- })
- })
- test("permission prompt on session A does not block message updates for session B", async () => {
- await using tmp = await tmpdir()
- await Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const permissionReplies: string[] = []
- let resolvePermissionA: (() => void) | undefined
- const permissionABlocking = new Promise<void>((r) => {
- resolvePermissionA = r
- })
- const { agent, controller, chunks, stop, sdk, connection } = createFakeAgent()
- // Make permission request for session A block until we release it
- const originalRequestPermission = connection.requestPermission.bind(connection)
- let permissionCalls = 0
- connection.requestPermission = async (params: RequestPermissionParams) => {
- permissionCalls++
- if (params.sessionId.endsWith("1")) {
- await permissionABlocking
- }
- return originalRequestPermission(params)
- }
- sdk.permission.reply = async (params: any) => {
- permissionReplies.push(params.requestID)
- return { data: true }
- }
- const cwd = "/tmp/opencode-acp-test"
- const sessionA = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- const sessionB = await agent.newSession({ cwd, mcpServers: [] } as any).then((x) => x.sessionId)
- // Push permission.asked for session A (will block)
- controller.push({
- directory: cwd,
- payload: {
- type: "permission.asked",
- properties: {
- id: "perm_a",
- sessionID: sessionA,
- permission: "bash",
- patterns: ["*"],
- metadata: {},
- always: [],
- },
- },
- } as any)
- // Give time for permission handling to start
- await new Promise((r) => setTimeout(r, 10))
- // Push message for session B while A's permission is pending
- controller.push({
- directory: cwd,
- payload: {
- type: "message.part.updated",
- properties: {
- part: {
- sessionID: sessionB,
- messageID: "msg_b",
- type: "text",
- synthetic: false,
- },
- delta: "session_b_message",
- },
- },
- } as any)
- // Wait for session B's message to be processed
- await new Promise((r) => setTimeout(r, 20))
- // Session B should have received message even though A's permission is still pending
- expect(chunks.get(sessionB) ?? "").toContain("session_b_message")
- expect(permissionReplies).not.toContain("perm_a")
- // Release session A's permission
- resolvePermissionA!()
- await new Promise((r) => setTimeout(r, 20))
- // Now session A's permission should be replied
- expect(permissionReplies).toContain("perm_a")
- stop()
- },
- })
- })
- })
|