@runeditrun/rer 0.2.94 → 0.2.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +3 -0
  2. package/cli/README.md +3 -0
  3. package/cli/agent-journey-fixtures.mjs +1 -1
  4. package/cli/authoring.mjs +1 -1
  5. package/cli/business/carry-files.mjs +115 -0
  6. package/cli/business/carry.mjs +20 -81
  7. package/cli/business/choices.mjs +12 -7
  8. package/cli/contract.mjs +1 -1
  9. package/cli/door-clauses.mjs +28 -2
  10. package/cli/hosted-output.mjs +4 -2
  11. package/cli/hosted-queue-producers.mjs +43 -0
  12. package/cli/jsonc.mjs +1 -115
  13. package/cli/parse.mjs +1 -1
  14. package/cli/portability-migrations.mjs +2 -12
  15. package/edition-authoring/archive-source.mjs +86 -1
  16. package/edition-authoring/base-copies.mjs +15 -45
  17. package/edition-authoring/cli.mjs +1 -1
  18. package/edition-authoring/edition-migrations.mjs +2 -2
  19. package/edition-authoring/module-closure.mjs +119 -50
  20. package/edition-authoring/module-packages.mjs +12 -5
  21. package/edition-authoring/module-syntax.mjs +13 -50
  22. package/edition-authoring/runtime-dependencies.mjs +4 -4
  23. package/global/CHANGELOG.md +38 -0
  24. package/global/README.md +3 -0
  25. package/modules/agent-door/CLAUSES.md +12 -0
  26. package/modules/agent-door/conformance/audit.mjs +22 -4
  27. package/modules/agent-door/conformance/discovery.mjs +32 -1
  28. package/modules/agent-door/conformance/erasure.mjs +103 -0
  29. package/modules/agent-door/conformance/journey.mjs +19 -1
  30. package/modules/agent-door/conformance/policy-phases.mjs +6 -0
  31. package/modules/agent-door/conformance/protocol.mjs +13 -3
  32. package/modules/deploy-kit/jsonc.mjs +115 -0
  33. package/modules/deploy-kit/migration-directory.mjs +16 -0
  34. package/modules/deploy-kit/prepare.mjs +1 -1
  35. package/modules/deploy-kit/templates.mjs +9 -4
  36. package/modules/erasure/cascade.mjs +4 -4
  37. package/modules/erasure/index.d.mts +1 -1
  38. package/modules/erasure/index.mjs +1 -1
  39. package/modules/import-kit/src/d1/README.md +16 -1
  40. package/modules/import-kit/src/d1/preserved-reverse.ts +5 -2
  41. package/modules/import-kit/src/d1/read-fence.ts +14 -2
  42. package/modules/import-kit/src/d1/reverse.ts +4 -1
  43. package/modules/import-kit/src/d1/sets.ts +8 -0
  44. package/modules/import-kit/src/d1/snapshot-text.ts +68 -0
  45. package/modules/import-kit/src/d1/undo-report.ts +4 -1
  46. package/modules/import-kit/src/door/ADOPTING.md +9 -4
  47. package/modules/portability/README.md +28 -2
  48. package/modules/portability/bookkeeping.mjs +8 -4
  49. package/modules/portability/checkout-history.mjs +12 -6
  50. package/modules/portability/import.mjs +1 -1
  51. package/modules/portability/storage.mjs +7 -1
  52. package/modules/portability/worker-history.d.mts +1 -1
  53. package/modules/portability/worker-history.mjs +1 -1
  54. package/package.json +1 -1
@@ -0,0 +1,115 @@
1
+ function location(text, position) {
2
+ const lines = text.slice(0, position).split('\n');
3
+ return `position ${position} (line ${lines.length} column ${lines.at(-1).length + 1})`;
4
+ }
5
+
6
+ // Validate JSONC before JSON.parse so diagnostics do not depend on the Node
7
+ // version's error wording. An explicit stack also handles deeply nested input.
8
+ // Only comments and commas accepted by the grammar are masked; offsets stay in
9
+ // the original source and comment removal can never join separate tokens.
10
+ export function parseJsonc(text, onProperty) {
11
+ const result = text.split(''), stack = [{ kind: 'root', state: 'value', path: [] }];
12
+ let index = 0;
13
+ const fail = (message, position = index) => {
14
+ throw new SyntaxError(`${position === text.length ? 'Unexpected end of JSON input' : message} at ${location(text, position)}`);
15
+ };
16
+ const unterminated = (kind, start) => {
17
+ throw new SyntaxError(`Unterminated ${kind} at ${location(text, start)}; close the ${kind}.`);
18
+ };
19
+ const blank = start => {
20
+ for (let i = start; i < index; i++) if (text[i] !== '\n' && text[i] !== '\r') result[i] = ' ';
21
+ };
22
+ const space = () => {
23
+ while (index < text.length) {
24
+ if (' \t\r\n'.includes(text[index])) { index++; continue; }
25
+ const start = index;
26
+ if (text[index] === '/' && text[index + 1] === '/') {
27
+ index += 2;
28
+ while (index < text.length && text[index] !== '\n' && text[index] !== '\r') index++;
29
+ } else if (text[index] === '/' && text[index + 1] === '*') {
30
+ const end = text.indexOf('*/', index + 2);
31
+ if (end === -1) unterminated('block comment', start);
32
+ index = end + 2;
33
+ } else break;
34
+ blank(start);
35
+ }
36
+ };
37
+ const string = () => {
38
+ const start = index++;
39
+ let end = index;
40
+ while (end < text.length && text[end] !== '"') end = Math.min(text.length, end + (text[end] === '\\' ? 2 : 1));
41
+ if (end === text.length) unterminated('string', start);
42
+ while (index < end) {
43
+ if (text.charCodeAt(index) < 32) fail('Unescaped control character in string');
44
+ if (text[index++] !== '\\') continue;
45
+ if (text[index] === 'u') {
46
+ index++;
47
+ for (let digit = 0; digit < 4; digit++, index++) {
48
+ if (index >= end || !/[0-9a-fA-F]/.test(text[index])) fail('Invalid Unicode escape');
49
+ }
50
+ } else {
51
+ if (!'"\\/bfnrt'.includes(text[index])) fail('Invalid string escape');
52
+ index++;
53
+ }
54
+ }
55
+ index++;
56
+ };
57
+ const digit = () => index < text.length && text[index] >= '0' && text[index] <= '9';
58
+ const digits = () => {
59
+ if (!digit()) fail('Expected a digit');
60
+ do { index++; } while (digit());
61
+ };
62
+ const number = () => {
63
+ if (text[index] === '-') index++;
64
+ if (text[index] === '0') index++;
65
+ else digits();
66
+ if (text[index] === '.') { index++; digits(); }
67
+ if (text[index] === 'e' || text[index] === 'E') {
68
+ index++;
69
+ if (text[index] === '+' || text[index] === '-') index++;
70
+ digits();
71
+ }
72
+ };
73
+ while (stack.length) {
74
+ space();
75
+ const frame = stack.at(-1), char = text[index], closing = frame.kind === 'object' ? '}' : ']';
76
+ if (frame.state === 'end') {
77
+ if (index < text.length) fail('Unexpected token after JSON value');
78
+ stack.pop();
79
+ } else if ((frame.state === 'keyOrEnd' || frame.state === 'valueOrEnd' || frame.state === 'comma') && char === closing) {
80
+ index++; stack.pop();
81
+ } else if (frame.state === 'keyOrEnd') {
82
+ if (char !== '"') fail('Expected a property name');
83
+ const start = index; string();
84
+ frame.key = onProperty ? JSON.parse(text.slice(start, index)) : undefined;
85
+ onProperty?.([...frame.path, frame.key], start);
86
+ frame.state = 'colon';
87
+ } else if (frame.state === 'colon') {
88
+ if (char !== ':') fail("Expected ':' after property name");
89
+ index++; frame.state = 'value';
90
+ } else if (frame.state === 'comma') {
91
+ if (char !== ',') fail(`Expected ',' or '${closing}'`);
92
+ const comma = index++;
93
+ space();
94
+ if (text[index] === closing) result[comma] = ' ';
95
+ frame.state = frame.kind === 'object' ? 'keyOrEnd' : 'valueOrEnd';
96
+ } else {
97
+ const path = !onProperty ? undefined : frame.kind === 'root' ? [] : [...frame.path, frame.kind === 'array' ? frame.next++ : frame.key];
98
+ frame.state = frame.kind === 'root' ? 'end' : 'comma';
99
+ if (char === '{' || char === '[') {
100
+ index++;
101
+ stack.push({ kind: char === '{' ? 'object' : 'array', state: char === '{' ? 'keyOrEnd' : 'valueOrEnd', path, next: 0 });
102
+ } else if (char === '"') string();
103
+ else if (char === '-' || digit()) number();
104
+ else {
105
+ const literal = { t: 'true', f: 'false', n: 'null' }[char];
106
+ if (!literal) fail('Unexpected token in JSON value');
107
+ for (const expected of literal) {
108
+ if (text[index] !== expected) fail(`Expected '${literal}'`);
109
+ index++;
110
+ }
111
+ }
112
+ }
113
+ }
114
+ return JSON.parse(result.join(''));
115
+ }
@@ -0,0 +1,16 @@
1
+ import {join} from 'node:path';
2
+ import {readFile} from 'node:fs/promises';
3
+ import {insist} from '../portability/storage.mjs';
4
+ import {parseJsonc} from './jsonc.mjs';
5
+
6
+ // Native archive/migration checks and deploy packaging use the same seed-owned
7
+ // directory declaration. Only packaging supplies its historical fallback.
8
+ export async function migrationDirectory(root,binding,{fallback}={}) {
9
+ let config;
10
+ try { config=parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8')); }
11
+ catch(error) { if(error.code!=='ENOENT' || fallback===undefined)throw error; return join(root,fallback); }
12
+ const bindings=config.d1_databases?.filter(entry=>entry.binding===binding) ?? [];
13
+ insist(bindings.length===1 || bindings.length===0 && fallback!==undefined,'migration_ledger:binding',409);
14
+ return join(root,bindings[0]?.migrations_dir ?? fallback ?? 'migrations');
15
+ }
16
+
@@ -7,7 +7,7 @@ import { assetsIgnore, collectFiles, regularBytes, validateArtifact, MAX_ARTIFAC
7
7
  import { collectBuildModules } from './build-modules.mjs';
8
8
  import { refuseLocalEnvFiles, refuseLocalModels } from './local-only.mjs';
9
9
  import { readMigrationRewrites, withRewrites } from './migrations.mjs';
10
- import { migrationDirectory } from '../../cli/portability-migrations.mjs';
10
+ import { migrationDirectory } from './migration-directory.mjs';
11
11
 
12
12
  export function assertCredentialFree(env) {
13
13
  for (const [key,value] of Object.entries(env)) if (value && /(?:TOKEN|SECRET|PASSWORD|API_KEY|PRIVATE_KEY|AUTHORIZATION|COOKIE|CREDENTIAL)/i.test(key)) throw new Error('prepare-credential-environment-forbidden');
@@ -1,10 +1,9 @@
1
- import { access, mkdir, copyFile, writeFile } from 'node:fs/promises';
1
+ import { access, mkdir, copyFile, writeFile, readdir } from 'node:fs/promises';
2
2
  import { constants } from 'node:fs';
3
3
  import { execFileSync } from 'node:child_process';
4
- import { resolve, join, dirname } from 'node:path';
4
+ import { resolve, join, dirname, relative } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import { assert } from './validation.mjs';
7
- import { toolFiles } from '../../edition-authoring/files.mjs';
8
7
  const base = resolve(dirname(fileURLToPath(import.meta.url)),'../..');
9
8
  /** Generate alongside an edition. The existing edition tree is never an output directory. */
10
9
  export async function createTemplates({destination,project,sourcePaths,commands,loops=[]}) {
@@ -15,7 +14,13 @@ export async function createTemplates({destination,project,sourcePaths,commands,
15
14
  // A new directory only: callers add actual edition sources then run the lock command.
16
15
  const scripts = join(root,'scripts'); await mkdir(scripts);
17
16
  for (const name of ['operations.mjs','project-operations.mjs']) await copyFile(join(base,'operations',name),join(scripts,name),constants.COPYFILE_EXCL);
18
- for (const name of toolFiles(join(base,'edition-authoring'))) { await mkdir(dirname(join(scripts,'edition-authoring',name)),{recursive:true}); await copyFile(join(base,'edition-authoring',name),join(scripts,'edition-authoring',name),constants.COPYFILE_EXCL); }
17
+ const tools = join(base, 'edition-authoring');
18
+ const modules = (await readdir(tools)).filter(name => name.endsWith('.mjs') && !name.endsWith('.test.mjs'));
19
+ const vendored = (await readdir(join(tools, 'vendor'), {recursive: true, withFileTypes: true})).filter(entry => {
20
+ if (!entry.isFile() && !entry.isDirectory()) throw Error(`Unsupported vendored tool entry: ${entry.name}`);
21
+ return entry.isFile();
22
+ }).map(entry => relative(tools, join(entry.parentPath, entry.name)));
23
+ for (const name of [...modules, ...vendored].sort()) { await mkdir(dirname(join(scripts,'edition-authoring',name)),{recursive:true}); await copyFile(join(base,'edition-authoring',name),join(scripts,'edition-authoring',name),constants.COPYFILE_EXCL); }
19
24
  await writeFile(join(root,'OPERATIONS.json'),JSON.stringify({version:2,project,runtime:{requirements:[{name:'node',version:'>=22.12.0'}]},requiredEnvironment:[],commands,loops},null,2)+'\n',{flag:'wx'});
20
25
  await writeFile(join(root,'edition.json'),JSON.stringify({schemaVersion:1,id:project,toolchain:{id:'runeditrun/edition-authoring',version:'1.1.0'},parent:null,components:[{id:project,role:'edition',source:{kind:'local',paths:sourcePaths}}]},null,2)+'\n',{flag:'wx'});
21
26
  return {root,lock:['node','scripts/edition-authoring/cli.mjs','lock'],check:['node','scripts/edition-authoring/cli.mjs','check']};
@@ -35,8 +35,8 @@ export async function deletionStatements(ctx, d, plan, entries, id, deleted) {
35
35
  }
36
36
  return out;
37
37
  }
38
- export async function firstStatements(ctx, d, plan, label) {
39
- return d.beforeStatements ? await d.beforeStatements({ db: ctx.db, subject: plan.subject, label }) : [];
38
+ export async function firstStatements(ctx, d, plan, label, operation) {
39
+ return d.beforeStatements ? await d.beforeStatements({ db: ctx.db, subject: plan.subject, label, operation }) : [];
40
40
  }
41
41
  // `relabel: false` (a slice of the plan) leaves the table-wide relabel of earlier audit rows to the final batch.
42
42
  export const cleanupStatements = (ctx, d, plan, label, options) => [...redactStatements(ctx.db, d, plan), ...auditStatements(ctx.db, d, plan, label, options)];
@@ -72,7 +72,7 @@ async function sliceStep(ctx, d, op, entries, { first = null, rest = null } = {}
72
72
  guard(db, "EXISTS(SELECT 1 FROM rer_erasure_operations WHERE id=? AND step=? AND phase='deleting')", id, op.step),
73
73
  ...await fence.transfers()];
74
74
  batch.push(...await scopeStatements(db, d, first ?? plan, id, first ? [...entries, first.root] : entries, { whole: Boolean(first) }));
75
- if (first) batch.push(...await firstStatements(ctx, d, plan, op.label));
75
+ if (first) batch.push(...await firstStatements(ctx, d, plan, op.label, { id: op.id, at: op.requested_at }));
76
76
  batch.push(...await verificationStatements(db, d, plan, id, op.label));
77
77
  batch.push(...cleanupStatements(ctx, d, plan, op.label, { relabel: false }));
78
78
  batch.push(...await deletionStatements(ctx, d, plan, entries, id, deleted));
@@ -117,7 +117,7 @@ export async function step(ctx, d, id, { rows = 500 } = {}) {
117
117
  guard(db, "EXISTS(SELECT 1 FROM rer_erasure_operations WHERE id=? AND step=? AND phase='deleting')", id, op.step),
118
118
  ...await fence.transfers()];
119
119
  batch.push(...await scopeStatements(db, d, plan, id, [...take, plan.root]));
120
- if (op.step === 0) batch.push(...await firstStatements(ctx, d, plan, op.label));
120
+ if (op.step === 0) batch.push(...await firstStatements(ctx, d, plan, op.label, { id: op.id, at: op.requested_at }));
121
121
  batch.push(...await verificationStatements(db, d, plan, id, op.label));
122
122
  batch.push(...cleanupStatements(ctx, d, plan, op.label));
123
123
  batch.push(...await deletionStatements(ctx, d, plan, take, id, deleted));
@@ -5,7 +5,7 @@ export type Edge = Match & { table: string; blob?: { bucket: string; keyColumn:
5
5
  export type Redaction = Match & { table: string; set: Record<string, Cell>; reason: string };
6
6
  export interface AuditReference { table: string; column: string; of: string; where?: string; clear?: Record<string, Cell> }
7
7
  export interface Retained { table: string; reason: string }
8
- export type PolicyInput = { db: D1Database; subject: Key; label: string };
8
+ export type PolicyInput = { db: D1Database; subject: Key; label: string; operation?: { id: string; at: string } };
9
9
  export type RootRedaction = { identityColumns?: string[]; set: Record<string, Cell> | ((input: { subject: Key; anonymousId: string }) => Record<string, Cell> | Promise<Record<string, Cell>>); reason: string };
10
10
  export interface Declaration {
11
11
  entity: string;
@@ -75,7 +75,7 @@ export function createErasure({ db, buckets = {}, portability, declarations }) {
75
75
  insist(!reason, 'held:' + reason, 409);
76
76
  const id = randomHex(16), label = newLabel(), now = new Date().toISOString(), deleted = {};
77
77
  const statements = [...await ctx.fence.transfers(), openStatement(db, d, { id, subject: plan.subject, label, planned: totals.deleted, blobs: totals.blobs, now }),
78
- ...await scopeStatements(db, d, plan, id), ...await firstStatements(ctx, d, plan, label), ...await verificationStatements(db, d, plan, id, label), ...cleanupStatements(ctx, d, plan, label), ...await deletionStatements(ctx, d, plan, plan.order, id, deleted),
78
+ ...await scopeStatements(db, d, plan, id), ...await firstStatements(ctx, d, plan, label, { id, at: now }), ...await verificationStatements(db, d, plan, id, label), ...cleanupStatements(ctx, d, plan, label), ...await deletionStatements(ctx, d, plan, plan.order, id, deleted),
79
79
  ...await finalStatements(ctx, d, plan, id, label, now, deleted), progressStatement(db, id, 0, deleted), db.prepare('DELETE FROM rer_erasure_guard')];
80
80
  return { operationId: id, statements: statements.map(toD1Sql) };
81
81
  },
@@ -90,7 +90,10 @@ SELECT-only batches at the smaller of the caller limit and 2 MiB. Each batch
90
90
  checks the same monotonic custody revisions, including tombstone changes;
91
91
  the original plan revision is captured before decoding. This requires the
92
92
  additive custody-state schema. Ordinary reads without a transport policy retain
93
- their existing atomic admission. `readD1PersistedImport` keeps one complete
93
+ their existing atomic admission, except preserved undo-report reads whose kit-only
94
+ preconditions exceed 2 MiB: those use the bounded SELECT checks and final revision
95
+ fence described under "Adopting preserved-conflict undo" below.
96
+ `readD1PersistedImport` keeps one complete
94
97
  atomic native snapshot capped at 2 MiB even when its caller omits a limit. It
95
98
  reads each destination job's `ik_d1_custody_states` revision (the same additive
96
99
  schema) before any custody and fences those revisions instead of resending
@@ -878,6 +881,18 @@ provenance IDs, owner IDs and reversal flags, including SQL storage types. Exact
878
881
  report bytes, positions and a scoped row count remain fenced in the same batch;
879
882
  the report hash is not a substitute for those comparisons. Resuming undo and
880
883
  reading terminal pages therefore do not add one SQL predicate per prior effect.
884
+
885
+ Exact row snapshots may use chunk-local lexical templates to share repeated
886
+ JSON structure and evidence. SQLite reconstructs the original text slices;
887
+ comparison still covers every original byte, field, position and scoped count.
888
+ Without an explicit transport policy, preserved undo checks kit-only
889
+ preconditions larger than 2 MiB in bounded SELECT batches under monotonic
890
+ custody revisions, then fences those same revisions before any mutation.
891
+ Report reads use the same kit-only checks and final revision fence.
892
+ The existing custody-state schema is required for that large-snapshot path.
893
+ Native observations, indivisible reversal groups, report postconditions and
894
+ all native, attribution, disposition and progress writes remain together in
895
+ one atomic batch. No native mutation is split across RPCs.
881
896
  For large native sets, a snapshot can supply `nativeRows`, the complete
882
897
  destination set returned by `targets.list`. Kit requires `targets.matchesMany`,
883
898
  rejects duplicate or differently bound rows, and compares each observation's
@@ -15,6 +15,7 @@ import { readOriginals } from './originals.ts';
15
15
  import { reversalState, reportHash, reversalGuards, undoEvidence } from './undo-state.ts';
16
16
  import { classifyUndo } from './undo-snapshot.ts';
17
17
  import { insertJsonRows } from './sets.ts';
18
+ import { compactCustodyGuards } from './read-fence.ts';
18
19
 
19
20
  /** Called after public authority, binding, reversal-mode and custody checks. */
20
21
  export async function preserveReverse(current: { job: ImportJob; plan: ImportPlan }, context: D1ImportContext, action: 'undo' | 'rollback'): Promise<ImportJob> {
@@ -23,7 +24,9 @@ export async function preserveReverse(current: { job: ImportJob; plan: ImportPla
23
24
  const { database: db, targets } = context.storage;
24
25
  if (!targets.preparePreservedUndo) throw new Error('IMPORT_PRESERVED_UNDO_ADAPTER_REQUIRED');
25
26
  if (current.job.state !== reversing) {
26
- await atomic(context, action, [...boundary(current.job, current.plan, action), await noSuccessors(id, context), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context), changed(sql('UPDATE ik_jobs SET state=?,fence=fence+1 WHERE id=?', reversing, id),updateRows('ik_jobs',row=>row.id===id,row=>({...row,state:reversing,fence:current.job.fence+1})))], storedReads(current.job),nativeStagingScope(current.job,current.plan));
27
+ const scope = nativeStagingScope(current.job, current.plan);
28
+ const guards = await compactCustodyGuards(context, [...boundary(current.job, current.plan, action), await noSuccessors(id, context), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context)], scope, action);
29
+ await atomic(context, action, [...guards, changed(sql('UPDATE ik_jobs SET state=?,fence=fence+1 WHERE id=?', reversing, id),updateRows('ik_jobs',row=>row.id===id,row=>({...row,state:reversing,fence:current.job.fence+1})))], storedReads(current.job),scope);
27
30
  current = await stored(id, context, action);
28
31
  }
29
32
  let published: ImportJob | undefined;
@@ -33,7 +36,7 @@ export async function preserveReverse(current: { job: ImportJob; plan: ImportPla
33
36
  if (job.state !== reversing) throw new Error('REVERSAL_MODE_CONFLICT');
34
37
  await readOriginals(plan, id, context);
35
38
  const scope=nativeStagingScope(job,plan);
36
- const statements: D1Sql[] = [...boundary(job, plan, action), await noSuccessors(id, context), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)];
39
+ const statements: D1Sql[] = await compactCustodyGuards(context, [...boundary(job, plan, action), await noSuccessors(id, context), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], scope, action);
37
40
  const { results: rows } = await db.prepare('SELECT * FROM ik_provenance WHERE job_id=? AND reversed=0 ORDER BY ordinal DESC,effect_ordinal DESC').bind(id).all();
38
41
  const retained = new Set(state.dispositions.filter(d => d.disposition === 'retained').map(d => d.effectId));
39
42
  const effects: D1UndoEffect[] = [];
@@ -9,11 +9,19 @@ import { batchRows, checkBatchResults } from './read-results.ts';
9
9
  import { erasedIndexGuards } from './erasure-index.ts';
10
10
  import { jsonChunks } from './sets.ts';
11
11
 
12
+ /** Transfer oversized kit-only preconditions into an exact, revision-fenced
13
+ * read. The returned predicates must precede every mutation in one atomic
14
+ * batch. Native snapshots and postconditions never use this transfer. */
15
+ export async function compactCustodyGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, action: ImportAction): Promise<D1Sql[]> {
16
+ if (context.maxBatchBytes !== undefined || batchBytes(packGuards(guards, true)) <= 2 * 1024 * 1024) return guards;
17
+ return readBoundedGuards(context, guards, scope, 2 * 1024 * 1024, action);
18
+ }
19
+
12
20
  /** Check exact custody without writing private copies merely to read them.
13
21
  * Every batch sees the same monotonic revisions, captured before any exact
14
22
  * check. The final fence also retains the plan revision captured before decode.
15
23
  * Native target reads have no such custody fence and remain one atomic batch. */
16
- export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, limit: number, action: ImportAction = 'read'): Promise<void> {
24
+ export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, limit: number, action: ImportAction = 'read'): Promise<D1Sql[]> {
17
25
  const ids = new Set([scope.job.id]);
18
26
  for (const query of guards) {
19
27
  if (query.retainedValues?.privacyJobId) ids.add(query.retainedValues.privacyJobId);
@@ -40,7 +48,10 @@ export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[
40
48
  }
41
49
  if (revisions.length !== ids.size || new Set(revisions.map(row => row[0])).size !== ids.size) throw new Error('IMPORT_ATOMIC_GUARD');
42
50
  const fences = jsonChunks(revisions).map(body => guard(sql("NOT EXISTS(SELECT 1 FROM json_each(?) e WHERE NOT EXISTS(SELECT 1 FROM ik_d1_custody_states s JOIN ik_d1_custody_revisions r ON r.job_id=s.job_id WHERE s.job_id=json_extract(e.value,'$[0]') AND s.revision=json_extract(e.value,'$[1]') AND r.revision=json_extract(e.value,'$[2]')))", body)));
43
- const prefix = [authority(context, action), ...fences, ...nativeStagingPostBinding(scope)];
51
+ // Explicitly bounded callers captured the staging plan revision before
52
+ // decode. Ordinary callers instead validate their exact plan guards below
53
+ // (or the large-plan fence), under the revisions just captured here.
54
+ const prefix = [authority(context, action), ...fences, ...context.maxBatchBytes === undefined ? [] : nativeStagingPostBinding(scope)];
44
55
  const batches: D1Sql[][] = []; let pending: D1Sql[] = [];
45
56
  const packed = (members: D1Sql[]) => packGuards([...prefix, ...members], true);
46
57
  for (const member of [...guards, ...erasedIndexGuards(scope.plan)]) {
@@ -61,4 +72,5 @@ export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[
61
72
  if (rows.length !== 1 || rows[0].ok !== 1) throw new Error('IMPORT_ATOMIC_GUARD');
62
73
  }
63
74
  }
75
+ return prefix;
64
76
  }
@@ -14,6 +14,7 @@ import { noSuccessors, restorePredecessor } from './history.ts';
14
14
  import { readOriginals } from './originals.ts';
15
15
  import { reversalState } from './undo-state.ts';
16
16
  import { preserveReverse } from './preserved-reverse.ts';
17
+ import { compactCustodyGuards } from './read-fence.ts';
17
18
  import { fencedReversal, recordReversalFence, reversalProgress } from './reversal-fence.ts';
18
19
 
19
20
  export async function reverseD1Import(id: string, action: 'undo' | 'rollback', context: D1ImportContext): Promise<ImportJob> {
@@ -32,7 +33,9 @@ export async function reverseD1Import(id: string, action: 'undo' | 'rollback', c
32
33
  if ((action === 'undo' && current.job.state === 'rolled_back') || (action === 'rollback' && current.job.state === 'undone')) throw new Error('REVERSAL_MODE_CONFLICT');
33
34
  if (['undone', 'rolled_back'].includes(current.job.state)) {
34
35
  await readOriginals(current.plan, id, context);
35
- await atomic(context, action, [...boundary(current.job, current.plan, action), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context)], 0, nativeStagingScope(current.job,current.plan));
36
+ const scope = nativeStagingScope(current.job, current.plan);
37
+ const guards = [...boundary(current.job, current.plan, action), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context)];
38
+ await atomic(context, action, reversalState(current.job).policy === 'preserve-conflicts' ? await compactCustodyGuards(context, guards, scope, action) : guards, 0, scope);
36
39
  return current.job;
37
40
  }
38
41
  if (current.job.state === (action === 'undo' ? 'rolling_back' : 'undoing')) throw new Error('REVERSAL_MODE_CONFLICT');
@@ -4,6 +4,7 @@ import type { D1Database, D1Sql } from './types.ts';
4
4
  import { guard, sql } from './database.ts';
5
5
  import { retainedSql } from './retained-values.ts';
6
6
  import { changedLazy, putRow } from './expected-state.ts';
7
+ import { snapshotText, snapshotTextRows, snapshotTextSaves } from './snapshot-text.ts';
7
8
 
8
9
  // JSON snapshots are split by UTF-8 size, never by source count alone. Each
9
10
  // statement compares the original column values, not their recorded hashes.
@@ -59,6 +60,13 @@ export function exactRows(table: string, columns: string[], rows: any[], scope?:
59
60
  } else ordinary.push(picked);
60
61
  }
61
62
  for (const chunk of jsonChunks(ordinary, maxBytes)) {
63
+ const encoded = snapshotText(JSON.parse(chunk).map((row: any) => columns.map(column => row[column])));
64
+ if (snapshotTextSaves(chunk, encoded)) {
65
+ const parts = [snapshotTextRows.slice(0, snapshotTextRows.indexOf('?')), snapshotTextRows.slice(snapshotTextRows.indexOf('?') + 1) + ` WHERE NOT EXISTS(SELECT 1 FROM ${table} t WHERE ${columns.map((c, i) => `t.${c} IS json_extract(e.value,'$[${i}]')`).join(' AND ')})`];
66
+ parts[0] = 'NOT EXISTS(' + parts[0]; parts[1] += ')';
67
+ result.push(guard(retain ? retainedSql(parts, [encoded], [0]) : sql(parts.join('?'), encoded)));
68
+ continue;
69
+ }
62
70
  const parts=['NOT EXISTS(SELECT 1 FROM json_each(',`) e WHERE NOT EXISTS(SELECT 1 FROM ${table} t WHERE ${columns.map(c => `t.${c} IS json_extract(e.value,'$.${c}')`).join(' AND ')}))`];
63
71
  result.push(guard(retain?retainedSql(parts,[chunk],[0]):sql(parts.join('?'),chunk)));
64
72
  }
@@ -0,0 +1,68 @@
1
+ import { Buffer } from 'node:buffer';
2
+
3
+ type Piece = string | { value: string };
4
+
5
+ /** Split JSON text at value tokens, retaining its original lexical bytes.
6
+ * Keys, whitespace, number spelling and escaping are never reserialized. */
7
+ function textPieces(text: string): Piece[] {
8
+ try { JSON.parse(text); } catch { return [{ value: JSON.stringify(text) }]; }
9
+ const pieces: Piece[] = ['"'];
10
+ const escape = (value: string) => JSON.stringify(value).slice(1, -1);
11
+ let position = 0;
12
+ for (const match of text.matchAll(/"(?:\\.|[^"\\])*"|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g)) {
13
+ const end = match.index + match[0].length;
14
+ if (/^\s*:/.test(text.slice(end))) continue;
15
+ pieces.push(escape(text.slice(position, match.index)), { value: escape(match[0]) });
16
+ position = end;
17
+ }
18
+ pieces.push(escape(text.slice(position)), '"');
19
+ return pieces;
20
+ }
21
+
22
+ /** A chunk-local template dictionary for exact row snapshots. Repeated JSON
23
+ * structure and identical evidence travel once; unique values remain literal
24
+ * text slices. SQLite concatenates those slices, never interprets them as SQL
25
+ * or regenerates the JSON whose exact bytes the guard must compare. */
26
+ export function snapshotText(rows: unknown[][]): string {
27
+ const pieces = rows.map(row => row.flatMap((value, index): Piece[] => [
28
+ index ? ',' : '[',
29
+ ...(typeof value === 'string' && /^\s*[\[{]/.test(value) ? textPieces(value) : [{ value: JSON.stringify(value) ?? 'null' }]),
30
+ ...index === row.length - 1 ? [']'] : [],
31
+ ]));
32
+ const counts = new Map<string, number>();
33
+ for (const row of pieces) for (const piece of row) if (typeof piece !== 'string') counts.set(piece.value, (counts.get(piece.value) ?? 0) + 1);
34
+ const templates: (string | number)[][] = [], known = new Map<string, number>();
35
+ const encoded = pieces.map(row => {
36
+ const template: (string | number)[] = [], values: string[] = [], slots = new Map<string, number>();
37
+ const literal = (text: string) => {
38
+ if (typeof template.at(-1) === 'string') template[template.length - 1] = String(template.at(-1)) + text;
39
+ else template.push(text);
40
+ };
41
+ for (const piece of row) {
42
+ if (typeof piece === 'string') literal(piece);
43
+ else if (counts.get(piece.value)! > rows.length / 2) literal(piece.value);
44
+ else {
45
+ let slot = slots.get(piece.value);
46
+ if (slot === undefined) { slot = values.length + 1; slots.set(piece.value, slot); values.push(piece.value); }
47
+ template.push(slot);
48
+ }
49
+ }
50
+ const key = JSON.stringify(template);
51
+ let index = known.get(key);
52
+ if (index === undefined) { index = templates.length; known.set(key, index); templates.push(template); }
53
+ return [index, ...values];
54
+ });
55
+ return JSON.stringify([templates, encoded]);
56
+ }
57
+
58
+ export const snapshotTextRows = `WITH snapshot(body) AS (SELECT ?), decoded(value) AS MATERIALIZED (
59
+ SELECT (SELECT group_concat(piece,'') FROM (
60
+ SELECT CASE WHEN p.type='integer' THEN json_extract(r.value,'$['||p.value||']') ELSE p.value END AS piece
61
+ FROM json_each(snapshot.body,'$[0]['||json_extract(r.value,'$[0]')||']') p ORDER BY CAST(p.key AS INTEGER)
62
+ )) FROM snapshot,json_each(snapshot.body,'$[1]') r
63
+ ) SELECT 1 FROM decoded e`;
64
+
65
+ // Include JSON escaping in the choice, as the binding itself is a JSON string
66
+ // in D1's request. Small snapshots keep the simpler direct representation.
67
+ export const snapshotTextSaves = (plain: string, encoded: string): boolean =>
68
+ Buffer.byteLength(JSON.stringify(encoded)) + Buffer.byteLength(snapshotTextRows) < Buffer.byteLength(JSON.stringify(plain));
@@ -7,6 +7,7 @@ import { custodyGuards, inventoryGuards } from './integrity.ts';
7
7
  import { readOriginals } from './originals.ts';
8
8
  import { reversalState } from './undo-state.ts';
9
9
  import { nativeStagingScope } from './native-staging-context.ts';
10
+ import { compactCustodyGuards } from './read-fence.ts';
10
11
 
11
12
  export async function readD1ImportUndoReport(id: string, context: D1ImportContext, page: { offset?: number; limit?: number } = {}): Promise<D1UndoReport> {
12
13
  const offset = page.offset ?? 0, limit = page.limit ?? 100;
@@ -15,7 +16,9 @@ export async function readD1ImportUndoReport(id: string, context: D1ImportContex
15
16
  await authorize(context, 'read');
16
17
  const { job, plan } = await stored(id, context);
17
18
  await readOriginals(plan, id, context);
18
- await readAtomic(context, [...boundary(job, plan), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], nativeStagingScope(job, plan));
19
+ const scope = nativeStagingScope(job, plan);
20
+ const guards = await compactCustodyGuards(context, [...boundary(job, plan), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], scope, 'read');
21
+ await readAtomic(context, guards, scope);
19
22
  const state = reversalState(job);
20
23
  const outcome = !['undone','rolled_back'].includes(job.state) ? 'pending' : state.dispositions.some(d => d.disposition === 'retained') ? 'compensated' : 'undone';
21
24
  return { job, policy: state.policy, cursor: state.cursor, total: state.dispositions.length, nextOffset: offset + limit < state.dispositions.length ? offset + limit : null, dispositions: state.dispositions.slice(offset, offset + limit), outcome, ...(state.conversion ? { conversion: state.conversion } : {}) };
@@ -32,10 +32,15 @@ Owner history and undo must include jobs from either authority.
32
32
  Unadapted recorded dependencies advance together. If a dependency is marked
33
33
  adapted or is unrecorded seed source, refresh preserves it and refuses before
34
34
  writing when an incoming module needs an export it lacks. Use the printed
35
- comparison command, merge the required exports while preserving seed edits,
36
- and remove the comparison file. For a recorded adaptation, run the printed
37
- `adapt-base` acknowledgement after the merge, then repeat refresh. Acknowledging
38
- the old bytes alone does not resolve a missing export.
35
+ comparison command and save the seed file plus its original Base merge
36
+ ancestor. Restore the retained file from the incoming Base copy and
37
+ `record-base` that file at the pin which imports it. Add all named packages
38
+ at Base's listed versions with the seed's package manager, then refresh to
39
+ deliver the new Base dependencies. Only after that refresh, use
40
+ `git merge-file` to merge the saved seed edits onto the new Base file,
41
+ resolve conflicts, run `adapt-base` on the reconciled file, and refresh
42
+ again. Remove the comparison and backup files after reconciliation.
43
+ Acknowledging the old bytes alone does not resolve a missing export.
39
44
  Apply these in new numbered seed migrations, in this order,
40
45
  skipping any the seed already applied:
41
46
  - the D1 kit schemas, in the order `initializeD1ImportStorage` in
@@ -608,7 +608,30 @@ of bounded deterministic seeded rows. Volatile seeded rows cannot be vouched for
608
608
  their values are never bundled. Runtime policy projects each inventory through
609
609
  the same schema identity code as live SQLite. The Worker adapter binds evidence
610
610
  to the live hashed ledger and complete schema, then supplies the existing legacy
611
- collector, archived-ledger validation, bridge and restore guards. Archive SQL is
611
+ collector, archived-ledger validation, bridge and restore guards.
612
+
613
+ The same explicit build-evidence adoption supports native Wrangler's plain
614
+ `d1_migrations` ledger. Keep the reviewed, unchanged applied migration files in
615
+ the binding's declared directory when building. Every live name must select the
616
+ exact ordered prefix of those files, and replay of that prefix must reproduce
617
+ the complete live schema, including indexes and triggers on excluded tables.
618
+ Unknown names, gaps, reordered names, tracked but missing hashes, and schema
619
+ drift refuse before restore writes. Existing hashes must still match; the
620
+ Node automatic checkout path still requires hashed history. This binds the
621
+ reviewed build to the verified current installation; it does not attest bytes
622
+ Wrangler never recorded. Do not adopt an installation whose applied source
623
+ cannot be established from its retained checkout/build provenance. No ledger
624
+ row, ID, timestamp or schema is changed, no historical hash is backfilled, and
625
+ no archive ancestry is inferred. The archive still needs its own authenticated
626
+ recorded history and schema proof through the existing collector. Native
627
+ seed adapters that normalize an older product format remain responsible for
628
+ that format's existing validation. `restorableManifest` preserves a recognized
629
+ raw table-hash representation carrying an object inventory when projecting out
630
+ bookkeeping so those native
631
+ adapters can recognize their retained formats. Base's importer requests the
632
+ complete canonical identity independently; it never uses that raw projection as
633
+ schema compatibility evidence. Older inventory-free archives retain their
634
+ existing projection and verified legacy-history path. Archive SQL is
612
635
  never executed as evidence. Unsupported migration selections and missing evidence
613
636
  fail closed with `schema_mismatch:legacy_evidence` (the public HTTP vocabulary
614
637
  reports `schema_mismatch`). The ordinary automatic Node library path is unchanged.
@@ -616,7 +639,10 @@ reports `schema_mismatch`). The ordinary automatic Node library path is unchange
616
639
  The plugin runs for direct Vite builds and managed builds, rejects client imports,
617
640
  and puts its module in the existing Worker bundle/package path. Seeds that do not
618
641
  declare it generate no evidence module and need no migration or configuration
619
- change. Keep the same service instance throughout the initial legacy upload.
642
+ change. Refresh installed helpers and any copied portability runtime together;
643
+ constructor-issued refusals retain their identity across those module copies,
644
+ while arbitrary errors are not promoted to `PortabilityError`. Keep the same
645
+ service instance throughout the initial legacy upload.
620
646
 
621
647
  Later data statements also qualify when a surviving trigger from that same
622
648
  migration repeats the complete statement block, in order, on Base's restore
@@ -1,4 +1,4 @@
1
- import { schemaIdentityOf } from './schema-identity.mjs';
1
+ import { schemaIdentityOf, rawSchemaIdentityOf } from './schema-identity.mjs';
2
2
  import { insist } from './storage.mjs';
3
3
 
4
4
  // Installation authority and transient working state, never product data.
@@ -10,21 +10,25 @@ export const platformTables=Object.freeze([
10
10
  'rer_mail_development_outbox','rer_ai_development_outbox',
11
11
  'rer_dependency_keys','rer_dependency_pending','rer_dependency_codes',
12
12
  'rer_command_receipts','rer_erasure_blobs','rer_erasure_guard',
13
- 'rer_erasure_scope','rer_erasure_rates','rer_demo',
13
+ 'rer_erasure_scope','rer_erasure_rates','rer_demo','agent_door_replay_aliases',
14
14
  ]);
15
15
  export const identifier=value=>value.replace(/[A-Z]/g,letter=>letter.toLowerCase());
16
16
  export const matches=(table,pattern)=>pattern.endsWith('%') ? identifier(table).startsWith(identifier(pattern.slice(0,-1))) : identifier(table)===identifier(pattern);
17
17
  export const platformTable=table=>platformTables.some(pattern=>matches(table,pattern));
18
18
  // Compatibility uses the product schema, while the original manifest and parts
19
19
  // remain intact for end-to-end archive verification, including skipped rows.
20
- export async function restorableManifest(manifest) {
20
+ // Native adapters can recognize a table-hash format carrying objects after bookkeeping is
21
+ // removed. Retain that wire representation here; Base's validator explicitly
22
+ // requests the complete canonical identity before deciding compatibility.
23
+ export async function restorableManifest(manifest,{canonical=false}={}) {
21
24
  // Successful import-kit units always clear their CHECK guard rows before
22
25
  // committing. An old archive may carry the empty table, never its rows.
23
26
  for(const table of manifest.tables)if(identifier(table.name)==='ik_d1_assertions')
24
27
  insist(table.count===0,'invalid_bookkeeping_rows:'+table.name,409);
25
28
  const tables=manifest.tables.filter(table=>!platformTable(table.name));
26
29
  const schemaObjects=manifest.schemaObjects?.filter(object=>!manifest.tables.some(table=>platformTable(table.name) && table.name===object.table));
27
- return {...manifest,tables,...(schemaObjects?{schemaObjects}:{}),schema:await schemaIdentityOf(tables,schemaObjects),
30
+ const raw=!canonical && manifest.schemaObjects!==undefined && manifest.schema===await rawSchemaIdentityOf(manifest.tables);
31
+ return {...manifest,tables,...(schemaObjects?{schemaObjects}:{}),schema:await (raw?rawSchemaIdentityOf(tables):schemaIdentityOf(tables,schemaObjects)),
28
32
  excludedColumns:Object.fromEntries(Object.entries(manifest.excludedColumns).filter(([name])=>!platformTable(name)))};
29
33
  }
30
34
  export const skippedTablesLine=tables=>'Skipped platform bookkeeping tables: '+tables.join(', ')+'.';
@@ -1,12 +1,18 @@
1
1
  import { catalogue, policy } from './catalogue.mjs';
2
- import { destinationLedger, matchMigrationLedger } from './ledgers.mjs';
2
+ import { readLedger, matchMigrationLedger } from './ledgers.mjs';
3
3
  import { insist } from './storage.mjs';
4
4
 
5
- // Both evidence adapters bind the seed's files to the live hashed history and
6
- // complete schema before the shared legacy collector can accept an upload.
7
- export async function checkoutHistory(db,input,files,replayFor) {
8
- const ledger=await destinationLedger(db)??[];
9
- insist(ledger.length && ledger.every(row=>row.sha256),'schema_mismatch:legacy_evidence',409);
5
+ // Both evidence adapters bind the seed's files to live history and the complete
6
+ // schema. Only an explicitly bundled Worker may bind plain Wrangler history:
7
+ // literal names must select an ordered checkout prefix, never aliases or gaps.
8
+ export async function checkoutHistory(db,input,files,replayFor,{nativeWrangler=false}={}) {
9
+ const history=await readLedger(db),ledger=history?.entries??[];
10
+ insist(ledger.length,'schema_mismatch:legacy_evidence',409);
11
+ if(!ledger.every(row=>row.sha256)) {
12
+ insist(nativeWrangler && history.table==='d1_migrations'
13
+ && ledger.every((row,i)=>row.sha256===undefined && row.name===files[i]?.name),
14
+ 'schema_mismatch:legacy_evidence',409);
15
+ }
10
16
  const applied=matchMigrationLedger(ledger,files);
11
17
  const replay=await replayFor(applied);
12
18
  insist(await replay.schemaAfter(replay.names)===(await catalogue(db,policy(input))).identity,'schema_mismatch');
@@ -28,7 +28,7 @@ export async function createImport(context,manifest,actor='direct',legacyRecover
28
28
  insist(manifest.product===config.product,'wrong_product');
29
29
  // Explicit schema upgrades can reconcile rows, but cannot attest an interrupted deployment.
30
30
  await appliedMigrations(db);
31
- const original=manifest;manifest=await restorableManifest(original);
31
+ const original=manifest;manifest=await restorableManifest(original,{canonical:true});
32
32
  const skippedTables=original.tables.filter(table=>platformTable(table.name)).map(table=>table.name);
33
33
  const cat=await catalogue(db,config),upgrade=config.acceptedSchemas[original.schema];
34
34
  manifest=await withoutAbsentExcludedObjects(manifest,cat,config);
@@ -1,8 +1,14 @@
1
1
  import { canonicalJson } from './hash.mjs';
2
- const errorCodes = new WeakMap();
2
+ // Installed helpers and copied runtimes can coexist in one Worker. Share only
3
+ // constructor-issued identities, so arbitrary objects with code/status fields
4
+ // never become PortabilityError instances across that boundary.
5
+ const registry = Symbol.for('runeditrun.portability.errors.v1');
6
+ if (!Object.hasOwn(globalThis,registry)) Object.defineProperty(globalThis,registry,{value:new WeakMap()});
7
+ const errorCodes = globalThis[registry];
3
8
  // A verifier's original machine code remains independent of mutable properties.
4
9
  export const portabilityErrorCode = error => errorCodes.get(error);
5
10
  export class PortabilityError extends Error {
11
+ static [Symbol.hasInstance](error) { return errorCodes.has(error); }
6
12
  constructor(code, status=400) { super(code); this.code=code; this.status=status; errorCodes.set(this,code); }
7
13
  }
8
14
  export function insist(condition, code, status=400) { if (!condition) throw new PortabilityError(code,status); }