The web host now composes the sandboxed product path (sandbox-local + sandbox-policy behind bash-sandbox/fs-sandbox, with user-approval and permission on top); BootHostOptions.sandbox carries the deployment defaults (workspace-write + ask). createApiProxy owns the approval pending registry: a ctx.approval ask becomes an answerable approval/requested mux frame with a stable rpcId, replayed verbatim on every mux open until settled; respond routes by the echoed rpcId, validates the ApprovalResponsePayload audit correlation, and broadcasts approval/resolved; the ask's abort signal withdraws the question as cancelled. session.permissions / session.setPermission project ctx.permission into a protocol-owned PermissionOption select; idle switches are held last-write-wins and flushed into the next prompted turn (the ACP bridge's anchoring pattern). The shared hasOpenTurn fold moved to dsh-session, deduplicating the private copies in user-approval, the ACP bridge, and the proxy. Client, per the designer draft: a pending approval takes over the composer (ApprovalPanel replaces the InputBar — amber strip, justification headline, paired command, one-shot refuse/allow, keyed by rpcId so a queued second approval remounts live; the resolved frame restores the composer); the sidebar session row shows an amber waiting-approval dot that outranks the running ring (manager-tracked approvalId set, idempotent under mux-open replays, cleared per connection generation, lit for uninstantiated sessions too); the permission selector is a composer bottom-row chip over an invisible native select, with a presentation-only title-case transform (workspace-write renders as Workspace Write; wire names untouched). Question placeholders stay in the message flow. The connection fixture mirrors the host behavior for keyless browser acceptance.
169 lines
7.2 KiB
TypeScript
169 lines
7.2 KiB
TypeScript
// Test-local programmable IApiClient fake (NOT the fixture: fixture is a demo
|
|
// data source on a real clock; behavior tests need per-case responses and
|
|
// deferred-controlled timing). Streams are hand pumps: pushMux/pushHost.
|
|
import type {
|
|
HostFrame, IApiClient, MuxFrame, RpcRequest, RpcResponse, SessionId,
|
|
} from '../src/client/api.ts'
|
|
import { RpcId } from '../src/client/api.ts'
|
|
|
|
export interface Deferred<T> {
|
|
promise: Promise<T>
|
|
resolve(value: T): void
|
|
reject(error: unknown): void
|
|
}
|
|
|
|
/** Test-held settlement: the case decides when an RPC lands (history-pending injections etc.). */
|
|
export function deferred<T>(): Deferred<T> {
|
|
let resolve!: (value: T) => void
|
|
let reject!: (error: unknown) => void
|
|
const promise = new Promise<T>((res, rej) => {
|
|
resolve = res
|
|
reject = rej
|
|
})
|
|
return { promise, resolve, reject }
|
|
}
|
|
|
|
let nextRpc = 0
|
|
|
|
export function ok<T>(value: T): RpcResponse<T> {
|
|
return { rpcId: RpcId(`fake-${nextRpc++}`), result: { ok: true, value } }
|
|
}
|
|
|
|
|
|
type StreamItem<F> = { kind: 'frame'; envelope: RpcRequest<F> } | { kind: 'end' } | { kind: 'fail'; error: unknown }
|
|
|
|
interface StreamConn<F> {
|
|
feed(item: StreamItem<F>): void
|
|
}
|
|
|
|
export class FakeApiClient implements IApiClient {
|
|
/** Chronological call record: [method, payload]. */
|
|
readonly calls: { method: string; payload: unknown }[] = []
|
|
|
|
// Programmable slots (defaults answer OK-empty); reassign per case.
|
|
onList: (payload: unknown) => Promise<RpcResponse<{ items: never[] }>> = () => Promise.resolve(ok({ items: [] }))
|
|
onCreate: (payload: unknown) => Promise<RpcResponse<{ sessionId: SessionId }>> = () => Promise.resolve(ok({ sessionId: 'fk-new' as SessionId }))
|
|
onHistory: (payload: { sessionId: SessionId; beforeSeq?: number; maxMessages?: number })
|
|
=> Promise<RpcResponse<{ events: never[]; hasMore: boolean }>> =
|
|
() => Promise.resolve(ok({ events: [], hasMore: false }))
|
|
|
|
onPrompt: (payload: unknown) => Promise<RpcResponse<{ accepted: true }>> = () => Promise.resolve(ok({ accepted: true as const }))
|
|
onCancel: (payload: unknown) => Promise<RpcResponse<{ accepted: true }>> = () => Promise.resolve(ok({ accepted: true as const }))
|
|
onPermissions: (payload: unknown) =>
|
|
Promise<RpcResponse<{ options: { value: string; name: string; description?: string }[]; currentValue: string }>> =
|
|
() => Promise.resolve(ok({ options: [], currentValue: 'custom' }))
|
|
|
|
onSetPermission: (payload: { sessionId: SessionId; value: string }) => Promise<RpcResponse<{ currentValue: string }>> =
|
|
payload => Promise.resolve(ok({ currentValue: payload.value }))
|
|
onDescribe: (payload: unknown) => Promise<RpcResponse<{ version: string; cwd: string; attachedSessions: number }>> =
|
|
() => Promise.resolve(ok({ version: '0-fake', cwd: '/f', attachedSessions: 0 }))
|
|
|
|
private readonly muxConns: StreamConn<MuxFrame>[] = []
|
|
private readonly hostConns: StreamConn<HostFrame>[] = []
|
|
|
|
// Parameter annotations below are local structural types on purpose: the CI
|
|
// lint lane runs without built artifacts, where IApiClient's wire types
|
|
// (apiproxy subpath) resolve to any and inferred params trip no-unsafe-argument.
|
|
readonly sessions: IApiClient['sessions'] = {
|
|
list: (payload: unknown) => this.record('session.list', payload, this.onList(payload)),
|
|
create: (payload: unknown) => this.record('session.create', payload, this.onCreate(payload)),
|
|
history: (payload: { sessionId: SessionId; beforeSeq?: number; maxMessages?: number }) =>
|
|
this.record('session.history', payload, this.onHistory(payload)),
|
|
prompt: (payload: unknown) => this.record('session.prompt', payload, this.onPrompt(payload)),
|
|
cancel: (payload: unknown) => this.record('session.cancel', payload, this.onCancel(payload)),
|
|
permissions: (payload: unknown) => this.record('session.permissions', payload, this.onPermissions(payload)),
|
|
setPermission: (payload: { sessionId: SessionId; value: string }) => this.record('session.setPermission', payload, this.onSetPermission(payload)),
|
|
}
|
|
|
|
readonly host: IApiClient['host'] = {
|
|
describe: payload => this.record('host.describe', payload, this.onDescribe(payload)),
|
|
}
|
|
|
|
/** When true, streams never fire onOpen (misbehaving-carrier material for the handshake timeout guard). */
|
|
suppressStreamOpen = false
|
|
|
|
/** When true, onOpen callbacks are parked instead of fired; releaseStreamOpens() fires them.
|
|
* Lets a case hold the readiness handshake open (describe done, streams not yet "established"). */
|
|
holdStreamOpen = false
|
|
private heldOpens: (() => void)[] = []
|
|
|
|
releaseStreamOpens(): void {
|
|
const held = this.heldOpens
|
|
this.heldOpens = []
|
|
for (const fire of held) fire()
|
|
}
|
|
|
|
readonly events: IApiClient['events'] = {
|
|
mux: (_payload: unknown, signal: AbortSignal, onOpen?: () => void) =>
|
|
this.openStream(this.muxConns, signal, onOpen),
|
|
host: (_payload: unknown, signal: AbortSignal, onOpen?: () => void) =>
|
|
this.openStream(this.hostConns, signal, onOpen),
|
|
}
|
|
|
|
respond(): Promise<{ accepted: false; reason: 'not-pending' }> {
|
|
return Promise.resolve({ accepted: false, reason: 'not-pending' })
|
|
}
|
|
|
|
/** Push one mux frame to every open mux stream (rpcId minted unless pinned by the case). */
|
|
pushMux(frame: MuxFrame, rpcId?: string): void {
|
|
for (const conn of [...this.muxConns]) conn.feed({ kind: 'frame', envelope: { rpcId: RpcId(rpcId ?? `push-${nextRpc++}`), payload: frame } })
|
|
}
|
|
|
|
pushHost(frame: HostFrame, rpcId?: string): void {
|
|
for (const conn of [...this.hostConns]) conn.feed({ kind: 'frame', envelope: { rpcId: RpcId(rpcId ?? `push-${nextRpc++}`), payload: frame } })
|
|
}
|
|
|
|
/** End (clean close) or fail (throw) every open stream — reconnect-path material. */
|
|
endStreams(): void {
|
|
for (const conn of [...this.muxConns, ...this.hostConns]) conn.feed({ kind: 'end' })
|
|
}
|
|
|
|
failStreams(error: unknown): void {
|
|
for (const conn of [...this.muxConns, ...this.hostConns]) conn.feed({ kind: 'fail', error })
|
|
}
|
|
|
|
get openMuxCount(): number {
|
|
return this.muxConns.length
|
|
}
|
|
|
|
callsOf(method: string): unknown[] {
|
|
return this.calls.filter(c => c.method === method).map(c => c.payload)
|
|
}
|
|
|
|
private record<T>(method: string, payload: unknown, response: Promise<T>): Promise<T> {
|
|
this.calls.push({ method, payload })
|
|
return response
|
|
}
|
|
|
|
private async *openStream<F>(registry: StreamConn<F>[], signal: AbortSignal, onOpen?: () => void): AsyncGenerator<RpcRequest<F>> {
|
|
const inbox: StreamItem<F>[] = []
|
|
let wake: (() => void) | null = null
|
|
const conn: StreamConn<F> = {
|
|
feed: (item) => {
|
|
inbox.push(item)
|
|
wake?.()
|
|
},
|
|
}
|
|
registry.push(conn)
|
|
if (this.holdStreamOpen && onOpen !== undefined) this.heldOpens.push(onOpen)
|
|
else if (!this.suppressStreamOpen) onOpen?.()
|
|
try {
|
|
while (!signal.aborted) {
|
|
while (inbox.length > 0) {
|
|
const item = inbox.shift() as StreamItem<F>
|
|
if (item.kind === 'end') return
|
|
if (item.kind === 'fail') throw item.error
|
|
yield item.envelope
|
|
}
|
|
await new Promise<void>((resolve) => {
|
|
wake = resolve
|
|
signal.addEventListener('abort', () => { resolve() }, { once: true })
|
|
})
|
|
wake = null
|
|
}
|
|
} finally {
|
|
registry.splice(registry.indexOf(conn), 1)
|
|
}
|
|
}
|
|
}
|