import type { AcpVirtualWorkspaceView } from './virtual-catalog.js' import { nativeWorkspace } from './virtual-catalog.js' import type { AcpClientLike, AcpSessionState, JsonRecord, PendingApproval } from './virtual-contract.js' import { AsyncValueQueue, record, string } from './virtual-contract.js' /** Collaborates with one AcpVirtualHarness; never creates a second session or connection state. */ export interface AcpEventCarrierContext { readonly workspaceStreams: Set readonly visibleWorkspaces: () => AcpVirtualWorkspaceView[] readonly controlStreams: Set readonly eventStreams: Map readonly selectedWorkspaceId: string | undefined readonly workspaceById: Map readonly backendLabel: () => string readonly pendingApprovals: Map readonly client: AcpClientLike readonly sessions: Map readonly nextProjectionSeq: () => number readonly archivedSessionIds: ReadonlySet } export class AcpEventCarrier { constructor(private readonly context: AcpEventCarrierContext) {} async workspaceFollow(signal: AbortSignal): Promise> { const queue = new AsyncValueQueue(signal) this.context.workspaceStreams.add(queue) queue.push({ type: 'baseline', value: { items: this.context.visibleWorkspaces().map(nativeWorkspace), archivedSessionIds: [...this.context.archivedSessionIds], pinnedSessionIds: [], }, }) return queue.iterate(() => this.context.workspaceStreams.delete(queue)) } async sessionControl(signal: AbortSignal): Promise> { const queue = new AsyncValueQueue(signal) this.context.controlStreams.add(queue) // Session Controller treats the first control frame as the opening // snapshot. Without it the Client-side RemoteSnapshotStream never moves // to `ready`, so a workspace selected through the Remote modal remains // stuck on the empty shell after the page reload. queue.push({ type: 'baseline', value: { projections: {} } }) return queue.iterate(() => this.context.controlStreams.delete(queue)) } /** * Background jobs belong to the Host Harness session, not to a backend-scoped * virtual Workspace. The native session header mounts a job roster stream as * soon as a Session opens, so answer it with an authoritative empty roster * instead of a terminal `method-not-found` failure that tears the page down. */ emptyJobStream(endpoint: 'job/list' | 'job/follow', signal: AbortSignal): AsyncIterable { const queue = new AsyncValueQueue(signal) if (endpoint === 'job/list') queue.push({ type: 'rows', jobs: [] }) return queue.iterate(() => queue.close()) } async remoteEvents(signal: AbortSignal): Promise> { const id = `cursor-events:${Date.now()}:${Math.random()}` const queue = new AsyncValueQueue(signal) this.context.eventStreams.set(id, queue) const workspace = this.context.selectedWorkspaceId === undefined ? undefined : this.context.workspaceById.get(this.context.selectedWorkspaceId) queue.push({ type: 'ready', clientId: id, host: { home: workspace?.path ?? '/' }, }) return queue.iterate(() => this.context.eventStreams.delete(id)) } emitRemoteEvent(event: string, args: unknown[]): void { for (const queue of this.context.eventStreams.values()) queue.push({ type: 'emit', event, args }) } emitApproval(input: { eventId: string; agentId: string; request: JsonRecord }): void { for (const queue of this.context.eventStreams.values()) queue.push({ type: 'waterfall', event: 'approval/request', eventId: input.eventId, agentId: input.agentId, request: input.request, }) } async answerRemoteEvent(args: JsonRecord, signal: AbortSignal): Promise { const eventId = string(args.eventId) const outcome = record(args.outcome) if (eventId === undefined) throw new Error(`The ${this.context.backendLabel()} approval result is missing its event id.`) const pending = this.context.pendingApprovals.get(eventId) if (pending === undefined) return undefined this.context.pendingApprovals.delete(eventId) const decision = outcome.kind === 'result' && outcome.value === 'allowed-once' ? 'allow-once' : outcome.kind === 'result' && outcome.value === 'cancelled' ? 'cancel' : 'reject-once' await this.context.client.respond(pending.requestHandle, decision, undefined, signal) return undefined } publishProjection(sessionId: string, key: string, value: unknown, seq = this.context.nextProjectionSeq()): void { for (const queue of this.context.controlStreams) { // 0.2.2 sends per-event `changes`; keep the legacy key/value fields so // older official clients keep applying the same frame. queue.push({ type: 'projection', sessionId, seq, key, value, asOfSeq: seq, changes: [{ key, val: value }], }) } } }