@runeditrun/rer 0.2.93 → 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 (79) hide show
  1. package/README.md +3 -0
  2. package/cli/README.md +3 -0
  3. package/cli/agent-door.mjs +20 -10
  4. package/cli/agent-history-database.mjs +82 -0
  5. package/cli/agent-history-isolation.mjs +68 -0
  6. package/cli/agent-history.mjs +58 -0
  7. package/cli/agent-journey-fixtures.mjs +3 -3
  8. package/cli/agent-network.mjs +12 -6
  9. package/cli/authoring.mjs +1 -1
  10. package/cli/business/carry-files.mjs +115 -0
  11. package/cli/business/carry.mjs +20 -81
  12. package/cli/business/choices.mjs +12 -7
  13. package/cli/business/command.mjs +19 -0
  14. package/cli/business/setup-command.mjs +14 -1
  15. package/cli/contract.mjs +1 -1
  16. package/cli/door-clauses.mjs +28 -2
  17. package/cli/export.mjs +1 -1
  18. package/cli/hosted-hosts.mjs +21 -0
  19. package/cli/hosted-output.mjs +93 -8
  20. package/cli/hosted-queue-producers.mjs +43 -0
  21. package/cli/jsonc.mjs +1 -115
  22. package/cli/lifecycle.mjs +5 -0
  23. package/cli/local.mjs +1 -1
  24. package/cli/parse.mjs +1 -1
  25. package/cli/port.mjs +1 -2
  26. package/cli/portability-evidence.d.mts +5 -0
  27. package/cli/portability-evidence.mjs +85 -0
  28. package/cli/portability-migrations.mjs +3 -5
  29. package/edition-authoring/archive-source.mjs +86 -1
  30. package/edition-authoring/base-copies.mjs +15 -45
  31. package/edition-authoring/cli.mjs +1 -1
  32. package/edition-authoring/edition-migrations.mjs +2 -2
  33. package/edition-authoring/module-closure.mjs +119 -50
  34. package/edition-authoring/module-edges.mjs +14 -1
  35. package/edition-authoring/module-packages.mjs +12 -5
  36. package/edition-authoring/module-syntax.mjs +13 -50
  37. package/edition-authoring/runtime-dependencies.mjs +4 -4
  38. package/foundation/runtime/contract-anchor.mjs +87 -0
  39. package/foundation/runtime/contract-docs.mjs +20 -4
  40. package/global/CHANGELOG.md +74 -0
  41. package/global/README.md +3 -0
  42. package/install.sh +4 -1
  43. package/modules/agent-door/CLAUSES.md +12 -0
  44. package/modules/agent-door/conformance/audit.mjs +22 -4
  45. package/modules/agent-door/conformance/discovery.mjs +32 -1
  46. package/modules/agent-door/conformance/erasure.mjs +103 -0
  47. package/modules/agent-door/conformance/injection.mjs +5 -3
  48. package/modules/agent-door/conformance/journey.mjs +20 -2
  49. package/modules/agent-door/conformance/policy-phases.mjs +6 -0
  50. package/modules/agent-door/conformance/protocol.mjs +13 -3
  51. package/modules/deploy-kit/README.md +1 -1
  52. package/modules/deploy-kit/jsonc.mjs +115 -0
  53. package/modules/deploy-kit/migration-directory.mjs +16 -0
  54. package/modules/deploy-kit/prepare.mjs +5 -2
  55. package/modules/deploy-kit/templates.mjs +9 -4
  56. package/modules/erasure/cascade.mjs +4 -4
  57. package/modules/erasure/index.d.mts +1 -1
  58. package/modules/erasure/index.mjs +1 -1
  59. package/modules/import-kit/src/d1/README.md +16 -1
  60. package/modules/import-kit/src/d1/preserved-reverse.ts +5 -2
  61. package/modules/import-kit/src/d1/read-fence.ts +14 -2
  62. package/modules/import-kit/src/d1/reverse.ts +4 -1
  63. package/modules/import-kit/src/d1/sets.ts +8 -0
  64. package/modules/import-kit/src/d1/snapshot-text.ts +68 -0
  65. package/modules/import-kit/src/d1/undo-report.ts +4 -1
  66. package/modules/import-kit/src/door/ADOPTING.md +9 -4
  67. package/modules/portability/README.md +68 -10
  68. package/modules/portability/bookkeeping.mjs +8 -4
  69. package/modules/portability/catalogue.mjs +11 -1
  70. package/modules/portability/checkout-history.mjs +20 -0
  71. package/modules/portability/import.mjs +1 -1
  72. package/modules/portability/index.d.mts +1 -1
  73. package/modules/portability/library-history.mjs +2 -7
  74. package/modules/portability/restore-safety.mjs +6 -3
  75. package/modules/portability/storage.mjs +7 -1
  76. package/modules/portability/worker-history.d.mts +9 -0
  77. package/modules/portability/worker-history.mjs +29 -0
  78. package/onboarding/adapter.mjs +25 -13
  79. package/package.json +9 -1
@@ -65,6 +65,10 @@ export function excludedTable(table,input={}) {
65
65
  async function archived(db,config) {
66
66
  const objects=await all(db,"SELECT name,sql,type,tbl_name FROM sqlite_master WHERE sql IS NOT NULL AND substr(lower(name),1,7) <> 'sqlite_' AND substr(lower(name),1,4) <> '_cf_' ORDER BY name");
67
67
  const kinds=await all(db,'PRAGMA table_list');
68
+ return archivedInventory({objects,kinds},config);
69
+ }
70
+ function archivedInventory({objects:input,kinds},config) {
71
+ const objects=[...input];
68
72
  const derived=config.derivedTables;
69
73
  const excluded=objects.filter(x=>x.type==='table' && (excludedTable(x.name,config) || (!config.includeTables.includes(x.name) &&
70
74
  derived.some(p=>x.name===p || (kinds.find(k=>k.name===x.name)?.type==='shadow' && x.name.startsWith(p+'_'))))));
@@ -76,7 +80,13 @@ async function archived(db,config) {
76
80
  }
77
81
  // The archive schema identity alone, for databases that need not satisfy the policy.
78
82
  export async function schemaIdentity(db,config,{legacyExcludedObjects=false,tablesOnly=false}={}) {
79
- const {entries,excluded,schemaObjects}=await archived(db,config);
83
+ return inventoryIdentity(await archived(db,config),{legacyExcludedObjects,tablesOnly});
84
+ }
85
+ // Build evidence and live SQLite use exactly the same policy projection.
86
+ export function inventorySchemaIdentity(inventory,config,options) {
87
+ return inventoryIdentity(archivedInventory(inventory,config),options);
88
+ }
89
+ function inventoryIdentity({entries,excluded,schemaObjects},{legacyExcludedObjects=false,tablesOnly=false}={}) {
80
90
  // Acknowledged v1 recovery places an old backup using its historical
81
91
  // projection; acknowledged recovery of a version 2 archive without an object
82
92
  // inventory compares tables only. Current archives always retain the
@@ -0,0 +1,20 @@
1
+ import { catalogue, policy } from './catalogue.mjs';
2
+ import { readLedger, matchMigrationLedger } from './ledgers.mjs';
3
+ import { insist } from './storage.mjs';
4
+
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
+ }
16
+ const applied=matchMigrationLedger(ledger,files);
17
+ const replay=await replayFor(applied);
18
+ insist(await replay.schemaAfter(replay.names)===(await catalogue(db,policy(input))).identity,'schema_mismatch');
19
+ return replay;
20
+ }
@@ -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);
@@ -44,7 +44,7 @@ export interface Status {schemaUnverified?:SchemaUnverifiedReceipt;id:string;kin
44
44
  /** Later additive migrations an older archive was restored across. */
45
45
  export interface Bridge {fromSchema:string;toSchema:string;migrations:string[];tables:string[];columns:Record<string,string[]>;/** Later backfills of tables the archive lacks, replayed at finalize in this order; rows is null until the apply commits. */replayed?:{migration:string;statement:number;table:string;rows:number|null}[]}
46
46
  /** A runner's replay of the destination's applied migrations: the archive schema identity after a chosen set, and each migration's statements. */
47
- export interface MigrationReplay {names:string[];/** Seed-declared data-only Platform entries without migration files. */dataOnly?:string[];/** Names generated by the seed's deployment specification for this file. */aliases?(name:string):string[]|undefined;statements(name:string):string[];/** The migration file's SHA-256 hex, where the runner read the file. */sha256?(name:string):string|undefined;schemaAfter(names:string[],options?:{tablesOnly?:boolean}):Promise<string>}
47
+ export interface MigrationReplay {names:string[];/** Seed-declared data-only Platform entries without migration files. */dataOnly?:string[];/** Names generated by the seed's deployment specification for this file. */aliases?(name:string):string[]|undefined;statements(name:string):string[];/** The migration file's SHA-256 hex, where the runner read the file. */sha256?(name:string):string|undefined;schemaAfter(names:string[],options?:{tablesOnly?:boolean;legacyExcludedObjects?:boolean}):Promise<string>}
48
48
  /** Export job creation only; no record data, object keys or file paths. */
49
49
  export interface ExportEvent {id:string;/** Opaque caller id; the direct/CLI sentinel is normalized to null. */actor:string|null;scope:'installation'}
50
50
  export interface PortabilityOptions {
@@ -1,9 +1,8 @@
1
- import { catalogue, policy } from './catalogue.mjs';
2
1
  import { sha256 } from './hash.mjs';
3
- import { destinationLedger, matchMigrationLedger } from './ledgers.mjs';
4
2
  import { splitStatements } from './sql-text.mjs';
5
3
  import { insist, PortabilityError } from './storage.mjs';
6
4
  import { migrationReplay } from './migration-history.mjs';
5
+ import { checkoutHistory } from './checkout-history.mjs';
7
6
 
8
7
  // Node library callers have the same checkout evidence as the runner. Resolve
9
8
  // only its single declared D1 migration directory, never an archive-supplied path.
@@ -25,11 +24,7 @@ async function checkoutMigrations(db,config,root) {
25
24
  const bytes=await readFile(resolve(directory,name));
26
25
  return {name,sha256:await sha256(bytes),statements:splitStatements(bytes.toString('utf8'))};
27
26
  }));
28
- const ledger=await destinationLedger(db)??[];
29
- insist(ledger.length && ledger.every(row=>row.sha256),'schema_mismatch:legacy_evidence',409);
30
- const applied=matchMigrationLedger(ledger,files);
31
- const replay=migrationReplay(applied.map(name=>files.find(file=>file.name===name)),config);
32
- insist(await replay.schemaAfter(replay.names)===(await catalogue(db,policy(config))).identity,'schema_mismatch');
27
+ const replay=await checkoutHistory(db,config,files,applied=>migrationReplay(applied.map(name=>files.find(file=>file.name===name)),config));
33
28
  return async()=>replay;
34
29
  }
35
30
 
@@ -1,5 +1,5 @@
1
1
  import { all,statement,guard,quote,insist } from './storage.mjs';
2
- import { canonicalJson } from './hash.mjs';
2
+ import { canonicalJson, sha256 } from './hash.mjs';
3
3
  const identifier=value=>value.replace(/[A-Z]/g,c=>c.toLowerCase());
4
4
 
5
5
  export async function excludedDependentGuards(db,targets,replaceable) {
@@ -40,12 +40,15 @@ export async function migrationSeeded(db,names,migrations) {
40
40
  const requests=[];
41
41
  for(const name of names)requests.push({name,columns:(await all(db,`SELECT name FROM pragma_table_info(?)`,name)).map(c=>c.name)});
42
42
  // Without a usable replay no seed is vouched for, so those rows count as the installation's own.
43
- const expected=await migrations().then(replay=>replay.rowImages(requests,MAX_SEED_ROWS),()=>new Map()),seeded=[];
43
+ const replay=await migrations().catch(()=>null);
44
+ const hashed=Boolean(replay?.rowHashes);
45
+ const expected=replay?await (hashed?replay.rowHashes(requests,MAX_SEED_ROWS):replay.rowImages(requests,MAX_SEED_ROWS)):new Map(),seeded=[];
44
46
  for(const {name,columns} of requests) {
45
47
  const want=expected.get(name);
46
48
  if(!want || (await statement(db,`SELECT count(*) AS n FROM ${quote(name)}`).first()).n!==want.length)continue;
47
49
  const found=(await all(db,imageSql(name,columns))).map(r=>r.image).sort();
48
- if(canonicalJson(found)===canonicalJson(want))seeded.push({name,columns,images:found});
50
+ const identities=hashed?(await Promise.all(found.map(sha256))).sort():found;
51
+ if(canonicalJson(identities)===canonicalJson(want))seeded.push({name,columns,images:found});
49
52
  }
50
53
  return seeded;
51
54
  }
@@ -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); }
@@ -0,0 +1,9 @@
1
+ import type { ArchivePolicy, MigrationReplay } from './index.mjs';
2
+ export interface MigrationEvidence {
3
+ version:1;
4
+ files:{name:string;sha256:string;statements:string[]}[];
5
+ objects:{name:string;sql:string;type:string;tbl_name:string}[];
6
+ prefixes:{objects:number[];shadows:string[];seeds:{name:string;columns:string[];hashes:string[]}[]}[];
7
+ }
8
+ /** Return a replay bound to the destination's hashed history or exact native Wrangler prefix, and complete schema. */
9
+ export function workerMigrations(db:D1Database,policy:ArchivePolicy,evidence:MigrationEvidence):Promise<MigrationReplay>;
@@ -0,0 +1,29 @@
1
+ import { policy, inventorySchemaIdentity } from './catalogue.mjs';
2
+ import { canonicalJson } from './hash.mjs';
3
+ import { checkoutHistory } from './checkout-history.mjs';
4
+ import { insist, PortabilityError } from './storage.mjs';
5
+
6
+ // This adapter supplies replay evidence, never archive validation. The same
7
+ // collector, ledger matching, schema bridge and restore guards serve both doors.
8
+ export async function workerMigrations(db,input,evidence) {
9
+ try {
10
+ insist(evidence?.version===1 && evidence.prefixes.length===evidence.files.length+1,'schema_mismatch:legacy_evidence',409);
11
+ const config=policy(input),files=evidence.files;
12
+ return await checkoutHistory(db,input,files,names=>{
13
+ const prefix=selected=>{
14
+ insist(selected.length<=names.length && canonicalJson(selected)===canonicalJson(files.slice(0,selected.length).map(f=>f.name)),'schema_mismatch:legacy_evidence',409);
15
+ const found=evidence.prefixes[selected.length];
16
+ insist(found,'schema_mismatch:legacy_evidence',409);return found;
17
+ };
18
+ const final=prefix(names);
19
+ return {names,statements:name=>files.find(f=>f.name===name)?.statements,sha256:name=>files.find(f=>f.name===name)?.sha256,
20
+ schemaAfter(selected,options){
21
+ const snapshot=prefix(selected);
22
+ return inventorySchemaIdentity({objects:snapshot.objects.map(i=>evidence.objects[i]),kinds:snapshot.shadows.map(name=>({name,type:'shadow'}))},config,options);
23
+ },
24
+ async rowImages(requests){return new Map(requests.filter(r=>final.seeds.some(s=>s.name===r.name && !s.hashes.length)).map(r=>[r.name,[]]));},
25
+ async rowHashes(requests,limit){return new Map(requests.flatMap(r=>{const found=final.seeds.find(s=>s.name===r.name && s.hashes.length<=limit && canonicalJson(s.columns)===canonicalJson(r.columns));return found?[[r.name,found.hashes]]:[];}));},
26
+ };
27
+ },{nativeWrangler:true});
28
+ }catch(error){if(error instanceof PortabilityError)throw error;throw new PortabilityError('schema_mismatch:legacy_evidence',409);}
29
+ }
@@ -10,8 +10,8 @@ import { credentialValues } from './redaction.mjs';
10
10
  import { dependencyEnvironment } from './dependency-environment.mjs';
11
11
  import { labels } from './adapter-labels.mjs';
12
12
  import { chooseOrigin, prepareSettings, readSettings } from './adapter-settings.mjs';
13
- import {listenerPorts,listenerOwnership,inspectionContext} from './process-diagnostics.mjs';
14
- import { appFetch, loopbackRequest } from './app-fetch.mjs';
13
+ import {listenerPorts,listenerOwnership,processName,inspectionContext} from './process-diagnostics.mjs';
14
+ import { appFetch } from './app-fetch.mjs';
15
15
  import { owner, signIn } from './adapter-auth.mjs';
16
16
  import { installerDevKey } from './dev-identity.mjs';
17
17
  import { assertArchiveFree } from './archive-output.mjs';
@@ -220,7 +220,10 @@ async function start(context, hooks) {
220
220
  // credential prefix as private even when it happens to be an ordinary port.
221
221
  const mismatch=port=>({fatal:detectedMismatch=`the application listened on port ${port} but the CLI expected ${expectedPort} for the runtime listener`});
222
222
  const announced=message=>{if(message?.type==='rer-listening')announcement=message;};
223
- if(runtimePortHandshake)child.on('message',announced);
223
+ child.on('message',announced);
224
+ // Only explicit application bindings are candidates for a port mismatch. A
225
+ // descendant's open socket alone says nothing about its protocol or purpose.
226
+ const declaredPorts=new Set([spawnOptions.env?.RER_RUNTIME_PORT,...args.flatMap((arg,index)=>arg==='--port'?[args[index+1]]:arg.startsWith('--port=')?[arg.slice(7)]:[])].filter(value=>/^\d+$/.test(String(value))).map(Number).filter(port=>port>0&&port<=65535));
224
227
  context.app = child;
225
228
  await onApp(child);
226
229
  const url = new URL(health, context.origin).href;
@@ -243,16 +246,25 @@ async function start(context, hooks) {
243
246
  context.setRuntimePort(port);context.runtimePort=port;expectedPort=port;
244
247
  }
245
248
  }
246
- const result=await (ready??hooks.health??defaultHealth)(url,probeSignal,context);
249
+ let result;
250
+ try { result=await (ready??hooks.health??defaultHealth)(url,probeSignal,context); }
251
+ catch(error){if(probeSignal.aborted)throw error;result={ready:false,message:'Health probe failed'};}
247
252
  if(result===true||result?.ready===true)return result;
248
- // Auxiliary runtime listeners are common. Only an owned listener serving the
249
- // application's healthy response proves a mismatched application port.
250
- const ports=await listenerPorts(child.pid,inspectionContext({signal:probeSignal,budgetMs:3000}));
251
- if(!ports.includes(expectedPort))for(const port of ports){
252
- try{
253
- const reply=await loopbackRequest({port,path:health,headers:{host:new URL(url).host},signal:probeSignal,timeoutMs:1000});
254
- if(reply.status===200&&JSON.parse(reply.body.toString()).status==='ok')return mismatch(port);
255
- }catch(error){if(probeSignal.aborted)throw error;}
253
+ // Diagnose an explicit misbinding through socket custody, never HTTP. In
254
+ // particular, workerd's auxiliary RPC sockets must not receive /health while
255
+ // Vite is still starting. A runtime announcement can identify its application
256
+ // socket even when workerd itself serves it (the production target).
257
+ const candidates=[...declaredPorts].filter(port=>port!==expectedPort);
258
+ if(candidates.length){
259
+ const inspection=inspectionContext({signal:probeSignal,budgetMs:3000});
260
+ const ports=await listenerPorts(child.pid,inspection);
261
+ if(!ports.includes(expectedPort))for(const port of candidates.filter(port=>ports.includes(port))){
262
+ const ownership=await listenerOwnership(child.pid,port,'127.0.0.1',inspection);
263
+ if(ownership.status!=='owned')continue;
264
+ const name=await processName(ownership.pid,inspection);
265
+ if(/^workerd(?:[.-]|$)/i.test(name)&&!(announcement?.port===port&&announcement.requestedPort===port))continue;
266
+ return mismatch(port);
267
+ }
256
268
  }
257
269
  return result;
258
270
  }, { signal, timeoutMs: Math.min(timeoutMs??60000,60000), ...(softAfterMs ? { softAfterMs } : {}), ...(intervalMs ? { intervalMs } : {}) });
@@ -268,7 +280,7 @@ async function start(context, hooks) {
268
280
  store.report(message);const prior=approvedFailure(error);error=commandFailure(`${prior?.message??readinessLabel}\n${message}`,prior??{});
269
281
  }
270
282
  throw installationFailure('command_failed', { step: 'start', diagnostic: error });
271
- } finally { if(runtimePortHandshake)child.removeListener('message',announced); }
283
+ } finally { child.removeListener('message',announced); }
272
284
  // Another app can take the loopback port between the port check and the bind;
273
285
  // its /health would pass. Claim and sign in only against the process this
274
286
  // setup started.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.93",
3
+ "version": "0.2.95",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -12,6 +12,14 @@
12
12
  "./deploy": "./cli/deploy.mjs",
13
13
  "./portability": "./cli/portability.mjs",
14
14
  "./portability-migrations": "./cli/portability-migrations.mjs",
15
+ "./portability-evidence": {
16
+ "types": "./cli/portability-evidence.d.mts",
17
+ "default": "./cli/portability-evidence.mjs"
18
+ },
19
+ "./modules/portability/worker-history": {
20
+ "types": "./modules/portability/worker-history.d.mts",
21
+ "default": "./modules/portability/worker-history.mjs"
22
+ },
15
23
  "./errors": "./cli/errors.mjs",
16
24
  "./jsonc": "./cli/jsonc.mjs",
17
25
  "./parse": "./cli/parse.mjs",