subagent-interrupt.e2e.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Web e2e scenario (browserless): the subagents interrupt Remote against the real
  2. // composition. A live continuable child holds its model turn open through a
  3. // replay hang entry; plain HTTP queues a follow-up, interrupts the turn, and
  4. // proves from the real session state that the turn aborted, the follow-up
  5. // parked without auto-starting a new turn, and a later waking send resumed the
  6. // preserved FIFO order. No browser: the RPC surface is the product surface
  7. // under test, and subagent-interrupt-ui.e2e.ts owns the composer interaction.
  8. import { randomUUID } from 'node:crypto'
  9. import { existsSync } from 'node:fs'
  10. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  11. import { tmpdir } from 'node:os'
  12. import { join } from 'node:path'
  13. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  14. import { SessionId as sessionId, type SessionId } from '@deepseek-ai/dsh-session'
  15. import type {} from '@deepseek-ai/dsh-agent'
  16. import { launchWebScaffold, readPersistedEvents, webSnapshotMode, type WebScaffold } from './scaffold.ts'
  17. const MODE = webSnapshotMode()
  18. const INITIAL = 'Explain event sourcing in one sentence.'
  19. const FOLLOWUP = 'Now give the same explanation to a human reader.'
  20. const WAKING = 'And add one concrete example.'
  21. type RpcResult<T> = { ok: true; value: T } | { ok: false; error: { code: string; message: string } }
  22. /** POST one generated Remote unary through the API Gateway carrier. */
  23. async function remote<T>(
  24. scaffold: WebScaffold,
  25. endpoint: string,
  26. args: Readonly<Record<string, unknown>>,
  27. ): Promise<RpcResult<T>> {
  28. const response = await scaffold.hostFetch(`/api/${endpoint}`, {
  29. method: 'POST',
  30. headers: { 'content-type': 'application/json' },
  31. body: JSON.stringify({
  32. type: 'client-request',
  33. rpcId: `interrupt-e2e-${endpoint}-${randomUUID()}`,
  34. method: endpoint,
  35. payload: { args },
  36. }),
  37. })
  38. if (!response.ok) throw new Error(`${endpoint} failed over HTTP ${response.status}: ${await response.text()}`)
  39. return (await response.json() as { result: RpcResult<T> }).result
  40. }
  41. /** POST one generated Session Remote unary through the API Gateway carrier. */
  42. function sessionRemote<T>(scaffold: WebScaffold, method: string, request: unknown): Promise<RpcResult<T>> {
  43. return remote<T>(scaffold, `session/${method}`, { request })
  44. }
  45. /** Poll a synchronous condition (hook-safe; expect.poll is test-body only). */
  46. async function waitFor(predicate: () => boolean, what: string, timeoutMs = 30_000): Promise<void> {
  47. const deadline = Date.now() + timeoutMs
  48. while (!predicate()) {
  49. if (Date.now() >= deadline) throw new Error(`timed out waiting for ${what}`)
  50. await new Promise<void>(resolve => setTimeout(resolve, 10))
  51. }
  52. }
  53. /** One text-only scripted model completion (no tool calls: real tools are mounted). */
  54. function textCompletion(text: string): object {
  55. return {
  56. kind: 'chunks',
  57. chunks: [
  58. { type: 'block-start', index: 0, blockType: 'text' },
  59. { type: 'text-delta', index: 0, text },
  60. { type: 'block-end', index: 0, block: { type: 'text', text } },
  61. { type: 'usage', usage: { inputTokens: 20, outputTokens: 8 } },
  62. { type: 'finish', reason: { kind: 'stop' } },
  63. ],
  64. }
  65. }
  66. describe.skipIf(MODE === 'record')('web e2e: subagents/interruptByParent over the real composition', () => {
  67. let scaffold: WebScaffold
  68. let sidecarRoot: string
  69. let readyFile: string
  70. let parentId: SessionId
  71. let childId: SessionId
  72. beforeAll(async () => {
  73. sidecarRoot = await mkdtemp(join(tmpdir(), 'dsh-web-subagent-interrupt-'))
  74. readyFile = join(sidecarRoot, 'hang-ready')
  75. // Whole-script replacement: the child's three model calls are the hang
  76. // (turn 1, interrupted), the parked follow-up's turn, and the waking turn.
  77. // The parent never runs a turn, so the child claims this primary script.
  78. await writeFile(join(sidecarRoot, 'replay.override.json'), JSON.stringify([
  79. { kind: 'hang', readyFile },
  80. textCompletion('resumed response one'),
  81. textCompletion('resumed response two'),
  82. ]))
  83. // Header-only primary fixture: the bare-array override replaces the
  84. // derived script entirely; the path only anchors replay installation.
  85. await writeFile(
  86. join(sidecarRoot, 'session.jsonl'),
  87. '{"type":"session","version":0,"id":"primary","createdAt":0}\n',
  88. )
  89. scaffold = await launchWebScaffold({
  90. replayFixture: join(sidecarRoot, 'session.jsonl'),
  91. replayOverride: join(sidecarRoot, 'replay.override.json'),
  92. })
  93. // A live parent Agent through the real API; no workspace or browser.
  94. const created = await sessionRemote<{ sessionId: string }>(scaffold, 'create', {
  95. cwd: scaffold.workspaceCwd,
  96. })
  97. if (!created.ok) throw new Error(`session.create failed: ${created.error.code}`)
  98. parentId = sessionId(created.value.sessionId)
  99. const parent = scaffold.ctx.agents.get(parentId)
  100. if (parent === undefined) throw new Error('created parent session did not publish a live Agent')
  101. const started = await scaffold.ctx.subagents.startContinuable({
  102. provider: 'spawn',
  103. label: 'event-sourcing researcher',
  104. signal: new AbortController().signal,
  105. request: { prompt: [{ type: 'text', text: INITIAL }], parent },
  106. })
  107. childId = started.childId
  108. // The hang entry writes readyFile after its prefix chunks, immediately
  109. // before waiting for cancellation: the deterministic "turn is open" gate.
  110. await waitFor(() => existsSync(readyFile), 'the held child turn to open')
  111. }, 120_000)
  112. afterAll(async () => {
  113. const failures: unknown[] = []
  114. await scaffold?.close().catch((error: unknown) => failures.push(error))
  115. await rm(sidecarRoot, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  116. if (failures.length === 1) throw failures[0]
  117. if (failures.length > 1) throw new AggregateError(failures, 'subagent interrupt teardown failed')
  118. })
  119. it('parks a queued follow-up on interrupt and resumes it FIFO on a waking send', async () => {
  120. // Queue the follow-up while the turn is still open, then interrupt.
  121. const queued = await remote<{ messageId: string }>(scaffold, 'subagents/prompt', {
  122. request: {
  123. requestId: randomUUID(),
  124. parentSessionId: parentId,
  125. childSessionId: childId,
  126. mode: 'continuable',
  127. delivery: 'queue',
  128. content: [{ type: 'text', text: FOLLOWUP }],
  129. },
  130. })
  131. expect(queued).toMatchObject({ ok: true })
  132. const settled = scaffold.whenTurnSettled()
  133. const interrupted = await remote<{ accepted: true }>(scaffold, 'subagents/interruptByParent', {
  134. childSessionId: childId,
  135. parentSessionId: parentId,
  136. mode: 'continuable',
  137. })
  138. expect(interrupted).toMatchObject({ ok: true, value: { accepted: true } })
  139. // accepted acknowledges the admitted cancel, not quiescence: wait for the
  140. // aborted turn/end (the composition's first turn/end) before asserting.
  141. expect(await settled).toBe(childId)
  142. // Parked, not resumed: the Activation stays resident with an idle driver,
  143. // the follow-up is retained, and no second turn opened.
  144. const child = scaffold.ctx.agents.get(childId)
  145. expect(child).toBeDefined()
  146. expect(child!.status).toBe('idle')
  147. expect(child!.inbox.nextTurn).toHaveLength(1)
  148. expect(child!.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(1)
  149. const lastEnd = child!.session.snapshotEvents().filter(event => event.type === 'turn/end').at(-1)
  150. expect((lastEnd)?.data.reason.kind).toBe('aborted')
  151. // Only an explicit waking send resumes the parked queue, FIFO, then the
  152. // child runs both turns to completion and settles.
  153. const waking = await remote<{ messageId: string }>(scaffold, 'subagents/prompt', {
  154. request: {
  155. requestId: randomUUID(),
  156. parentSessionId: parentId,
  157. childSessionId: childId,
  158. mode: 'continuable',
  159. delivery: 'queue',
  160. content: [{ type: 'text', text: WAKING }],
  161. },
  162. })
  163. expect(waking).toMatchObject({ ok: true })
  164. await expect.poll(() => scaffold.ctx.agents.get(childId), { timeout: 60_000 }).toBeUndefined()
  165. // The settled child's loop appended every turn's closing events durably,
  166. // so the physical log carries the complete record asserted here.
  167. const events = await readPersistedEvents(scaffold, childId)
  168. // Human-origin messages only: the real composition also injects
  169. // runtime-context snapshots as non-user-source messages.
  170. const userTexts = events.flatMap(event => event.type === 'user/message'
  171. && event.data.source.kind === 'user'
  172. ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : [])
  173. : [])
  174. expect(userTexts[0]).toBe(INITIAL)
  175. expect(userTexts[1]).toMatch(/^Your parent agent id is .+send_message\(\{ agent_id: /)
  176. expect(userTexts.slice(2)).toEqual([FOLLOWUP, WAKING])
  177. const turnEndKinds = events
  178. .filter(event => event.type === 'turn/end')
  179. .map(event => (event).data.reason.kind)
  180. expect(turnEndKinds).toEqual(['aborted', 'completed', 'completed'])
  181. }, 120_000)
  182. })