@pikku/core 0.12.38 → 0.12.39

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 (38) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/dist/errors/error-handler.d.ts +8 -0
  3. package/dist/errors/error-handler.js +8 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.js +1 -0
  6. package/dist/services/in-memory-queue-service.d.ts +9 -0
  7. package/dist/services/in-memory-queue-service.js +42 -11
  8. package/dist/services/in-memory-workflow-service.js +2 -0
  9. package/dist/services/workflow-service.d.ts +3 -0
  10. package/dist/testing/service-tests.js +35 -0
  11. package/dist/types/core.types.d.ts +1 -0
  12. package/dist/wirings/cli/cli-runner.js +12 -3
  13. package/dist/wirings/workflow/graph/graph-runner.js +51 -61
  14. package/dist/wirings/workflow/index.d.ts +2 -0
  15. package/dist/wirings/workflow/index.js +2 -0
  16. package/dist/wirings/workflow/pikku-workflow-service.d.ts +32 -0
  17. package/dist/wirings/workflow/pikku-workflow-service.js +136 -119
  18. package/dist/wirings/workflow/run-timeline.d.ts +85 -0
  19. package/dist/wirings/workflow/run-timeline.js +153 -0
  20. package/dist/wirings/workflow/workflow.types.d.ts +2 -0
  21. package/package.json +1 -1
  22. package/src/errors/error-handler.ts +10 -0
  23. package/src/index.ts +1 -0
  24. package/src/services/in-memory-queue-service.test.ts +97 -0
  25. package/src/services/in-memory-queue-service.ts +51 -16
  26. package/src/services/in-memory-workflow-service.ts +2 -0
  27. package/src/services/workflow-service.ts +9 -0
  28. package/src/testing/service-tests.ts +65 -0
  29. package/src/types/core.types.ts +4 -0
  30. package/src/wirings/cli/cli-runner.ts +11 -3
  31. package/src/wirings/workflow/graph/graph-runner.test.ts +72 -0
  32. package/src/wirings/workflow/graph/graph-runner.ts +95 -86
  33. package/src/wirings/workflow/index.ts +14 -0
  34. package/src/wirings/workflow/pikku-workflow-service.ts +193 -137
  35. package/src/wirings/workflow/run-timeline.test.ts +211 -0
  36. package/src/wirings/workflow/run-timeline.ts +241 -0
  37. package/src/wirings/workflow/workflow.types.ts +2 -0
  38. package/tsconfig.tsbuildinfo +1 -1
@@ -29,10 +29,11 @@ const resolveWorkflowMeta = (name) => {
29
29
  };
30
30
  const toKebab = (s) => s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
31
31
  import { continueGraph, executeGraphStep, runWorkflowGraph, runFromMeta, } from './graph/graph-runner.js';
32
- import { PikkuError, addError } from '../../errors/error-handler.js';
32
+ import { PikkuError, addError, isExpectedError, } from '../../errors/error-handler.js';
33
33
  import { RPCNotFoundError } from '../rpc/rpc-runner.js';
34
34
  import { ChildWorkflowStartedException } from './graph/graph-runner.js';
35
35
  import { deriveInvocationId } from './workflow-invocation-id.js';
36
+ import { buildRunTimeline, reconstructStateAt, } from './run-timeline.js';
36
37
  /**
37
38
  * Default number of retries for a workflow step when none is specified. The
38
39
  * workflow — not the queue — owns retry policy; a step inherits this unless it
@@ -293,6 +294,7 @@ export class PikkuWorkflowService {
293
294
  status: step.status,
294
295
  startedAt: step.runningAt ?? step.createdAt,
295
296
  completedAt: step.succeededAt ?? step.failedAt,
297
+ attempts: step.attemptCount,
296
298
  });
297
299
  }
298
300
  }
@@ -302,6 +304,7 @@ export class PikkuWorkflowService {
302
304
  duration: s.startedAt && s.completedAt
303
305
  ? s.completedAt.getTime() - s.startedAt.getTime()
304
306
  : undefined,
307
+ attempts: s.attempts,
305
308
  }));
306
309
  return {
307
310
  id: run.id,
@@ -317,6 +320,30 @@ export class PikkuWorkflowService {
317
320
  : undefined,
318
321
  };
319
322
  }
323
+ /**
324
+ * Build the run's time-travel event stream from durable history.
325
+ * @param id - Run ID
326
+ * @returns Ordered timeline, or null if the run doesn't exist
327
+ */
328
+ async getRunTimeline(id) {
329
+ const run = await this.getRun(id);
330
+ if (!run)
331
+ return null;
332
+ return buildRunTimeline(await this.getRunHistory(id));
333
+ }
334
+ /**
335
+ * Reconstruct the run's state at a point in its timeline.
336
+ * @param id - Run ID
337
+ * @param at - A seq index (inclusive) or a Date (inclusive); omit for the
338
+ * final state.
339
+ * @returns Reconstructed state, or null if the run doesn't exist
340
+ */
341
+ async reconstructRunStateAt(id, at) {
342
+ const timeline = await this.getRunTimeline(id);
343
+ if (!timeline)
344
+ return null;
345
+ return reconstructStateAt(timeline, at ?? timeline.length - 1);
346
+ }
320
347
  /**
321
348
  * Update workflow run status
322
349
  * @param id - Run ID
@@ -390,6 +417,7 @@ export class PikkuWorkflowService {
390
417
  message: error.message,
391
418
  stack: error.stack,
392
419
  code: error.code,
420
+ expected: isExpectedError(error),
393
421
  };
394
422
  await this.safeMirror(() => this.mirror.setStepError(stepId, serialized));
395
423
  }
@@ -626,7 +654,12 @@ export class PikkuWorkflowService {
626
654
  message: error.message,
627
655
  stack: error.stack,
628
656
  });
629
- getSingletonServices().logger.error(`Workflow ${name} (run ${runId}) failed:`, error);
657
+ // An expected failure (a PikkuError, e.g. a build gate tripping)
658
+ // its message is the whole story, so don't dump the stack. The
659
+ // `expected` flag survives the step-boundary rehydration that strips
660
+ // the class. Anything else is an uncaught/unexpected error: log it in
661
+ // full so the trace is there to debug.
662
+ getSingletonServices().logger.error(`Workflow ${name} (run ${runId}) failed:`, isExpectedError(error) ? error.message : error);
630
663
  throw error;
631
664
  }
632
665
  if (error.name === 'WorkflowDispatchException') {
@@ -924,17 +957,7 @@ export class PikkuWorkflowService {
924
957
  }
925
958
  }
926
959
  else {
927
- result = await rpcService.rpcWithWire(rpcName, data, {
928
- workflowStep: {
929
- runId,
930
- stepId: stepState.stepId,
931
- invocationId: deriveInvocationId(runId, stepName),
932
- attemptCount: stepState.attemptCount,
933
- fromInvocationId: stepState.fromStepName
934
- ? deriveInvocationId(runId, stepState.fromStepName)
935
- : undefined,
936
- },
937
- });
960
+ result = await this.invokeStepRpc(runId, stepName, stepState, rpcName, data, rpcService);
938
961
  }
939
962
  }
940
963
  // Store result and mark succeeded
@@ -1004,6 +1027,63 @@ export class PikkuWorkflowService {
1004
1027
  }
1005
1028
  return getSingletonServices().queueService;
1006
1029
  }
1030
+ /**
1031
+ * Invoke a step's RPC with the workflow-step wire (step identity + provenance).
1032
+ * Identical for the queue executor and the inline executor — the only thing
1033
+ * that differs between transports is who calls it, not the call itself.
1034
+ */
1035
+ async invokeStepRpc(runId, stepName, stepState, rpcName, data, rpcService) {
1036
+ return rpcService.rpcWithWire(rpcName, data, {
1037
+ workflowStep: {
1038
+ runId,
1039
+ stepId: stepState.stepId,
1040
+ invocationId: deriveInvocationId(runId, stepName),
1041
+ attemptCount: stepState.attemptCount,
1042
+ fromInvocationId: stepState.fromStepName
1043
+ ? deriveInvocationId(runId, stepState.fromStepName)
1044
+ : undefined,
1045
+ },
1046
+ });
1047
+ }
1048
+ /**
1049
+ * Inline (straight-through) step execution with an in-process retry loop —
1050
+ * shared by inline RPC steps and inline function steps. Same scaffolding
1051
+ * (running → result, or fail → retry-attempt → backoff → retry) wrapped
1052
+ * around a step-specific `doWork` body. Stays O(K): no suspend/replay.
1053
+ *
1054
+ * `onError` is an optional hook for terminal errors that must NOT retry
1055
+ * (e.g. RPC-not-found → suspend the run for redeploy). If it throws, the
1056
+ * loop exits immediately without recording a step error or retrying.
1057
+ */
1058
+ async runInlineRetryLoop(stepState, retries, retryDelay, doWork, onError) {
1059
+ let currentStepState = stepState;
1060
+ while (true) {
1061
+ try {
1062
+ await this.setStepRunning(currentStepState.stepId);
1063
+ const result = await doWork(currentStepState);
1064
+ await this.setStepResult(currentStepState.stepId, result);
1065
+ return result;
1066
+ }
1067
+ catch (error) {
1068
+ if (onError)
1069
+ await onError(error);
1070
+ // Record the error (marks step as failed)
1071
+ await this.setStepError(currentStepState.stepId, error);
1072
+ if (currentStepState.attemptCount < retries) {
1073
+ // Create a new pending retry attempt, then back off if configured.
1074
+ currentStepState = await this.createRetryAttempt(currentStepState.stepId, 'pending');
1075
+ if (retryDelay) {
1076
+ await new Promise((resolve) => setTimeout(resolve, getDurationInMilliseconds(retryDelay)));
1077
+ }
1078
+ // Continue loop to retry
1079
+ }
1080
+ else {
1081
+ // No more retries, fail the workflow
1082
+ throw error;
1083
+ }
1084
+ }
1085
+ }
1086
+ }
1007
1087
  async rpcStep(runId, logicalStepName, rpcName, data, rpcService, stepOptions) {
1008
1088
  // Capture the predecessor before nextStepKey advances the lineage to us.
1009
1089
  const fromStepName = this.lastStepName(runId);
@@ -1055,88 +1135,50 @@ export class PikkuWorkflowService {
1055
1135
  await this.setStepScheduled(stepState.stepId);
1056
1136
  throw new WorkflowAsyncException(runId, stepName);
1057
1137
  }
1058
- {
1059
- // Inline (no transport available) - execute locally with retry loop
1060
- const retries = resolvedStepOptions.retries ?? this.getConfig().retries;
1061
- const retryDelay = resolvedStepOptions.retryDelay;
1062
- let currentStepState = stepState;
1063
- while (true) {
1064
- try {
1065
- await this.setStepRunning(currentStepState.stepId);
1066
- // Check if the name refers to a workflow
1067
- const workflowMeta = pikkuState(null, 'workflows', 'meta')[rpcName];
1068
- let result;
1069
- if (workflowMeta) {
1070
- const childWire = {
1071
- type: 'workflow',
1072
- id: rpcName,
1073
- parentRunId: runId,
1074
- pikkuUserId: rpcService.wire?.pikkuUserId,
1075
- };
1076
- const { runId: childRunId } = await this.startWorkflow(rpcName, data, childWire, rpcService, { inline: true });
1077
- await this.setStepChildRunId(currentStepState.stepId, childRunId);
1078
- // Poll until child workflow completes
1079
- while (true) {
1080
- const childRun = await this.getRun(childRunId);
1081
- if (!childRun) {
1082
- throw new WorkflowRunNotFoundError(childRunId);
1083
- }
1084
- if (WORKFLOW_END_STATES.has(childRun.status)) {
1085
- if (childRun.status === 'failed') {
1086
- throw new Error(childRun.error?.message || 'Sub-workflow failed');
1087
- }
1088
- if (childRun.status === 'cancelled') {
1089
- throw new Error('Sub-workflow was cancelled');
1090
- }
1091
- result = childRun.output;
1092
- break;
1093
- }
1094
- await new Promise((resolve) => setTimeout(resolve, 500));
1095
- }
1096
- }
1097
- else {
1098
- result = await rpcService.rpcWithWire(rpcName, data, {
1099
- workflowStep: {
1100
- runId,
1101
- stepId: currentStepState.stepId,
1102
- invocationId: deriveInvocationId(runId, stepName),
1103
- attemptCount: currentStepState.attemptCount,
1104
- fromInvocationId: currentStepState.fromStepName
1105
- ? deriveInvocationId(runId, currentStepState.fromStepName)
1106
- : undefined,
1107
- },
1108
- });
1109
- }
1110
- await this.setStepResult(currentStepState.stepId, result);
1111
- return result;
1112
- }
1113
- catch (error) {
1114
- if (error instanceof RPCNotFoundError) {
1115
- await this.updateRunStatus(runId, 'suspended', undefined, {
1116
- message: `RPC '${rpcName}' not found. Deploy the missing function and resume.`,
1117
- code: 'RPC_NOT_FOUND',
1118
- });
1119
- throw error;
1138
+ // Inline (no transport available) - execute locally with the shared retry
1139
+ // loop. The body resolves to a sub-workflow result or a plain RPC result.
1140
+ const retries = resolvedStepOptions.retries ?? this.getConfig().retries;
1141
+ const retryDelay = resolvedStepOptions.retryDelay;
1142
+ return this.runInlineRetryLoop(stepState, retries, retryDelay, async (currentStepState) => {
1143
+ // Check if the name refers to a workflow
1144
+ const workflowMeta = pikkuState(null, 'workflows', 'meta')[rpcName];
1145
+ if (workflowMeta) {
1146
+ const childWire = {
1147
+ type: 'workflow',
1148
+ id: rpcName,
1149
+ parentRunId: runId,
1150
+ pikkuUserId: rpcService.wire?.pikkuUserId,
1151
+ };
1152
+ const { runId: childRunId } = await this.startWorkflow(rpcName, data, childWire, rpcService, { inline: true });
1153
+ await this.setStepChildRunId(currentStepState.stepId, childRunId);
1154
+ // Poll until child workflow completes
1155
+ while (true) {
1156
+ const childRun = await this.getRun(childRunId);
1157
+ if (!childRun) {
1158
+ throw new WorkflowRunNotFoundError(childRunId);
1120
1159
  }
1121
- // Record the error (marks step as failed)
1122
- await this.setStepError(currentStepState.stepId, error);
1123
- // Check if we should retry
1124
- if (currentStepState.attemptCount < retries) {
1125
- // Create a new pending retry attempt
1126
- currentStepState = await this.createRetryAttempt(currentStepState.stepId, 'pending');
1127
- // Wait for retry delay if specified
1128
- if (retryDelay) {
1129
- await new Promise((resolve) => setTimeout(resolve, getDurationInMilliseconds(retryDelay)));
1160
+ if (WORKFLOW_END_STATES.has(childRun.status)) {
1161
+ if (childRun.status === 'failed') {
1162
+ throw new Error(childRun.error?.message || 'Sub-workflow failed');
1130
1163
  }
1131
- // Continue loop to retry
1132
- }
1133
- else {
1134
- // No more retries, fail the workflow
1135
- throw error;
1164
+ if (childRun.status === 'cancelled') {
1165
+ throw new Error('Sub-workflow was cancelled');
1166
+ }
1167
+ return childRun.output;
1136
1168
  }
1169
+ await new Promise((resolve) => setTimeout(resolve, 500));
1137
1170
  }
1138
1171
  }
1139
- }
1172
+ return this.invokeStepRpc(runId, stepName, currentStepState, rpcName, data, rpcService);
1173
+ }, async (error) => {
1174
+ if (error instanceof RPCNotFoundError) {
1175
+ await this.updateRunStatus(runId, 'suspended', undefined, {
1176
+ message: `RPC '${rpcName}' not found. Deploy the missing function and resume.`,
1177
+ code: 'RPC_NOT_FOUND',
1178
+ });
1179
+ throw error;
1180
+ }
1181
+ });
1140
1182
  }
1141
1183
  async inlineStep(runId, logicalStepName, fn, stepOptions) {
1142
1184
  const fromStepName = this.lastStepName(runId);
@@ -1157,39 +1199,14 @@ export class PikkuWorkflowService {
1157
1199
  // Execute inline function
1158
1200
  const retries = stepOptions?.retries ?? this.getConfig().retries;
1159
1201
  const retryDelay = stepOptions?.retryDelay ?? this.getConfig().retryDelay;
1160
- let currentStepState = stepState;
1161
1202
  // Check if we're running inline (in-memory) or remote (queue-based)
1162
1203
  if (this.isInline(runId)) {
1163
- // Inline mode - execute with retry loop
1164
- while (true) {
1165
- try {
1166
- await this.setStepRunning(currentStepState.stepId);
1167
- const result = await fn();
1168
- await this.setStepResult(currentStepState.stepId, result);
1169
- return result;
1170
- }
1171
- catch (error) {
1172
- // Record the error (marks step as failed)
1173
- await this.setStepError(currentStepState.stepId, error);
1174
- // Check if we should retry
1175
- if (currentStepState.attemptCount < retries) {
1176
- // Create a new pending retry attempt
1177
- currentStepState = await this.createRetryAttempt(currentStepState.stepId, 'pending');
1178
- // Wait for retry delay if specified
1179
- if (retryDelay) {
1180
- await new Promise((resolve) => setTimeout(resolve, getDurationInMilliseconds(retryDelay)));
1181
- }
1182
- // Continue loop to retry
1183
- }
1184
- else {
1185
- // No more retries, fail the workflow
1186
- throw error;
1187
- }
1188
- }
1189
- }
1204
+ // Inline mode - execute with the shared in-process retry loop.
1205
+ return this.runInlineRetryLoop(stepState, retries, retryDelay, () => fn());
1190
1206
  }
1191
1207
  else {
1192
- // Remote mode - use queue-based retry
1208
+ // Remote mode - single attempt, then suspend for orchestrator-driven retry.
1209
+ let currentStepState = stepState;
1193
1210
  try {
1194
1211
  await this.setStepRunning(currentStepState.stepId);
1195
1212
  const result = await fn();
@@ -0,0 +1,85 @@
1
+ import type { SerializedError } from '../../types/core.types.js';
2
+ import type { StepState, StepStatus } from './workflow.types.js';
3
+ /**
4
+ * Time-travel over a workflow run.
5
+ *
6
+ * A run's durable history (`getRunHistory`) is one row per step *attempt*, each
7
+ * carrying lifecycle timestamps (created/scheduled/running/succeeded/failed).
8
+ * `buildRunTimeline` explodes those rows into a flat, chronologically-ordered
9
+ * event stream, and `reconstructStateAt` folds the stream up to any point to
10
+ * recover what the run "knew" then — the same step cache a replay would hold:
11
+ * per-step status, accumulated results, and the walked path.
12
+ *
13
+ * These are pure functions over history — no IO — so they're trivially testable
14
+ * and transport-independent (the same fold works for Redis/Kysely/in-memory).
15
+ */
16
+ /** A single observable transition of one step attempt. */
17
+ export interface RunTimelineEvent {
18
+ /** Monotonic 0-based position in the timeline. */
19
+ seq: number;
20
+ /** When it happened. */
21
+ at: Date;
22
+ /** The step's new status at this event (matches StepStatus lifecycle). */
23
+ type: Extract<StepStatus, 'pending' | 'scheduled' | 'running' | 'succeeded' | 'failed'>;
24
+ /** Physical step name (includes revisit ordinal, e.g. `attempt#2`). */
25
+ stepName: string;
26
+ /** Which attempt this event belongs to (1-based). */
27
+ attemptCount: number;
28
+ /** Predecessor that scheduled this step — only on the `pending` (created)
29
+ * event; undefined for entry steps. Reconstructs the walked edge. */
30
+ fromStepName?: string;
31
+ /** Result snapshot — only on `succeeded`. */
32
+ result?: unknown;
33
+ /** Error snapshot — only on `failed`. */
34
+ error?: SerializedError;
35
+ }
36
+ export type RunTimeline = RunTimelineEvent[];
37
+ type HistoryEntry = StepState & {
38
+ stepName: string;
39
+ };
40
+ /**
41
+ * Build the ordered event stream for a run from its raw history.
42
+ *
43
+ * History is expected oldest-first but is sorted defensively by (timestamp,
44
+ * lifecycle, original index) so simultaneous timestamps stay deterministic.
45
+ */
46
+ export declare function buildRunTimeline(history: HistoryEntry[]): RunTimeline;
47
+ /** A step's reconstructed state at a point in the timeline. */
48
+ export interface ReconstructedStep {
49
+ stepName: string;
50
+ status: StepStatus;
51
+ attemptCount: number;
52
+ fromStepName?: string;
53
+ result?: unknown;
54
+ error?: SerializedError;
55
+ }
56
+ /** Coarse run phase derived purely from step states (not the run's output). */
57
+ export type RunPhase = 'pending' | 'running' | 'failed' | 'idle';
58
+ /** The whole run's reconstructed state at a point in the timeline. */
59
+ export interface ReconstructedRunState {
60
+ /** seq of the last applied event, or -1 if the point precedes all events. */
61
+ seq: number;
62
+ /** Wall-clock time of the last applied event. */
63
+ at?: Date;
64
+ /** Per-step latest state, in first-seen (walked) order. */
65
+ steps: ReconstructedStep[];
66
+ /** Outputs available to downstream steps — the replay step cache at this
67
+ * point (succeeded steps only). */
68
+ results: Record<string, unknown>;
69
+ /** Step names in the order they were first created — the walked path. */
70
+ path: string[];
71
+ /** Derived phase: `running` if any step is in-flight, else `failed` if a step
72
+ * is failed with no in-flight work, else `idle` (between transitions). */
73
+ phase: RunPhase;
74
+ }
75
+ /**
76
+ * Fold the timeline up to `at` and return the run's state at that point.
77
+ *
78
+ * `at` is either a seq index (inclusive — apply events `0..at`) or a `Date`
79
+ * (inclusive — apply every event at or before that instant). A point before the
80
+ * first event yields the empty initial state.
81
+ */
82
+ export declare function reconstructStateAt(timeline: RunTimeline, at: number | Date): ReconstructedRunState;
83
+ /** Reconstruct the final state (fold the whole timeline). */
84
+ export declare function reconstructFinalState(timeline: RunTimeline): ReconstructedRunState;
85
+ export {};
@@ -0,0 +1,153 @@
1
+ /** Lifecycle tiebreak for events that share a timestamp. */
2
+ const LIFECYCLE_ORDER = {
3
+ pending: 0,
4
+ scheduled: 1,
5
+ running: 2,
6
+ succeeded: 3,
7
+ failed: 3,
8
+ };
9
+ /**
10
+ * Build the ordered event stream for a run from its raw history.
11
+ *
12
+ * History is expected oldest-first but is sorted defensively by (timestamp,
13
+ * lifecycle, original index) so simultaneous timestamps stay deterministic.
14
+ */
15
+ export function buildRunTimeline(history) {
16
+ const raw = [];
17
+ history.forEach((entry, order) => {
18
+ const base = {
19
+ stepName: entry.stepName,
20
+ attemptCount: entry.attemptCount,
21
+ order,
22
+ };
23
+ // The created event always exists; it carries provenance.
24
+ raw.push({
25
+ ...base,
26
+ at: entry.createdAt,
27
+ type: 'pending',
28
+ fromStepName: entry.fromStepName,
29
+ });
30
+ // Intermediate events are optional enrichment — emit only if the backend
31
+ // recorded their timestamp.
32
+ if (entry.scheduledAt) {
33
+ raw.push({ ...base, at: entry.scheduledAt, type: 'scheduled' });
34
+ }
35
+ if (entry.runningAt) {
36
+ raw.push({ ...base, at: entry.runningAt, type: 'running' });
37
+ }
38
+ // The terminal event is driven by the row's authoritative status (the
39
+ // lifecycle timestamps aren't populated by every backend — Kysely leaves
40
+ // them null), falling back to updatedAt when the specific stamp is absent.
41
+ if (entry.status === 'succeeded') {
42
+ raw.push({
43
+ ...base,
44
+ at: entry.succeededAt ?? entry.updatedAt,
45
+ type: 'succeeded',
46
+ result: entry.result,
47
+ });
48
+ }
49
+ else if (entry.status === 'failed') {
50
+ raw.push({
51
+ ...base,
52
+ at: entry.failedAt ?? entry.updatedAt,
53
+ type: 'failed',
54
+ error: entry.error,
55
+ });
56
+ }
57
+ });
58
+ raw.sort((a, b) => {
59
+ const ta = a.at.getTime();
60
+ const tb = b.at.getTime();
61
+ if (ta !== tb)
62
+ return ta - tb;
63
+ const la = LIFECYCLE_ORDER[a.type];
64
+ const lb = LIFECYCLE_ORDER[b.type];
65
+ if (la !== lb)
66
+ return la - lb;
67
+ return a.order - b.order;
68
+ });
69
+ return raw.map(({ order: _order, ...event }, seq) => ({ ...event, seq }));
70
+ }
71
+ const IN_FLIGHT = new Set([
72
+ 'pending',
73
+ 'scheduled',
74
+ 'running',
75
+ ]);
76
+ /**
77
+ * Fold the timeline up to `at` and return the run's state at that point.
78
+ *
79
+ * `at` is either a seq index (inclusive — apply events `0..at`) or a `Date`
80
+ * (inclusive — apply every event at or before that instant). A point before the
81
+ * first event yields the empty initial state.
82
+ */
83
+ export function reconstructStateAt(timeline, at) {
84
+ const cutoff = (event) => typeof at === 'number'
85
+ ? event.seq <= at
86
+ : event.at.getTime() <= at.getTime();
87
+ const steps = new Map();
88
+ const path = [];
89
+ let lastSeq = -1;
90
+ let lastAt;
91
+ for (const event of timeline) {
92
+ if (!cutoff(event))
93
+ break;
94
+ lastSeq = event.seq;
95
+ lastAt = event.at;
96
+ let step = steps.get(event.stepName);
97
+ if (!step) {
98
+ step = {
99
+ stepName: event.stepName,
100
+ status: event.type,
101
+ attemptCount: event.attemptCount,
102
+ fromStepName: event.fromStepName,
103
+ };
104
+ steps.set(event.stepName, step);
105
+ path.push(event.stepName);
106
+ }
107
+ step.status = event.type;
108
+ step.attemptCount = event.attemptCount;
109
+ if (event.fromStepName !== undefined) {
110
+ step.fromStepName = event.fromStepName;
111
+ }
112
+ // A retry's created event reopens the step — drop the prior outcome.
113
+ if (event.type === 'pending') {
114
+ delete step.result;
115
+ delete step.error;
116
+ }
117
+ if (event.type === 'succeeded') {
118
+ step.result = event.result;
119
+ step.error = undefined;
120
+ }
121
+ if (event.type === 'failed') {
122
+ step.error = event.error;
123
+ }
124
+ }
125
+ const orderedSteps = path.map((name) => steps.get(name));
126
+ const results = {};
127
+ for (const step of orderedSteps) {
128
+ if (step.status === 'succeeded') {
129
+ results[step.stepName] = step.result;
130
+ }
131
+ }
132
+ return {
133
+ seq: lastSeq,
134
+ at: lastAt,
135
+ steps: orderedSteps,
136
+ results,
137
+ path,
138
+ phase: derivePhase(orderedSteps),
139
+ };
140
+ }
141
+ /** Reconstruct the final state (fold the whole timeline). */
142
+ export function reconstructFinalState(timeline) {
143
+ return reconstructStateAt(timeline, timeline.length - 1);
144
+ }
145
+ function derivePhase(steps) {
146
+ if (steps.length === 0)
147
+ return 'pending';
148
+ if (steps.some((s) => IN_FLIGHT.has(s.status)))
149
+ return 'running';
150
+ if (steps.some((s) => s.status === 'failed'))
151
+ return 'failed';
152
+ return 'idle';
153
+ }
@@ -112,6 +112,8 @@ export interface WorkflowRunStatus {
112
112
  name: string;
113
113
  status: StepStatus;
114
114
  duration?: number;
115
+ /** Number of attempts for this step (1 = first try; > 1 means it retried). */
116
+ attempts?: number;
115
117
  }>;
116
118
  output?: unknown;
117
119
  error?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/core",
3
- "version": "0.12.38",
3
+ "version": "0.12.39",
4
4
  "author": "yasser.fadl@gmail.com",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -16,6 +16,16 @@ export class PikkuError extends Error {
16
16
  }
17
17
  }
18
18
 
19
+ /**
20
+ * Whether an error is a deliberate, expected failure rather than an uncaught
21
+ * bug. A `PikkuError` always counts; so does any error carrying `expected:
22
+ * true` — used to keep the marker alive when an error is serialized across a
23
+ * workflow step boundary and rehydrated as a plain `Error`. Callers log the
24
+ * message alone for expected errors and the full stack for everything else.
25
+ */
26
+ export const isExpectedError = (error: unknown): boolean =>
27
+ error instanceof PikkuError || (error as any)?.expected === true
28
+
19
29
  /**
20
30
  * Interface for error details.
21
31
  */
package/src/index.ts CHANGED
@@ -93,6 +93,7 @@ export type {
93
93
  export { runQueueJob } from './wirings/queue/queue-runner.js'
94
94
  export { runScheduledTask } from './wirings/scheduler/scheduler-runner.js'
95
95
  export { NotFoundError } from './errors/errors.js'
96
+ export { PikkuError, isExpectedError } from './errors/error-handler.js'
96
97
  export type { EventHubService } from './wirings/channel/eventhub-service.js'
97
98
  export type { QueueService } from './wirings/queue/queue.types.js'
98
99
  export type { JWTService } from './services/jwt-service.js'