@gobing-ai/ts-dual-workflow-engine 0.4.68 → 0.5.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.
package/src/types.ts CHANGED
@@ -5,7 +5,29 @@ import type { EventBus } from '@gobing-ai/ts-infra';
5
5
  import type { WorkflowEngineEvents } from './events';
6
6
 
7
7
  /** Workflow execution status persisted for runs and phases. */
8
- export type WorkflowStatus = 'running' | 'done' | 'failed' | 'paused';
8
+ export type WorkflowStatus = 'running' | 'done' | 'failed' | 'paused' | 'interrupted';
9
+
10
+ /**
11
+ * How a resume re-enters the current state (task 0902 R1):
12
+ * - `skip-enter`: on-enter actions are NOT re-executed (pause semantics — they
13
+ * completed before the pause was declared).
14
+ * - `rerun-enter`: on-enter actions ARE re-executed (interrupt semantics — they
15
+ * may have half-completed at the interruption point). Only legal into states
16
+ * explicitly marked `resumeRerun: true`.
17
+ */
18
+ export type WorkflowResumeMode = 'skip-enter' | 'rerun-enter';
19
+
20
+ /**
21
+ * Ownership claim for a resume (task 0902 R3). The adapter CAS records the
22
+ * attempt at the actual status mutation, so stale/concurrent resumers lose the
23
+ * claim deterministically instead of double-driving one run.
24
+ */
25
+ export interface ResumeOwnership {
26
+ /** Unique id for this resume attempt (crypto.randomUUID() when caller omits it). */
27
+ readonly attemptId: string;
28
+ /** Optional OS pid of the resuming process, for crash-reconciliation diagnostics. */
29
+ readonly pid?: number;
30
+ }
9
31
 
10
32
  /** Runtime variables and user variables available to workflow definitions. */
11
33
  export type Vars = Record<string, string>;
@@ -49,6 +71,13 @@ export interface StateDef {
49
71
  readonly onExit?: readonly ActionDef[];
50
72
  /** When true, the engine pauses the run at this state instead of auto-advancing. */
51
73
  readonly pause?: boolean;
74
+ /**
75
+ * Author declaration that this state's on-enter actions are safe to re-run
76
+ * after an interruption (idempotent, or deduped by runId+state context).
77
+ * Required for `rerun-enter` resume; unmarked states are refused loudly
78
+ * before any action executes (task 0902 R1/R2).
79
+ */
80
+ readonly resumeRerun?: boolean;
52
81
  }
53
82
 
54
83
  /** One transition in a state-machine workflow. */
@@ -95,6 +124,8 @@ export interface FlowNodeDef {
95
124
  readonly action?: ActionDef;
96
125
  /** When true, the engine pauses the run at this node instead of auto-advancing. */
97
126
  readonly pause?: boolean;
127
+ /** Author declaration that this node's action is safe to re-run after an interruption. See StateDef.resumeRerun. */
128
+ readonly resumeRerun?: boolean;
98
129
  }
99
130
 
100
131
  /** Transition-flow edge definition. */
@@ -202,6 +233,19 @@ export interface WorkflowRunOptions {
202
233
  readonly dryRun?: boolean;
203
234
  /** Optional caller-supplied external key, unique per workflow definition. */
204
235
  readonly externalKey?: string;
236
+ /**
237
+ * Resume semantics override (task 0902). Only meaningful on driver/service
238
+ * resume paths; fresh runs ignore it. Defaults derive from run status:
239
+ * paused → 'skip-enter', interrupted → 'rerun-enter'. Interrupted runs may
240
+ * downgrade to 'skip-enter' explicitly when re-execution is provably unsafe.
241
+ */
242
+ readonly resumeMode?: WorkflowResumeMode;
243
+ /**
244
+ * Ownership claim recorded atomically at the resume status flip (task 0902 R3).
245
+ * Omitted → a fresh attemptId is generated. Concurrent resumers: exactly one
246
+ * wins the CAS, the rest receive a typed resume error.
247
+ */
248
+ readonly resumeOwner?: ResumeOwnership;
205
249
  }
206
250
 
207
251
  /** Result returned by both driver loops. */
@@ -226,6 +270,12 @@ export interface WorkflowRunRecord {
226
270
  readonly metadata_json: string;
227
271
  /** Optional caller-supplied external key, unique per workflow definition. */
228
272
  readonly external_key?: string | null;
273
+ /** Attempt id of the last resume that won the ownership claim (task 0902 R3). */
274
+ readonly owner_attempt?: string | null;
275
+ /** OS pid recorded alongside the winning resume attempt, when supplied. */
276
+ readonly owner_pid?: number | null;
277
+ /** Reason recorded by the last interruption (task 0902 R2). */
278
+ readonly interrupt_reason?: string | null;
229
279
  }
230
280
 
231
281
  /** Result of force-setting the current state of a run. */
@@ -338,4 +388,22 @@ export interface WorkflowPersistenceAdapter {
338
388
  loadLatestStateSnapshot(runId: string): Promise<{ state: string; data: Record<string, unknown> } | undefined>;
339
389
  /** List runs with status 'paused'. Optional filters: workflow name, limit. Ordered most-recent-first. */
340
390
  listPausedRuns(options?: { workflowName?: string; limit?: number }): Promise<readonly WorkflowRunRecord[]>;
391
+ /**
392
+ * Atomically claim ownership of a resumable run (task 0902 R3). CAS: flips
393
+ * the run from one of `expectedStatuses` to 'running' and records the owner
394
+ * in the same update. Returns the claimed record, or undefined when the run
395
+ * is missing, already running/done, or was claimed by a competing resume —
396
+ * callers must treat undefined as a lost race, never retry blindly.
397
+ */
398
+ claimRunOwnership(
399
+ runId: string,
400
+ owner: ResumeOwnership,
401
+ expectedStatuses: readonly ('paused' | 'interrupted')[],
402
+ ): Promise<WorkflowRunRecord | undefined>;
403
+ /**
404
+ * Mark a running run as interrupted with a reason (task 0902 R2) —
405
+ * crash/lost-owner reconciliation. CAS running → 'interrupted'; returns the
406
+ * updated record, or undefined when the run is missing or not running.
407
+ */
408
+ interruptRun(runId: string, reason: string): Promise<WorkflowRunRecord | undefined>;
341
409
  }