subagent-interrupt.e2e.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Web e2e scenario (browserless): the subagent.interrupt RPC 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, 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 unary RPC through the real HTTP carrier and unwrap its result. */
  23. async function rpc<T>(baseUrl: string, method: string, payload: unknown): Promise<RpcResult<T>> {
  24. const response = await fetch(`${baseUrl}/api/${method}`, {
  25. method: 'POST',
  26. headers: { 'content-type': 'application/json' },
  27. body: JSON.stringify({
  28. type: 'client-request',
  29. rpcId: `interrupt-e2e-${method}-${randomUUID()}`,
  30. method,
  31. payload,
  32. }),
  33. })
  34. if (!response.ok) throw new Error(`${method} failed over HTTP ${response.status}: ${await response.text()}`)
  35. return (await response.json() as { result: RpcResult<T> }).result
  36. }
  37. /** Poll a synchronous condition (hook-safe; expect.poll is test-body only). */
  38. async function waitFor(predicate: () => boolean, what: string, timeoutMs = 30_000): Promise<void> {
  39. const deadline = Date.now() + timeoutMs
  40. while (!predicate()) {
  41. if (Date.now() >= deadline) throw new Error(`timed out waiting for ${what}`)
  42. await new Promise<void>(resolve => setTimeout(resolve, 10))
  43. }
  44. }
  45. /** One text-only scripted model completion (no tool calls: real tools are mounted). */
  46. function textCompletion(text: string): object {
  47. return {
  48. kind: 'chunks',
  49. chunks: [
  50. { type: 'block-start', index: 0, blockType: 'text' },
  51. { type: 'text-delta', index: 0, text },
  52. { type: 'block-end', index: 0, block: { type: 'text', text } },
  53. { type: 'usage', usage: { inputTokens: 20, outputTokens: 8 } },
  54. { type: 'finish', reason: { kind: 'stop' } },
  55. ],
  56. }
  57. }
  58. describe.skipIf(MODE === 'record')('web e2e: subagent.interrupt over the real composition', () => {
  59. let scaffold: WebScaffold
  60. let sidecarRoot: string
  61. let readyFile: string
  62. let parentId: SessionId
  63. let childId: SessionId
  64. beforeAll(async () => {
  65. sidecarRoot = await mkdtemp(join(tmpdir(), 'dsh-web-subagent-interrupt-'))
  66. readyFile = join(sidecarRoot, 'hang-ready')
  67. // Whole-script replacement: the child's three model calls are the hang
  68. // (turn 1, interrupted), the parked follow-up's turn, and the waking turn.
  69. // The parent never runs a turn, so the child claims this primary script.
  70. await writeFile(join(sidecarRoot, 'replay.override.json'), JSON.stringify([
  71. { kind: 'hang', readyFile },
  72. textCompletion('resumed response one'),
  73. textCompletion('resumed response two'),
  74. ]))
  75. // Header-only primary fixture: the bare-array override replaces the
  76. // derived script entirely; the path only anchors replay installation.
  77. await writeFile(
  78. join(sidecarRoot, 'session.jsonl'),
  79. '{"type":"session","version":0,"id":"primary","createdAt":0}\n',
  80. )
  81. scaffold = await launchWebScaffold({
  82. replayFixture: join(sidecarRoot, 'session.jsonl'),
  83. replayOverride: join(sidecarRoot, 'replay.override.json'),
  84. })
  85. // A live parent Agent through the real API; no workspace or browser.
  86. const created = await rpc<{ sessionId: string }>(scaffold.baseUrl, 'session.create', {
  87. cwd: scaffold.workspaceCwd,
  88. })
  89. if (!created.ok) throw new Error(`session.create failed: ${created.error.code}`)
  90. parentId = sessionId(created.value.sessionId)
  91. const parent = scaffold.ctx.agents.get(parentId)
  92. if (parent === undefined) throw new Error('created parent session did not publish a live Agent')
  93. const started = await scaffold.ctx.subagents.startContinuable({
  94. provider: 'spawn',
  95. label: 'event-sourcing researcher',
  96. signal: new AbortController().signal,
  97. request: { prompt: [{ type: 'text', text: INITIAL }], parent },
  98. })
  99. childId = started.childId
  100. // The hang entry writes readyFile after its prefix chunks, immediately
  101. // before waiting for cancellation: the deterministic "turn is open" gate.
  102. await waitFor(() => existsSync(readyFile), 'the held child turn to open')
  103. }, 120_000)
  104. afterAll(async () => {
  105. const failures: unknown[] = []
  106. await scaffold?.close().catch((error: unknown) => failures.push(error))
  107. await rm(sidecarRoot, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  108. if (failures.length === 1) throw failures[0]
  109. if (failures.length > 1) throw new AggregateError(failures, 'subagent interrupt teardown failed')
  110. })
  111. it('parks a queued follow-up on interrupt and resumes it FIFO on a waking send', async () => {
  112. // Queue the follow-up while the turn is still open, then interrupt.
  113. const queued = await rpc<{ messageId: string }>(scaffold.baseUrl, 'subagent.prompt', {
  114. parentSessionId: parentId,
  115. childSessionId: childId,
  116. mode: 'continuable',
  117. content: [{ type: 'text', text: FOLLOWUP }],
  118. })
  119. expect(queued).toMatchObject({ ok: true })
  120. const settled = scaffold.whenTurnSettled()
  121. const interrupted = await rpc<{ accepted: true }>(scaffold.baseUrl, 'subagent.interrupt', {
  122. parentSessionId: parentId,
  123. childSessionId: childId,
  124. mode: 'continuable',
  125. })
  126. expect(interrupted).toMatchObject({ ok: true, value: { accepted: true } })
  127. // accepted acknowledges the admitted cancel, not quiescence: wait for the
  128. // aborted turn/end (the composition's first turn/end) before asserting.
  129. expect(await settled).toBe(childId)
  130. // Parked, not resumed: the Activation stays resident with an idle driver,
  131. // the follow-up is retained, and no second turn opened.
  132. const child = scaffold.ctx.agents.get(childId)
  133. expect(child).toBeDefined()
  134. expect(child!.status).toBe('idle')
  135. expect(child!.inbox.nextTurn).toHaveLength(1)
  136. expect(child!.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1)
  137. const lastEnd = child!.session.events.filter(event => event.type === 'turn/end').at(-1)
  138. expect((lastEnd)?.data.reason.kind).toBe('aborted')
  139. // Only an explicit waking send resumes the parked queue, FIFO, then the
  140. // child runs both turns to completion and settles.
  141. const waking = await rpc<{ messageId: string }>(scaffold.baseUrl, 'subagent.prompt', {
  142. parentSessionId: parentId,
  143. childSessionId: childId,
  144. mode: 'continuable',
  145. content: [{ type: 'text', text: WAKING }],
  146. })
  147. expect(waking).toMatchObject({ ok: true })
  148. await expect.poll(() => scaffold.ctx.agents.get(childId), { timeout: 60_000 }).toBeUndefined()
  149. const loaded = await scaffold.ctx.sessionPersistence.load(childId)
  150. // Human-origin messages only: the real composition also injects
  151. // runtime-context snapshots as non-user-source messages.
  152. const userTexts = loaded.events.flatMap(event => event.type === 'user/message'
  153. && event.data.source.kind === 'user'
  154. ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : [])
  155. : [])
  156. expect(userTexts).toEqual([INITIAL, FOLLOWUP, WAKING])
  157. const turnEndKinds = loaded.events
  158. .filter(event => event.type === 'turn/end')
  159. .map(event => (event).data.reason.kind)
  160. expect(turnEndKinds).toEqual(['aborted', 'completed', 'completed'])
  161. }, 120_000)
  162. })