@reventlessdev/reventless-core 3.0.0-alpha.321 → 3.0.0-alpha.322

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/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0-alpha.322 (2026-10-30)
7
+
8
+ ### Features
9
+
10
+ * **automation:** a work item with personal data is stored like an event ([f9e173f](https://github.com/ReventlessDev/reventless-core/commit/f9e173fb6f1dc671900052f2c4b6590ee99b5bdf))
11
+
12
+
6
13
  # 3.0.0-alpha.321 (2026-10-30)
7
14
 
8
15
  ### Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-core",
3
- "version": "3.0.0-alpha.321",
3
+ "version": "3.0.0-alpha.322",
4
4
  "description": "Core package for Reventless framework",
5
5
  "license": "Apache-2.0",
6
6
  "jest": {
@@ -31,14 +31,14 @@
31
31
  "@reventlessdev/rescript-effect": "1.0.0-alpha.1",
32
32
  "@reventlessdev/rescript-fast-csv": "2.0.0-alpha.24",
33
33
  "@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
34
- "@reventlessdev/rescript-node": "2.0.0-alpha.24",
35
34
  "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
36
- "@reventlessdev/rescript-uuid": "2.0.0-alpha.1",
37
- "@reventlessdev/rescript-ssh2": "2.0.0-alpha.25",
35
+ "@reventlessdev/rescript-node": "2.0.0-alpha.24",
38
36
  "@reventlessdev/rescript-pulumi-pulumi": "3.0.0-alpha.0",
39
- "@reventlessdev/reventless-infra": "3.0.0-alpha.218",
37
+ "@reventlessdev/reventless-infra": "3.0.0-alpha.219",
38
+ "@reventlessdev/rescript-ssh2": "2.0.0-alpha.25",
40
39
  "@reventlessdev/reventless-interop": "3.0.0-alpha.42",
41
- "@reventlessdev/reventless-spec": "3.0.0-alpha.189"
40
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.190",
41
+ "@reventlessdev/rescript-uuid": "2.0.0-alpha.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "rescript": "12.3.1",
@@ -26,6 +26,12 @@ module Make = (
26
26
  Schedules: {
27
27
  let platformFor: (~slice: string) => ReventlessInfra.AutomationSlice.platformSchedules
28
28
  },
29
+ // What the platform provisioned: the key a work item carrying personal data is
30
+ // fingerprinted with. A thunk, as on the outbound builder, so a platform
31
+ // resolves it when the runtime exists.
32
+ Capabilities: {
33
+ let capabilities: unit => Reventless.Capabilities.t
34
+ },
29
35
  ) => {
30
36
  let finish = EventCollectorRuntimeBuilder.finish
31
37
  module Make = (
@@ -227,7 +233,7 @@ module Make = (
227
233
  Effect.promise(
228
234
  async () => {
229
235
  // Phase 1: per-source decode + collect/resolve, threaded with context
230
- Callback.phase1(jsons, context)
236
+ await Callback.phase1(jsons, context, ~capabilities=Capabilities.capabilities())
231
237
  // Sync the post-phase-1 TODO state so consumers awaiting the
232
238
  // originating publishEvent observe Pending rows immediately.
233
239
  await syncToQueryDb(queryDbOps)
@@ -307,7 +313,11 @@ module Make = (
307
313
  // As the AWS sweep does: the rows phase 2 moved are written back.
308
314
  processPending,
309
315
  tick: async (~key, ~at) => {
310
- Callback.phase1([AutomationSlice_Callback.tickEvent(~key, ~at)], context)
316
+ await Callback.phase1(
317
+ [AutomationSlice_Callback.tickEvent(~key, ~at)],
318
+ context,
319
+ ~capabilities=Capabilities.capabilities(),
320
+ )
311
321
  await syncToQueryDb(queryDbOps)
312
322
  // A once-only schedule's row went when it fired, and a Clock mapping
313
323
  // may change schedules too.
@@ -28,7 +28,7 @@ import * as AutomationSlice_Callback$ReventlessCore from "./AutomationSlice_Call
28
28
  import * as AutomationSlice_Schedules$ReventlessCore from "./AutomationSlice_Schedules.res.mjs";
29
29
 
30
30
  function Make(RuntimeEnvironment) {
31
- return QueryDbStorage => (QueryDbResolvers => (EventCollectorChannel => (EventCollectorRuntimeBuilder => (Api => (Schedules => {
31
+ return QueryDbStorage => (QueryDbResolvers => (EventCollectorChannel => (EventCollectorRuntimeBuilder => (Api => (Schedules => (Capabilities => {
32
32
  let Make = Spec => (Automation => {
33
33
  let Callback = AutomationSlice_Callback$ReventlessCore.Make(Spec)(Automation);
34
34
  let queryDbName = Spec.name + "Todo";
@@ -131,7 +131,7 @@ function Make(RuntimeEnvironment) {
131
131
  name: Spec.name
132
132
  }, opts);
133
133
  let jsonEventsHandler = stream => Effect.flatMap(Stream.runCollect(stream), jsons => Effect.promise(async () => {
134
- Callback.phase1(jsons, context);
134
+ await Callback.phase1(jsons, context, Capabilities.capabilities());
135
135
  await syncToQueryDb(queryDbOps);
136
136
  let changes = Callback.takeScheduleChanges();
137
137
  if (changes.length !== 0) {
@@ -172,7 +172,7 @@ function Make(RuntimeEnvironment) {
172
172
  enqueueEvent: param[0].enqueueEvent,
173
173
  processPending: processPending,
174
174
  tick: async (key, at) => {
175
- Callback.phase1([AutomationSlice_Callback$ReventlessCore.tickEvent(key, at)], context);
175
+ await Callback.phase1([AutomationSlice_Callback$ReventlessCore.tickEvent(key, at)], context, Capabilities.capabilities());
176
176
  await syncToQueryDb(queryDbOps);
177
177
  await syncSchedules(schedulesOps, storedSchedules);
178
178
  await AutomationSlice_Schedules$ReventlessCore.tell(platform, Callback.takeScheduleChanges());
@@ -208,7 +208,7 @@ function Make(RuntimeEnvironment) {
208
208
  finish: EventCollectorRuntimeBuilder.finish,
209
209
  Make: Make
210
210
  };
211
- })))));
211
+ }))))));
212
212
  }
213
213
 
214
214
  export {
@@ -82,7 +82,11 @@ Unlike outbound, phase 1 here takes *raw* envelope JSONs: the callback routes by
82
82
  `meta.service` and unwraps `{id, meta, event}` itself, per-source, through the
83
83
  registered Mappings.
84
84
  */
85
- type phase1 = (array<JSON.t>, Reventless.AutomationSlice.context) => unit
85
+ type phase1 = (
86
+ array<JSON.t>,
87
+ Reventless.AutomationSlice.context,
88
+ ~capabilities: Reventless.Capabilities.t,
89
+ ) => promise<unit>
86
90
 
87
91
  /** Phase 2: process all pending items, publishing commands. */
88
92
  type phase2 = ReventlessInfra.CommandTopic.publishJsons => promise<unit>
@@ -178,6 +182,98 @@ module Make = (
178
182
  // the slice here makes the target's own events unmatchable, silently.
179
183
  let makeMeta = (): Reventless.Message.meta => Message.generateMeta(~service=Spec.targetName)
180
184
 
185
+ // A work item that carries personal data is stored the way an event is: its
186
+ // marked fields sealed, its row keyed by a fingerprint of the key the mapping
187
+ // chose, and that key sealed inside the item under `keyField`, so `process`,
188
+ // `resolve` and `onExhausted` still see the key the slice wrote.
189
+ let personalFields = Reventless.Pii.recordFields(Spec.todoItemSchema)
190
+ let keyField = "$key"
191
+ let fingerprintPurpose = `automationWorkItem:${Spec.name}`
192
+ let warnedUnkeyed = ref(false)
193
+
194
+ /** The id a row is stored under, or `None` when it cannot be stored safely. */
195
+ let storedKey = (key, ~capabilities: Reventless.Capabilities.t): option<string> =>
196
+ if personalFields == [] {
197
+ Some(key)
198
+ } else {
199
+ switch capabilities.secrets.fingerprint(~purpose=fingerprintPurpose, key) {
200
+ | Ok(fingerprint) => Some(fingerprint)
201
+ | Error(Unavailable(why)) =>
202
+ if Reventless.Pii.Provider.isRequired() {
203
+ EffectLogger.logError(
204
+ ~comp=`AutomationSlice(${Spec.name})`,
205
+ `a work item was not stored: its row id cannot be fingerprinted (${why})`,
206
+ )->Effect.runSync
207
+ None
208
+ } else {
209
+ if !warnedUnkeyed.contents {
210
+ warnedUnkeyed := true
211
+ EffectLogger.logWarn(
212
+ ~comp=`AutomationSlice(${Spec.name})`,
213
+ `work items carrying personal data are keyed in the clear: no key to fingerprint them with (${why})`,
214
+ )->Effect.runSync
215
+ }
216
+ Some(key)
217
+ }
218
+ }
219
+ }
220
+
221
+ /** The item as a row stores it: sealed, with the key it was collected under. */
222
+ let storedItem = async (~key, item: Spec.todoItem): result<JSON.t, string> => {
223
+ let json = item->Reventless.Util_Sury.toJson(Spec.todoItemSchema)
224
+ switch (json, personalFields->Array.get(0)) {
225
+ | (_, None) => Ok(json)
226
+ | (Object(data), Some((_, marker))) =>
227
+ let data = data->Dict.copy
228
+ data->Dict.set(keyField, JSON.Encode.string(key))
229
+
230
+ (
231
+ await Reventless.Pii.sealFields(
232
+ ~fields=personalFields->Array.concat([(keyField, marker)]),
233
+ ~label=Spec.name,
234
+ ~bare=true,
235
+ data,
236
+ )
237
+ )
238
+ ->Result.map(d => JSON.Object(d))
239
+ ->Result.mapError(Reventless.Pii.errorMessage)
240
+ | (other, _) => Ok(other)
241
+ }
242
+ }
243
+
244
+ type opened =
245
+ | Open({key: string, item: Spec.todoItem})
246
+ | Gone(string)
247
+ | Unopened(string)
248
+
249
+ /** A row's item and the key it was collected under, opened. */
250
+ let openRow = async (~id, row: todoRow): opened => {
251
+ let decode = (json, ~key) =>
252
+ switch json->Reventless.Util_Sury.fromJson(Spec.todoItemSchema) {
253
+ | item => Open({key, item})
254
+ | exception exn =>
255
+ Unopened(exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown"))
256
+ }
257
+ switch row.item {
258
+ | Object(data) if personalFields != [] =>
259
+ switch await Reventless.Pii.openFields(
260
+ ~fields=personalFields->Array.map(((f, _)) => f)->Array.concat([keyField]),
261
+ data,
262
+ ) {
263
+ | Error(e) => Unopened(e->Reventless.Pii.errorMessage)
264
+ | Ok(data) =>
265
+ switch Reventless.Pii.goneField(Spec.todoItemSchema, data) {
266
+ | Some(field) => Gone(field)
267
+ | None =>
268
+ let key = data->Dict.get(keyField)->Option.flatMap(JSON.Decode.string)->Option.getOr(id)
269
+ data->Dict.delete(keyField)
270
+ JSON.Object(data)->decode(~key)
271
+ }
272
+ }
273
+ | json => json->decode(~key=id)
274
+ }
275
+ }
276
+
181
277
  /** How long a row may stay `Processing` before its command is sent again. */
182
278
  let processingTimeoutMs =
183
279
  15. *. 60. *. 1000.
@@ -193,10 +289,12 @@ module Make = (
193
289
 
194
290
  /** The command a slice wants published now that this item will never be
195
291
  processed again, if it wants one. The row is `Abandoned` either way. */
196
- let abandonmentCommand = (~id: string, ~row: todoRow): option<Reventless.Message.commandJson> =>
197
- switch row.item->Reventless.Util_Sury.fromJson(Spec.todoItemSchema) {
198
- | item =>
199
- Automation.onExhausted(id, item)->Option.flatMap(((targetId, command)) =>
292
+ let abandonmentCommand = async (~id: string, ~row: todoRow): option<
293
+ Reventless.Message.commandJson,
294
+ > =>
295
+ switch await openRow(~id, row) {
296
+ | Open({key, item}) =>
297
+ Automation.onExhausted(key, item)->Option.flatMap(((targetId, command)) =>
200
298
  switch command->Reventless.Util_Sury.toJson(Spec.commandSchema) {
201
299
  | commandJson => Some({Reventless.Message.id: targetId, meta: makeMeta(), commandJson})
202
300
  | exception _ =>
@@ -207,7 +305,8 @@ module Make = (
207
305
  None
208
306
  }
209
307
  )
210
- | exception _ =>
308
+ | Gone(_) => None
309
+ | Unopened(_) =>
211
310
  EffectLogger.logError(
212
311
  ~comp=`AutomationSlice(${Spec.name})`,
213
312
  `retries exhausted for ${id}, and its item could not be decoded to announce it`,
@@ -224,24 +323,37 @@ module Make = (
224
323
  | millis => Some(millis)
225
324
  }
226
325
 
227
- let apply = ({collected, resolved}: AutomationSlice_Route.routed<Spec.todoItem>) => {
228
- // Collect — append new items keyed by ID; first writer wins (idempotent).
229
- collected->Array.forEach(((id, item)) => {
230
- switch todoItems->Dict.get(id) {
231
- | Some(_) => () // Already exists — skip
232
- | None =>
233
- let row: todoRow = {
234
- item: item->Reventless.Util_Sury.toJson(Spec.todoItemSchema),
235
- status: Pending,
236
- createdAt: now(),
237
- retryCount: 0,
238
- maxRetries: Spec.maxRetries,
326
+ let apply = async (
327
+ {collected, resolved}: AutomationSlice_Route.routed<Spec.todoItem>,
328
+ ~capabilities,
329
+ ) => {
330
+ for i in 0 to collected->Array.length - 1 {
331
+ let (key, item) = collected->Array.getUnsafe(i)
332
+ switch storedKey(key, ~capabilities) {
333
+ | None => ()
334
+ | Some(id) if todoItems->Dict.get(id)->Option.isSome => () // Already exists — skip
335
+ | Some(id) =>
336
+ switch await storedItem(~key, item) {
337
+ | Error(why) =>
338
+ EffectLogger.logError(
339
+ ~comp=`AutomationSlice(${Spec.name})`,
340
+ `a work item was not stored: ${why}`,
341
+ )->Effect.runSync
342
+ | Ok(stored) =>
343
+ let row: todoRow = {
344
+ item: stored,
345
+ status: Pending,
346
+ createdAt: now(),
347
+ retryCount: 0,
348
+ maxRetries: Spec.maxRetries,
349
+ }
350
+ todoItems->Dict.set(id, row)
239
351
  }
240
- todoItems->Dict.set(id, row)
241
352
  }
242
- })
243
- // Resolve — mark a pending item as completed.
244
- resolved->Option.forEach(id =>
353
+ }
354
+ resolved
355
+ ->Option.flatMap(key => storedKey(key, ~capabilities))
356
+ ->Option.forEach(id =>
245
357
  todoItems
246
358
  ->Dict.get(id)
247
359
  ->Option.forEach(row => {
@@ -276,8 +388,13 @@ module Make = (
276
388
  )
277
389
  )
278
390
 
279
- let phase1 = (events: array<JSON.t>, sliceCtx: Reventless.AutomationSlice.context) => {
280
- events->Array.forEach(json => {
391
+ let phase1 = async (
392
+ events: array<JSON.t>,
393
+ sliceCtx: Reventless.AutomationSlice.context,
394
+ ~capabilities: Reventless.Capabilities.t,
395
+ ) =>
396
+ for i in 0 to events->Array.length - 1 {
397
+ let json = events->Array.getUnsafe(i)
281
398
  let context = json->Message.decode(Reventless.Message.contextSchema)
282
399
  let ctx = switch recordedAtOf(context.meta.time) {
283
400
  | Some(recordedAt) => {...sliceCtx, recordedAt}
@@ -293,7 +410,9 @@ module Make = (
293
410
  }
294
411
  // Multiple mappings can share a sourceName; all matching dispatches run.
295
412
  let routed = Route.route(eventPayload, ~sourceName, ~sourceId=context.id, ctx)
296
- routed->Array.forEach(apply)
413
+ for j in 0 to routed->Array.length - 1 {
414
+ await apply(routed->Array.getUnsafe(j), ~capabilities)
415
+ }
297
416
  reschedule(
298
417
  routed,
299
418
  ~setBy=context.meta.msgId,
@@ -304,8 +423,7 @@ module Make = (
304
423
  if sourceName == Reventless.Clock.name {
305
424
  schedules->AutomationSlice_Schedules.fired(~key=context.id)
306
425
  }
307
- })
308
- }
426
+ }
309
427
 
310
428
  let rebuildSchedules = (events: array<JSON.t>) => {
311
429
  schedules->Dict.keysToArray->Array.forEach(key => schedules->Dict.delete(key))
@@ -367,20 +485,22 @@ module Make = (
367
485
 
368
486
  let commands = []
369
487
 
370
- pending->Array.forEach(((id, row)) => {
371
- let item = try row.item->Reventless.Util_Sury.fromJson(Spec.todoItemSchema)->Some catch {
372
- | exn =>
373
- let errMsg =
374
- exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown")
488
+ for i in 0 to pending->Array.length - 1 {
489
+ let (id, row) = pending->Array.getUnsafe(i)
490
+ switch await openRow(~id, row) {
491
+ | Gone(field) =>
492
+ EffectLogger.logInfo(
493
+ ~comp=`AutomationSlice(${Spec.name})`,
494
+ `${id}: its ${field} was erased; nothing to do`,
495
+ )->Effect.runSync
496
+ todoItems->Dict.set(id, {...row, status: Completed, completedAt: now()})
497
+ | Unopened(errMsg) =>
375
498
  EffectLogger.logError(
376
499
  ~comp=`AutomationSlice(${Spec.name})`,
377
500
  `failed to decode todoItem: ${errMsg}`,
378
501
  )->Effect.runSync
379
- None
380
- }
381
-
382
- item->Option.forEach(item => {
383
- switch Automation.process(id, item, ~now=currentTime) {
502
+ | Open({key, item}) =>
503
+ switch Automation.process(key, item, ~now=currentTime) {
384
504
  | Some((targetId, command)) =>
385
505
  todoItems->Dict.set(id, {...row, status: Processing, processedAt: now()})
386
506
  let commandJson = try command
@@ -400,20 +520,18 @@ module Make = (
400
520
  }
401
521
  None
402
522
  }
403
- commandJson->Option.forEach(
404
- commandJson => {
405
- let msg: Reventless.Message.commandJson = {
406
- id: targetId,
407
- meta: makeMeta(),
408
- commandJson,
409
- }
410
- let _ = commands->Array.push(msg)
411
- },
412
- )
523
+ commandJson->Option.forEach(commandJson => {
524
+ let msg: Reventless.Message.commandJson = {
525
+ id: targetId,
526
+ meta: makeMeta(),
527
+ commandJson,
528
+ }
529
+ let _ = commands->Array.push(msg)
530
+ })
413
531
  | None => () // Skip — process returned None
414
532
  }
415
- })
416
- })
533
+ }
534
+ }
417
535
 
418
536
  if commands->Array.length > 0 {
419
537
  try await publishJsons(commands) catch {
@@ -442,10 +560,18 @@ module Make = (
442
560
  // Announced after the batch, so a publish failure above has already had its
443
561
  // say on which rows ran out of budget. A failure here cannot mark anything for
444
562
  // retry — the budget is what ran out — so the rows stay `Abandoned` regardless.
445
- let abandonmentCommands =
446
- newlyAbandoned->Array.filterMap(id =>
447
- todoItems->Dict.get(id)->Option.flatMap(row => abandonmentCommand(~id, ~row))
448
- )
563
+ let abandonmentCommands = []
564
+ for i in 0 to newlyAbandoned->Array.length - 1 {
565
+ let id = newlyAbandoned->Array.getUnsafe(i)
566
+ switch todoItems->Dict.get(id) {
567
+ | Some(row) =>
568
+ switch await abandonmentCommand(~id, ~row) {
569
+ | Some(command) => abandonmentCommands->Array.push(command)
570
+ | None => ()
571
+ }
572
+ | None => ()
573
+ }
574
+ }
449
575
  if abandonmentCommands->Array.length > 0 {
450
576
  try await publishJsons(abandonmentCommands) catch {
451
577
  | exn =>
@@ -3,13 +3,14 @@
3
3
  import * as Sury from "sury";
4
4
  import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
5
5
  import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
6
- import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
7
6
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
8
7
  import * as Clock from "effect/Clock";
9
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
9
+ import * as Stdlib_Result from "@rescript/runtime/lib/es6/Stdlib_Result.js";
10
10
  import * as Effect from "effect/Effect";
11
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
11
12
  import * as Clock$Reventless from "@reventlessdev/reventless-spec/src/components/Clock.res.mjs";
12
- import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
13
+ import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
13
14
  import * as Message$Reventless from "@reventlessdev/reventless-spec/src/types/Message.res.mjs";
14
15
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
15
16
  import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
@@ -84,6 +85,98 @@ function Make(Spec) {
84
85
  };
85
86
  let readsClock = Automation.mappings.some(M => M.sourceName === Clock$Reventless.name);
86
87
  let makeMeta = () => Message$ReventlessCore.generateMeta(Spec.targetName, undefined, undefined, undefined, undefined, undefined, undefined);
88
+ let personalFields = Pii$Reventless.recordFields(Spec.todoItemSchema);
89
+ let keyField = "$key";
90
+ let fingerprintPurpose = `automationWorkItem:` + Spec.name;
91
+ let warnedUnkeyed = {
92
+ contents: false
93
+ };
94
+ let storedKey = (key, capabilities) => {
95
+ if (Primitive_object.equal(personalFields, [])) {
96
+ return key;
97
+ }
98
+ let fingerprint = capabilities.secrets.fingerprint(fingerprintPurpose, key);
99
+ if (fingerprint.TAG === "Ok") {
100
+ return fingerprint._0;
101
+ }
102
+ let why = fingerprint._0._0;
103
+ if (Pii$Reventless.Provider.isRequired()) {
104
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `a work item was not stored: its row id cannot be fingerprinted (` + why + `)`));
105
+ return;
106
+ } else {
107
+ if (!warnedUnkeyed.contents) {
108
+ warnedUnkeyed.contents = true;
109
+ Effect.runSync(EffectLogger$ReventlessCore.logWarn(`AutomationSlice(` + Spec.name + `)`, undefined, `work items carrying personal data are keyed in the clear: no key to fingerprint them with (` + why + `)`));
110
+ }
111
+ return key;
112
+ }
113
+ };
114
+ let storedItem = async (key, item) => {
115
+ let json = Util_Sury$Reventless.toJson(item, Spec.todoItemSchema);
116
+ let match = personalFields[0];
117
+ if (match === undefined) {
118
+ return {
119
+ TAG: "Ok",
120
+ _0: json
121
+ };
122
+ }
123
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
124
+ return {
125
+ TAG: "Ok",
126
+ _0: json
127
+ };
128
+ }
129
+ let data = Object.assign({}, json);
130
+ data[keyField] = key;
131
+ return Stdlib_Result.mapError(Stdlib_Result.map(await Pii$Reventless.sealFields(personalFields.concat([[
132
+ keyField,
133
+ match[1]
134
+ ]]), Spec.name, undefined, true, data), d => (d)), Pii$Reventless.errorMessage);
135
+ };
136
+ let openRow = async (id, row) => {
137
+ let decode = (json, key) => {
138
+ let item;
139
+ try {
140
+ item = Util_Sury$Reventless.fromJson(json, Spec.todoItemSchema);
141
+ } catch (raw_exn) {
142
+ let exn = Primitive_exceptions.internalToException(raw_exn);
143
+ return {
144
+ TAG: "Unopened",
145
+ _0: Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown")
146
+ };
147
+ }
148
+ return {
149
+ TAG: "Open",
150
+ key: key,
151
+ item: item
152
+ };
153
+ };
154
+ let data = row.item;
155
+ if (typeof data !== "object" || data === null || Array.isArray(data)) {
156
+ return decode(data, id);
157
+ }
158
+ if (!Primitive_object.notequal(personalFields, [])) {
159
+ return decode(data, id);
160
+ }
161
+ let e = await Pii$Reventless.openFields(personalFields.map(param => param[0]).concat([keyField]), data);
162
+ if (e.TAG !== "Ok") {
163
+ return {
164
+ TAG: "Unopened",
165
+ _0: Pii$Reventless.errorMessage(e._0)
166
+ };
167
+ }
168
+ let data$1 = e._0;
169
+ let field = Pii$Reventless.goneField(Spec.todoItemSchema, data$1);
170
+ if (field !== undefined) {
171
+ return {
172
+ TAG: "Gone",
173
+ _0: field
174
+ };
175
+ }
176
+ let key = Stdlib_Option.getOr(Stdlib_Option.flatMap(data$1[keyField], Stdlib_JSON.Decode.string), id);
177
+ Stdlib_Dict.$$delete(data$1, keyField);
178
+ return decode(data$1, key);
179
+ };
87
180
  let processingTimeoutMs = 15 * 60 * 1000;
88
181
  let afterFailedAttempt = row => {
89
182
  let retryCount = row.retryCount + 1 | 0;
@@ -92,6 +185,31 @@ function Make(Spec) {
92
185
  newrecord.status = retryCount >= Spec.maxRetries ? "Abandoned" : "Failed";
93
186
  return newrecord;
94
187
  };
188
+ let abandonmentCommand = async (id, row) => {
189
+ let match = await openRow(id, row);
190
+ switch (match.TAG) {
191
+ case "Open" :
192
+ return Stdlib_Option.flatMap(Automation.onExhausted(match.key, match.item), param => {
193
+ let commandJson;
194
+ try {
195
+ commandJson = Util_Sury$Reventless.toJson(param[1], Spec.commandSchema);
196
+ } catch (exn) {
197
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `retries exhausted for ` + id + `, and the abandonment command could not be encoded`));
198
+ return;
199
+ }
200
+ return {
201
+ id: param[0],
202
+ meta: makeMeta(),
203
+ commandJson: commandJson
204
+ };
205
+ });
206
+ case "Gone" :
207
+ return;
208
+ case "Unopened" :
209
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `retries exhausted for ` + id + `, and its item could not be decoded to announce it`));
210
+ return;
211
+ }
212
+ };
95
213
  let Route = AutomationSlice_Route$ReventlessCore.Make(Spec)(Automation);
96
214
  let recordedAtOf = time => {
97
215
  let millis = new Date(time).getTime();
@@ -101,26 +219,32 @@ function Make(Spec) {
101
219
  return millis;
102
220
  }
103
221
  };
104
- let apply = param => {
105
- param.collected.forEach(param => {
106
- let id = param[0];
107
- let match = todoItems[id];
108
- if (match !== undefined) {
109
- return;
222
+ let apply = async (param, capabilities) => {
223
+ let collected = param.collected;
224
+ for (let i = 0, i_finish = collected.length; i < i_finish; ++i) {
225
+ let match = collected[i];
226
+ let key = match[0];
227
+ let id = storedKey(key, capabilities);
228
+ if (id !== undefined && !Stdlib_Option.isSome(todoItems[id])) {
229
+ let why = await storedItem(key, match[1]);
230
+ if (why.TAG === "Ok") {
231
+ let row_item = why._0;
232
+ let row_createdAt = new Date().toISOString();
233
+ let row_maxRetries = Spec.maxRetries;
234
+ let row = {
235
+ item: row_item,
236
+ status: "Pending",
237
+ createdAt: row_createdAt,
238
+ retryCount: 0,
239
+ maxRetries: row_maxRetries
240
+ };
241
+ todoItems[id] = row;
242
+ } else {
243
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `a work item was not stored: ` + why._0));
244
+ }
110
245
  }
111
- let row_item = Util_Sury$Reventless.toJson(param[1], Spec.todoItemSchema);
112
- let row_createdAt = new Date().toISOString();
113
- let row_maxRetries = Spec.maxRetries;
114
- let row = {
115
- item: row_item,
116
- status: "Pending",
117
- createdAt: row_createdAt,
118
- retryCount: 0,
119
- maxRetries: row_maxRetries
120
- };
121
- todoItems[id] = row;
122
- });
123
- Stdlib_Option.forEach(param.resolved, id => Stdlib_Option.forEach(todoItems[id], row => {
246
+ }
247
+ return Stdlib_Option.forEach(Stdlib_Option.flatMap(param.resolved, key => storedKey(key, capabilities)), id => Stdlib_Option.forEach(todoItems[id], row => {
124
248
  let newrecord = {...row};
125
249
  newrecord.completedAt = new Date().toISOString();
126
250
  newrecord.status = "Completed";
@@ -143,8 +267,9 @@ function Make(Spec) {
143
267
  });
144
268
  });
145
269
  };
146
- let phase1 = (events, sliceCtx) => {
147
- events.forEach(json => {
270
+ let phase1 = async (events, sliceCtx, capabilities) => {
271
+ for (let i = 0, i_finish = events.length; i < i_finish; ++i) {
272
+ let json = events[i];
148
273
  let context = Message$ReventlessCore.decode(json, Message$Reventless.contextSchema);
149
274
  let recordedAt = recordedAtOf(context.meta.time);
150
275
  let ctx;
@@ -159,12 +284,14 @@ function Make(Spec) {
159
284
  let dict = Stdlib_JSON.Decode.object(json);
160
285
  let eventPayload = dict !== undefined ? Stdlib_Option.getOr(dict["event"], json) : json;
161
286
  let routed = Route.route(eventPayload, sourceName, context.id, ctx);
162
- routed.forEach(apply);
287
+ for (let j = 0, j_finish = routed.length; j < j_finish; ++j) {
288
+ await apply(routed[j], capabilities);
289
+ }
163
290
  reschedule(routed, context.meta.msgId, Stdlib_Option.getOr(ctx.recordedAt, Date.now()));
164
291
  if (sourceName === Clock$Reventless.name) {
165
- return AutomationSlice_Schedules$ReventlessCore.fired(schedules, context.id);
292
+ AutomationSlice_Schedules$ReventlessCore.fired(schedules, context.id);
166
293
  }
167
- });
294
+ }
168
295
  };
169
296
  let rebuildSchedules = events => {
170
297
  Object.keys(schedules).forEach(key => Stdlib_Dict.$$delete(schedules, key));
@@ -229,60 +356,64 @@ function Make(Spec) {
229
356
  }
230
357
  });
231
358
  let commands = [];
232
- pending.forEach(param => {
233
- let row = param[1];
234
- let id = param[0];
235
- let item;
236
- try {
237
- item = Primitive_option.some(Util_Sury$Reventless.fromJson(row.item, Spec.todoItemSchema));
238
- } catch (raw_exn) {
239
- let exn = Primitive_exceptions.internalToException(raw_exn);
240
- let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
241
- Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to decode todoItem: ` + errMsg));
242
- item = undefined;
243
- }
244
- Stdlib_Option.forEach(item, item => {
245
- let match = Automation.process(id, item, currentTime);
246
- if (match === undefined) {
247
- return;
248
- }
249
- let targetId = match[0];
250
- let newrecord = {...row};
251
- newrecord.processedAt = new Date().toISOString();
252
- newrecord.status = "Processing";
253
- todoItems[id] = newrecord;
254
- let commandJson;
255
- try {
256
- commandJson = Util_Sury$Reventless.toJson(match[1], Spec.commandSchema);
257
- } catch (raw_exn) {
258
- let exn = Primitive_exceptions.internalToException(raw_exn);
259
- let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
260
- Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to encode command: ` + errMsg));
261
- let updated = afterFailedAttempt(row);
262
- todoItems[id] = updated;
263
- if (updated.status === "Abandoned") {
264
- newlyAbandoned.push(id);
359
+ for (let i = 0, i_finish = pending.length; i < i_finish; ++i) {
360
+ let match = pending[i];
361
+ let row = match[1];
362
+ let id = match[0];
363
+ let field = await openRow(id, row);
364
+ switch (field.TAG) {
365
+ case "Open" :
366
+ let match$1 = Automation.process(field.key, field.item, currentTime);
367
+ if (match$1 !== undefined) {
368
+ let targetId = match$1[0];
369
+ let newrecord = {...row};
370
+ newrecord.processedAt = new Date().toISOString();
371
+ newrecord.status = "Processing";
372
+ todoItems[id] = newrecord;
373
+ let commandJson;
374
+ try {
375
+ commandJson = Util_Sury$Reventless.toJson(match$1[1], Spec.commandSchema);
376
+ } catch (raw_exn) {
377
+ let exn = Primitive_exceptions.internalToException(raw_exn);
378
+ let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
379
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to encode command: ` + errMsg));
380
+ let updated = afterFailedAttempt(row);
381
+ todoItems[id] = updated;
382
+ if (updated.status === "Abandoned") {
383
+ newlyAbandoned.push(id);
384
+ }
385
+ commandJson = undefined;
386
+ }
387
+ Stdlib_Option.forEach(commandJson, commandJson => {
388
+ let msg_meta = makeMeta();
389
+ let msg = {
390
+ id: targetId,
391
+ meta: msg_meta,
392
+ commandJson: commandJson
393
+ };
394
+ commands.push(msg);
395
+ });
265
396
  }
266
- commandJson = undefined;
267
- }
268
- Stdlib_Option.forEach(commandJson, commandJson => {
269
- let msg_meta = makeMeta();
270
- let msg = {
271
- id: targetId,
272
- meta: msg_meta,
273
- commandJson: commandJson
274
- };
275
- commands.push(msg);
276
- });
277
- });
278
- });
397
+ break;
398
+ case "Gone" :
399
+ Effect.runSync(EffectLogger$ReventlessCore.logInfo(`AutomationSlice(` + Spec.name + `)`, undefined, id + `: its ` + field._0 + ` was erased; nothing to do`));
400
+ let newrecord$1 = {...row};
401
+ newrecord$1.completedAt = new Date().toISOString();
402
+ newrecord$1.status = "Completed";
403
+ todoItems[id] = newrecord$1;
404
+ break;
405
+ case "Unopened" :
406
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to decode todoItem: ` + field._0));
407
+ break;
408
+ }
409
+ }
279
410
  if (commands.length !== 0) {
280
411
  try {
281
412
  await publishJsons(commands);
282
- } catch (raw_exn) {
283
- let exn = Primitive_exceptions.internalToException(raw_exn);
284
- let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
285
- Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to publish commands: ` + errMsg));
413
+ } catch (raw_exn$1) {
414
+ let exn$1 = Primitive_exceptions.internalToException(raw_exn$1);
415
+ let errMsg$1 = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn$1), Stdlib_JsExn.message), "unknown");
416
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to publish commands: ` + errMsg$1));
286
417
  pending.forEach(param => {
287
418
  let id = param[0];
288
419
  let current = todoItems[id];
@@ -301,38 +432,26 @@ function Make(Spec) {
301
432
  });
302
433
  }
303
434
  }
304
- let abandonmentCommands = Stdlib_Array.filterMap(newlyAbandoned, id => Stdlib_Option.flatMap(todoItems[id], row => {
305
- let item;
306
- try {
307
- item = Util_Sury$Reventless.fromJson(row.item, Spec.todoItemSchema);
308
- } catch (exn) {
309
- Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `retries exhausted for ` + id + `, and its item could not be decoded to announce it`));
310
- return;
311
- }
312
- return Stdlib_Option.flatMap(Automation.onExhausted(id, item), param => {
313
- let commandJson;
314
- try {
315
- commandJson = Util_Sury$Reventless.toJson(param[1], Spec.commandSchema);
316
- } catch (exn) {
317
- Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `retries exhausted for ` + id + `, and the abandonment command could not be encoded`));
318
- return;
435
+ let abandonmentCommands = [];
436
+ for (let i$1 = 0, i_finish$1 = newlyAbandoned.length; i$1 < i_finish$1; ++i$1) {
437
+ let id$1 = newlyAbandoned[i$1];
438
+ let row$1 = todoItems[id$1];
439
+ if (row$1 !== undefined) {
440
+ let command = await abandonmentCommand(id$1, row$1);
441
+ if (command !== undefined) {
442
+ abandonmentCommands.push(command);
319
443
  }
320
- return {
321
- id: param[0],
322
- meta: makeMeta(),
323
- commandJson: commandJson
324
- };
325
- });
326
- }));
444
+ }
445
+ }
327
446
  if (abandonmentCommands.length === 0) {
328
447
  return;
329
448
  }
330
449
  try {
331
450
  return await publishJsons(abandonmentCommands);
332
- } catch (raw_exn$1) {
333
- let exn$1 = Primitive_exceptions.internalToException(raw_exn$1);
334
- let errMsg$1 = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn$1), Stdlib_JsExn.message), "unknown");
335
- return Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to publish abandonment commands: ` + errMsg$1));
451
+ } catch (raw_exn$2) {
452
+ let exn$2 = Primitive_exceptions.internalToException(raw_exn$2);
453
+ let errMsg$2 = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn$2), Stdlib_JsExn.message), "unknown");
454
+ return Effect.runSync(EffectLogger$ReventlessCore.logError(`AutomationSlice(` + Spec.name + `)`, undefined, `failed to publish abandonment commands: ` + errMsg$2));
336
455
  }
337
456
  };
338
457
  return {
@@ -21,3 +21,8 @@ type command = Join({@owner memberId: string, @pii name: string, @pii address: R
21
21
  // only ever opened, so it needs no subject.
22
22
  @schema
23
23
  type consumedEvent = CustomerRegistered({@pii email: Reventless.Email.t})
24
+
25
+ // An automation's work item: the type stays, and the mark keeps it out of logs
26
+ // and makes the work list seal it.
27
+ @schema
28
+ type todoItem = {memberId: string, @pii("memberId") name: string}
@@ -35,6 +35,11 @@ let consumedEventSchema = Sury.$schema(s => ({
35
35
  email: s.m(Pii$Reventless.marked("$stream", undefined, Email$Reventless.schema))
36
36
  }));
37
37
 
38
+ let todoItemSchema = Sury.$schema(s => ({
39
+ memberId: s.m(Sury.string),
40
+ name: s.m(Pii$Reventless.mark("memberId", undefined, Sury.string))
41
+ }));
42
+
38
43
  function authorizationOf(param) {
39
44
  return "AllowAuthenticated";
40
45
  }
@@ -59,6 +64,7 @@ export {
59
64
  eventSchema,
60
65
  commandSchema,
61
66
  consumedEventSchema,
67
+ todoItemSchema,
62
68
  moduleUrl,
63
69
  authorizationOf,
64
70
  readConsistency,
@@ -53,6 +53,14 @@ describe("@pii on an event", () => {
53
53
  ])
54
54
  })
55
55
 
56
+ testSync("an automation's work item is marked and keeps its type", () => {
57
+ let item: M.todoItem = {memberId: "m-1", name: "Ada"}
58
+ expect(
59
+ M.todoItemSchema->Reventless.Pii.recordFields->Array.map(((name, m)) => (name, m.subject)),
60
+ )->toEqual([("name", "memberId")])
61
+ expect(item.name)->toBe("Ada")
62
+ })
63
+
56
64
  testSync("a consumed field naming no subject is about the aggregate it came from", () =>
57
65
  expect(
58
66
  M.consumedEventSchema
@@ -110,6 +110,16 @@ globalThis.describe("@pii on an event", () => {
110
110
  "invitationId"
111
111
  ]]);
112
112
  });
113
+ globalThis.test("an automation's work item is marked and keeps its type", () => {
114
+ globalThis.expect(Pii$Reventless.recordFields(PiiMember$ReventlessCore.todoItemSchema).map(param => [
115
+ param[0],
116
+ param[1].subject
117
+ ])).toEqual([[
118
+ "name",
119
+ "memberId"
120
+ ]]);
121
+ globalThis.expect("Ada").toBe("Ada");
122
+ });
113
123
  globalThis.test("a consumed field naming no subject is about the aggregate it came from", () => {
114
124
  globalThis.expect(Pii$Reventless.variantFields(PiiMember$ReventlessCore.consumedEventSchema, "CustomerRegistered").map(param => [
115
125
  param[0],