@smithers-orchestrator/engine 0.16.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 (90) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +50 -0
  3. package/src/AlertHumanRequestOptions.ts +8 -0
  4. package/src/AlertRuntimeServices.ts +10 -0
  5. package/src/ChildWorkflowDefinition.ts +5 -0
  6. package/src/ChildWorkflowExecuteOptions.ts +14 -0
  7. package/src/ContinuationRequest.ts +3 -0
  8. package/src/HijackState.ts +19 -0
  9. package/src/HumanRequestKind.ts +1 -0
  10. package/src/HumanRequestStatus.ts +1 -0
  11. package/src/PlanNode.ts +29 -0
  12. package/src/RalphMeta.ts +7 -0
  13. package/src/RalphState.ts +4 -0
  14. package/src/RalphStateMap.ts +3 -0
  15. package/src/ScheduleResult.ts +15 -0
  16. package/src/SignalRunOptions.ts +5 -0
  17. package/src/alert-runtime.js +22 -0
  18. package/src/approvals.js +220 -0
  19. package/src/child-workflow.js +163 -0
  20. package/src/effect/ApprovalDeferredResolution.ts +13 -0
  21. package/src/effect/ApprovalDurableDeferredResolution.ts +11 -0
  22. package/src/effect/ApprovalPayload.ts +7 -0
  23. package/src/effect/ApprovalResult.ts +6 -0
  24. package/src/effect/BuilderNode.ts +52 -0
  25. package/src/effect/BuilderStepHandle.ts +47 -0
  26. package/src/effect/CancelPayload.ts +3 -0
  27. package/src/effect/CancelResult.ts +4 -0
  28. package/src/effect/DeferredResolution.ts +7 -0
  29. package/src/effect/DiffBundle.ts +7 -0
  30. package/src/effect/ExecuteTaskActivityOptions.ts +7 -0
  31. package/src/effect/FilePatch.ts +6 -0
  32. package/src/effect/GetRunPayload.ts +3 -0
  33. package/src/effect/GetRunResult.ts +3 -0
  34. package/src/effect/LegacyExecuteTaskFn.ts +24 -0
  35. package/src/effect/ListRunsPayload.ts +6 -0
  36. package/src/effect/RunStatusSchema.ts +9 -0
  37. package/src/effect/RunSummary.ts +23 -0
  38. package/src/effect/SignalPayload.ts +7 -0
  39. package/src/effect/SignalResult.ts +6 -0
  40. package/src/effect/SmithersSqliteOptions.ts +3 -0
  41. package/src/effect/SqlMessageStorageEventHistoryQuery.ts +7 -0
  42. package/src/effect/TaggedWorkerError.ts +46 -0
  43. package/src/effect/TaskActivityContext.ts +4 -0
  44. package/src/effect/TaskActivityRetryOptions.ts +4 -0
  45. package/src/effect/TaskBridgeToolConfig.ts +6 -0
  46. package/src/effect/TaskFailure.ts +3 -0
  47. package/src/effect/TaskResult.ts +5 -0
  48. package/src/effect/UnknownWorkerError.ts +5 -0
  49. package/src/effect/WaitForEventDurableDeferredResolution.ts +11 -0
  50. package/src/effect/WorkerDispatchKind.ts +1 -0
  51. package/src/effect/WorkerTask.ts +14 -0
  52. package/src/effect/WorkerTaskError.ts +4 -0
  53. package/src/effect/WorkerTaskKind.ts +1 -0
  54. package/src/effect/WorkflowPatchDecisionRecord.ts +4 -0
  55. package/src/effect/WorkflowPatchDecisions.ts +1 -0
  56. package/src/effect/WorkflowVersioningRuntime.ts +7 -0
  57. package/src/effect/activity-bridge.js +131 -0
  58. package/src/effect/bridge-utils.js +45 -0
  59. package/src/effect/builder.js +837 -0
  60. package/src/effect/compute-task-bridge.js +734 -0
  61. package/src/effect/deferred-bridge.js +63 -0
  62. package/src/effect/deferred-state-bridge.js +1343 -0
  63. package/src/effect/diff-bundle.js +352 -0
  64. package/src/effect/durable-deferred-bridge.js +282 -0
  65. package/src/effect/entity-worker.js +154 -0
  66. package/src/effect/http-runner.js +86 -0
  67. package/src/effect/rpc-schema.js +101 -0
  68. package/src/effect/single-runner.js +189 -0
  69. package/src/effect/sql-message-storage.js +817 -0
  70. package/src/effect/static-task-bridge.js +308 -0
  71. package/src/effect/versioning.js +123 -0
  72. package/src/effect/workflow-bridge.js +260 -0
  73. package/src/effect/workflow-make-bridge.js +233 -0
  74. package/src/engine.js +6933 -0
  75. package/src/events.js +237 -0
  76. package/src/external/json-schema-to-zod.js +214 -0
  77. package/src/getDefinedToolMetadata.js +10 -0
  78. package/src/hot/HotReloadEvent.ts +21 -0
  79. package/src/hot/HotWorkflowController.js +220 -0
  80. package/src/hot/OverlayOptions.ts +4 -0
  81. package/src/hot/WatchTreeOptions.ts +6 -0
  82. package/src/hot/index.js +9 -0
  83. package/src/hot/overlay.js +177 -0
  84. package/src/hot/watch.js +174 -0
  85. package/src/human-requests.js +120 -0
  86. package/src/index.d.ts +1597 -0
  87. package/src/index.js +41 -0
  88. package/src/runtime-owner.js +36 -0
  89. package/src/scheduler.js +31 -0
  90. package/src/signals.js +82 -0
package/src/index.js ADDED
@@ -0,0 +1,41 @@
1
+ // @smithers-type-exports-begin
2
+ /** @typedef {import("./ChildWorkflowDefinition.ts").ChildWorkflowDefinition} ChildWorkflowDefinition */
3
+ // @smithers-type-exports-end
4
+
5
+ export { runWorkflow } from "./engine.js";
6
+ export { renderFrame } from "./engine.js";
7
+ export { resolveSchema } from "./engine.js";
8
+ export { isRunHeartbeatFresh } from "./engine.js";
9
+
10
+ // Top-level public modules
11
+ export * from "./alert-runtime.js";
12
+ export * from "./approvals.js";
13
+ export * from "./child-workflow.js";
14
+ export * from "./events.js";
15
+ export * from "./getDefinedToolMetadata.js";
16
+ export * from "./human-requests.js";
17
+ export * from "./runtime-owner.js";
18
+ export * from "./scheduler.js";
19
+ export * from "./signals.js";
20
+
21
+ // Hot reload public surface (aggregated in hot/index.js)
22
+ export * from "./hot/index.js";
23
+
24
+ // Effect bridge public surface.
25
+ // workflow-bridge.js is the authoritative umbrella for: durable-deferred-bridge,
26
+ // deferred-state-bridge, workflow-make-bridge, sql-message-storage, entity-worker,
27
+ // single-runner, http-runner.
28
+ export * from "./effect/workflow-bridge.js";
29
+ export * from "./effect/activity-bridge.js";
30
+ export * from "./effect/bridge-utils.js";
31
+ export * from "./effect/builder.js";
32
+ export * from "./effect/compute-task-bridge.js";
33
+ // (bridgeApprovalResolve / bridgeTimerResolve namespace comes from workflow-bridge.js via durable-deferred-bridge.js)
34
+ // deferred-bridge.js is intentionally not re-exported to avoid collisions with the durable variants.
35
+ export * from "./effect/diff-bundle.js";
36
+ export * from "./effect/rpc-schema.js";
37
+ export * from "./effect/static-task-bridge.js";
38
+ export * from "./effect/versioning.js";
39
+
40
+ // External helpers
41
+ export * from "./external/json-schema-to-zod.js";
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @param {string | null | undefined} runtimeOwnerId
3
+ * @returns {number | null}
4
+ */
5
+ export function parseRuntimeOwnerPid(runtimeOwnerId) {
6
+ if (!runtimeOwnerId)
7
+ return null;
8
+ const trimmed = runtimeOwnerId.trim();
9
+ if (trimmed.length === 0)
10
+ return null;
11
+ const exact = trimmed.match(/^pid:(\d+)(?::.*)?$/i);
12
+ if (exact) {
13
+ const pid = Number(exact[1]);
14
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
15
+ }
16
+ if (/^\d+$/.test(trimmed)) {
17
+ const pid = Number(trimmed);
18
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
19
+ }
20
+ return null;
21
+ }
22
+ /**
23
+ * @param {number} pid
24
+ * @returns {boolean}
25
+ */
26
+ export function isPidAlive(pid) {
27
+ if (!Number.isInteger(pid) || pid <= 0)
28
+ return false;
29
+ try {
30
+ process.kill(pid, 0);
31
+ return true;
32
+ }
33
+ catch (error) {
34
+ return error?.code === "EPERM";
35
+ }
36
+ }
@@ -0,0 +1,31 @@
1
+ // @smithers-type-exports-begin
2
+ /** @typedef {import("./ContinuationRequest.ts").ContinuationRequest} ContinuationRequest */
3
+ /** @typedef {import("./PlanNode.ts").PlanNode} PlanNode */
4
+ /** @typedef {import("./RalphMeta.ts").RalphMeta} RalphMeta */
5
+ /** @typedef {import("./RalphState.ts").RalphState} RalphState */
6
+ /** @typedef {import("./RalphStateMap.ts").RalphStateMap} RalphStateMap */
7
+ /** @typedef {import("@smithers-orchestrator/scheduler").ReadonlyTaskStateMap} ReadonlyTaskStateMap */
8
+ /** @typedef {import("@smithers-orchestrator/scheduler").RetryWaitMap} RetryWaitMap */
9
+ /** @typedef {import("./ScheduleResult.ts").ScheduleResult} ScheduleResult */
10
+ /** @typedef {import("@smithers-orchestrator/scheduler").ScheduleSnapshot} ScheduleSnapshot */
11
+ /** @typedef {import("@smithers-orchestrator/scheduler").TaskRecord} TaskRecord */
12
+ /** @typedef {import("@smithers-orchestrator/scheduler").TaskState} TaskState */
13
+ /** @typedef {import("@smithers-orchestrator/scheduler").TaskStateMap} TaskStateMap */
14
+ /** @typedef {import("@smithers-orchestrator/graph/TaskDescriptor").TaskDescriptor} _TaskDescriptor */
15
+ /** @typedef {import("@smithers-orchestrator/graph/XmlNode").XmlNode} XmlNode */
16
+ // @smithers-type-exports-end
17
+
18
+ import { buildPlanTree as coreBuildPlanTree, scheduleTasks as coreScheduleTasks, } from "@smithers-orchestrator/scheduler";
19
+ export { buildStateKey } from "@smithers-orchestrator/scheduler";
20
+ export { Scheduler, SchedulerLive } from "@smithers-orchestrator/scheduler";
21
+ export { cloneTaskStateMap, isTerminalState, parseStateKey, } from "@smithers-orchestrator/scheduler";
22
+
23
+ /**
24
+ * @type {(xml: XmlNode | null, ralphState?: RalphStateMap) => { plan: PlanNode | null; ralphs: RalphMeta[] }}
25
+ */
26
+ export const buildPlanTree = coreBuildPlanTree;
27
+
28
+ /**
29
+ * @type {(plan: PlanNode | null, states: TaskStateMap, descriptors: Map<string, _TaskDescriptor>, ralphState: RalphStateMap, retryWait: Map<string, number>, nowMs: number) => ScheduleResult}
30
+ */
31
+ export const scheduleTasks = coreScheduleTasks;
package/src/signals.js ADDED
@@ -0,0 +1,82 @@
1
+ import { Effect } from "effect";
2
+ import { SmithersDb } from "@smithers-orchestrator/db/adapter";
3
+ import { bridgeSignalResolve } from "./effect/durable-deferred-bridge.js";
4
+ import { SmithersError } from "@smithers-orchestrator/errors/SmithersError";
5
+ import { nowMs } from "@smithers-orchestrator/scheduler/nowMs";
6
+ /** @typedef {import("./SignalRunOptions.ts").SignalRunOptions} SignalRunOptions */
7
+
8
+ /**
9
+ * @param {string} signalName
10
+ * @returns {string}
11
+ */
12
+ function normalizeSignalName(signalName) {
13
+ const normalized = signalName.trim();
14
+ if (!normalized) {
15
+ throw new SmithersError("INVALID_INPUT", "Signal name must be a non-empty string.", { signalName });
16
+ }
17
+ return normalized;
18
+ }
19
+ /**
20
+ * @param {unknown} payload
21
+ * @returns {string}
22
+ */
23
+ function serializeSignalPayload(payload) {
24
+ try {
25
+ const payloadJson = JSON.stringify(payload ?? null);
26
+ if (payloadJson === undefined) {
27
+ throw new Error("Signal payload serialized to undefined.");
28
+ }
29
+ return payloadJson;
30
+ }
31
+ catch (error) {
32
+ throw new SmithersError("INVALID_INPUT", "Signal payload must be valid JSON-serializable data.", undefined, { cause: error });
33
+ }
34
+ }
35
+ /**
36
+ * @param {SmithersDb} adapter
37
+ * @param {string} runId
38
+ * @param {string} signalName
39
+ * @param {unknown} payload
40
+ * @param {SignalRunOptions} [options]
41
+ * @returns {Effect.Effect<{ runId: string; seq: number; signalName: string; correlationId: string | null; receivedAtMs: number }, SmithersError, never>}
42
+ */
43
+ export function signalRun(adapter, runId, signalName, payload, options = {}) {
44
+ const normalizedSignalName = normalizeSignalName(signalName);
45
+ const payloadJson = serializeSignalPayload(payload);
46
+ const receivedAtMs = options.timestampMs ?? nowMs();
47
+ return Effect.gen(function* () {
48
+ const run = yield* adapter.getRun(runId);
49
+ if (!run) {
50
+ throw new SmithersError("RUN_NOT_FOUND", `Run not found: ${runId}`, {
51
+ runId,
52
+ });
53
+ }
54
+ const seq = yield* adapter.insertSignalWithNextSeq({
55
+ runId,
56
+ signalName: normalizedSignalName,
57
+ correlationId: options.correlationId ?? null,
58
+ payloadJson,
59
+ receivedAtMs,
60
+ receivedBy: options.receivedBy ?? null,
61
+ });
62
+ const delivered = {
63
+ runId,
64
+ seq,
65
+ signalName: normalizedSignalName,
66
+ correlationId: options.correlationId ?? null,
67
+ receivedAtMs,
68
+ };
69
+ yield* Effect.promise(() => bridgeSignalResolve(adapter, runId, {
70
+ signalName: delivered.signalName,
71
+ correlationId: delivered.correlationId ?? null,
72
+ payloadJson,
73
+ seq: delivered.seq,
74
+ receivedAtMs: delivered.receivedAtMs,
75
+ }));
76
+ return delivered;
77
+ }).pipe(Effect.annotateLogs({
78
+ runId,
79
+ signalName: normalizedSignalName,
80
+ correlationId: options.correlationId ?? null,
81
+ }), Effect.withLogSpan("signal:send"));
82
+ }