@reventlessdev/reventless-core 3.0.0-alpha.315 → 3.0.0-alpha.317

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 (31) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/package.json +7 -7
  3. package/src/Message.res +3 -1
  4. package/src/Message.res.mjs +4 -1
  5. package/src/components/Aggregate/Aggregate_Callback.res +3 -0
  6. package/src/components/Aggregate/Aggregate_Callback.res.mjs +1 -1
  7. package/src/components/EventLog/EventLog_Operations.res +174 -53
  8. package/src/components/EventLog/EventLog_Operations.res.mjs +148 -38
  9. package/src/components/Extension/Extension_Operations.res +8 -2
  10. package/src/components/Extension/Extension_Operations.res.mjs +9 -2
  11. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +3 -0
  12. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +4 -0
  13. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +170 -90
  14. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +153 -69
  15. package/src/components/StateViewSlice/StateViewSlice_Builder.res +6 -0
  16. package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +8 -1
  17. package/src/components/StateViewSlice/StateViewSlice_Callback.res +5 -0
  18. package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +5 -0
  19. package/src/util/Lru.res +33 -6
  20. package/src/util/Lru.res.mjs +32 -8
  21. package/src/util/Lru.resi +8 -2
  22. package/tests/dcb/DcbPiiSliceTest.res +142 -0
  23. package/tests/dcb/DcbPiiSliceTest.res.mjs +275 -0
  24. package/tests/eventlog/EventLogFixtures.res +14 -5
  25. package/tests/eventlog/EventLogFixtures.res.mjs +10 -4
  26. package/tests/eventlog/EventLogPiiTest.res +114 -0
  27. package/tests/eventlog/EventLogPiiTest.res.mjs +230 -0
  28. package/tests/pii/PiiPrefetchTest.res +116 -0
  29. package/tests/pii/PiiPrefetchTest.res.mjs +206 -0
  30. package/tests/util/LruTest.res +35 -0
  31. package/tests/util/LruTest.res.mjs +46 -8
package/CHANGELOG.md CHANGED
@@ -3,6 +3,29 @@
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.317 (2026-10-29)
7
+
8
+ * feat(core)!: every reader opens a sealed field, with the keys its batch prepared ([d4ac204](https://github.com/ReventlessDev/reventless-core/commit/d4ac204c307d34617d4e32c556030b44bb55b178))
9
+
10
+ ### BREAKING CHANGES
11
+
12
+ * `Pii.provider` has `prefetch` and `openNow` in place of `open_`.
13
+
14
+ Not yet: `prepare` in the AWS entry points, the conformance scenario, the runs on
15
+ DynamoDB and Postgres.
16
+
17
+ Begins step PD10 of docs/plans/a-personal-field-is-marked-redacted-and-encrypted-at-the-seam.md.
18
+ Claude-Session: https://claude.ai/code/session_01HiQfdxVEsHwLHRoHSUcus1
19
+
20
+
21
+
22
+ # 3.0.0-alpha.316 (2026-10-29)
23
+
24
+ ### Features
25
+
26
+ * **core:** an event's personal fields are sealed once at append and opened on read ([ef3daae](https://github.com/ReventlessDev/reventless-core/commit/ef3daae9ad02090b9ec6de4decbe41c44b5dd554))
27
+
28
+
6
29
  # 3.0.0-alpha.315 (2026-10-29)
7
30
 
8
31
  * feat(ppx)!: `@pii` marks a value about a person, and an event reads it as `Pii.t` ([37174c1](https://github.com/ReventlessDev/reventless-core/commit/37174c10eb80e9e3adbb93a52ad7a9ef5def7100))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-core",
3
- "version": "3.0.0-alpha.315",
3
+ "version": "3.0.0-alpha.317",
4
4
  "description": "Core package for Reventless framework",
5
5
  "license": "Apache-2.0",
6
6
  "jest": {
@@ -30,15 +30,15 @@
30
30
  "uuid": "^13.0.0",
31
31
  "@reventlessdev/rescript-effect": "1.0.0-alpha.1",
32
32
  "@reventlessdev/rescript-fast-csv": "2.0.0-alpha.24",
33
- "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
34
- "@reventlessdev/rescript-node": "2.0.0-alpha.24",
35
33
  "@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
34
+ "@reventlessdev/rescript-node": "2.0.0-alpha.24",
35
+ "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
36
+ "@reventlessdev/rescript-ssh2": "2.0.0-alpha.25",
36
37
  "@reventlessdev/rescript-pulumi-pulumi": "3.0.0-alpha.0",
37
- "@reventlessdev/reventless-infra": "3.0.0-alpha.212",
38
+ "@reventlessdev/reventless-infra": "3.0.0-alpha.214",
39
+ "@reventlessdev/rescript-uuid": "2.0.0-alpha.1",
38
40
  "@reventlessdev/reventless-interop": "3.0.0-alpha.42",
39
- "@reventlessdev/reventless-spec": "3.0.0-alpha.183",
40
- "@reventlessdev/rescript-ssh2": "2.0.0-alpha.25",
41
- "@reventlessdev/rescript-uuid": "2.0.0-alpha.1"
41
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.185"
42
42
  },
43
43
  "devDependencies": {
44
44
  "rescript": "12.3.1",
package/src/Message.res CHANGED
@@ -71,7 +71,9 @@ let toCommandSchema' = (idSchema, commandSchema) =>
71
71
  // `Reventless.Message.decode` (the other central decoder) is tolerant at the source.
72
72
  // See docs/plans/done/platform-infrastructure-in-plugin-list.md (durable fix option 2).
73
73
  let decodeEvent' = (json, idSchema, eventSchema) =>
74
- json->parseJsonTolerant(toEventSchema'(idSchema, eventSchema))
74
+ json
75
+ ->Reventless.Message.openEnvelope(eventSchema)
76
+ ->parseJsonTolerant(toEventSchema'(idSchema, eventSchema))
75
77
  let decodeCommand' = (json, idSchema, commandSchema) =>
76
78
  json->parseJsonTolerant(toCommandSchema'(idSchema, commandSchema))
77
79
 
@@ -46,7 +46,7 @@ function toCommandSchema$p(idSchema, commandSchema) {
46
46
  }
47
47
 
48
48
  function decodeEvent$p(json, idSchema, eventSchema) {
49
- return Message$Reventless.parseJsonTolerant(json, toEventSchema$p(idSchema, eventSchema));
49
+ return Message$Reventless.parseJsonTolerant(Message$Reventless.openEnvelope(json, eventSchema), toEventSchema$p(idSchema, eventSchema));
50
50
  }
51
51
 
52
52
  function decodeCommand$p(json, idSchema, commandSchema) {
@@ -410,6 +410,8 @@ let encode = Message$Reventless.encode;
410
410
 
411
411
  let InvalidEvent = Message$Reventless.InvalidEvent;
412
412
 
413
+ let openEnvelope = Message$Reventless.openEnvelope;
414
+
413
415
  let deriveStableMeta = Message$ReventlessInfra.deriveStableMeta;
414
416
 
415
417
  let hasStableMsgId = Message$ReventlessInfra.hasStableMsgId;
@@ -427,6 +429,7 @@ export {
427
429
  decode,
428
430
  encode,
429
431
  InvalidEvent,
432
+ openEnvelope,
430
433
  _rawMemoize,
431
434
  memoizeBySchemaPair,
432
435
  toEventSchema$p,
@@ -201,8 +201,11 @@ module Make = (
201
201
  // the fixed capacity (a per-aggregate knob is a future refinement — see
202
202
  // docs/plans/done/aggregate-snapshotting.md).
203
203
  let replayCacheCapacity = 100
204
+ // Decoded state holds personal values in the clear: it is served for minutes at
205
+ // most, so an erased person's leaves a warm container within a named bound.
204
206
  let replayCache: Lru.t<string, (Decision.state, int, array<string>)> = Lru.make(
205
207
  ~capacity=replayCacheCapacity,
208
+ ~ttlMs=Lru.decodedStateTtlMs(),
206
209
  )
207
210
 
208
211
  let resetCache = () => replayCache->Lru.clear
@@ -129,7 +129,7 @@ function Make(Spec) {
129
129
  ]])
130
130
  };
131
131
  });
132
- let replayCache = Lru$ReventlessCore.make(100);
132
+ let replayCache = Lru$ReventlessCore.make(100, Lru$ReventlessCore.decodedStateTtlMs(), undefined);
133
133
  let resetCache = () => Lru$ReventlessCore.clear(replayCache);
134
134
  let snapshotConfig = actsOnOwned ? undefined : Decision.snapshot;
135
135
  if (actsOnOwned && Stdlib_Option.isSome(Decision.snapshot)) {
@@ -44,22 +44,63 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
44
44
  ) => {
45
45
  module Spec = Spec
46
46
 
47
- let encodeEvent' = (id, sequenceNr, event') => {
48
- let json = event'.Reventless.Message.event->Message.encode(Spec.eventSchema)
49
- let (eventType, data) = json->Message.splitMessage
47
+ // The stored item for an event already encoded and sealed: its type and fields.
48
+ let storedEventOf = (id, sequenceNr, meta, (eventType, data)) => {
50
49
  let stored: Reventless.StoredEvent.storedEvent<Spec.Id.t> = {
51
50
  id,
52
51
  position: sequenceNr->Int.toString->String.padStart(9, "0"),
53
52
  event: eventType,
54
53
  data: JSON.Object(data),
55
- meta: event'.meta,
54
+ meta,
56
55
  recordedAt: Message.nowAsISOString(),
57
56
  }
58
57
  stored->Message.storedEventToFlatJson(Spec.Id.schema)
59
58
  }
60
59
 
61
- let encodeEvents' = (events', id, startingSeqNr) =>
62
- events'->Array.mapWithIndex((event, i) => encodeEvent'(id, startingSeqNr + i, event))
60
+ /**
61
+ Every event encoded once and sealed once: the form both the stored item and the
62
+ published message are built from. An encrypting provider draws a fresh nonce on
63
+ every seal, so a second encode would publish a ciphertext the log does not hold.
64
+ Refused, never degraded, when a personal field cannot be sealed or comes out
65
+ unsealed; the message names the event type and the field, never the value.
66
+ */
67
+ let sealEvents = async (events': array<Message.event'<Spec.Id.t, Spec.event>>): result<
68
+ array<(string, dict<JSON.t>)>,
69
+ string,
70
+ > => {
71
+ let out = []
72
+ let failure = ref(None)
73
+ for i in 0 to events'->Array.length - 1 {
74
+ if failure.contents->Option.isNone {
75
+ let (eventType, data) =
76
+ (events'->Array.getUnsafe(i)).event
77
+ ->Message.encode(Spec.eventSchema)
78
+ ->Message.splitMessage
79
+ switch await Reventless.Pii.sealData(Spec.eventSchema, ~eventType, data) {
80
+ | Error(e) => failure := Some(`cannot seal ${eventType}: ${e->Reventless.Pii.errorMessage}`)
81
+ | Ok(sealed) =>
82
+ switch Reventless.Pii.unsealedField(Spec.eventSchema, ~eventType, sealed) {
83
+ | Some(field) =>
84
+ failure := Some(`refused ${eventType}: its personal field ${field} is not sealed`)
85
+ | None => out->Array.push((eventType, sealed))
86
+ }
87
+ }
88
+ }
89
+ }
90
+ switch failure.contents {
91
+ | Some(msg) => Error(msg)
92
+ | None => Ok(out)
93
+ }
94
+ }
95
+
96
+ /** What the topic publishes for one sealed event: the envelope `encodeEvent'`
97
+ writes, with the sealed fields rather than a second encode. */
98
+ let publishedJsonOf = (id, meta, (eventType, data)) =>
99
+ Dict.fromArray([
100
+ ("id", id->Reventless.Util_Sury.toJson(Spec.Id.schema)),
101
+ ("meta", meta->Message.encode(Message.metaSchema)),
102
+ ("event", Message.combineMessage(eventType, data)),
103
+ ])->JSON.Encode.object
63
104
 
64
105
  let makePublishedEvent = (idStr, eventsJson, meta): EventPublish_Callback.publishedEvent => {
65
106
  componentName: Spec.name,
@@ -104,13 +145,30 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
104
145
  }
105
146
 
106
147
  // Returns result<unit, string> — never throws. Publish failures are surfaced as Error.
107
- let publishToEventTopic = async (id, events': array<Message.event'<_, _>>, eventsJson) => {
148
+ // Publishes the sealed form the log stored, never a fresh encode.
149
+ let publishToEventTopic = async (
150
+ id,
151
+ events': array<Message.event'<_, _>>,
152
+ sealed: array<(string, dict<JSON.t>)>,
153
+ eventsJson,
154
+ ) => {
108
155
  let idStr = id->Spec.Id.toString
109
156
  let firstEvent = events'->Array.getUnsafe(0)
110
157
  let meta = firstEvent.meta
111
158
  await runBeforePublishHook(idStr, eventsJson, meta)
112
159
  try {
113
- let _ = await Ops.eventTopic.publish(events')
160
+ let count = events'->Array.length->Int.toString
161
+ let _ = await events'
162
+ ->Array.mapWithIndex(async (event', i) => {
163
+ let json = publishedJsonOf(event'.id, event'.meta, sealed->Array.getUnsafe(i))
164
+ let _ = await Ops.eventTopic.publishJson(idStr, event'.meta, json)
165
+ EffectLogger.logInfo(
166
+ ~comp=`EventTopic(${Spec.name})`,
167
+ ~detail=json,
168
+ `published event ${(i + 1)->Int.toString}/${count}: ${LogFormat.eventDetail(json)}`,
169
+ )->Effect.runSync
170
+ })
171
+ ->Promise.all
114
172
  await runAfterPublishHook(idStr, eventsJson, meta)
115
173
  Ok()
116
174
  } catch {
@@ -126,43 +184,54 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
126
184
  // Transient storage failures are retried with exponential backoff (up to 5
127
185
  // times); a Conflict is returned immediately (retried a layer up), and other
128
186
  // failures propagate after retries are exhausted.
129
- let append = async (sequenceNr, id, events') => {
130
- let eventsJson = events'->encodeEvents'(id, sequenceNr)
131
- let idStr = id->Spec.Id.toString
132
- // A thrown storage error becomes a StorageFailure; a returned Error keeps its
133
- // typed variant. The Effect fails with the typed appendError so the schedule
134
- // can retry only transient failures, then we collapse the outcome back into a
135
- // result (no Cause stringification / substring matching).
136
- let storageEffect =
137
- Effect.tryPromise(
138
- ~catch=(err: unknown) => EventLog.StorageFailure(
139
- Util.Error.messageFromUnknown(err, "storage error"),
140
- ),
141
- () => Ops.storage.append(sequenceNr, idStr, eventsJson),
142
- )
143
- ->Effect.flatMap(result =>
144
- switch result {
145
- | Ok(_) => Effect.succeed()
146
- | Error(e) => Effect.fail(e)
147
- }
148
- )
149
- ->Effect.retry(storageRetrySchedule)
150
- ->Effect.map(_ => Ok())
151
- ->Effect.catchAll(e => Effect.succeed(Error(e)))
152
- switch await storageEffect->Effect.runPromise {
153
- | Ok() =>
154
- (
155
- await publishToEventTopic(id, events', eventsJson)
156
- )->Result.mapError(msg => EventLog.StorageFailure(msg))
157
- | Error(EventLog.Conflict) => Error(EventLog.Conflict)
158
- | Error(StorageFailure(msg)) =>
187
+ let append = async (sequenceNr, id, events') =>
188
+ switch await sealEvents(events') {
189
+ | Error(msg) =>
159
190
  Error(
160
191
  EventLog.StorageFailure(
161
- `EventLog: Error: Couldn't append for ${Spec.name}(${idStr}): ${msg}`,
192
+ `EventLog: Error: Couldn't append for ${Spec.name}(${id->Spec.Id.toString}): ${msg}`,
162
193
  ),
163
194
  )
195
+ | Ok(sealed) =>
196
+ let eventsJson =
197
+ sealed->Array.mapWithIndex((part, i) =>
198
+ storedEventOf(id, sequenceNr + i, (events'->Array.getUnsafe(i)).meta, part)
199
+ )
200
+ let idStr = id->Spec.Id.toString
201
+ // A thrown storage error becomes a StorageFailure; a returned Error keeps its
202
+ // typed variant. The Effect fails with the typed appendError so the schedule
203
+ // can retry only transient failures, then we collapse the outcome back into a
204
+ // result (no Cause stringification / substring matching).
205
+ let storageEffect =
206
+ Effect.tryPromise(
207
+ ~catch=(err: unknown) => EventLog.StorageFailure(
208
+ Util.Error.messageFromUnknown(err, "storage error"),
209
+ ),
210
+ () => Ops.storage.append(sequenceNr, idStr, eventsJson),
211
+ )
212
+ ->Effect.flatMap(result =>
213
+ switch result {
214
+ | Ok(_) => Effect.succeed()
215
+ | Error(e) => Effect.fail(e)
216
+ }
217
+ )
218
+ ->Effect.retry(storageRetrySchedule)
219
+ ->Effect.map(_ => Ok())
220
+ ->Effect.catchAll(e => Effect.succeed(Error(e)))
221
+ switch await storageEffect->Effect.runPromise {
222
+ | Ok() =>
223
+ (
224
+ await publishToEventTopic(id, events', sealed, eventsJson)
225
+ )->Result.mapError(msg => EventLog.StorageFailure(msg))
226
+ | Error(EventLog.Conflict) => Error(EventLog.Conflict)
227
+ | Error(StorageFailure(msg)) =>
228
+ Error(
229
+ EventLog.StorageFailure(
230
+ `EventLog: Error: Couldn't append for ${Spec.name}(${idStr}): ${msg}`,
231
+ ),
232
+ )
233
+ }
164
234
  }
165
- }
166
235
 
167
236
  let decodeEvent = (id, json) =>
168
237
  try {
@@ -188,16 +257,58 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
188
257
 
189
258
  let decodeEvents = (jsons, id) => jsons->Array.map(json => decodeEvent(id, json))
190
259
 
260
+ // The stored item with its sealed personal fields opened by the installed
261
+ // provider, so the decoder reads values and the markers of values that are gone.
262
+ // Most items hold nothing sealed and come back as they are, without a promise.
263
+ let sealedParts = json =>
264
+ switch json->JSON.Decode.object {
265
+ | Some(dict) =>
266
+ switch (dict->Dict.get("event"), dict->Dict.get("data")) {
267
+ | (Some(JSON.String(eventType)), Some(JSON.Object(data)))
268
+ if Reventless.Pii.needsOpen(Spec.eventSchema, ~eventType, data) =>
269
+ Some((dict, eventType, data))
270
+ | _ => None
271
+ }
272
+ | None => None
273
+ }
274
+
275
+ let openStored = async (id, json) =>
276
+ switch sealedParts(json) {
277
+ | None => json
278
+ | Some((dict, eventType, data)) =>
279
+ switch await Reventless.Pii.openData(Spec.eventSchema, ~eventType, data) {
280
+ | Ok(opened) =>
281
+ let out = dict->Dict.copy
282
+ out->Dict.set("data", JSON.Object(opened))
283
+ JSON.Object(out)
284
+ | Error(e) =>
285
+ JsError.throwWithMessage(
286
+ `EventLog.replay: Error: id:${id}: cannot open ${eventType}: ${e->Reventless.Pii.errorMessage}`,
287
+ )
288
+ }
289
+ }
290
+
291
+ let openAndDecode = (id, json) =>
292
+ switch sealedParts(json) {
293
+ | None => Effect.sync(() => decodeEvent(id, json))
294
+ | Some(_) => Effect.promise(() => openStored(id, json))->Effect.map(j => decodeEvent(id, j))
295
+ }
296
+
191
297
  let replay = async id => {
192
- let eventsJson = await Ops.storage.replay(id->Spec.Id.toString)
193
- eventsJson->decodeEvents(id->Spec.Id.toString)
298
+ let idStr = id->Spec.Id.toString
299
+ let eventsJson = await Ops.storage.replay(idStr)
300
+ let opened = []
301
+ for i in 0 to eventsJson->Array.length - 1 {
302
+ opened->Array.push(await openStored(idStr, eventsJson->Array.getUnsafe(i)))
303
+ }
304
+ opened->decodeEvents(idStr)
194
305
  }
195
306
 
196
- // Lazy streaming replay — wraps decodeEvent in Effect.sync so thrown exceptions
307
+ // Lazy streaming replay — decodes inside the effect so thrown exceptions
197
308
  // surface through the stream's error channel rather than as unhandled exceptions.
198
309
  let replayStream = (id, ~fromSeq=?) =>
199
310
  Ops.storage.replayStream(id->Spec.Id.toString, ~fromSeq?)->Stream.mapEffect(json =>
200
- Effect.sync(() => decodeEvent(id->Spec.Id.toString, json))
311
+ openAndDecode(id->Spec.Id.toString, json)
201
312
  )
202
313
 
203
314
  // The stored item is the flat form `storedEventToFlatJson` writes: meta.* at the
@@ -221,10 +332,10 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
221
332
  let replayStream' = (id, ~fromSeq=?) => {
222
333
  let idStr = id->Spec.Id.toString
223
334
  Ops.storage.replayStream(idStr, ~fromSeq?)->Stream.mapEffect(json =>
224
- Effect.sync(() => {
335
+ openAndDecode(idStr, json)->Effect.map(event => {
225
336
  Reventless.Message.id,
226
337
  meta: decodeMeta(idStr, json),
227
- event: decodeEvent(idStr, json),
338
+ event,
228
339
  })
229
340
  )
230
341
  }
@@ -244,12 +355,22 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
244
355
  Ops.storage.appendStream(
245
356
  startingSeqNr,
246
357
  id->Spec.Id.toString,
247
- stream->Stream.map(event => {
248
- let json = event->Message.encode(Spec.eventSchema)
249
- let (eventType, data) = json->Message.splitMessage
250
- [("event", JSON.String(eventType)), ("data", JSON.Object(data))]
251
- ->Dict.fromArray
252
- ->JSON.Encode.object
253
- }),
358
+ // Sealed as `append` seals, so a copied stream stores no personal value in
359
+ // the clear; a field that cannot be sealed fails the stream.
360
+ stream->Stream.mapEffect(event =>
361
+ Effect.promise(async () => {
362
+ let (eventType, data) = event->Message.encode(Spec.eventSchema)->Message.splitMessage
363
+ let sealed = switch await Reventless.Pii.sealData(Spec.eventSchema, ~eventType, data) {
364
+ | Ok(sealed) => sealed
365
+ | Error(e) =>
366
+ JsError.throwWithMessage(
367
+ `EventLog.appendStream: cannot seal ${eventType}: ${e->Reventless.Pii.errorMessage}`,
368
+ )
369
+ }
370
+ [("event", JSON.String(eventType)), ("data", JSON.Object(sealed))]
371
+ ->Dict.fromArray
372
+ ->JSON.Encode.object
373
+ })
374
+ ),
254
375
  )
255
376
  }