subagent-interrupt.e2e.ts 8.1 KB

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