@substrat-run/kernel 0.212.0 → 0.214.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.
- package/dist/directory-crossing.d.ts +247 -0
- package/dist/directory-crossing.d.ts.map +1 -0
- package/dist/directory-crossing.js +299 -0
- package/dist/directory-crossing.js.map +1 -0
- package/dist/directory-lake.d.ts +30 -0
- package/dist/directory-lake.d.ts.map +1 -1
- package/dist/directory-lake.js +51 -1
- package/dist/directory-lake.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/platform-kinds.d.ts.map +1 -1
- package/dist/platform-kinds.js +5 -3
- package/dist/platform-kinds.js.map +1 -1
- package/dist/platform-sweep.d.ts.map +1 -1
- package/dist/platform-sweep.js +9 -16
- package/dist/platform-sweep.js.map +1 -1
- package/dist/provision-reconcile.d.ts +36 -0
- package/dist/provision-reconcile.d.ts.map +1 -0
- package/dist/provision-reconcile.js +80 -0
- package/dist/provision-reconcile.js.map +1 -0
- package/dist/scope-host.d.ts +10 -1
- package/dist/scope-host.d.ts.map +1 -1
- package/dist/scope-host.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A change that crosses two directory objects commits its intent first (K-68, directory split;
|
|
3
|
+
* rfc/tenant-scale.md §7.3). K-64's rule — nothing crosses a Durable Object's boundary inside a
|
|
4
|
+
* transaction — applied to the platform's own objects.
|
|
5
|
+
*
|
|
6
|
+
* Two tables, each in one object:
|
|
7
|
+
*
|
|
8
|
+
* - `_substrat_directory_crossings`, in the object that DECIDES (a tenant's directory object, or the
|
|
9
|
+
* registry for a change it decides itself): one row per crossing, written in the same transaction
|
|
10
|
+
* as the decision and its admin row. Its id is also the claim's id, so every later step is keyed by
|
|
11
|
+
* it and a retry repeats a step rather than doubling it.
|
|
12
|
+
* - `_substrat_directory_claims`, in the registry: one row per key that must be unique across every
|
|
13
|
+
* tenant (a tenant id, a tenant slug, a hostname). A claim is `leased` until its crossing confirms
|
|
14
|
+
* it, `confirmed` after, and `tombstone` for a key that may never be claimed again (a reaped
|
|
15
|
+
* tenant's id).
|
|
16
|
+
*
|
|
17
|
+
* The steps, each its own transaction in its own object:
|
|
18
|
+
*
|
|
19
|
+
* 1. the deciding object records the crossing (`intent`);
|
|
20
|
+
* 2. host code asks the registry to CLAIM the key with the crossing's id and a lease;
|
|
21
|
+
* 3. the deciding object marks the crossing `claimed`;
|
|
22
|
+
* 4. host code asks the registry to CONFIRM the claim, which refuses a lease that ran out or a claim
|
|
23
|
+
* another crossing superseded, and turns a live one permanent;
|
|
24
|
+
* 5. the deciding object settles the crossing `done` (or `failed`, with the refusal).
|
|
25
|
+
*
|
|
26
|
+
* A crash between any two leaves a crossing in `intent` or `claimed`, and `runCrossing` resumes it
|
|
27
|
+
* from there: a repeated claim with the same id returns the same claim, a repeated confirm of a
|
|
28
|
+
* confirmed claim answers ok, and a settle is a compare-and-set from an open state. Nothing is half
|
|
29
|
+
* done with nobody owning it: the open crossing is the owner, and the safety net finds it by
|
|
30
|
+
* `openCrossingsIn`. A claim whose crossing never comes back is superseded by the next claimant once
|
|
31
|
+
* its lease has run out.
|
|
32
|
+
*
|
|
33
|
+
* `adapter-sqlite` keeps ONE database (rfc §11): both tables live in it, and `runCrossingSync` runs
|
|
34
|
+
* the same steps inside one transaction. The contract suite holds the end state on both adapters;
|
|
35
|
+
* the Cloudflare adapter's own suite crashes between the halves.
|
|
36
|
+
*
|
|
37
|
+
* Step 3a of the migration order (rfc §12): nothing calls this yet. Hostname bind, tenant create and
|
|
38
|
+
* tenant reap are wired onto it in the steps after.
|
|
39
|
+
*/
|
|
40
|
+
import type { SwitchSql } from './system-switch.js';
|
|
41
|
+
/** Interpolated into the registry's DDL (and a one-database directory's). */
|
|
42
|
+
export declare const DIRECTORY_CLAIMS_DDL = "\n CREATE TABLE IF NOT EXISTS _substrat_directory_claims (\n namespace TEXT NOT NULL,\n claim_key TEXT NOT NULL,\n claim_id TEXT NOT NULL,\n holder TEXT NOT NULL,\n scope_id TEXT,\n jurisdiction TEXT,\n state TEXT NOT NULL,\n lease_until TEXT,\n claimed_at TEXT NOT NULL,\n confirmed_at TEXT,\n PRIMARY KEY (namespace, claim_key)\n );\n CREATE INDEX IF NOT EXISTS _substrat_directory_claims_holder ON _substrat_directory_claims (holder, namespace);\n";
|
|
43
|
+
/** Interpolated into every deciding object's DDL: each tenant directory object, the registry. */
|
|
44
|
+
export declare const DIRECTORY_CROSSINGS_DDL = "\n CREATE TABLE IF NOT EXISTS _substrat_directory_crossings (\n crossing_id TEXT PRIMARY KEY,\n owner TEXT NOT NULL,\n kind TEXT NOT NULL,\n namespace TEXT NOT NULL,\n claim_key TEXT NOT NULL,\n holder TEXT NOT NULL,\n scope_id TEXT,\n jurisdiction TEXT,\n payload TEXT NOT NULL,\n lease_ms INTEGER NOT NULL,\n state TEXT NOT NULL,\n outcome TEXT,\n attempts INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n );\n CREATE INDEX IF NOT EXISTS _substrat_directory_crossings_open\n ON _substrat_directory_crossings (owner, updated_at) WHERE state IN ('intent', 'claimed');\n";
|
|
45
|
+
/** What must be unique across every tenant, and so is claimed in the registry. */
|
|
46
|
+
export declare const CLAIM_NAMESPACES: readonly ['tenant-id', 'tenant-slug', 'hostname'];
|
|
47
|
+
export type ClaimNamespace = (typeof CLAIM_NAMESPACES)[number];
|
|
48
|
+
/** The registry's owner name, and a tenant directory object's (the `owner` column, rfc §7.1). */
|
|
49
|
+
export declare const REGISTRY_OWNER = "registry";
|
|
50
|
+
export declare const tenantOwnerOf: (tenantId: string) => string;
|
|
51
|
+
/** The default lease on a claim: long enough for host code to make two calls, short enough that a
|
|
52
|
+
* crossing whose caller died does not hold a hostname for long. */
|
|
53
|
+
export declare const DEFAULT_CLAIM_LEASE_MS: number;
|
|
54
|
+
export type ClaimState = 'leased' | 'confirmed' | 'tombstone';
|
|
55
|
+
export interface ClaimRow {
|
|
56
|
+
namespace: ClaimNamespace;
|
|
57
|
+
claimKey: string;
|
|
58
|
+
claimId: string;
|
|
59
|
+
holder: string;
|
|
60
|
+
scopeId: string | null;
|
|
61
|
+
jurisdiction: string | null;
|
|
62
|
+
state: ClaimState;
|
|
63
|
+
leaseUntil: string | null;
|
|
64
|
+
claimedAt: string;
|
|
65
|
+
confirmedAt: string | null;
|
|
66
|
+
}
|
|
67
|
+
export interface ClaimInput {
|
|
68
|
+
namespace: ClaimNamespace;
|
|
69
|
+
key: string;
|
|
70
|
+
/** The crossing's id. A repeated claim with the same id is the same claim. */
|
|
71
|
+
claimId: string;
|
|
72
|
+
/** The tenant the key is claimed for. */
|
|
73
|
+
holder: string;
|
|
74
|
+
scopeId?: string | null;
|
|
75
|
+
/** On a `tenant-id` claim: the jurisdiction of the tenant's directory object (K-7), fixed here. */
|
|
76
|
+
jurisdiction?: string | null;
|
|
77
|
+
leaseMs: number;
|
|
78
|
+
}
|
|
79
|
+
export type ClaimOutcome = {
|
|
80
|
+
ok: true;
|
|
81
|
+
claim: ClaimRow;
|
|
82
|
+
} | {
|
|
83
|
+
ok: false;
|
|
84
|
+
reason: 'held';
|
|
85
|
+
holder: string;
|
|
86
|
+
} | {
|
|
87
|
+
ok: false;
|
|
88
|
+
reason: 'tombstone';
|
|
89
|
+
holder: string;
|
|
90
|
+
};
|
|
91
|
+
export interface ConfirmInput {
|
|
92
|
+
namespace: ClaimNamespace;
|
|
93
|
+
key: string;
|
|
94
|
+
claimId: string;
|
|
95
|
+
}
|
|
96
|
+
export type ConfirmOutcome = {
|
|
97
|
+
ok: true;
|
|
98
|
+
claim: ClaimRow;
|
|
99
|
+
} | {
|
|
100
|
+
ok: false;
|
|
101
|
+
reason: 'expired' | 'superseded' | 'missing';
|
|
102
|
+
};
|
|
103
|
+
export interface ReleaseInput {
|
|
104
|
+
namespace: ClaimNamespace;
|
|
105
|
+
key: string;
|
|
106
|
+
claimId: string;
|
|
107
|
+
/** Keep the key held for good (a reaped tenant's id is never reused, rfc §7.3). */
|
|
108
|
+
tombstone?: boolean;
|
|
109
|
+
}
|
|
110
|
+
export type CrossingState = 'intent' | 'claimed' | 'done' | 'failed';
|
|
111
|
+
export interface CrossingRow {
|
|
112
|
+
crossingId: string;
|
|
113
|
+
owner: string;
|
|
114
|
+
kind: string;
|
|
115
|
+
namespace: ClaimNamespace;
|
|
116
|
+
claimKey: string;
|
|
117
|
+
holder: string;
|
|
118
|
+
scopeId: string | null;
|
|
119
|
+
jurisdiction: string | null;
|
|
120
|
+
payload: unknown;
|
|
121
|
+
leaseMs: number;
|
|
122
|
+
state: CrossingState;
|
|
123
|
+
outcome: string | null;
|
|
124
|
+
attempts: number;
|
|
125
|
+
createdAt: string;
|
|
126
|
+
updatedAt: string;
|
|
127
|
+
}
|
|
128
|
+
export interface CrossingInput {
|
|
129
|
+
crossingId: string;
|
|
130
|
+
kind: string;
|
|
131
|
+
namespace: ClaimNamespace;
|
|
132
|
+
key: string;
|
|
133
|
+
holder: string;
|
|
134
|
+
scopeId?: string | null;
|
|
135
|
+
jurisdiction?: string | null;
|
|
136
|
+
/** What the second half needs, as JSON. */
|
|
137
|
+
payload?: unknown;
|
|
138
|
+
leaseMs?: number;
|
|
139
|
+
}
|
|
140
|
+
export interface SettleInput {
|
|
141
|
+
state: 'done' | 'failed';
|
|
142
|
+
outcome?: string | null;
|
|
143
|
+
}
|
|
144
|
+
export declare function readClaimIn(sql: SwitchSql, namespace: ClaimNamespace, key: string): ClaimRow | null;
|
|
145
|
+
/**
|
|
146
|
+
* Claim `key` for `holder`, keyed by the crossing's id. Refuses a key another crossing holds
|
|
147
|
+
* (confirmed, or leased and still live) and a tombstone. A repeated claim with the same id answers
|
|
148
|
+
* the same claim (a lease that ran out meanwhile is leased afresh, since nobody superseded it).
|
|
149
|
+
* Run it inside the registry's transaction.
|
|
150
|
+
*/
|
|
151
|
+
export declare function claimIn(sql: SwitchSql, input: ClaimInput & {
|
|
152
|
+
nowMs: number;
|
|
153
|
+
}): ClaimOutcome;
|
|
154
|
+
/**
|
|
155
|
+
* Turn a live claim permanent. Refuses one another crossing superseded (or a tombstone), one never
|
|
156
|
+
* made, and one whose lease ran out — which it also deletes, so its key is free at once rather than
|
|
157
|
+
* held until the next claimant supersedes it. Confirming a confirmed claim answers ok.
|
|
158
|
+
*/
|
|
159
|
+
export declare function confirmClaimIn(sql: SwitchSql, input: ConfirmInput & {
|
|
160
|
+
nowMs: number;
|
|
161
|
+
}): ConfirmOutcome;
|
|
162
|
+
/** Release a claim this crossing holds, or keep it as a tombstone. A claim another crossing holds is left alone. */
|
|
163
|
+
export declare function releaseClaimIn(sql: SwitchSql, input: ReleaseInput): {
|
|
164
|
+
released: boolean;
|
|
165
|
+
};
|
|
166
|
+
export declare function crossingIn(sql: SwitchSql, owner: string, crossingId: string): CrossingRow | null;
|
|
167
|
+
/**
|
|
168
|
+
* Record a crossing's intent. Call it inside the deciding object's transaction, beside the decision
|
|
169
|
+
* and its admin row. Recording the same id again returns the row already there (and refuses one
|
|
170
|
+
* that names a different key), so a caller retrying its own request does not start a second.
|
|
171
|
+
*/
|
|
172
|
+
export declare function recordCrossingIn(sql: SwitchSql, owner: string, input: CrossingInput & {
|
|
173
|
+
nowMs: number;
|
|
174
|
+
}): CrossingRow;
|
|
175
|
+
/** `intent` → `claimed`, once the registry answered the claim. False when it had already moved. */
|
|
176
|
+
export declare function markCrossingClaimedIn(sql: SwitchSql, owner: string, crossingId: string, nowMs: number): boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Settle an open crossing `done` or `failed`. A compare-and-set from `intent` or `claimed`: a
|
|
179
|
+
* second settle (two runners racing) answers false and changes nothing. In steps 3b on, the
|
|
180
|
+
* deciding object applies the crossing's effect in this same transaction.
|
|
181
|
+
*/
|
|
182
|
+
export declare function settleCrossingIn(sql: SwitchSql, owner: string, crossingId: string, settle: SettleInput & {
|
|
183
|
+
nowMs: number;
|
|
184
|
+
}): boolean;
|
|
185
|
+
/** Open crossings last touched before `updatedBefore`: what the safety net resumes (rfc §7.3). */
|
|
186
|
+
export declare function openCrossingsIn(sql: SwitchSql, owner: string, query: {
|
|
187
|
+
updatedBefore: string;
|
|
188
|
+
limit: number;
|
|
189
|
+
}): CrossingRow[];
|
|
190
|
+
/** The registry's half, as host code reaches it (an RPC stub on Cloudflare, the one database on SQLite). */
|
|
191
|
+
export interface DirectoryClaimsPort {
|
|
192
|
+
claim(input: ClaimInput): Promise<ClaimOutcome>;
|
|
193
|
+
confirmClaim(input: ConfirmInput): Promise<ConfirmOutcome>;
|
|
194
|
+
releaseClaim(input: ReleaseInput): Promise<{
|
|
195
|
+
released: boolean;
|
|
196
|
+
}>;
|
|
197
|
+
readClaim(namespace: ClaimNamespace, key: string): Promise<ClaimRow | null>;
|
|
198
|
+
}
|
|
199
|
+
/** A deciding object's ledger, as host code reaches it. */
|
|
200
|
+
export interface CrossingLedgerPort {
|
|
201
|
+
/** `registry`, or `tenant:<id>`. */
|
|
202
|
+
readonly owner: string;
|
|
203
|
+
recordCrossing(input: CrossingInput): Promise<CrossingRow>;
|
|
204
|
+
crossing(crossingId: string): Promise<CrossingRow | null>;
|
|
205
|
+
markCrossingClaimed(crossingId: string): Promise<boolean>;
|
|
206
|
+
settleCrossing(crossingId: string, settle: SettleInput): Promise<boolean>;
|
|
207
|
+
openCrossings(query: {
|
|
208
|
+
updatedBefore: string;
|
|
209
|
+
limit: number;
|
|
210
|
+
}): Promise<CrossingRow[]>;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The seam the contract suites are written against (rfc §11): the directory's objects as host code
|
|
214
|
+
* reaches them. The Cloudflare adapter answers with a registry object and one directory object per
|
|
215
|
+
* tenant; `adapter-sqlite` with one database behind every port.
|
|
216
|
+
*/
|
|
217
|
+
export interface DirectoryObjects {
|
|
218
|
+
registry(): DirectoryClaimsPort & CrossingLedgerPort;
|
|
219
|
+
tenant(tenantId: string): CrossingLedgerPort;
|
|
220
|
+
/**
|
|
221
|
+
* Drive a crossing recorded in `ledger`'s object to its end, and answer its row. On Cloudflare,
|
|
222
|
+
* `runCrossing` across the two objects; on one database, `runCrossingSync` in one transaction.
|
|
223
|
+
*/
|
|
224
|
+
complete(ledger: CrossingLedgerPort, crossingId: string): Promise<CrossingRow>;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Drive one crossing from wherever it stopped to `done` or `failed`. Every step is its own
|
|
228
|
+
* transaction in its own object, so this is host code, never called inside a transaction. Safe to
|
|
229
|
+
* run again after a crash at any point, and safe to run twice at once: each step is keyed by the
|
|
230
|
+
* crossing's id and each move is a compare-and-set.
|
|
231
|
+
*/
|
|
232
|
+
export declare function runCrossing(ledger: CrossingLedgerPort, claims: DirectoryClaimsPort, crossingId: string): Promise<CrossingRow>;
|
|
233
|
+
/**
|
|
234
|
+
* The same steps over ONE database, for a directory whose registry and tenant rows share it
|
|
235
|
+
* (`adapter-sqlite`, rfc §11). The caller runs it inside one transaction, so a crossing there never
|
|
236
|
+
* has an intermediate state anybody can read.
|
|
237
|
+
*/
|
|
238
|
+
export declare function runCrossingSync(sql: SwitchSql, owner: string, crossingId: string, nowMs: number): CrossingRow;
|
|
239
|
+
/**
|
|
240
|
+
* The seam over one database: every port reads and writes the same `sql`, and `complete` runs the
|
|
241
|
+
* whole crossing in one `transaction`. `now` is the host's clock (an injected one in a test).
|
|
242
|
+
*/
|
|
243
|
+
export declare function oneDatabaseDirectoryObjects(sql: SwitchSql, options: {
|
|
244
|
+
now: () => number;
|
|
245
|
+
transaction: <T>(fn: () => T) => T;
|
|
246
|
+
}): DirectoryObjects;
|
|
247
|
+
//# sourceMappingURL=directory-crossing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directory-crossing.d.ts","sourceRoot":"","sources":["../src/directory-crossing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;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A2B,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE;IAAE,GAAG,EAAE,MAAM,MAAM,CAAC;IAAC,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;CAAE,GAAG,gBAAgB,CAsBhJ"}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/** Interpolated into the registry's DDL (and a one-database directory's). */
|
|
2
|
+
export const DIRECTORY_CLAIMS_DDL = `
|
|
3
|
+
CREATE TABLE IF NOT EXISTS _substrat_directory_claims (
|
|
4
|
+
namespace TEXT NOT NULL,
|
|
5
|
+
claim_key TEXT NOT NULL,
|
|
6
|
+
claim_id TEXT NOT NULL,
|
|
7
|
+
holder TEXT NOT NULL,
|
|
8
|
+
scope_id TEXT,
|
|
9
|
+
jurisdiction TEXT,
|
|
10
|
+
state TEXT NOT NULL,
|
|
11
|
+
lease_until TEXT,
|
|
12
|
+
claimed_at TEXT NOT NULL,
|
|
13
|
+
confirmed_at TEXT,
|
|
14
|
+
PRIMARY KEY (namespace, claim_key)
|
|
15
|
+
);
|
|
16
|
+
CREATE INDEX IF NOT EXISTS _substrat_directory_claims_holder ON _substrat_directory_claims (holder, namespace);
|
|
17
|
+
`;
|
|
18
|
+
/** Interpolated into every deciding object's DDL: each tenant directory object, the registry. */
|
|
19
|
+
export const DIRECTORY_CROSSINGS_DDL = `
|
|
20
|
+
CREATE TABLE IF NOT EXISTS _substrat_directory_crossings (
|
|
21
|
+
crossing_id TEXT PRIMARY KEY,
|
|
22
|
+
owner TEXT NOT NULL,
|
|
23
|
+
kind TEXT NOT NULL,
|
|
24
|
+
namespace TEXT NOT NULL,
|
|
25
|
+
claim_key TEXT NOT NULL,
|
|
26
|
+
holder TEXT NOT NULL,
|
|
27
|
+
scope_id TEXT,
|
|
28
|
+
jurisdiction TEXT,
|
|
29
|
+
payload TEXT NOT NULL,
|
|
30
|
+
lease_ms INTEGER NOT NULL,
|
|
31
|
+
state TEXT NOT NULL,
|
|
32
|
+
outcome TEXT,
|
|
33
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
34
|
+
created_at TEXT NOT NULL,
|
|
35
|
+
updated_at TEXT NOT NULL
|
|
36
|
+
);
|
|
37
|
+
CREATE INDEX IF NOT EXISTS _substrat_directory_crossings_open
|
|
38
|
+
ON _substrat_directory_crossings (owner, updated_at) WHERE state IN ('intent', 'claimed');
|
|
39
|
+
`;
|
|
40
|
+
/** What must be unique across every tenant, and so is claimed in the registry. */
|
|
41
|
+
export const CLAIM_NAMESPACES = ['tenant-id', 'tenant-slug', 'hostname'];
|
|
42
|
+
/** The registry's owner name, and a tenant directory object's (the `owner` column, rfc §7.1). */
|
|
43
|
+
export const REGISTRY_OWNER = 'registry';
|
|
44
|
+
export const tenantOwnerOf = (tenantId) => `tenant:${tenantId}`;
|
|
45
|
+
/** The default lease on a claim: long enough for host code to make two calls, short enough that a
|
|
46
|
+
* crossing whose caller died does not hold a hostname for long. */
|
|
47
|
+
export const DEFAULT_CLAIM_LEASE_MS = 5 * 60 * 1000;
|
|
48
|
+
const iso = (ms) => new Date(ms).toISOString();
|
|
49
|
+
function assertNamespace(namespace) {
|
|
50
|
+
if (!CLAIM_NAMESPACES.includes(namespace)) {
|
|
51
|
+
throw new Error(`not a directory claim namespace: ${JSON.stringify(namespace)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function claimOf(r) {
|
|
55
|
+
return {
|
|
56
|
+
namespace: r.namespace,
|
|
57
|
+
claimKey: r.claim_key,
|
|
58
|
+
claimId: r.claim_id,
|
|
59
|
+
holder: r.holder,
|
|
60
|
+
scopeId: r.scope_id ?? null,
|
|
61
|
+
jurisdiction: r.jurisdiction ?? null,
|
|
62
|
+
state: r.state,
|
|
63
|
+
leaseUntil: r.lease_until ?? null,
|
|
64
|
+
claimedAt: r.claimed_at,
|
|
65
|
+
confirmedAt: r.confirmed_at ?? null,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const CLAIM_COLUMNS = 'namespace, claim_key, claim_id, holder, scope_id, jurisdiction, state, lease_until, claimed_at, confirmed_at';
|
|
69
|
+
export function readClaimIn(sql, namespace, key) {
|
|
70
|
+
const [row] = sql.all(`SELECT ${CLAIM_COLUMNS} FROM _substrat_directory_claims WHERE namespace = ? AND claim_key = ?`, namespace, key);
|
|
71
|
+
return row ? claimOf(row) : null;
|
|
72
|
+
}
|
|
73
|
+
/** A leased claim whose lease has run out is nobody's: the next claimant supersedes it. */
|
|
74
|
+
const leaseLive = (claim, nowMs) => claim.state !== 'leased' || (claim.leaseUntil !== null && claim.leaseUntil > iso(nowMs));
|
|
75
|
+
/**
|
|
76
|
+
* Claim `key` for `holder`, keyed by the crossing's id. Refuses a key another crossing holds
|
|
77
|
+
* (confirmed, or leased and still live) and a tombstone. A repeated claim with the same id answers
|
|
78
|
+
* the same claim (a lease that ran out meanwhile is leased afresh, since nobody superseded it).
|
|
79
|
+
* Run it inside the registry's transaction.
|
|
80
|
+
*/
|
|
81
|
+
export function claimIn(sql, input) {
|
|
82
|
+
assertNamespace(input.namespace);
|
|
83
|
+
if (!Number.isFinite(input.leaseMs) || input.leaseMs < 0)
|
|
84
|
+
throw new Error(`not a lease: ${input.leaseMs}`);
|
|
85
|
+
const now = iso(input.nowMs);
|
|
86
|
+
const leaseUntil = iso(input.nowMs + input.leaseMs);
|
|
87
|
+
const existing = readClaimIn(sql, input.namespace, input.key);
|
|
88
|
+
if (existing) {
|
|
89
|
+
if (existing.state === 'tombstone')
|
|
90
|
+
return { ok: false, reason: 'tombstone', holder: existing.holder };
|
|
91
|
+
if (existing.claimId === input.claimId) {
|
|
92
|
+
if (leaseLive(existing, input.nowMs))
|
|
93
|
+
return { ok: true, claim: existing };
|
|
94
|
+
sql.run(`UPDATE _substrat_directory_claims SET lease_until = ?, claimed_at = ? WHERE namespace = ? AND claim_key = ? AND claim_id = ?`, leaseUntil, now, input.namespace, input.key, input.claimId);
|
|
95
|
+
return { ok: true, claim: readClaimIn(sql, input.namespace, input.key) };
|
|
96
|
+
}
|
|
97
|
+
if (leaseLive(existing, input.nowMs))
|
|
98
|
+
return { ok: false, reason: 'held', holder: existing.holder };
|
|
99
|
+
// An expired lease of another crossing: superseded. Its confirm will refuse.
|
|
100
|
+
sql.run(`DELETE FROM _substrat_directory_claims WHERE namespace = ? AND claim_key = ?`, input.namespace, input.key);
|
|
101
|
+
}
|
|
102
|
+
sql.run(`INSERT INTO _substrat_directory_claims (${CLAIM_COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, 'leased', ?, ?, NULL)`, input.namespace, input.key, input.claimId, input.holder, input.scopeId ?? null, input.jurisdiction ?? null, leaseUntil, now);
|
|
103
|
+
return { ok: true, claim: readClaimIn(sql, input.namespace, input.key) };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Turn a live claim permanent. Refuses one another crossing superseded (or a tombstone), one never
|
|
107
|
+
* made, and one whose lease ran out — which it also deletes, so its key is free at once rather than
|
|
108
|
+
* held until the next claimant supersedes it. Confirming a confirmed claim answers ok.
|
|
109
|
+
*/
|
|
110
|
+
export function confirmClaimIn(sql, input) {
|
|
111
|
+
assertNamespace(input.namespace);
|
|
112
|
+
const existing = readClaimIn(sql, input.namespace, input.key);
|
|
113
|
+
if (!existing)
|
|
114
|
+
return { ok: false, reason: 'missing' };
|
|
115
|
+
if (existing.claimId !== input.claimId || existing.state === 'tombstone')
|
|
116
|
+
return { ok: false, reason: 'superseded' };
|
|
117
|
+
if (existing.state === 'confirmed')
|
|
118
|
+
return { ok: true, claim: existing };
|
|
119
|
+
if (!leaseLive(existing, input.nowMs)) {
|
|
120
|
+
sql.run(`DELETE FROM _substrat_directory_claims WHERE namespace = ? AND claim_key = ? AND claim_id = ?`, input.namespace, input.key, input.claimId);
|
|
121
|
+
return { ok: false, reason: 'expired' };
|
|
122
|
+
}
|
|
123
|
+
sql.run(`UPDATE _substrat_directory_claims SET state = 'confirmed', lease_until = NULL, confirmed_at = ?
|
|
124
|
+
WHERE namespace = ? AND claim_key = ? AND claim_id = ?`, iso(input.nowMs), input.namespace, input.key, input.claimId);
|
|
125
|
+
return { ok: true, claim: readClaimIn(sql, input.namespace, input.key) };
|
|
126
|
+
}
|
|
127
|
+
/** Release a claim this crossing holds, or keep it as a tombstone. A claim another crossing holds is left alone. */
|
|
128
|
+
export function releaseClaimIn(sql, input) {
|
|
129
|
+
assertNamespace(input.namespace);
|
|
130
|
+
const existing = readClaimIn(sql, input.namespace, input.key);
|
|
131
|
+
if (!existing || existing.claimId !== input.claimId)
|
|
132
|
+
return { released: false };
|
|
133
|
+
if (input.tombstone) {
|
|
134
|
+
sql.run(`UPDATE _substrat_directory_claims SET state = 'tombstone', lease_until = NULL WHERE namespace = ? AND claim_key = ? AND claim_id = ?`, input.namespace, input.key, input.claimId);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
sql.run(`DELETE FROM _substrat_directory_claims WHERE namespace = ? AND claim_key = ? AND claim_id = ?`, input.namespace, input.key, input.claimId);
|
|
138
|
+
}
|
|
139
|
+
return { released: true };
|
|
140
|
+
}
|
|
141
|
+
const CROSSING_COLUMNS = 'crossing_id, owner, kind, namespace, claim_key, holder, scope_id, jurisdiction, payload, lease_ms, state, outcome, attempts, created_at, updated_at';
|
|
142
|
+
function crossingOf(r) {
|
|
143
|
+
return {
|
|
144
|
+
crossingId: r.crossing_id,
|
|
145
|
+
owner: r.owner,
|
|
146
|
+
kind: r.kind,
|
|
147
|
+
namespace: r.namespace,
|
|
148
|
+
claimKey: r.claim_key,
|
|
149
|
+
holder: r.holder,
|
|
150
|
+
scopeId: r.scope_id ?? null,
|
|
151
|
+
jurisdiction: r.jurisdiction ?? null,
|
|
152
|
+
payload: JSON.parse(r.payload),
|
|
153
|
+
leaseMs: Number(r.lease_ms),
|
|
154
|
+
state: r.state,
|
|
155
|
+
outcome: r.outcome ?? null,
|
|
156
|
+
attempts: Number(r.attempts),
|
|
157
|
+
createdAt: r.created_at,
|
|
158
|
+
updatedAt: r.updated_at,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
export function crossingIn(sql, owner, crossingId) {
|
|
162
|
+
const [row] = sql.all(`SELECT ${CROSSING_COLUMNS} FROM _substrat_directory_crossings WHERE owner = ? AND crossing_id = ?`, owner, crossingId);
|
|
163
|
+
return row ? crossingOf(row) : null;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Record a crossing's intent. Call it inside the deciding object's transaction, beside the decision
|
|
167
|
+
* and its admin row. Recording the same id again returns the row already there (and refuses one
|
|
168
|
+
* that names a different key), so a caller retrying its own request does not start a second.
|
|
169
|
+
*/
|
|
170
|
+
export function recordCrossingIn(sql, owner, input) {
|
|
171
|
+
assertNamespace(input.namespace);
|
|
172
|
+
const existing = crossingIn(sql, owner, input.crossingId);
|
|
173
|
+
if (existing) {
|
|
174
|
+
if (existing.namespace !== input.namespace || existing.claimKey !== input.key || existing.holder !== input.holder) {
|
|
175
|
+
throw new Error(`crossing ${input.crossingId} is already recorded for ${existing.namespace} ${existing.claimKey}`);
|
|
176
|
+
}
|
|
177
|
+
return existing;
|
|
178
|
+
}
|
|
179
|
+
const now = iso(input.nowMs);
|
|
180
|
+
sql.run(`INSERT INTO _substrat_directory_crossings (${CROSSING_COLUMNS})
|
|
181
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'intent', NULL, 0, ?, ?)`, input.crossingId, owner, input.kind, input.namespace, input.key, input.holder, input.scopeId ?? null, input.jurisdiction ?? null, JSON.stringify(input.payload ?? null), input.leaseMs ?? DEFAULT_CLAIM_LEASE_MS, now, now);
|
|
182
|
+
return crossingIn(sql, owner, input.crossingId);
|
|
183
|
+
}
|
|
184
|
+
/** `intent` → `claimed`, once the registry answered the claim. False when it had already moved. */
|
|
185
|
+
export function markCrossingClaimedIn(sql, owner, crossingId, nowMs) {
|
|
186
|
+
const before = crossingIn(sql, owner, crossingId);
|
|
187
|
+
if (!before || before.state !== 'intent')
|
|
188
|
+
return false;
|
|
189
|
+
sql.run(`UPDATE _substrat_directory_crossings SET state = 'claimed', attempts = attempts + 1, updated_at = ?
|
|
190
|
+
WHERE owner = ? AND crossing_id = ? AND state = 'intent'`, iso(nowMs), owner, crossingId);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Settle an open crossing `done` or `failed`. A compare-and-set from `intent` or `claimed`: a
|
|
195
|
+
* second settle (two runners racing) answers false and changes nothing. In steps 3b on, the
|
|
196
|
+
* deciding object applies the crossing's effect in this same transaction.
|
|
197
|
+
*/
|
|
198
|
+
export function settleCrossingIn(sql, owner, crossingId, settle) {
|
|
199
|
+
const before = crossingIn(sql, owner, crossingId);
|
|
200
|
+
if (!before || (before.state !== 'intent' && before.state !== 'claimed'))
|
|
201
|
+
return false;
|
|
202
|
+
sql.run(`UPDATE _substrat_directory_crossings SET state = ?, outcome = ?, updated_at = ?
|
|
203
|
+
WHERE owner = ? AND crossing_id = ? AND state IN ('intent', 'claimed')`, settle.state, settle.outcome ?? null, iso(settle.nowMs), owner, crossingId);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
/** Open crossings last touched before `updatedBefore`: what the safety net resumes (rfc §7.3). */
|
|
207
|
+
export function openCrossingsIn(sql, owner, query) {
|
|
208
|
+
return sql
|
|
209
|
+
.all(`SELECT ${CROSSING_COLUMNS} FROM _substrat_directory_crossings
|
|
210
|
+
WHERE owner = ? AND state IN ('intent', 'claimed') AND updated_at < ?
|
|
211
|
+
ORDER BY updated_at, crossing_id LIMIT ?`, owner, query.updatedBefore, query.limit)
|
|
212
|
+
.map(crossingOf);
|
|
213
|
+
}
|
|
214
|
+
const refusalText = (r) => (r.reason === 'held' ? `held by ${r.holder}` : `tombstone of ${r.holder}`);
|
|
215
|
+
/**
|
|
216
|
+
* Drive one crossing from wherever it stopped to `done` or `failed`. Every step is its own
|
|
217
|
+
* transaction in its own object, so this is host code, never called inside a transaction. Safe to
|
|
218
|
+
* run again after a crash at any point, and safe to run twice at once: each step is keyed by the
|
|
219
|
+
* crossing's id and each move is a compare-and-set.
|
|
220
|
+
*/
|
|
221
|
+
export async function runCrossing(ledger, claims, crossingId) {
|
|
222
|
+
let crossing = await ledger.crossing(crossingId);
|
|
223
|
+
if (!crossing)
|
|
224
|
+
throw new Error(`no crossing ${crossingId} in ${ledger.owner}`);
|
|
225
|
+
if (crossing.state === 'intent') {
|
|
226
|
+
const claimed = await claims.claim({
|
|
227
|
+
namespace: crossing.namespace, key: crossing.claimKey, claimId: crossing.crossingId, holder: crossing.holder,
|
|
228
|
+
scopeId: crossing.scopeId, jurisdiction: crossing.jurisdiction, leaseMs: crossing.leaseMs,
|
|
229
|
+
});
|
|
230
|
+
if (!claimed.ok) {
|
|
231
|
+
await ledger.settleCrossing(crossingId, { state: 'failed', outcome: refusalText(claimed) });
|
|
232
|
+
return (await ledger.crossing(crossingId));
|
|
233
|
+
}
|
|
234
|
+
await ledger.markCrossingClaimed(crossingId);
|
|
235
|
+
crossing = (await ledger.crossing(crossingId));
|
|
236
|
+
}
|
|
237
|
+
if (crossing.state === 'claimed') {
|
|
238
|
+
const confirmed = await claims.confirmClaim({ namespace: crossing.namespace, key: crossing.claimKey, claimId: crossing.crossingId });
|
|
239
|
+
await ledger.settleCrossing(crossingId, confirmed.ok ? { state: 'done' } : { state: 'failed', outcome: confirmed.reason });
|
|
240
|
+
crossing = (await ledger.crossing(crossingId));
|
|
241
|
+
}
|
|
242
|
+
return crossing;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The same steps over ONE database, for a directory whose registry and tenant rows share it
|
|
246
|
+
* (`adapter-sqlite`, rfc §11). The caller runs it inside one transaction, so a crossing there never
|
|
247
|
+
* has an intermediate state anybody can read.
|
|
248
|
+
*/
|
|
249
|
+
export function runCrossingSync(sql, owner, crossingId, nowMs) {
|
|
250
|
+
let crossing = crossingIn(sql, owner, crossingId);
|
|
251
|
+
if (!crossing)
|
|
252
|
+
throw new Error(`no crossing ${crossingId} in ${owner}`);
|
|
253
|
+
if (crossing.state === 'intent') {
|
|
254
|
+
const claimed = claimIn(sql, {
|
|
255
|
+
namespace: crossing.namespace, key: crossing.claimKey, claimId: crossing.crossingId, holder: crossing.holder,
|
|
256
|
+
scopeId: crossing.scopeId, jurisdiction: crossing.jurisdiction, leaseMs: crossing.leaseMs, nowMs,
|
|
257
|
+
});
|
|
258
|
+
if (!claimed.ok) {
|
|
259
|
+
settleCrossingIn(sql, owner, crossingId, { state: 'failed', outcome: refusalText(claimed), nowMs });
|
|
260
|
+
return crossingIn(sql, owner, crossingId);
|
|
261
|
+
}
|
|
262
|
+
markCrossingClaimedIn(sql, owner, crossingId, nowMs);
|
|
263
|
+
crossing = crossingIn(sql, owner, crossingId);
|
|
264
|
+
}
|
|
265
|
+
if (crossing.state === 'claimed') {
|
|
266
|
+
const confirmed = confirmClaimIn(sql, { namespace: crossing.namespace, key: crossing.claimKey, claimId: crossing.crossingId, nowMs });
|
|
267
|
+
settleCrossingIn(sql, owner, crossingId, confirmed.ok ? { state: 'done', nowMs } : { state: 'failed', outcome: confirmed.reason, nowMs });
|
|
268
|
+
crossing = crossingIn(sql, owner, crossingId);
|
|
269
|
+
}
|
|
270
|
+
return crossing;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* The seam over one database: every port reads and writes the same `sql`, and `complete` runs the
|
|
274
|
+
* whole crossing in one `transaction`. `now` is the host's clock (an injected one in a test).
|
|
275
|
+
*/
|
|
276
|
+
export function oneDatabaseDirectoryObjects(sql, options) {
|
|
277
|
+
const { now, transaction } = options;
|
|
278
|
+
const ledger = (owner) => ({
|
|
279
|
+
owner,
|
|
280
|
+
recordCrossing: async (input) => transaction(() => recordCrossingIn(sql, owner, { ...input, nowMs: now() })),
|
|
281
|
+
crossing: async (id) => crossingIn(sql, owner, id),
|
|
282
|
+
markCrossingClaimed: async (id) => transaction(() => markCrossingClaimedIn(sql, owner, id, now())),
|
|
283
|
+
settleCrossing: async (id, settle) => transaction(() => settleCrossingIn(sql, owner, id, { ...settle, nowMs: now() })),
|
|
284
|
+
openCrossings: async (query) => openCrossingsIn(sql, owner, query),
|
|
285
|
+
});
|
|
286
|
+
const registry = {
|
|
287
|
+
...ledger(REGISTRY_OWNER),
|
|
288
|
+
claim: async (input) => transaction(() => claimIn(sql, { ...input, nowMs: now() })),
|
|
289
|
+
confirmClaim: async (input) => transaction(() => confirmClaimIn(sql, { ...input, nowMs: now() })),
|
|
290
|
+
releaseClaim: async (input) => transaction(() => releaseClaimIn(sql, input)),
|
|
291
|
+
readClaim: async (namespace, key) => readClaimIn(sql, namespace, key),
|
|
292
|
+
};
|
|
293
|
+
return {
|
|
294
|
+
registry: () => registry,
|
|
295
|
+
tenant: (tenantId) => ledger(tenantOwnerOf(tenantId)),
|
|
296
|
+
complete: async (port, id) => transaction(() => runCrossingSync(sql, port.owner, id, now())),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
//# sourceMappingURL=directory-crossing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directory-crossing.js","sourceRoot":"","sources":["../src/directory-crossing.ts"],"names":[],"mappings":"AAyCA,6EAA6E;AAC7E,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;CAenC,CAAC;AAEF,iGAAiG;AACjG,MAAM,CAAC,MAAM,uBAAuB,GAAG;;;;;;;;;;;;;;;;;;;;CAoBtC,CAAC;AAEF,kFAAkF;AAClF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,WAAW,EAAE,aAAa,EAAE,UAAU,CAAU,CAAC;AAGlF,iGAAiG;AACjG,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;AACzC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAgB,EAAU,EAAE,CAAC,UAAU,QAAQ,EAAE,CAAC;AAEhF;oEACoE;AACpE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAG,EAAE,GAA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AE,GAAG,EAAE,EAAE,CAAC,CAAC;QAC/G,YAAY,EAAE,KAAK,EAAE,KAAmB,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,cAAc,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1F,SAAS,EAAE,KAAK,EAAE,SAAyB,EAAE,GAAW,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,SAAS,EAAE,GAAG,CAAC;KAC9F,CAAC;IACF,OAAO;QACL,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ;QACxB,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACrD,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;KAC7F,CAAC;AACJ,CAAC"}
|
package/dist/directory-lake.d.ts
CHANGED
|
@@ -269,6 +269,24 @@ export declare function advanceDirectoryLakeBackfill(sql: RedactionSql, dataset:
|
|
|
269
269
|
state: DirectoryLakeCaptureState;
|
|
270
270
|
changed: boolean;
|
|
271
271
|
};
|
|
272
|
+
/** Why a prune of a data set did nothing this time, when it did nothing for a reason. */
|
|
273
|
+
export type DirectoryLakePruneRefusal = 'not-capturing' | 'not-backfilled' | 'queued-before-window';
|
|
274
|
+
/**
|
|
275
|
+
* K-70's window, applied: delete up to `limit` of a data set's rows older than `before`, oldest
|
|
276
|
+
* first, **only rows the lake already holds.** That is provable for a data set, not per row:
|
|
277
|
+
*
|
|
278
|
+
* - it captures, and its backfill reached the end (`backfilledAt`), so every row written before
|
|
279
|
+
* capture began was shipped by the backfill and every row since was queued by its trigger;
|
|
280
|
+
* - and none of its queued rows is older than `before`, so every such row has been confirmed by
|
|
281
|
+
* the sink and acknowledged out of the outbox.
|
|
282
|
+
*
|
|
283
|
+
* Otherwise it deletes nothing and says why. A delete queues nothing (no delete trigger), so the
|
|
284
|
+
* lake keeps every pruned row. Day-keyed data sets compare the day with `before`'s UTC day.
|
|
285
|
+
*/
|
|
286
|
+
export declare function pruneDirectoryLakeWindow(sql: RedactionSql, dataset: DirectoryLakeDataset, before: string, limit: number): {
|
|
287
|
+
pruned: number;
|
|
288
|
+
refused: DirectoryLakePruneRefusal | null;
|
|
289
|
+
};
|
|
272
290
|
/**
|
|
273
291
|
* The switch as the control plane's environment spells it: unset, empty or `off` captures
|
|
274
292
|
* nothing; `all` captures every data set; otherwise a comma list of data set names. A name this
|
|
@@ -288,6 +306,16 @@ export interface DirectoryLakeSweepOptions {
|
|
|
288
306
|
sink: DirectoryLakeSink;
|
|
289
307
|
/** The data sets that capture. Empty switches capture off; queued rows still drain. */
|
|
290
308
|
capture: readonly DirectoryLakeDataset[];
|
|
309
|
+
/**
|
|
310
|
+
* The data sets whose rows older than the window are pruned once the lake holds them
|
|
311
|
+
* (`pruneDirectoryLakeWindow`). Empty, the default, prunes nothing. The caller decides which may
|
|
312
|
+
* be listed: one whose history readers do not read the lake yet must not be.
|
|
313
|
+
*/
|
|
314
|
+
prune?: readonly DirectoryLakeDataset[];
|
|
315
|
+
/** The window, in days; `DIRECTORY_LAKE_WINDOW_DAYS` by default. */
|
|
316
|
+
windowDays?: number;
|
|
317
|
+
/** The pass's clock, for the window's start. */
|
|
318
|
+
nowMs?: number;
|
|
291
319
|
batch?: number;
|
|
292
320
|
batchesPerPass?: number;
|
|
293
321
|
}
|
|
@@ -297,6 +325,8 @@ export interface DirectoryLakeSweepReport {
|
|
|
297
325
|
shipped: number;
|
|
298
326
|
acked: number;
|
|
299
327
|
ref: string | null;
|
|
328
|
+
/** Per data set listed for pruning: rows pruned this pass, or why none were. */
|
|
329
|
+
pruned?: Partial<Record<DirectoryLakeDataset, number | DirectoryLakePruneRefusal>>;
|
|
300
330
|
}
|
|
301
331
|
/**
|
|
302
332
|
* The pass's phase: reconcile the switch, then ship → confirm → delete, batch after batch while
|