@fluidgrids/workflows 2026.1027.1 → 2026.1028.1

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 (166) hide show
  1. package/README.md +144 -13
  2. package/dist/activation-BBh1p82z.d.cts +57 -0
  3. package/dist/activation-DnhG7SmF.d.ts +57 -0
  4. package/dist/assemble-BLqQ8lSu.d.cts +39 -0
  5. package/dist/assemble-DWgaXHd-.d.ts +39 -0
  6. package/dist/chunk-2DP2Y6EU.cjs +832 -0
  7. package/dist/chunk-2J6TF7YI.cjs +86 -0
  8. package/dist/chunk-3FOTFQ3J.js +1622 -0
  9. package/dist/chunk-4LYSEANA.js +199 -0
  10. package/dist/{chunk-TQF7MYEN.cjs → chunk-57MSZ24T.cjs} +134 -96
  11. package/dist/{chunk-EEAIM35V.js → chunk-5HDSBTBX.js} +64 -23
  12. package/dist/chunk-623FVQSN.cjs +251 -0
  13. package/dist/{chunk-W32BSO3M.js → chunk-6FSHT5IL.js} +2 -2
  14. package/dist/{chunk-E3APHA6O.cjs → chunk-7AAZGVKD.js} +1 -64
  15. package/dist/chunk-7NQ37GZU.js +73 -0
  16. package/dist/chunk-ABFZWYHT.js +612 -0
  17. package/dist/{chunk-6U446SKY.js → chunk-BD7ZPG53.js} +2 -2
  18. package/dist/chunk-BO6OVILK.js +79 -0
  19. package/dist/chunk-C5OLWZ42.js +245 -0
  20. package/dist/chunk-CBL5JSCC.cjs +75 -0
  21. package/dist/{chunk-OSPTJCDI.cjs → chunk-CPDK3K5N.cjs} +6 -6
  22. package/dist/chunk-CSNWNFKU.cjs +1635 -0
  23. package/dist/chunk-DJYMDLV5.js +227 -0
  24. package/dist/{chunk-IHKXLG6I.js → chunk-E2VK7J2C.js} +1 -1
  25. package/dist/chunk-FMSW2HDF.cjs +267 -0
  26. package/dist/chunk-FQIHX7YI.cjs +80 -0
  27. package/dist/chunk-FR7KYWRO.cjs +41 -0
  28. package/dist/{chunk-7YR7DI7B.cjs → chunk-GBQ33SIZ.cjs} +44 -44
  29. package/dist/{chunk-6AIAC2FP.cjs → chunk-GVTZMA5C.cjs} +4 -4
  30. package/dist/chunk-H7KEVW2M.js +816 -0
  31. package/dist/{chunk-3M34AUIY.cjs → chunk-HR3BN4C4.cjs} +3 -3
  32. package/dist/chunk-IISBRUWJ.js +13 -0
  33. package/dist/chunk-JD2VQIDZ.cjs +15 -0
  34. package/dist/{chunk-UB2BHU2M.js → chunk-JZ4TOOLN.js} +1 -1
  35. package/dist/chunk-L24YIZY7.cjs +207 -0
  36. package/dist/chunk-LNUQE6D6.js +39 -0
  37. package/dist/{chunk-TWOYB4Y7.cjs → chunk-NEIUQASE.cjs} +13 -13
  38. package/dist/chunk-P6TA5EZT.cjs +634 -0
  39. package/dist/chunk-PDHLTDZB.js +72 -0
  40. package/dist/chunk-PMCM2OH4.js +46 -0
  41. package/dist/chunk-PZCVF76Q.cjs +868 -0
  42. package/dist/{chunk-6BEW4QD6.js → chunk-QAN5MZEM.js} +1 -1
  43. package/dist/{chunk-CSYBNN3V.cjs → chunk-QKZL3ONM.cjs} +2 -2
  44. package/dist/chunk-SAENRNBJ.cjs +52 -0
  45. package/dist/{chunk-XBIID5DY.js → chunk-SMCANK3F.js} +5 -4
  46. package/dist/{chunk-UWQWD7BP.cjs → chunk-SX2PNKKT.cjs} +18 -17
  47. package/dist/{chunk-UJNHTS6D.js → chunk-TBDXTV22.cjs} +8 -45
  48. package/dist/chunk-VOKMAAJP.js +848 -0
  49. package/dist/{chunk-5AW745OG.js → chunk-XB53G2DK.js} +1 -1
  50. package/dist/chunk-Z2Y65YOY.cjs +7 -0
  51. package/dist/codegen/index.cjs +9 -7
  52. package/dist/codegen/index.d.cts +2 -2
  53. package/dist/codegen/index.d.ts +2 -2
  54. package/dist/codegen/index.js +6 -5
  55. package/dist/file-BOoHZgoQ.d.cts +1557 -0
  56. package/dist/file-yuPCFU5b.d.ts +1557 -0
  57. package/dist/history/index.cjs +287 -0
  58. package/dist/history/index.d.cts +157 -0
  59. package/dist/history/index.d.ts +157 -0
  60. package/dist/history/index.js +5 -0
  61. package/dist/index.cjs +134 -36
  62. package/dist/index.d.cts +137 -9
  63. package/dist/index.d.ts +137 -9
  64. package/dist/index.js +8 -3
  65. package/dist/ir/index.cjs +152 -158
  66. package/dist/ir/index.d.cts +14 -4
  67. package/dist/ir/index.d.ts +14 -4
  68. package/dist/ir/index.js +41 -50
  69. package/dist/lower-BuSvAFqi.d.cts +129 -0
  70. package/dist/lower-C_Qv_34S.d.ts +129 -0
  71. package/dist/lower-types-nofRXw9c.d.cts +15 -0
  72. package/dist/lower-types-nofRXw9c.d.ts +15 -0
  73. package/dist/{manifest-Duf-gHdO.d.cts → manifest-0OLw0i7k.d.cts} +1 -1
  74. package/dist/{manifest-Bp9ZJlav.d.ts → manifest-C8N6HDYl.d.ts} +1 -1
  75. package/dist/node-evaluator-CJNP4GHX.js +12 -0
  76. package/dist/node-evaluator-DMKPKUWA.cjs +22 -0
  77. package/dist/quickjs/index.cjs +236 -12
  78. package/dist/quickjs/index.d.cts +46 -5
  79. package/dist/quickjs/index.d.ts +46 -5
  80. package/dist/quickjs/index.js +230 -9
  81. package/dist/roundtrip/index.cjs +333 -131
  82. package/dist/roundtrip/index.d.cts +39 -7
  83. package/dist/roundtrip/index.d.ts +39 -7
  84. package/dist/roundtrip/index.js +185 -13
  85. package/dist/runtime/index.cjs +90 -0
  86. package/dist/runtime/index.d.cts +297 -0
  87. package/dist/runtime/index.d.ts +297 -0
  88. package/dist/runtime/index.js +8 -0
  89. package/dist/testing/index.cjs +1057 -0
  90. package/dist/testing/index.d.cts +305 -0
  91. package/dist/testing/index.d.ts +305 -0
  92. package/dist/testing/index.js +1037 -0
  93. package/dist/{types-Dr2Fn1Ax.d.ts → types-BGoPzgcl.d.cts} +1 -1
  94. package/dist/{types-Dr2Fn1Ax.d.cts → types-BGoPzgcl.d.ts} +1 -1
  95. package/dist/types-CpBBjaGX.d.cts +380 -0
  96. package/dist/types-DV-HX_Gt.d.ts +380 -0
  97. package/dist/{assemble-CrpUyd_3.d.ts → workflow-Bpr3vnA_.d.ts} +40 -160
  98. package/dist/workflow-C2a05WUJ.d.ts +444 -0
  99. package/dist/{assemble-DEf3znGD.d.cts → workflow-FXUkpIBc.d.cts} +40 -160
  100. package/dist/workflow-J-yYixny.d.cts +444 -0
  101. package/fixtures/README.md +128 -0
  102. package/fixtures/histories/activity-failure-fails-run.history.json +163 -0
  103. package/fixtures/histories/activity-retry-then-success.history.json +147 -0
  104. package/fixtures/histories/cancel-activity-abandon.history.json +126 -0
  105. package/fixtures/histories/cancel-activity-compensate.history.json +307 -0
  106. package/fixtures/histories/cancel-concurrent-non-cancellable.history.json +234 -0
  107. package/fixtures/histories/cancel-concurrent-scope-timeout.history.json +184 -0
  108. package/fixtures/histories/cancel-condition-after-cancel.history.json +200 -0
  109. package/fixtures/histories/cancel-during-timer.history.json +123 -0
  110. package/fixtures/histories/cancel-saga-compensations.history.json +325 -0
  111. package/fixtures/histories/cancel-scope-timeout.history.json +148 -0
  112. package/fixtures/histories/child-abandon-outlives-parent.history.json +191 -0
  113. package/fixtures/histories/child-cancel-default.history.json +234 -0
  114. package/fixtures/histories/child-cancel-request.history.json +206 -0
  115. package/fixtures/histories/child-durable-child-side.history.json +124 -0
  116. package/fixtures/histories/child-durable-result.history.json +163 -0
  117. package/fixtures/histories/child-failure-caught.history.json +166 -0
  118. package/fixtures/histories/child-graph-by-key.history.json +138 -0
  119. package/fixtures/histories/child-terminated-by-parent-close.history.json +85 -0
  120. package/fixtures/histories/continue-as-new-first-run.history.json +146 -0
  121. package/fixtures/histories/continue-as-new-history-limit.history.json +489 -0
  122. package/fixtures/histories/continue-as-new-last-run.history.json +147 -0
  123. package/fixtures/histories/continue-as-new-suggested.history.json +352 -0
  124. package/fixtures/histories/details-and-context.history.json +176 -0
  125. package/fixtures/histories/deterministic-values.history.json +128 -0
  126. package/fixtures/histories/engine-builtins.history.json +172 -0
  127. package/fixtures/histories/gather-activities.history.json +291 -0
  128. package/fixtures/histories/gather-all-settled.history.json +219 -0
  129. package/fixtures/histories/gather-mixed.history.json +281 -0
  130. package/fixtures/histories/input-during-running-task.history.json +111 -0
  131. package/fixtures/histories/nondeterminism-reordered.history.json +218 -0
  132. package/fixtures/histories/patched-deprecated.history.json +271 -0
  133. package/fixtures/histories/patched-new-run.history.json +270 -0
  134. package/fixtures/histories/patched-old-history.history.json +190 -0
  135. package/fixtures/histories/race-activity-wins.history.json +152 -0
  136. package/fixtures/histories/race-signal-beats-child.history.json +161 -0
  137. package/fixtures/histories/race-timeout-wins.history.json +118 -0
  138. package/fixtures/histories/side-effect-recorded.history.json +153 -0
  139. package/fixtures/histories/signal-buffered-at-start.history.json +157 -0
  140. package/fixtures/histories/signal-default-handler.history.json +187 -0
  141. package/fixtures/histories/signal-handler.history.json +191 -0
  142. package/fixtures/histories/signal-parent-child-side.history.json +132 -0
  143. package/fixtures/histories/signal-parent.history.json +202 -0
  144. package/fixtures/histories/signal-run-delivered.history.json +128 -0
  145. package/fixtures/histories/signal-run-undelivered.history.json +134 -0
  146. package/fixtures/histories/signal-wait.history.json +111 -0
  147. package/fixtures/histories/task-failure-code-error.history.json +137 -0
  148. package/fixtures/histories/task-failure-then-transient-attempt.history.json +140 -0
  149. package/fixtures/histories/timer-condition-met.history.json +201 -0
  150. package/fixtures/histories/timer-condition-timeout.history.json +118 -0
  151. package/fixtures/histories/timer-sleep-once.history.json +118 -0
  152. package/fixtures/histories/timer-sleep-sequence.history.json +210 -0
  153. package/fixtures/histories/update-accepted-completed.history.json +149 -0
  154. package/fixtures/histories/update-with-start-rejected-validation.history.json +91 -0
  155. package/package.json +44 -5
  156. package/schema/history-file.schema.json +2544 -0
  157. package/schema/ir-v2.schema.json +16 -0
  158. package/schema/run-event.schema.json +2374 -0
  159. package/schema/workflow-command.schema.json +1095 -0
  160. package/schema/workflow-task.schema.json +3560 -0
  161. package/dist/chunk-MBBKGD7K.js +0 -440
  162. package/dist/chunk-Y7NWR3WH.cjs +0 -450
  163. package/dist/lower-CXggJTIo.d.ts +0 -67
  164. package/dist/lower-DAzvsJCo.d.cts +0 -67
  165. package/dist/node-evaluator-CH3XCJND.cjs +0 -17
  166. package/dist/node-evaluator-TULXA3WE.js +0 -8
@@ -0,0 +1,848 @@
1
+ import { isRecord } from './chunk-PMCM2OH4.js';
2
+ import { isWorkflowCommandEnabled } from './chunk-ABFZWYHT.js';
3
+ import { CHILD_CANCELLATION_TYPES, PARENT_CLOSE_POLICIES, ERROR_TYPES, ACTIVITY_CANCELLATION_TYPES, TASK_FAILURE_CAUSES, EVENT_TYPES, COMMAND_TYPES, BUILT_IN_QUERIES, HISTORY_PROTOCOL, TERMINAL_COMMANDS } from './chunk-DJYMDLV5.js';
4
+ import { isActivityNodeTypeAllowed } from './chunk-BO6OVILK.js';
5
+ import { Ajv2020 } from 'ajv/dist/2020.js';
6
+
7
+ // src/history/schema.ts
8
+ var BASE = "https://docs.fluidgrids.ai/schemas";
9
+ var RUN_EVENT_SCHEMA_ID = `${BASE}/run-event.schema.json`;
10
+ var WORKFLOW_COMMAND_SCHEMA_ID = `${BASE}/workflow-command.schema.json`;
11
+ var WORKFLOW_TASK_SCHEMA_ID = `${BASE}/workflow-task.schema.json`;
12
+ var HISTORY_FILE_SCHEMA_ID = `${BASE}/history-file.schema.json`;
13
+ var HISTORY_FILE_FORMAT = "fluidgrids.history";
14
+ var ID = { type: "string", minLength: 1, maxLength: 256 };
15
+ var NON_NEGATIVE_MS = { type: "integer", minimum: 0 };
16
+ var STRING_MAP = { type: "object", additionalProperties: { type: "string" } };
17
+ var SHARED_DEFS = {
18
+ json: {
19
+ description: "Any JSON value."
20
+ },
21
+ failure: {
22
+ type: "object",
23
+ required: ["errorType", "message"],
24
+ properties: {
25
+ errorType: { enum: [...ERROR_TYPES] },
26
+ message: { type: "string" },
27
+ type: { type: "string" },
28
+ details: {},
29
+ nonRetryable: { type: "boolean" },
30
+ nextRetryDelayMs: NON_NEGATIVE_MS,
31
+ category: { const: "BENIGN" },
32
+ timeoutType: { type: "string" },
33
+ cause: { $ref: "#/$defs/failure" },
34
+ stackTrace: { type: "string" },
35
+ source: { type: "string" }
36
+ },
37
+ additionalProperties: false
38
+ },
39
+ retryPolicy: {
40
+ type: "object",
41
+ required: ["retryDelay"],
42
+ description: "maxRetries absent means unlimited attempts, bounded by scheduleToCloseTimeout.",
43
+ properties: {
44
+ maxRetries: { type: "integer", minimum: 0 },
45
+ retryDelay: NON_NEGATIVE_MS,
46
+ backoffMultiplier: { type: "number", minimum: 1 },
47
+ maxDelay: NON_NEGATIVE_MS,
48
+ retryOnErrorTypes: { type: "array", items: { enum: [...ERROR_TYPES] } },
49
+ nonRetryableErrorTypes: { type: "array", items: { type: "string" } }
50
+ },
51
+ additionalProperties: false
52
+ },
53
+ runTarget: {
54
+ oneOf: [
55
+ {
56
+ type: "object",
57
+ required: ["runId"],
58
+ properties: { runId: ID },
59
+ additionalProperties: false
60
+ },
61
+ {
62
+ type: "object",
63
+ required: ["parent"],
64
+ properties: { parent: { const: true } },
65
+ additionalProperties: false
66
+ },
67
+ {
68
+ type: "object",
69
+ required: ["childId"],
70
+ properties: { childId: ID },
71
+ additionalProperties: false
72
+ },
73
+ {
74
+ type: "object",
75
+ required: ["workflowKey", "workflowId"],
76
+ properties: { workflowKey: ID, workflowId: ID },
77
+ additionalProperties: false
78
+ }
79
+ ]
80
+ },
81
+ childOptions: {
82
+ type: "object",
83
+ required: ["parentClosePolicy"],
84
+ properties: {
85
+ parentClosePolicy: { enum: [...PARENT_CLOSE_POLICIES] },
86
+ cancellationType: { enum: [...CHILD_CANCELLATION_TYPES] },
87
+ taskQueue: ID,
88
+ workflowId: ID,
89
+ priority: { type: "integer", minimum: 1, maximum: 5 },
90
+ summary: { type: "string", maxLength: 200 },
91
+ details: { type: "string" },
92
+ context: { type: "object" },
93
+ executionTimeout: NON_NEGATIVE_MS,
94
+ runTimeout: NON_NEGATIVE_MS,
95
+ workflowTaskTimeout: NON_NEGATIVE_MS,
96
+ startDelay: NON_NEGATIVE_MS
97
+ }
98
+ },
99
+ headers: STRING_MAP
100
+ };
101
+ function command(type, required, properties) {
102
+ return {
103
+ type: "object",
104
+ required: ["type", ...required],
105
+ properties: { type: { const: type }, headers: { $ref: "#/$defs/headers" }, ...properties },
106
+ additionalProperties: false
107
+ };
108
+ }
109
+ var COMMAND_DEFS = {
110
+ SCHEDULE_ACTIVITY: command(
111
+ "SCHEDULE_ACTIVITY",
112
+ ["activityId", "kind", "nodeType", "operation", "args", "retry"],
113
+ {
114
+ activityId: ID,
115
+ kind: { const: "node" },
116
+ nodeType: ID,
117
+ nodeVersion: { type: "string" },
118
+ operation: ID,
119
+ args: {},
120
+ connection: ID,
121
+ retry: { $ref: "#/$defs/retryPolicy" },
122
+ startToCloseTimeout: NON_NEGATIVE_MS,
123
+ scheduleToCloseTimeout: NON_NEGATIVE_MS,
124
+ scheduleToStartTimeout: NON_NEGATIVE_MS,
125
+ heartbeatTimeout: NON_NEGATIVE_MS,
126
+ cancellationType: { enum: [...ACTIVITY_CANCELLATION_TYPES] },
127
+ priority: { type: "integer", minimum: 1, maximum: 5 },
128
+ summary: { type: "string", maxLength: 200 },
129
+ runOn: { type: "string" }
130
+ }
131
+ ),
132
+ REQUEST_CANCEL_ACTIVITY: command("REQUEST_CANCEL_ACTIVITY", ["activityId"], { activityId: ID }),
133
+ START_TIMER: command("START_TIMER", ["timerId", "durationMs"], {
134
+ timerId: ID,
135
+ durationMs: { type: "integer", minimum: 1 },
136
+ summary: { type: "string", maxLength: 200 }
137
+ }),
138
+ CANCEL_TIMER: command("CANCEL_TIMER", ["timerId"], { timerId: ID }),
139
+ START_CHILD_RUN: {
140
+ ...command("START_CHILD_RUN", ["childId", "input", "options"], {
141
+ childId: ID,
142
+ workflowKey: ID,
143
+ workflowFn: ID,
144
+ version: { type: "string", minLength: 1 },
145
+ input: {},
146
+ options: { $ref: "#/$defs/childOptions" }
147
+ }),
148
+ oneOf: [{ required: ["workflowKey"] }, { required: ["workflowFn"] }]
149
+ },
150
+ SIGNAL_RUN: command("SIGNAL_RUN", ["externalId", "target", "name", "payload"], {
151
+ externalId: ID,
152
+ target: { $ref: "#/$defs/runTarget" },
153
+ name: ID,
154
+ payload: {}
155
+ }),
156
+ RECORD_SIDE_EFFECT: command("RECORD_SIDE_EFFECT", ["sideEffectId", "value"], {
157
+ sideEffectId: ID,
158
+ value: {}
159
+ }),
160
+ RECORD_MARKER: command("RECORD_MARKER", ["markerId", "data"], { markerId: ID, data: {} }),
161
+ COMPLETE_WORKFLOW: command("COMPLETE_WORKFLOW", ["output"], { output: {} }),
162
+ FAIL_WORKFLOW: command("FAIL_WORKFLOW", ["failure"], {
163
+ failure: { $ref: "#/$defs/failure" }
164
+ }),
165
+ CANCEL_WORKFLOW: command("CANCEL_WORKFLOW", [], {}),
166
+ UPSERT_CONTEXT: command("UPSERT_CONTEXT", ["entries"], { entries: { type: "object" } }),
167
+ RECORD_LOCAL_ACTIVITY: {
168
+ ...command("RECORD_LOCAL_ACTIVITY", ["localActivityId", "attempts"], {
169
+ localActivityId: ID,
170
+ result: {},
171
+ failure: { $ref: "#/$defs/failure" },
172
+ attempts: { type: "integer", minimum: 1 }
173
+ }),
174
+ oneOf: [{ required: ["result"] }, { required: ["failure"] }]
175
+ },
176
+ CONTINUE_AS_NEW: command("CONTINUE_AS_NEW", ["input", "options"], {
177
+ input: {},
178
+ options: { type: "object" }
179
+ }),
180
+ REQUEST_CANCEL_EXTERNAL_RUN: command("REQUEST_CANCEL_EXTERNAL_RUN", ["externalId", "target"], {
181
+ externalId: ID,
182
+ target: { $ref: "#/$defs/runTarget" }
183
+ }),
184
+ UPSERT_SEARCH_ATTRIBUTES: command("UPSERT_SEARCH_ATTRIBUTES", ["attributes"], {
185
+ attributes: { type: "object" }
186
+ }),
187
+ ACCEPT_UPDATE: command("ACCEPT_UPDATE", ["updateId"], { updateId: ID }),
188
+ REJECT_UPDATE: command("REJECT_UPDATE", ["updateId", "failure"], {
189
+ updateId: ID,
190
+ failure: { $ref: "#/$defs/failure" }
191
+ }),
192
+ COMPLETE_UPDATE: command("COMPLETE_UPDATE", ["updateId"], {
193
+ updateId: ID,
194
+ result: {},
195
+ failure: { $ref: "#/$defs/failure" }
196
+ })
197
+ };
198
+ function payload(required, properties) {
199
+ return { type: "object", required, properties };
200
+ }
201
+ var FAILURE_REF = { $ref: "#/$defs/failure" };
202
+ var EVENT_PAYLOADS = {
203
+ WORKFLOW_STARTED: payload(["workflowFn", "input"], {
204
+ workflowFn: ID,
205
+ input: {},
206
+ workflowKey: { type: "string" },
207
+ version: {},
208
+ buildId: { type: "string" },
209
+ taskQueue: { type: "string" },
210
+ parentRunId: { type: ["string", "null"] },
211
+ rootRunId: { type: ["string", "null"] },
212
+ firstRunId: { type: ["string", "null"] },
213
+ continuedFromRunId: { type: ["string", "null"] },
214
+ parentClosePolicy: { enum: [...PARENT_CLOSE_POLICIES] },
215
+ startDelay: NON_NEGATIVE_MS,
216
+ lastCompletionResult: {},
217
+ lastFailure: { oneOf: [{ type: "null" }, FAILURE_REF] },
218
+ summary: { type: "string" },
219
+ details: { type: "string" },
220
+ context: { type: "object" },
221
+ executionTimeout: NON_NEGATIVE_MS,
222
+ runTimeout: NON_NEGATIVE_MS,
223
+ workflowTaskTimeout: NON_NEGATIVE_MS,
224
+ versioningBehavior: { enum: ["PINNED", "AUTO_UPGRADE"] },
225
+ /** Seed of `ctx.random()`/`ctx.uuid()`; the service writes the run id. Absent: `info.runId`. */
226
+ randomnessSeed: ID
227
+ }),
228
+ WORKFLOW_TASK_SCHEDULED: payload(["taskId"], {
229
+ taskId: ID,
230
+ attempt: { type: "integer", minimum: 1 }
231
+ }),
232
+ WORKFLOW_TASK_STARTED: payload(["taskId", "attempt"], {
233
+ taskId: ID,
234
+ attempt: { type: "integer", minimum: 1 },
235
+ workerId: { type: "string" },
236
+ buildId: { type: "string" },
237
+ startedAt: {
238
+ type: "string",
239
+ description: "When the attempt started, if different from the event time (a transient attempt is recorded at completion). This is the clock the task saw: nowMs of its request."
240
+ },
241
+ historyLength: {
242
+ type: "integer",
243
+ minimum: 1,
244
+ description: "The history length the attempt was leased with (run.taskStartedSeq), when it is not this event's own seq. Required on the WORKFLOW_TASK_STARTED of a transient attempt: input events with a higher seq that precede this event arrived during the attempt and belong to the next activation."
245
+ }
246
+ }),
247
+ WORKFLOW_TASK_COMPLETED: payload(["taskId"], {
248
+ taskId: ID,
249
+ buildId: { type: "string" },
250
+ workerId: { type: "string" }
251
+ }),
252
+ WORKFLOW_TASK_FAILED: payload(["taskId", "attempt", "cause"], {
253
+ taskId: ID,
254
+ attempt: { type: "integer", minimum: 1 },
255
+ cause: { enum: [...TASK_FAILURE_CAUSES] },
256
+ message: { type: "string" },
257
+ stackTrace: { type: "string" },
258
+ buildId: { type: "string" }
259
+ }),
260
+ WORKFLOW_TASK_TIMED_OUT: payload(["taskId", "attempt"], {
261
+ taskId: ID,
262
+ attempt: { type: "integer", minimum: 1 }
263
+ }),
264
+ ACTIVITY_SCHEDULED: payload(["activityId", "kind", "nodeType", "operation", "args", "retry"], {
265
+ ...COMMAND_DEFS.SCHEDULE_ACTIVITY.properties,
266
+ type: void 0
267
+ }),
268
+ ACTIVITY_STARTED: payload(["activityId", "attempt"], {
269
+ activityId: ID,
270
+ attempt: { type: "integer", minimum: 1 },
271
+ workerId: { type: "string" }
272
+ }),
273
+ ACTIVITY_HEARTBEAT: payload(["activityId"], { activityId: ID, details: {} }),
274
+ ACTIVITY_COMPLETED: payload(["activityId", "attempt"], {
275
+ activityId: ID,
276
+ attempt: { type: "integer", minimum: 1 },
277
+ result: {}
278
+ }),
279
+ ACTIVITY_FAILED: payload(["activityId", "attempt", "failure"], {
280
+ activityId: ID,
281
+ attempt: { type: "integer", minimum: 1 },
282
+ failure: FAILURE_REF
283
+ }),
284
+ ACTIVITY_TIMED_OUT: payload(["activityId", "attempt", "timeoutType"], {
285
+ activityId: ID,
286
+ attempt: { type: "integer", minimum: 1 },
287
+ timeoutType: {
288
+ enum: ["START_TO_CLOSE", "SCHEDULE_TO_START", "SCHEDULE_TO_CLOSE", "HEARTBEAT"]
289
+ },
290
+ failure: FAILURE_REF
291
+ }),
292
+ ACTIVITY_CANCEL_REQUESTED: payload(["activityId"], { activityId: ID }),
293
+ ACTIVITY_CANCELLED: payload(["activityId"], { activityId: ID, details: {} }),
294
+ ACTIVITY_PAUSED: payload(["activityId"], { activityId: ID, reason: { type: "string" } }),
295
+ ACTIVITY_UNPAUSED: payload(["activityId"], { activityId: ID }),
296
+ ACTIVITY_RESET: payload(["activityId"], { activityId: ID }),
297
+ ACTIVITY_OPTIONS_UPDATED: payload(["activityId"], {
298
+ activityId: ID,
299
+ options: { type: "object" }
300
+ }),
301
+ TIMER_STARTED: payload(["timerId", "durationMs", "fireAt"], {
302
+ timerId: ID,
303
+ durationMs: { type: "integer", minimum: 1 },
304
+ fireAt: { type: "string" },
305
+ summary: { type: "string" }
306
+ }),
307
+ TIMER_FIRED: payload(["timerId"], { timerId: ID }),
308
+ TIMER_CANCELLED: payload(["timerId"], { timerId: ID }),
309
+ SIGNAL_RECEIVED: payload(["name"], {
310
+ name: ID,
311
+ payload: {},
312
+ receivedAt: { type: "string" },
313
+ actor: {},
314
+ idempotencyKey: { type: "string" }
315
+ }),
316
+ UPDATE_ADMITTED: payload(["updateId", "name"], { updateId: ID, name: ID, args: {} }),
317
+ UPDATE_ACCEPTED: payload(["updateId", "name"], { updateId: ID, name: ID, args: {} }),
318
+ UPDATE_COMPLETED: payload(["updateId"], { updateId: ID, result: {}, failure: FAILURE_REF }),
319
+ CHILD_RUN_INITIATED: payload(["childId", "input", "options"], {
320
+ childId: ID,
321
+ workflowKey: ID,
322
+ workflowFn: ID,
323
+ version: { type: "string" },
324
+ input: {},
325
+ options: { $ref: "#/$defs/childOptions" },
326
+ childRunId: { type: "string" },
327
+ executionKind: { enum: ["GRAPH", "DURABLE_FN"] }
328
+ }),
329
+ CHILD_RUN_STARTED: payload(["childId", "childRunId"], {
330
+ childId: ID,
331
+ childRunId: ID,
332
+ executionKind: { enum: ["GRAPH", "DURABLE_FN"] }
333
+ }),
334
+ CHILD_RUN_COMPLETED: payload(["childId"], { childId: ID, childRunId: ID, result: {} }),
335
+ CHILD_RUN_FAILED: payload(["childId", "failure"], {
336
+ childId: ID,
337
+ childRunId: ID,
338
+ failure: FAILURE_REF
339
+ }),
340
+ CHILD_RUN_CANCELLED: payload(["childId"], { childId: ID, childRunId: ID }),
341
+ CHILD_RUN_TERMINATED: payload(["childId"], {
342
+ childId: ID,
343
+ childRunId: ID,
344
+ reason: { type: "string" }
345
+ }),
346
+ CHILD_RUN_TIMED_OUT: payload(["childId"], {
347
+ childId: ID,
348
+ childRunId: ID,
349
+ timeoutType: { enum: ["WORKFLOW_RUN", "WORKFLOW_EXECUTION"] }
350
+ }),
351
+ EXTERNAL_RUN_CANCEL_REQUESTED: payload(["externalId", "target"], {
352
+ externalId: ID,
353
+ target: { $ref: "#/$defs/runTarget" }
354
+ }),
355
+ EXTERNAL_RUN_CANCEL_RESOLVED: payload(["externalId", "ok"], {
356
+ externalId: ID,
357
+ ok: { type: "boolean" },
358
+ targetRunId: { type: "string" },
359
+ failure: FAILURE_REF
360
+ }),
361
+ EXTERNAL_RUN_SIGNALED: {
362
+ type: "object",
363
+ required: ["externalId"],
364
+ description: "Two forms. Initiated (appended for the SIGNAL_RUN command): {externalId, target, name, payload}. Resolved (appended by the service after delivery, dedupeKey signal-out:<runId>:<externalId>): {externalId, resolved: true, ok, failure?, targetRunId?}.",
365
+ properties: {
366
+ externalId: ID,
367
+ target: { $ref: "#/$defs/runTarget" },
368
+ name: ID,
369
+ payload: {},
370
+ resolved: { const: true },
371
+ ok: { type: "boolean" },
372
+ targetRunId: { type: "string" },
373
+ failure: FAILURE_REF
374
+ },
375
+ if: { required: ["resolved"] },
376
+ then: { required: ["ok"] },
377
+ else: { required: ["target", "name"] }
378
+ },
379
+ SIDE_EFFECT_RECORDED: payload(["sideEffectId", "value"], { sideEffectId: ID, value: {} }),
380
+ LOCAL_ACTIVITY_COMPLETED: payload(["localActivityId", "attempts"], {
381
+ localActivityId: ID,
382
+ result: {},
383
+ attempts: { type: "integer", minimum: 1 }
384
+ }),
385
+ LOCAL_ACTIVITY_FAILED: payload(["localActivityId", "attempts", "failure"], {
386
+ localActivityId: ID,
387
+ failure: FAILURE_REF,
388
+ attempts: { type: "integer", minimum: 1 }
389
+ }),
390
+ SEARCH_ATTRIBUTES_UPSERTED: payload(["attributes"], { attributes: { type: "object" } }),
391
+ CONTEXT_UPSERTED: payload(["entries"], { entries: { type: "object" } }),
392
+ MARKER_RECORDED: payload(["markerId", "data"], {
393
+ markerId: ID,
394
+ data: {}
395
+ }),
396
+ WORKFLOW_CANCEL_REQUESTED: payload([], { actor: {}, reason: { type: "string" } }),
397
+ WORKFLOW_RESET: payload(["fromRunId", "toSeq"], {
398
+ fromRunId: ID,
399
+ toSeq: { type: "integer", minimum: 1 },
400
+ reason: { type: "string" }
401
+ }),
402
+ VERSIONING_OVERRIDE_SET: payload(["override"], { override: {} }),
403
+ WORKFLOW_COMPLETED: payload(["output"], { output: {} }),
404
+ WORKFLOW_FAILED: payload(["failure"], { failure: FAILURE_REF }),
405
+ WORKFLOW_CANCELLED: payload([], { details: {} }),
406
+ WORKFLOW_TERMINATED: payload([], { reason: { type: "string" }, actor: {} }),
407
+ WORKFLOW_TIMED_OUT: payload(["timeoutType"], {
408
+ timeoutType: { enum: ["WORKFLOW_RUN", "WORKFLOW_EXECUTION"] }
409
+ }),
410
+ WORKFLOW_CONTINUED_AS_NEW: payload(["input", "options"], {
411
+ input: {},
412
+ options: { type: "object" },
413
+ newRunId: { type: "string" }
414
+ })
415
+ };
416
+ function compact(value) {
417
+ const out = {};
418
+ for (const [key, entry] of Object.entries(value)) {
419
+ if (entry === void 0) continue;
420
+ out[key] = typeof entry === "object" && entry !== null && !Array.isArray(entry) ? compact(entry) : entry;
421
+ }
422
+ return out;
423
+ }
424
+ var RUN_EVENT_RECORD = {
425
+ type: "object",
426
+ required: ["seq", "type", "payload", "createdAt"],
427
+ properties: {
428
+ seq: { type: "integer", minimum: 1 },
429
+ type: { enum: [...EVENT_TYPES] },
430
+ taskId: { type: ["string", "null"] },
431
+ payload: { type: "object" },
432
+ createdAt: { type: "string", minLength: 1 },
433
+ dedupeKey: { type: ["string", "null"] }
434
+ },
435
+ allOf: EVENT_TYPES.map((type) => ({
436
+ if: { properties: { type: { const: type } }, required: ["type"] },
437
+ then: { properties: { payload: { $ref: `#/$defs/payload_${type}` } } }
438
+ }))
439
+ };
440
+ function payloadDefs() {
441
+ const defs = {};
442
+ for (const type of EVENT_TYPES) defs[`payload_${type}`] = compact(EVENT_PAYLOADS[type]);
443
+ return defs;
444
+ }
445
+ function commandDefs() {
446
+ const defs = {};
447
+ for (const type of COMMAND_TYPES) defs[`command_${type}`] = COMMAND_DEFS[type];
448
+ return defs;
449
+ }
450
+ var COMMAND_ONE_OF = {
451
+ type: "object",
452
+ required: ["type"],
453
+ properties: { type: { enum: [...COMMAND_TYPES] } },
454
+ allOf: COMMAND_TYPES.map((type) => ({
455
+ if: { properties: { type: { const: type } }, required: ["type"] },
456
+ then: { $ref: `#/$defs/command_${type}` }
457
+ }))
458
+ };
459
+ var RUN_EVENT_SCHEMA = {
460
+ $schema: "https://json-schema.org/draft/2020-12/schema",
461
+ $id: RUN_EVENT_SCHEMA_ID,
462
+ title: "FluidGrids durable-function history event",
463
+ description: "One row of workflow_run_events as the service returns it (offloaded payloads inlined). type is UPPER_SNAKE.",
464
+ ...RUN_EVENT_RECORD,
465
+ $defs: { ...SHARED_DEFS, ...payloadDefs() }
466
+ };
467
+ var WORKFLOW_COMMAND_SCHEMA = {
468
+ $schema: "https://json-schema.org/draft/2020-12/schema",
469
+ $id: WORKFLOW_COMMAND_SCHEMA_ID,
470
+ title: "FluidGrids workflow-task command",
471
+ description: "A flat object discriminated by an UPPER_SNAKE type. Commands travel as [JSON!]! (DB4). UPSERT_SEARCH_ATTRIBUTES, ACCEPT_UPDATE, REJECT_UPDATE and COMPLETE_UPDATE are defined but refused in Phase B.",
472
+ ...COMMAND_ONE_OF,
473
+ $defs: { ...SHARED_DEFS, ...commandDefs() }
474
+ };
475
+ var WORKFLOW_TASK_SCHEMA = {
476
+ $schema: "https://json-schema.org/draft/2020-12/schema",
477
+ $id: WORKFLOW_TASK_SCHEMA_ID,
478
+ title: "FluidGrids workflow task (runtime ABI protocol 1)",
479
+ description: 'globalThis.__fluidgridsRuntime: start(JSON of request); while (step() === "more") drain jobs; complete() returns JSON of result.',
480
+ oneOf: [{ $ref: "#/$defs/request" }, { $ref: "#/$defs/result" }],
481
+ $defs: {
482
+ ...SHARED_DEFS,
483
+ ...payloadDefs(),
484
+ ...commandDefs(),
485
+ event: RUN_EVENT_RECORD,
486
+ command: COMMAND_ONE_OF,
487
+ info: {
488
+ type: "object",
489
+ required: ["runId", "workspaceId"],
490
+ properties: {
491
+ runId: ID,
492
+ workspaceId: ID,
493
+ workflowKey: { type: "string" },
494
+ version: {},
495
+ buildId: { type: "string" },
496
+ businessId: { type: "string" },
497
+ attempt: { type: "integer", minimum: 1 },
498
+ taskQueue: { type: "string" },
499
+ parentRunId: { type: ["string", "null"] },
500
+ rootRunId: { type: ["string", "null"] },
501
+ firstRunId: { type: ["string", "null"] },
502
+ continuedFromRunId: { type: ["string", "null"] },
503
+ lastResult: {},
504
+ lastFailure: { oneOf: [{ type: "null" }, FAILURE_REF] },
505
+ startedAt: { type: "string" },
506
+ runStartedAt: { type: "string" },
507
+ historyLimit: {
508
+ type: "object",
509
+ required: ["events", "bytes"],
510
+ properties: {
511
+ events: { type: "integer", minimum: 1 },
512
+ bytes: { type: "integer", minimum: 1 }
513
+ }
514
+ },
515
+ timeouts: { type: "object" },
516
+ headers: { $ref: "#/$defs/headers" },
517
+ priority: { type: "integer", minimum: 1, maximum: 5 },
518
+ summary: { type: "string" },
519
+ details: { type: "string" },
520
+ context: { type: "object" }
521
+ }
522
+ },
523
+ request: {
524
+ type: "object",
525
+ required: ["protocol", "kind", "workflowFn", "info", "history", "nowMs"],
526
+ properties: {
527
+ protocol: { const: HISTORY_PROTOCOL },
528
+ kind: { enum: ["task", "query", "verify"] },
529
+ workflowFn: ID,
530
+ info: { $ref: "#/$defs/info" },
531
+ history: { type: "array", items: { $ref: "#/$defs/event" } },
532
+ nowMs: { type: "number" },
533
+ localActivityResults: {
534
+ type: "array",
535
+ items: {
536
+ type: "object",
537
+ required: ["localActivityId", "attempts"],
538
+ properties: {
539
+ localActivityId: ID,
540
+ result: {},
541
+ failure: FAILURE_REF,
542
+ attempts: { type: "integer", minimum: 1 }
543
+ }
544
+ }
545
+ },
546
+ queries: {
547
+ type: "array",
548
+ items: {
549
+ type: "object",
550
+ required: ["queryId", "name"],
551
+ properties: {
552
+ queryId: ID,
553
+ name: { enum: [...BUILT_IN_QUERIES] },
554
+ args: {}
555
+ }
556
+ }
557
+ }
558
+ }
559
+ },
560
+ result: {
561
+ type: "object",
562
+ required: ["protocol", "outcome", "commands", "logs"],
563
+ properties: {
564
+ protocol: { const: HISTORY_PROTOCOL },
565
+ outcome: { enum: ["commands", "failed", "localActivityRequested"] },
566
+ commands: { type: "array", items: { $ref: "#/$defs/command" } },
567
+ localActivityRequests: { type: "array", items: { type: "object" } },
568
+ queryResults: {
569
+ type: "array",
570
+ items: {
571
+ type: "object",
572
+ required: ["queryId"],
573
+ properties: { queryId: ID, value: {}, error: { type: "string" } }
574
+ }
575
+ },
576
+ failure: {
577
+ type: "object",
578
+ required: ["cause", "message"],
579
+ properties: {
580
+ cause: { enum: [...TASK_FAILURE_CAUSES] },
581
+ message: { type: "string" },
582
+ stackTrace: { type: "string" },
583
+ seq: { type: "integer" },
584
+ errorType: { type: "string" }
585
+ }
586
+ },
587
+ logs: {
588
+ type: "array",
589
+ items: {
590
+ type: "object",
591
+ required: ["level", "message"],
592
+ properties: {
593
+ level: { enum: ["debug", "info", "warn", "error"] },
594
+ message: { type: "string" },
595
+ data: {}
596
+ }
597
+ }
598
+ },
599
+ stackTrace: { type: "string" }
600
+ }
601
+ }
602
+ }
603
+ };
604
+ var HISTORY_FILE_SCHEMA = {
605
+ $schema: "https://json-schema.org/draft/2020-12/schema",
606
+ $id: HISTORY_FILE_SCHEMA_ID,
607
+ title: "FluidGrids durable-function history file",
608
+ type: "object",
609
+ required: ["format", "formatVersion", "workflowFn", "events"],
610
+ properties: {
611
+ format: { const: HISTORY_FILE_FORMAT },
612
+ formatVersion: { const: HISTORY_PROTOCOL },
613
+ name: { type: "string" },
614
+ description: { type: "string" },
615
+ workflowFn: ID,
616
+ source: { type: "string", description: "Fixture source module, relative to the file." },
617
+ replaySource: {
618
+ type: "string",
619
+ description: "Fixture module the replay check runs instead of source (a hotfix or a patched() version)."
620
+ },
621
+ replay: {
622
+ type: "boolean",
623
+ description: "False for histories that use Phase C features (updates); they are parsed and projected but not replayed."
624
+ },
625
+ info: { $ref: "#/$defs/info" },
626
+ nowMs: {
627
+ type: "number",
628
+ description: "nowMs of the replay request for the conformance check."
629
+ },
630
+ events: { type: "array", items: { $ref: "#/$defs/event" } },
631
+ expected: {
632
+ type: "object",
633
+ description: "The replay result the conformance check must reproduce."
634
+ }
635
+ },
636
+ $defs: {
637
+ ...SHARED_DEFS,
638
+ ...payloadDefs(),
639
+ event: RUN_EVENT_RECORD,
640
+ info: WORKFLOW_TASK_SCHEMA.$defs.info
641
+ }
642
+ };
643
+ var HISTORY_SCHEMA_FILES = [
644
+ ["run-event.schema.json", RUN_EVENT_SCHEMA],
645
+ ["workflow-command.schema.json", WORKFLOW_COMMAND_SCHEMA],
646
+ ["workflow-task.schema.json", WORKFLOW_TASK_SCHEMA],
647
+ ["history-file.schema.json", HISTORY_FILE_SCHEMA]
648
+ ];
649
+ var tools;
650
+ function schemaTools() {
651
+ if (!tools) {
652
+ const ajv = new Ajv2020({ allErrors: true, strict: false });
653
+ tools = {
654
+ ajv,
655
+ command: ajv.compile(WORKFLOW_COMMAND_SCHEMA),
656
+ event: ajv.compile(RUN_EVENT_SCHEMA),
657
+ file: ajv.compile(HISTORY_FILE_SCHEMA)
658
+ };
659
+ }
660
+ return tools;
661
+ }
662
+ function issuesOf(index, errors) {
663
+ const real = (errors ?? []).filter((error) => error.keyword !== "if");
664
+ return real.map((error) => ({
665
+ index,
666
+ code: `schema.${error.keyword}`,
667
+ message: `${error.instancePath || "/"} ${error.message ?? "is invalid"}`,
668
+ path: error.instancePath
669
+ }));
670
+ }
671
+ function validateCommand(command2, index = 0) {
672
+ const { command: validate } = schemaTools();
673
+ if (validate(command2)) {
674
+ return { ok: true, command: command2 };
675
+ }
676
+ return { ok: false, issues: issuesOf(index, validate.errors) };
677
+ }
678
+ function idOf(command2) {
679
+ switch (command2.type) {
680
+ case "SCHEDULE_ACTIVITY":
681
+ return `activity:${command2.activityId}`;
682
+ case "START_TIMER":
683
+ return `timer:${command2.timerId}`;
684
+ case "START_CHILD_RUN":
685
+ return `child:${command2.childId}`;
686
+ case "SIGNAL_RUN":
687
+ case "REQUEST_CANCEL_EXTERNAL_RUN":
688
+ return `external:${command2.externalId}`;
689
+ case "RECORD_SIDE_EFFECT":
690
+ return `sideEffect:${command2.sideEffectId}`;
691
+ case "RECORD_MARKER":
692
+ return `marker:${command2.markerId}`;
693
+ case "RECORD_LOCAL_ACTIVITY":
694
+ return `local:${command2.localActivityId}`;
695
+ default:
696
+ return void 0;
697
+ }
698
+ }
699
+ function validateCommands(commands, options = {}) {
700
+ const max = options.maxCommands ?? 1e3;
701
+ if (!Array.isArray(commands))
702
+ return {
703
+ ok: false,
704
+ cause: "INVALID_REQUEST",
705
+ issues: [{ index: 0, code: "commands.notArray", message: "commands must be an array" }]
706
+ };
707
+ if (commands.length > max)
708
+ return {
709
+ ok: false,
710
+ cause: "INVALID_REQUEST",
711
+ issues: [
712
+ {
713
+ index: max,
714
+ code: "commands.tooMany",
715
+ message: `A task may return at most ${max} commands; it returned ${commands.length}`
716
+ }
717
+ ]
718
+ };
719
+ const issues = [];
720
+ const valid = [];
721
+ const ids = /* @__PURE__ */ new Set();
722
+ let unsupported = false;
723
+ commands.forEach((raw, index) => {
724
+ const checked = validateCommand(raw, index);
725
+ if (!checked.ok) {
726
+ issues.push(...checked.issues);
727
+ return;
728
+ }
729
+ const command2 = checked.command;
730
+ if (!isWorkflowCommandEnabled(command2, options.enabled)) {
731
+ unsupported = true;
732
+ issues.push({
733
+ index,
734
+ code: "commands.unsupported",
735
+ message: `${command2.type} is not supported in this release`
736
+ });
737
+ }
738
+ const id = idOf(command2);
739
+ if (id !== void 0) {
740
+ if (ids.has(id))
741
+ issues.push({ index, code: "commands.duplicateId", message: `${id} is used twice` });
742
+ ids.add(id);
743
+ }
744
+ if (TERMINAL_COMMANDS.has(command2.type) && index !== commands.length - 1)
745
+ issues.push({
746
+ index,
747
+ code: "commands.terminalNotLast",
748
+ message: `${command2.type} must be the last command of the batch`
749
+ });
750
+ if (command2.type === "SCHEDULE_ACTIVITY" && !isActivityNodeTypeAllowed(command2.nodeType))
751
+ issues.push({
752
+ index,
753
+ code: "durable.activityTypeNotAllowed",
754
+ message: `${command2.nodeType} cannot run as a durable activity`,
755
+ path: "/nodeType"
756
+ });
757
+ valid.push(command2);
758
+ });
759
+ if (issues.length > 0)
760
+ return { ok: false, cause: unsupported ? "UNSUPPORTED_COMMAND" : "INVALID_REQUEST", issues };
761
+ return { ok: true, commands: valid };
762
+ }
763
+ function validateHistory(events) {
764
+ const { event: validate } = schemaTools();
765
+ if (!Array.isArray(events))
766
+ return {
767
+ ok: false,
768
+ issues: [{ index: 0, code: "history.notArray", message: "history must be an array" }]
769
+ };
770
+ const issues = [];
771
+ events.forEach((event, index) => {
772
+ if (!validate(event)) {
773
+ issues.push(...issuesOf(index, validate.errors));
774
+ return;
775
+ }
776
+ if (isRecord(event) && event["seq"] !== index + 1)
777
+ issues.push({
778
+ index,
779
+ code: "history.seqGap",
780
+ message: `event ${index + 1} has seq ${String(event["seq"])}`
781
+ });
782
+ });
783
+ return { ok: issues.length === 0, issues };
784
+ }
785
+ function validateHistoryFile(value) {
786
+ const { file: validate } = schemaTools();
787
+ if (validate(value)) return validateHistory(isRecord(value) ? value["events"] : void 0);
788
+ return { ok: false, issues: issuesOf(0, validate.errors) };
789
+ }
790
+ function compilePayloadValidator(schema) {
791
+ const { ajv } = schemaTools();
792
+ const validate = ajv.compile(schema);
793
+ return (value) => {
794
+ if (validate(value)) return { ok: true };
795
+ return {
796
+ ok: false,
797
+ errors: (validate.errors ?? []).map(
798
+ (error) => `${error.instancePath || "/"} ${error.message ?? "is invalid"}`
799
+ )
800
+ };
801
+ };
802
+ }
803
+
804
+ // src/history/file.ts
805
+ var HistoryFileError = class extends Error {
806
+ constructor(message, issues = []) {
807
+ super(message);
808
+ this.issues = issues;
809
+ this.name = "HistoryFileError";
810
+ }
811
+ issues;
812
+ };
813
+ function parseHistoryFile(input) {
814
+ let value = input;
815
+ if (typeof input === "string") {
816
+ try {
817
+ value = JSON.parse(input);
818
+ } catch (error) {
819
+ throw new HistoryFileError(
820
+ `Not JSON: ${error instanceof Error ? error.message : String(error)}`
821
+ );
822
+ }
823
+ }
824
+ if (!isRecord(value) || value["format"] !== HISTORY_FILE_FORMAT)
825
+ throw new HistoryFileError(`Not a ${HISTORY_FILE_FORMAT} document`);
826
+ const checked = validateHistoryFile(value);
827
+ if (!checked.ok)
828
+ throw new HistoryFileError(
829
+ "The history file does not match history-file.schema.json",
830
+ checked.issues.map((issue) => `#${issue.index}: ${issue.message}`)
831
+ );
832
+ return value;
833
+ }
834
+ function createHistoryFile(workflowFn, events, extra = {}) {
835
+ return {
836
+ format: HISTORY_FILE_FORMAT,
837
+ formatVersion: HISTORY_PROTOCOL,
838
+ ...extra,
839
+ workflowFn,
840
+ events
841
+ };
842
+ }
843
+ function serializeHistoryFile(file) {
844
+ return `${JSON.stringify(file, null, 2)}
845
+ `;
846
+ }
847
+
848
+ export { HISTORY_FILE_FORMAT, HISTORY_FILE_SCHEMA, HISTORY_FILE_SCHEMA_ID, HISTORY_SCHEMA_FILES, HistoryFileError, RUN_EVENT_SCHEMA, RUN_EVENT_SCHEMA_ID, WORKFLOW_COMMAND_SCHEMA, WORKFLOW_COMMAND_SCHEMA_ID, WORKFLOW_TASK_SCHEMA, WORKFLOW_TASK_SCHEMA_ID, compilePayloadValidator, createHistoryFile, parseHistoryFile, serializeHistoryFile, validateCommand, validateCommands, validateHistory, validateHistoryFile };