@pithy-sh/audit 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -0
- package/dist/accessHistory.d.ts +315 -0
- package/dist/accessHistory.d.ts.map +1 -0
- package/dist/accessHistory.js +299 -0
- package/dist/actions.d.ts +15 -1
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +16 -2
- package/dist/capability.d.ts +14 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +106 -12
- package/dist/chain/append.d.ts +43 -0
- package/dist/chain/append.d.ts.map +1 -0
- package/dist/chain/append.js +110 -0
- package/dist/chain/checkpoint.d.ts +70 -0
- package/dist/chain/checkpoint.d.ts.map +1 -0
- package/dist/chain/checkpoint.js +133 -0
- package/dist/chain/lockCheck.d.ts +15 -0
- package/dist/chain/lockCheck.d.ts.map +1 -0
- package/dist/chain/lockCheck.js +75 -0
- package/dist/chain/prune.d.ts +84 -0
- package/dist/chain/prune.d.ts.map +1 -0
- package/dist/chain/prune.js +231 -0
- package/dist/chain/rows.d.ts +74 -0
- package/dist/chain/rows.d.ts.map +1 -0
- package/dist/chain/rows.js +133 -0
- package/dist/chain/tombstone.d.ts +21 -0
- package/dist/chain/tombstone.d.ts.map +1 -0
- package/dist/chain/tombstone.js +98 -0
- package/dist/chain/verify.d.ts +63 -0
- package/dist/chain/verify.d.ts.map +1 -0
- package/dist/chain/verify.js +220 -0
- package/dist/citations.d.ts +17 -0
- package/dist/citations.d.ts.map +1 -0
- package/dist/citations.js +48 -0
- package/dist/cli/emitFromCLI.d.ts +5 -0
- package/dist/cli/emitFromCLI.d.ts.map +1 -1
- package/dist/cli/emitFromCLI.js +5 -0
- package/dist/data/auditEvent.d.ts +4 -0
- package/dist/data/auditEvent.d.ts.map +1 -1
- package/dist/data/auditEvent.js +7 -3
- package/dist/data/sinkState.d.ts +23 -0
- package/dist/data/sinkState.d.ts.map +1 -0
- package/dist/data/sinkState.js +25 -0
- package/dist/data/tables.d.ts +14 -1
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +5 -1
- package/dist/http/guards.d.ts +39 -6
- package/dist/http/guards.d.ts.map +1 -1
- package/dist/http/guards.js +101 -17
- package/dist/http/responses.d.ts +503 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +157 -5
- package/dist/http/routes.d.ts +25 -3
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +321 -15
- package/dist/http/schemas.d.ts +67 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +88 -4
- package/dist/http/views.d.ts +25 -1
- package/dist/http/views.d.ts.map +1 -1
- package/dist/http/views.js +107 -2
- package/dist/migrations/0003_call_index.d.ts +21 -0
- package/dist/migrations/0003_call_index.d.ts.map +1 -0
- package/dist/migrations/0003_call_index.js +33 -0
- package/dist/migrations/0004_request_index.d.ts +16 -0
- package/dist/migrations/0004_request_index.d.ts.map +1 -0
- package/dist/migrations/0004_request_index.js +24 -0
- package/dist/migrations/0005_chain.d.ts +24 -0
- package/dist/migrations/0005_chain.d.ts.map +1 -0
- package/dist/migrations/0005_chain.js +43 -0
- package/dist/migrations/0006_sinks.d.ts +11 -0
- package/dist/migrations/0006_sinks.d.ts.map +1 -0
- package/dist/migrations/0006_sinks.js +20 -0
- package/dist/provision/resolveAuditConfig.d.ts +31 -0
- package/dist/provision/resolveAuditConfig.d.ts.map +1 -0
- package/dist/provision/resolveAuditConfig.js +43 -0
- package/dist/purge/contribution.d.ts +21 -0
- package/dist/purge/contribution.d.ts.map +1 -0
- package/dist/purge/contribution.js +41 -0
- package/dist/reach.d.ts +250 -0
- package/dist/reach.d.ts.map +1 -0
- package/dist/reach.js +244 -0
- package/dist/recorder.d.ts +11 -0
- package/dist/recorder.d.ts.map +1 -1
- package/dist/recorder.js +23 -5
- package/dist/routePattern.d.ts +60 -0
- package/dist/routePattern.d.ts.map +1 -0
- package/dist/routePattern.js +130 -0
- package/dist/seeds/example.d.ts.map +1 -1
- package/dist/seeds/example.js +23 -6
- package/dist/ship/consumer.d.ts +45 -0
- package/dist/ship/consumer.d.ts.map +1 -0
- package/dist/ship/consumer.js +141 -0
- package/dist/ship/event.d.ts +59 -0
- package/dist/ship/event.d.ts.map +1 -0
- package/dist/ship/event.js +77 -0
- package/dist/ship/health.d.ts +33 -0
- package/dist/ship/health.d.ts.map +1 -0
- package/dist/ship/health.js +80 -0
- package/dist/ship/profiles.d.ts +34 -0
- package/dist/ship/profiles.d.ts.map +1 -0
- package/dist/ship/profiles.js +62 -0
- package/dist/ship/queue.d.ts +57 -0
- package/dist/ship/queue.d.ts.map +1 -0
- package/dist/ship/queue.js +70 -0
- package/dist/ship/secrets.d.ts +7 -0
- package/dist/ship/secrets.d.ts.map +1 -0
- package/dist/ship/secrets.js +40 -0
- package/dist/ship/shipper.d.ts +15 -0
- package/dist/ship/shipper.d.ts.map +1 -0
- package/dist/ship/shipper.js +27 -0
- package/dist/ship/sinks.d.ts +54 -0
- package/dist/ship/sinks.d.ts.map +1 -0
- package/dist/ship/sinks.js +45 -0
- package/dist/ship/state.d.ts +27 -0
- package/dist/ship/state.d.ts.map +1 -0
- package/dist/ship/state.js +29 -0
- package/dist/usage.d.ts +159 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +87 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/workflows/prune.d.ts +73 -0
- package/dist/workflows/prune.d.ts.map +1 -0
- package/dist/workflows/prune.js +128 -0
- package/dist/workflows/retryPolicy.d.ts +10 -0
- package/dist/workflows/retryPolicy.d.ts.map +1 -0
- package/dist/workflows/retryPolicy.js +14 -0
- package/dist/workflows/specs.d.ts +70 -0
- package/dist/workflows/specs.d.ts.map +1 -0
- package/dist/workflows/specs.js +70 -0
- package/dist/workflows/worker.d.ts +32 -0
- package/dist/workflows/worker.d.ts.map +1 -0
- package/dist/workflows/worker.js +41 -0
- package/package.json +5 -2
- package/pithy.manifest.json +36 -4
- package/src/accessHistory.ts +360 -0
- package/src/actions.ts +15 -1
- package/src/capability.ts +134 -12
- package/src/chain/append.ts +161 -0
- package/src/chain/checkpoint.ts +172 -0
- package/src/chain/lockCheck.ts +72 -0
- package/src/chain/prune.ts +340 -0
- package/src/chain/rows.ts +211 -0
- package/src/chain/tombstone.ts +104 -0
- package/src/chain/verify.ts +366 -0
- package/src/citations.ts +62 -0
- package/src/cli/emitFromCLI.ts +5 -0
- package/src/cloudflare-test.d.ts +1 -0
- package/src/data/auditEvent.ts +31 -1
- package/src/data/sinkState.ts +41 -0
- package/src/data/tables.ts +7 -1
- package/src/http/guards.ts +90 -6
- package/src/http/responses.ts +318 -3
- package/src/http/routes.ts +379 -7
- package/src/http/schemas.ts +158 -2
- package/src/http/views.ts +123 -1
- package/src/migrations/0003_call_index.ts +35 -0
- package/src/migrations/0004_request_index.ts +26 -0
- package/src/migrations/0005_chain.ts +56 -0
- package/src/migrations/0006_sinks.ts +28 -0
- package/src/provision/resolveAuditConfig.ts +60 -0
- package/src/purge/contribution.ts +39 -0
- package/src/reach.ts +454 -0
- package/src/recorder.ts +64 -42
- package/src/routePattern.ts +141 -0
- package/src/seeds/example.ts +13 -0
- package/src/ship/consumer.ts +169 -0
- package/src/ship/event.ts +86 -0
- package/src/ship/health.ts +92 -0
- package/src/ship/profiles.ts +99 -0
- package/src/ship/queue.ts +108 -0
- package/src/ship/secrets.ts +42 -0
- package/src/ship/shipper.ts +32 -0
- package/src/ship/sinks.ts +82 -0
- package/src/ship/state.ts +61 -0
- package/src/usage.ts +176 -0
- package/src/version.generated.ts +1 -1
- package/src/workflows/prune.ts +196 -0
- package/src/workflows/retryPolicy.ts +14 -0
- package/src/workflows/specs.ts +92 -0
- package/src/workflows/worker.ts +65 -0
- package/src/workflows/wrangler.jsonc +40 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: FSL-1.1-MIT
|
|
3
|
+
|
|
4
|
+
import type { R2Bucket } from "@cloudflare/workers-types";
|
|
5
|
+
import {
|
|
6
|
+
AUDIT_TOMBSTONE_FORMAT,
|
|
7
|
+
type AuditTombstone,
|
|
8
|
+
type ChainLink,
|
|
9
|
+
chainVerifier,
|
|
10
|
+
} from "@pithy-sh/core/src/audit/chain";
|
|
11
|
+
import { chunkByBoundParameters } from "@pithy-sh/core/src/data/boundParameters";
|
|
12
|
+
import { auditWindowDays, type EffectiveRetention, MS_PER_DAY } from "@pithy-sh/core/src/retention/policy";
|
|
13
|
+
import { type Expression, type SqlBool, sql } from "kysely";
|
|
14
|
+
import type { AuditDatabase } from "../data/tables";
|
|
15
|
+
import { readCheckpoints } from "./checkpoint";
|
|
16
|
+
import { type ChainRow, chainedEventOf, readChainHead, readChainLink, readChainRows, whereChain } from "./rows";
|
|
17
|
+
import { readTombstones, tombstoneEndingAt, writeTombstone } from "./tombstone";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* **The retention prune: audit events past their window, removed without breaking the chain.**
|
|
21
|
+
*
|
|
22
|
+
* The Worker's `retention.audit` block says how long each action is kept. An event past its window is removed, and
|
|
23
|
+
* because every chained event names the one before it, a removed run leaves a tombstone in the checkpoint bucket
|
|
24
|
+
* first: the run's positions, the link before it, and the hashes at either end. A verify bridges the gap with it and
|
|
25
|
+
* reads the chain as whole. Without a checkpoint bucket nothing chained is pruned, since nothing could vouch for the
|
|
26
|
+
* gap.
|
|
27
|
+
*
|
|
28
|
+
* - **Runs, not single events.** Windows differ by action, so an event kept longer can sit between two that have
|
|
29
|
+
* passed theirs. Each maximal run of consecutive positions past their windows is pruned on its own, and the event
|
|
30
|
+
* kept between two runs stays where it is.
|
|
31
|
+
* - **A run is verified before it goes.** Its links, its hashes and the link before it are checked as a verify would,
|
|
32
|
+
* and a run that does not verify is left in place and counted as refused: the prune never removes the evidence of a
|
|
33
|
+
* break. A run whose positions hold any row it did not read, a second row at one position among them, is refused
|
|
34
|
+
* too, and the delete names the ids of the rows it verified, never a range of positions.
|
|
35
|
+
* - **The head is never pruned.** The next append links to it, so the last event of every chain stays.
|
|
36
|
+
* - **Rows from before the chain** carry no link, and are removed on their own window with nothing to tombstone.
|
|
37
|
+
* - **A page reads only rows past their own windows.** An event kept longer than the shortest window, a floored
|
|
38
|
+
* control-plane call under a short default, is never read, so however many of them sit at the front of a chain a
|
|
39
|
+
* page still reaches the events behind them.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/** How many rows one prune step reads. */
|
|
43
|
+
export const AUDIT_PRUNE_PAGE = 500;
|
|
44
|
+
|
|
45
|
+
/** The shortest window any action can have under this policy, in days, or null when nothing is ever pruned. */
|
|
46
|
+
export function shortestWindowDays(retention: EffectiveRetention): number | null {
|
|
47
|
+
const days = [
|
|
48
|
+
...retention.audit.rules.map((rule) => rule.days),
|
|
49
|
+
...(retention.audit.defaultDays === null ? [] : [retention.audit.defaultDays]),
|
|
50
|
+
];
|
|
51
|
+
return days.length === 0 ? null : Math.min(...days);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether an event with this action, occurring at `occurredAt` ms, is past its window at `now`. */
|
|
55
|
+
export function pastWindow(retention: EffectiveRetention, action: string, occurredAt: number, now: Date): boolean {
|
|
56
|
+
const days = auditWindowDays(retention, action);
|
|
57
|
+
return days !== null && occurredAt < now.getTime() - days * MS_PER_DAY;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The actions that share one window, and the instant, in stored ms, before which an event with any of them is past it. */
|
|
61
|
+
export interface ExpiryCutoff {
|
|
62
|
+
readonly before: number;
|
|
63
|
+
readonly actions: readonly string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The actions grouped by the instant each is past its window at `now`. An action kept forever is in no group. */
|
|
67
|
+
export function expiryCutoffs(retention: EffectiveRetention, actions: readonly string[], now: Date): ExpiryCutoff[] {
|
|
68
|
+
const groups = new Map<number, string[]>();
|
|
69
|
+
for (const action of new Set(actions)) {
|
|
70
|
+
const days = auditWindowDays(retention, action);
|
|
71
|
+
if (days === null) continue;
|
|
72
|
+
const before = now.getTime() - days * MS_PER_DAY;
|
|
73
|
+
groups.set(before, [...(groups.get(before) ?? []), action]);
|
|
74
|
+
}
|
|
75
|
+
return [...groups.entries()]
|
|
76
|
+
.sort(([a], [b]) => a - b)
|
|
77
|
+
.map(([before, grouped]) => ({ before, actions: grouped.sort() }));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The rows past their own windows, as one condition. Each group's actions travel as one JSON array, so the bound
|
|
82
|
+
* parameters stay two per window however many actions the trail holds.
|
|
83
|
+
*/
|
|
84
|
+
function pastCutoffs(cutoffs: readonly ExpiryCutoff[]): Expression<SqlBool> {
|
|
85
|
+
return sql<SqlBool>`(${sql.join(
|
|
86
|
+
cutoffs.map(
|
|
87
|
+
(cutoff) =>
|
|
88
|
+
sql`(${sql.ref("action")} in (select value from json_each(${JSON.stringify(cutoff.actions)})) and ${sql.ref("occurredAt")} < ${cutoff.before})`,
|
|
89
|
+
),
|
|
90
|
+
sql` or `,
|
|
91
|
+
)})`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Every maximal run of consecutive positions among `rows` that are all past their windows. */
|
|
95
|
+
export function expiredRuns(rows: readonly ChainRow[], retention: EffectiveRetention, now: Date): ChainRow[][] {
|
|
96
|
+
const runs: ChainRow[][] = [];
|
|
97
|
+
let current: ChainRow[] = [];
|
|
98
|
+
for (const row of rows) {
|
|
99
|
+
const expired = pastWindow(retention, row.action, row.occurredAt, now);
|
|
100
|
+
const last = current.at(-1);
|
|
101
|
+
const follows = last !== undefined && row.chainSeq === (last.chainSeq ?? 0) + 1;
|
|
102
|
+
if (!expired || (last && !follows)) {
|
|
103
|
+
if (current.length > 0) runs.push(current);
|
|
104
|
+
current = [];
|
|
105
|
+
}
|
|
106
|
+
if (expired) current.push(row);
|
|
107
|
+
}
|
|
108
|
+
if (current.length > 0) runs.push(current);
|
|
109
|
+
return runs;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What one page of one chain's prune did. */
|
|
113
|
+
export interface ChainPrunePage {
|
|
114
|
+
/** How many events were removed. */
|
|
115
|
+
pruned: number;
|
|
116
|
+
/** How many runs were past their windows and left in place, because they did not verify. */
|
|
117
|
+
refused: number;
|
|
118
|
+
/** The position the next page resumes after, or null when this page reached the end of what is pruneable. */
|
|
119
|
+
next: number | null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The link before a run at `fromSeq`: null at the chain's start, else the stored event or a tombstone ending there. */
|
|
123
|
+
async function linkBeforeRun(
|
|
124
|
+
db: AuditDatabase,
|
|
125
|
+
tenant: string | null,
|
|
126
|
+
fromSeq: number,
|
|
127
|
+
tombstones: readonly AuditTombstone[],
|
|
128
|
+
): Promise<ChainLink | null | undefined> {
|
|
129
|
+
if (fromSeq === 1) return null;
|
|
130
|
+
const stored = await readChainLink(db, tenant, fromSeq - 1);
|
|
131
|
+
if (stored) return stored;
|
|
132
|
+
const run = tombstoneEndingAt(tombstones, fromSeq - 1);
|
|
133
|
+
return run ? { seq: run.throughSeq, hash: run.last } : undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Whether a run is whole: every link and hash, from the link before it, against the checkpoints covering it. */
|
|
137
|
+
async function runVerifies(
|
|
138
|
+
bucket: R2Bucket,
|
|
139
|
+
tenant: string | null,
|
|
140
|
+
run: readonly ChainRow[],
|
|
141
|
+
prior: ChainLink | null,
|
|
142
|
+
): Promise<boolean> {
|
|
143
|
+
const from = run[0]?.chainSeq ?? 0;
|
|
144
|
+
const through = run.at(-1)?.chainSeq ?? 0;
|
|
145
|
+
const checkpoints = await readCheckpoints(bucket, tenant, { fromCount: from, throughCount: through });
|
|
146
|
+
const verifier = chainVerifier({ tenant, prior, checkpoints });
|
|
147
|
+
for (const row of run) {
|
|
148
|
+
if (await verifier.push(await chainedEventOf(row, { detail: false }))) return false;
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** How many rows of this chain sit at positions `fromSeq` through `throughSeq`. */
|
|
154
|
+
async function rowsAt(db: AuditDatabase, tenant: string | null, fromSeq: number, throughSeq: number): Promise<number> {
|
|
155
|
+
const row = await whereChain(
|
|
156
|
+
db
|
|
157
|
+
.selectFrom("pithyAuditEvents")
|
|
158
|
+
.select((eb) => eb.fn.countAll<number>().as("count"))
|
|
159
|
+
.where("chainSeq", ">=", fromSeq)
|
|
160
|
+
.where("chainSeq", "<=", throughSeq),
|
|
161
|
+
tenant,
|
|
162
|
+
).executeTakeFirst();
|
|
163
|
+
return Number(row?.count ?? 0);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Prune one page of one chain: read up to `limit` chained rows after `afterSeq` that are old enough for the shortest
|
|
168
|
+
* window, and remove every run among them that is past its windows and verifies, tombstone first.
|
|
169
|
+
*/
|
|
170
|
+
export async function pruneChainPage(
|
|
171
|
+
db: AuditDatabase,
|
|
172
|
+
bucket: R2Bucket,
|
|
173
|
+
options: { tenant: string | null; retention: EffectiveRetention; now: Date; afterSeq: number; limit: number },
|
|
174
|
+
): Promise<ChainPrunePage> {
|
|
175
|
+
const { tenant, retention, now } = options;
|
|
176
|
+
const shortest = shortestWindowDays(retention);
|
|
177
|
+
const head = await readChainHead(db, tenant);
|
|
178
|
+
if (shortest === null || !head || head.seq <= 1) return { pruned: 0, refused: 0, next: null };
|
|
179
|
+
const occurredBefore = now.getTime() - shortest * MS_PER_DAY;
|
|
180
|
+
const candidates = await whereChain(
|
|
181
|
+
db
|
|
182
|
+
.selectFrom("pithyAuditEvents")
|
|
183
|
+
.select("action")
|
|
184
|
+
.distinct()
|
|
185
|
+
.where("chainSeq", ">", options.afterSeq)
|
|
186
|
+
.where("chainSeq", "<=", head.seq - 1)
|
|
187
|
+
.where("occurredAt", "<", occurredBefore),
|
|
188
|
+
tenant,
|
|
189
|
+
).execute();
|
|
190
|
+
const cutoffs = expiryCutoffs(
|
|
191
|
+
retention,
|
|
192
|
+
candidates.map((row) => row.action),
|
|
193
|
+
now,
|
|
194
|
+
);
|
|
195
|
+
if (cutoffs.length === 0) return { pruned: 0, refused: 0, next: null };
|
|
196
|
+
const rows = await readChainRows(db, {
|
|
197
|
+
tenant,
|
|
198
|
+
afterSeq: options.afterSeq,
|
|
199
|
+
limit: options.limit,
|
|
200
|
+
throughSeq: head.seq - 1,
|
|
201
|
+
occurredBefore,
|
|
202
|
+
matching: pastCutoffs(cutoffs),
|
|
203
|
+
});
|
|
204
|
+
const tombstones = await readTombstones(bucket, tenant);
|
|
205
|
+
let pruned = 0;
|
|
206
|
+
let refused = 0;
|
|
207
|
+
for (const run of expiredRuns(rows, retention, now)) {
|
|
208
|
+
const first = run[0] as ChainRow;
|
|
209
|
+
const last = run.at(-1) as ChainRow;
|
|
210
|
+
const fromSeq = first.chainSeq as number;
|
|
211
|
+
if ((await rowsAt(db, tenant, fromSeq, last.chainSeq as number)) !== run.length) {
|
|
212
|
+
refused++;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const prior = await linkBeforeRun(db, tenant, fromSeq, tombstones);
|
|
216
|
+
if (prior === undefined || !(await runVerifies(bucket, tenant, run, prior))) {
|
|
217
|
+
refused++;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const stored = await writeTombstone(bucket, {
|
|
221
|
+
format: AUDIT_TOMBSTONE_FORMAT,
|
|
222
|
+
tenant,
|
|
223
|
+
fromSeq,
|
|
224
|
+
throughSeq: last.chainSeq as number,
|
|
225
|
+
count: run.length,
|
|
226
|
+
prior: prior?.hash ?? null,
|
|
227
|
+
first: first.chainHash as string,
|
|
228
|
+
last: last.chainHash as string,
|
|
229
|
+
prunedAt: now.toISOString(),
|
|
230
|
+
});
|
|
231
|
+
// A retry finds the tombstone an earlier attempt wrote. What it deletes is the run that tombstone names, and only
|
|
232
|
+
// when the rows in hand are the rows it names and no other row shares their positions.
|
|
233
|
+
const end = run.find((row) => row.chainSeq === stored.throughSeq);
|
|
234
|
+
const named = run.filter(
|
|
235
|
+
(row) => (row.chainSeq as number) >= stored.fromSeq && (row.chainSeq as number) <= stored.throughSeq,
|
|
236
|
+
);
|
|
237
|
+
if (
|
|
238
|
+
!end ||
|
|
239
|
+
end.chainHash !== stored.last ||
|
|
240
|
+
first.chainHash !== stored.first ||
|
|
241
|
+
named.length !== stored.count ||
|
|
242
|
+
(await rowsAt(db, tenant, stored.fromSeq, stored.throughSeq)) !== stored.count
|
|
243
|
+
) {
|
|
244
|
+
refused++;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const fixed = tenant === null ? 2 : 3;
|
|
248
|
+
for (const ids of chunkByBoundParameters(
|
|
249
|
+
named.map((row) => row.id),
|
|
250
|
+
fixed,
|
|
251
|
+
)) {
|
|
252
|
+
await whereChain(
|
|
253
|
+
db
|
|
254
|
+
.deleteFrom("pithyAuditEvents")
|
|
255
|
+
.where("id", "in", ids)
|
|
256
|
+
.where("chainSeq", ">=", stored.fromSeq)
|
|
257
|
+
.where("chainSeq", "<=", stored.throughSeq),
|
|
258
|
+
tenant,
|
|
259
|
+
).execute();
|
|
260
|
+
}
|
|
261
|
+
tombstones.push(stored);
|
|
262
|
+
pruned += stored.count;
|
|
263
|
+
}
|
|
264
|
+
const lastRead = rows.at(-1);
|
|
265
|
+
return { pruned, refused, next: rows.length === options.limit && lastRead ? (lastRead.chainSeq as number) : null };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The chains the trail holds: each tenant with a chained event, and null for the untenanted chain. */
|
|
269
|
+
export async function chainTenants(db: AuditDatabase): Promise<(string | null)[]> {
|
|
270
|
+
const rows = await db
|
|
271
|
+
.selectFrom("pithyAuditEvents")
|
|
272
|
+
.select("tenant")
|
|
273
|
+
.distinct()
|
|
274
|
+
.where("chainSeq", "is not", null)
|
|
275
|
+
.execute();
|
|
276
|
+
return rows.map((row) => row.tenant ?? null).sort((a, b) => (a ?? "").localeCompare(b ?? ""));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** What one page of the unchained prune did. */
|
|
280
|
+
export interface UnchainedPrunePage {
|
|
281
|
+
/** How many rows were removed. */
|
|
282
|
+
deleted: number;
|
|
283
|
+
/** The row id the next page resumes after, or null at the end. */
|
|
284
|
+
next: number | null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Prune one page of the rows that carry no chain — written before it began, or by an older recorder since. Each is
|
|
289
|
+
* removed on its own window, unless a hold stands on its tenant's trail, and nothing is tombstoned: no chain names it.
|
|
290
|
+
*/
|
|
291
|
+
export async function pruneUnchainedPage(
|
|
292
|
+
db: AuditDatabase,
|
|
293
|
+
options: {
|
|
294
|
+
retention: EffectiveRetention;
|
|
295
|
+
now: Date;
|
|
296
|
+
afterId: number;
|
|
297
|
+
limit: number;
|
|
298
|
+
held: (tenant: string | null) => Promise<boolean>;
|
|
299
|
+
},
|
|
300
|
+
): Promise<UnchainedPrunePage> {
|
|
301
|
+
const { retention, now } = options;
|
|
302
|
+
const shortest = shortestWindowDays(retention);
|
|
303
|
+
if (shortest === null) return { deleted: 0, next: null };
|
|
304
|
+
const occurredBefore = now.getTime() - shortest * MS_PER_DAY;
|
|
305
|
+
const candidates = await db
|
|
306
|
+
.selectFrom("pithyAuditEvents")
|
|
307
|
+
.select("action")
|
|
308
|
+
.distinct()
|
|
309
|
+
.where("chainSeq", "is", null)
|
|
310
|
+
.where("id", ">", options.afterId)
|
|
311
|
+
.where("occurredAt", "<", occurredBefore)
|
|
312
|
+
.execute();
|
|
313
|
+
const cutoffs = expiryCutoffs(
|
|
314
|
+
retention,
|
|
315
|
+
candidates.map((row) => row.action),
|
|
316
|
+
now,
|
|
317
|
+
);
|
|
318
|
+
if (cutoffs.length === 0) return { deleted: 0, next: null };
|
|
319
|
+
const rows = await db
|
|
320
|
+
.selectFrom("pithyAuditEvents")
|
|
321
|
+
.select(["id", "action", "occurredAt", "tenant"])
|
|
322
|
+
.where("chainSeq", "is", null)
|
|
323
|
+
.where("id", ">", options.afterId)
|
|
324
|
+
.where("occurredAt", "<", occurredBefore)
|
|
325
|
+
.where(pastCutoffs(cutoffs))
|
|
326
|
+
.orderBy("id", "asc")
|
|
327
|
+
.limit(options.limit)
|
|
328
|
+
.execute();
|
|
329
|
+
const holds = new Map<string | null, boolean>();
|
|
330
|
+
const doomed: number[] = [];
|
|
331
|
+
for (const row of rows) {
|
|
332
|
+
if (!pastWindow(retention, row.action, row.occurredAt as number, now)) continue;
|
|
333
|
+
const tenant = row.tenant ?? null;
|
|
334
|
+
if (!holds.has(tenant)) holds.set(tenant, await options.held(tenant));
|
|
335
|
+
if (!holds.get(tenant)) doomed.push(row.id as number);
|
|
336
|
+
}
|
|
337
|
+
if (doomed.length > 0) await db.deleteFrom("pithyAuditEvents").where("id", "in", doomed).execute();
|
|
338
|
+
const lastRead = rows.at(-1);
|
|
339
|
+
return { deleted: doomed.length, next: rows.length === options.limit && lastRead ? (lastRead.id as number) : null };
|
|
340
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: FSL-1.1-MIT
|
|
3
|
+
|
|
4
|
+
import { type ChainedEvent, type ChainedEventFields, type ChainLink, commitment } from "@pithy-sh/core/src/audit/chain";
|
|
5
|
+
import type { Expression, SqlBool } from "kysely";
|
|
6
|
+
import type { AuditDatabase } from "../data/tables";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* **Reading a tenant's chain out of `pithy_audit_events` (#822).**
|
|
10
|
+
*
|
|
11
|
+
* One chain per tenant, and one for the events with no tenant. Every read here names which, through
|
|
12
|
+
* {@link whereChain}, because `tenant = null` matches nothing in SQL and the untenanted chain would read as empty.
|
|
13
|
+
*
|
|
14
|
+
* Rows are read raw — the `z.input` side, `occurredAt` as its stored ms and `metadata` as its stored JSON text —
|
|
15
|
+
* because the hash covers the bytes as stored. Decoding the metadata and encoding it again would hash a value the
|
|
16
|
+
* row does not hold the first time the two serializations differ.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The columns a chain read selects: every hashed field, the link, and the personal values with their salt. */
|
|
20
|
+
const CHAIN_COLUMNS = [
|
|
21
|
+
"id",
|
|
22
|
+
"eventId",
|
|
23
|
+
"occurredAt",
|
|
24
|
+
"action",
|
|
25
|
+
"outcome",
|
|
26
|
+
"severity",
|
|
27
|
+
"actorType",
|
|
28
|
+
"actorId",
|
|
29
|
+
"actorKind",
|
|
30
|
+
"sessionId",
|
|
31
|
+
"resourceType",
|
|
32
|
+
"resourceId",
|
|
33
|
+
"requestId",
|
|
34
|
+
"tenant",
|
|
35
|
+
"project",
|
|
36
|
+
"environment",
|
|
37
|
+
"worker",
|
|
38
|
+
"version",
|
|
39
|
+
"ip",
|
|
40
|
+
"userAgent",
|
|
41
|
+
"metadata",
|
|
42
|
+
"chainSeq",
|
|
43
|
+
"chainPrev",
|
|
44
|
+
"chainHash",
|
|
45
|
+
"chainSalt",
|
|
46
|
+
] as const;
|
|
47
|
+
|
|
48
|
+
/** One stored row of a chain, raw. */
|
|
49
|
+
export interface ChainRow {
|
|
50
|
+
id: number;
|
|
51
|
+
eventId: string;
|
|
52
|
+
occurredAt: number;
|
|
53
|
+
action: string;
|
|
54
|
+
outcome: string;
|
|
55
|
+
severity: string;
|
|
56
|
+
actorType: string;
|
|
57
|
+
actorId: string | null;
|
|
58
|
+
actorKind: string | null;
|
|
59
|
+
sessionId: string | null;
|
|
60
|
+
resourceType: string | null;
|
|
61
|
+
resourceId: string | null;
|
|
62
|
+
requestId: string | null;
|
|
63
|
+
tenant: string | null;
|
|
64
|
+
project: string | null;
|
|
65
|
+
environment: string | null;
|
|
66
|
+
worker: string | null;
|
|
67
|
+
version: string | null;
|
|
68
|
+
ip: string | null;
|
|
69
|
+
userAgent: string | null;
|
|
70
|
+
metadata: string | null;
|
|
71
|
+
chainSeq: number | null;
|
|
72
|
+
chainPrev: string | null;
|
|
73
|
+
chainHash: string | null;
|
|
74
|
+
chainSalt: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A query builder narrowed to one chain: one tenant's, or the untenanted one. */
|
|
78
|
+
// biome-ignore lint/suspicious/noExplicitAny: Kysely's builders share no exported interface for `.where`.
|
|
79
|
+
export function whereChain<Q extends { where: (...args: any[]) => Q }>(query: Q, tenant: string | null): Q {
|
|
80
|
+
return tenant === null ? query.where("tenant", "is", null) : query.where("tenant", "=", tenant);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The chain's head — its last position and that event's hash — or null when it holds no event. */
|
|
84
|
+
export async function readChainHead(db: AuditDatabase, tenant: string | null): Promise<ChainLink | null> {
|
|
85
|
+
const row = await whereChain(
|
|
86
|
+
db
|
|
87
|
+
.selectFrom("pithyAuditEvents")
|
|
88
|
+
.select(["chainSeq", "chainHash"])
|
|
89
|
+
.where("chainSeq", "is not", null)
|
|
90
|
+
.orderBy("chainSeq", "desc")
|
|
91
|
+
.limit(1),
|
|
92
|
+
tenant,
|
|
93
|
+
).executeTakeFirst();
|
|
94
|
+
return linkOf(row);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A selected link, or null. The type check is not decoration: SQLite reads an unknown double-quoted identifier as a
|
|
99
|
+
* string literal, so on a database without `audit_0005_chain` the column comes back as the text `chain_seq`.
|
|
100
|
+
*/
|
|
101
|
+
function linkOf(row: { chainSeq: unknown; chainHash: unknown } | undefined): ChainLink | null {
|
|
102
|
+
return typeof row?.chainSeq === "number" && typeof row.chainHash === "string"
|
|
103
|
+
? { seq: row.chainSeq, hash: row.chainHash }
|
|
104
|
+
: null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The link at one position, or null when no event holds it. */
|
|
108
|
+
export async function readChainLink(db: AuditDatabase, tenant: string | null, seq: number): Promise<ChainLink | null> {
|
|
109
|
+
const row = await whereChain(
|
|
110
|
+
db.selectFrom("pithyAuditEvents").select(["chainSeq", "chainHash"]).where("chainSeq", "=", seq).limit(1),
|
|
111
|
+
tenant,
|
|
112
|
+
).executeTakeFirst();
|
|
113
|
+
return linkOf(row);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A page of one chain in chain order — by position, then id — never past `throughSeq`. It resumes after
|
|
118
|
+
* `afterSeq`, or, given `afterId`, after that one row: a page that resumed from a bare position would skip a second
|
|
119
|
+
* row holding the position the last page ended on, and that second row is exactly the insert a walk must see.
|
|
120
|
+
*/
|
|
121
|
+
export async function readChainRows(
|
|
122
|
+
db: AuditDatabase,
|
|
123
|
+
options: {
|
|
124
|
+
tenant: string | null;
|
|
125
|
+
afterSeq: number;
|
|
126
|
+
afterId?: number;
|
|
127
|
+
limit: number;
|
|
128
|
+
throughSeq?: number;
|
|
129
|
+
/** Only rows that occurred before this instant, in stored ms: what the retention prune asks for. */
|
|
130
|
+
occurredBefore?: number;
|
|
131
|
+
/** Only rows this condition holds for: the retention prune's rows past their own windows. */
|
|
132
|
+
matching?: Expression<SqlBool>;
|
|
133
|
+
},
|
|
134
|
+
): Promise<ChainRow[]> {
|
|
135
|
+
const { afterSeq, afterId } = options;
|
|
136
|
+
let query = db
|
|
137
|
+
.selectFrom("pithyAuditEvents")
|
|
138
|
+
.select([...CHAIN_COLUMNS])
|
|
139
|
+
.where((eb) =>
|
|
140
|
+
afterId === undefined
|
|
141
|
+
? eb("chainSeq", ">", afterSeq)
|
|
142
|
+
: eb.or([eb("chainSeq", ">", afterSeq), eb.and([eb("chainSeq", "=", afterSeq), eb("id", ">", afterId)])]),
|
|
143
|
+
)
|
|
144
|
+
.orderBy("chainSeq", "asc")
|
|
145
|
+
.orderBy("id", "asc")
|
|
146
|
+
.limit(options.limit);
|
|
147
|
+
if (options.throughSeq !== undefined) query = query.where("chainSeq", "<=", options.throughSeq);
|
|
148
|
+
if (options.occurredBefore !== undefined) query = query.where("occurredAt", "<", options.occurredBefore);
|
|
149
|
+
if (options.matching !== undefined) query = query.where(options.matching);
|
|
150
|
+
return (await whereChain(query, options.tenant).execute()) as ChainRow[];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The hashed fields of a stored row, its personal values committed under its salt. */
|
|
154
|
+
export async function chainFieldsOf(
|
|
155
|
+
row: Omit<ChainRow, "id" | "chainSeq" | "chainPrev" | "chainHash">,
|
|
156
|
+
salt: string,
|
|
157
|
+
): Promise<ChainedEventFields> {
|
|
158
|
+
return {
|
|
159
|
+
eventId: row.eventId,
|
|
160
|
+
occurredAt: row.occurredAt,
|
|
161
|
+
action: row.action,
|
|
162
|
+
outcome: row.outcome,
|
|
163
|
+
severity: row.severity,
|
|
164
|
+
actorType: row.actorType,
|
|
165
|
+
actorId: row.actorId,
|
|
166
|
+
actorKind: row.actorKind,
|
|
167
|
+
sessionId: row.sessionId,
|
|
168
|
+
resourceType: row.resourceType,
|
|
169
|
+
resourceId: row.resourceId,
|
|
170
|
+
requestId: row.requestId,
|
|
171
|
+
tenant: row.tenant,
|
|
172
|
+
project: row.project,
|
|
173
|
+
environment: row.environment,
|
|
174
|
+
worker: row.worker,
|
|
175
|
+
version: row.version,
|
|
176
|
+
ipCommitment: await commitment(salt, row.ip),
|
|
177
|
+
userAgentCommitment: await commitment(salt, row.userAgent),
|
|
178
|
+
metadataCommitment: await commitment(salt, row.metadata),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* One stored row as an export carries it. Its commitments are recomputed from the values it holds now, so a value
|
|
184
|
+
* edited in place changes its commitment and the hash no longer matches. The values and the salt travel only with
|
|
185
|
+
* `detail`; without it the commitments are all a reader gets.
|
|
186
|
+
*/
|
|
187
|
+
export async function chainedEventOf(row: ChainRow, options: { detail: boolean }): Promise<ChainedEvent> {
|
|
188
|
+
const salt = row.chainSalt ?? "";
|
|
189
|
+
const fields = await chainFieldsOf(row, salt);
|
|
190
|
+
return {
|
|
191
|
+
...fields,
|
|
192
|
+
seq: row.chainSeq ?? 0,
|
|
193
|
+
prev: row.chainPrev,
|
|
194
|
+
hash: row.chainHash ?? "",
|
|
195
|
+
...(options.detail ? { salt, ip: row.ip, userAgent: row.userAgent, metadata: row.metadata } : {}),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The stored rows for a set of event ids, raw, in no particular order (#824). What the shipping consumer builds each
|
|
201
|
+
* delivery from: the row as stored now, read at delivery rather than carried on the queue, so no personal value
|
|
202
|
+
* ever sits in a queue message. An id with no row is simply absent from the answer.
|
|
203
|
+
*/
|
|
204
|
+
export async function readRowsByEventId(db: AuditDatabase, eventIds: readonly string[]): Promise<ChainRow[]> {
|
|
205
|
+
if (eventIds.length === 0) return [];
|
|
206
|
+
return (await db
|
|
207
|
+
.selectFrom("pithyAuditEvents")
|
|
208
|
+
.select([...CHAIN_COLUMNS])
|
|
209
|
+
.where("eventId", "in", [...eventIds])
|
|
210
|
+
.execute()) as ChainRow[];
|
|
211
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: FSL-1.1-MIT
|
|
3
|
+
|
|
4
|
+
import type { R2Bucket } from "@cloudflare/workers-types";
|
|
5
|
+
import { AuditTombstone } from "@pithy-sh/core/src/audit/chain";
|
|
6
|
+
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
7
|
+
import { checkpointPrefix } from "./checkpoint";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* **Tombstones: the runs of a chain the retention prune removed, kept beside its checkpoints.**
|
|
11
|
+
*
|
|
12
|
+
* One object per pruned run, under the chain's checkpoint prefix, so the bucket's object-lock rule covers it as it
|
|
13
|
+
* covers the checkpoints. The key carries the run's first position and the write is conditional on the key being
|
|
14
|
+
* new, so a run is tombstoned once and a prune retried after a failed delete finds the tombstone it already wrote.
|
|
15
|
+
*
|
|
16
|
+
* A checkpoint listing skips these keys: a checkpoint's key is a bare count, and a tombstone's is under `tombstones/`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The zero-padded width of a position in a key, so keys list in chain order. */
|
|
20
|
+
const SEQ_WIDTH = 15;
|
|
21
|
+
|
|
22
|
+
/** The key prefix of one chain's tombstones. */
|
|
23
|
+
export function tombstonePrefix(tenant: string | null): string {
|
|
24
|
+
return `${checkpointPrefix(tenant)}tombstones/`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The key of one run's tombstone: its chain's prefix and its first position. */
|
|
28
|
+
export function tombstoneKey(tenant: string | null, fromSeq: number): string {
|
|
29
|
+
return `${tombstonePrefix(tenant)}${String(fromSeq).padStart(SEQ_WIDTH, "0")}.json`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The first position a key names, or null when the key is not a tombstone's. */
|
|
33
|
+
function fromSeqOf(key: string, prefix: string): number | null {
|
|
34
|
+
const match = /^(\d+)\.json$/.exec(key.slice(prefix.length));
|
|
35
|
+
return match ? Number(match[1]) : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A stored tombstone, or null when the object does not parse or names another chain or position than its key. */
|
|
39
|
+
async function parseTombstone(text: string, tenant: string | null, fromSeq: number): Promise<AuditTombstone | null> {
|
|
40
|
+
let body: unknown;
|
|
41
|
+
try {
|
|
42
|
+
body = JSON.parse(text);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const parsed = AuditTombstone.safeParse(body);
|
|
47
|
+
if (!parsed.success || parsed.data.tenant !== tenant || parsed.data.fromSeq !== fromSeq) return null;
|
|
48
|
+
return parsed.data;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Write a run's tombstone, or hand back the one already written at its key. What the prune deletes is the run the
|
|
53
|
+
* stored tombstone names, never the one it meant to write, so a retry cannot delete past what the bucket records.
|
|
54
|
+
*/
|
|
55
|
+
export async function writeTombstone(bucket: R2Bucket, tombstone: AuditTombstone): Promise<AuditTombstone> {
|
|
56
|
+
const key = tombstoneKey(tombstone.tenant, tombstone.fromSeq);
|
|
57
|
+
// `etagDoesNotMatch: "*"` is "only if no object has this key": a tombstone is never replaced.
|
|
58
|
+
const put = await bucket.put(key, JSON.stringify(AuditTombstone.parse(tombstone)), {
|
|
59
|
+
onlyIf: { etagDoesNotMatch: "*" },
|
|
60
|
+
httpMetadata: { contentType: "application/json" },
|
|
61
|
+
});
|
|
62
|
+
if (put) return tombstone;
|
|
63
|
+
const existing = await bucket.get(key);
|
|
64
|
+
const stored = existing ? await parseTombstone(await existing.text(), tombstone.tenant, tombstone.fromSeq) : null;
|
|
65
|
+
if (!stored) {
|
|
66
|
+
throw new InternalError({
|
|
67
|
+
message: "The audit prune found a tombstone it cannot read, and pruned nothing there.",
|
|
68
|
+
action: "Inspect the object in the checkpoint bucket; the prune leaves that run in place until it reads.",
|
|
69
|
+
detail: `tombstone ${key} exists but does not parse as one`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return stored;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Every tombstone of one chain, in chain order. An object that does not parse, or names another chain or position
|
|
77
|
+
* than its key, is skipped: a tombstone bridges a gap only where it says it does.
|
|
78
|
+
*/
|
|
79
|
+
export async function readTombstones(bucket: R2Bucket, tenant: string | null): Promise<AuditTombstone[]> {
|
|
80
|
+
const prefix = tombstonePrefix(tenant);
|
|
81
|
+
const keys: { key: string; fromSeq: number }[] = [];
|
|
82
|
+
let cursor: string | undefined;
|
|
83
|
+
do {
|
|
84
|
+
const page = await bucket.list({ prefix, ...(cursor ? { cursor } : {}) });
|
|
85
|
+
for (const object of page.objects) {
|
|
86
|
+
const fromSeq = fromSeqOf(object.key, prefix);
|
|
87
|
+
if (fromSeq !== null) keys.push({ key: object.key, fromSeq });
|
|
88
|
+
}
|
|
89
|
+
cursor = page.truncated ? page.cursor : undefined;
|
|
90
|
+
} while (cursor);
|
|
91
|
+
const tombstones: AuditTombstone[] = [];
|
|
92
|
+
for (const { key, fromSeq } of keys.sort((a, b) => a.fromSeq - b.fromSeq)) {
|
|
93
|
+
const object = await bucket.get(key);
|
|
94
|
+
if (!object) continue;
|
|
95
|
+
const parsed = await parseTombstone(await object.text(), tenant, fromSeq);
|
|
96
|
+
if (parsed) tombstones.push(parsed);
|
|
97
|
+
}
|
|
98
|
+
return tombstones;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The tombstone whose run ends at `throughSeq`, or null: the link a range starting after a pruned run begins from. */
|
|
102
|
+
export function tombstoneEndingAt(tombstones: readonly AuditTombstone[], throughSeq: number): AuditTombstone | null {
|
|
103
|
+
return tombstones.find((tombstone) => tombstone.throughSeq === throughSeq) ?? null;
|
|
104
|
+
}
|