api-proxy-approval.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * Approval pending registry over the proxy: an ask through `ctx.approval`
  3. * becomes an answerable `approval/requested` mux frame (stable rpcId, replayed
  4. * verbatim on a later mux open), `respond` routes by the echoed rpcId and
  5. * validates the audit correlation, and the ask's abort signal withdraws the
  6. * question with a broadcast `cancelled`.
  7. */
  8. import { describe, expect, it } from 'vitest'
  9. import { Context } from 'cordis'
  10. import AgentRegistry from '@deepseek-ai/dsh-agent'
  11. import type { Agent } from '@deepseek-ai/dsh-agent'
  12. import SessionStore from '@deepseek-ai/dsh-session'
  13. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  14. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  15. import ApprovalService from '@deepseek-ai/dsh-user-approval'
  16. import type { ApprovalRequestId } from '@deepseek-ai/dsh-user-approval'
  17. import type { ApiProxy, MuxFrame, RpcRequest } from '@deepseek-ai/dsh-host-apiproxy/api'
  18. import type { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  19. import { RpcId as mintRpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  20. import { createApiProxy } from '../src/api-proxy.ts'
  21. async function harness(): Promise<{ ctx: Context; api: ApiProxy }> {
  22. const ctx = new Context()
  23. await ctx.plugin(SessionStore)
  24. await ctx.plugin(SystemPrompt, { persona: '' })
  25. await ctx.plugin(UserInteractionService)
  26. await ctx.plugin(AgentRegistry)
  27. await ctx.plugin(ApprovalService)
  28. const api = createApiProxy(ctx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' })
  29. return { ctx, api }
  30. }
  31. /** A minimal agent stand-in inside an open turn (the service only reaches `.session`). */
  32. function agentOf(ctx: Context): Agent {
  33. const session = ctx.sessions.create()
  34. session.append('turn/start', { turn: 1 })
  35. return { session } as unknown as Agent
  36. }
  37. /** Open a mux stream and capture frames into an array (returns an on-demand waiter). */
  38. function openMux(api: ApiProxy, abort: AbortController): { frames: MuxFrame[]; envelopes: RpcRequest<MuxFrame>[]; waitFor(type: MuxFrame['type']): Promise<MuxFrame> } {
  39. const frames: MuxFrame[] = []
  40. const envelopes: RpcRequest<MuxFrame>[] = []
  41. const waiters: { type: MuxFrame['type']; resolve: (frame: MuxFrame) => void }[] = []
  42. void (async () => {
  43. for await (const envelope of api.events.mux({ rpcId: mintRpcId('t-mux'), payload: {} }, abort.signal)) {
  44. frames.push(envelope.payload)
  45. envelopes.push(envelope)
  46. for (let i = waiters.length - 1; i >= 0; i -= 1) {
  47. const waiter = waiters[i] as (typeof waiters)[number]
  48. if (waiter.type === envelope.payload.type) {
  49. waiters.splice(i, 1)
  50. waiter.resolve(envelope.payload)
  51. }
  52. }
  53. }
  54. })()
  55. return {
  56. frames,
  57. envelopes,
  58. waitFor: (type) => {
  59. const found = frames.find(frame => frame.type === type)
  60. if (found !== undefined) return Promise.resolve(found)
  61. return new Promise((resolve) => { waiters.push({ type, resolve }) })
  62. },
  63. }
  64. }
  65. function requestedOf(frame: MuxFrame): Extract<MuxFrame, { type: 'approval/requested' }> {
  66. if (frame.type !== 'approval/requested') throw new Error(`expected approval/requested, got ${frame.type}`)
  67. return frame
  68. }
  69. /** Wait until the stream delivered `count` frames of `type` (bounded poll; waitFor only covers the first). */
  70. async function waitForCount(mux: { frames: MuxFrame[] }, type: MuxFrame['type'], count: number): Promise<void> {
  71. for (let i = 0; i < 200 && mux.frames.filter(frame => frame.type === type).length < count; i += 1) {
  72. await new Promise(resolve => setTimeout(resolve, 5))
  73. }
  74. expect(mux.frames.filter(frame => frame.type === type).length).toBeGreaterThanOrEqual(count)
  75. }
  76. function answer(rpcId: RpcId, sessionId: unknown, approvalId: ApprovalRequestId, outcome: 'allowed-once' | 'rejected'): Parameters<ApiProxy['respond']>[0] {
  77. return { type: 'client-response', rpcId, result: { ok: true, value: { sessionId, approvalId, outcome } } }
  78. }
  79. describe('approval pending registry', () => {
  80. it('round-trips ask → requested frame → respond → outcome + resolved broadcast', async () => {
  81. const { ctx, api } = await harness()
  82. const abort = new AbortController()
  83. const mux = openMux(api, abort)
  84. const agent = agentOf(ctx)
  85. const asked = ctx.approval.request({ agent, toolName: 'bash', reason: 'sandbox escalation' })
  86. const requested = requestedOf(await mux.waitFor('approval/requested'))
  87. expect(requested).toMatchObject({ toolName: 'bash', reason: 'sandbox escalation', sessionId: agent.session.id })
  88. const envelope = mux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  89. const receipt = await api.respond(answer(envelope.rpcId, requested.sessionId, requested.approvalId, 'allowed-once'))
  90. expect(receipt).toEqual({ accepted: true })
  91. await expect(asked).resolves.toBe('allowed-once')
  92. const resolved = await mux.waitFor('approval/resolved')
  93. expect(resolved).toMatchObject({ approvalId: requested.approvalId, outcome: 'allowed-once' })
  94. // The question settled: a duplicate answer is late, not re-decidable.
  95. const dup = await api.respond(answer(envelope.rpcId, requested.sessionId, requested.approvalId, 'rejected'))
  96. expect(dup).toEqual({ accepted: false, reason: 'not-pending' })
  97. abort.abort()
  98. })
  99. it('replays a still-pending requested frame (same rpcId) on a later mux open', async () => {
  100. const { ctx, api } = await harness()
  101. const first = new AbortController()
  102. const firstMux = openMux(api, first)
  103. const agent = agentOf(ctx)
  104. const asked = ctx.approval.request({ agent, toolName: 'write' })
  105. const requested = requestedOf(await firstMux.waitFor('approval/requested'))
  106. const firstEnvelope = firstMux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  107. first.abort()
  108. // A fresh subscriber (refresh recovery) sees the same stable rpcId.
  109. const second = new AbortController()
  110. const secondMux = openMux(api, second)
  111. const replayed = requestedOf(await secondMux.waitFor('approval/requested'))
  112. const secondEnvelope = secondMux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  113. expect(secondEnvelope.rpcId).toBe(firstEnvelope.rpcId)
  114. expect(replayed.approvalId).toBe(requested.approvalId)
  115. const receipt = await api.respond(answer(secondEnvelope.rpcId, replayed.sessionId, replayed.approvalId, 'rejected'))
  116. expect(receipt).toEqual({ accepted: true })
  117. await expect(asked).resolves.toBe('rejected')
  118. second.abort()
  119. })
  120. it('rejects malformed and mismatched answers as bad-response, unknown ids as not-pending', async () => {
  121. const { ctx, api } = await harness()
  122. const abort = new AbortController()
  123. const mux = openMux(api, abort)
  124. const agent = agentOf(ctx)
  125. void ctx.approval.request({ agent, toolName: 'bash' })
  126. const requested = requestedOf(await mux.waitFor('approval/requested'))
  127. const envelope = mux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  128. // Unknown rpcId: not routed to any pending entry.
  129. expect(await api.respond(answer(mintRpcId('ghost'), requested.sessionId, requested.approvalId, 'rejected')))
  130. .toEqual({ accepted: false, reason: 'not-pending' })
  131. // Error-branch result: the client can only answer with a value.
  132. expect(await api.respond({ type: 'client-response', rpcId: envelope.rpcId, result: { ok: false, error: { code: 'internal', message: 'x', details: {} } } }))
  133. .toEqual({ accepted: false, reason: 'bad-response' })
  134. // Wrong audit correlation: the rpcId routed, but the payload disagrees.
  135. expect(await api.respond(answer(envelope.rpcId, requested.sessionId, 'other-approval' as ApprovalRequestId, 'rejected')))
  136. .toEqual({ accepted: false, reason: 'bad-response' })
  137. // Malformed payload shape.
  138. expect(await api.respond({ type: 'client-response', rpcId: envelope.rpcId, result: { ok: true, value: { nonsense: 1 } } }))
  139. .toEqual({ accepted: false, reason: 'bad-response' })
  140. abort.abort()
  141. })
  142. it('withdraws the question on the ask signal: cancelled outcome, resolved broadcast, late answer not-pending', async () => {
  143. const { ctx, api } = await harness()
  144. const abort = new AbortController()
  145. const mux = openMux(api, abort)
  146. const agent = agentOf(ctx)
  147. const cancel = new AbortController()
  148. const asked = ctx.approval.request({ agent, toolName: 'bash', signal: cancel.signal })
  149. const requested = requestedOf(await mux.waitFor('approval/requested'))
  150. const envelope = mux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  151. cancel.abort()
  152. await expect(asked).resolves.toBe('cancelled')
  153. const resolved = await mux.waitFor('approval/resolved')
  154. expect(resolved).toMatchObject({ approvalId: requested.approvalId, outcome: 'cancelled' })
  155. expect(await api.respond(answer(envelope.rpcId, requested.sessionId, requested.approvalId, 'allowed-once')))
  156. .toEqual({ accepted: false, reason: 'not-pending' })
  157. abort.abort()
  158. })
  159. it('an ask whose signal aborted before dispatch settles cancelled without publishing', async () => {
  160. // The service checks the signal, then dispatch rides a microtask: an
  161. // abort in that window must not register a dead listener and strand the
  162. // entry (zombie frame on every replay). Drive the waterfall directly
  163. // with a pre-aborted signal to hit the answerer's register-path guard.
  164. const { ctx, api } = await harness()
  165. const abort = new AbortController()
  166. const mux = openMux(api, abort)
  167. const session = ctx.sessions.create()
  168. session.append('turn/start', { turn: 1 })
  169. session.append('approval/asked', { id: 'pre-aborted' as ApprovalRequestId, toolName: 'bash' })
  170. const agent = { session } as unknown as Agent
  171. const cancelled = new AbortController()
  172. cancelled.abort()
  173. const outcome = await ctx.waterfall(
  174. 'approval/request',
  175. { agent, toolName: 'bash', signal: cancelled.signal },
  176. () => Promise.resolve('unavailable' as const),
  177. )
  178. expect(outcome).toBe('cancelled')
  179. // Nothing was published: a fresh mux open replays no approval frame.
  180. const abort2 = new AbortController()
  181. const mux2 = openMux(api, abort2)
  182. await new Promise(resolve => setTimeout(resolve, 10))
  183. expect(mux2.envelopes.some(e => e.payload.type === 'approval/requested')).toBe(false)
  184. abort2.abort()
  185. abort.abort()
  186. void mux
  187. })
  188. it('gateway teardown settles pending approvals as cancelled (question-provider parity)', async () => {
  189. // Mount the proxy on its own fiber so disposal exercises the teardown
  190. // effect while an ask is still pending.
  191. const ctx = new Context()
  192. await ctx.plugin(SessionStore)
  193. await ctx.plugin(SystemPrompt, { persona: '' })
  194. await ctx.plugin(UserInteractionService)
  195. await ctx.plugin(AgentRegistry)
  196. await ctx.plugin(ApprovalService)
  197. let api!: ApiProxy
  198. const fiber = ctx.plugin(Object.assign((fiberCtx: Context) => {
  199. api = createApiProxy(fiberCtx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' })
  200. }, { inject: ['sessions', 'agents', 'userInteraction', 'approval'] }))
  201. await fiber.await()
  202. const abort = new AbortController()
  203. const mux = openMux(api, abort)
  204. const asked = ctx.approval.request({ agent: agentOf(ctx), toolName: 'bash' })
  205. const requested = requestedOf(await mux.waitFor('approval/requested'))
  206. await fiber.dispose()
  207. await expect(asked).resolves.toBe('cancelled')
  208. const resolved = await mux.waitFor('approval/resolved')
  209. expect(resolved).toMatchObject({ approvalId: requested.approvalId, outcome: 'cancelled' })
  210. abort.abort()
  211. })
  212. it('carries callId on the frame and ignores a late abort after the answer settled', async () => {
  213. const { ctx, api } = await harness()
  214. const abort = new AbortController()
  215. const mux = openMux(api, abort)
  216. const agent = agentOf(ctx)
  217. const cancel = new AbortController()
  218. const asked = ctx.approval.request({ agent, toolName: 'bash', callId: 'call-9' as never, signal: cancel.signal })
  219. const requested = requestedOf(await mux.waitFor('approval/requested'))
  220. expect(requested.callId).toBe('call-9')
  221. const envelope = mux.envelopes.find(e => e.payload.type === 'approval/requested') as RpcRequest<MuxFrame>
  222. expect(await api.respond(answer(envelope.rpcId, requested.sessionId, requested.approvalId, 'allowed-once')))
  223. .toEqual({ accepted: true })
  224. await expect(asked).resolves.toBe('allowed-once')
  225. // Late abort: the pending entry is gone; settle's delete-guard returns.
  226. cancel.abort()
  227. expect(mux.frames.filter(f => f.type === 'approval/resolved')).toHaveLength(1)
  228. abort.abort()
  229. })
  230. it('pairs parallel asks by callId: each requested frame carries its own audit id', async () => {
  231. const { ctx, api } = await harness()
  232. const abort = new AbortController()
  233. const mux = openMux(api, abort)
  234. const agent = agentOf(ctx)
  235. // Both asks append their approval/asked audit events before either
  236. // answerer's microtask dispatch runs — the parallel tool-call window.
  237. const askA = ctx.approval.request({ agent, toolName: 'bash', callId: 'call-a' as never })
  238. const askB = ctx.approval.request({ agent, toolName: 'bash', callId: 'call-b' as never })
  239. await waitForCount(mux, 'approval/requested', 2)
  240. const frames = mux.envelopes.filter(e => e.payload.type === 'approval/requested')
  241. const frameA = frames.find(e => requestedOf(e.payload).callId === 'call-a') as RpcRequest<MuxFrame>
  242. const frameB = frames.find(e => requestedOf(e.payload).callId === 'call-b') as RpcRequest<MuxFrame>
  243. // Each frame claimed the asked event with its own callId, not merely the newest.
  244. const askedIdByCall = new Map(agent.session.events
  245. .filter(event => event.type === 'approval/asked')
  246. .map(event => [String(event.data.callId), event.data.id]))
  247. expect(requestedOf(frameA.payload).approvalId).toBe(askedIdByCall.get('call-a'))
  248. expect(requestedOf(frameB.payload).approvalId).toBe(askedIdByCall.get('call-b'))
  249. // Answers route back to the right ask through the pairing.
  250. expect(await api.respond(answer(frameB.rpcId, agent.session.id, requestedOf(frameB.payload).approvalId, 'rejected')))
  251. .toEqual({ accepted: true })
  252. expect(await api.respond(answer(frameA.rpcId, agent.session.id, requestedOf(frameA.payload).approvalId, 'allowed-once')))
  253. .toEqual({ accepted: true })
  254. await expect(askA).resolves.toBe('allowed-once')
  255. await expect(askB).resolves.toBe('rejected')
  256. abort.abort()
  257. })
  258. it('gives parallel callId-less asks distinct audit ids (claimed-entry skip); both stay answerable', async () => {
  259. const { ctx, api } = await harness()
  260. const abort = new AbortController()
  261. const mux = openMux(api, abort)
  262. const agent = agentOf(ctx)
  263. const askA = ctx.approval.request({ agent, toolName: 'alpha' })
  264. const askB = ctx.approval.request({ agent, toolName: 'beta' })
  265. await waitForCount(mux, 'approval/requested', 2)
  266. const frames = mux.envelopes.filter(e => e.payload.type === 'approval/requested')
  267. const frameA = frames.find(e => requestedOf(e.payload).toolName === 'alpha') as RpcRequest<MuxFrame>
  268. const frameB = frames.find(e => requestedOf(e.payload).toolName === 'beta') as RpcRequest<MuxFrame>
  269. // Without a callId the pairing is heuristic, but never shared: the second
  270. // dispatch skips the id the first pending entry already claimed.
  271. expect(requestedOf(frameA.payload).approvalId).not.toBe(requestedOf(frameB.payload).approvalId)
  272. expect(await api.respond(answer(frameA.rpcId, agent.session.id, requestedOf(frameA.payload).approvalId, 'allowed-once')))
  273. .toEqual({ accepted: true })
  274. expect(await api.respond(answer(frameB.rpcId, agent.session.id, requestedOf(frameB.payload).approvalId, 'rejected')))
  275. .toEqual({ accepted: true })
  276. await expect(askA).resolves.toBe('allowed-once')
  277. await expect(askB).resolves.toBe('rejected')
  278. abort.abort()
  279. })
  280. it('delegates a dispatch whose only asked candidate is already decided (stale re-dispatch)', async () => {
  281. const { ctx, api } = await harness()
  282. void api // the answerer is registered; the fake below bypasses the service
  283. // Bypass ApprovalService: a log whose sole asked event already has its
  284. // decided partner must not be re-claimed — the answerer delegates.
  285. const session = ctx.sessions.create()
  286. session.append('turn/start', { turn: 1 })
  287. session.append('approval/asked', { id: 'stale-ask' as ApprovalRequestId, toolName: 'bash' })
  288. session.append('approval/decided', { id: 'stale-ask' as ApprovalRequestId, outcome: 'rejected' })
  289. const agent = { session } as unknown as Agent
  290. const outcome = await ctx.waterfall('approval/request', { agent, toolName: 'bash' }, () => Promise.resolve('unavailable' as const))
  291. expect(outcome).toBe('unavailable')
  292. })
  293. it('delegates an ask whose session log carries no asked audit event (foreign channel)', async () => {
  294. const { ctx, api } = await harness()
  295. void api // the answerer is registered; the fake below bypasses the audit path
  296. // Bypass ApprovalService: dispatch the waterfall directly with a session
  297. // that has no approval/asked event — the proxy answerer must call next().
  298. const session = ctx.sessions.create()
  299. session.append('turn/start', { turn: 1 })
  300. const agent = { session } as unknown as Agent
  301. const outcome = await ctx.waterfall('approval/request', { agent, toolName: 'x' }, () => Promise.resolve('unavailable' as const))
  302. expect(outcome).toBe('unavailable')
  303. })
  304. })