workflow-worker-thread.spec.ts 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { tmpdir } from 'node:os'
  3. import { fileURLToPath } from 'node:url'
  4. import type { Worker } from 'node:worker_threads'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import Loader from '@deepseek-ai/cordis-plugin-loader'
  7. import type { Agent } from '@deepseek-ai/dsh-agent'
  8. import SubagentRuntime from '@deepseek-ai/dsh-subagent'
  9. import type { SubagentCapabilities, SubagentProvider, SubagentResult, SubagentRun, SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  10. import type { WorkflowMeta, WorkflowResult, WorkflowResultInfo, WorkflowRun, WorkflowRunInfo } from '@deepseek-ai/dsh-workflow'
  11. import * as workerEngineModule from '../src/index.ts'
  12. import WorkerThreadWorkflowEngine, { type Config } from '../src/index.ts'
  13. import { workerSpawnEnv } from '../src/host.ts'
  14. import { HostToWorkerType, WorkerToHostType } from '../src/protocol.ts'
  15. import { SessionId } from '@deepseek-ai/dsh-session'
  16. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  17. /** A minimal parent stand-in: the engine only threads it through to the provider. */
  18. function fakeParent(): Agent {
  19. return { id: SessionId('workflow-parent'), options: {} } as unknown as Agent
  20. }
  21. // Allow cold worker startup on contended CI runners.
  22. vi.setConfig({ testTimeout: 30_000 })
  23. /**
  24. * Wait up to 60 seconds for CPU-bound worker startup or cross-thread delivery on contended CI:
  25. * startup is the only environment-sensitive phase of a same-process worker exchange, and the
  26. * loaded self-hosted Windows pool stretches the tsx-in-worker boot past 10 seconds. Host
  27. * reactions after an observed event use explicit tight overrides, so this generous startup
  28. * allowance cannot hide multi-second reap regressions.
  29. */
  30. function waitFor(assertion: () => void, timeout = 60_000): Promise<void> {
  31. return vi.waitFor(assertion, { timeout, interval: 50 })
  32. }
  33. /** The vm-context escape hatch, spelled once: real Worker tests use it to make the WORKER misbehave. */
  34. const ESCAPE = "globalThis.constructor.constructor('return process')()"
  35. /** One controllable child run: the test (or auto mode) settles it. */
  36. interface ControlledRun {
  37. request: SubagentStartRequest
  38. /** Fulfill the provider's async start with a published child. */
  39. publish(): void
  40. /** Reject the provider's async start before ownership transfer. */
  41. rejectStart(error: unknown): void
  42. settle(result: SubagentResult): void
  43. rejectResult(error: unknown): void
  44. cancelled: string | undefined
  45. disposed: boolean
  46. disposeCalls: number
  47. }
  48. /**
  49. * A scripted in-test provider over the REAL SubagentRuntime registry: `auto`
  50. * settles each run via the reply function on a microtask; `manual` piles runs
  51. * up in `runs` for the test to settle. A run aborts (settles `aborted`) when
  52. * the request signal fires, like the real in-process backends.
  53. */
  54. class StubProvider implements SubagentProvider {
  55. readonly capabilities: SubagentCapabilities = {
  56. agentOptions: true,
  57. outputSchema: true,
  58. depthLimit: true,
  59. toolFilter: true,
  60. persona: false,
  61. }
  62. readonly inheritsParentContext = false
  63. readonly runs: ControlledRun[] = []
  64. constructor(
  65. readonly name: string,
  66. private readonly reply?: (request: SubagentStartRequest, index: number) => SubagentResult,
  67. private readonly disposeDelayMs = 0,
  68. private readonly deferStart = false,
  69. private readonly onAbortString?: (reason: string | undefined, index: number) => void,
  70. private readonly onSignalAbort?: (reason: unknown, index: number) => void,
  71. ) {}
  72. async start(request: SubagentStartRequest): Promise<SubagentRun> {
  73. const startGate = Promise.withResolvers<undefined>()
  74. const terminal = Promise.withResolvers<SubagentResult>()
  75. terminal.promise.catch(() => { /* provider owns early settlement until publication */ })
  76. let published = false
  77. const controlled: ControlledRun = {
  78. request,
  79. publish: () => { published = true; startGate.resolve(undefined) },
  80. rejectStart: (error) => { startGate.reject(error) },
  81. settle: (result) => { terminal.resolve(result) },
  82. rejectResult: (error) => { terminal.reject(error) },
  83. cancelled: undefined,
  84. disposed: false,
  85. disposeCalls: 0,
  86. }
  87. this.runs.push(controlled)
  88. const index = this.runs.length - 1
  89. request.signal.addEventListener('abort', () => {
  90. controlled.cancelled = String(request.signal.reason ?? 'cancelled')
  91. this.onAbortString?.(String(request.signal.reason ?? 'cancelled'), index)
  92. this.onSignalAbort?.(request.signal.reason, index)
  93. if (published) terminal.resolve({ output: [], stopReason: 'aborted' })
  94. else startGate.reject(new Error('child start aborted before publication'))
  95. }, { once: true })
  96. if (!this.deferStart) controlled.publish()
  97. if (this.reply) {
  98. const reply = this.reply
  99. queueMicrotask(() => { terminal.resolve(reply(request, index)) })
  100. }
  101. try {
  102. await startGate.promise
  103. } catch (error: unknown) {
  104. controlled.disposeCalls += 1
  105. controlled.disposed = true
  106. throw error
  107. }
  108. if (request.signal.aborted) throw new Error('child start aborted before publication')
  109. return {
  110. id: SessionId(`stub-child-${index}`),
  111. localAgent: undefined,
  112. result: terminal.promise,
  113. dispose: () => {
  114. controlled.disposeCalls += 1
  115. if (this.disposeDelayMs === 0) {
  116. controlled.disposed = true
  117. return Promise.resolve()
  118. }
  119. return new Promise<void>((resolve) => {
  120. setTimeout(() => {
  121. controlled.disposed = true
  122. resolve()
  123. }, this.disposeDelayMs)
  124. })
  125. },
  126. }
  127. }
  128. }
  129. /** Text-reply helper for auto providers. */
  130. function text(reply: string): SubagentResult {
  131. return { output: [{ type: 'text', text: reply }], stopReason: 'completed' }
  132. }
  133. interface SetupOptions {
  134. config?: Config
  135. reply?: (request: SubagentStartRequest, index: number) => SubagentResult
  136. manual?: boolean
  137. disposeDelayMs?: number
  138. deferStart?: boolean
  139. onChildAbortString?: (reason: string | undefined, index: number) => void
  140. onChildSignalAbort?: (reason: unknown, index: number) => void
  141. }
  142. async function setup(options?: SetupOptions) {
  143. const ctx = new Context()
  144. await ctx.plugin(SessionProjectionRegistry)
  145. await ctx.plugin(SubagentRuntime)
  146. const provider = new StubProvider(
  147. 'stub',
  148. options?.manual ? undefined : options?.reply ?? (() => text('stub reply')),
  149. options?.disposeDelayMs ?? 0,
  150. options?.deferStart ?? false,
  151. options?.onChildAbortString,
  152. options?.onChildSignalAbort,
  153. )
  154. ctx.subagents.registerProvider(provider)
  155. // A fixed concurrency ceiling: the auto-resolved default is machine-derived
  156. // (cores - 2, floored at 1), so tests that expect N children in flight
  157. // would wedge on small CI runners.
  158. const engineFiber = await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'stub', maxConcurrentAgents: 8, ...options?.config })
  159. return { ctx, provider, parent: fakeParent(), engineFiber }
  160. }
  161. /** The standard test meta plus a body, spread into a start request. */
  162. function scripted(body: string, metaExtra?: Partial<WorkflowMeta>): { script: string; meta: WorkflowMeta } {
  163. return { script: body, meta: { name: 'test-flow', description: 'a test workflow', ...metaExtra } }
  164. }
  165. /** Start + await one run, disposing on the way out. */
  166. async function run(ctx: Context, parent: Agent, source: { script: string; meta: WorkflowMeta }, args?: unknown): Promise<WorkflowResult> {
  167. const handle = ctx.workflowEngine.start({ ...source, parent, ...args !== undefined ? { args } : {} })
  168. try {
  169. return await handle.result
  170. } finally {
  171. await handle.dispose()
  172. }
  173. }
  174. // The per-test cap leaves room for one generous startup wait plus the tight
  175. // post-event assertions; explicit narrower timeouts inside stay authoritative.
  176. describe('dsh-workflow-worker-thread', { timeout: 120_000 }, () => {
  177. describe('script execution over a real worker thread', () => {
  178. it('runs a script end-to-end: agent() text results, phases, log, args, return value, events', async () => {
  179. const { ctx, parent, provider } = await setup({ reply: (_request, index) => text(`answer-${index}`) })
  180. const events: [string, unknown[]][] = []
  181. for (const name of ['workflow/start', 'workflow/phase', 'workflow/log', 'workflow/agent-start', 'workflow/agent-end', 'workflow/end'] as const) {
  182. ctx.on(name, (...payload: unknown[]) => { events.push([name, payload]) })
  183. }
  184. const result = await run(ctx, parent, scripted(`
  185. phase('Scan')
  186. log('starting with ' + args.files.length + ' files')
  187. const answers = await pipeline(args.files, (prev, item) => agent('read ' + item))
  188. phase('Report')
  189. return { answers, count: args.files.length }
  190. `, { phases: [{ title: 'Scan' }, { title: 'Report' }] }), { files: ['a.ts', 'b.ts'] })
  191. expect(result.stopReason).toBe('completed')
  192. expect(result.agentsStarted).toBe(2)
  193. expect(result.value).toEqual({ answers: ['answer-0', 'answer-1'], count: 2 })
  194. expect(provider.runs.every(r => r.disposed)).toBe(true)
  195. const names = events.map(([name]) => name)
  196. expect(names[0]).toBe('workflow/start')
  197. expect(names).toContain('workflow/phase')
  198. expect(names).toContain('workflow/log')
  199. expect(names.at(-1)).toBe('workflow/end')
  200. const info = events[0]![1][0] as WorkflowRunInfo
  201. expect(info.meta.name).toBe('test-flow')
  202. const end = events.at(-1)![1][1] as Record<string, unknown>
  203. expect(end).toEqual({ stopReason: 'completed', agentsStarted: 2 })
  204. expect('value' in end).toBe(false)
  205. })
  206. it('agent({schema, model}) forwards outputSchema and agentOptions to the provider across the thread', async () => {
  207. const { ctx, parent, provider } = await setup({
  208. reply: () => ({ output: [], structured: { files: ['x.ts', 'y.ts'] }, stopReason: 'completed' }),
  209. })
  210. const result = await run(ctx, parent, scripted(`
  211. const found = await agent('list files', { model: 'deepseek-v4-pro', schema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' } } }, required: ['files'] } })
  212. return { first: found.files[0], count: found.files.length }
  213. `))
  214. expect(result.value).toEqual({ first: 'x.ts', count: 2 })
  215. expect(provider.runs[0]!.request.outputSchema).toEqual({
  216. type: 'object',
  217. properties: { files: { type: 'array', items: { type: 'string' } } },
  218. required: ['files'],
  219. })
  220. expect(provider.runs[0]!.request.agentOptions).toEqual({ model: 'deepseek-v4-pro' })
  221. expect(provider.runs[0]!.request.parent).toBeDefined()
  222. })
  223. it('agent({provider}) forwards provider-only agentOptions across the thread', async () => {
  224. const { ctx, parent, provider } = await setup()
  225. const result = await run(ctx, parent, scripted("return await agent('route me', { provider: 'openai' })"))
  226. expect(result.value).toBe('stub reply')
  227. expect(provider.runs[0]!.request.agentOptions).toEqual({ provider: 'openai' })
  228. })
  229. it('a start-request provider override selects every child without changing the engine default', async () => {
  230. const { ctx, parent, provider } = await setup()
  231. const selected = new StubProvider('selected', () => text('selected reply'))
  232. ctx.subagents.registerProvider(selected)
  233. const overridden = ctx.workflowEngine.start({
  234. ...scripted("return await agent('route this run')"),
  235. parent,
  236. subagentProvider: 'selected',
  237. })
  238. expect((await overridden.result).value).toBe('selected reply')
  239. await overridden.dispose()
  240. expect(selected.runs).toHaveLength(1)
  241. expect(provider.runs).toHaveLength(0)
  242. const ordinary = await run(ctx, parent, scripted("return await agent('use the default')"))
  243. expect(ordinary.value).toBe('stub reply')
  244. expect(provider.runs).toHaveLength(1)
  245. })
  246. it('rejects invalid start-request provider routes before publishing a run', async () => {
  247. const { ctx, parent } = await setup()
  248. let starts = 0
  249. ctx.on('workflow/start', () => { starts += 1 })
  250. const messages: string[] = []
  251. for (const subagentProvider of ['', 'missing']) {
  252. let run: WorkflowRun | undefined
  253. let thrown: unknown
  254. try {
  255. run = ctx.workflowEngine.start({
  256. ...scripted("return 'must not start'"),
  257. parent,
  258. subagentProvider,
  259. })
  260. } catch (error: unknown) {
  261. thrown = error
  262. }
  263. await run?.dispose()
  264. messages.push(thrown instanceof Error ? thrown.message : '')
  265. }
  266. expect(messages).toEqual([
  267. 'workflow subagentProvider must be a non-empty normalized string',
  268. 'no subagent provider registered for "missing"',
  269. ])
  270. expect(starts).toBe(0)
  271. })
  272. it('rejects invalid per-run total-agent caps before publishing a run', async () => {
  273. const { ctx, parent } = await setup({ config: { maxTotalAgents: 2 } })
  274. let starts = 0
  275. ctx.on('workflow/start', () => { starts += 1 })
  276. const errors: unknown[] = []
  277. for (const maxTotalAgents of [0, 1.5, Number.NaN, 3]) {
  278. try {
  279. const handle = ctx.workflowEngine.start({
  280. ...scripted("return 'must not start'"),
  281. parent,
  282. maxTotalAgents,
  283. })
  284. await handle.dispose()
  285. } catch (error: unknown) {
  286. errors.push(error)
  287. }
  288. }
  289. expect(errors.slice(0, 3)).toEqual(Array(3).fill(expect.objectContaining({
  290. code: 'INVALID_ARGUMENT',
  291. message: 'workflow maxTotalAgents must be a positive safe integer',
  292. })))
  293. expect(errors[3]).toMatchObject({
  294. code: 'INVALID_ARGUMENT',
  295. message: 'workflow maxTotalAgents 3 exceeds the engine ceiling 2',
  296. })
  297. expect(starts).toBe(0)
  298. })
  299. it('enforces a per-run total-agent cap below the engine ceiling', async () => {
  300. const { ctx, parent } = await setup({ config: { maxTotalAgents: 2 } })
  301. const handle = ctx.workflowEngine.start({
  302. ...scripted("await agent('first'); await agent('second'); return 'unreachable'"),
  303. parent,
  304. maxTotalAgents: 1,
  305. })
  306. const result = await handle.result
  307. expect(result.stopReason).toBe('error')
  308. expect(result.agentsStarted).toBe(1)
  309. expect(result.error).toContain('total agent cap (1)')
  310. await handle.dispose()
  311. })
  312. it('a fatal hook error inside the worker kills the script and reports the error', async () => {
  313. const { ctx, parent } = await setup()
  314. const result = await run(ctx, parent, scripted("return await parallel([() => agent('x', { isolation: 'worktree' })])"))
  315. expect(result.stopReason).toBe('error')
  316. expect(result.error).toContain('"isolation" is deferred')
  317. })
  318. it('rejects an unregistered configured provider before publishing a run', async () => {
  319. const { ctx, parent } = await setup({ config: { provider: 'nonexistent' } })
  320. let thrown: unknown
  321. try {
  322. ctx.workflowEngine.start({ ...scripted("return 'must not start'"), parent })
  323. } catch (error: unknown) {
  324. thrown = error
  325. }
  326. expect(thrown).toMatchObject({
  327. code: 'AGENT_START',
  328. message: 'no subagent provider registered for "nonexistent"',
  329. })
  330. })
  331. it('waits for async provider start before announcing a result that settled early', async () => {
  332. const { ctx, parent, provider } = await setup({ manual: true, deferStart: true })
  333. const order: string[] = []
  334. ctx.on('workflow/agent-start', (_info, agent) => { order.push(`start:${agent.seq}`) })
  335. ctx.on('workflow/agent-end', (_info, agent) => { order.push(`end:${agent.outcome}`) })
  336. ctx.on('workflow/end', () => { order.push('run-end') })
  337. const handle = ctx.workflowEngine.start({ ...scripted("return await agent('p')"), parent })
  338. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  339. const early = text('accepted value')
  340. provider.runs[0]!.settle(early)
  341. // The provider still owns this early result while start is pending.
  342. await new Promise(resolve => setTimeout(resolve, 0))
  343. expect(order).toEqual([])
  344. provider.runs[0]!.publish()
  345. const result = await handle.result
  346. expect(result.value).toBe('accepted value')
  347. expect(order).toEqual(['start:1', 'end:completed', 'run-end'])
  348. await handle.dispose()
  349. expect(provider.runs[0]!.disposeCalls).toBe(1)
  350. })
  351. it('observes an early result rejection but sends ChildStarted before ChildFailed after start fulfills', async () => {
  352. const { ctx, parent, provider } = await setup({ manual: true, deferStart: true })
  353. const lifecycle: string[] = []
  354. ctx.on('workflow/agent-start', () => { lifecycle.push('start') })
  355. ctx.on('workflow/agent-end', (_info, agent) => { lifecycle.push(`end:${agent.outcome}`) })
  356. const handle = ctx.workflowEngine.start({
  357. ...scripted("try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }"),
  358. parent,
  359. })
  360. const worker = (handle as unknown as { worker: { postMessage(message: unknown): void } }).worker
  361. const post = vi.spyOn(worker, 'postMessage')
  362. const childMessageTypes = (): HostToWorkerType[] => post.mock.calls
  363. .map(([message]) => (message as { type: HostToWorkerType }).type)
  364. .filter(type => type === HostToWorkerType.ChildStarted || type === HostToWorkerType.ChildFailed)
  365. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  366. provider.runs[0]!.rejectResult(new Error('backend failed before publication'))
  367. await new Promise(resolve => setTimeout(resolve, 0))
  368. expect(childMessageTypes()).toEqual([])
  369. expect(lifecycle).toEqual([])
  370. provider.runs[0]!.publish()
  371. const result = await handle.result
  372. expect(result.value).toMatchObject({ code: 'AGENT_RESULT' })
  373. expect((result.value as { message: string }).message).toContain('backend failed before publication')
  374. expect(childMessageTypes()).toEqual([HostToWorkerType.ChildStarted, HostToWorkerType.ChildFailed])
  375. expect(lifecycle).toEqual(['start', 'end:failed'])
  376. post.mockRestore()
  377. await handle.dispose()
  378. })
  379. it('classifies provider start rejection as AGENT_START, drops an early result, and emits no false lifecycle pair', async () => {
  380. const { ctx, parent, provider } = await setup({ manual: true, deferStart: true })
  381. const lifecycle: string[] = []
  382. ctx.on('workflow/agent-start', () => { lifecycle.push('start') })
  383. ctx.on('workflow/agent-end', () => { lifecycle.push('end') })
  384. const handle = ctx.workflowEngine.start({
  385. ...scripted("try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }"),
  386. parent,
  387. })
  388. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  389. // ACP-style failure can settle result(error) before its session/publication
  390. // boundary rejects. Start rejection must dominate that buffered child outcome.
  391. provider.runs[0]!.settle({ output: [], stopReason: 'error' })
  392. await new Promise(resolve => setTimeout(resolve, 0))
  393. provider.runs[0]!.rejectStart(new Error('publication rolled back'))
  394. const result = await handle.result
  395. expect(result.value).toMatchObject({ code: 'AGENT_START' })
  396. expect((result.value as { message: string }).message).toContain('publication rolled back')
  397. expect(lifecycle).toEqual([])
  398. await waitFor(() => {
  399. expect(provider.runs[0]!.disposed).toBe(true)
  400. expect(provider.runs[0]!.disposeCalls).toBe(1)
  401. })
  402. await handle.dispose()
  403. expect(provider.runs[0]!.disposeCalls).toBe(1)
  404. })
  405. it('aborts a pending provider start once without publishing workflow lifecycle', async () => {
  406. const { ctx, parent, provider } = await setup({ manual: true, deferStart: true, config: { disposeGraceMs: 500 } })
  407. const lifecycle: string[] = []
  408. ctx.on('workflow/agent-start', () => { lifecycle.push('start') })
  409. ctx.on('workflow/agent-end', () => { lifecycle.push('end') })
  410. const handle = ctx.workflowEngine.start({ ...scripted("return await agent('pending')"), parent })
  411. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  412. const disposal = handle.dispose()
  413. await waitFor(() => {
  414. expect(provider.runs[0]!.cancelled).toBe('workflow disposed')
  415. expect(provider.runs[0]!.disposed).toBe(true)
  416. })
  417. // Ensure the host-driven disposal removed the registry entry before the
  418. // late start rejection; its callback must not invoke dispose again.
  419. await new Promise(resolve => setTimeout(resolve, 0))
  420. provider.runs[0]!.rejectStart(new Error('cancelled before publication'))
  421. const result = await handle.result
  422. await disposal
  423. expect(result.stopReason).toBe('cancelled')
  424. expect(lifecycle).toEqual([])
  425. expect(provider.runs[0]!.disposeCalls).toBe(1)
  426. })
  427. it('a child result REJECTION crosses back as a fatal AGENT_RESULT error (a broken provider is not a failed child)', async () => {
  428. const ctx = new Context()
  429. await ctx.plugin(SessionProjectionRegistry)
  430. await ctx.plugin(SubagentRuntime)
  431. const provider: SubagentProvider = {
  432. name: 'rejecting',
  433. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  434. inheritsParentContext: false,
  435. start: async () => ({
  436. id: SessionId('reject-child'),
  437. localAgent: undefined,
  438. result: Promise.reject(new Error('backend exploded')),
  439. dispose: () => Promise.resolve(),
  440. }),
  441. }
  442. ctx.subagents.registerProvider(provider)
  443. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'rejecting', maxConcurrentAgents: 2 })
  444. const result = await run(ctx, fakeParent(), scripted(`
  445. try { await agent('p'); return 'unreachable' } catch (e) { return { name: e.name, code: e.code, fatal: e.fatal, message: e.message } }
  446. `))
  447. expect(result.value).toMatchObject({ name: 'WorkflowError', code: 'AGENT_RESULT', fatal: true })
  448. expect((result.value as { message: string }).message).toContain('backend exploded')
  449. })
  450. it('maps a non-JSON ready-child result to fatal AGENT_RESULT instead of wedging the bridge', async () => {
  451. const { ctx, parent } = await setup({
  452. reply: () => ({ output: [], structured: () => { /* deliberately outside lossless JSON */ }, stopReason: 'completed' }),
  453. })
  454. const result = await run(ctx, parent, scripted(`
  455. try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }
  456. `))
  457. expect(result.value).toMatchObject({ code: 'AGENT_RESULT' })
  458. expect((result.value as { message: string }).message).toContain('workflow child result could not cross the worker boundary')
  459. })
  460. it('contains a non-JSON result even if the injected subagent service violates its normalization contract', async () => {
  461. // The real worker boundary must reject a non-JSON same-process result.
  462. const { ctx, parent } = await setup()
  463. const invalid = {
  464. output: [],
  465. structured: () => { /* deliberately outside lossless JSON */ },
  466. stopReason: 'completed',
  467. } as unknown as SubagentResult
  468. const start = vi.spyOn(ctx.subagents, 'start').mockResolvedValue({
  469. id: SessionId('raw-invalid-child'),
  470. localAgent: undefined,
  471. result: Promise.resolve(invalid),
  472. dispose: () => Promise.resolve(),
  473. })
  474. const result = await run(ctx, parent, scripted(`
  475. try { await agent('p'); return 'unreachable' } catch (e) { return { code: e.code, message: e.message } }
  476. `))
  477. expect(start).toHaveBeenCalledOnce()
  478. expect(result.value).toMatchObject({ code: 'AGENT_RESULT' })
  479. expect((result.value as { message: string }).message)
  480. .toContain('workflow child result could not cross the worker boundary')
  481. })
  482. it('a child whose dispose() throws synchronously cannot wedge the script (the host acks anyway)', async () => {
  483. const ctx = new Context()
  484. await ctx.plugin(SessionProjectionRegistry)
  485. await ctx.plugin(SubagentRuntime)
  486. const provider: SubagentProvider = {
  487. name: 'bad-dispose',
  488. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  489. inheritsParentContext: false,
  490. start: async () => ({
  491. id: SessionId('bad-dispose-child'),
  492. localAgent: undefined,
  493. result: Promise.resolve({ output: [{ type: 'text', text: 'fine' }], stopReason: 'completed' }),
  494. cancel: () => { /* settled already */ },
  495. dispose: () => { throw new Error('dispose exploded') },
  496. }),
  497. }
  498. ctx.subagents.registerProvider(provider)
  499. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'bad-dispose', maxConcurrentAgents: 2 })
  500. const result = await run(ctx, fakeParent(), scripted("return await agent('p')"))
  501. expect(result.stopReason).toBe('completed')
  502. expect(result.value).toBe('fine')
  503. })
  504. it('a child dispose() rejecting an UNRENDERABLE value still acks — the containment warn is total', async () => {
  505. const ctx = new Context()
  506. await ctx.plugin(SessionProjectionRegistry)
  507. await ctx.plugin(SubagentRuntime)
  508. const provider: SubagentProvider = {
  509. name: 'coercion-trap-dispose',
  510. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  511. inheritsParentContext: false,
  512. start: async () => ({
  513. id: SessionId('trap-child'),
  514. localAgent: undefined,
  515. result: Promise.resolve({ output: [{ type: 'text', text: 'fine' }], stopReason: 'completed' }),
  516. cancel: () => { /* settled already */ },
  517. // The rejection VALUE's own coercion throws: a warn built with bare
  518. // String(error) would itself throw, skipping the ChildDisposed ack
  519. // and wedging the script's finally until the grace/terminate path.
  520. // oxlint-disable-next-line typescript/prefer-promise-reject-errors -- the non-Error rejection IS the scenario under test
  521. dispose: () => Promise.reject({ toString: () => { throw new Error('coercion trap') } }),
  522. }),
  523. }
  524. ctx.subagents.registerProvider(provider)
  525. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'coercion-trap-dispose', maxConcurrentAgents: 2 })
  526. const result = await run(ctx, fakeParent(), scripted("return await agent('p')"))
  527. expect(result.stopReason).toBe('completed')
  528. expect(result.value).toBe('fine')
  529. })
  530. it('the worker spawns with a scrubbed environment: an escaped script finds no ambient credentials', async () => {
  531. const { ctx, parent } = await setup()
  532. // A canary in the HARNESS process's env: with an inherited environment
  533. // the escape below would read it back (exactly how DEEPSEEK_API_KEY
  534. // would leak); the worker env keeps every ambient variable out. Windows
  535. // additionally receives the host temp path (TMP/TEMP) so `os.tmpdir()`
  536. // inside the worker resolves instead of degrading to a cwd-relative
  537. // `undefined\temp` (tsx writes its transform cache there).
  538. process.env.WORKFLOW_ENV_CANARY = 'leak me'
  539. // The unbuilt worker forwards TSX_TSCONFIG_PATH (a path pin, not a
  540. // credential); clear it so this test observes the empty ambient case
  541. // regardless of the parent's environment.
  542. const tsconfigPath = process.env.TSX_TSCONFIG_PATH
  543. delete process.env.TSX_TSCONFIG_PATH
  544. try {
  545. const result = await run(ctx, parent, scripted(`
  546. const proc = ${ESCAPE}
  547. return { canary: proc.env.WORKFLOW_ENV_CANARY ?? null, keys: Object.keys(proc.env).sort() }
  548. `))
  549. expect(result.stopReason).toBe('completed')
  550. const expectedKeys = process.platform === 'win32' ? ['TEMP', 'TMP'] : []
  551. expect(result.value).toEqual({ canary: null, keys: expectedKeys })
  552. } finally {
  553. if (tsconfigPath === undefined) delete process.env.TSX_TSCONFIG_PATH
  554. else process.env.TSX_TSCONFIG_PATH = tsconfigPath
  555. delete process.env.WORKFLOW_ENV_CANARY
  556. }
  557. })
  558. it('workerSpawnEnv injects the host temp path on win32 and leaves the POSIX peer empty', () => {
  559. const tmp = tmpdir()
  560. expect(workerSpawnEnv('win32')).toEqual({ TMP: tmp, TEMP: tmp })
  561. expect(workerSpawnEnv('linux')).toEqual({})
  562. })
  563. it('workerSpawnEnv forwards TSX_TSCONFIG_PATH when the snapshot harness pins it', () => {
  564. const tsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  565. expect(workerSpawnEnv('linux', tsconfig)).toEqual({ TSX_TSCONFIG_PATH: tsconfig })
  566. expect(workerSpawnEnv('win32', tsconfig)).toEqual({
  567. TMP: tmpdir(),
  568. TEMP: tmpdir(),
  569. TSX_TSCONFIG_PATH: tsconfig,
  570. })
  571. })
  572. it('the unbuilt worker forwards exactly TSX_TSCONFIG_PATH through the scrub: the paths-map pin survives, secrets do not', async () => {
  573. const { ctx, parent } = await setup()
  574. // The ACP snapshot harness runs the parent with its cwd OUTSIDE the
  575. // repo and pins the repo tsconfig through this variable; the worker
  576. // must inherit the pin (or its dsh-* imports silently resolve to
  577. // unbuilt lib/ bundles) while every other variable stays scrubbed.
  578. const tsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  579. process.env.TSX_TSCONFIG_PATH = tsconfig
  580. process.env.WORKFLOW_ENV_CANARY = 'leak me'
  581. try {
  582. const result = await run(ctx, parent, scripted(`
  583. const proc = ${ESCAPE}
  584. return { keys: Object.keys(proc.env).sort(), tsconfig: proc.env.TSX_TSCONFIG_PATH }
  585. `))
  586. expect(result.stopReason).toBe('completed')
  587. const expectedKeys = process.platform === 'win32'
  588. ? ['TEMP', 'TMP', 'TSX_TSCONFIG_PATH']
  589. : ['TSX_TSCONFIG_PATH']
  590. expect(result.value).toEqual({ keys: expectedKeys, tsconfig })
  591. } finally {
  592. delete process.env.TSX_TSCONFIG_PATH
  593. delete process.env.WORKFLOW_ENV_CANARY
  594. }
  595. })
  596. })
  597. describe('lifecycle: parse errors, cancellation, termination, disposal', () => {
  598. it('start() throws synchronously for invalid meta data or an unparseable body (host-side pre-checks)', async () => {
  599. const { ctx, parent } = await setup()
  600. // Meta is DATA — shape violations reject loud, every one named.
  601. expect(() => ctx.workflowEngine.start({ script: 'return 1', meta: { name: '', description: 'd' }, parent })).toThrow(/meta\.name must be a non-empty string/)
  602. expect(() => ctx.workflowEngine.start({ script: 'return 1', meta: { name: 'x', description: 'd', extra: 1 } as unknown as WorkflowMeta, parent })).toThrow(/META_INVALID|not a recognized field/)
  603. expect(() => ctx.workflowEngine.start({ ...scripted('return ((('), parent })).toThrow(/does not parse/)
  604. // The likeliest authoring slip — a Claude Code-style meta header in the
  605. // body — gets a pointed message, not a bare SyntaxError.
  606. expect(() => ctx.workflowEngine.start({ ...scripted("export const meta = { name: 'x', description: 'd' }\nreturn 1"), parent })).toThrow(/meta rides the `meta` request field/)
  607. })
  608. it('cancel() aborts in-flight children (signal AND cancel RPC) and settles the run cancelled', async () => {
  609. const { ctx, parent, provider } = await setup({ manual: true })
  610. const ends: unknown[] = []
  611. ctx.on('workflow/agent-end', (_info, agent) => { ends.push(agent) })
  612. const runEnds: WorkflowResultInfo[] = []
  613. ctx.on('workflow/end', (_info, result) => { runEnds.push(result) })
  614. const handle = ctx.workflowEngine.start({ ...scripted("return await agent('long job')"), parent })
  615. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  616. handle.cancel('user stopped it')
  617. const result = await handle.result
  618. expect(result.stopReason).toBe('cancelled')
  619. expect(result.error).toContain('user stopped it')
  620. await handle.dispose()
  621. expect(provider.runs[0]!.disposed).toBe(true)
  622. expect(ends).toEqual([expect.objectContaining({ seq: 1, outcome: 'cancelled' })])
  623. // workflow/end is an observer's only death signal: it fires for a
  624. // cancelled run too, mirroring the settled outcome data.
  625. expect(runEnds).toEqual([{ stopReason: 'cancelled', error: result.error, agentsStarted: result.agentsStarted }])
  626. })
  627. it('an already-aborted request signal cancels before the body ever runs (the go handshake holds it)', async () => {
  628. const { ctx, parent, provider } = await setup()
  629. const controller = new AbortController()
  630. controller.abort()
  631. const logs: string[] = []
  632. ctx.on('workflow/log', (_info, message) => { logs.push(message) })
  633. const handle = ctx.workflowEngine.start({ ...scripted("log('ran')\nreturn 123"), parent, signal: controller.signal })
  634. const result = await handle.result
  635. expect(result.stopReason).toBe('cancelled')
  636. expect(result.value).toBeNull()
  637. expect(logs).toEqual([])
  638. expect(provider.runs.length).toBe(0)
  639. await handle.dispose()
  640. })
  641. it('cancel() right after start() cancels before the body runs; the signal aborting mid-run cancels like cancel()', async () => {
  642. const { ctx, parent, provider } = await setup({ manual: true })
  643. const first = ctx.workflowEngine.start({ ...scripted("return await agent('never')"), parent })
  644. // No-reason cancel: the canonical default reason must ride the result.
  645. first.cancel()
  646. const firstResult = await first.result
  647. expect(firstResult.stopReason).toBe('cancelled')
  648. expect(firstResult.error).toContain('workflow cancelled')
  649. expect(provider.runs.length).toBe(0)
  650. await first.dispose()
  651. const controller = new AbortController()
  652. const second = ctx.workflowEngine.start({ ...scripted("return await agent('job')"), parent, signal: controller.signal })
  653. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  654. controller.abort()
  655. expect((await second.result).stopReason).toBe('cancelled')
  656. await second.dispose()
  657. })
  658. it('removes the exact external abort callback on first settlement or teardown', async () => {
  659. const { ctx, parent } = await setup()
  660. const settledController = new AbortController()
  661. const settledAdd = vi.spyOn(settledController.signal, 'addEventListener')
  662. const settledRemove = vi.spyOn(settledController.signal, 'removeEventListener')
  663. const completed = ctx.workflowEngine.start({ ...scripted('return 123'), parent, signal: settledController.signal })
  664. const settledAbort = settledAdd.mock.calls.find(([type]) => type === 'abort')?.[1]
  665. expect(typeof settledAbort).toBe('function')
  666. await expect(completed.result).resolves.toMatchObject({ value: 123, stopReason: 'completed' })
  667. expect(settledRemove).toHaveBeenCalledWith('abort', settledAbort)
  668. const cancelAfterSettle = vi.spyOn(completed, 'cancel')
  669. settledController.abort()
  670. expect(cancelAfterSettle).not.toHaveBeenCalled()
  671. cancelAfterSettle.mockRestore()
  672. await completed.dispose()
  673. const manual = await setup({ manual: true })
  674. const teardownController = new AbortController()
  675. const teardownAdd = vi.spyOn(teardownController.signal, 'addEventListener')
  676. const teardownRemove = vi.spyOn(teardownController.signal, 'removeEventListener')
  677. const tornDown = manual.ctx.workflowEngine.start({
  678. ...scripted("return await agent('job')"),
  679. parent: manual.parent,
  680. signal: teardownController.signal,
  681. })
  682. await waitFor(() => { expect(manual.provider.runs).toHaveLength(1) })
  683. const teardownAbort = teardownAdd.mock.calls.find(([type]) => type === 'abort')?.[1]
  684. expect(typeof teardownAbort).toBe('function')
  685. const disposing = tornDown.dispose()
  686. expect(teardownRemove).toHaveBeenCalledWith('abort', teardownAbort)
  687. await disposing
  688. })
  689. it('a child-start racing the host cancel is refused: no child starts after cancellation', async () => {
  690. const { ctx, parent, provider } = await setup({ manual: true })
  691. // Cancel from INSIDE the log listener: the worker has already posted
  692. // its child-start (queued right behind the log message), so the host
  693. // processes it with cancelReason set — the refusal arm no real-world
  694. // timing can hit reliably. (The closure runs only after `handle` below
  695. // is initialized — the listener fires on the worker's first message.)
  696. ctx.on('workflow/log', () => { handle.cancel('cancelled from the log listener') })
  697. const handle = ctx.workflowEngine.start({ ...scripted("log('mark')\nreturn await agent('late')"), parent })
  698. const result = await handle.result
  699. expect(result.stopReason).toBe('cancelled')
  700. expect(provider.runs.length).toBe(0)
  701. await handle.dispose()
  702. })
  703. it('post-cancel narration is suppressed host-side, and completion racing a cancel reports cancelled', async () => {
  704. const { ctx, parent } = await setup()
  705. const narration: string[] = []
  706. ctx.on('workflow/log', (_info, message) => { narration.push(message) })
  707. ctx.on('workflow/phase', (_info, title) => { narration.push(`phase:${title}`) })
  708. const handle = ctx.workflowEngine.start({
  709. // The sync spin keeps the worker's loop busy so the cancel message
  710. // cannot be processed before the script settles `completed` — the
  711. // worker posts a completed result that must LOSE to the in-flight
  712. // host cancellation. The trailing narration exercises host-side
  713. // suppression: posted pre-cancel-processing worker-side, arriving
  714. // post-cancel host-side.
  715. ...scripted(`
  716. log('started')
  717. const end = Date.now() + 1000
  718. while (Date.now() < end) {}
  719. phase('late phase')
  720. log('late log')
  721. return 'done'
  722. `),
  723. parent,
  724. })
  725. await waitFor(() => { expect(narration).toContain('started') })
  726. handle.cancel('raced the completion')
  727. const result = await handle.result
  728. expect(result.stopReason).toBe('cancelled')
  729. expect(result.error).toContain('raced the completion')
  730. expect(narration).toEqual(['started'])
  731. await handle.dispose()
  732. }, 90_000)
  733. it('cancel() force-settles a script parked on a promise no hook owns, and TERMINATES its worker', async () => {
  734. const { ctx, parent } = await setup({ config: { provider: 'stub', disposeGraceMs: 50 } })
  735. const runEnds: WorkflowResultInfo[] = []
  736. ctx.on('workflow/end', (_info, result) => { runEnds.push(result) })
  737. const handle = ctx.workflowEngine.start({
  738. ...scripted("await new Promise(() => {})\nreturn 'unreachable'"),
  739. parent,
  740. })
  741. handle.cancel('user aborted')
  742. const result = await handle.result
  743. expect(result.stopReason).toBe('cancelled')
  744. expect(result.error).toContain('user aborted')
  745. // The grace force-settle fires workflow/end exactly like an ordinary
  746. // settlement — a terminated script's death still reaches observers.
  747. expect(runEnds).toEqual([{ stopReason: 'cancelled', error: result.error, agentsStarted: 0 }])
  748. await handle.dispose()
  749. })
  750. it('dispose() on a stuck script returns within the grace instead of hanging (result settles cancelled)', async () => {
  751. const { ctx, parent } = await setup({ config: { provider: 'stub', disposeGraceMs: 50 } })
  752. const handle = ctx.workflowEngine.start({
  753. ...scripted("await new Promise(() => {})\nreturn 'unreachable'"),
  754. parent,
  755. })
  756. const before = Date.now()
  757. await handle.dispose()
  758. expect(Date.now() - before).toBeLessThan(2000)
  759. const result = await handle.result
  760. expect(result.stopReason).toBe('cancelled')
  761. })
  762. it('dispose() is idempotent and settles cleanly after a completed run', async () => {
  763. const { ctx, parent } = await setup()
  764. const handle = ctx.workflowEngine.start({ ...scripted('return 1'), parent })
  765. await handle.result
  766. await handle.dispose()
  767. await handle.dispose()
  768. })
  769. it('a settled run arms NO grace timer: disposing a completed run must not pin it for disposeGraceMs', async () => {
  770. // A distinctive grace so the spy can tell the cancel-path grace timer
  771. // apart from every other timeout in flight.
  772. const GRACE = 44_444
  773. const { ctx, parent } = await setup({ config: { provider: 'stub', disposeGraceMs: GRACE } })
  774. const handle = ctx.workflowEngine.start({ ...scripted('return 1'), parent })
  775. await handle.result
  776. const spy = vi.spyOn(globalThis, 'setTimeout')
  777. try {
  778. await handle.dispose()
  779. // dispose()'s own bounded-wait sleep is the ONLY grace-sized timer
  780. // allowed here; before the settled guard, cancel() armed a second one
  781. // that nothing would ever clear (the run was already settled), keeping
  782. // the WorkerRun/Worker closure alive until the grace expired.
  783. const graceTimers = spy.mock.calls.filter(call => call[1] === GRACE)
  784. expect(graceTimers.length).toBe(1)
  785. } finally {
  786. spy.mockRestore()
  787. }
  788. })
  789. it('strays: children fired without await are aborted once the script settles, and dispose() waits for their disposal', async () => {
  790. const { ctx, parent, provider } = await setup({ manual: true, disposeDelayMs: 40 })
  791. const handle = ctx.workflowEngine.start({
  792. ...scripted(`
  793. agent('stray')
  794. return 'done without awaiting'
  795. `),
  796. parent,
  797. })
  798. const result = await handle.result
  799. expect(result.stopReason).toBe('completed')
  800. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  801. await handle.dispose()
  802. // Not a waitFor: by the time dispose() returns, the slow child disposal
  803. // must already be complete (host-side registry quiescence).
  804. expect(provider.runs[0]!.disposed).toBe(true)
  805. })
  806. it('result settlement reaps a registered stray even when the worker cannot relay disposal', async () => {
  807. const { ctx, parent, provider } = await setup({
  808. manual: true,
  809. config: { provider: 'stub', disposeGraceMs: 30_000 },
  810. })
  811. const handle = ctx.workflowEngine.start({
  812. ...scripted("agent('stray')\nawait new Promise(() => {})"),
  813. parent,
  814. })
  815. await waitFor(() => { expect(provider.runs).toHaveLength(1) })
  816. // Claim the host result while the real worker remains wedged, so it can
  817. // send neither ChildDispose nor an exit. This leaves the accepted child
  818. // in the host registry when public disposal begins.
  819. const worker = (handle as unknown as { worker: Worker }).worker
  820. worker.emit('message', {
  821. type: WorkerToHostType.Result,
  822. result: { value: 'synthetic completion', stopReason: 'completed', agentsStarted: 1 },
  823. })
  824. await expect(handle.result).resolves.toMatchObject({ stopReason: 'completed' })
  825. await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000)
  826. const disposal = handle.dispose()
  827. await disposal
  828. expect(provider.runs[0]!.disposeCalls).toBe(1)
  829. await ctx.fiber.dispose()
  830. })
  831. it('the settle-reap fires the request signal too: a provider honoring ONLY the signal winds its stray down promptly', async () => {
  832. const ctx = new Context()
  833. await ctx.plugin(SessionProjectionRegistry)
  834. await ctx.plugin(SubagentRuntime)
  835. const aborted: string[] = []
  836. const provider: SubagentProvider = {
  837. name: 'signal-only',
  838. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  839. inheritsParentContext: false,
  840. start: async (request) => {
  841. let settle!: (result: SubagentResult) => void
  842. const result = new Promise<SubagentResult>((resolve) => { settle = resolve })
  843. request.signal.addEventListener('abort', () => {
  844. aborted.push(String(request.signal.reason))
  845. settle({ output: [], stopReason: 'aborted' })
  846. }, { once: true })
  847. return {
  848. id: SessionId('signal-only-child'),
  849. localAgent: undefined,
  850. result,
  851. dispose: () => Promise.resolve(),
  852. }
  853. },
  854. }
  855. ctx.subagents.registerProvider(provider)
  856. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'signal-only', maxConcurrentAgents: 2 })
  857. const handle = ctx.workflowEngine.start({
  858. ...scripted(`
  859. agent('stray, never awaited')
  860. return 'done'
  861. `),
  862. parent: fakeParent(),
  863. })
  864. const result = await handle.result
  865. expect(result.stopReason).toBe('completed')
  866. // BEFORE dispose(): the settlement itself must have aborted the signal —
  867. // without it this child would stay live until dispose's terminate. This
  868. // is a HOST-PROMPTNESS claim, not a cold-start race — a tight explicit
  869. // bound (unlike the file default) so a multi-second reap regression
  870. // cannot pass by outlasting the wait.
  871. await waitFor(() => { expect(aborted).toEqual(['workflow settled']) }, 1000)
  872. await handle.dispose()
  873. })
  874. it('the settle-reap aborts a pending provider start before workflow/end', async () => {
  875. const { ctx, parent, provider } = await setup({ manual: true, deferStart: true })
  876. const childLifecycle: string[] = []
  877. let cancellationAtWorkflowEnd: string | undefined
  878. ctx.on('workflow/agent-start', () => { childLifecycle.push('start') })
  879. ctx.on('workflow/agent-end', () => { childLifecycle.push('end') })
  880. ctx.on('workflow/end', () => {
  881. cancellationAtWorkflowEnd = provider.runs[0]?.cancelled
  882. })
  883. const handle = ctx.workflowEngine.start({
  884. ...scripted(`
  885. agent('start-pending stray')
  886. return 'done'
  887. `),
  888. parent,
  889. })
  890. const result = await handle.result
  891. expect(result.stopReason).toBe('completed')
  892. expect(provider.runs).toHaveLength(1)
  893. expect(provider.runs[0]!.request.signal?.aborted).toBe(true)
  894. expect(provider.runs[0]!.request.signal?.reason).toBe('workflow settled')
  895. expect(provider.runs[0]!.cancelled).toBe('workflow settled')
  896. expect(cancellationAtWorkflowEnd).toBe('workflow settled')
  897. expect(childLifecycle).toEqual([])
  898. await handle.dispose()
  899. expect(provider.runs[0]!.disposeCalls).toBe(1)
  900. })
  901. it('a duplicate Result after the terminal claim cannot repeat cleanup or rewrite the outcome', async () => {
  902. let signalAborts = 0
  903. const { ctx, parent, provider } = await setup({
  904. manual: true,
  905. onChildAbortString: (_reason, index) => { if (index === 0) signalAborts += 1 },
  906. })
  907. const handle = ctx.workflowEngine.start({
  908. ...scripted("agent('stray')\nawait new Promise(() => {})"),
  909. parent,
  910. })
  911. await waitFor(() => { expect(provider.runs).toHaveLength(1) })
  912. const worker = (handle as unknown as { worker: Worker }).worker
  913. worker.emit('message', {
  914. type: WorkerToHostType.Result,
  915. result: { value: 'first', stopReason: 'completed', agentsStarted: 1 },
  916. })
  917. worker.emit('message', {
  918. type: WorkerToHostType.Result,
  919. result: { value: 'late', stopReason: 'completed', agentsStarted: 1 },
  920. })
  921. await expect(handle.result).resolves.toMatchObject({ value: 'first', stopReason: 'completed' })
  922. expect(signalAborts).toBe(1)
  923. await handle.dispose()
  924. expect(signalAborts).toBe(1)
  925. await ctx.fiber.dispose()
  926. })
  927. it('a grace-terminated worker reaps its child on exit without waiting for consumer dispose()', async () => {
  928. const { ctx, parent, provider } = await setup({
  929. manual: true,
  930. config: { provider: 'stub', maxConcurrentAgents: 2, disposeGraceMs: 100 },
  931. })
  932. const handle = ctx.workflowEngine.start({
  933. // Let child-start cross, then make the worker unable to process its
  934. // Cancel message. Grace settles the result and terminates the thread;
  935. // that exit must independently own the host registry's disposal pass.
  936. ...scripted(`
  937. agent('survives until exit reap')
  938. for (let i = 0; i < 20; i++) await null
  939. const end = Date.now() + 1500
  940. while (Date.now() < end) {}
  941. return 'unreachable'
  942. `),
  943. parent,
  944. })
  945. await waitFor(() => { expect(provider.runs).toHaveLength(1) })
  946. handle.cancel('force termination')
  947. const result = await handle.result
  948. expect(result.stopReason).toBe('cancelled')
  949. // Deliberately assert before handle.dispose(): host-owned worker exit,
  950. // not consumer courtesy, is responsible for this resource guarantee.
  951. await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000)
  952. expect(provider.runs[0]!.disposeCalls).toBe(1)
  953. await handle.dispose()
  954. await ctx.fiber.dispose()
  955. }, 90_000)
  956. it('dispose() on a wedged worker host-drives child disposal inside the grace: it returns with the children DISPOSED, not with their teardown still in flight', async () => {
  957. const { ctx, parent, provider } = await setup({
  958. manual: true,
  959. disposeDelayMs: 40,
  960. config: { provider: 'stub', maxConcurrentAgents: 8, disposeGraceMs: 400 },
  961. })
  962. const handle = ctx.workflowEngine.start({
  963. // Same shape as the wedged-cancel test above: the child's start RPC
  964. // reaches the host, then the script seizes its worker's loop, so the
  965. // worker can relay NO dispose RPC — the host's own dispose() drive is
  966. // the only thing that can start (and finish) this child's disposal
  967. // before the grace runs out.
  968. ...scripted(`
  969. agent('wedged child')
  970. for (let i = 0; i < 20; i++) await null
  971. const end = Date.now() + 1500
  972. while (Date.now() < end) {}
  973. return 'raced'
  974. `),
  975. parent,
  976. })
  977. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  978. const before = Date.now()
  979. await handle.dispose()
  980. // Bounded by the grace (plus the terminate), never by the 1.5s spin.
  981. expect(Date.now() - before).toBeLessThan(1200)
  982. // Not a waitFor: dispose() resolving IS the quiescence claim — the slow
  983. // child disposal must be complete, not merely started (before the
  984. // host-driven drive, disposal only STARTED at the post-terminate reap,
  985. // so dispose() returned with it still in flight).
  986. expect(provider.runs[0]!.disposed).toBe(true)
  987. const result = await handle.result
  988. expect(result.stopReason).toBe('cancelled')
  989. }, 90_000)
  990. it('a live child disposed by the dispose() drive is disposed ONCE, and the worker\'s late dispose RPC still gets its ack (the script settles, not the grace)', async () => {
  991. const { ctx, parent, provider } = await setup({ manual: true })
  992. const handle = ctx.workflowEngine.start({
  993. ...scripted(`
  994. await agent('long child')
  995. return 'unreachable'
  996. `),
  997. parent,
  998. })
  999. await waitFor(() => { expect(provider.runs.length).toBe(1) })
  1000. const handleDispose = handle.dispose()
  1001. const result = await handle.result
  1002. // The script itself settled (the wrapper's own dispose RPC found the
  1003. // child already reaped host-side and was acked) — a missing ack would
  1004. // wedge the wrapper's finally until the 5s default grace force-settle.
  1005. expect(result.stopReason).toBe('cancelled')
  1006. expect(result.error).toContain('workflow disposed')
  1007. await handleDispose
  1008. expect(provider.runs[0]!.disposed).toBe(true)
  1009. // The memo: the host drive and the worker's RPC share one disposal.
  1010. expect(provider.runs[0]!.disposeCalls).toBe(1)
  1011. })
  1012. it('the grace force-settle pairs every stranded start: a host-synthesized cancelled agent-end lands before workflow/end', async () => {
  1013. const { ctx, parent, provider } = await setup({ manual: true, config: { provider: 'stub', maxConcurrentAgents: 8, disposeGraceMs: 300 } })
  1014. const ends: { seq: number; outcome: string }[] = []
  1015. const order: string[] = []
  1016. ctx.on('workflow/agent-start', (_info, agent) => { order.push(`start:${agent.seq}`) })
  1017. ctx.on('workflow/agent-end', (_info, agent) => {
  1018. ends.push({ seq: agent.seq, outcome: agent.outcome })
  1019. order.push(`end:${agent.seq}`)
  1020. })
  1021. ctx.on('workflow/end', () => { order.push('run-end') })
  1022. const handle = ctx.workflowEngine.start({
  1023. // 'slow' starts and its agent-start crosses to observers (the awaited
  1024. // 'fast' call keeps the worker loop turning), then the script seizes
  1025. // the loop: the wedged worker can never author slow's agent-end —
  1026. // only the host's ledger can close the pair.
  1027. ...scripted(`
  1028. const p = agent('slow')
  1029. await agent('fast')
  1030. const end = Date.now() + 1500
  1031. while (Date.now() < end) {}
  1032. return 'raced'
  1033. `),
  1034. parent,
  1035. })
  1036. await waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) })
  1037. const fast = provider.runs.find(run => (run.request.prompt[0] as { text?: string }).text === 'fast')!
  1038. fast.settle(text('fast done'))
  1039. handle.cancel('stop now')
  1040. const result = await handle.result
  1041. expect(result.stopReason).toBe('cancelled')
  1042. // fast's end is the worker's own report; slow's is host-synthesized at
  1043. // the force-settle — exactly one end per started seq, no third event.
  1044. expect(ends).toEqual([
  1045. { seq: 2, outcome: 'completed' },
  1046. { seq: 1, outcome: 'cancelled' },
  1047. ])
  1048. // Both ends reached observers BEFORE workflow/end: a progress consumer
  1049. // can finalize its state at run-end without dangling agents.
  1050. expect(order.indexOf('run-end')).toBe(order.length - 1)
  1051. await handle.dispose()
  1052. }, 90_000)
  1053. it('graceful cancellation keeps pairing worker-authored: exactly one agent-end per start, nothing synthesized on top', async () => {
  1054. const { ctx, parent, provider } = await setup({ manual: true })
  1055. const ends: { seq: number; outcome: string }[] = []
  1056. const order: string[] = []
  1057. ctx.on('workflow/agent-end', (_info, agent) => {
  1058. ends.push({ seq: agent.seq, outcome: agent.outcome })
  1059. order.push(`end:${agent.seq}`)
  1060. })
  1061. ctx.on('workflow/end', () => { order.push('run-end') })
  1062. const handle = ctx.workflowEngine.start({
  1063. ...scripted("await parallel([() => agent('a'), () => agent('b')])\nreturn 'unreachable'"),
  1064. parent,
  1065. })
  1066. await waitFor(() => { expect(provider.runs.length).toBe(2) })
  1067. handle.cancel('user stop')
  1068. const result = await handle.result
  1069. expect(result.stopReason).toBe('cancelled')
  1070. // The live worker reported both pairs itself; the ledger must not add
  1071. // a synthesized duplicate on any path that settles inside the grace.
  1072. expect(ends.map(end => end.outcome)).toEqual(['cancelled', 'cancelled'])
  1073. expect(new Set(ends.map(end => end.seq)).size).toBe(2)
  1074. expect(order.indexOf('run-end')).toBe(order.length - 1)
  1075. await handle.dispose()
  1076. })
  1077. })
  1078. describe('worker death', () => {
  1079. it('the first death signal closes admission to messages Node delivers before exit', async () => {
  1080. const { ctx, parent, provider } = await setup({ manual: true })
  1081. const phases: string[] = []
  1082. ctx.on('workflow/phase', (_info, title) => { phases.push(title) })
  1083. const handle = ctx.workflowEngine.start({
  1084. ...scripted('await new Promise(() => {})'),
  1085. parent,
  1086. })
  1087. const worker = (handle as unknown as { worker: Worker }).worker
  1088. // Node may physically emit error -> queued message -> exit. Reproduce
  1089. // that ordering deterministically at the Worker event boundary: the
  1090. // late protocol data must not create work, narrate, or rewrite error.
  1091. worker.emit('error', new Error('synthetic error-before-message'))
  1092. worker.emit('message', { type: WorkerToHostType.Phase, title: 'late phase' })
  1093. worker.emit('message', {
  1094. type: WorkerToHostType.ChildStart,
  1095. callId: 999,
  1096. request: { prompt: 'late child' },
  1097. })
  1098. worker.emit('message', {
  1099. type: WorkerToHostType.Result,
  1100. result: { value: 'late', stopReason: 'completed', agentsStarted: 1 },
  1101. })
  1102. const result = await handle.result
  1103. expect(result.stopReason).toBe('error')
  1104. expect(result.error).toContain('synthetic error-before-message')
  1105. expect(provider.runs).toHaveLength(0)
  1106. expect(phases).toEqual([])
  1107. await handle.dispose()
  1108. await ctx.fiber.dispose()
  1109. })
  1110. it('refuses and disposes a provider run that becomes ready after its real worker dies', async () => {
  1111. const ctx = new Context()
  1112. await ctx.plugin(SessionProjectionRegistry)
  1113. await ctx.plugin(SubagentRuntime)
  1114. const requested = Promise.withResolvers<SubagentStartRequest>()
  1115. const ready = Promise.withResolvers<SubagentRun>()
  1116. let disposeCalls = 0
  1117. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => ctx.logger)
  1118. const provider: SubagentProvider = {
  1119. name: 'late-ready',
  1120. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  1121. inheritsParentContext: false,
  1122. start: (request) => {
  1123. requested.resolve(request)
  1124. // Model a backend whose independent startup boundary cannot be
  1125. // interrupted promptly. The host must still reject ownership if the
  1126. // worker dies before this promise transfers the ready run.
  1127. return ready.promise
  1128. },
  1129. }
  1130. ctx.subagents.registerProvider(provider)
  1131. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'late-ready', maxConcurrentAgents: 1 })
  1132. const lifecycle: string[] = []
  1133. ctx.on('workflow/agent-start', () => { lifecycle.push('start') })
  1134. ctx.on('workflow/agent-end', () => { lifecycle.push('end') })
  1135. const handle = ctx.workflowEngine.start({
  1136. ...scripted("return await agent('pending startup')"),
  1137. parent: fakeParent(),
  1138. })
  1139. const request = await requested.promise
  1140. const worker = (handle as unknown as { worker: Worker }).worker
  1141. // Kill the actual Worker while provider startup is independently
  1142. // pending. Death closes admission and aborts the shared signal, but this
  1143. // deliberately uncooperative provider still fulfills afterward.
  1144. await worker.terminate()
  1145. const result = await handle.result
  1146. expect(result.stopReason).toBe('error')
  1147. expect(result.error).toContain('exit code')
  1148. expect(request.signal.aborted).toBe(true)
  1149. expect(request.signal.reason).toBe('workflow worker gone')
  1150. ready.resolve({
  1151. id: SessionId('late-ready-child'),
  1152. localAgent: undefined,
  1153. result: Promise.resolve({ output: [], stopReason: 'aborted' }),
  1154. dispose: () => {
  1155. disposeCalls += 1
  1156. return Promise.reject(new Error('late ready dispose failed'))
  1157. },
  1158. })
  1159. await waitFor(() => {
  1160. expect(disposeCalls).toBe(1)
  1161. expect(warn).toHaveBeenCalledWith(expect.stringContaining('refused child dispose failed: Error: late ready dispose failed'))
  1162. }, 1000)
  1163. expect(lifecycle).toEqual([])
  1164. await handle.dispose()
  1165. expect(disposeCalls).toBe(1)
  1166. await ctx.fiber.dispose()
  1167. })
  1168. it('a worker that exits before settling reports an error result and reaps its children', async () => {
  1169. const ctx = new Context()
  1170. await ctx.plugin(SessionProjectionRegistry)
  1171. await ctx.plugin(SubagentRuntime)
  1172. // The child's dispose() REJECTS on top of the worker death: the reap
  1173. // must contain it (warn, not crash) while still emptying the registry.
  1174. const signalAborts: unknown[] = []
  1175. const provider: SubagentProvider = {
  1176. name: 'doomed',
  1177. capabilities: { agentOptions: true, outputSchema: true, depthLimit: true, toolFilter: true, persona: false },
  1178. inheritsParentContext: false,
  1179. start: async (request) => {
  1180. request.signal.addEventListener('abort', () => {
  1181. signalAborts.push(request.signal.reason)
  1182. // The death claim precedes the shared-signal fanout. This
  1183. // synchronous callback cannot turn death into cancellation.
  1184. handle.cancel('reentered from worker-death signal cleanup')
  1185. }, { once: true })
  1186. return {
  1187. id: SessionId('doomed-child'),
  1188. localAgent: undefined,
  1189. result: new Promise(() => { /* never settles; the reap is the teardown */ }),
  1190. dispose: () => Promise.reject(new Error('dispose exploded during reap')),
  1191. }
  1192. },
  1193. }
  1194. ctx.subagents.registerProvider(provider)
  1195. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'doomed', maxConcurrentAgents: 2 })
  1196. const runEnds: WorkflowResultInfo[] = []
  1197. ctx.on('workflow/end', (_info, result) => { runEnds.push(result) })
  1198. const childStarted = Promise.withResolvers<undefined>()
  1199. ctx.on('workflow/agent-start', () => { childStarted.resolve(undefined) })
  1200. const handle = ctx.workflowEngine.start({
  1201. ...scripted("return await agent('doomed')"),
  1202. parent: fakeParent(),
  1203. })
  1204. const worker = (handle as unknown as { worker: Worker }).worker
  1205. await childStarted.promise
  1206. await worker.terminate()
  1207. const result = await handle.result
  1208. expect(result.stopReason).toBe('error')
  1209. expect(result.error).toContain('exit code 1')
  1210. expect(result.agentsStarted).toBe(1)
  1211. // A worker death is a stop reason like any other: workflow/end fires
  1212. // with the error outcome — for a bus observer it is the only obituary.
  1213. expect(runEnds).toEqual([{ stopReason: 'error', error: result.error, agentsStarted: 1 }])
  1214. // Result already settled — this is the reap's promptness, not a
  1215. // cold-start race; tight explicit bound (see the helper's doc comment).
  1216. await waitFor(() => {
  1217. expect(signalAborts).toEqual(['workflow worker gone'])
  1218. }, 1000)
  1219. await Promise.resolve()
  1220. expect(result.stopReason).toBe('error')
  1221. await handle.dispose()
  1222. }, 90_000)
  1223. it('an uncaught exception inside the worker surfaces as an error result and reaps the in-flight child', async () => {
  1224. const { ctx, parent, provider } = await setup({ manual: true })
  1225. const handle = ctx.workflowEngine.start({
  1226. ...scripted(`
  1227. agent('in flight when the worker dies')
  1228. const proc = ${ESCAPE}
  1229. const st = globalThis.constructor.constructor('return setTimeout')()
  1230. await new Promise(resolve => st(resolve, 200))
  1231. proc.nextTick(() => { throw new Error('worker blew up') })
  1232. await new Promise(() => {})
  1233. `),
  1234. parent,
  1235. })
  1236. const result = await handle.result
  1237. expect(result.stopReason).toBe('error')
  1238. expect(result.error).toContain('worker blew up')
  1239. // The reap wound the stray child down (cancel + a CLEAN dispose).
  1240. // Result already settled — this is the reap's promptness, not a
  1241. // cold-start race; tight explicit bound (see the helper's doc comment).
  1242. await waitFor(() => {
  1243. expect(provider.runs.length).toBe(1)
  1244. expect(provider.runs[0]!.disposed).toBe(true)
  1245. }, 1000)
  1246. await handle.dispose()
  1247. }, 90_000)
  1248. it('a worker death pairs every stranded start: the synthesized cancelled agent-end precedes the error workflow/end', async () => {
  1249. const { ctx, parent, provider } = await setup({ manual: true })
  1250. const ends: { seq: number; outcome: string }[] = []
  1251. const order: string[] = []
  1252. ctx.on('workflow/agent-start', (_info, agent) => { order.push(`start:${agent.seq}`) })
  1253. ctx.on('workflow/agent-end', (_info, agent) => {
  1254. ends.push({ seq: agent.seq, outcome: agent.outcome })
  1255. order.push(`end:${agent.seq}`)
  1256. })
  1257. ctx.on('workflow/end', () => { order.push('run-end') })
  1258. const handle = ctx.workflowEngine.start({
  1259. ...scripted(`
  1260. const p = agent('slow')
  1261. await agent('fast')
  1262. await new Promise(() => {})
  1263. `),
  1264. parent,
  1265. })
  1266. const worker = (handle as unknown as { worker: Worker }).worker
  1267. await waitFor(() => { expect(order.filter(entry => entry.startsWith('start:')).length).toBe(2) })
  1268. const fast = provider.runs.find(run => (run.request.prompt[0] as { text?: string }).text === 'fast')!
  1269. fast.settle(text('fast done'))
  1270. await waitFor(() => { expect(ends).toContainEqual({ seq: 2, outcome: 'completed' }) })
  1271. await worker.terminate()
  1272. const result = await handle.result
  1273. expect(result.stopReason).toBe('error')
  1274. expect(result.error).toContain('exit code 1')
  1275. expect(ends).toEqual([
  1276. { seq: 2, outcome: 'completed' },
  1277. { seq: 1, outcome: 'cancelled' },
  1278. ])
  1279. expect(order.indexOf('run-end')).toBe(order.length - 1)
  1280. await handle.dispose()
  1281. }, 90_000)
  1282. it('a dispose ack racing the worker death is dropped, not crashed (post after exit)', async () => {
  1283. // Slow child disposal: the ack resolves only AFTER the worker died, so
  1284. // it has nowhere to go and must be dropped silently (the workerGone
  1285. // guard in post()).
  1286. const { ctx, parent, provider } = await setup({ disposeDelayMs: 300 })
  1287. const handle = ctx.workflowEngine.start({
  1288. ...scripted(`
  1289. agent('stray, never awaited')
  1290. await new Promise(() => {})
  1291. `),
  1292. parent,
  1293. })
  1294. const worker = (handle as unknown as { worker: Worker }).worker
  1295. await waitFor(() => {
  1296. expect(provider.runs).toHaveLength(1)
  1297. expect(provider.runs[0]!.disposeCalls).toBe(1)
  1298. expect(provider.runs[0]!.disposed).toBe(false)
  1299. })
  1300. await worker.terminate()
  1301. const result = await handle.result
  1302. expect(result.stopReason).toBe('error')
  1303. expect(result.error).toContain('exit code 1')
  1304. // Result already settled — this is the reap's promptness (bounded
  1305. // above the mock's fixed 300ms dispose delay, not a cold-start race);
  1306. // tight explicit bound (see the helper's doc comment).
  1307. await waitFor(() => { expect(provider.runs[0]!.disposed).toBe(true) }, 1000)
  1308. await handle.dispose()
  1309. }, 90_000)
  1310. it('a worker death AFTER a cancel reports cancelled, not error', async () => {
  1311. const { ctx, parent } = await setup({ config: { provider: 'stub', disposeGraceMs: 60_000 } })
  1312. const handle = ctx.workflowEngine.start({
  1313. ...scripted(`
  1314. log('armed')
  1315. await new Promise(() => {})
  1316. `),
  1317. parent,
  1318. })
  1319. const worker = (handle as unknown as { worker: Worker }).worker
  1320. const logs: string[] = []
  1321. ctx.on('workflow/log', (_info, message) => { logs.push(message) })
  1322. await waitFor(
  1323. () => { expect(logs).toContain('armed') },
  1324. process.platform === 'win32' ? 20_000 : 10_000,
  1325. )
  1326. handle.cancel('stop it')
  1327. // The grace is deliberately huge: only the host-triggered worker death,
  1328. // not the cancellation timer, settles this.
  1329. await worker.terminate()
  1330. const result = await handle.result
  1331. expect(result.stopReason).toBe('cancelled')
  1332. expect(result.error).toContain('stop it')
  1333. await handle.dispose()
  1334. }, process.platform === 'win32' ? 90_000 : 15_000)
  1335. })
  1336. describe('service API', () => {
  1337. it('run ids are unique and lifecycle meta is the run\'s borrowed immutable value', async () => {
  1338. const { ctx, parent } = await setup()
  1339. let eventMeta: WorkflowRunInfo | undefined
  1340. ctx.on('workflow/start', (info) => { eventMeta = info })
  1341. const first = ctx.workflowEngine.start({ ...scripted('return 1'), parent })
  1342. const second = ctx.workflowEngine.start({ ...scripted('return 2'), parent })
  1343. expect(first.id).not.toBe(second.id)
  1344. expect(eventMeta!.meta).toBe(second.meta)
  1345. expect(second.meta.name).toBe('test-flow')
  1346. await Promise.all([first.result, second.result])
  1347. await first.dispose()
  1348. await second.dispose()
  1349. })
  1350. it('unregisters ctx.workflowEngine when the engine fiber is disposed (HMR safety)', async () => {
  1351. const ctx = new Context()
  1352. await ctx.plugin(SessionProjectionRegistry)
  1353. await ctx.plugin(SubagentRuntime)
  1354. const fiber = await ctx.plugin(WorkerThreadWorkflowEngine, {})
  1355. expect(ctx.get('workflowEngine')).toBeDefined()
  1356. await fiber.dispose()
  1357. expect(ctx.get('workflowEngine')).toBeUndefined()
  1358. })
  1359. it('keeps a holder-owned run usable when the engine unloads before its child starts', async () => {
  1360. const { ctx, parent, provider, engineFiber } = await setup({ reply: () => text('survived reload') })
  1361. let handle!: ReturnType<typeof ctx.workflowEngine.start>
  1362. const holder = await ctx.plugin(Object.assign((inner: Context) => {
  1363. handle = inner.workflowEngine.start({ ...scripted("return await agent('after reload')"), parent })
  1364. }, { inject: ['workflowEngine'] }))
  1365. try {
  1366. // A real worker cannot deliver child-start in the synchronous start()
  1367. // slice. Unload the provider before that message arrives: the returned
  1368. // run belongs to `holder`, not to the engine fiber being reloaded.
  1369. expect(provider.runs).toHaveLength(0)
  1370. await engineFiber.dispose()
  1371. expect(ctx.get('workflowEngine')).toBeUndefined()
  1372. await expect(handle.result).resolves.toEqual({
  1373. value: 'survived reload',
  1374. stopReason: 'completed',
  1375. agentsStarted: 1,
  1376. })
  1377. expect(provider.runs).toHaveLength(1)
  1378. } finally {
  1379. await handle.dispose()
  1380. await holder.dispose()
  1381. await ctx.fiber.dispose()
  1382. }
  1383. })
  1384. it('has the class-plugin export shape (default = the engine service class)', () => {
  1385. expect(workerEngineModule.default).toBe(WorkerThreadWorkflowEngine)
  1386. expect('WorkerThreadWorkflowEngine' in workerEngineModule).toBe(false)
  1387. const loader = Object.create(Loader.prototype) as Loader
  1388. const unwrapped: unknown = loader.unwrapExports(workerEngineModule)
  1389. expect(unwrapped).toBe(WorkerThreadWorkflowEngine)
  1390. })
  1391. })
  1392. })