@substrat-run/kernel 0.216.0 → 0.217.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/attachment-gate.d.ts +42 -0
  2. package/dist/attachment-gate.d.ts.map +1 -0
  3. package/dist/attachment-gate.js +36 -0
  4. package/dist/attachment-gate.js.map +1 -0
  5. package/dist/broker-erasure.d.ts +51 -0
  6. package/dist/broker-erasure.d.ts.map +1 -0
  7. package/dist/broker-erasure.js +161 -0
  8. package/dist/broker-erasure.js.map +1 -0
  9. package/dist/broker-relay-erasure.d.ts +27 -0
  10. package/dist/broker-relay-erasure.d.ts.map +1 -0
  11. package/dist/broker-relay-erasure.js +115 -0
  12. package/dist/broker-relay-erasure.js.map +1 -0
  13. package/dist/broker-relay.d.ts +292 -0
  14. package/dist/broker-relay.d.ts.map +1 -0
  15. package/dist/broker-relay.js +494 -0
  16. package/dist/broker-relay.js.map +1 -0
  17. package/dist/broker-schema.d.ts +25 -0
  18. package/dist/broker-schema.d.ts.map +1 -0
  19. package/dist/broker-schema.js +198 -0
  20. package/dist/broker-schema.js.map +1 -0
  21. package/dist/broker-telemetry.d.ts +288 -0
  22. package/dist/broker-telemetry.d.ts.map +1 -0
  23. package/dist/broker-telemetry.js +334 -0
  24. package/dist/broker-telemetry.js.map +1 -0
  25. package/dist/cost-rollup.d.ts +6 -0
  26. package/dist/cost-rollup.d.ts.map +1 -1
  27. package/dist/cost-rollup.js +22 -1
  28. package/dist/cost-rollup.js.map +1 -1
  29. package/dist/delivery-broker.d.ts +278 -0
  30. package/dist/delivery-broker.d.ts.map +1 -0
  31. package/dist/delivery-broker.js +516 -0
  32. package/dist/delivery-broker.js.map +1 -0
  33. package/dist/delivery-policy.d.ts +105 -0
  34. package/dist/delivery-policy.d.ts.map +1 -0
  35. package/dist/delivery-policy.js +105 -0
  36. package/dist/delivery-policy.js.map +1 -0
  37. package/dist/index.d.ts +12 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +6 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/job-run.d.ts +21 -1
  42. package/dist/job-run.d.ts.map +1 -1
  43. package/dist/job-run.js +6 -0
  44. package/dist/job-run.js.map +1 -1
  45. package/dist/outbox-prune.d.ts +8 -1
  46. package/dist/outbox-prune.d.ts.map +1 -1
  47. package/dist/outbox-prune.js +6 -2
  48. package/dist/outbox-prune.js.map +1 -1
  49. package/dist/platform-sweep.d.ts +10 -1
  50. package/dist/platform-sweep.d.ts.map +1 -1
  51. package/dist/platform-sweep.js +37 -1
  52. package/dist/platform-sweep.js.map +1 -1
  53. package/dist/scope-host.d.ts +33 -8
  54. package/dist/scope-host.d.ts.map +1 -1
  55. package/dist/scope-host.js.map +1 -1
  56. package/dist/store-driver-releases.d.ts +9 -2
  57. package/dist/store-driver-releases.d.ts.map +1 -1
  58. package/dist/store-driver-releases.js +12 -7
  59. package/dist/store-driver-releases.js.map +1 -1
  60. package/dist/subject-keys.d.ts +7 -0
  61. package/dist/subject-keys.d.ts.map +1 -1
  62. package/dist/subject-keys.js +29 -5
  63. package/dist/subject-keys.js.map +1 -1
  64. package/dist/subject-redaction.d.ts +7 -0
  65. package/dist/subject-redaction.d.ts.map +1 -1
  66. package/dist/subject-redaction.js +34 -27
  67. package/dist/subject-redaction.js.map +1 -1
  68. package/package.json +2 -2
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The permission gate an attachment target is held to (#473, #3434), in one place so both
3
+ * adapters judge a declaration, and a remove, the same way.
4
+ *
5
+ * - `read` decides `list`, `open`, `openStream` and `search`.
6
+ * - `write` decides `upload` and `remove`. Absent from the declaration, it is `read`.
7
+ * - `remove`, when declared, admits a REMOVE beside `write` and nothing else (#3434). It is the
8
+ * key a retention schedule holds to delete a file it never uploaded, without holding the
9
+ * uploader's key (and so without being able to upload). Declared `holder: 'system'`, it is a
10
+ * key no person can hold.
11
+ */
12
+ import type { Decision, EntityRef, PermissionKey } from '@substrat-run/contracts';
13
+ /** What a manifest's `attachmentTargets` entry declares about its keys. */
14
+ export interface AttachmentTargetKeys {
15
+ readonly readPermission: PermissionKey;
16
+ readonly writePermission?: PermissionKey;
17
+ readonly removePermission?: PermissionKey;
18
+ }
19
+ /** A target's keys, with the defaults applied. */
20
+ export interface AttachmentGate {
21
+ readonly read: PermissionKey;
22
+ readonly write: PermissionKey;
23
+ readonly remove?: PermissionKey;
24
+ }
25
+ /** The gate a declaration means. */
26
+ export declare function attachmentGateOf(target: AttachmentTargetKeys): AttachmentGate;
27
+ /**
28
+ * Refuse a second declaration of one entity type whose gate differs from the first: who may
29
+ * read or remove an entity's files must not depend on which module registered first. The same
30
+ * gate declared twice is idempotent.
31
+ */
32
+ export declare function assertSameAttachmentGate(entityType: string, existing: AttachmentGate | undefined, gate: AttachmentGate): void;
33
+ /**
34
+ * Decide a remove: the write key, or else the declared remove key, on the owning entity, as
35
+ * whoever `check` checks as. A refusal names the remove key when one is declared, since that is
36
+ * the narrowest key that would have admitted it, and the write key otherwise.
37
+ *
38
+ * The write key is asked first so a person's remove decides exactly as it did before #3434
39
+ * whenever they hold it.
40
+ */
41
+ export declare function decideAttachmentRemove(check: (permission: PermissionKey, entity: EntityRef) => Promise<Decision>, gate: AttachmentGate, entity: EntityRef): Promise<Decision>;
42
+ //# sourceMappingURL=attachment-gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-gate.d.ts","sourceRoot":"","sources":["../src/attachment-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAElF,2EAA2E;AAC3E,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,cAAc,EAAE,aAAa,CAAC;IACvC,QAAQ,CAAC,eAAe,CAAC,EAAE,aAAa,CAAC;IACzC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,aAAa,CAAC;CAC3C;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC;CACjC;AAED,oCAAoC;AACpC,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc,CAM7E;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,cAAc,GAAG,SAAS,EACpC,IAAI,EAAE,cAAc,GACnB,IAAI,CAKN;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,CAAC,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC,QAAQ,CAAC,EAC1E,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,SAAS,GAChB,OAAO,CAAC,QAAQ,CAAC,CAInB"}
@@ -0,0 +1,36 @@
1
+ /** The gate a declaration means. */
2
+ export function attachmentGateOf(target) {
3
+ return {
4
+ read: target.readPermission,
5
+ write: target.writePermission ?? target.readPermission,
6
+ ...(target.removePermission !== undefined ? { remove: target.removePermission } : {}),
7
+ };
8
+ }
9
+ /**
10
+ * Refuse a second declaration of one entity type whose gate differs from the first: who may
11
+ * read or remove an entity's files must not depend on which module registered first. The same
12
+ * gate declared twice is idempotent.
13
+ */
14
+ export function assertSameAttachmentGate(entityType, existing, gate) {
15
+ if (!existing)
16
+ return;
17
+ if (existing.read === gate.read && existing.write === gate.write && existing.remove === gate.remove)
18
+ return;
19
+ const show = (g) => `${g.read}/${g.write}${g.remove !== undefined ? `/${g.remove}` : ''}`;
20
+ throw new Error(`conflicting attachmentTargets for '${entityType}': (${show(existing)}) vs (${show(gate)})`);
21
+ }
22
+ /**
23
+ * Decide a remove: the write key, or else the declared remove key, on the owning entity, as
24
+ * whoever `check` checks as. A refusal names the remove key when one is declared, since that is
25
+ * the narrowest key that would have admitted it, and the write key otherwise.
26
+ *
27
+ * The write key is asked first so a person's remove decides exactly as it did before #3434
28
+ * whenever they hold it.
29
+ */
30
+ export async function decideAttachmentRemove(check, gate, entity) {
31
+ const byWrite = await check(gate.write, entity);
32
+ if (byWrite.allowed || gate.remove === undefined || gate.remove === gate.write)
33
+ return byWrite;
34
+ return check(gate.remove, entity);
35
+ }
36
+ //# sourceMappingURL=attachment-gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-gate.js","sourceRoot":"","sources":["../src/attachment-gate.ts"],"names":[],"mappings":"AA2BA,oCAAoC;AACpC,MAAM,UAAU,gBAAgB,CAAC,MAA4B;IAC3D,OAAO;QACL,IAAI,EAAE,MAAM,CAAC,cAAc;QAC3B,KAAK,EAAE,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC,cAAc;QACtD,GAAG,CAAC,MAAM,CAAC,gBAAgB,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACtF,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACtC,UAAkB,EAClB,QAAoC,EACpC,IAAoB;IAEpB,IAAI,CAAC,QAAQ;QAAE,OAAO;IACtB,IAAI,QAAQ,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,IAAI,QAAQ,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,IAAI,QAAQ,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;QAAE,OAAO;IAC5G,MAAM,IAAI,GAAG,CAAC,CAAiB,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1G,MAAM,IAAI,KAAK,CAAC,sCAAsC,UAAU,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/G,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,KAA0E,EAC1E,IAAoB,EACpB,MAAiB;IAEjB,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,IAAI,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,KAAK;QAAE,OAAO,OAAO,CAAC;IAC/F,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,51 @@
1
+ import type { BrokerMessage, BrokerMessageKind, BrokerSql } from './delivery-broker.js';
2
+ /**
3
+ * Every broker column that can hold a message's content or text about it, which the erasure
4
+ * reaches: the body (`broker_messages`, which is also every dead letter, a `dead` delivery of a row
5
+ * there) and each receiver's error text about delivering it (`broker_deliveries.last_error`). A new
6
+ * column holding either is added here and to `eraseSubjectFromBroker`; the kernel's
7
+ * `broker-erasure.test.ts` refuses a broker column named for a body, payload or error that this list
8
+ * does not name.
9
+ */
10
+ export declare const BROKER_CONTENT_COLUMNS: readonly {
11
+ table: string;
12
+ column: string;
13
+ }[];
14
+ /** Which subjects' erasure reached one broker, and how many of its messages it rewrote. */
15
+ export interface BrokerErasure {
16
+ messages: number;
17
+ }
18
+ /** One subject's erasure, as a scope asks its broker for it. */
19
+ export interface BrokerErasureTarget {
20
+ tenantId: string;
21
+ scopeId: string;
22
+ subjectId: string;
23
+ /** The erasure's instant, ISO 8601: the one the spine's tombstones carry. */
24
+ at: string;
25
+ }
26
+ /**
27
+ * One message's body without the subject's content, or null when it carries none of it (or was
28
+ * already redacted). Pure: the erasure and the screen at acceptance both decide with it.
29
+ */
30
+ export declare function brokerBodyWithoutSubject(kind: BrokerMessageKind, body: string, subjectId: string, at: string): string | null;
31
+ /**
32
+ * Erase one subject from one broker: tombstone them for the scope, then rewrite every message of that
33
+ * scope whose body carries them, whatever the state of its deliveries (held, pending, delivered,
34
+ * dead), and put a note in place of each receiver's error text about them. The columns it reaches are
35
+ * `BROKER_CONTENT_COLUMNS`. Synchronous; `BrokerStore.eraseSubject` runs it in one transaction.
36
+ * Idempotent: a re-run finds the bodies rewritten and changes nothing.
37
+ *
38
+ * The read narrows by substring (`instr`), as the spine's intent redaction does, and the predicate
39
+ * decides. A subject id is a ULID, so the JSON text of a body holds it unescaped at any depth.
40
+ * Unindexed, and affordable: it runs once per staff-triggered erasure over what one broker holds, a
41
+ * bounded window (K-70).
42
+ */
43
+ export declare function eraseSubjectFromBroker(sql: BrokerSql, target: BrokerErasureTarget): BrokerErasure;
44
+ /**
45
+ * The messages of one handoff with every tombstoned subject's content taken out, before the store
46
+ * keeps them: a handoff the scope read before an erasure, arriving after it, stores nothing in the
47
+ * clear. Messages of a scope with no tombstone pass as they are. A tombstone read that fails throws,
48
+ * so the handoff is not acknowledged and the scope retries it: fail closed.
49
+ */
50
+ export declare function screenErasedSubjects(sql: BrokerSql, messages: readonly BrokerMessage[]): BrokerMessage[];
51
+ //# sourceMappingURL=broker-erasure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-erasure.d.ts","sourceRoot":"","sources":["../src/broker-erasure.ts"],"names":[],"mappings":"AAiDA,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAIxF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,EAAE,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAK9E,CAAC;AAEF,2FAA2F;AAC3F,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,gEAAgE;AAChE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,EAAE,EAAE,MAAM,CAAC;CACZ;AAUD;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAwB5H;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,mBAAmB,GAAG,aAAa,CAuCjG;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,aAAa,EAAE,GAAG,aAAa,EAAE,CAuBxG"}
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Subject erasure in the delivery broker's storage (#3484, K-37, K-74).
3
+ *
4
+ * A broker keeps each message a scope hands it as the row's JSON (`BrokerMessage.body`), so an event
5
+ * carrying personal data has a copy there: held, due, retrying, delivered (kept for a duplicate
6
+ * handoff to find) or dead-lettered (#3470: a dead letter is the same row, its delivery `dead`, and a
7
+ * resend delivers the body as stored). This module is how a subject's erasure
8
+ * reaches every one of them, on both adapters, through the `BrokerStore` they share.
9
+ *
10
+ * **Redacted at erasure, not sealed at handoff.** K-37 splits erasure by store: a mutable store is
11
+ * redacted, an immutable copy is sealed under the subject's key. The broker's store is mutable, with
12
+ * bounded retention (K-70), so it is redacted. Sealing at handoff would also put the directory and
13
+ * the platform's `SecretBox` on every message's path: the broker sits in the app's own script, where
14
+ * neither may be reachable (module code in that script reaches every binding it declares), so each
15
+ * handoff would seal and each delivery open through the platform, the per-message hop K-69 and K-74
16
+ * take away. Every receiver but the lake needs the plaintext anyway, and the lake, once it is a
17
+ * receiver, seals what it ships with #3464's mechanism at its own egress.
18
+ *
19
+ * **What a redaction writes is what the spine's writes.** An event's copy loses its `payload` exactly
20
+ * when the scope's outbox row does (`subject_id` matches and `pii_class` is not `none`); an intent's
21
+ * copy is rewritten exactly when the scope's intent journal row is (`intentPayloadCarriesSubject`,
22
+ * and a `sweep-runs` entry's error naming the subject). A copy judged more harshly than its original
23
+ * is an incoherent rule, so the predicates are the spine's own, imported. A body that is not a row the
24
+ * broker can read, and names the subject, is replaced whole: fail closed, since nothing can say what
25
+ * in it is theirs. A message's `last_error`, a receiver's text about delivering it, goes with it.
26
+ *
27
+ * **The tombstone is what closes the race.** A handoff the scope read before its spine was redacted
28
+ * can reach the broker after the broker's own redaction ran (on the Durable Object the scope's handoff
29
+ * awaits an RPC, and the erasure interleaves). So the erasure leaves a tombstone per (tenant, scope,
30
+ * subject), and `BrokerStore.accept` screens every message against the scope's tombstones before it
31
+ * stores it: a tombstoned subject's content is refused, and the message is kept redacted. It is still
32
+ * acknowledged, or the scope's marks would stop at it for good. Tombstones are kept for good, as the
33
+ * directory's key tombstones are: one row per erased subject, and a scope restored from a dump taken
34
+ * before the erasure hands its rows off again.
35
+ *
36
+ * **What stays out of reach** is a delivery already started when the erasure lands: the receiver
37
+ * holds what it was handed, as a consumer mid-dispatch holds the payload (K-37's copies already
38
+ * handed out). A delivery pass re-reads each message just before it goes (`BrokerStore.current`), so
39
+ * the rest of a batch read before the erasure goes redacted.
40
+ */
41
+ import { SWEEP_RUNS_KIND } from '@substrat-run/contracts';
42
+ import { REDACTED_FAILURE_NOTE, REDACTED_INTENT_MARKER, intentPayloadCarriesSubject, redactedIntentPayload, sweepRunPayloadWithoutSubject, } from './subject-redaction.js';
43
+ import { eraseSubjectFromRelay, RELAY_CONTENT_COLUMNS } from './broker-relay-erasure.js';
44
+ // The tombstones live in `broker_erased_subjects`, step 3 of `BROKER_SCHEMA` (`broker-schema.ts`).
45
+ /**
46
+ * Every broker column that can hold a message's content or text about it, which the erasure
47
+ * reaches: the body (`broker_messages`, which is also every dead letter, a `dead` delivery of a row
48
+ * there) and each receiver's error text about delivering it (`broker_deliveries.last_error`). A new
49
+ * column holding either is added here and to `eraseSubjectFromBroker`; the kernel's
50
+ * `broker-erasure.test.ts` refuses a broker column named for a body, payload or error that this list
51
+ * does not name.
52
+ */
53
+ export const BROKER_CONTENT_COLUMNS = [
54
+ { table: 'broker_messages', column: 'body' },
55
+ { table: 'broker_deliveries', column: 'last_error' },
56
+ // #3469: the relay's requests, replies and failure texts, reached by `eraseSubjectFromRelay`.
57
+ ...RELAY_CONTENT_COLUMNS,
58
+ ];
59
+ const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
60
+ /** A body this module wrote in place of one it could not read: nothing but the marker. */
61
+ const isRedactedWhole = (v) => {
62
+ const keys = Object.keys(v);
63
+ return keys.length === 1 && keys[0] === REDACTED_INTENT_MARKER;
64
+ };
65
+ /**
66
+ * One message's body without the subject's content, or null when it carries none of it (or was
67
+ * already redacted). Pure: the erasure and the screen at acceptance both decide with it.
68
+ */
69
+ export function brokerBodyWithoutSubject(kind, body, subjectId, at) {
70
+ let row;
71
+ try {
72
+ row = JSON.parse(body);
73
+ }
74
+ catch {
75
+ row = undefined;
76
+ }
77
+ // Fail closed: a body the broker cannot read as a row, and that names the subject, goes whole.
78
+ if (!isObject(row))
79
+ return body.includes(subjectId) ? redactedIntentPayload(subjectId, at) : null;
80
+ if (isRedactedWhole(row))
81
+ return null;
82
+ if (kind === 'event') {
83
+ // The outbox's own predicate: `subject_id = ? AND pii_class != 'none' AND payload IS NOT NULL`.
84
+ if (row.subject_id !== subjectId || row.pii_class === 'none' || row.payload === null || row.payload === undefined)
85
+ return null;
86
+ return JSON.stringify({ ...row, payload: null });
87
+ }
88
+ const payload = row.payload;
89
+ if (typeof payload !== 'string')
90
+ return body.includes(subjectId) ? redactedIntentPayload(subjectId, at) : null;
91
+ // The intent journal's own predicates (#1600, #1632).
92
+ if (intentPayloadCarriesSubject(payload, subjectId))
93
+ return JSON.stringify({ ...row, payload: redactedIntentPayload(subjectId, at) });
94
+ if (row.kind === SWEEP_RUNS_KIND) {
95
+ const rewritten = sweepRunPayloadWithoutSubject(payload, subjectId);
96
+ return rewritten === null ? null : JSON.stringify({ ...row, payload: rewritten });
97
+ }
98
+ return null;
99
+ }
100
+ /**
101
+ * Erase one subject from one broker: tombstone them for the scope, then rewrite every message of that
102
+ * scope whose body carries them, whatever the state of its deliveries (held, pending, delivered,
103
+ * dead), and put a note in place of each receiver's error text about them. The columns it reaches are
104
+ * `BROKER_CONTENT_COLUMNS`. Synchronous; `BrokerStore.eraseSubject` runs it in one transaction.
105
+ * Idempotent: a re-run finds the bodies rewritten and changes nothing.
106
+ *
107
+ * The read narrows by substring (`instr`), as the spine's intent redaction does, and the predicate
108
+ * decides. A subject id is a ULID, so the JSON text of a body holds it unescaped at any depth.
109
+ * Unindexed, and affordable: it runs once per staff-triggered erasure over what one broker holds, a
110
+ * bounded window (K-70).
111
+ */
112
+ export function eraseSubjectFromBroker(sql, target) {
113
+ const { tenantId, scopeId, subjectId, at } = target;
114
+ sql.run(`INSERT OR IGNORE INTO broker_erased_subjects (tenant_id, scope_id, subject_id, erased_at) VALUES (?, ?, ?, ?)`, tenantId, scopeId, subjectId, at);
115
+ const rows = sql.all('SELECT id, kind, body FROM broker_messages WHERE tenant_id = ? AND scope_id = ? AND instr(body, ?) > 0', tenantId, scopeId, subjectId);
116
+ const rewritten = [];
117
+ for (const row of rows) {
118
+ const body = brokerBodyWithoutSubject(row.kind, row.body, subjectId, at);
119
+ if (body === null)
120
+ continue;
121
+ sql.run('UPDATE broker_messages SET body = ? WHERE id = ?', body, row.id);
122
+ rewritten.push(row.id);
123
+ }
124
+ // Each receiver's error text goes with its message, and wherever it names the subject (#1632's
125
+ // rule), at every receiver and in every state: pending, delivered, dead.
126
+ sql.run(`UPDATE broker_deliveries SET last_error = ?
127
+ WHERE last_error IS NOT NULL AND last_error != ?
128
+ AND message_id IN (SELECT id FROM broker_messages WHERE tenant_id = ? AND scope_id = ?)
129
+ AND (message_id IN (SELECT value FROM json_each(?)) OR instr(last_error, ?) > 0)`, REDACTED_FAILURE_NOTE, REDACTED_FAILURE_NOTE, tenantId, scopeId, JSON.stringify(rewritten), subjectId);
130
+ // #3469: what the relay keeps for this scope (`broker-relay-erasure.ts`).
131
+ eraseSubjectFromRelay(sql, target, rewritten);
132
+ return { messages: rewritten.length };
133
+ }
134
+ /**
135
+ * The messages of one handoff with every tombstoned subject's content taken out, before the store
136
+ * keeps them: a handoff the scope read before an erasure, arriving after it, stores nothing in the
137
+ * clear. Messages of a scope with no tombstone pass as they are. A tombstone read that fails throws,
138
+ * so the handoff is not acknowledged and the scope retries it: fail closed.
139
+ */
140
+ export function screenErasedSubjects(sql, messages) {
141
+ const tombstones = new Map();
142
+ const of = (m) => {
143
+ const key = `${m.tenantId}/${m.scopeId}`;
144
+ let found = tombstones.get(key);
145
+ if (!found) {
146
+ found = sql.all('SELECT subject_id, erased_at FROM broker_erased_subjects WHERE tenant_id = ? AND scope_id = ?', m.tenantId, m.scopeId);
147
+ tombstones.set(key, found);
148
+ }
149
+ return found;
150
+ };
151
+ return messages.map((m) => {
152
+ let body = m.body;
153
+ for (const t of of(m)) {
154
+ if (!body.includes(t.subject_id))
155
+ continue;
156
+ body = brokerBodyWithoutSubject(m.kind, body, t.subject_id, t.erased_at) ?? body;
157
+ }
158
+ return body === m.body ? m : { ...m, body };
159
+ });
160
+ }
161
+ //# sourceMappingURL=broker-erasure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-erasure.js","sourceRoot":"","sources":["../src/broker-erasure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,2BAA2B,EAC3B,qBAAqB,EACrB,6BAA6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C;QACzC,IAAI,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,KAAK,GAAG,GAAG,CAAC,GAAG,CACb,+FAA+F,EAC/F,CAAC,CAAC,QAAQ,EACV,CAAC,CAAC,OAAO,CACV,CAAC;YACF,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACxB,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC;QAClB,KAAK,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACtB,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC;gBAAE,SAAS;YAC3C,IAAI,GAAG,wBAAwB,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC;QACnF,CAAC;QACD,OAAO,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { BrokerSql } from './delivery-broker.js';
2
+ /** The relay's columns that can hold content or text about it, which `BROKER_CONTENT_COLUMNS` lists. */
3
+ export declare const RELAY_CONTENT_COLUMNS: readonly {
4
+ table: string;
5
+ column: string;
6
+ }[];
7
+ /** A request's or reply's body without the subject: the marker when it names them, else null. */
8
+ export declare function relayBodyWithoutSubject(body: unknown, subjectId: string, at: string): unknown | null;
9
+ /** A body about to be stored for a scope, with every subject tombstoned there taken out of it. */
10
+ export declare function screenRelayBody(sql: BrokerSql, tenantId: string, scopeId: string, body: unknown): unknown;
11
+ /**
12
+ * A failure's text about to be stored for a scope: the redacted note when it names a subject tombstoned
13
+ * there. A call that was out when the erasure ran may fail with the content in its message, and that
14
+ * text is written after the erasure's own rewrite.
15
+ */
16
+ export declare function screenRelayError(sql: BrokerSql, tenantId: string, scopeId: string, text: string): string;
17
+ /**
18
+ * Erase one subject from the relay's tables, for one scope of this app. `rewritten` is the message
19
+ * ids `eraseSubjectFromBroker` redacted, whose hops' error texts go with them. Idempotent.
20
+ */
21
+ export declare function eraseSubjectFromRelay(sql: BrokerSql, target: {
22
+ tenantId: string;
23
+ scopeId: string;
24
+ subjectId: string;
25
+ at: string;
26
+ }, rewritten: readonly string[]): void;
27
+ //# sourceMappingURL=broker-relay-erasure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-relay-erasure.d.ts","sourceRoot":"","sources":["../src/broker-relay-erasure.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,wGAAwG;AACxG,eAAO,MAAM,qBAAqB,EAAE,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAQ7E,CAAC;AAkBF,iGAAiG;AACjG,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAGpG;AAED,kGAAkG;AAClG,wBAAgB,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAYzG;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CASxG;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,SAAS,EACd,MAAM,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,EAC5E,SAAS,EAAE,SAAS,MAAM,EAAE,GAC3B,IAAI,CAqEN"}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Subject erasure in the broker relay's tables (#3484 for #3469, K-37, K-74): the relay's half of
3
+ * `eraseSubjectFromBroker`, which calls it, and of the screen a late arrival meets.
4
+ *
5
+ * What the relay keeps that can carry a subject is a request's or a reply's body, which the two apps'
6
+ * host code write and the platform does not classify, and the text of a failure about one. Neither is
7
+ * a spine row with a subject column, so a body is judged the way `broker-erasure.ts` judges a body it
8
+ * cannot read as a row: one that names the subject is replaced whole, failing closed. An event is not
9
+ * reached: a relayed event crosses only classified `none`, which the spine never redacts, and a copy
10
+ * judged more harshly than its original is an incoherent rule. Its row in `broker_messages` is
11
+ * `eraseSubjectFromBroker`'s.
12
+ *
13
+ * Each place is reached for the scope it belongs to: what a scope of this app sent (`outgoing`), what
14
+ * one of its scopes waits for (`waits`), and what another app relayed to one of its scopes (`inbox`,
15
+ * by the envelope's `target`). The tombstone `eraseSubjectFromBroker` leaves closes the race: what is
16
+ * stored after it, on the way out or on arrival, goes through `screenRelayBody` first.
17
+ */
18
+ import { REDACTED_FAILURE_NOTE, REDACTED_INTENT_MARKER, redactedIntentPayload } from './subject-redaction.js';
19
+ /** The relay's columns that can hold content or text about it, which `BROKER_CONTENT_COLUMNS` lists. */
20
+ export const RELAY_CONTENT_COLUMNS = [
21
+ { table: 'broker_relay_hops', column: 'last_error' },
22
+ { table: 'broker_relay_outgoing', column: 'body' },
23
+ { table: 'broker_relay_outgoing', column: 'last_error' },
24
+ { table: 'broker_relay_inbox', column: 'envelope_body' },
25
+ { table: 'broker_relay_inbox', column: 'last_error' },
26
+ { table: 'broker_waits', column: 'reply_body' },
27
+ { table: 'broker_waits', column: 'resume_error' },
28
+ ];
29
+ /**
30
+ * Is this value exactly the marker an erasure of this subject writes, and nothing else? The key alone
31
+ * is no proof: an app's body may carry it with content inside. Only the marker's own shape is, since
32
+ * a body that is exactly that shape holds nothing but the subject's id and an instant, which is all
33
+ * the marker itself holds.
34
+ */
35
+ function isMarkerFor(v, subjectId) {
36
+ if (typeof v !== 'object' || v === null || Array.isArray(v))
37
+ return false;
38
+ if (Object.keys(v).length !== 1 || !(REDACTED_INTENT_MARKER in v))
39
+ return false;
40
+ const m = v[REDACTED_INTENT_MARKER];
41
+ if (typeof m !== 'object' || m === null || Array.isArray(m))
42
+ return false;
43
+ const keys = Object.keys(m).sort();
44
+ const r = m;
45
+ return keys.join() === 'at,reason,subjectId' && r.reason === 'subject-erasure' && r.subjectId === subjectId && typeof r.at === 'string';
46
+ }
47
+ /** A request's or reply's body without the subject: the marker when it names them, else null. */
48
+ export function relayBodyWithoutSubject(body, subjectId, at) {
49
+ if (isMarkerFor(body, subjectId))
50
+ return null;
51
+ return JSON.stringify(body ?? null).includes(subjectId) ? JSON.parse(redactedIntentPayload(subjectId, at)) : null;
52
+ }
53
+ /** A body about to be stored for a scope, with every subject tombstoned there taken out of it. */
54
+ export function screenRelayBody(sql, tenantId, scopeId, body) {
55
+ const text = JSON.stringify(body ?? null);
56
+ for (const t of sql.all('SELECT subject_id, erased_at FROM broker_erased_subjects WHERE tenant_id = ? AND scope_id = ?', tenantId, scopeId)) {
57
+ if (!text.includes(t.subject_id))
58
+ continue;
59
+ const redacted = relayBodyWithoutSubject(body, t.subject_id, t.erased_at);
60
+ if (redacted !== null)
61
+ return redacted;
62
+ }
63
+ return body;
64
+ }
65
+ /**
66
+ * A failure's text about to be stored for a scope: the redacted note when it names a subject tombstoned
67
+ * there. A call that was out when the erasure ran may fail with the content in its message, and that
68
+ * text is written after the erasure's own rewrite.
69
+ */
70
+ export function screenRelayError(sql, tenantId, scopeId, text) {
71
+ for (const t of sql.all('SELECT subject_id FROM broker_erased_subjects WHERE tenant_id = ? AND scope_id = ?', tenantId, scopeId)) {
72
+ if (text.includes(t.subject_id))
73
+ return REDACTED_FAILURE_NOTE;
74
+ }
75
+ return text;
76
+ }
77
+ /**
78
+ * Erase one subject from the relay's tables, for one scope of this app. `rewritten` is the message
79
+ * ids `eraseSubjectFromBroker` redacted, whose hops' error texts go with them. Idempotent.
80
+ */
81
+ export function eraseSubjectFromRelay(sql, target, rewritten) {
82
+ const { tenantId, scopeId, subjectId, at } = target;
83
+ const note = (table, column, where, ...params) => sql.run(`UPDATE ${table} SET ${column} = ? WHERE ${column} IS NOT NULL AND ${column} != ? AND instr(${column}, ?) > 0 AND ${where}`, REDACTED_FAILURE_NOTE, REDACTED_FAILURE_NOTE, subjectId, ...params);
84
+ // What a scope of this app sent: a request or a reply, as its host code wrote it.
85
+ for (const r of sql.all('SELECT id, body FROM broker_relay_outgoing WHERE tenant_id = ? AND scope_id = ? AND instr(body, ?) > 0', tenantId, scopeId, subjectId)) {
86
+ const body = relayBodyWithoutSubject(JSON.parse(r.body), subjectId, at);
87
+ if (body !== null)
88
+ sql.run('UPDATE broker_relay_outgoing SET body = ? WHERE id = ?', JSON.stringify(body), r.id);
89
+ }
90
+ note('broker_relay_outgoing', 'last_error', 'tenant_id = ? AND scope_id = ?', tenantId, scopeId);
91
+ // The reply a scope of this app was answered with.
92
+ for (const w of sql.all('SELECT request_id, reply_body FROM broker_waits WHERE tenant_id = ? AND scope_id = ? AND instr(reply_body, ?) > 0', tenantId, scopeId, subjectId)) {
93
+ const reply = JSON.parse(w.reply_body);
94
+ const body = relayBodyWithoutSubject(reply.body, subjectId, at);
95
+ if (body !== null)
96
+ sql.run('UPDATE broker_waits SET reply_body = ? WHERE request_id = ?', JSON.stringify({ ...reply, body }), w.request_id);
97
+ }
98
+ note('broker_waits', 'resume_error', 'tenant_id = ? AND scope_id = ?', tenantId, scopeId);
99
+ // What another app relayed to a scope of this app. An event's body is left (see the header).
100
+ const inboxOf = `json_extract(envelope_body, '$.tenantId') = ? AND json_extract(envelope_body, '$.target.scopeId') = ?`;
101
+ for (const r of sql.all(`SELECT caller_app, id, envelope_body FROM broker_relay_inbox WHERE kind != 'event' AND ${inboxOf} AND instr(envelope_body, ?) > 0`, tenantId, scopeId, subjectId)) {
102
+ const envelope = JSON.parse(r.envelope_body);
103
+ const body = relayBodyWithoutSubject(envelope.message.body, subjectId, at);
104
+ if (body === null)
105
+ continue;
106
+ sql.run('UPDATE broker_relay_inbox SET envelope_body = ? WHERE caller_app = ? AND id = ?', JSON.stringify({ ...envelope, message: { ...envelope.message, body } }), r.caller_app, r.id);
107
+ }
108
+ note('broker_relay_inbox', 'last_error', inboxOf, tenantId, scopeId);
109
+ // An event's hops: their error text goes with a rewritten message, and wherever it names the subject.
110
+ sql.run(`UPDATE broker_relay_hops SET last_error = ?
111
+ WHERE last_error IS NOT NULL AND last_error != ?
112
+ AND message_id IN (SELECT id FROM broker_messages WHERE tenant_id = ? AND scope_id = ?)
113
+ AND (message_id IN (SELECT value FROM json_each(?)) OR instr(last_error, ?) > 0)`, REDACTED_FAILURE_NOTE, REDACTED_FAILURE_NOTE, tenantId, scopeId, JSON.stringify(rewritten), subjectId);
114
+ }
115
+ //# sourceMappingURL=broker-relay-erasure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broker-relay-erasure.js","sourceRoot":"","sources":["../src/broker-relay-erasure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAG9G,wGAAwG;AACxG,MAAM,CAAC,MAAM,qBAAqB,GAAiD;IACjF,EAAE,KAAK,EAAE,mBAAmB,EAAE,MAAM,EAAE,YAAY,EAAE;IACpD,EAAE,KAAK,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,EAAE;IAClD,EAAE,KAAK,EAAE,u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