@runeditrun/rer 0.2.98 → 0.2.100
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 +1 -1
- package/cli/README.md +88 -2
- package/cli/business/api.mjs +4 -2
- package/cli/business/bounded-waves.d.mts +23 -0
- package/cli/business/bounded-waves.mjs +57 -0
- package/cli/business/catchup-task.mjs +9 -2
- package/cli/business/command.mjs +16 -4
- package/cli/business/connect.mjs +39 -33
- package/cli/business/export-inputs.mjs +12 -2
- package/cli/business/export-task.mjs +9 -2
- package/cli/business/legacy-package.mjs +117 -0
- package/cli/business/local-commands.mjs +10 -1
- package/cli/business/package-transfer.mjs +127 -0
- package/cli/business/questions.mjs +2 -2
- package/cli/business/state.mjs +47 -5
- package/cli/business/supervisor.mjs +46 -7
- package/cli/business/vendor-door.mjs +86 -13
- package/cli/business/vendors/README.md +41 -2
- package/cli/business/vendors/common.mjs +81 -0
- package/cli/business/vendors/index.mjs +12 -2
- package/cli/business/vendors/zendesk-attachments.mjs +57 -0
- package/cli/business/vendors/zendesk-encode.mjs +133 -0
- package/cli/business/vendors/zendesk-guide.mjs +34 -0
- package/cli/business/vendors/zendesk-package.mjs +28 -0
- package/cli/business/vendors/zendesk-pages.mjs +61 -0
- package/cli/business/vendors/zendesk-people.mjs +7 -0
- package/cli/business/vendors/zendesk-store.mjs +154 -0
- package/cli/business/vendors/zendesk-tickets.mjs +21 -0
- package/cli/business/vendors/zendesk-token.mjs +31 -0
- package/cli/business/vendors/zendesk-transport.mjs +48 -0
- package/cli/business/vendors/zendesk.mjs +61 -0
- package/cli/deploy-command.mjs +5 -4
- package/cli/deploy-inputs.mjs +1 -1
- package/cli/deploy.mjs +6 -4
- package/cli/production-auth.mjs +16 -3
- package/cli/production-mail.mjs +18 -1
- package/cli/site.mjs +1 -1
- package/global/CHANGELOG.md +42 -0
- package/global/README.md +1 -1
- package/modules/deploy-kit/README.md +10 -0
- package/modules/deploy-kit/provider.mjs +18 -2
- package/modules/import-kit/src/d1/jobs.ts +3 -1
- package/modules/import-kit/src/d1/retirement-scope.ts +2 -2
- package/modules/import-kit/src/d1/retirement-tally.ts +1 -0
- package/modules/import-kit/src/d1/retirement.ts +15 -5
- package/modules/import-kit/src/d1/types.ts +3 -0
- package/modules/import-kit/src/d1/wave-boundary.ts +33 -0
- package/modules/import-kit/src/d1/wave-retirement.ts +47 -0
- package/modules/import-kit/src/door/ADOPTING.md +37 -0
- package/modules/import-kit/src/door/README.md +66 -0
- package/modules/import-kit/src/door/admission.ts +13 -3
- package/modules/import-kit/src/door/authority.ts +1 -0
- package/modules/import-kit/src/door/failures.ts +1 -1
- package/modules/import-kit/src/door/grants.ts +19 -5
- package/modules/import-kit/src/door/handler.ts +12 -1
- package/modules/import-kit/src/door/index.ts +6 -0
- package/modules/import-kit/src/door/package.ts +121 -0
- package/modules/import-kit/src/door/planning.ts +44 -19
- package/modules/import-kit/src/door/protocol.ts +4 -2
- package/modules/import-kit/src/door/schema.ts +2 -0
- package/modules/import-kit/src/door/store.ts +2 -2
- package/modules/import-kit/src/door/types.ts +24 -1
- package/modules/import-kit/src/door/waves/README.md +158 -0
- package/modules/import-kit/src/door/waves/digests.ts +38 -0
- package/modules/import-kit/src/door/waves/inventory.ts +84 -0
- package/modules/import-kit/src/door/waves/journal.ts +19 -0
- package/modules/import-kit/src/door/waves/lifecycle.ts +242 -0
- package/modules/import-kit/src/door/waves/preparation.ts +190 -0
- package/modules/import-kit/src/door/waves/schema.ts +73 -0
- package/modules/import-kit/src/door/waves/store.ts +112 -0
- package/modules/import-kit/src/door/waves/types.ts +76 -0
- package/onboarding/adapter.mjs +7 -1
- package/onboarding/operator-installer.mjs +3 -1
- package/package.json +1 -1
- package/scripts/publish-check/README.md +20 -0
- package/scripts/publish-check/diagnostics.mjs +73 -0
- package/scripts/publish-check/slots.py +37 -7
- package/scripts/publish-check.mjs +64 -37
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { D1Database, D1Sql, D1TargetAdapter } from '../d1/types.ts';
|
|
2
2
|
import type { D1ReversalPolicy } from '../d1/undo-types.ts';
|
|
3
3
|
import type { ImportMapping, ImportPlan, Json, ParsedRecord, ReaderLimits } from '../types.ts';
|
|
4
|
+
import type { ImportDoorBoundedSource } from './waves/types.ts';
|
|
4
5
|
import type { DoorGrant } from './protocol.ts';
|
|
5
6
|
import type { ImportDoorIntake } from './intake/types.ts';
|
|
6
7
|
export type { ImportDoorIntake, IntakeFormat } from './intake/types.ts';
|
|
@@ -12,8 +13,30 @@ export interface ImportDoorRawUpload {
|
|
|
12
13
|
readonly sha256: string;
|
|
13
14
|
readonly size: number;
|
|
14
15
|
}
|
|
16
|
+
export interface DoorPackageFile { readonly path: string; readonly bytes: number; readonly sha256: string; readonly offset: number }
|
|
17
|
+
export interface DoorPackageManifest {
|
|
18
|
+
format: 'rer-door-package/1'; source: {format: string; [key: string]: Json}; mode: string;
|
|
19
|
+
stream: {bytes: number; sha256: string}; segments: {bytes: number; sha256: string}[];
|
|
20
|
+
index: {path: 'index.ndjson'; bytes: number; sha256: string};
|
|
21
|
+
records: {total: number | null; byKind: Record<string, number>}; totals: Json;
|
|
22
|
+
}
|
|
23
|
+
export interface ImportDoorPackageUpload {
|
|
24
|
+
readonly manifest: Readonly<DoorPackageManifest>;
|
|
25
|
+
readonly files: readonly Readonly<DoorPackageFile>[];
|
|
26
|
+
/** A verified file, bounded by the declaration and the caller's optional tighter limit. */
|
|
27
|
+
read(path: string, maxBytes?: number): Promise<Uint8Array>;
|
|
28
|
+
}
|
|
29
|
+
export interface ImportDoorPackageSource {
|
|
30
|
+
formats: readonly string[];
|
|
31
|
+
maxBytes: number;
|
|
32
|
+
maxSegments: number;
|
|
33
|
+
/** Return the whole logical snapshot, never one plan per segment or record wave. */
|
|
34
|
+
read(upload: ImportDoorPackageUpload, limits: Readonly<Required<ReaderLimits>>): ParsedRecord[] | Promise<ParsedRecord[]>;
|
|
35
|
+
}
|
|
15
36
|
/** One decoded source; existing adapters may keep the mount's format. */
|
|
16
37
|
export interface ImportDoorSource {
|
|
38
|
+
package?: ImportDoorPackageSource;
|
|
39
|
+
bounded?: ImportDoorBoundedSource;
|
|
17
40
|
mapping: ImportMapping;
|
|
18
41
|
parse(value: Json, index: number): ParsedRecord | ParsedRecord[];
|
|
19
42
|
ownerPrincipalId?: string;
|
|
@@ -72,7 +95,7 @@ export type ImportDoorOptions = ImportDoorMountOptions & (
|
|
|
72
95
|
parse?: undefined; mapping?: undefined; readRaw?: undefined;
|
|
73
96
|
});
|
|
74
97
|
export type DoorAccess = Pick<DoorGrant, 'action' | 'installationId' | 'email' | 'namespace'
|
|
75
|
-
| 'maxBytes' | 'maxChunks' | 'maxRecords' | 'fingerprint' | 'approvalRecordId' | 'approvedAt' | 'catchup'>;
|
|
98
|
+
| 'bounded' | 'maxBytes' | 'maxChunks' | 'maxRecords' | 'fingerprint' | 'approvalRecordId' | 'approvedAt' | 'catchup'>;
|
|
76
99
|
export interface ImportOperatorOptions extends ImportDoorRuntimeOptions {
|
|
77
100
|
installationId: string;
|
|
78
101
|
appOrigin: string;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# Opt-in complete-parent imports
|
|
2
|
+
|
|
3
|
+
The bounded-waves protocol shown below is a grant-only source protocol. Ordinary `/plan`, including
|
|
4
|
+
the logical-package path, keeps its existing aggregate refusal and quotas.
|
|
5
|
+
Transport segments never grant execution authority. This source change does not
|
|
6
|
+
adopt the protocol in an edition, raise a grant, approve an aggregate, or authorize
|
|
7
|
+
a real import. An ordinary whole snapshot above its record grant continues to refuse.
|
|
8
|
+
|
|
9
|
+
Apply `importDoorBoundedWavesMigration` in a new numbered edition migration after
|
|
10
|
+
the existing door migrations. The maintained reference includes the additive
|
|
11
|
+
`0042_import_bounded_waves.sql`; it supplies no bounded source or Business tasks.
|
|
12
|
+
Existing jobs, originals, approvals and migration names stay intact. The schema
|
|
13
|
+
adds root, ordered child, hashed membership and approval tables. Source content
|
|
14
|
+
uses the existing door snapshot chunks, and original/attachment bytes use the
|
|
15
|
+
ordinary D1 job custody. Erasure withdraws snapshots and equivalent authorized
|
|
16
|
+
package segments, marks their roots withdrawn, and retires affected root journals
|
|
17
|
+
with the existing stream/reuse/history closure. Peer native rows are retained
|
|
18
|
+
unless the product's erasure statements select them. Withdrawal is not Undo.
|
|
19
|
+
|
|
20
|
+
The edition supplies `ImportDoorBoundedSource` from `types.ts` as `source.bounded`
|
|
21
|
+
alongside its existing `source.package`. This is an actual typed input boundary;
|
|
22
|
+
no HappyToHelp reader, parser, native grouping or Business approval implementation
|
|
23
|
+
is supplied here. Those upstream integrations must exist and be independently
|
|
24
|
+
qualified before adoption. The ordinary whole-array package `read` is not used
|
|
25
|
+
by preparation.
|
|
26
|
+
|
|
27
|
+
`describe(upload)` independently enumerates the complete source and returns one
|
|
28
|
+
finite `WaveCensus`, including source/native counts by kind/table, originals,
|
|
29
|
+
objects, logical/stored/native/blob/context bytes, accounting version, source
|
|
30
|
+
mode and high water, and complete inventory/relationship/baseline digests.
|
|
31
|
+
`page(upload, ordinal, limits)` returns exactly one indivisible `WaveUnit` with
|
|
32
|
+
all its primary identities, file roles and dependencies on earlier units. The
|
|
33
|
+
adapter must include metadata/context/archive/rejected aliases in its reader's
|
|
34
|
+
erasure recognition, and coalesce cycles and publication closures before paging.
|
|
35
|
+
Files, source identities, additional subject aliases and native projection keys each have exactly one owner.
|
|
36
|
+
`aliases` explicitly indexes context, archive, rejected and metadata subjects that
|
|
37
|
+
are not primary source units; their files must be in the verified package. The
|
|
38
|
+
census reports their separate count and its inventory digest includes every
|
|
39
|
+
alias. The server-owned erasure adapter can supply exact
|
|
40
|
+
`D1ImportPerson.aliases: [{installationId, namespace, kind, sourceId}]`, including
|
|
41
|
+
a subject that never became a native target. It persists scoped digest tombstones
|
|
42
|
+
even before preparation. These selectors are never client input. Wave erasure uses the journaled
|
|
43
|
+
four-argument `retireD1ImportCustody` API and the actual restore checkpoint; the
|
|
44
|
+
separate legacy `operationKey` erasure-step interface refuses wave custody with
|
|
45
|
+
`IMPORT_WAVE_JOURNAL_REQUIRED`. The journal API retains its existing 1000-statement
|
|
46
|
+
and 2 MiB RPC bounds.
|
|
47
|
+
An unsupported indivisible unit refuses; no remainder is silently deferred.
|
|
48
|
+
|
|
49
|
+
`native(plan)` enumerates every actual expanded native row, including joins,
|
|
50
|
+
origin/hold rows and native blob chunks, with its stable key, projection digest
|
|
51
|
+
and stored/blob byte sizes. Its totals cannot be smaller than the kit effects.
|
|
52
|
+
It must describe the installed native adapter, including unchanged reused rows;
|
|
53
|
+
an effect/container is not a substitute record unit. The native adapter's
|
|
54
|
+
`read`, exact `matches` and full-set predicates must cover those physical rows,
|
|
55
|
+
references and byte representations. Kit protocol/journal metadata is separate.
|
|
56
|
+
`batchSize` optionally lowers the ordinary engine batch (1..20), without changing
|
|
57
|
+
the independent source and expanded-native execution quotas.
|
|
58
|
+
|
|
59
|
+
`waveInventoryHash({source,native,files})`, exported from the door, defines the
|
|
60
|
+
complete census digest. Source rows supply kind, stable ID and payload hash;
|
|
61
|
+
native rows supply the full `WaveNativeRow`, and `aliases` supplies additional
|
|
62
|
+
`WaveAlias` records; verified files also supply their
|
|
63
|
+
role and logical bytes. `inventoryMember(type, identity, content)` exposes each
|
|
64
|
+
hashed leaf. Sort its hexadecimal digest leaves in ascending ASCII order and
|
|
65
|
+
hash their canonical JSON array. The server computes this from its immutable
|
|
66
|
+
membership index with bounded keyset pages, without building a full inventory
|
|
67
|
+
JSON value. An adapter can do the same in its paged reader or use the convenience
|
|
68
|
+
function for a small complete census.
|
|
69
|
+
|
|
70
|
+
`relationshipsHash` hashes the ordered array of each unit's relationship digest.
|
|
71
|
+
`baselineHash` is the previous complete parent fingerprint, or SHA-256 of
|
|
72
|
+
canonical `[]` for the initial snapshot. Each child separately commits its exact
|
|
73
|
+
native before-images. The sealed parent commits all child fingerprints, census,
|
|
74
|
+
package, destination/claim, migration/revision/policy, mapping and erasure epoch.
|
|
75
|
+
|
|
76
|
+
`authority(binding, action)` MUST return a same-database scalar predicate for the
|
|
77
|
+
CURRENT installation, owner/claim, migration/revision/policy and active exact
|
|
78
|
+
parent/child approvals. It must consult the edition/Business approval rows;
|
|
79
|
+
returning a cached boolean or constant permission defeats the integration.
|
|
80
|
+
The door combines it with current enablement, trust/key revocation, single-use
|
|
81
|
+
nonce, expiry, namespace/destination binding and erasure epoch before and after
|
|
82
|
+
each existing D1 transaction. A root approval has no apply authority.
|
|
83
|
+
|
|
84
|
+
For example, the trusted signer issues fresh grants using these additional
|
|
85
|
+
payload members (the existing issuer/audience/owner/nonce/expiry/quotas remain):
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const bounded = {
|
|
89
|
+
protocol: 'rer-bounded-waves/1', migration: 'migration-17',
|
|
90
|
+
revision: 'revision-2', policy: 'policy-1',
|
|
91
|
+
};
|
|
92
|
+
// prepare / seal-parent / wave-status: { bounded }
|
|
93
|
+
// review-parent:
|
|
94
|
+
const reviewedParent = {
|
|
95
|
+
...bounded, parentFingerprint, parentApprovalRecordId, parentApprovedAt,
|
|
96
|
+
};
|
|
97
|
+
// { bounded: reviewedParent, fingerprint: parentFingerprint,
|
|
98
|
+
// approvalRecordId: parentApprovalRecordId, approvedAt: parentApprovedAt }
|
|
99
|
+
// apply-child:
|
|
100
|
+
// { bounded: {...reviewedParent, childFingerprint}, fingerprint: childFingerprint,
|
|
101
|
+
// approvalRecordId: childApprovalRecordId, approvedAt: childApprovedAt }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The concrete POST bodies are:
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
{"uploadId":"server-upload-id","ordinal":0}
|
|
108
|
+
{"parentId":"server-parent-id","ordinal":1}
|
|
109
|
+
{"parentId":"server-parent-id"}
|
|
110
|
+
{"parentId":"server-parent-id","ordinal":0,"reviewedFingerprint":"<child-sha256>"}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The first two are `/prepare`. The third is `/seal-parent`, `/review-parent`,
|
|
114
|
+
`/wave-status`, `/finalize-parent` or `/reverse-parent` under its own action
|
|
115
|
+
grant. The fourth is `/apply-child`. Before approval, `/prepare` with
|
|
116
|
+
`{"parentId":"server-parent-id","cancel":true}` can discard private custody;
|
|
117
|
+
the previous complete head stays authoritative. A cancelled revision is not
|
|
118
|
+
mutated/reused: review a new revision. All routes remain on `/api/rer/import/`.
|
|
119
|
+
|
|
120
|
+
Child responses say `childSuccess` and always `aggregateSuccess:false`. Missing,
|
|
121
|
+
altered, foreign, unapproved, revoked or out-of-order children refuse. Fresh
|
|
122
|
+
grants resolve lost responses through durable inventory/status, operation keys
|
|
123
|
+
and the existing D1 checkpoints. A child uses an ordinary D1 job, preserving
|
|
124
|
+
attachment/original eligibility, strict before-images, apply and rollback. The
|
|
125
|
+
kit's internal source namespace binds the reviewed snapshot revision, not a
|
|
126
|
+
child/upload/attempt; the logical namespace and migration lineage never split.
|
|
127
|
+
|
|
128
|
+
Finalization requires all exact children, current approvals, readable custody,
|
|
129
|
+
complete inventory totals and exact native/provenance reconciliation. Only that
|
|
130
|
+
transaction advances the complete head and releases gone-but-retained reporting.
|
|
131
|
+
Absence never deletes. `wave-status` reports progress/completion history, not a
|
|
132
|
+
fresh aggregate proof; the sender consumes `/finalize-parent` itself.
|
|
133
|
+
Routine grants marked `catchup:'bounded'` require the global strict under-five-
|
|
134
|
+
percent comparison and all existing gone/conflict/rejected/unmapped exclusions.
|
|
135
|
+
A separately reviewed proposal can describe a complete snapshot with absence;
|
|
136
|
+
its previous targets remain attributed and retained. Catch-up Undo preserves
|
|
137
|
+
unchanged original owners. Reverse checks all known conflicts first, then uses
|
|
138
|
+
ordinary strict Undo or partial-job rollback, descending dependants first. It is
|
|
139
|
+
bounded, resumable reversal, not whole-database atomicity. Reversal reserves the
|
|
140
|
+
same pending-parent slot, so concurrent cumulative preparation cannot race its
|
|
141
|
+
preflight or clear another parent during complete-head restoration.
|
|
142
|
+
|
|
143
|
+
Every request retains the existing plan/value/parser/SQL/transport limits. A
|
|
144
|
+
complete reconciliation or baseline read that cannot fit the actual request
|
|
145
|
+
budget refuses before publication; there is no unchecked paged remainder or
|
|
146
|
+
hosted-capacity claim. Independent qualification must establish the capacity of
|
|
147
|
+
the adopting edition and its exact native adapter. The tiny-grant public proof
|
|
148
|
+
does not establish capacity for a production-sized legacy snapshot.
|
|
149
|
+
|
|
150
|
+
The reusable sender is `cli/business/bounded-waves.mjs`, with its actual typed
|
|
151
|
+
`BoundedArrangement` in `bounded-waves.d.mts`. `rer business run` opts in only
|
|
152
|
+
when Business supplies `task.bounded_waves`; ordinary tasks keep their path.
|
|
153
|
+
Business supplies the claimed destination and fresh action grants, then the
|
|
154
|
+
exact server parent fingerprint and ordered immutable child approvals. The
|
|
155
|
+
sender cannot synthesize them or treat task completion as reconciliation.
|
|
156
|
+
An arrangement without approval prepares/seals and pauses truthfully. Approval
|
|
157
|
+
arrangement, grant minting, edition-native grouping, and real adoption remain
|
|
158
|
+
upstream dependencies, not evidence supplied by this Base implementation.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type { D1Database } from '../../d1/types.ts';
|
|
3
|
+
import { canonicalHash } from '../../provenance/canonical-stream.ts';
|
|
4
|
+
import type { WaveAlias, WaveNativeRow } from './types.ts';
|
|
5
|
+
|
|
6
|
+
export interface WaveInventoryMember {type: string; identity: string; digest: string}
|
|
7
|
+
export const inventoryMember = (type: string, identity: unknown, content: unknown): WaveInventoryMember => ({
|
|
8
|
+
type,identity:canonicalHash(identity),digest:canonicalHash([type,identity,content]),
|
|
9
|
+
});
|
|
10
|
+
export interface WaveInventoryInput {
|
|
11
|
+
source: readonly {kind:string;sourceId:string;payloadHash:string}[];
|
|
12
|
+
native: readonly WaveNativeRow[];
|
|
13
|
+
aliases?: readonly WaveAlias[];
|
|
14
|
+
files: readonly {path:string;sha256:string;bytes:number;role:'source'|'object'|'context';logicalBytes:number}[];
|
|
15
|
+
}
|
|
16
|
+
/** Adapter convenience for a complete independently enumerated census. An
|
|
17
|
+
* edition can equivalently sort these digest leaves in its paged reader/store. */
|
|
18
|
+
export function waveInventoryHash(input: WaveInventoryInput): string {
|
|
19
|
+
return canonicalHash([
|
|
20
|
+
...input.source.map(row=>inventoryMember('source',[row.kind,row.sourceId],row.payloadHash)),
|
|
21
|
+
...(input.aliases ?? []).map(alias=>inventoryMember('alias',[alias.kind,alias.sourceId],alias)),
|
|
22
|
+
...input.native.map(row=>inventoryMember('native',[row.table,row.key],row)),
|
|
23
|
+
...input.files.map(file=>inventoryMember('file',file.path,[file.sha256,file.bytes,file.role,file.logicalBytes])),
|
|
24
|
+
].map(row=>row.digest).sort());
|
|
25
|
+
}
|
|
26
|
+
/** Hash the complete exact inventory without materializing its JSON or all
|
|
27
|
+
* source/native membership rows. The unique identity index seals coverage;
|
|
28
|
+
* the digest index permits bounded keyset reads. */
|
|
29
|
+
export async function storedInventoryHash(db: D1Database, parentId: string): Promise<string> {
|
|
30
|
+
const digest=createHash('sha256');digest.update('[');
|
|
31
|
+
let after='',first=true;
|
|
32
|
+
for (;;) {
|
|
33
|
+
const {results}=await db.prepare('SELECT digest FROM ik_door_wave_members WHERE parent_id=? AND digest>? ORDER BY digest LIMIT 128').bind(parentId,after).all<{digest:string}>();
|
|
34
|
+
for (const row of results) {digest.update((first?'':',')+JSON.stringify(row.digest));first=false;after=row.digest;}
|
|
35
|
+
if(results.length<128)break;
|
|
36
|
+
}
|
|
37
|
+
digest.update(']');return digest.digest('hex');
|
|
38
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { DoorRecord, DoorRequest, DoorSnapshot } from '../types.ts';
|
|
2
|
+
import type { D1Sql } from '../../d1/types.ts';
|
|
3
|
+
import { canonicalJson, attachmentHash, sha256 } from '../../provenance/identity.ts';
|
|
4
|
+
import { importedTargets, recordKey } from '../inventory.ts';
|
|
5
|
+
import { DoorError } from '../protocol.ts';
|
|
6
|
+
import { authorized, sql } from '../store.ts';
|
|
7
|
+
import { childSnapshot, children, hash } from './store.ts';
|
|
8
|
+
import type { WaveRoot } from './types.ts';
|
|
9
|
+
|
|
10
|
+
/** Snapshot pages are read independently. Request budgets are never waived for
|
|
11
|
+
* a large inventory or a native adapter lacking compact exact predicates. */
|
|
12
|
+
export async function records(request: DoorRequest, id: string | null, retained = true): Promise<DoorRecord[]> {
|
|
13
|
+
if (!id) return [];
|
|
14
|
+
const root = await request.options.database.prepare('SELECT * FROM ik_door_wave_roots WHERE id=? AND installation_id=? AND namespace=? AND destination=? AND state=\'completed\'')
|
|
15
|
+
.bind(id,request.access.installationId,request.access.namespace,request.options.destination).first<WaveRoot>();
|
|
16
|
+
if (!root) throw new DoorError('complete_parent_baseline_required',409);
|
|
17
|
+
const result: DoorRecord[] = [];
|
|
18
|
+
for (const child of await children(request,root)) result.push(...(await childSnapshot(request,child)).records);
|
|
19
|
+
// Retained-gone ownership is cumulative, but never counted as delivered source.
|
|
20
|
+
const seen = new Set(result.map(recordKey));
|
|
21
|
+
for (const old of retained ? await records(request,root.previous_id) : []) if (!seen.has(recordKey(old))) result.push(old);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
export async function prior(request: DoorRequest, root: WaveRoot) {
|
|
25
|
+
return new Map((await records(request,root.previous_id)).map(record=>[recordKey(record),record]));
|
|
26
|
+
}
|
|
27
|
+
/** Re-read exact journal ownership and byte-bearing native values. The kit's
|
|
28
|
+
* read fences the complete native set, rather than accepting a client receipt. */
|
|
29
|
+
export async function ownedFence(request: DoorRequest, rows: DoorRecord[]): Promise<D1Sql[]> {
|
|
30
|
+
const targets = rows.flatMap(row=>row.targets);
|
|
31
|
+
const found = await importedTargets(request,targets).catch(async error=>{
|
|
32
|
+
if (error instanceof Error && error.message==='IMPORT_ATOMIC_GUARD') {await authorized(request);throw new DoorError('native_edit_conflict',409);}
|
|
33
|
+
throw error;
|
|
34
|
+
});
|
|
35
|
+
if (found.some(row=>!row.matches)) throw new DoorError('native_edit_conflict',409);
|
|
36
|
+
const predicates: D1Sql[] = [];
|
|
37
|
+
for (let i=0;i<targets.length;i++) {
|
|
38
|
+
const t = targets[i], p = found[i].provenance;
|
|
39
|
+
const current = await request.options.targets.read(request.options.database,request.options.destination,t.kind,t.id);
|
|
40
|
+
if (!current || current.revision !== p.appliedRevision || sha256(canonicalJson(current.value)) !== p.valueHash
|
|
41
|
+
|| attachmentHash(current.attachment) !== p.attachmentHash) throw new DoorError('native_edit_conflict',409);
|
|
42
|
+
predicates.push(request.options.targets.matches(current,request.options.destination,t.kind,t.id),
|
|
43
|
+
sql(`EXISTS(SELECT 1 FROM ik_provenance WHERE id=? AND job_id=? AND reversed=0 AND json_extract(record_json,'$.appliedRevision')=? AND json_extract(record_json,'$.valueHash')=? AND json_extract(record_json,'$.attachmentHash') IS ?)`,p.id,p.jobId,p.appliedRevision,p.valueHash,p.attachmentHash));
|
|
44
|
+
}
|
|
45
|
+
return predicates;
|
|
46
|
+
}
|
|
47
|
+
export function conjunction(predicates: D1Sql[]): D1Sql {
|
|
48
|
+
return predicates.length ? sql(predicates.map(p=>`(${p.sql})`).join(' AND '),...predicates.flatMap(p=>p.params ?? [])) : sql('1');
|
|
49
|
+
}
|
|
50
|
+
/** Before every child batch, future baselines and earlier siblings are checked
|
|
51
|
+
* together. A sibling's actual journal replaces only its reviewed baselines. */
|
|
52
|
+
export async function executionFence(request: DoorRequest, root: WaveRoot, executingOrdinal = -1) {
|
|
53
|
+
const predicates: D1Sql[] = [], completed: DoorRecord[] = [], replacing = new Set<string>();
|
|
54
|
+
for (const child of await children(request,root)) {
|
|
55
|
+
const snapshot = await childSnapshot(request,child);
|
|
56
|
+
for (const record of snapshot.records) replacing.add(recordKey(record));
|
|
57
|
+
const job = child.job_id ? await request.options.database.prepare('SELECT state,checkpoint FROM ik_jobs WHERE id=?').bind(child.job_id).first<any>() : null;
|
|
58
|
+
if (job?.state === 'completed') { if (child.ordinal!==executingOrdinal) completed.push(...snapshot.records); continue; }
|
|
59
|
+
for (const baseline of snapshot.wave.descriptor.baseline) {
|
|
60
|
+
// A partially committed child's own completed targets are fenced by its
|
|
61
|
+
// actual journal below; its uncommitted targets keep the prepared baseline.
|
|
62
|
+
const provenance = child.job_id ? await request.options.database.prepare("SELECT json_extract(record_json,'$.appliedRevision') AS applied_revision,json_extract(record_json,'$.valueHash') AS value_hash,json_extract(record_json,'$.attachmentHash') AS attachment_hash FROM ik_provenance WHERE job_id=? AND target_kind=? AND target_id=? AND reversed=0").bind(child.job_id,baseline.kind,baseline.id).first<any>() : null;
|
|
63
|
+
const current = await request.options.targets.read(request.options.database,root.destination,baseline.kind,baseline.id);
|
|
64
|
+
if (provenance) {
|
|
65
|
+
if (!current || current.revision !== provenance.applied_revision || hash(current.value) !== provenance.value_hash || attachmentHash(current.attachment) !== provenance.attachment_hash) throw new DoorError('native_edit_conflict',409);
|
|
66
|
+
if (child.ordinal!==executingOrdinal) predicates.push(request.options.targets.matches(current,root.destination,baseline.kind,baseline.id));
|
|
67
|
+
} else {
|
|
68
|
+
if (canonicalJson(current ?? null) !== canonicalJson(baseline.row)) throw new DoorError('native_edit_conflict',409);
|
|
69
|
+
if (child.ordinal!==executingOrdinal) predicates.push(request.options.targets.matches(baseline.row,root.destination,baseline.kind,baseline.id));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const retained = [...(await prior(request,root)).values()].filter(record=>!replacing.has(recordKey(record)));
|
|
74
|
+
predicates.push(...await ownedFence(request,[...completed,...retained]));
|
|
75
|
+
return conjunction(predicates);
|
|
76
|
+
}
|
|
77
|
+
export function snapshotRecords(snapshot: DoorSnapshot, old: Map<string,DoorRecord>, relationshipHash: string): DoorRecord[] {
|
|
78
|
+
return snapshot.plan.entries.map(entry=>{
|
|
79
|
+
const previous = old.get(recordKey(entry.source));
|
|
80
|
+
return {kind:entry.source.kind,sourceId:entry.source.sourceId,sourceHash:entry.rejection && previous ? previous.sourceHash : hash([entry.source.payloadHash,relationshipHash,snapshot.plan.mappingVersion]),
|
|
81
|
+
targets:entry.effects.length ? entry.effects.map(effect=>({kind:effect.targetKind,id:effect.targetId,ownerPlanId:snapshot.id,sourceIdentity:entry.identity})) : previous?.targets ?? [],
|
|
82
|
+
...(entry.rejection || entry.unmapped.length ? {explanation:entry.rejection ?? entry.unmapped.map(u=>u.reason).join('; ')} : {})};
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ImportPlan } from '../../types.ts';
|
|
2
|
+
import { jsonChunks } from '../../d1/sets.ts';
|
|
3
|
+
import { sql } from '../store.ts';
|
|
4
|
+
import { hash } from './store.ts';
|
|
5
|
+
|
|
6
|
+
/** A completed flag alone is not reconciliation. Every primary disposition,
|
|
7
|
+
* including an archive/rejection with no native effect, needs its exact durable
|
|
8
|
+
* decision and source claim. These predicates are rechecked at final commit. */
|
|
9
|
+
export function childJournalGuards(jobId: string, plan: ImportPlan) {
|
|
10
|
+
const expected=plan.entries.map((entry,ordinal)=>[ordinal,entry.identity,entry.source.payloadHash,hash(entry.effects)]);
|
|
11
|
+
return [
|
|
12
|
+
sql('(SELECT count(*) FROM ik_d1_entries WHERE job_id=?)=?',jobId,expected.length),
|
|
13
|
+
...jsonChunks(expected).map(body=>sql(`NOT EXISTS(SELECT 1 FROM json_each(?) x WHERE NOT EXISTS(
|
|
14
|
+
SELECT 1 FROM ik_d1_entries e JOIN ik_sources s ON s.identity=e.source_identity
|
|
15
|
+
WHERE e.job_id=? AND e.ordinal=json_extract(x.value,'$[0]') AND e.source_identity=json_extract(x.value,'$[1]')
|
|
16
|
+
AND e.effects_hash=json_extract(x.value,'$[3]') AND e.owner_job_id=? AND s.job_id=e.owner_job_id
|
|
17
|
+
AND s.source_hash=json_extract(x.value,'$[2]') AND s.effects_hash=e.effects_hash AND s.mapping_version=?))`,body,jobId,jobId,plan.mappingVersion)),
|
|
18
|
+
];
|
|
19
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {childJournalGuards} from './journal.ts';
|
|
2
|
+
import {storedInventoryHash} from './digests.ts';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
import type { DoorRequest } from '../types.ts';
|
|
5
|
+
import type { D1ImportContext } from '../../d1/types.ts';
|
|
6
|
+
import { createD1ImportJob, readD1ImportJob } from '../../d1/jobs.ts';
|
|
7
|
+
import { applyD1ImportBatches } from '../../d1/apply.ts';
|
|
8
|
+
import { rollbackD1Import, undoD1Import } from '../../d1/reverse.ts';
|
|
9
|
+
import { readD1ImportOriginals } from '../../d1/originals.ts';
|
|
10
|
+
import { assertion, authorized, sql, transact } from '../store.ts';
|
|
11
|
+
import { DoorError } from '../protocol.ts';
|
|
12
|
+
import { recordKey } from '../inventory.ts';
|
|
13
|
+
import { failedPlan, reasonCode, requireNewPlan } from '../failures.ts';
|
|
14
|
+
import { sizeLimitError } from '../limits.ts';
|
|
15
|
+
import { sha256 } from '../../provenance/identity.ts';
|
|
16
|
+
import { bindWaveContext } from '../../d1/wave-boundary.ts';
|
|
17
|
+
import { BOUNDED_WAVES, totalKeys, zeroTotals, type WaveCensus, type WaveChild, type WaveRoot } from './types.ts';
|
|
18
|
+
import { activeParent, approve, assertPredicate, authority, childSnapshot, children, hash, parentFingerprint, rootFence, rootRow } from './store.ts';
|
|
19
|
+
import { conjunction, executionFence, ownedFence, prior, records } from './inventory.ts';
|
|
20
|
+
|
|
21
|
+
async function transition(request: DoorRequest, root: WaveRoot, state: string, extra = sql('1')) {
|
|
22
|
+
await transact(request.options.database,authority(request,extra),[
|
|
23
|
+
assertion(rootFence(root)),sql('UPDATE ik_door_wave_roots SET state=? WHERE id=?',state,root.id),
|
|
24
|
+
]);
|
|
25
|
+
root.state=state;
|
|
26
|
+
}
|
|
27
|
+
export async function seal(request: DoorRequest, input: Record<string,unknown>) {
|
|
28
|
+
const root=await rootRow(request,input), expected=JSON.parse(root.census_json) as WaveCensus;
|
|
29
|
+
if (root.state!=='preparing') return parentView(request,root);
|
|
30
|
+
if (root.pages!==expected.children) throw new DoorError('complete_census_required',409);
|
|
31
|
+
const upload = await request.options.database.prepare('SELECT total_bytes FROM ik_door_uploads WHERE id=?').bind(root.upload_id).first<any>();
|
|
32
|
+
if (!upload) throw new DoorError('parent_custody_missing',410);
|
|
33
|
+
const totals=zeroTotals(), byKind:Record<string,number>=Object.create(null), nativeByTable:Record<string,number>=Object.create(null);
|
|
34
|
+
const relationships:string[]=[];
|
|
35
|
+
const seen=new Set<string>(), changes={additions:0,replacements:0,unchanged:0,rejected:0,unmapped:0};
|
|
36
|
+
for (const child of await children(request,root)) {
|
|
37
|
+
for (const key of totalKeys) totals[key]+=child[key];
|
|
38
|
+
const snapshot=await childSnapshot(request,child), d=snapshot.wave.descriptor;
|
|
39
|
+
for (const [kind,n] of Object.entries(snapshot.plan.counts.byKind)) byKind[kind]=(byKind[kind] ?? 0)+n;
|
|
40
|
+
for (const [table,n] of Object.entries(d.nativeByTable)) nativeByTable[table]=(nativeByTable[table] ?? 0)+n;
|
|
41
|
+
relationships.push(d.relationshipsHash);
|
|
42
|
+
for (const record of snapshot.records) seen.add(hash([record.kind,record.sourceId]));
|
|
43
|
+
changes.additions+=snapshot.differences.additions.length;changes.replacements+=snapshot.differences.replacements.length;changes.unchanged+=snapshot.differences.unchanged.length;
|
|
44
|
+
changes.rejected+=snapshot.plan.counts.rejected;
|
|
45
|
+
// Unchanged sources retain their earlier disposition, including an empty
|
|
46
|
+
// target set. New or changed targetless sources still require a proposal.
|
|
47
|
+
const unchanged=new Set(snapshot.differences.unchanged);
|
|
48
|
+
changes.unmapped+=snapshot.records.filter(r=>!r.targets.length && !unchanged.has(recordKey(r))).length;
|
|
49
|
+
}
|
|
50
|
+
if (totalKeys.some(k=>totals[k]!==expected[k]) || hash(byKind)!==hash(expected.byKind) || hash(nativeByTable)!==hash(expected.nativeByTable)) throw new DoorError('aggregate_census_mismatch',422);
|
|
51
|
+
// Every verified indexed file, including context and objects, has one custody
|
|
52
|
+
// owner. Transport bytes remain distinct from logical original/native bytes.
|
|
53
|
+
const {packageUpload}=await import('../package.ts'), {uploadRow}=await import('../uploads.ts');
|
|
54
|
+
const pkg=await packageUpload(request,await uploadRow(request,root.upload_id));
|
|
55
|
+
const member=await request.options.database.prepare("SELECT count(*) n FROM ik_door_wave_members WHERE parent_id=? AND type='file'").bind(root.id).first<any>();
|
|
56
|
+
if (member?.n!==pkg.files.length || totals.storedBytes!==pkg.files.reduce((n,f)=>n+f.bytes,0)) throw new DoorError('complete_file_coverage_required',422);
|
|
57
|
+
const inventoryHash=await storedInventoryHash(request.options.database,root.id);
|
|
58
|
+
const previousRoot=root.previous_id ? await request.options.database.prepare('SELECT fingerprint FROM ik_door_wave_roots WHERE id=?').bind(root.previous_id).first<any>() : null;
|
|
59
|
+
if (inventoryHash!==expected.inventoryHash || hash(relationships)!==expected.relationshipsHash || (previousRoot?.fingerprint ?? hash([]))!==expected.baselineHash) throw new DoorError('complete_inventory_hash_mismatch',422);
|
|
60
|
+
if (root.catchup===1) {
|
|
61
|
+
const previous=await request.options.database.prepare('SELECT census_json FROM ik_door_wave_roots WHERE id=? AND state=\'completed\'').bind(root.previous_id).first<any>();
|
|
62
|
+
const baseline=previous && JSON.parse(previous.census_json).source;
|
|
63
|
+
const gone=(await records(request,root.previous_id,false)).filter(r=>!seen.has(hash([r.kind,r.sourceId]))).length;
|
|
64
|
+
const reason=!baseline?'empty_baseline':gone?'source_records_gone':changes.rejected || changes.unmapped?'rejected_or_unmapped_records':(changes.additions+changes.replacements)*100>=baseline*5?'change_must_be_under_five_percent':null;
|
|
65
|
+
if (reason) throw new DoorError('catchup_requires_proposal:'+reason,409);
|
|
66
|
+
}
|
|
67
|
+
const fingerprint=parentFingerprint(root);
|
|
68
|
+
await transact(request.options.database,authority(request),[
|
|
69
|
+
assertion(rootFence(root)),sql("UPDATE ik_door_wave_roots SET fingerprint=?,state='sealed' WHERE id=?",fingerprint,root.id),
|
|
70
|
+
]);
|
|
71
|
+
return parentView(request,{...root,fingerprint,state:'sealed'});
|
|
72
|
+
}
|
|
73
|
+
export async function reviewParent(request: DoorRequest, input: Record<string,unknown>) {
|
|
74
|
+
const root=await rootRow(request,input);
|
|
75
|
+
if (!['sealed','approved','applying','completed'].includes(root.state) || !root.fingerprint) throw new DoorError('sealed_parent_required',409);
|
|
76
|
+
if (await storedInventoryHash(request.options.database,root.id)!==JSON.parse(root.census_json).inventoryHash) throw new DoorError('aggregate_census_mismatch',409);
|
|
77
|
+
await approve(request,root,-1,root.fingerprint);
|
|
78
|
+
if (root.state==='sealed') await transition(request,root,'approved');
|
|
79
|
+
return parentView(request,root);
|
|
80
|
+
}
|
|
81
|
+
function approvalFence(request: DoorRequest, root: WaveRoot, child: WaveChild) {
|
|
82
|
+
return conjunction([activeParent(request,root),sql(`EXISTS(SELECT 1 FROM ik_door_wave_approvals WHERE parent_id=? AND ordinal=? AND revoked=0 AND fingerprint=? AND approval_id=? AND approved_at=? AND owner=?)`,root.id,child.ordinal,child.fingerprint,request.access.approvalRecordId,request.access.approvedAt,request.access.email)]);
|
|
83
|
+
}
|
|
84
|
+
async function context(request: DoorRequest, root: WaveRoot, child: WaveChild, mode: 'apply'|'reverse'|'read'): Promise<D1ImportContext> {
|
|
85
|
+
const snapshot=await childSnapshot(request,child);
|
|
86
|
+
if (hash(request.options.bounded!.native(snapshot.plan))!==hash(snapshot.wave.descriptor.projections)) throw new DoorError('native_accounting_changed',409);
|
|
87
|
+
const fence=mode==='read'?sql('1'):await executionFence(request,root,child.ordinal);
|
|
88
|
+
const approved=mode==='apply'?approvalFence(request,root,child):activeParent(request,root);
|
|
89
|
+
const externalChild=request.options.bounded!.authority({...request.access.bounded!,childFingerprint:child.fingerprint},request.access.action);
|
|
90
|
+
const associated=sql("EXISTS(SELECT 1 FROM ik_door_plans p LEFT JOIN ik_jobs j ON j.id=p.job_id WHERE p.id=? AND p.fingerprint=? AND (p.job_id IS NULL OR (j.operation_key=? AND j.fingerprint=p.fingerprint)))",child.plan_id,snapshot.plan.fingerprint,'door:'+child.plan_id);
|
|
91
|
+
const active=conjunction([approved,externalChild,associated,rootFence(root),fence,sql('EXISTS(SELECT 1 FROM ik_door_plan_chunks WHERE plan_id=?)',child.plan_id)]);
|
|
92
|
+
const result: D1ImportContext={destination:root.destination,namespace:snapshot.kitNamespace,mappingVersion:root.mapping_version,
|
|
93
|
+
operationKey:'door:'+child.plan_id,reviewedFingerprint:snapshot.plan.fingerprint,
|
|
94
|
+
storage:{database:request.options.database,targets:request.options.targets},batchSize:request.options.bounded!.batchSize ?? 20,maxBatches:1,reversalPolicy:'strict',
|
|
95
|
+
authorization:{principalId:request.options.ownerPrincipalId,predicate(action,destination) {
|
|
96
|
+
if (destination!==root.destination || !['read',...(mode==='apply'?['create','apply']:mode==='reverse'?['undo','rollback']:[])].includes(action)) return sql('0');
|
|
97
|
+
return authority(request,active);
|
|
98
|
+
}}};
|
|
99
|
+
bindWaveContext(result,root.id,mode);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
export async function applyChild(request: DoorRequest, input: Record<string,unknown>) {
|
|
103
|
+
const root=await rootRow(request,input);
|
|
104
|
+
if (!['approved','applying','completed'].includes(root.state)) throw new DoorError('approved_parent_required',409);
|
|
105
|
+
if (!Number.isSafeInteger(input.ordinal) || (input.ordinal as number)<0) throw new DoorError('invalid_child_ordinal',400);
|
|
106
|
+
const all=await children(request,root), child=all[input.ordinal as number];
|
|
107
|
+
if (!child || request.access.bounded?.childFingerprint!==child.fingerprint || input.reviewedFingerprint!==child.fingerprint) throw new DoorError('child_fingerprint_mismatch');
|
|
108
|
+
const failure=await failedPlan(request,child.plan_id);if (failure) requireNewPlan(child.plan_id,failure);
|
|
109
|
+
if (child.source>Math.min(request.access.maxRecords,request.options.limits?.maxRecords ?? request.access.maxRecords) || child.native>Math.min(request.access.maxRecords,request.options.limits?.maxRecords ?? request.access.maxRecords)) throw new DoorError('child_exceeds_grant_quota',413);
|
|
110
|
+
await assertPredicate(request,activeParent(request,root),'parent_approval_revoked');
|
|
111
|
+
for (const predecessor of all.slice(0,child.ordinal)) {
|
|
112
|
+
const job=predecessor.job_id && await request.options.database.prepare('SELECT state FROM ik_jobs WHERE id=?').bind(predecessor.job_id).first<any>();
|
|
113
|
+
if (!job || job.state!=='completed') throw new DoorError('child_dependency_incomplete',409);
|
|
114
|
+
}
|
|
115
|
+
await approve(request,root,child.ordinal,child.fingerprint);
|
|
116
|
+
if (root.state==='approved') await transition(request,root,'applying',activeParent(request,root));
|
|
117
|
+
const snapshot=await childSnapshot(request,child), ctx=await context(request,root,child,'apply');
|
|
118
|
+
const job=await createD1ImportJob(snapshot.plan,ctx);
|
|
119
|
+
await transact(request.options.database,authority(request,approvalFence(request,root,child)),[
|
|
120
|
+
sql('INSERT OR IGNORE INTO ik_door_wave_origins(job_id,parent_id,ordinal,plan_id,fingerprint) VALUES(?,?,?,?,?)',job.id,root.id,child.ordinal,child.plan_id,snapshot.plan.fingerprint),
|
|
121
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_wave_origins WHERE job_id=? AND parent_id=? AND ordinal=? AND plan_id=? AND fingerprint=?)',job.id,root.id,child.ordinal,child.plan_id,snapshot.plan.fingerprint)),
|
|
122
|
+
sql('UPDATE ik_door_plans SET job_id=? WHERE id=? AND (job_id IS NULL OR job_id=?)',job.id,child.plan_id,job.id),
|
|
123
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_plans WHERE id=? AND job_id=?)',child.plan_id,job.id)),
|
|
124
|
+
]);
|
|
125
|
+
const result=await applyD1ImportBatches(job.id,ctx).catch(async error=>{
|
|
126
|
+
if (sizeLimitError(error)) throw error;
|
|
127
|
+
const code=reasonCode(error);
|
|
128
|
+
// Durable failure bookkeeping grants no native authority. A concurrent
|
|
129
|
+
// successful batch or erasure prevents this exact pre-batch transition.
|
|
130
|
+
await transact(request.options.database,sql('1'),[
|
|
131
|
+
assertion(sql('(SELECT count(*) FROM ik_d1_erasure_journal WHERE destination=?)=?',root.destination,root.epoch)),
|
|
132
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_plan_chunks WHERE plan_id=?)',child.plan_id)),
|
|
133
|
+
assertion(sql("EXISTS(SELECT 1 FROM ik_door_grants WHERE installation_id=? AND jti=? AND action='apply-child' AND namespace=?)",root.installation_id,request.nonce!.jti,root.namespace)),
|
|
134
|
+
assertion(sql("EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND fingerprint=? AND fence=? AND checkpoint=? AND state IN ('queued','applying'))",job.id,job.fingerprint,job.fence,job.checkpoint)),
|
|
135
|
+
sql('INSERT INTO ik_door_apply_failures(plan_id,reason_code) VALUES(?,?)',child.plan_id,code),
|
|
136
|
+
sql("UPDATE ik_jobs SET state='failed',error=?,fence=fence+1 WHERE id=?",code,job.id),
|
|
137
|
+
]);
|
|
138
|
+
throw new DoorError(code,code==='import_door_apply_transient'?503:409,{parentId:root.id,ordinal:child.ordinal,requiresNewParent:true,requiresNewApproval:true});
|
|
139
|
+
});
|
|
140
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,ordinal:child.ordinal,fingerprint:child.fingerprint,job:result,
|
|
141
|
+
childSuccess:result.state==='completed',aggregateSuccess:false};
|
|
142
|
+
}
|
|
143
|
+
export async function finalize(request: DoorRequest, input: Record<string,unknown>) {
|
|
144
|
+
const root=await rootRow(request,input);
|
|
145
|
+
if (!['applying','completed'].includes(root.state)) throw new DoorError('parent_not_applied',409);
|
|
146
|
+
const parent=activeParent(request,root);
|
|
147
|
+
await assertPredicate(request,parent,'parent_approval_revoked');
|
|
148
|
+
const all=await children(request,root), expected=JSON.parse(root.census_json) as WaveCensus;
|
|
149
|
+
if (await storedInventoryHash(request.options.database,root.id)!==expected.inventoryHash) throw new DoorError('aggregate_census_mismatch',409);
|
|
150
|
+
if (all.length!==expected.children) throw new DoorError('aggregate_children_missing',409);
|
|
151
|
+
const totals=zeroTotals(), guards=[parent,rootFence(root)];
|
|
152
|
+
const dispositions={mapped:0,rejected:0,explained:0,reused:0};
|
|
153
|
+
let protocolBytes=0;
|
|
154
|
+
const delivered=[], old=await prior(request,root);
|
|
155
|
+
for (const child of all) {
|
|
156
|
+
if (!child.job_id) throw new DoorError('aggregate_children_missing',409);
|
|
157
|
+
const revision=await request.options.database.prepare('SELECT revision FROM ik_d1_custody_states WHERE job_id=?').bind(child.job_id).first<{revision:number}>();
|
|
158
|
+
if (!revision) throw new DoorError('aggregate_child_journal_mismatch',409);
|
|
159
|
+
guards.push(sql('EXISTS(SELECT 1 FROM ik_d1_custody_states WHERE job_id=? AND revision=?)',child.job_id,revision.revision),
|
|
160
|
+
sql('EXISTS(SELECT 1 FROM ik_door_wave_origins WHERE job_id=? AND parent_id=? AND ordinal=? AND plan_id=?)',child.job_id,root.id,child.ordinal,child.plan_id));
|
|
161
|
+
const ctx=await context(request,root,child,'read'), job=await readD1ImportJob(child.job_id,ctx);
|
|
162
|
+
if (job.state!=='completed' || job.checkpoint!==job.total) throw new DoorError('aggregate_children_incomplete',409);
|
|
163
|
+
const snapshot=await childSnapshot(request,child);
|
|
164
|
+
if (job.total!==snapshot.sourceCount) throw new DoorError('aggregate_child_journal_mismatch',409);
|
|
165
|
+
const journal=childJournalGuards(job.id,snapshot.plan);
|
|
166
|
+
for (const guard of journal) await assertPredicate(request,guard,'aggregate_child_journal_mismatch');
|
|
167
|
+
guards.push(...journal);
|
|
168
|
+
for (const original of await readD1ImportOriginals(child.job_id,ctx)) {
|
|
169
|
+
if (original.original.status!=='retained') throw new DoorError('aggregate_bytes_unreadable',410);
|
|
170
|
+
const bytes=Buffer.from(original.original.base64,'base64');
|
|
171
|
+
if (bytes.length!==original.size || sha256(bytes)!==original.sha256) throw new DoorError('aggregate_bytes_unreadable',410);
|
|
172
|
+
}
|
|
173
|
+
guards.push(request.options.bounded!.authority({...request.access.bounded!,childFingerprint:child.fingerprint},'finalize-parent'),sql(`EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND state='completed' AND fingerprint=? AND fence=? AND checkpoint=?)`,job.id,job.fingerprint,job.fence,job.checkpoint),
|
|
174
|
+
sql('EXISTS(SELECT 1 FROM ik_door_wave_approvals WHERE parent_id=? AND ordinal=? AND revoked=0 AND fingerprint=?)',root.id,child.ordinal,child.fingerprint));
|
|
175
|
+
dispositions.mapped+=snapshot.plan.counts.mapped;dispositions.rejected+=snapshot.plan.counts.rejected;dispositions.explained+=snapshot.records.filter(r=>!r.targets.length && !snapshot.plan.entries.find(e=>e.source.kind===r.kind && e.source.sourceId===r.sourceId)?.rejection).length;dispositions.reused+=snapshot.differences.unchanged.length;
|
|
176
|
+
protocolBytes+=snapshot.plan.source.artifacts.filter(a=>a.name==='rer-package.json').reduce((n,a)=>n+a.size,0);
|
|
177
|
+
delivered.push(...snapshot.records);
|
|
178
|
+
for (const key of totalKeys) totals[key]+=child[key];
|
|
179
|
+
}
|
|
180
|
+
if (totalKeys.some(k=>totals[k]!==expected[k])) throw new DoorError('aggregate_census_mismatch',409);
|
|
181
|
+
const seen=new Set(delivered.map(r=>hash([r.kind,r.sourceId]))), gone=[...old.values()].filter(r=>!seen.has(hash([r.kind,r.sourceId])));
|
|
182
|
+
guards.push(...await ownedFence(request,[...delivered,...gone]));
|
|
183
|
+
const lineage=sql('EXISTS(SELECT 1 FROM ik_door_namespaces WHERE installation_id=? AND namespace=? AND ((pending_id=? AND head_id IS ?) OR (pending_id IS NULL AND head_id=?)))',root.installation_id,root.namespace,root.id,root.previous_id,root.id);
|
|
184
|
+
await transact(request.options.database,authority(request,conjunction([...guards.slice(0,1),lineage])),[
|
|
185
|
+
...guards.slice(1).map(assertion),
|
|
186
|
+
sql("UPDATE ik_door_wave_roots SET state='completed' WHERE id=?",root.id),
|
|
187
|
+
sql('UPDATE ik_door_namespaces SET head_id=?,pending_id=NULL WHERE installation_id=? AND namespace=?',root.id,root.installation_id,root.namespace),
|
|
188
|
+
],[...guards.filter((_,i)=>i!==1),rootFence({...root,state:'completed'})]);
|
|
189
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,fingerprint:root.fingerprint,aggregateSuccess:true,completeHead:root.id,
|
|
190
|
+
counts:totals,dispositions,nativeSuccess:dispositions.rejected===0 && dispositions.explained===0,metadata:{manifestCopies:all.length,originalManifestBytes:protocolBytes},retainedNativeTargets:gone.reduce((n,r)=>n+r.targets.length,0),goneButRetained:gone.map(r=>({kind:r.kind,sourceId:r.sourceId})),children:all.length};
|
|
191
|
+
}
|
|
192
|
+
export async function reverseParent(request: DoorRequest, input: Record<string,unknown>) {
|
|
193
|
+
const root=await rootRow(request,input), parent=activeParent(request,root);
|
|
194
|
+
await assertPredicate(request,parent,'parent_approval_revoked');
|
|
195
|
+
// A lost final response must resolve to the same durable reversal even after
|
|
196
|
+
// the complete head has already been restored to the previous snapshot.
|
|
197
|
+
if (root.state==='reversed') return parentView(request,root);
|
|
198
|
+
const line=await request.options.database.prepare('SELECT head_id,pending_id FROM ik_door_namespaces WHERE installation_id=? AND namespace=?').bind(root.installation_id,root.namespace).first<any>();
|
|
199
|
+
if (!line || (line.pending_id!==root.id && line.head_id!==root.id) || line.pending_id && line.pending_id!==root.id) throw new DoorError('parent_superseded',409);
|
|
200
|
+
if (!['approved','applying','completed','reversing'].includes(root.state)) throw new DoorError('parent_reverse_refused',409);
|
|
201
|
+
// Full known-conflict preflight, followed by each ordinary strict transaction.
|
|
202
|
+
// This is resumable partial reversal, never whole-database atomicity.
|
|
203
|
+
await executionFence(request,root);
|
|
204
|
+
if (root.state!=='reversing') {
|
|
205
|
+
// Reserve the same pending-parent slot before reversing a completed head.
|
|
206
|
+
// A catch-up prepared after our read cannot race or be cleared by Undo.
|
|
207
|
+
await transact(request.options.database,authority(request,parent),[
|
|
208
|
+
assertion(rootFence(root)),sql("UPDATE ik_door_wave_roots SET state='reversing' WHERE id=?",root.id),
|
|
209
|
+
sql('UPDATE ik_door_namespaces SET pending_id=? WHERE installation_id=? AND namespace=?',root.id,root.installation_id,root.namespace),
|
|
210
|
+
],[rootFence({...root,state:'reversing'})]);
|
|
211
|
+
root.state='reversing';
|
|
212
|
+
}
|
|
213
|
+
const all=await children(request,root);
|
|
214
|
+
for (const child of all.toReversed()) {
|
|
215
|
+
if (!child.job_id) continue;
|
|
216
|
+
const ctx=await context(request,root,child,'reverse'), job=await readD1ImportJob(child.job_id,ctx);
|
|
217
|
+
if (['undone','rolled_back'].includes(job.state)) continue;
|
|
218
|
+
const result=await (['completed','undoing'].includes(job.state)?undoD1Import:rollbackD1Import)(job.id,ctx);
|
|
219
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,state:'reversing',ordinal:child.ordinal,job:result,aggregateSuccess:false};
|
|
220
|
+
}
|
|
221
|
+
await transact(request.options.database,authority(request,parent),[
|
|
222
|
+
assertion(rootFence(root)),
|
|
223
|
+
sql("UPDATE ik_door_wave_roots SET state='reversed' WHERE id=?",root.id),
|
|
224
|
+
sql('UPDATE ik_door_namespaces SET head_id=CASE WHEN head_id=? THEN ? ELSE head_id END,pending_id=NULL WHERE installation_id=? AND namespace=?',root.id,root.previous_id,root.installation_id,root.namespace),
|
|
225
|
+
],[rootFence({...root,state:'reversed'})]);
|
|
226
|
+
return parentView(request,{...root,state:'reversed'});
|
|
227
|
+
}
|
|
228
|
+
async function parentView(request: DoorRequest, root: WaveRoot) {
|
|
229
|
+
const all=await children(request,root), states=[];
|
|
230
|
+
for (const child of all) {
|
|
231
|
+
const job=child.job_id ? await request.options.database.prepare('SELECT state,checkpoint,total,error FROM ik_jobs WHERE id=?').bind(child.job_id).first<any>() : null;
|
|
232
|
+
states.push({ordinal:child.ordinal,fingerprint:child.fingerprint,source:child.source,native:child.native,job:job ?? null,childSuccess:job?.state==='completed',skipped:!job && root.state==='reversed'});
|
|
233
|
+
}
|
|
234
|
+
const withdrawn=root.state==='withdrawn' || root.epoch!==request.epoch;
|
|
235
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,fingerprint:root.fingerprint,state:withdrawn?'withdrawn':root.state,
|
|
236
|
+
census:JSON.parse(root.census_json),children:states,progress:{expected:JSON.parse(root.census_json).children,applied:states.filter(c=>c.childSuccess).length,pending:states.filter(c=>!c.skipped && (!c.job || ['queued','applying'].includes(c.job.state))).length,skipped:states.filter(c=>c.skipped).length,failed:states.filter(c=>c.job?.state==='failed').length,reversed:states.filter(c=>['undone','rolled_back'].includes(c.job?.state)).length,withdrawn},aggregateSuccess:false,completionRecorded:!withdrawn && root.state==='completed'};
|
|
237
|
+
}
|
|
238
|
+
export async function waveStatus(request: DoorRequest, input: Record<string,unknown>) {
|
|
239
|
+
const root=await rootRow(request,input,true), result=await parentView(request,root);
|
|
240
|
+
await authorized({...request,authority:authority(request)});
|
|
241
|
+
return result;
|
|
242
|
+
}
|