workflow-worker-thread.spec.ts 69 KB

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