@smthrs/notifications 0.0.0-stage → 1.0.0-rc.13

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 (146) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/LICENSE +21 -0
  3. package/README.md +93 -2
  4. package/dist/cjs/AlertPolicy.d.ts +284 -0
  5. package/dist/cjs/AlertPolicy.d.ts.map +1 -0
  6. package/dist/cjs/AlertPolicy.js +124 -0
  7. package/dist/cjs/AlertPolicy.js.map +7 -0
  8. package/dist/cjs/AlertRuntime.d.ts +90 -0
  9. package/dist/cjs/AlertRuntime.d.ts.map +1 -0
  10. package/dist/cjs/AlertRuntime.js +166 -0
  11. package/dist/cjs/AlertRuntime.js.map +7 -0
  12. package/dist/cjs/AlertSink.d.ts +101 -0
  13. package/dist/cjs/AlertSink.d.ts.map +1 -0
  14. package/dist/cjs/AlertSink.js +44 -0
  15. package/dist/cjs/AlertSink.js.map +7 -0
  16. package/dist/cjs/Alerts.d.ts +57 -0
  17. package/dist/cjs/Alerts.d.ts.map +1 -0
  18. package/dist/cjs/Alerts.js +46 -0
  19. package/dist/cjs/Alerts.js.map +7 -0
  20. package/dist/cjs/Notification.d.ts +214 -0
  21. package/dist/cjs/Notification.d.ts.map +1 -0
  22. package/dist/cjs/Notification.js +62 -0
  23. package/dist/cjs/Notification.js.map +7 -0
  24. package/dist/cjs/NotificationEvent.d.ts +214 -0
  25. package/dist/cjs/NotificationEvent.d.ts.map +1 -0
  26. package/dist/cjs/NotificationEvent.js +84 -0
  27. package/dist/cjs/NotificationEvent.js.map +7 -0
  28. package/dist/cjs/NotificationQueue.d.ts +194 -0
  29. package/dist/cjs/NotificationQueue.d.ts.map +1 -0
  30. package/dist/cjs/NotificationQueue.js +477 -0
  31. package/dist/cjs/NotificationQueue.js.map +7 -0
  32. package/dist/cjs/NotificationState.d.ts +169 -0
  33. package/dist/cjs/NotificationState.d.ts.map +1 -0
  34. package/dist/cjs/NotificationState.js +110 -0
  35. package/dist/cjs/NotificationState.js.map +7 -0
  36. package/dist/cjs/Projection.d.ts +30 -0
  37. package/dist/cjs/Projection.d.ts.map +1 -0
  38. package/dist/cjs/Projection.js +53 -0
  39. package/dist/cjs/Projection.js.map +7 -0
  40. package/dist/cjs/SteerPayload.d.ts +177 -0
  41. package/dist/cjs/SteerPayload.d.ts.map +1 -0
  42. package/dist/cjs/SteerPayload.js +61 -0
  43. package/dist/cjs/SteerPayload.js.map +7 -0
  44. package/dist/cjs/index.d.ts +41 -0
  45. package/dist/cjs/index.d.ts.map +1 -0
  46. package/dist/cjs/index.js +47 -0
  47. package/dist/cjs/index.js.map +7 -0
  48. package/dist/cjs/internal/foldCache.d.ts +36 -0
  49. package/dist/cjs/internal/foldCache.d.ts.map +1 -0
  50. package/dist/cjs/internal/foldCache.js +54 -0
  51. package/dist/cjs/internal/foldCache.js.map +7 -0
  52. package/dist/cjs/internal/replay.d.ts +22 -0
  53. package/dist/cjs/internal/replay.d.ts.map +1 -0
  54. package/dist/cjs/internal/replay.js +46 -0
  55. package/dist/cjs/internal/replay.js.map +7 -0
  56. package/dist/cjs/layerWebhook.d.ts +60 -0
  57. package/dist/cjs/layerWebhook.d.ts.map +1 -0
  58. package/dist/cjs/layerWebhook.js +145 -0
  59. package/dist/cjs/layerWebhook.js.map +7 -0
  60. package/dist/cjs/package.json +1 -0
  61. package/dist/esm/AlertPolicy.d.ts +284 -0
  62. package/dist/esm/AlertPolicy.d.ts.map +1 -0
  63. package/dist/esm/AlertPolicy.js +284 -0
  64. package/dist/esm/AlertPolicy.js.map +1 -0
  65. package/dist/esm/AlertRuntime.d.ts +90 -0
  66. package/dist/esm/AlertRuntime.d.ts.map +1 -0
  67. package/dist/esm/AlertRuntime.js +191 -0
  68. package/dist/esm/AlertRuntime.js.map +1 -0
  69. package/dist/esm/AlertSink.d.ts +101 -0
  70. package/dist/esm/AlertSink.d.ts.map +1 -0
  71. package/dist/esm/AlertSink.js +63 -0
  72. package/dist/esm/AlertSink.js.map +1 -0
  73. package/dist/esm/Alerts.d.ts +57 -0
  74. package/dist/esm/Alerts.d.ts.map +1 -0
  75. package/dist/esm/Alerts.js +57 -0
  76. package/dist/esm/Alerts.js.map +1 -0
  77. package/dist/esm/Notification.d.ts +214 -0
  78. package/dist/esm/Notification.d.ts.map +1 -0
  79. package/dist/esm/Notification.js +121 -0
  80. package/dist/esm/Notification.js.map +1 -0
  81. package/dist/esm/NotificationEvent.d.ts +214 -0
  82. package/dist/esm/NotificationEvent.d.ts.map +1 -0
  83. package/dist/esm/NotificationEvent.js +158 -0
  84. package/dist/esm/NotificationEvent.js.map +1 -0
  85. package/dist/esm/NotificationQueue.d.ts +194 -0
  86. package/dist/esm/NotificationQueue.d.ts.map +1 -0
  87. package/dist/esm/NotificationQueue.js +625 -0
  88. package/dist/esm/NotificationQueue.js.map +1 -0
  89. package/dist/esm/NotificationState.d.ts +169 -0
  90. package/dist/esm/NotificationState.d.ts.map +1 -0
  91. package/dist/esm/NotificationState.js +191 -0
  92. package/dist/esm/NotificationState.js.map +1 -0
  93. package/dist/esm/Projection.d.ts +30 -0
  94. package/dist/esm/Projection.d.ts.map +1 -0
  95. package/dist/esm/Projection.js +47 -0
  96. package/dist/esm/Projection.js.map +1 -0
  97. package/dist/esm/SteerPayload.d.ts +177 -0
  98. package/dist/esm/SteerPayload.d.ts.map +1 -0
  99. package/dist/esm/SteerPayload.js +139 -0
  100. package/dist/esm/SteerPayload.js.map +1 -0
  101. package/dist/esm/index.d.ts +41 -0
  102. package/dist/esm/index.d.ts.map +1 -0
  103. package/dist/esm/index.js +41 -0
  104. package/dist/esm/index.js.map +1 -0
  105. package/dist/esm/internal/foldCache.d.ts +36 -0
  106. package/dist/esm/internal/foldCache.d.ts.map +1 -0
  107. package/dist/esm/internal/foldCache.js +58 -0
  108. package/dist/esm/internal/foldCache.js.map +1 -0
  109. package/dist/esm/internal/replay.d.ts +22 -0
  110. package/dist/esm/internal/replay.d.ts.map +1 -0
  111. package/dist/esm/internal/replay.js +31 -0
  112. package/dist/esm/internal/replay.js.map +1 -0
  113. package/dist/esm/layerWebhook.d.ts +60 -0
  114. package/dist/esm/layerWebhook.d.ts.map +1 -0
  115. package/dist/esm/layerWebhook.js +186 -0
  116. package/dist/esm/layerWebhook.js.map +1 -0
  117. package/docs/README.md +146 -0
  118. package/docs/api.md +443 -0
  119. package/docs/concepts/admission-and-promotion.md +144 -0
  120. package/docs/concepts/alerting.md +138 -0
  121. package/docs/concepts/journal-records.md +132 -0
  122. package/docs/guides/alert-on-a-stuck-run.md +140 -0
  123. package/docs/guides/drain-at-a-turn-boundary.md +95 -0
  124. package/docs/guides/handle-a-full-queue.md +102 -0
  125. package/docs/guides/report-pending-notifications.md +92 -0
  126. package/docs/guides/send-alerts-to-a-webhook.md +155 -0
  127. package/docs/guides/steer-a-run.md +127 -0
  128. package/docs/guides/testing.md +161 -0
  129. package/docs/installation.md +123 -0
  130. package/docs/quickstart.md +159 -0
  131. package/docs/troubleshooting.md +254 -0
  132. package/package.json +182 -3
  133. package/src/AlertPolicy.ts +377 -0
  134. package/src/AlertRuntime.ts +280 -0
  135. package/src/AlertSink.ts +105 -0
  136. package/src/Alerts.ts +71 -0
  137. package/src/Notification.ts +171 -0
  138. package/src/NotificationEvent.ts +213 -0
  139. package/src/NotificationQueue.ts +856 -0
  140. package/src/NotificationState.ts +295 -0
  141. package/src/Projection.ts +48 -0
  142. package/src/SteerPayload.ts +198 -0
  143. package/src/index.ts +47 -0
  144. package/src/internal/foldCache.ts +72 -0
  145. package/src/internal/replay.ts +39 -0
  146. package/src/layerWebhook.ts +239 -0
@@ -0,0 +1,625 @@
1
+ /**
2
+ * Journal-backed durable notification admission and turn-boundary drain.
3
+ *
4
+ * Governing contract: `packages/smithers/notifications/docs/api.md`, published as
5
+ * `/api/notifications`.
6
+ *
7
+ * @since 0.1.0
8
+ */
9
+ import { canonicalize } from "@smthrs/canonical";
10
+ import { digestSync } from "@smthrs/crypto/Sha256";
11
+ import { Journal, JournalEvent } from "@smthrs/journal";
12
+ import { Context, Effect, HashMap, Layer, Option, Schema, SchemaIssue, Semaphore } from "effect";
13
+ import * as FoldCache from "./internal/foldCache.js";
14
+ import * as Replay from "./internal/replay.js";
15
+ import * as NotificationModel from "./Notification.js";
16
+ import * as NotificationEvent from "./NotificationEvent.js";
17
+ import * as NotificationState from "./NotificationState.js";
18
+ /**
19
+ * The queue could not be reached, or it refused what a caller handed it.
20
+ *
21
+ * `code` is the stable half and the one to branch on. `notification_unavailable`
22
+ * is the seam reporting that it serves nothing; `notification_id_reused` says a
23
+ * stable id was admitted once already with different content, which is a
24
+ * producer bug rather than a storage failure; `notification_invalid` says the
25
+ * value is not a notification, and `path` names the field inside it that failed.
26
+ * `notification_closed` means the receiver has finished and the caller must
27
+ * start a new request; `notification_full` means nothing was retained and the
28
+ * same notification may be retried after pending work drains. Neither the
29
+ * message nor `path` ever carries the offending value.
30
+ * `notification_refused` rejects reserved outside-change payloads before admission.
31
+ *
32
+ * @category errors
33
+ * @since 0.1.0
34
+ */
35
+ export class NotificationError extends Schema.TaggedError()("/notifications/NotificationError", {
36
+ code: Schema.Literals([
37
+ "notification_unavailable",
38
+ "notification_closed",
39
+ "notification_full",
40
+ "notification_id_reused",
41
+ "notification_invalid",
42
+ "notification_refused"
43
+ ]).pipe(Schema.withConstructorDefault(Effect.succeed("notification_unavailable"))),
44
+ message: Schema.String,
45
+ /** The notification the failure is about, when one was readable. */
46
+ notificationId: Schema.optional(Schema.String),
47
+ /** Dotted path of the offending field, for `notification_invalid`. */
48
+ path: Schema.optional(Schema.String)
49
+ }) {
50
+ }
51
+ /**
52
+ * The {@link Service} tag.
53
+ *
54
+ * @category services
55
+ * @since 0.1.0
56
+ */
57
+ export class NotificationQueue extends Context.Service()("/notifications/NotificationQueue") {
58
+ }
59
+ /**
60
+ * Builds a {@link Service} from an implementation of its methods.
61
+ *
62
+ * @param implementation the methods to serve
63
+ * @category constructors
64
+ * @since 0.1.0
65
+ */
66
+ export const make = (implementation) => NotificationQueue.of(implementation);
67
+ const unavailable = (operation) => new NotificationError({ code: "notification_unavailable", message: `${operation} is unavailable` });
68
+ /**
69
+ * A {@link Service} that fails every method as unavailable. Overrides
70
+ * replace individual methods.
71
+ *
72
+ * @param overrides methods to serve instead of failing
73
+ * @category constructors
74
+ * @since 0.1.0
75
+ */
76
+ export const makeNoop = (overrides = {}) => make({
77
+ admit: Effect.fn("NotificationQueue.admit")(() => Effect.fail(unavailable("admit"))),
78
+ drain: Effect.fn("NotificationQueue.drain")(() => Effect.fail(unavailable("drain"))),
79
+ pending: Effect.fn("NotificationQueue.pending")(() => Effect.fail(unavailable("pending"))),
80
+ ...overrides
81
+ });
82
+ /**
83
+ * Provides {@link makeNoop}.
84
+ *
85
+ * @param overrides methods to serve instead of failing
86
+ * @category layers
87
+ * @since 0.1.0
88
+ */
89
+ export const layerNoop = (overrides = {}) => Layer.succeed(NotificationQueue)(makeNoop(overrides));
90
+ /**
91
+ * The deepest a notification's JSON may nest.
92
+ *
93
+ * The bound exists because the value is walked and serialized on the way to
94
+ * the journal, and an unbounded walk turns a hostile payload into an untyped
95
+ * `RangeError` instead of a refusal a caller can read.
96
+ */
97
+ const maximumPayloadDepth = 256;
98
+ /**
99
+ * The largest a notification may be, in UTF-16 code units: every string and
100
+ * object key counted at its length, and every other value as one.
101
+ *
102
+ * The bound exists because an admission copies, decodes, canonicalizes,
103
+ * fingerprints, and journals the whole value, so a producer admitting a huge
104
+ * array or string spends the notifying process's CPU and memory and bloats
105
+ * every replay of the run. It is checked before any of that work starts.
106
+ */
107
+ const maximumNotificationCodeUnits = 1_048_576;
108
+ /** Bounds an issue message so a refusal cannot journal or log an essay. */
109
+ const maximumIssueCodeUnits = 200;
110
+ /**
111
+ * The bound a value breaks, if any, walked iteratively so the walk itself
112
+ * cannot exhaust the stack. A cycle is caught by the depth bound: it has no
113
+ * finite depth, so it trips the limit rather than looping forever. The size
114
+ * count stops the walk as soon as it passes the bound, so an oversized value
115
+ * costs the walk no more than the bound does.
116
+ */
117
+ const exceededBound = (value) => {
118
+ const pending = [{ value, depth: 0 }];
119
+ let size = 0;
120
+ while (pending.length > 0) {
121
+ const next = pending.pop();
122
+ size += typeof next.value === "string" ? next.value.length : 1;
123
+ if (size > maximumNotificationCodeUnits)
124
+ return "size";
125
+ if (typeof next.value !== "object" || next.value === null)
126
+ continue;
127
+ if (next.depth >= maximumPayloadDepth)
128
+ return "depth";
129
+ if (Array.isArray(next.value)) {
130
+ for (const child of next.value)
131
+ pending.push({ value: child, depth: next.depth + 1 });
132
+ continue;
133
+ }
134
+ for (const [key, child] of Object.entries(next.value)) {
135
+ size += key.length;
136
+ pending.push({ value: child, depth: next.depth + 1 });
137
+ }
138
+ }
139
+ return undefined;
140
+ };
141
+ /**
142
+ * A structural copy of the caller's value, so nothing the queue journals or
143
+ * retains can be edited afterwards through a reference the caller kept. Values
144
+ * that are not arrays or objects pass through, which is what leaves a function
145
+ * or a symbol in place for the schema to reject.
146
+ */
147
+ const copied = (value) => {
148
+ if (Array.isArray(value))
149
+ return value.map(copied);
150
+ if (typeof value === "object" && value !== null) {
151
+ return Object.fromEntries(Object.entries(value).map(([key, nested]) => [key, copied(nested)]));
152
+ }
153
+ return value;
154
+ };
155
+ const formatIssue = SchemaIssue.makeFormatterStandardSchemaV1({ leafHook: (issue) => issue._tag });
156
+ const bounded = (text) => text.length <= maximumIssueCodeUnits ? text : `${text.slice(0, maximumIssueCodeUnits - 3)}...`;
157
+ const identifiedBy = (value) => {
158
+ if (typeof value !== "object" || value === null)
159
+ return undefined;
160
+ const id = value["id"];
161
+ return typeof id === "string" ? id : undefined;
162
+ };
163
+ const decodeNotification = Schema.decodeUnknownEffect(NotificationModel.Notification);
164
+ /**
165
+ * Decodes the caller's argument at the durability boundary and returns a value
166
+ * the caller cannot reach.
167
+ *
168
+ * Nothing downstream re-checks the shape: a structurally invalid notification
169
+ * that reached the journal would be acknowledged at a real sequence and then
170
+ * skipped by every replay, which is acknowledged data loss. The depth and size
171
+ * bounds run first so a hostile payload can neither exhaust the stack nor make
172
+ * the copy, the decode, and the fingerprint walk it in full.
173
+ */
174
+ const validated = (notification) => Effect.gen(function* () {
175
+ const notificationId = identifiedBy(notification);
176
+ const exceeded = exceededBound(notification);
177
+ if (exceeded !== undefined) {
178
+ return yield* new NotificationError({
179
+ code: "notification_invalid",
180
+ message: exceeded === "depth"
181
+ ? `A notification nests deeper than the ${maximumPayloadDepth} level bound`
182
+ : `A notification is larger than the ${maximumNotificationCodeUnits} code unit bound`,
183
+ ...(notificationId === undefined ? {} : { notificationId })
184
+ });
185
+ }
186
+ return yield* decodeNotification(copied(notification)).pipe(Effect.mapError((error) => {
187
+ // The formatter reports at least one issue for a failure and gives
188
+ // every issue a concrete path. Standard Schema's result type widens
189
+ // both, so a fallback for either would be code no input can reach.
190
+ const issue = formatIssue(error.issue).issues[0];
191
+ const path = issue.path.map(String).join(".");
192
+ return new NotificationError({
193
+ code: "notification_invalid",
194
+ message: bounded(issue.message),
195
+ ...(notificationId === undefined ? {} : { notificationId }),
196
+ ...(path === "" ? {} : { path })
197
+ });
198
+ }));
199
+ });
200
+ // Persisted notification JSON predates RFC 8785's rejection of lone surrogates.
201
+ const canonical = (value) => canonicalize(value, { loneSurrogates: "escape" });
202
+ /** Fingerprint the validated input before the journal redacts its payload. */
203
+ const fingerprint = (notification) => Effect.sync(() => digestSync(canonical(notification)));
204
+ /** Freeze the decoded JSON graph before any queue output can expose it. */
205
+ const freeze = (value) => {
206
+ if (typeof value !== "object" || value === null)
207
+ return;
208
+ for (const nested of Object.values(value))
209
+ freeze(nested);
210
+ Object.freeze(value);
211
+ };
212
+ /**
213
+ * How many runs one layer keeps folded at a time. Evicting is safe: the next
214
+ * call for an evicted run folds it again from the beginning, and the retention
215
+ * policy is scan-resistant, so a supervisor sweeping more runs than this reads
216
+ * journal tails for all but a fixed handful of them.
217
+ */
218
+ const maximumCachedRuns = 64;
219
+ /**
220
+ * Journal-backed production layer, with the pending capacity a composition
221
+ * chooses.
222
+ *
223
+ * Admission and drain evidence is written through `emitDurableUnfenced`, so
224
+ * both return only after the corresponding entry is durably committed and a
225
+ * dropped lifecycle event is unrepresentable. The channel is the unfenced one
226
+ * on purpose: the notifying process owns no run, so there is no ownership
227
+ * fence to hand over, and both records are first-writer-wins on their own
228
+ * identity instead.
229
+ *
230
+ * The capacity guard is a conditional write at the storage layer, not a check
231
+ * against a fold one process happened to load: the fold, the guard, and the
232
+ * admission record are decided and written inside ONE `journal.transact`
233
+ * write transaction, which the durable writer serializes against every other
234
+ * writer of the database and replays when a concurrent writer commits between
235
+ * the guard's reads and the insert. Two processes admitting to one full queue
236
+ * therefore produce one admission and one `rejected-full`, never two
237
+ * admissions.
238
+ *
239
+ * @param options the pending capacity, defaulting to `NotificationState.defaultCapacity`
240
+ * @category layers
241
+ * @since 1.0.0
242
+ */
243
+ export const layerWith = (options = {}) => Layer.effect(NotificationQueue, Effect.gen(function* () {
244
+ const journal = yield* Journal.Journal;
245
+ // Orders this layer's own fibers so one process never runs two of its
246
+ // admissions or drains against the fold cache at once. It is
247
+ // process-local by construction and is NOT what enforces capacity or
248
+ // drain identity across processes: those are the conditional writes
249
+ // inside `journal.transact`, decided and committed in one serialized
250
+ // write transaction. Always enter the journal transaction before taking
251
+ // this permit, matching callers already inside an enclosing transaction.
252
+ const operations = yield* Semaphore.make(1);
253
+ const capacity = options.capacity ?? NotificationState.defaultCapacity;
254
+ // Only committed folds may be shared. A transaction reads its own
255
+ // inserts, but its read-back is still provisional until the OUTERMOST
256
+ // commit; neither a retry nor a later caller may dedupe against it.
257
+ const folded = FoldCache.make(maximumCachedRuns);
258
+ /**
259
+ * One entry by the sequence it committed at.
260
+ *
261
+ * The fold keeps identities and sequences rather than payloads, so the
262
+ * paths that must report an admitted notification read it back here.
263
+ */
264
+ const entryAt = (runId, seq) => Effect.map(journal.entries({
265
+ runId,
266
+ ...(seq === 0 ? {} : { after: JournalEvent.Seq.make(seq - 1) }),
267
+ limit: 1
268
+ }), (page) => {
269
+ const found = page.entries[0];
270
+ return found !== undefined && found.seq === seq ? Option.some(found) : Option.none();
271
+ });
272
+ const admittedAt = (runId, seq) => Effect.map(entryAt(runId, seq), (entry) => {
273
+ if (Option.isNone(entry))
274
+ return Option.none();
275
+ const decoded = NotificationEvent.fromEntry(entry.value);
276
+ if (Option.isNone(decoded) || !NotificationEvent.isAdmitted(decoded.value))
277
+ return Option.none();
278
+ freeze(decoded.value.notification);
279
+ return Option.some(decoded.value);
280
+ });
281
+ const promotedAt = (runId, seq) => Effect.map(entryAt(runId, seq), (entry) => {
282
+ if (Option.isNone(entry))
283
+ return Option.none();
284
+ const decoded = NotificationEvent.fromEntry(entry.value);
285
+ if (Option.isNone(decoded) || !NotificationEvent.isPromoted(decoded.value))
286
+ return Option.none();
287
+ return Option.some(decoded.value);
288
+ });
289
+ const load = (runId) => Effect.gen(function* () {
290
+ const base = folded.get(runId) ?? {
291
+ state: NotificationState.empty(capacity),
292
+ admissions: HashMap.empty(),
293
+ promotions: HashMap.empty(),
294
+ cursor: undefined
295
+ };
296
+ const fresh = [];
297
+ let after = base.cursor === undefined ? undefined : JournalEvent.Seq.make(base.cursor);
298
+ while (true) {
299
+ // `after` is an exact-optional property upstream, so an explicit
300
+ // `undefined` is not the same as an absent key.
301
+ const page = yield* journal.entries({
302
+ runId,
303
+ eventTypes: [NotificationEvent.AdmittedEventType, NotificationEvent.PromotedEventType],
304
+ ...(after === undefined ? {} : { after }),
305
+ limit: 512
306
+ });
307
+ fresh.push(...page.entries);
308
+ if (!page.hasMore || page.entries.length === 0)
309
+ break;
310
+ after = page.entries.at(-1).seq;
311
+ }
312
+ if (fresh.length === 0)
313
+ return base;
314
+ let state = base.state;
315
+ let cursor = base.cursor;
316
+ // Persistent maps: one event adds a node and leaves the fold the
317
+ // previous load published untouched, where copying both maps would
318
+ // cost the run's whole history on every event and make a long run's
319
+ // folding quadratic in its own admissions.
320
+ let admissions = base.admissions;
321
+ let promotions = base.promotions;
322
+ for (const entry of fresh) {
323
+ cursor = entry.seq;
324
+ const decoded = NotificationEvent.fromEntry(entry);
325
+ if (Option.isNone(decoded))
326
+ continue;
327
+ const event = decoded.value;
328
+ if (NotificationEvent.isAdmitted(event)) {
329
+ freeze(event.notification);
330
+ admissions = HashMap.set(admissions, event.notification.id, {
331
+ version: event.version,
332
+ fingerprint: event.fingerprint,
333
+ decision: event.decision,
334
+ seq: entry.seq
335
+ });
336
+ state = Replay.applyAdmitted(state, event, entry.seq);
337
+ }
338
+ else {
339
+ promotions = HashMap.set(promotions, NotificationEvent.drainKey(event.targetLineageId, event.boundary), entry.seq);
340
+ state = NotificationState.applyPromoted(state, event.ids);
341
+ }
342
+ }
343
+ const loaded = { state, admissions, promotions, cursor };
344
+ yield* journal.whenCommitted(Effect.sync(() => folded.put(runId, loaded)));
345
+ return loaded;
346
+ });
347
+ /**
348
+ * One conditional admission: the duplicate check, the capacity guard,
349
+ * and the admission record, decided and written inside ONE journal
350
+ * write transaction.
351
+ *
352
+ * This is the queue's conditional insert, the same read-guarded write
353
+ * the journal itself uses for its dedupe and fence conditions: the
354
+ * guard's reads and the insert share one serialized write transaction,
355
+ * and the durable writer replays the whole body when a concurrent
356
+ * writer commits between them. A decision a stale fold produced can
357
+ * therefore never commit — the replay re-folds against the committed
358
+ * state and a queue that filled meanwhile refuses — however many
359
+ * processes share the database. The process-local `operations`
360
+ * semaphore orders this layer's own fibers only; it is not what
361
+ * enforces capacity across processes.
362
+ */
363
+ const admitInTransaction = (runId, admitted, admittedFingerprint, version) => Effect.gen(function* () {
364
+ const loaded = yield* load(runId);
365
+ const prior = Option.getOrUndefined(HashMap.get(loaded.admissions, admitted.id));
366
+ const replacing = prior !== undefined && version !== undefined && version > (prior.version ?? 0);
367
+ if (replacing) {
368
+ const original = yield* admittedAt(runId, prior.seq);
369
+ if (Option.isNone(original)) {
370
+ return yield* new NotificationError({
371
+ code: "notification_unavailable",
372
+ notificationId: admitted.id,
373
+ message: "Original steering input is unreadable"
374
+ });
375
+ }
376
+ const identity = (input) => ({ ...input, payload: null });
377
+ if (canonical(identity(original.value.notification)) !== canonical(identity(admitted))) {
378
+ return yield* new NotificationError({
379
+ code: "notification_id_reused",
380
+ notificationId: admitted.id,
381
+ message: "An edit cannot change the notification owner or delivery binding"
382
+ });
383
+ }
384
+ }
385
+ if (replacing && !loaded.state.items.some((item) => item.notification.id === admitted.id)) {
386
+ const original = yield* admittedAt(runId, prior.seq);
387
+ if (Option.isNone(original)) {
388
+ return yield* new NotificationError({
389
+ code: "notification_unavailable",
390
+ message: "Consumed input is unreadable"
391
+ });
392
+ }
393
+ return {
394
+ notificationId: admitted.id,
395
+ decision: prior.decision,
396
+ seq: prior.seq,
397
+ duplicate: true,
398
+ consumed: true,
399
+ consumedNotification: original.value.notification
400
+ };
401
+ }
402
+ if (prior !== undefined && version !== undefined && version < (prior.version ?? 0)) {
403
+ return { notificationId: admitted.id, decision: prior.decision, seq: prior.seq, duplicate: true };
404
+ }
405
+ if (prior !== undefined && !replacing) {
406
+ // Legacy rows have no fingerprint; only their persisted content is
407
+ // available for comparison, and the fold keeps identities rather
408
+ // than payloads, so the original is read back at its sequence.
409
+ // New rows settle on the fingerprint alone and read nothing.
410
+ let same = prior.fingerprint === admittedFingerprint;
411
+ if (prior.fingerprint === undefined) {
412
+ const original = yield* admittedAt(runId, prior.seq);
413
+ if (Option.isNone(original)) {
414
+ return yield* new NotificationError({
415
+ code: "notification_unavailable",
416
+ notificationId: admitted.id,
417
+ message: `The admission for notification ${admitted.id} is no longer readable`
418
+ });
419
+ }
420
+ same = canonical(original.value.notification) === canonical(admitted);
421
+ }
422
+ if (!same) {
423
+ return yield* new NotificationError({
424
+ code: "notification_id_reused",
425
+ notificationId: admitted.id,
426
+ message: `Notification id ${admitted.id} was already admitted with different content`
427
+ });
428
+ }
429
+ return {
430
+ notificationId: admitted.id,
431
+ decision: prior.decision,
432
+ seq: prior.seq,
433
+ duplicate: true
434
+ };
435
+ }
436
+ const admission = replacing ? { decision: "coalesced" } : NotificationState.admit(loaded.state, admitted, loaded.cursor === undefined ? 0 : loaded.cursor + 1);
437
+ if (admission.decision === "rejected-full") {
438
+ // Nothing is journaled. A rejection recorded as an admission
439
+ // would match on every later attempt and burn the id forever,
440
+ // so the queue refuses in the receipt alone and the caller may
441
+ // admit again once a boundary has drained.
442
+ return {
443
+ notificationId: admitted.id,
444
+ decision: admission.decision,
445
+ seq: undefined,
446
+ duplicate: false
447
+ };
448
+ }
449
+ const receipt = yield* journal.emitDurableUnfenced(new JournalEvent.Input({
450
+ runId,
451
+ sourceId: NotificationEvent.admissionSourceId(admitted.id),
452
+ sourceSeq: JournalEvent.SourceSeq.make(version ?? 0),
453
+ // The sequence is derived from the notification's own id, so
454
+ // a collision IS this admission observed twice. Comparing
455
+ // bytes instead would fail the second writer over `decision`,
456
+ // a field neither caller supplied and each derives from the
457
+ // fill level it happened to load at.
458
+ dedupe: "identity",
459
+ eventType: NotificationEvent.AdmittedEventType,
460
+ payload: {
461
+ notification: admitted,
462
+ decision: admission.decision,
463
+ fingerprint: admittedFingerprint,
464
+ ...(version === undefined ? {} : { version })
465
+ }
466
+ }));
467
+ const committed = Option.getOrUndefined(HashMap.get((yield* load(runId)).admissions, admitted.id));
468
+ if (committed === undefined) {
469
+ // Only reachable when the emit deduped against an entry this
470
+ // queue did not write, so nothing was committed and there is
471
+ // no admission to report.
472
+ return yield* new NotificationError({
473
+ code: "notification_id_reused",
474
+ notificationId: admitted.id,
475
+ message: `The journal identity for notification ${admitted.id} holds an event this queue did not write`
476
+ });
477
+ }
478
+ return {
479
+ notificationId: admitted.id,
480
+ decision: committed.decision,
481
+ seq: committed.seq,
482
+ duplicate: receipt._tag === "Duplicate"
483
+ };
484
+ });
485
+ return make({
486
+ admit: Effect.fn("NotificationQueue.admit")((rawRunId, notification, version) => Effect.gen(function* () {
487
+ if (version !== undefined && (!Number.isSafeInteger(version) || version < 1)) {
488
+ return yield* new NotificationError({
489
+ code: "notification_invalid",
490
+ message: "version must be a positive safe integer"
491
+ });
492
+ }
493
+ const admitted = yield* validated(notification);
494
+ // T-COL-12 stays dark: nothing yet authenticates a watcher burst, pins
495
+ // its coding run or enforces fresh machine reads. Refusing here, before
496
+ // the journal, keeps co-admitted steers deliverable on every replay.
497
+ const payload = admitted.payload;
498
+ if (typeof payload === "object" && payload !== null && !Array.isArray(payload) &&
499
+ payload["kind"] === "outside_change") {
500
+ return yield* new NotificationError({
501
+ code: "notification_refused",
502
+ notificationId: admitted.id,
503
+ message: "Outside-change delivery requires authenticated watcher facts and stale-write enforcement"
504
+ });
505
+ }
506
+ const admittedFingerprint = yield* fingerprint(admitted);
507
+ return yield* journal.transact(operations.withPermits(1)(admitInTransaction(JournalEvent.RunId.make(rawRunId), admitted, admittedFingerprint, version)));
508
+ })),
509
+ drain: Effect.fn("NotificationQueue.drain")((input) => journal.transact(operations.withPermits(1)(Effect.gen(function* () {
510
+ // A cutoff no sequence can satisfy would commit this boundary as
511
+ // delivering nothing, and every retry would replay that record.
512
+ if (input.cutoffSeq !== undefined && !(Number.isSafeInteger(input.cutoffSeq) && input.cutoffSeq >= 0)) {
513
+ return yield* new NotificationError({
514
+ code: "notification_invalid",
515
+ message: "A drain cutoff must be a non-negative integer journal sequence",
516
+ path: "cutoffSeq"
517
+ });
518
+ }
519
+ const runId = JournalEvent.RunId.make(input.runId);
520
+ const key = NotificationEvent.drainKey(input.targetLineageId, input.boundary);
521
+ const loaded = yield* load(runId);
522
+ // A replay reports what the committed record delivered, read
523
+ // back from the journal: the fold holds the identities, not the
524
+ // notifications, so a run's drained payloads are not retained.
525
+ const delivered = (record, admissions) => Effect.forEach(record.ids, (id) => Effect.gen(function* () {
526
+ const committed = HashMap.get(admissions, id);
527
+ const event = Option.isNone(committed)
528
+ ? Option.none()
529
+ : yield* admittedAt(runId, committed.value.seq);
530
+ // A committed drain promises this exact delivery. Missing or
531
+ // corrupt payloads cannot become a successful partial replay.
532
+ if (Option.isNone(event)) {
533
+ return yield* new NotificationError({
534
+ code: "notification_unavailable",
535
+ notificationId: id,
536
+ message: `The delivered notification ${id} is no longer readable`
537
+ });
538
+ }
539
+ return event.value.notification;
540
+ }));
541
+ const replayed = (seq, admissions) => Effect.gen(function* () {
542
+ const record = yield* promotedAt(runId, seq);
543
+ if (Option.isNone(record)) {
544
+ return yield* new NotificationError({
545
+ code: "notification_unavailable",
546
+ message: `The drain record for boundary ${input.boundary} is no longer readable`
547
+ });
548
+ }
549
+ return {
550
+ notifications: yield* delivered(record.value, admissions),
551
+ boundary: input.boundary,
552
+ duplicate: true
553
+ };
554
+ });
555
+ const prior = HashMap.get(loaded.promotions, key);
556
+ if (Option.isSome(prior))
557
+ return yield* replayed(prior.value, loaded.admissions);
558
+ const cutoff = input.cutoffSeq ?? loaded.cursor ?? 0;
559
+ // The existing coding park ladder waits for a person's answer.
560
+ // System facts must remain pending there: promoting them would
561
+ // make the harness treat an outside edit as an answered question.
562
+ // Filtering this projection changes only promoted IDs; the journal
563
+ // fold retains every held admission and its original sequence.
564
+ const promotionState = /^\d+:[a-f0-9]{64}:park:\d+$/.test(input.boundary)
565
+ ? {
566
+ ...loaded.state,
567
+ items: loaded.state.items.filter((item) => {
568
+ const payload = item.notification.payload;
569
+ return !(typeof payload === "object" && payload !== null && !Array.isArray(payload) &&
570
+ payload.kind === "outside_change");
571
+ })
572
+ }
573
+ : loaded.state;
574
+ const steers = NotificationState.promoteSteers(promotionState, cutoff, input.targetLineageId);
575
+ const queued = input.wouldIdle && steers.promoted.length === 0
576
+ ? NotificationState.promoteQueued(steers.state, input.targetLineageId)
577
+ : { state: steers.state, promoted: [] };
578
+ const promoted = [...steers.promoted, ...queued.promoted].map((item) => item.notification);
579
+ const receipt = yield* journal.emitDurableUnfenced(new JournalEvent.Input({
580
+ runId,
581
+ sourceId: NotificationEvent.drainSourceId(input.targetLineageId, input.boundary),
582
+ sourceSeq: JournalEvent.SourceSeq.make(0),
583
+ // The identity is the drain: this lineage closing this
584
+ // boundary. Two processes that both reach it have observed one
585
+ // event, and the first record committed is the delivery.
586
+ dedupe: "identity",
587
+ eventType: NotificationEvent.PromotedEventType,
588
+ payload: {
589
+ boundary: input.boundary,
590
+ targetLineageId: input.targetLineageId,
591
+ ids: promoted.map((notification) => notification.id)
592
+ }
593
+ }));
594
+ const settled = yield* load(runId);
595
+ const committed = HashMap.get(settled.promotions, key);
596
+ if (Option.isNone(committed)) {
597
+ return yield* new NotificationError({
598
+ code: "notification_unavailable",
599
+ message: `The journal identity for boundary ${input.boundary} holds an event this queue did not write`
600
+ });
601
+ }
602
+ // An accepted emit wrote THIS drain, so the promoted set is the
603
+ // one already in hand and no read-back is owed.
604
+ if (receipt._tag === "Accepted") {
605
+ return { notifications: promoted, boundary: input.boundary, duplicate: false };
606
+ }
607
+ return yield* replayed(committed.value, settled.admissions);
608
+ })))),
609
+ pending: Effect.fn("NotificationQueue.pending")((rawRunId) => Effect.map(load(JournalEvent.RunId.make(rawRunId)),
610
+ // The fold `load` already performs: admissions add, promotions
611
+ // remove. Read outside the operations semaphore, because a count
612
+ // taken while a boundary drains is a count taken at some instant
613
+ // either side of it, and blocking a supervisor behind a turn
614
+ // boundary would be the worse answer.
615
+ (loaded) => loaded.state.items.map((item) => item.notification)))
616
+ });
617
+ }));
618
+ /**
619
+ * Journal-backed production layer at the default capacity.
620
+ *
621
+ * @category layers
622
+ * @since 0.1.0
623
+ */
624
+ export const layer = layerWith();
625
+ //# sourceMappingURL=NotificationQueue.js.map