@hexabot-ai/agentic 3.4.0-beta.2 → 3.5.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/README.md +67 -15
  2. package/dist/cjs/action/abstract-action.js +13 -1
  3. package/dist/cjs/checkpoint.js +301 -0
  4. package/dist/cjs/context.js +11 -27
  5. package/dist/cjs/dsl.types.js +3 -0
  6. package/dist/cjs/errors.js +116 -9
  7. package/dist/cjs/index.js +15 -6
  8. package/dist/cjs/resume-payload.js +257 -0
  9. package/dist/cjs/runner-runtime-control.js +181 -306
  10. package/dist/cjs/step-executors/conditional-executor.js +27 -29
  11. package/dist/cjs/step-executors/loop-executor.js +53 -62
  12. package/dist/cjs/step-executors/parallel-executor.js +169 -102
  13. package/dist/cjs/step-executors/task-executor.js +180 -96
  14. package/dist/cjs/utils/clone.js +73 -0
  15. package/dist/cjs/workflow-fingerprint.js +138 -0
  16. package/dist/cjs/workflow-runner.js +220 -155
  17. package/dist/cjs/workflow.js +24 -12
  18. package/dist/esm/action/abstract-action.js +14 -2
  19. package/dist/esm/checkpoint.js +293 -0
  20. package/dist/esm/context.js +11 -27
  21. package/dist/esm/dsl.types.js +3 -0
  22. package/dist/esm/errors.js +105 -6
  23. package/dist/esm/index.js +3 -3
  24. package/dist/esm/resume-payload.js +250 -0
  25. package/dist/esm/runner-runtime-control.js +179 -303
  26. package/dist/esm/step-executors/conditional-executor.js +27 -29
  27. package/dist/esm/step-executors/loop-executor.js +53 -62
  28. package/dist/esm/step-executors/parallel-executor.js +165 -98
  29. package/dist/esm/step-executors/task-executor.js +181 -97
  30. package/dist/esm/utils/clone.js +67 -0
  31. package/dist/esm/workflow-fingerprint.js +134 -0
  32. package/dist/esm/workflow-runner.js +220 -155
  33. package/dist/esm/workflow.js +24 -12
  34. package/dist/types/action/abstract-action.d.ts.map +1 -1
  35. package/dist/types/action/action.types.d.ts +0 -14
  36. package/dist/types/action/action.types.d.ts.map +1 -1
  37. package/dist/types/checkpoint.d.ts +188 -0
  38. package/dist/types/checkpoint.d.ts.map +1 -0
  39. package/dist/types/context.d.ts +45 -11
  40. package/dist/types/context.d.ts.map +1 -1
  41. package/dist/types/dsl.types.d.ts.map +1 -1
  42. package/dist/types/errors.d.ts +77 -3
  43. package/dist/types/errors.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +6 -7
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/dist/types/resume-payload.d.ts +69 -0
  47. package/dist/types/resume-payload.d.ts.map +1 -0
  48. package/dist/types/runner-runtime-control.d.ts +63 -51
  49. package/dist/types/runner-runtime-control.d.ts.map +1 -1
  50. package/dist/types/step-executors/conditional-executor.d.ts +6 -3
  51. package/dist/types/step-executors/conditional-executor.d.ts.map +1 -1
  52. package/dist/types/step-executors/loop-executor.d.ts +6 -4
  53. package/dist/types/step-executors/loop-executor.d.ts.map +1 -1
  54. package/dist/types/step-executors/parallel-executor.d.ts +11 -3
  55. package/dist/types/step-executors/parallel-executor.d.ts.map +1 -1
  56. package/dist/types/step-executors/task-executor.d.ts +11 -4
  57. package/dist/types/step-executors/task-executor.d.ts.map +1 -1
  58. package/dist/types/step-executors/types.d.ts +21 -13
  59. package/dist/types/step-executors/types.d.ts.map +1 -1
  60. package/dist/types/utils/clone.d.ts +33 -0
  61. package/dist/types/utils/clone.d.ts.map +1 -0
  62. package/dist/types/workflow-event-emitter.d.ts +29 -5
  63. package/dist/types/workflow-event-emitter.d.ts.map +1 -1
  64. package/dist/types/workflow-fingerprint.d.ts +14 -0
  65. package/dist/types/workflow-fingerprint.d.ts.map +1 -0
  66. package/dist/types/workflow-runner.d.ts +83 -41
  67. package/dist/types/workflow-runner.d.ts.map +1 -1
  68. package/dist/types/workflow-types.d.ts +25 -54
  69. package/dist/types/workflow-types.d.ts.map +1 -1
  70. package/dist/types/workflow.d.ts +24 -9
  71. package/dist/types/workflow.d.ts.map +1 -1
  72. package/examples/defs-bindings-agent/bindings.ts +1 -1
  73. package/examples/defs-bindings-agent/workflow.ts +3 -2
  74. package/examples/full/actions/await-user-input.ts +3 -2
  75. package/examples/full/actions/call-llm.ts +18 -18
  76. package/examples/full/actions/create-ticket.ts +1 -2
  77. package/examples/full/actions/decision-router.ts +6 -2
  78. package/examples/full/actions/get-calendar-events.ts +1 -2
  79. package/examples/full/actions/get-user-profile.ts +1 -2
  80. package/examples/full/actions/query-memory.ts +1 -2
  81. package/examples/full/actions/search-web.ts +1 -2
  82. package/examples/full/actions/send-email.ts +3 -2
  83. package/examples/full/workflow.ts +3 -2
  84. package/examples/loop/actions/await-reply.ts +2 -7
  85. package/examples/loop/workflow.ts +4 -2
  86. package/examples/suspend-resume/actions/wait-for-user.ts +11 -9
  87. package/examples/suspend-resume/workflow.ts +51 -11
  88. package/package.json +3 -3
  89. package/src/__tests__/checkpoint.test.ts +2568 -0
  90. package/src/__tests__/resume-payload.test.ts +245 -0
  91. package/src/__tests__/runner-runtime-control.test.ts +265 -0
  92. package/src/__tests__/workflow-fingerprint.test.ts +165 -0
  93. package/src/__tests__/workflow-runner.test.ts +167 -144
  94. package/src/__tests__/workflow.test.ts +97 -6
  95. package/src/action/abstract-action.ts +21 -2
  96. package/src/action/action.types.ts +0 -12
  97. package/src/checkpoint.ts +561 -0
  98. package/src/context.ts +51 -41
  99. package/src/dsl.types.ts +3 -0
  100. package/src/errors.ts +161 -8
  101. package/src/index.ts +33 -14
  102. package/src/resume-payload.ts +326 -0
  103. package/src/runner-runtime-control.ts +222 -456
  104. package/src/step-executors/conditional-executor.test.ts +77 -46
  105. package/src/step-executors/conditional-executor.ts +47 -40
  106. package/src/step-executors/loop-executor.test.ts +106 -92
  107. package/src/step-executors/loop-executor.ts +84 -101
  108. package/src/step-executors/parallel-executor.test.ts +300 -89
  109. package/src/step-executors/parallel-executor.ts +243 -125
  110. package/src/step-executors/task-executor.test.ts +321 -51
  111. package/src/step-executors/task-executor.ts +232 -145
  112. package/src/step-executors/types.ts +23 -22
  113. package/src/utils/clone.ts +75 -0
  114. package/src/workflow-event-emitter.ts +32 -6
  115. package/src/workflow-fingerprint.ts +159 -0
  116. package/src/workflow-runner.ts +325 -209
  117. package/src/workflow-types.ts +25 -56
  118. package/src/workflow.ts +41 -34
  119. package/dist/cjs/step-executors/suspension-continuation.js +0 -27
  120. package/dist/cjs/suspension-rebuilder.js +0 -265
  121. package/dist/cjs/utils/deferred.js +0 -17
  122. package/dist/esm/step-executors/suspension-continuation.js +0 -24
  123. package/dist/esm/suspension-rebuilder.js +0 -260
  124. package/dist/esm/utils/deferred.js +0 -14
  125. package/dist/types/step-executors/suspension-continuation.d.ts +0 -10
  126. package/dist/types/step-executors/suspension-continuation.d.ts.map +0 -1
  127. package/dist/types/suspension-rebuilder.d.ts +0 -67
  128. package/dist/types/suspension-rebuilder.d.ts.map +0 -1
  129. package/dist/types/utils/deferred.d.ts +0 -7
  130. package/dist/types/utils/deferred.d.ts.map +0 -1
  131. package/src/__tests__/suspension-rebuilder.test.ts +0 -404
  132. package/src/step-executors/suspension-continuation.ts +0 -31
  133. package/src/suspension-rebuilder.ts +0 -446
  134. package/src/utils/deferred.ts +0 -23
@@ -5,9 +5,13 @@
5
5
  */
6
6
 
7
7
  import type { Action } from '../action/action.types';
8
+ import type { TaskFrame } from '../checkpoint';
8
9
  import { BaseWorkflowContext, type StepExecutionRecord } from '../context';
9
- import type { RuntimeSuspensionRequest } from '../runner-runtime-control';
10
- import { createDeferred } from '../utils/deferred';
10
+ import {
11
+ NonDeterministicWorkflowError,
12
+ WorkflowResumeRejectedError,
13
+ } from '../errors';
14
+ import { RunnerRuntimeControl } from '../runner-runtime-control';
11
15
  import {
12
16
  type EventEmitterLike,
13
17
  type StepInfo,
@@ -74,11 +78,17 @@ const createEnv = (
74
78
  stepInfo: StepInfo,
75
79
  ): StepExecutorEnv => {
76
80
  const stepRecords: Record<string, StepExecutionRecord> = {};
81
+ const context = new TestContext();
82
+ const runtime = new RunnerRuntimeControl();
83
+ context.attachWorkflowRuntime(runtime);
77
84
  const env = {
78
85
  compiled,
79
- context: new TestContext(),
86
+ context,
80
87
  signal: new AbortController().signal,
81
88
  runId: 'run-123',
89
+ runtime,
90
+ resolutions: new Map<string, unknown>(),
91
+ rejectResume: jest.fn(),
82
92
  buildInstanceStepInfo: jest.fn().mockReturnValue(stepInfo),
83
93
  markSnapshot: jest.fn(),
84
94
  recordStepExecution: jest.fn((step, update) => {
@@ -107,14 +117,6 @@ const createEnv = (
107
117
  return nextRecord;
108
118
  }),
109
119
  emit: jest.fn(),
110
- setCurrentStep: jest.fn(),
111
- waitForStepSuspension: jest
112
- .fn()
113
- .mockImplementation(
114
- () => new Promise<RuntimeSuspensionRequest>(() => undefined),
115
- ),
116
- clearStepSuspensions: jest.fn(),
117
- primeStepResumeData: jest.fn(),
118
120
  captureTaskOutput: jest.fn().mockResolvedValue(undefined),
119
121
  executeFlow: jest.fn(),
120
122
  executeStep: jest.fn(),
@@ -155,9 +157,8 @@ describe('executeTaskStep', () => {
155
157
  env.context,
156
158
  task.settings,
157
159
  task.bindings,
158
- env.signal,
160
+ expect.any(AbortSignal),
159
161
  );
160
- expect(env.setCurrentStep).toHaveBeenNthCalledWith(1, stepInfo);
161
162
  expect(env.markSnapshot).toHaveBeenNthCalledWith(1, stepInfo, 'running');
162
163
  expect(env.markSnapshot).toHaveBeenNthCalledWith(2, stepInfo, 'completed');
163
164
  expect(env.recordStepExecution).toHaveBeenNthCalledWith(
@@ -200,7 +201,6 @@ describe('executeTaskStep', () => {
200
201
  status: 'completed',
201
202
  }),
202
203
  });
203
- expect(env.setCurrentStep).toHaveBeenLastCalledWith(undefined);
204
204
  });
205
205
 
206
206
  it('throws when the task is missing', async () => {
@@ -215,43 +215,38 @@ describe('executeTaskStep', () => {
215
215
  expect(env.buildInstanceStepInfo).not.toHaveBeenCalled();
216
216
  });
217
217
 
218
- it('resumes an in-flight action and captures post-suspend output', async () => {
219
- const request: RuntimeSuspensionRequest = {
220
- stepId: step.id,
221
- stepExecId: `${step.id}#1`,
222
- suspendIndex: 1,
223
- suspendKey: 'index:1',
224
- reason: 'awaiting_user',
225
- data: { channel: 'sms' },
226
- awaitResults: {},
227
- resume: createDeferred<unknown>(),
228
- };
218
+ it('returns a frame on suspension and re-runs the body once its wait is resolved', async () => {
229
219
  const task = createTask(
230
- jest.fn().mockImplementation(async () => {
231
- const resumed = (await request.resume.promise) as { reply: string };
220
+ jest.fn().mockImplementation(async (_inputs, context, ...rest) => {
221
+ const signal = rest[2] as AbortSignal;
222
+ const resumed = (await context.workflow.suspend({
223
+ reason: 'awaiting_user',
224
+ data: { channel: 'sms' },
225
+ })) as { reply: string };
232
226
 
233
- return { reply: resumed.reply.toUpperCase() };
227
+ return { reply: resumed.reply.toUpperCase(), aborted: signal.aborted };
234
228
  }),
235
229
  );
236
230
  const compiled = createCompiled(task);
237
231
  const env = createEnv(compiled, stepInfo);
238
232
  const state = createState();
233
+ const frame = await executeTaskStep(env, step, state, []);
239
234
 
240
- (env.waitForStepSuspension as jest.Mock)
241
- .mockResolvedValueOnce(request)
242
- .mockImplementationOnce(
243
- () => new Promise<RuntimeSuspensionRequest>(() => undefined),
244
- );
245
-
246
- const suspension = await executeTaskStep(env, step, state, []);
247
-
248
- expect(suspension).toEqual(
249
- expect.objectContaining({
235
+ expect(frame).toEqual({
236
+ kind: 'task',
237
+ stepId: step.id,
238
+ inputs: { payload: 123 },
239
+ awaitResults: {},
240
+ wait: {
241
+ waitId: '0:test_task@index:1',
250
242
  step: stepInfo,
243
+ suspendKey: 'index:1',
251
244
  reason: 'awaiting_user',
252
245
  data: { channel: 'sms' },
253
- }),
254
- );
246
+ },
247
+ });
248
+ const [, , , , signal] = (task.action.run as jest.Mock).mock.calls[0];
249
+ expect((signal as AbortSignal).aborted).toBe(true);
255
250
  expect(env.markSnapshot).toHaveBeenCalledWith(
256
251
  stepInfo,
257
252
  'suspended',
@@ -260,26 +255,38 @@ describe('executeTaskStep', () => {
260
255
  expect(env.emit).toHaveBeenCalledWith('hook:step:suspended', {
261
256
  runId: 'run-123',
262
257
  step: stepInfo,
258
+ wait: (frame as TaskFrame).wait,
263
259
  stepExecution: expect.objectContaining({
264
260
  id: stepInfo.id,
265
261
  status: 'suspended',
266
262
  }),
267
- reason: 'awaiting_user',
268
- data: { channel: 'sms' },
269
263
  });
270
264
 
271
- await suspension?.continue({ reply: 'Sure' });
265
+ const resumedEnv = {
266
+ ...env,
267
+ resolutions: new Map([['0:test_task@index:1', { reply: 'Sure' }]]),
268
+ };
269
+ const result = await executeTaskStep(
270
+ resumedEnv,
271
+ step,
272
+ state,
273
+ [],
274
+ frame as TaskFrame,
275
+ );
272
276
 
277
+ expect(result).toBeUndefined();
278
+ expect(task.action.run).toHaveBeenCalledTimes(2);
273
279
  expect(env.captureTaskOutput).toHaveBeenCalledWith(task, state, {
274
280
  reply: 'SURE',
281
+ aborted: false,
282
+ });
283
+ expect(env.emit).toHaveBeenCalledWith('hook:step:resumed', {
284
+ runId: 'run-123',
285
+ step: stepInfo,
286
+ stepExecution: expect.objectContaining({ status: 'running' }),
287
+ wait: (frame as TaskFrame).wait,
288
+ resumeData: { reply: 'Sure' },
275
289
  });
276
- expect(env.recordStepExecution).toHaveBeenCalledWith(
277
- stepInfo,
278
- expect.objectContaining({
279
- status: 'completed',
280
- output: { reply: 'SURE' },
281
- }),
282
- );
283
290
  expect(env.emit).toHaveBeenCalledWith('hook:step:success', {
284
291
  runId: 'run-123',
285
292
  step: stepInfo,
@@ -290,6 +297,270 @@ describe('executeTaskStep', () => {
290
297
  });
291
298
  });
292
299
 
300
+ it('leaves a parked frame untouched when this pass does not resolve its wait', async () => {
301
+ const task = createTask(jest.fn());
302
+ const env = createEnv(createCompiled(task), stepInfo);
303
+ const frame: TaskFrame = {
304
+ kind: 'task',
305
+ stepId: step.id,
306
+ inputs: { payload: 123 },
307
+ awaitResults: {},
308
+ wait: {
309
+ waitId: '0:test_task@index:1',
310
+ step: stepInfo,
311
+ suspendKey: 'index:1',
312
+ },
313
+ };
314
+
315
+ await expect(
316
+ executeTaskStep(env, step, createState(), [], frame),
317
+ ).resolves.toBe(frame);
318
+ expect(task.action.run).not.toHaveBeenCalled();
319
+ expect(env.markSnapshot).not.toHaveBeenCalled();
320
+ });
321
+
322
+ it('re-runs a resumed body with the inputs recorded in its frame', async () => {
323
+ const task = createTask(
324
+ jest.fn().mockImplementation(async (inputs, context) => ({
325
+ inputs,
326
+ reply: await context.workflow.suspend(),
327
+ })),
328
+ );
329
+ const env = {
330
+ ...createEnv(createCompiled(task), stepInfo),
331
+ resolutions: new Map([['0:test_task@index:1', 'yes']]),
332
+ };
333
+ const state = createState();
334
+ const frame: TaskFrame = {
335
+ kind: 'task',
336
+ stepId: step.id,
337
+ // Differs from the compiled `{ payload: 123 }` on purpose.
338
+ inputs: { payload: 'recorded' },
339
+ awaitResults: {},
340
+ wait: {
341
+ waitId: '0:test_task@index:1',
342
+ step: stepInfo,
343
+ suspendKey: 'index:1',
344
+ },
345
+ };
346
+
347
+ await expect(
348
+ executeTaskStep(env, step, state, [], frame),
349
+ ).resolves.toBeUndefined();
350
+ expect(task.action.run).toHaveBeenCalledWith(
351
+ { payload: 'recorded' },
352
+ env.context,
353
+ task.settings,
354
+ task.bindings,
355
+ expect.any(AbortSignal),
356
+ );
357
+ expect(env.captureTaskOutput).toHaveBeenCalledWith(task, state, {
358
+ inputs: { payload: 'recorded' },
359
+ reply: 'yes',
360
+ });
361
+ });
362
+
363
+ it('fails a resumed body that completes without reaching its suspension', async () => {
364
+ const task = createTask(jest.fn().mockResolvedValue('done'));
365
+ const env = {
366
+ ...createEnv(createCompiled(task), stepInfo),
367
+ resolutions: new Map([['0:test_task@index:1', 'yes']]),
368
+ };
369
+ const frame: TaskFrame = {
370
+ kind: 'task',
371
+ stepId: step.id,
372
+ inputs: { payload: 123 },
373
+ awaitResults: {},
374
+ wait: {
375
+ waitId: '0:test_task@index:1',
376
+ step: stepInfo,
377
+ suspendKey: 'index:1',
378
+ },
379
+ };
380
+
381
+ await expect(
382
+ executeTaskStep(env, step, createState(), [], frame),
383
+ ).rejects.toThrow(NonDeterministicWorkflowError);
384
+ expect(env.captureTaskOutput).not.toHaveBeenCalled();
385
+ expect(env.markSnapshot).toHaveBeenLastCalledWith(
386
+ stepInfo,
387
+ 'failed',
388
+ expect.stringContaining('completed without reaching suspension'),
389
+ );
390
+ });
391
+
392
+ it('keeps the wait open when a resumed body refuses its payload', async () => {
393
+ const refusal = new WorkflowResumeRejectedError('Unknown order.');
394
+ const task = createTask(
395
+ jest.fn().mockImplementation(async (_inputs, context) => {
396
+ await context.workflow.suspend({ reason: 'awaiting_order' });
397
+ throw refusal;
398
+ }),
399
+ );
400
+ const env = {
401
+ ...createEnv(createCompiled(task), stepInfo),
402
+ resolutions: new Map([['0:test_task@index:1', 'order-404']]),
403
+ };
404
+ const frame: TaskFrame = {
405
+ kind: 'task',
406
+ stepId: step.id,
407
+ inputs: { payload: 123 },
408
+ awaitResults: {},
409
+ wait: {
410
+ waitId: '0:test_task@index:1',
411
+ step: stepInfo,
412
+ suspendKey: 'index:1',
413
+ reason: 'awaiting_order',
414
+ },
415
+ };
416
+
417
+ await expect(
418
+ executeTaskStep(env, step, createState(), [], frame),
419
+ ).resolves.toBe(frame);
420
+ expect(env.rejectResume).toHaveBeenCalledWith({
421
+ waitId: '0:test_task@index:1',
422
+ error: refusal,
423
+ });
424
+ expect(env.captureTaskOutput).not.toHaveBeenCalled();
425
+ expect(env.markSnapshot).toHaveBeenLastCalledWith(
426
+ stepInfo,
427
+ 'suspended',
428
+ 'awaiting_order',
429
+ );
430
+ expect(env.emit).toHaveBeenLastCalledWith('hook:step:resume_rejected', {
431
+ runId: 'run-123',
432
+ step: stepInfo,
433
+ stepExecution: expect.objectContaining({ status: 'suspended' }),
434
+ wait: frame.wait,
435
+ error: refusal,
436
+ });
437
+ });
438
+
439
+ it('fails a body that refuses a payload it has not read', async () => {
440
+ const refuse = jest
441
+ .fn()
442
+ .mockRejectedValue(new WorkflowResumeRejectedError('Too early.'));
443
+ const frame: TaskFrame = {
444
+ kind: 'task',
445
+ stepId: step.id,
446
+ inputs: { payload: 123 },
447
+ awaitResults: {},
448
+ wait: {
449
+ waitId: '0:test_task@index:1',
450
+ step: stepInfo,
451
+ suspendKey: 'index:1',
452
+ },
453
+ };
454
+ // Resumed, but refusing before reaching the resolved suspend() call.
455
+ const resumedTask = createTask(refuse);
456
+ const resumedEnv = {
457
+ ...createEnv(createCompiled(resumedTask), stepInfo),
458
+ resolutions: new Map([['0:test_task@index:1', 'yes']]),
459
+ };
460
+ await expect(
461
+ executeTaskStep(resumedEnv, step, createState(), [], frame),
462
+ ).rejects.toThrow('Too early.');
463
+ expect(resumedEnv.rejectResume).not.toHaveBeenCalled();
464
+ expect(resumedEnv.markSnapshot).toHaveBeenLastCalledWith(
465
+ stepInfo,
466
+ 'failed',
467
+ 'Too early.',
468
+ );
469
+
470
+ // Not resumed at all.
471
+ const freshEnv = createEnv(createCompiled(createTask(refuse)), stepInfo);
472
+ await expect(
473
+ executeTaskStep(freshEnv, step, createState(), []),
474
+ ).rejects.toThrow('Too early.');
475
+ expect(freshEnv.rejectResume).not.toHaveBeenCalled();
476
+ });
477
+
478
+ it('does not start the action when the run is already aborted', async () => {
479
+ const task = createTask(jest.fn());
480
+ const controller = new AbortController();
481
+ controller.abort(new Error('stopped'));
482
+ const env = {
483
+ ...createEnv(createCompiled(task), stepInfo),
484
+ signal: controller.signal,
485
+ };
486
+
487
+ await expect(executeTaskStep(env, step, createState(), [])).rejects.toThrow(
488
+ 'stopped',
489
+ );
490
+ expect(task.action.run).not.toHaveBeenCalled();
491
+ expect(env.markSnapshot).toHaveBeenCalledWith(
492
+ stepInfo,
493
+ 'cancelled',
494
+ 'stopped',
495
+ );
496
+ });
497
+
498
+ it('reports the wait a cancelled resume drops', async () => {
499
+ const task = createTask(jest.fn());
500
+ const controller = new AbortController();
501
+ controller.abort(new Error('stopped'));
502
+ const frame: TaskFrame = {
503
+ kind: 'task',
504
+ stepId: step.id,
505
+ inputs: { payload: 123 },
506
+ awaitResults: {},
507
+ wait: {
508
+ waitId: '0:test_task@index:1',
509
+ step: stepInfo,
510
+ suspendKey: 'index:1',
511
+ },
512
+ };
513
+ const env = {
514
+ ...createEnv(createCompiled(task), stepInfo),
515
+ signal: controller.signal,
516
+ resolutions: new Map([['0:test_task@index:1', 'yes']]),
517
+ };
518
+
519
+ await expect(
520
+ executeTaskStep(env, step, createState(), [], frame),
521
+ ).rejects.toThrow('stopped');
522
+ expect(env.emit).toHaveBeenCalledWith(
523
+ 'hook:step:cancelled',
524
+ expect.objectContaining({ step: stepInfo, wait: frame.wait }),
525
+ );
526
+ });
527
+
528
+ it('records a cancellation when the run is aborted while the action runs', async () => {
529
+ const task = createTask(
530
+ jest.fn().mockImplementation(() => new Promise(() => undefined)),
531
+ );
532
+ const controller = new AbortController();
533
+ const env = {
534
+ ...createEnv(createCompiled(task), stepInfo),
535
+ signal: controller.signal,
536
+ };
537
+ const execution = executeTaskStep(env, step, createState(), []);
538
+
539
+ await new Promise((resolve) => setTimeout(resolve, 0));
540
+ expect(task.action.run).toHaveBeenCalledTimes(1);
541
+ controller.abort(new Error('stopped'));
542
+
543
+ await expect(execution).rejects.toThrow('stopped');
544
+ expect(env.recordStepExecution).toHaveBeenLastCalledWith(
545
+ stepInfo,
546
+ expect.objectContaining({
547
+ status: 'cancelled',
548
+ error: { message: 'stopped', stack: expect.any(String) },
549
+ }),
550
+ );
551
+ expect(env.markSnapshot).toHaveBeenLastCalledWith(
552
+ stepInfo,
553
+ 'cancelled',
554
+ 'stopped',
555
+ );
556
+ expect(env.emit).toHaveBeenCalledWith('hook:step:cancelled', {
557
+ runId: 'run-123',
558
+ step: stepInfo,
559
+ stepExecution: expect.objectContaining({ status: 'cancelled' }),
560
+ error: expect.objectContaining({ message: 'stopped' }),
561
+ });
562
+ });
563
+
293
564
  it('marks failure and rethrows errors from the task', async () => {
294
565
  const task = createTask(jest.fn().mockRejectedValue(new Error('boom')));
295
566
  const compiled = createCompiled(task);
@@ -316,6 +587,5 @@ describe('executeTaskStep', () => {
316
587
  }),
317
588
  error: expect.any(Error),
318
589
  });
319
- expect(env.setCurrentStep).toHaveBeenLastCalledWith(undefined);
320
590
  });
321
591
  });