subagent-spawn.spec.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import LlmService from '@deepseek-ai/dsh-llm'
  5. import SessionStore from '@deepseek-ai/dsh-session'
  6. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  7. import ToolRegistry from '@deepseek-ai/dsh-tools'
  8. import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent'
  9. import { SessionId } from '@deepseek-ai/dsh-session'
  10. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  11. import * as Invariants from '@deepseek-ai/dsh-invariants'
  12. import SubagentService, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  13. import { MockAdapter, maxTokensResponse, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  14. import * as spawn from '../src/index.ts'
  15. import { depthOf, STRUCTURED_OUTPUT_TOOL, SubagentDepthError } from '@deepseek-ai/dsh-subagent-inprocess'
  16. type Script = ConstructorParameters<typeof MockAdapter>[0]
  17. /**
  18. * Drives the REAL spawn backend end-to-end: a real agent loop + a scripted mock
  19. * MODEL (the only mocked boundary) + the real SubagentService + the real
  20. * dsh-invariants plugin (so a malformed child session log would fail the test).
  21. * The parent is a real config agent; the spawn provider creates a real child
  22. * agent on the same context and we assert its output.
  23. */
  24. async function setup(script: Script) {
  25. const ctx = new Context()
  26. const adapter = new MockAdapter(script)
  27. await ctx.plugin(LlmService)
  28. await ctx.plugin(SessionStore)
  29. await ctx.plugin(SystemPrompt)
  30. await ctx.plugin(ToolRegistry)
  31. await ctx.plugin(AgentRegistry)
  32. await ctx.plugin(Invariants)
  33. await ctx.plugin(AgentLoop, { agents: [] })
  34. await ctx.plugin(SubagentService)
  35. await ctx.plugin(spawn, { providerName: 'spawn' })
  36. ctx.llm.registerAdapter(['mock'], adapter)
  37. const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' })
  38. return { ctx, parent, adapter }
  39. }
  40. function text(blocks: { type: string; text?: string }[]): string {
  41. return blocks.filter(b => b.type === 'text').map(b => b.text).join('')
  42. }
  43. function start(ctx: Context, provider: string, request: Omit<SubagentStartRequest, 'signal'> & { signal?: AbortSignal }) {
  44. return ctx.subagents.start(provider, { signal: request.signal ?? new AbortController().signal, ...request })
  45. }
  46. describe('dsh-subagent-spawn', () => {
  47. it('runs a fresh child to completion and returns its final assistant output', async () => {
  48. // One model call for the child: a plain text answer.
  49. const { ctx, parent } = await setup([textResponse('child answer')])
  50. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'do X' }], parent })
  51. const result = await run.result
  52. expect(result.stopReason).toBe('completed')
  53. expect(text(result.output)).toBe('child answer')
  54. await run.dispose()
  55. })
  56. it('emits subagent/start only after the fresh child is published', async () => {
  57. const { ctx, parent } = await setup([textResponse('child answer')])
  58. let childAtStart: ReturnType<typeof ctx.agents.get>
  59. ctx.on('subagent/start', (info) => {
  60. if (info.provider === 'spawn') childAtStart = ctx.agents.get(info.id)
  61. })
  62. const starting = start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'do X' }], parent })
  63. // Creation is asynchronous; no lifecycle claim is made while the child is
  64. // still inside its unpublished setup transaction.
  65. expect(childAtStart).toBeUndefined()
  66. const run = await starting
  67. expect(childAtStart).toBe(ctx.agents.get(run.id))
  68. expect(childAtStart?.id).toBe(run.id)
  69. await run.result
  70. await run.dispose()
  71. })
  72. it('gives the child its OWN session (not the parent\'s), with parentSession lineage', async () => {
  73. const { ctx, parent } = await setup([textResponse('hi')])
  74. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  75. await run.result
  76. const child = ctx.agents.get(run.id)!
  77. expect(child.session.header.id).not.toBe(parent.session.header.id)
  78. expect(child.session.header.parentSession).toBe(parent.session.header.id)
  79. await run.dispose()
  80. })
  81. it('a fresh child does NOT inherit the parent conversation (its log starts empty before the prompt)', async () => {
  82. // Drive the parent through one real turn so it has history, THEN spawn.
  83. const { ctx, parent } = await setup([textResponse('parent turn'), textResponse('child sees nothing')])
  84. parent.send([{ type: 'text', text: 'parent prompt' }])
  85. await parent.whenIdle()
  86. const parentEventCount = parent.session.events.length
  87. expect(parentEventCount).toBeGreaterThan(0)
  88. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'child prompt' }], parent })
  89. await run.result
  90. const child = ctx.agents.get(run.id)!
  91. // The child's first user/message is its OWN prompt, not the parent's history.
  92. const firstUser = child.session.events.find(e => e.type === 'user/message')
  93. expect(firstUser).toBeDefined()
  94. await run.dispose()
  95. })
  96. it('disposes the child to quiescence (agent removed from the registry)', async () => {
  97. const { ctx, parent } = await setup([textResponse('x')])
  98. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  99. await run.result
  100. expect(ctx.agents.get(run.id)).toBeDefined()
  101. await run.dispose()
  102. // After dispose, the child is unregistered (the AgentHandle teardown ran).
  103. expect(ctx.agents.get(run.id)).toBeUndefined()
  104. })
  105. it('stamps child depth = parent depth + 1 (via the merged AgentOptions field)', async () => {
  106. const { ctx, parent } = await setup([textResponse('x')])
  107. expect(depthOf(parent)).toBe(0)
  108. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  109. await run.result
  110. const child = ctx.agents.get(run.id)!
  111. expect(depthOf(child)).toBe(1)
  112. await run.dispose()
  113. })
  114. it('refuses to spawn past maxDepth (depthLimit capability)', async () => {
  115. const { ctx, parent } = await setup([])
  116. // parent is depth 0, child would be depth 1 — cap at 0 forbids any child.
  117. await expect(start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, maxDepth: 0 }))
  118. .rejects.toThrow(SubagentDepthError)
  119. })
  120. it('maps a child that hit its token ceiling to stopReason "max-tokens"', async () => {
  121. const { ctx, parent } = await setup([maxTokensResponse('cut off')])
  122. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  123. const result = await run.result
  124. expect(result.stopReason).toBe('max-tokens')
  125. await run.dispose()
  126. })
  127. it('maps a child whose turn errored (script exhausted) to stopReason "error" with empty output', async () => {
  128. // Empty script: the child's first model call throws "script exhausted", the
  129. // turn ends `error`, and there is no assistant/message → empty output.
  130. const { ctx, parent } = await setup([])
  131. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  132. const result = await run.result
  133. expect(result.stopReason).toBe('error')
  134. expect(result.output).toEqual([])
  135. await run.dispose()
  136. })
  137. it('rejects without publishing when the request signal is already aborted', async () => {
  138. // Regression: a signal aborted BEFORE the run starts never fires an `abort`
  139. // event, so the listener can't catch it. The driver must check the
  140. // already-aborted case up front and settle `aborted` without running the
  141. // child — otherwise an already-cancelled request runs to `completed`. The
  142. // empty script proves the child's model is never called.
  143. const controller = new AbortController()
  144. controller.abort()
  145. const { ctx, parent } = await setup([])
  146. await expect(start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal }))
  147. .rejects.toThrow('aborted before child publication')
  148. })
  149. it('same-tick cancellation rejects start and prevents child publication', async () => {
  150. // Regression: cancellation before publication used to set a flag but let the
  151. // async factory publish a child anyway, so `started` fulfilled and lifecycle
  152. // observers saw an agent for an attempt the caller had already cancelled.
  153. // The empty script also proves no model turn can run.
  154. const { ctx, parent } = await setup([])
  155. const beforeAgents = ctx.agents.list().length
  156. const beforeSessions = ctx.sessions.list().length
  157. const published: string[] = []
  158. ctx.on('session/created', () => void published.push('session/created'))
  159. ctx.on('agent/created', () => void published.push('agent/created'))
  160. ctx.on('agent/session-start', () => void published.push('agent/session-start'))
  161. ctx.on('subagent/start', () => void published.push('subagent/start'))
  162. ctx.on('subagent/end', () => void published.push('subagent/end'))
  163. const controller = new AbortController()
  164. const starting = start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal })
  165. controller.abort('early')
  166. await expect(starting).rejects.toThrow()
  167. await Promise.resolve()
  168. expect(ctx.agents.list()).toHaveLength(beforeAgents)
  169. expect(ctx.sessions.list()).toHaveLength(beforeSessions)
  170. expect(published).toEqual([])
  171. })
  172. it('a cancel from agent/queued maps a no-turn child log to aborted', async () => {
  173. const { ctx, parent } = await setup([])
  174. const controller = new AbortController()
  175. ctx.on('agent/queued', () => { controller.abort('queued-window') })
  176. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal })
  177. const result = await run.result
  178. expect(result).toMatchObject({ stopReason: 'aborted', output: [] })
  179. const child = ctx.agents.get(run.id)!
  180. expect(child.session.events.some(event => event.type === 'turn/end')).toBe(false)
  181. await run.dispose()
  182. })
  183. it('cancelling a running child settles the run as aborted (the abort bridge + cancel())', async () => {
  184. // 'hang' makes the child's model stream one chunk then wait until aborted.
  185. const controller = new AbortController()
  186. const { ctx, parent } = await setup(['hang'])
  187. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent, signal: controller.signal })
  188. // Let the child's turn start, then abort via the request signal (the
  189. // backend bridges it to child.cancel()).
  190. await new Promise(r => setTimeout(r, 30))
  191. controller.abort()
  192. const result = await run.result
  193. expect(result.stopReason).toBe('aborted')
  194. await run.dispose()
  195. })
  196. it('dispose cancels the child and reaches quiescence', async () => {
  197. const { ctx, parent } = await setup(['hang'])
  198. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  199. await new Promise(r => setTimeout(r, 30))
  200. await run.dispose()
  201. const result = await run.result
  202. expect(result.stopReason).toBe('aborted')
  203. })
  204. it('does not expose the optional runtime methods (sendMessage/resume) in this cut', async () => {
  205. const { ctx, parent } = await setup([textResponse('x')])
  206. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent })
  207. expect('sendMessage' in run).toBe(false)
  208. expect('resume' in run).toBe(false)
  209. await run.result
  210. await run.dispose()
  211. })
  212. it('inherits the parent cwd into the child session', async () => {
  213. const { ctx } = await setup([textResponse('x')])
  214. // A parent WITH a cwd (config agents have none, so create one explicitly).
  215. const parentHandle = await ctx.agents.create({
  216. agentId: AgentId('cwd-parent'),
  217. sessionId: SessionId('cwd-parent-session'),
  218. meta: { cwd: '/tmp/parent-workspace' },
  219. agentOptions: { model: 'mock' },
  220. })
  221. const run = await start(ctx, 'spawn', { prompt: [{ type: 'text', text: 'p' }], parent: parentHandle.agent })
  222. await run.result
  223. const child = ctx.agents.get(run.id)!
  224. expect(child.session.header.cwd).toBe('/tmp/parent-workspace')
  225. await run.dispose()
  226. await parentHandle.dispose()
  227. })
  228. it('uses request.agentOptions.model when the parent has no model of its own', async () => {
  229. const { ctx } = await setup([textResponse('explicit model child')])
  230. // A parent with NO model (its own turns would need one supplied per-request).
  231. const parentHandle = await ctx.agents.create({
  232. agentId: AgentId('modelless-parent'),
  233. sessionId: SessionId('modelless-parent-session'),
  234. agentOptions: {},
  235. })
  236. // The request supplies the child's model explicitly.
  237. const run = await start(ctx, 'spawn', {
  238. prompt: [{ type: 'text', text: 'p' }],
  239. parent: parentHandle.agent,
  240. agentOptions: { model: 'mock' },
  241. })
  242. const result = await run.result
  243. expect(result.stopReason).toBe('completed')
  244. expect(text(result.output)).toBe('explicit model child')
  245. await run.dispose()
  246. await parentHandle.dispose()
  247. })
  248. it('advertises every start-time capability (depthLimit, outputSchema, toolFilter, persona)', async () => {
  249. const { ctx } = await setup([])
  250. const provider = ctx.subagents.getProvider('spawn')!
  251. expect(provider.capabilities).toEqual({ outputSchema: true, depthLimit: true, toolFilter: true, persona: true })
  252. })
  253. it('unregisters the provider when its fiber is disposed (HMR safety)', async () => {
  254. const ctx = new Context()
  255. await ctx.plugin(SubagentService)
  256. await ctx.plugin(AgentRegistry)
  257. const fiber = await ctx.plugin(spawn, { providerName: 'spawn' })
  258. expect(ctx.subagents.list()).toEqual(['spawn'])
  259. await fiber.dispose()
  260. expect(ctx.subagents.list()).toEqual([])
  261. })
  262. it('captures structured output through the shipped plugin (driver runtime, plugin wiring)', async () => {
  263. const { ctx, parent } = await setup([
  264. toolCallResponse('c1', STRUCTURED_OUTPUT_TOOL, { answer: 42 }),
  265. ])
  266. const run = await start(ctx, 'spawn', {
  267. prompt: [{ type: 'text', text: 'produce the answer' }],
  268. parent,
  269. outputSchema: { type: 'object', properties: { answer: { type: 'number' } }, required: ['answer'] },
  270. })
  271. const result = await run.result
  272. expect(result.stopReason).toBe('completed')
  273. expect(result.structured).toEqual({ answer: 42 })
  274. // Run-scoped runtime: the settle released the last acquisition.
  275. expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined()
  276. await run.dispose()
  277. })
  278. it('a backend unload does not revoke an accepted holder-owned run', async () => {
  279. // Rebuild the stack by hand so we hold the backend's fiber.
  280. const ctx = new Context()
  281. const adapter = new MockAdapter(['hang'])
  282. await ctx.plugin(LlmService)
  283. await ctx.plugin(SessionStore)
  284. await ctx.plugin(SystemPrompt)
  285. await ctx.plugin(ToolRegistry)
  286. await ctx.plugin(AgentRegistry)
  287. await ctx.plugin(Invariants)
  288. await ctx.plugin(AgentLoop, { agents: [] })
  289. await ctx.plugin(SubagentService)
  290. const fiber = await ctx.plugin(spawn, { providerName: 'spawn' })
  291. ctx.llm.registerAdapter(['mock'], adapter)
  292. const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' })
  293. const controller = new AbortController()
  294. const run = await start(ctx, 'spawn', {
  295. prompt: [{ type: 'text', text: 'q' }],
  296. parent,
  297. signal: controller.signal,
  298. outputSchema: { type: 'object', properties: { a: { type: 'number' } } },
  299. })
  300. // Provider removal prevents new starts but the returned run belongs to its
  301. // holder and remains live.
  302. await new Promise(resolve => setTimeout(resolve, 30))
  303. await fiber.dispose()
  304. expect(ctx.subagents.getProvider('spawn')).toBeUndefined()
  305. expect(ctx.agents.get(run.id)).toBeDefined()
  306. controller.abort('test complete')
  307. const result = await run.result
  308. expect(result.stopReason).toBe('aborted')
  309. expect(ctx.tools.get(STRUCTURED_OUTPUT_TOOL)).toBeUndefined()
  310. await run.dispose()
  311. })
  312. it('a start racing an already-unloading backend cannot begin child creation', async () => {
  313. const ctx = new Context()
  314. await ctx.plugin(LlmService)
  315. await ctx.plugin(SessionStore)
  316. await ctx.plugin(SystemPrompt)
  317. await ctx.plugin(ToolRegistry)
  318. await ctx.plugin(AgentRegistry)
  319. await ctx.plugin(AgentLoop, { agents: [] })
  320. await ctx.plugin(SubagentService)
  321. const fiber = await ctx.plugin(spawn, { providerName: 'spawn' })
  322. const parent = ctx.agentLoop.create(AgentId('parent'), { model: 'mock' })
  323. const parentEffects = parent.ctx.fiber.getEffects().length
  324. const published: string[] = []
  325. ctx.on('session/created', () => void published.push('session/created'))
  326. ctx.on('agent/created', () => void published.push('agent/created'))
  327. const unloading = fiber.dispose()
  328. await unloading
  329. await expect(start(ctx, 'spawn', {
  330. prompt: [{ type: 'text', text: 'must never start' }], parent,
  331. })).rejects.toThrow(/no subagent provider/)
  332. expect(parent.ctx.fiber.getEffects()).toHaveLength(parentEffects)
  333. expect(published).toEqual([])
  334. })
  335. it('has the namespace-plugin export shape (no stray default)', () => {
  336. expect('default' in spawn).toBe(false)
  337. expect(spawn.name).toBe('subagent-spawn')
  338. expect(spawn.inject).toEqual(['subagents'])
  339. const loader = Object.create(Loader.prototype) as Loader
  340. const unwrapped = loader.unwrapExports(spawn) as Record<string, unknown>
  341. expect(unwrapped).toBe(spawn)
  342. expect(unwrapped.name).toBe('subagent-spawn')
  343. expect(unwrapped.inject).toEqual(['subagents'])
  344. expect(typeof unwrapped.apply).toBe('function')
  345. })
  346. describe('persona and toolFilter (the scoped child world)', () => {
  347. it('a per-child persona shadows the deployment persona in the child request only', async () => {
  348. const { ctx, parent, adapter } = await setup([
  349. textResponse('parent answer'),
  350. textResponse('child answer'),
  351. ])
  352. parent.send([{ type: 'text', text: 'hi' }])
  353. await parent.whenIdle()
  354. const run = await start(ctx, 'spawn', {
  355. prompt: [{ type: 'text', text: 'do X' }],
  356. parent,
  357. persona: 'You are the tersest test runner.',
  358. })
  359. await run.result
  360. const childRequest = adapter.requests.at(-1)!
  361. expect(childRequest.system).toContain('You are the tersest test runner.')
  362. // The parent's earlier request carried no such persona.
  363. expect(adapter.requests[0]!.system ?? '').not.toContain('tersest test runner')
  364. await run.dispose()
  365. })
  366. it('toolFilter hides denied tools from the child prompt AND refuses their execution', async () => {
  367. const { ctx, parent, adapter } = await setup([
  368. // The child tries the denied tool anyway, then answers.
  369. toolCallResponse('c1', 'forbidden_tool', {}),
  370. textResponse('done'),
  371. ])
  372. ctx.tools.register({
  373. name: 'forbidden_tool', description: 'global', parameters: {},
  374. execute: () => Promise.resolve([{ type: 'text', text: 'ran' }]),
  375. })
  376. const run = await start(ctx, 'spawn', {
  377. prompt: [{ type: 'text', text: 'do X' }],
  378. parent,
  379. toolFilter: { deny: ['forbidden_tool'] },
  380. })
  381. const result = await run.result
  382. expect(result.stopReason).toBe('completed')
  383. // Not advertised…
  384. const childRequest = adapter.requests[0]!
  385. expect((childRequest.tools ?? []).map(t => t.name)).not.toContain('forbidden_tool')
  386. // …and the attempted call executed as UNKNOWN_TOOL (visible in the log).
  387. const child = ctx.agents.get(run.id)!
  388. const toolResult = child.session.events.find(e => e.type === 'tool/result')!
  389. expect(JSON.stringify(toolResult.data)).toContain('unknown tool')
  390. await run.dispose()
  391. })
  392. it('an unknown toolFilter name fails the spawn loudly with no orphaned child', async () => {
  393. const { ctx, parent } = await setup([])
  394. const before = ctx.agents.list().length
  395. await expect(start(ctx, 'spawn', {
  396. prompt: [{ type: 'text', text: 'do X' }],
  397. parent,
  398. toolFilter: { deny: ['no_such_tool'] },
  399. })).rejects.toThrow(/unknown global tool "no_such_tool"/)
  400. expect(ctx.agents.list().length).toBe(before)
  401. })
  402. })
  403. it('spawning from a DISPOSING parent fails loud with no orphaned child (INACTIVE_EFFECT teaching error)', async () => {
  404. const { ctx } = await setup([])
  405. // A handle-owned parent we can dispose (config agents dispose with the loop fiber).
  406. const parentHandle = await ctx.agents.create({
  407. agentId: AgentId('doomed-parent'),
  408. sessionId: SessionId('doomed-s'),
  409. agentOptions: { model: 'mock' },
  410. })
  411. await parentHandle.dispose()
  412. const before = ctx.agents.list().length
  413. const sessionsBefore = ctx.sessions.list().length
  414. const published: string[] = []
  415. ctx.on('session/created', () => void published.push('session/created'))
  416. ctx.on('agent/created', () => void published.push('agent/created'))
  417. ctx.on('agent/session-start', () => void published.push('agent/session-start'))
  418. await expect(start(ctx, 'spawn', {
  419. prompt: [{ type: 'text', text: 'do X' }],
  420. parent: parentHandle.agent,
  421. })).rejects.toThrow(/inactive context/)
  422. expect(ctx.agents.list().length).toBe(before)
  423. expect(ctx.sessions.list()).toHaveLength(sessionsBefore)
  424. expect(published).toEqual([])
  425. })
  426. it('parent disposal during the child setup transaction prevents every publication notification', async () => {
  427. const { ctx } = await setup([])
  428. const parentHandle = await ctx.agents.create({
  429. agentId: AgentId('setup-race-parent'),
  430. sessionId: SessionId('setup-race-parent-session'),
  431. agentOptions: { model: 'mock' },
  432. })
  433. const published: string[] = []
  434. ctx.on('session/created', () => void published.push('session/created'))
  435. ctx.on('agent/created', () => void published.push('agent/created'))
  436. ctx.on('agent/session-start', () => void published.push('agent/session-start'))
  437. const starting = start(ctx, 'spawn', {
  438. prompt: [{ type: 'text', text: 'must never run' }],
  439. parent: parentHandle.agent,
  440. })
  441. // The factory has entered its awaited unpublished setup transaction. The
  442. // parent context owns that transaction, so disposal wins without an
  443. // observer ever seeing the child.
  444. await parentHandle.dispose()
  445. await expect(starting).rejects.toThrow(/owner disposed during setup|inactive context/)
  446. expect(published).toEqual([])
  447. })
  448. })