@aikirun/workflow 0.20.0 → 0.22.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 (2) hide show
  1. package/dist/index.js +25 -41
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -529,7 +529,6 @@ function createEventMulticaster(workflowName, workflowVersionId, eventName, sche
529
529
  // run/handle.ts
530
530
  import { INTERNAL as INTERNAL2 } from "@aikirun/types/symbols";
531
531
  import {
532
- isTerminalWorkflowRunStatus,
533
532
  WorkflowRunNotExecutableError,
534
533
  WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError2
535
534
  } from "@aikirun/types/workflow-run";
@@ -569,9 +568,6 @@ var WorkflowRunHandleImpl = class {
569
568
  const { run: currentRun } = await this.api.workflowRun.getByIdV1({ id: this.run.id });
570
569
  this._run = currentRun;
571
570
  }
572
- // TODO: instead checking the current state, use the transition history
573
- // because it is possible for a workflow to flash though a state
574
- // and the handle will never know that the workflow hit that state
575
571
  async waitForStatus(status, options) {
576
572
  return this.waitForStatusByPolling(status, options);
577
573
  }
@@ -585,49 +581,37 @@ var WorkflowRunHandleImpl = class {
585
581
  const delayMs = options?.interval ? toMilliseconds(options.interval) : 1e3;
586
582
  const maxAttempts = options?.timeout ? Math.ceil(toMilliseconds(options.timeout) / delayMs) : Number.POSITIVE_INFINITY;
587
583
  const retryStrategy = { type: "fixed", maxAttempts, delayMs };
588
- const loadState = async () => {
589
- await this.refresh();
590
- return this.run.state;
584
+ const afterStateTransitionId = this._run.stateTransitionId;
585
+ const hasTerminated = async () => {
586
+ const { terminated } = await this.api.workflowRun.hasTerminatedV1({
587
+ id: this._run.id,
588
+ afterStateTransitionId
589
+ });
590
+ return terminated;
591
591
  };
592
- const isNeitherExpectedNorTerminal = (state) => state.status !== expectedStatus && !isTerminalWorkflowRunStatus(state.status);
593
- if (!Number.isFinite(maxAttempts) && !options?.abortSignal) {
594
- const maybeResult2 = await withRetry(loadState, retryStrategy, {
595
- shouldRetryOnResult: async (state) => isNeitherExpectedNorTerminal(state)
596
- }).run();
597
- if (maybeResult2.state === "timeout") {
592
+ const shouldRetryOnResult = async (terminated) => !terminated;
593
+ const maybeResult = options?.abortSignal ? await withRetry(hasTerminated, retryStrategy, {
594
+ abortSignal: options.abortSignal,
595
+ shouldRetryOnResult
596
+ }).run() : await withRetry(hasTerminated, retryStrategy, { shouldRetryOnResult }).run();
597
+ if (maybeResult.state === "timeout") {
598
+ if (!Number.isFinite(maxAttempts)) {
598
599
  throw new Error("Something's wrong, this should've never timed out");
599
600
  }
600
- if (maybeResult2.result.status !== expectedStatus) {
601
- return {
602
- success: false,
603
- cause: "run_terminated"
604
- };
605
- }
606
- return {
607
- success: true,
608
- state: maybeResult2.result
609
- };
601
+ return { success: false, cause: "timeout" };
610
602
  }
611
- const maybeResult = options?.abortSignal ? await withRetry(loadState, retryStrategy, {
612
- abortSignal: options.abortSignal,
613
- shouldRetryOnResult: async (state) => isNeitherExpectedNorTerminal(state)
614
- }).run() : await withRetry(loadState, retryStrategy, {
615
- shouldRetryOnResult: async (state) => isNeitherExpectedNorTerminal(state)
616
- }).run();
617
- this.logger.info("Maybe result", { maybeResult });
618
- if (maybeResult.state === "completed") {
619
- if (maybeResult.result.status !== expectedStatus) {
620
- return {
621
- success: false,
622
- cause: "run_terminated"
623
- };
624
- }
603
+ if (maybeResult.state === "aborted") {
604
+ return { success: false, cause: "aborted" };
605
+ }
606
+ maybeResult.state;
607
+ await this.refresh();
608
+ if (this._run.state.status === expectedStatus) {
625
609
  return {
626
610
  success: true,
627
- state: maybeResult.result
611
+ state: this._run.state
628
612
  };
629
613
  }
630
- return { success: false, cause: maybeResult.state };
614
+ return { success: false, cause: "run_terminated" };
631
615
  }
632
616
  async cancel(reason) {
633
617
  await this.transitionState({ status: "cancelled", reason });
@@ -930,7 +914,7 @@ import {
930
914
  // run/handle-child.ts
931
915
  import { INTERNAL as INTERNAL4 } from "@aikirun/types/symbols";
932
916
  import {
933
- isTerminalWorkflowRunStatus as isTerminalWorkflowRunStatus2,
917
+ isTerminalWorkflowRunStatus,
934
918
  WorkflowRunRevisionConflictError as WorkflowRunRevisionConflictError4,
935
919
  WorkflowRunSuspendedError as WorkflowRunSuspendedError3
936
920
  } from "@aikirun/types/workflow-run";
@@ -978,7 +962,7 @@ function createStatusWaiter(handle, parentRun, childWorkflowRunWaitQueues, logge
978
962
  state: existingChildWorkflowRunWait.childWorkflowRunState
979
963
  };
980
964
  }
981
- if (isTerminalWorkflowRunStatus2(childWorkflowRunStatus)) {
965
+ if (isTerminalWorkflowRunStatus(childWorkflowRunStatus)) {
982
966
  logger.debug("Child workflow run reached termnial state", {
983
967
  "aiki.childWorkflowTerminalStatus": childWorkflowRunStatus
984
968
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aikirun/workflow",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "Workflow SDK for Aiki - define durable workflows with tasks, sleeps, waits, and event handling",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,7 @@
18
18
  "build": "tsup"
19
19
  },
20
20
  "dependencies": {
21
- "@aikirun/types": "0.20.0",
21
+ "@aikirun/types": "0.22.0",
22
22
  "@standard-schema/spec": "^1.1.0"
23
23
  },
24
24
  "publishConfig": {