@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
package/cli/jsonc.mjs CHANGED
@@ -1,115 +1 @@
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
- }
1
+ export {parseJsonc} from '../modules/deploy-kit/jsonc.mjs';
package/cli/lifecycle.mjs CHANGED
@@ -20,6 +20,11 @@ import { withoutLocalSettings, refuseLocalEnvOutput } from './local-env.mjs';
20
20
  import { announceDoorRoute } from './door-mode.mjs';
21
21
 
22
22
  export async function runCommand(project, options, out) {
23
+ if (project.profile === 'first-party' && ['build', 'dev', 'preview', 'deploy'].includes(options.verb)) {
24
+ const { readAgentSurface } = await import('./agent-surface.mjs');
25
+ const surface = await readAgentSurface(project.root);
26
+ if (surface?.surface.verification?.history !== undefined) await (await import('./agent-history.mjs')).historyDeclaration(project.root, surface.surface.verification.history);
27
+ }
23
28
  if (options.verb === 'deploy' && !options.site && !options.demo) {
24
29
  const { validateDeployOptions } = await import('./deploy-command.mjs');
25
30
  validateDeployOptions(options);
package/cli/local.mjs CHANGED
@@ -128,5 +128,5 @@ export async function refuseLegacyProcess(project,{reset=false,verb='dev',operat
128
128
  }
129
129
  return {lockProof:true};
130
130
  }
131
- if(conflict)throw refuse(`A local process holds ${conflict.reason}: PID ${conflict.pid} (${conflict.command}). Stop it with kill -TERM ${conflict.pid}, then retry. Use rer dev --stop or rer preview --stop for a registered server.`);
131
+ if(conflict)throw refuse(`A local process holds ${conflict.reason}: PID ${conflict.pid} (${conflict.command}). Close the program holding it, then retry.`);
132
132
  }
package/cli/parse.mjs CHANGED
@@ -3,7 +3,7 @@ import { authoringVerbs, commonFlags, serverDefaults, signinVerb, verbs } from '
3
3
  import { invalid, refuse } from './errors.mjs';
4
4
  import { isLoopbackHost } from '../modules/operator-auth/loopback.mjs';
5
5
  import { normalizeEmail } from '../modules/operator-auth/policy.mjs';
6
- const booleans = new Set(['--help', '--json', '--non-interactive', '--yes', '--dry-run', '--stop', '--detach', '--foreground', '--no-queue', '--login', '--legacy-v1', '--acknowledge-unverified-metadata', '--acknowledge-unverified-schema-objects', '--door', '--direct', '--site', '--remove', '--demo', '--offline']);
6
+ const booleans = new Set(['--help', '--json', '--non-interactive', '--yes', '--dry-run', '--stop', '--detach', '--foreground', '--no-queue', '--login', '--legacy-v1', '--acknowledge-unverified-metadata', '--acknowledge-unverified-schema-objects', '--door', '--direct', '--site', '--remove', '--demo', '--offline', '--from-api']);
7
7
  export const mailbox = value => typeof value === 'string' && value.length <= 254 && /^[a-zA-Z0-9.!+_%\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+$/.test(value);
8
8
  export const validOwnerName = value => typeof value === 'string' && Boolean(value.trim()) && value.length <= 200 && !/[\p{Cc}\p{Cf}]/u.test(value);
9
9
  // A loopback host: the shared predicate (including `*.localhost` door names),
package/cli/port.mjs CHANGED
@@ -53,6 +53,5 @@ export async function busyPortMessage({ port, host, root, saved, env = process.e
53
53
  const who = holder.server
54
54
  ? `the ${holder.server.mode} server for ${holder.checkout} (PID ${holder.pid})`
55
55
  : `PID ${holder.pid} (${holder.command})${holder.cwd ? ` running in ${holder.cwd}` : ''}`;
56
- const stop = holder.server ? `\nOr stop that server first: ${stopInstruction(holder.server)}` : `\nOr stop it with: kill -TERM ${holder.pid}`;
57
- return `${kind} in use by ${who}.${move}${stop}`;
56
+ return `${kind} in use by ${who}.${move}`;
58
57
  }
@@ -0,0 +1,5 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { MigrationEvidence } from '../modules/portability/worker-history.mjs';
3
+ export function generateMigrationEvidence(root:string,options?:{binding?:string}):Promise<MigrationEvidence>;
4
+ /** Seed opt-in: generates the server-only virtual:rer-legacy-migration-evidence module. */
5
+ export function legacyMigrationEvidence(options?:{binding?:string}):Plugin;
@@ -0,0 +1,85 @@
1
+ import { DatabaseSync } from 'node:sqlite';
2
+ import { realpath, readFile, readdir } from 'node:fs/promises';
3
+ import { resolve, relative, isAbsolute, sep } from 'node:path';
4
+ import { migrationDirectory } from './portability-migrations.mjs';
5
+ import { splitStatements } from '../modules/portability/sql-text.mjs';
6
+ import { canonicalJson, sha256 } from '../modules/portability/hash.mjs';
7
+ import { quote, insist } from '../modules/portability/storage.mjs';
8
+ import { dataStatement, triggerBodies } from '../modules/portability/migration-statements.mjs';
9
+ import { sqliteTokens } from '../modules/portability/sqlite-tokens.mjs';
10
+
11
+ // Date functions can default to 'now'; even fixed arguments are conservatively
12
+ // excluded from seeded-row evidence. Their schema and statements remain intact.
13
+ const variable=sql=>/\b(?:random|randomblob|date|time|datetime|julianday|strftime|unixepoch|sqlite_version|sqlite_source_id)\s*\(|\bcurrent_(?:date|time|timestamp)\b/i.test(sqliteTokens(sql).map(t=>t.value).join(' '));
14
+
15
+ // Only migration-owned schema and hashed seeded-row identities enter the bundle.
16
+ // Never open a live database, settings file, archive, or archive-supplied path.
17
+ export async function generateMigrationEvidence(root,{binding='DB'}={}) {
18
+ root=await realpath(root);
19
+ const directory=await realpath(await migrationDirectory(root,binding));
20
+ const path=relative(root,directory);
21
+ insist(!isAbsolute(path) && path!=='..' && !path.startsWith('..'+sep),'schema_mismatch:legacy_evidence',409);
22
+ const files=await Promise.all((await readdir(directory)).filter(name=>name.endsWith('.sql')).sort().map(async name=>{
23
+ const file=await realpath(resolve(directory,name)),path=relative(directory,file);
24
+ insist(!isAbsolute(path) && path!=='..' && !path.startsWith('..'+sep),'schema_mismatch:legacy_evidence',409);
25
+ const bytes=await readFile(file);
26
+ return {name,sha256:await sha256(bytes),statements:splitStatements(bytes.toString('utf8'))};
27
+ }));
28
+ const objects=[],indices=new Map(),prefixes=[];
29
+ const scratch=new DatabaseSync(':memory:');
30
+ try {
31
+ // Volatile defaults, trigger bodies and reads propagate to their writers.
32
+ // Such rows cannot equal a fresh library replay reliably either. Preserve
33
+ // deterministic seeds in unrelated tables, and empty evidence everywhere.
34
+ const unstable=new Set(),applied=[];
35
+ const capture=async()=>{
36
+ const schema=scratch.prepare("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,type").all();
37
+ const refs=schema.map(object=>{
38
+ const key=canonicalJson(object);if(!indices.has(key)){indices.set(key,objects.length);objects.push(object);}return indices.get(key);
39
+ });
40
+ const shadows=scratch.prepare('PRAGMA table_list').all().filter(t=>t.type==='shadow').map(t=>t.name).sort();
41
+ const seeds=[];
42
+ for(const {name} of schema.filter(o=>o.type==='table')) {
43
+ const columns=scratch.prepare(`PRAGMA table_info(${quote(name)})`).all().map(c=>c.name);
44
+ const rows=scratch.prepare(`SELECT json_array(${columns.map(c=>`quote(${quote(c)})`).join(',')}) image FROM ${quote(name)} LIMIT 10001`).all().map(r=>r.image);
45
+ if(rows.length>10000 || unstable.has(name.toLowerCase()) && rows.length)continue;
46
+ seeds.push({name,columns,hashes:(await Promise.all(rows.map(sha256))).sort()});
47
+ }
48
+ prefixes.push({objects:refs,shadows,seeds});
49
+ };
50
+ await capture();
51
+ for(const file of files) {
52
+ for(const sql of file.statements) {
53
+ const data=dataStatement(sql);
54
+ if(data) {
55
+ const writers=new Set([data.target]);
56
+ for(const name of writers)for(const target of triggerBodies(applied,name))writers.add(target);
57
+ const definitions=scratch.prepare("SELECT name,sql FROM sqlite_master WHERE sql IS NOT NULL").all();
58
+ if(variable(sql) || data.names.some(n=>unstable.has(n)) || definitions.some(d=>writers.has(d.name.toLowerCase()) && variable(d.sql))
59
+ || applied.some(s=>variable(s) && [...writers].some(n=>triggerBodies([s],data.target).includes(n))))for(const name of writers)unstable.add(name);
60
+ }
61
+ scratch.exec(sql);
62
+ applied.push(sql);
63
+ }
64
+ await capture();
65
+ }
66
+ }finally{scratch.close();}
67
+ return {version:1,files,objects,prefixes};
68
+ }
69
+
70
+ // Explicit seed opt-in. The module is consumed only by the server/Worker graph;
71
+ // it is bundled and hashed by the existing Vite and deploy package paths.
72
+ export function legacyMigrationEvidence({binding='DB'}={}) {
73
+ const id='virtual:rer-legacy-migration-evidence',internal='\0'+id;
74
+ let root;
75
+ return {
76
+ name:'rer:legacy-migration-evidence',
77
+ configResolved(config){root=config.root;},
78
+ resolveId(source){if(source===id)return internal;},
79
+ async load(source){
80
+ if(source!==internal)return;
81
+ if(this.environment?.name==='client')throw new Error('Legacy migration evidence is server-only.');
82
+ return 'export default '+canonicalJson(await generateMigrationEvidence(root,{binding}))+';';
83
+ },
84
+ };
85
+ }
@@ -4,7 +4,8 @@ import { readFile, readdir } from 'node:fs/promises';
4
4
  import { migrationStatements } from '../modules/portability/migration-sql.mjs';
5
5
  import { destinationLedger, ledgerNames, matchMigrationLedger } from '../modules/portability/ledgers.mjs';
6
6
  import { all, insist, PortabilityError } from '../modules/portability/storage.mjs';
7
- import { parseJsonc } from './jsonc.mjs';
7
+ import {migrationDirectory} from '../modules/deploy-kit/migration-directory.mjs';
8
+ export {migrationDirectory};
8
9
 
9
10
  // Seed-owned declarations, never archive metadata: the deploymentSpec prefix
10
11
  // for this binding's directory, and data-only seeds without schema files.
@@ -19,10 +20,7 @@ export function platformMigrationDeclaration(input={}) {
19
20
  // Reproduce the runner's stored SQL text, then recover SQLite's statement
20
21
  // boundaries (migration-sql.mjs).
21
22
  export async function migrationInventory(root,binding,{prefix},split) {
22
- const config=parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8'));
23
- const bindings=config.d1_databases?.filter(entry=>entry.binding===binding) ?? [];
24
- insist(bindings.length===1,'migration_ledger:binding',409);
25
- const directory=join(root,bindings[0].migrations_dir ?? 'migrations');
23
+ const directory=await migrationDirectory(root,binding);
26
24
  return Promise.all((await readdir(directory)).filter(name=>name.endsWith('.sql')).sort().map(async name=>{
27
25
  const bytes=await readFile(join(directory,name));
28
26
  return {name,...(split?{statements:migrationStatements(bytes.toString('utf8'),split)}:{}),sha256:createHash('sha256').update(bytes).digest('hex'),...(prefix===undefined?{}:{aliases:[prefix+name]})};
@@ -1,11 +1,96 @@
1
- // A source archive supplies one caller-identified revision, never Git history.
1
+ // Directory archives supply one snapshot. GitHub can also prove ancestry and
2
+ // supply older immutable trees without requiring a local Git object store.
2
3
  import fs from 'node:fs';
3
4
  import path from 'node:path';
4
5
  import crypto from 'node:crypto';
6
+ import {execFileSync} from 'node:child_process';
5
7
  import {hash, safe} from './files.mjs';
6
8
 
7
9
  const limit = 64 * 1024 * 1024;
8
10
  const objectId = bytes => crypto.createHash('sha1').update(`blob ${bytes.length}\0`).update(bytes).digest('hex');
11
+ const commit = /^[a-f0-9]{40}$/;
12
+
13
+ export function apiReader(repository, revision) {
14
+ if (!commit.test(revision)) throw Error('Base API requires an immutable full 40-character commit');
15
+ const request = endpoint => {
16
+ try {
17
+ return JSON.parse(execFileSync('gh', ['api', `repos/${repository}/${endpoint}`], {
18
+ encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 60_000, maxBuffer: limit,
19
+ }));
20
+ } catch (error) {
21
+ // Keep provider failures explicit without echoing arbitrary gh stderr,
22
+ // authentication diagnostics or credential-bearing transport URLs.
23
+ const status = /\bHTTP (\d{3})\b/.exec(error.stderr?.toString() ?? '')?.[1];
24
+ throw Error(`Cannot read Base ${revision} from the GitHub API${status ? ` (HTTP ${status})` : ''} (${endpoint}); no files were refreshed or copy record changed.`);
25
+ }
26
+ };
27
+ const trees = new Map(), objects = new Map(), ancestry = new Map();
28
+ return {
29
+ // Workflow-template discovery remains snapshot-only; no path history is
30
+ // invented from the API tree or the archive's version.
31
+ historyAvailable: false,
32
+ objectId,
33
+ tree(pin) {
34
+ if (!commit.test(pin)) throw Error('Base API requires an immutable full 40-character commit');
35
+ if (!trees.has(pin)) {
36
+ const pinned = request(`git/commits/${pin}`);
37
+ if (pinned.sha !== pin || !commit.test(pinned.tree?.sha)) throw Error('Base API did not resolve the immutable commit');
38
+ const entries = new Map();
39
+ const walk = (identity, prefix = '', recursive = true) => {
40
+ const tree = request(`git/trees/${identity}${recursive ? '?recursive=1' : ''}`);
41
+ if (recursive && tree.sha === identity && tree.truncated === true) return walk(identity, prefix, false);
42
+ if (tree.sha !== identity || tree.truncated !== false || !Array.isArray(tree.tree)) throw Error('Incomplete Base API tree');
43
+ for (const entry of tree.tree) {
44
+ safe(entry.path);
45
+ if ((!recursive && entry.path.includes('/')) || !commit.test(entry.sha)) throw Error('Invalid Base API tree entry');
46
+ const name = prefix + entry.path;
47
+ if (!recursive && entry.type === 'tree' && entry.mode === '040000') walk(entry.sha, name + '/');
48
+ else if (entry.type === 'blob' && ['100644', '100755'].includes(entry.mode)) entries.set(name, {object: entry.sha, mode: entry.mode});
49
+ }
50
+ };
51
+ walk(pinned.tree.sha);
52
+ trees.set(pin, new Map([...entries].sort(([a], [b]) => Buffer.compare(Buffer.from(a), Buffer.from(b)))));
53
+ }
54
+ return trees.get(pin);
55
+ },
56
+ blobs(ids) {
57
+ const result = new Map();
58
+ let size = 0;
59
+ for (const object of new Set(ids)) {
60
+ if (!commit.test(object)) throw Error('Invalid Base API blob identity');
61
+ if (!objects.has(object)) {
62
+ const blob = request(`git/blobs/${object}`);
63
+ if (blob.sha !== object || blob.encoding !== 'base64' || typeof blob.content !== 'string') throw Error('Invalid Base API blob');
64
+ const bytes = Buffer.from(blob.content, 'base64');
65
+ if (blob.size !== bytes.length || objectId(bytes) !== object) throw Error('Base API blob integrity mismatch');
66
+ objects.set(object, bytes);
67
+ }
68
+ const bytes = objects.get(object);
69
+ size += bytes.length;
70
+ if (size > limit) throw Error('Base API source output exceeds the 64 MiB acquisition limit');
71
+ result.set(object, bytes);
72
+ }
73
+ return result;
74
+ },
75
+ digests(ids) { return new Map([...this.blobs(ids)].map(([object, bytes]) => [object, hash(bytes)])); },
76
+ contains(pin, introduced) {
77
+ if (!commit.test(pin) || !commit.test(introduced)) throw Error('Base API ancestry requires immutable full commits');
78
+ const key = introduced + '...' + pin;
79
+ if (!ancestry.has(key)) {
80
+ const comparison = request(`compare/${key}?per_page=1`);
81
+ if (comparison.base_commit?.sha !== introduced || !commit.test(comparison.merge_base_commit?.sha) ||
82
+ !['ahead', 'identical', 'behind', 'diverged'].includes(comparison.status)) throw Error('Invalid Base API ancestry comparison');
83
+ if (['ahead', 'identical'].includes(comparison.status) && comparison.merge_base_commit.sha !== introduced) throw Error('Invalid Base API ancestry proof');
84
+ ancestry.set(key, ['ahead', 'identical'].includes(comparison.status));
85
+ }
86
+ return ancestry.get(key);
87
+ },
88
+ history(pin, name) {
89
+ const entry = this.tree(pin).get(safe(name));
90
+ return entry ? [{revision: pin, ...entry}] : [];
91
+ },
92
+ };
93
+ }
9
94
 
10
95
  export function archiveReader(directory, revision) {
11
96
  if (!/^[a-f0-9]{40}$/.test(revision)) throw Error('A Base archive requires its immutable full 40-character Base commit in --ref');
@@ -17,7 +17,7 @@ import {requireMaterializedCopies, preflightRefresh} from './copy-checkout.mjs';
17
17
  import {completeModuleCopies} from './module-closure.mjs';
18
18
  import {editionMigrations} from './edition-migrations.mjs';
19
19
  import {adoptWorkflowTemplates, completeWorkflowHelpers} from './workflow-copies.mjs';
20
- import {archiveReader} from './archive-source.mjs';
20
+ import {archiveReader, apiReader} from './archive-source.mjs';
21
21
 
22
22
  export const RECORD = 'base-copies.json';
23
23
  const commit = /^[a-f0-9]{40}$/, sha256 = /^[a-f0-9]{64}$/;
@@ -94,50 +94,19 @@ export function options(args, count) {
94
94
  result.positional.push(result.ref);
95
95
  }
96
96
  if (result.base && result.baseArchive) throw Error('Choose either --base or --base-archive');
97
- if (result.positional.length !== count) throw Error('Usage: verify-base [--record <file>] [--base <checkout>] [--offline] | record-base <path> <origin-path> <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api|--offline --base <checkout>] | adapt-base <file> [--record <file>] | refresh-base <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>]');
98
- if (result.fromApi && count !== 3) throw Error('--from-api is only supported by record-base');
97
+ if (result.positional.length !== count) throw Error('Usage: verify-base [--record <file>] [--base <checkout>] [--offline] | record-base <path> <origin-path> <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api|--offline --base <checkout>] | adapt-base <file> [--record <file>] | refresh-base <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api]');
98
+ if (result.fromApi && ![1, 3].includes(count)) throw Error('--from-api is supported by record-base and refresh-base');
99
+ if (result.fromApi && (result.base || result.baseArchive || result.offline)) throw Error('--from-api cannot be combined with --base, --base-archive or --offline');
99
100
  safe(result.record);
100
101
  return result;
101
102
  }
102
103
 
103
- // Read immutable Git objects through GitHub, without cloning Base or trusting
104
- // an archive's filesystem modes. Refuse incomplete trees and verify blob IDs.
105
- function apiReader(record, root, revision) {
104
+ // Share the authenticated immutable API reader for recording and refreshing.
105
+ function apiSource(record, root, revision) {
106
106
  const declared = record.repository ?? baseRepository(root);
107
107
  const repository = /^(?:https:\/\/github\.com\/)?([\w.-]+\/[\w.-]+?)(?:\.git)?$/.exec(declared)?.[1];
108
108
  if (!repository) throw Error('--from-api requires a GitHub Base repository');
109
- const request = endpoint => {
110
- try {
111
- return JSON.parse(execFileSync('gh', ['api', `repos/${repository}/git/${endpoint}`], {
112
- encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 60_000, maxBuffer: 64 * 1024 * 1024,
113
- }));
114
- } catch { throw Error(`Cannot read immutable Base ${revision} from the GitHub API; no copy record was changed.`); }
115
- };
116
- const pinned = request(`commits/${revision}`);
117
- if (pinned.sha !== revision || !commit.test(pinned.tree?.sha)) throw Error('Base API did not resolve the immutable commit; no copy record was changed.');
118
- const tree = request(`trees/${pinned.tree.sha}?recursive=1`);
119
- if (tree.sha !== pinned.tree.sha || tree.truncated !== false || !Array.isArray(tree.tree)) throw Error('Incomplete Base API tree; no copy record was changed.');
120
- const entries = new Map();
121
- for (const entry of tree.tree) {
122
- safe(entry.path);
123
- if (entry.type === 'blob' && FILE_MODES.includes(entry.mode)) {
124
- if (!commit.test(entry.sha)) throw Error('Invalid Base API blob identity');
125
- entries.set(entry.path, {object: entry.sha, mode: entry.mode});
126
- }
127
- }
128
- return {
129
- tree: () => entries,
130
- digests(objects) {
131
- return new Map([...new Set(objects)].map(object => {
132
- const blob = request(`blobs/${object}`);
133
- if (blob.sha !== object || blob.encoding !== 'base64' || typeof blob.content !== 'string') throw Error('Invalid Base API blob');
134
- const bytes = Buffer.from(blob.content, 'base64');
135
- const actual = crypto.createHash('sha1').update(`blob ${bytes.length}\0`).update(bytes).digest('hex');
136
- if (blob.size !== bytes.length || actual !== object) throw Error('Base API blob integrity mismatch; no copy record was changed.');
137
- return [object, hash(bytes)];
138
- }));
139
- },
140
- };
109
+ return {read: apiReader(repository, revision), head: revision, close() {}};
141
110
  }
142
111
 
143
112
  export function validate(record) {
@@ -398,9 +367,9 @@ export function recordCopy(root, {positional: [localPath, originPath, revision],
398
367
  const files = seedFiles(root);
399
368
  if (fromApi && (base || baseArchive || offline)) throw Error('--from-api cannot be combined with --base, --base-archive or --offline');
400
369
  if (offline && !base && !baseArchive) throw Error('record-base --offline requires --base <checkout> or --base-archive <directory>; use --from-api while online');
401
- const origin = fromApi ? {close() {}} : open(record, base, root, offline, baseArchive, revision);
370
+ const origin = fromApi ? apiSource(record, root, revision) : open(record, base, root, offline, baseArchive, revision);
402
371
  try {
403
- const read = fromApi ? apiReader(record, root, revision) : reader(origin);
372
+ const read = reader(origin);
404
373
  delete record.repository;
405
374
  const sources = [...read.tree(revision)].filter(([name]) => within(name, originPath));
406
375
  if (!sources.length) throw Error(`No Base source at ${originPath} in ${revision}`);
@@ -427,8 +396,8 @@ export function recordCopy(root, {positional: [localPath, originPath, revision],
427
396
 
428
397
  // Explicitly transfer one recorded file to seed ownership without changing its bytes
429
398
  // or its Base digest/mode. The per-file revision survives later whole-copy refreshes.
430
- export function adaptCopy(root, {positional: [name], record: recordPath, baseArchive, ref}) {
431
- if (baseArchive || ref) throw Error('--base-archive and --ref are supported by record-base and refresh-base');
399
+ export function adaptCopy(root, {positional: [name], record: recordPath, baseArchive, ref, fromApi}) {
400
+ if (baseArchive || ref || fromApi) throw Error('--base-archive, --ref and --from-api are supported by record-base and refresh-base');
432
401
  safe(name);
433
402
  const filename = recordFile(root, recordPath);
434
403
  if (!filename) throw Error(`No ${recordPath} found; run record-base first`);
@@ -455,21 +424,22 @@ export function adaptCopy(root, {positional: [name], record: recordPath, baseArc
455
424
  // entirely, just as absent files are dropped from directory copies. Local edits
456
425
  // are refused, never overwritten, as is any change to an existing file under a
457
426
  // migrations folder.
458
- export function refreshCopies(root, {positional: [revision], record: recordPath, base, baseArchive, offline, editionMigration = []}) {
427
+ export function refreshCopies(root, {positional: [revision], record: recordPath, base, baseArchive, offline, fromApi, editionMigration = []}) {
459
428
  if (!commit.test(revision)) throw Error('Refresh requires an immutable full 40-character Base commit. Resolve the release tag with git -C <base-checkout> rev-parse <tag>^{commit}, then repeat with --ref <commit> --base <base-checkout>.');
429
+ if (fromApi && (base || baseArchive || offline)) throw Error('--from-api cannot be combined with --base, --base-archive or --offline');
460
430
  if (offline && !base && !baseArchive) throw Error('Offline refresh needs a Base checkout or archive. Repeat rer refresh-base --ref <commit> --offline --base <checkout> containing that commit, or use --base-archive <directory>.');
461
431
  const filename = recordFile(root, recordPath);
462
432
  if (!filename) throw Error(`No ${recordPath} found; run record-base <path> <origin-path> <commit> first`);
463
433
  const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
464
434
  const files = seedFiles(root);
465
435
  requireMaterializedCopies(root, record, files);
466
- const origin = open(record, base, root, offline, baseArchive, revision);
436
+ const origin = fromApi ? apiSource(record, root, revision) : open(record, base, root, offline, baseArchive, revision);
467
437
  origin.refresh = {revision, recordPath, editionMigration};
468
438
  let writing = false, notices = [];
469
439
  try {
470
440
  const read = reader(origin);
471
441
  const next = read.tree(revision);
472
- const migrations = editionMigrations(record, {tree: next, read, revision, base: base ? origin.repository : null, recordPath, offline, consent: editionMigration});
442
+ const migrations = editionMigrations(record, {tree: next, read, revision, base: base ? origin.repository : null, recordPath, offline, fromApi, consent: editionMigration});
473
443
  const index = new Set(raw(root, 'ls-files', '-z').toString('utf8').split('\0').filter(Boolean));
474
444
  for (const name of record.copies.flatMap(copy => Object.keys(copy.files))) requirePhysical(root, name);
475
445
  const upstream = read.digests([...next.values()].map(({object}) => object));
@@ -77,7 +77,7 @@ try {
77
77
  } else if (command === 'record-base') result = recordCopy(root, options(args.slice(1), 3));
78
78
  else if (command === 'adapt-base') result = adaptCopy(root, options(args.slice(1), 1));
79
79
  else if (command === 'refresh-base') result = refreshCopies(root, options(args.slice(1), 1));
80
- else throw Error('Usage: cli.mjs adopt|inspect|lock|check|receipt <sidecar-json>|changes|extract <id|parent> <new-output>|compare <id|parent> <candidate-checkout> [current-parent-checkout]|materialize <new-output>|promote <id>|release <new-local-git-repository>|verify-base [--record <file>] [--base <checkout>] [--offline]|record-base <path> <origin-path> <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api|--offline --base <checkout>]|adapt-base <file> [--record <file>]|refresh-base <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>]');
80
+ else throw Error('Usage: cli.mjs adopt|inspect|lock|check|receipt <sidecar-json>|changes|extract <id|parent> <new-output>|compare <id|parent> <candidate-checkout> [current-parent-checkout]|materialize <new-output>|promote <id>|release <new-local-git-repository>|verify-base [--record <file>] [--base <checkout>] [--offline]|record-base <path> <origin-path> <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api|--offline --base <checkout>]|adapt-base <file> [--record <file>]|refresh-base <revision>|--ref <revision> [--record <file>] [--base <checkout>|--base-archive <directory>|--from-api]');
81
81
  console.log(json(result));
82
82
  } catch (error) {
83
83
  console.error(error.message);
@@ -3,7 +3,7 @@ import {safe} from './files.mjs';
3
3
  import {fileURLToPath} from 'node:url';
4
4
  const dataPath = 'foundation/edition-migrations.json';
5
5
  const quote = value => "'" + value.replaceAll("'", "'\\''") + "'";
6
- export function editionMigrations(record, {tree, read, revision, base, recordPath, offline, consent = []}) {
6
+ export function editionMigrations(record, {tree, read, revision, base, recordPath, offline, fromApi, consent = []}) {
7
7
  let migrations = [];
8
8
  const fail = reason => { throw Error(`Base edition migration data error: ${reason}. Repair ${dataPath} in Base and commit it, then refresh to that commit; no files were refreshed.`); };
9
9
  if (tree.has(dataPath)) {
@@ -38,7 +38,7 @@ export function editionMigrations(record, {tree, read, revision, base, recordPat
38
38
  if (missing.length) {
39
39
  const cli = fileURLToPath(new URL('./cli.mjs', import.meta.url));
40
40
  // Keep automatic acquisition automatic: its temporary checkout closes on refusal.
41
- const command = `${quote(process.execPath)} ${quote(cli)} refresh-base ${revision}${base ? ' --base ' + quote(base) : ''} --record ${quote(recordPath)}${offline ? ' --offline' : ''}${applicable.map(step => ' --edition-migration ' + step.id).join('')}`;
41
+ const command = `${quote(process.execPath)} ${quote(cli)} refresh-base ${revision}${base ? ' --base ' + quote(base) : ''} --record ${quote(recordPath)}${offline ? ' --offline' : ''}${fromApi ? ' --from-api' : ''}${applicable.map(step => ' --edition-migration ' + step.id).join('')}`;
42
42
  throw Error(`Edition migrations require consent before planning:\n${missing.map(step => `${step.id}: ${step.copies.join(', ')}; ${step.guide}: ${step.edit}`).join('\n')}\nReview the guides and repeat exactly: ${command}\nRefresh will leave these edition-owned edits owed; no files were refreshed.`);
43
43
  }
44
44
  return applicable;