@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/README.md CHANGED
@@ -23,6 +23,9 @@ for adoption, refusal behavior, JSON fields and the original Base-checkout form.
23
23
  contract, preserving its judgments and its own specialist clauses, then runs
24
24
  contract document generation and checking. `rer door clauses --check` reports
25
25
  missing or stale requirements without writing; use `--cwd DIR` to select a seed.
26
+ Writing requires a `Shared duties (Base)` table and skips duties listed in its
27
+ `Base duty` column. Unlisted duties are synchronized as clauses; a missing table
28
+ refuses the write. The read-only check still inspects clause definitions.
26
29
  The CLI can sync a contract before runtime adoption; regenerate the seed's source lock last.
27
30
  See [agent door adoption](https://github.com/runeditrun/base/blob/main/modules/agent-door/ADOPTING.md#contract-clauses).
28
31
 
package/cli/README.md CHANGED
@@ -592,6 +592,9 @@ or use its stored device credential.
592
592
 
593
593
  `rer door clauses --write` synchronizes Base's thirteen shared agent duties
594
594
  into a seed's `CONTRACT.md` and runs its contract-docs generator and check.
595
+ It requires the seed's `Shared duties (Base)` table and skips duties listed
596
+ in its `Base duty` column; only unlisted duties need clause synchronization.
597
+ Without that table it refuses before writing any files.
595
598
  `rer door clauses --check` names missing or stale clauses and exits nonzero;
596
599
  `--cwd DIR` selects the seed root. Existing statuses, evidence and gaps survive,
597
600
  new clauses start missing, and seed-owned AGENT and SURFACE clauses stay intact.
@@ -15,6 +15,8 @@ import { verificationSponsors } from './agent-sponsors.mjs';
15
15
  import { verificationWebhooks } from './agent-webhooks.mjs';
16
16
  import { verificationInvoke, verificationJourney } from './agent-verifier.mjs';
17
17
  import { verificationBindings } from './agent-bindings.mjs';
18
+ import { writeVerificationHistory } from './agent-history-database.mjs';
19
+ import { verificationHistory } from './agent-history.mjs';
18
20
  import { journeyFixtures } from './agent-journey-fixtures.mjs';
19
21
  import { verificationAgentInitializer } from './agent-initializer.mjs';
20
22
 
@@ -65,10 +67,10 @@ export async function checkAgentDoor(root, { declaration, setupManifest, owner,
65
67
  const publicKeys = { [kid]: { kty, crv, x, y, kid, alg: 'ES256', use: 'sig' } };
66
68
  const values = { issuer, installationId, appOrigin: HOSTED_ORIGIN, publicKeys: JSON.stringify(publicKeys), email, sub, token, sessionSecret: randomBytes(32).toString('hex') };
67
69
  const nativeBindings = await verificationBindings(root, native.bindings, values);
68
- const network = await createAgentConformanceNetwork(root, verification.network, { email });
69
- let hosted;
70
+ const network = await createAgentConformanceNetwork(root, verification.network, { email, sponsorEmails: principals.slice(1).map(person => person.email) });
71
+ let hosted, history;
70
72
  try {
71
- hosted = await serveHosted(root, { declaration, rpc: verification.rpc, workerEntrypoints: workerDriver ? [workerDriver.entrypoint] : [], outboundService: async request => {
73
+ hosted = await serveHosted(root, { declaration, publicHosts: verification.publicHosts, fixturePreparation: true, rpc: verification.rpc, workerEntrypoints: workerDriver ? [workerDriver.entrypoint] : [], outboundService: async request => {
72
74
  const copy = reviewers.observes(request) ? request.clone() : null, response = await network.fetch(request);
73
75
  if (copy) await reviewers.capture(copy, response.clone());
74
76
  return response;
@@ -86,9 +88,9 @@ export async function checkAgentDoor(root, { declaration, setupManifest, owner,
86
88
  } });
87
89
  const request = (absolute, init = {}) => {
88
90
  const url = new URL(absolute);
89
- if (url.origin !== hosted.origin) throw new RerError('agent_door_origin', 'Agent conformance request must use the fresh hosted installation origin.', 1);
91
+ if (url.username || url.password || !hosted.origins.includes(url.origin)) throw new RerError('agent_door_origin', 'Agent conformance request must use a declared hosted installation origin.', 1);
90
92
  const timeout = AbortSignal.timeout(30_000);
91
- return hosted.fetch(url.href.slice(hosted.origin.length + 1), { ...init, signal: init.signal ? AbortSignal.any([init.signal, timeout]) : timeout, headers: Object.fromEntries(new Headers(init.headers)) });
93
+ return hosted.fetch(url.href.slice(url.origin.length + 1), { ...init, origin: url.origin, signal: init.signal ? AbortSignal.any([init.signal, timeout]) : timeout, headers: Object.fromEntries(new Headers(init.headers)) });
92
94
  };
93
95
  const path = claim?.mode === 'signed' ? claim.path ?? '/auth/owner-claim' : owner?.path ?? claim?.path ?? '/auth/bootstrap';
94
96
  const issued = seconds();
@@ -130,7 +132,7 @@ export async function checkAgentDoor(root, { declaration, setupManifest, owner,
130
132
  try {
131
133
  response = await principal.initialize({ request, origin: hosted.origin, email: principal.email, sub: principal.sub,
132
134
  label: principal.label, displayName: principal.label, token, claim: { mode: 'signed', path, token: signedClaim },
133
- owner: sponsors[0], database: hosted.database, mail: network.mail, fixtures: network.fixtures, returnTo: owner?.returnTo ?? '/',
135
+ owner: sponsors[0], database: hosted.database, mail: network.mailFor(principal.email), fixtures: network.fixtures, returnTo: owner?.returnTo ?? '/',
134
136
  });
135
137
  } catch { throw new RerError('agent_sponsor_bootstrap', 'An additional sponsor could not complete its native lifecycle through the disposable installation.', 1); }
136
138
  const session = response?.headers?.getSetCookie().map(value => value.split(';', 1)[0]).join('; ');
@@ -140,15 +142,23 @@ export async function checkAgentDoor(root, { declaration, setupManifest, owner,
140
142
  }
141
143
  const secrets = await network.provision(hosted);
142
144
  const webhooks = await verificationWebhooks(root, verification.webhooks, secrets);
143
- const journey = await verificationJourney(root, verification.journey, { request, origin: hosted.origin, email, cookie, mail: network.mail, fixtures: network.fixtures, database: hosted.database });
144
- const prepare = await journeyFixtures(root, verification.fixtures, { request, origin: hosted.origin, email, cookie, mail: network.mail, fixtures: network.fixtures, database: hosted.database });
145
+ history = await verificationHistory(root, verification.history);
146
+ const journey = await verificationJourney(root, verification.journey, { request, origin: hosted.origin, email, cookie, mail: network.mail, fixtures: network.fixtures, ...await hosted.preparation() });
147
+ const prepare = await journeyFixtures(root, verification.fixtures, { request, origin: hosted.origin, email, cookie, mail: network.mail, fixtures: network.fixtures, ...await hosted.preparation() });
148
+ const nativeStep = history ? async value => {
149
+ await history.run(value, (declaration, rows) => writeVerificationHistory(hosted, declaration, rows));
150
+ await journey.nativeStep?.(value);
151
+ } : journey.nativeStep;
145
152
  const result = await runAgentDoorConformance({ fetch: request, origin: hosted.origin, surface: declared.surface,
146
- signer: { issuer, installationId, kid, privateKey: pair.privateKey }, person: sponsors[0], sponsors: sponsors.filter(person => !negativeOnly.has(person.label)), webhooks, business, prove, ...journey, prepare, invoke: workerDriver?.run(hosted),
153
+ signer: { issuer, installationId, kid, privateKey: pair.privateKey }, person: sponsors[0], sponsors: sponsors.filter(person => !negativeOnly.has(person.label)), webhooks, business, prove, ...journey, nativeStep, prepare, invoke: workerDriver?.run(hosted),
147
154
  initializeAgent: initializeAgent && (({ agentId, person }) => initializeAgent({ agentId, person, request, origin: hosted.origin, mail: network.mail, fixtures: network.fixtures })),
148
155
  });
149
156
  const points = [...result.points, ...reviewers.points()];
150
157
  return finish({ ...result, points, ok: points.every(point => point.ok) }, report);
151
- } finally { reviewers.close(); try { await hosted?.close(); } finally { await network.close(); } }
158
+ } finally {
159
+ try { await history?.close(); }
160
+ finally { reviewers.close(); try { await hosted?.close(); } finally { await network.close(); } }
161
+ }
152
162
  }
153
163
 
154
164
  if (process.argv[1] && await realpath(new URL(import.meta.url)) === await realpath(process.argv[1])) {
@@ -0,0 +1,82 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { RerError } from './errors.mjs';
3
+ import { FixtureDatabaseError } from '../modules/agent-door/conformance/fixtures.mjs';
4
+ import { historyInvalid, isHistoryDeclaration } from './agent-history.mjs';
5
+
6
+ const installations = new WeakMap();
7
+ export const registerHistoryInstallation = (hosted, database) => installations.set(hosted, database);
8
+
9
+ // The only caller supplies Miniflare's fresh, nonpersistent local databases.
10
+ // No URL, database id, provider client or raw prepare capability is accepted
11
+ // from fixture output. The ordinary hosted database reader stays SELECT-only.
12
+ export async function writeVerificationHistory(hosted, declaration, rows) {
13
+ try {
14
+ if (!installations.has(hosted) || !isHistoryDeclaration(declaration)) throw historyInvalid();
15
+ const database = installations.get(hosted);
16
+ if (!Array.isArray(rows) || rows.length > 1000) throw historyInvalid();
17
+ const batches = new Map(), checked = new Set();
18
+ for (const row of rows) {
19
+ if (!row || Object.keys(row).sort().join(',') !== 'statement,values' || !Array.isArray(row.values)) throw historyInvalid();
20
+ const statement = declaration.statements.get(row.statement);
21
+ if (!statement || row.values.length !== statement.parameters
22
+ || row.values.some(value => value !== null && typeof value !== 'string' && !(typeof value === 'number' && Number.isFinite(value)))) throw historyInvalid();
23
+ if (!batches.has(statement.binding)) batches.set(statement.binding, { ...await database(statement.binding), rows: [] });
24
+ const batch = batches.get(statement.binding);
25
+ const key = statement.binding + ':' + statement.table.toLowerCase();
26
+ if (!checked.has(key)) {
27
+ const table = await batch.database.prepare("SELECT sql FROM sqlite_schema WHERE type='table' AND name=? COLLATE NOCASE").bind(statement.table).first();
28
+ const trigger = await batch.database.prepare("SELECT 1 AS found FROM sqlite_schema WHERE type='trigger' AND tbl_name=? COLLATE NOCASE").bind(statement.table).first();
29
+ // Refuse every target trigger, rather than trying to parse its transitive
30
+ // effects. INSERT cannot cause foreign-key UPDATE/DELETE cascades.
31
+ if (!table || !/^CREATE TABLE\b/i.test(table.sql) || trigger) throw historyInvalid();
32
+ checked.add(key);
33
+ }
34
+ batch.rows.push({ sql: statement.sql, values: row.values });
35
+ }
36
+ for (const { batch, rows } of batches.values()) if (rows.length) await batch(rows);
37
+ } catch { throw historyInvalid(); }
38
+ }
39
+
40
+ // Both preparation APIs use this one disposable D1 writer. The public phase
41
+ // handle exposes individual statements; declared history retains atomic batches
42
+ // and its stricter insert-only, trigger-free table checks above.
43
+ export function preparationDatabase(db) {
44
+ const refuse = () => { throw new RerError('hosted_database_invalid', new FixtureDatabaseError().message, 1); };
45
+ const check = sql => {
46
+ if (typeof sql !== 'string' || sql.includes(';')) return refuse();
47
+ const readOnly = /^\s*SELECT\b/i.test(sql);
48
+ const target = /^\s*(?:UPDATE\s+|INSERT\s+INTO\s+|DELETE\s+FROM\s+)([a-z_][a-z0-9_]*)\b/i.exec(sql)?.[1];
49
+ if (!readOnly && (!target || /^(?:agent_door_|sqlite_)/i.test(target))) return refuse();
50
+ return { readOnly, target };
51
+ };
52
+ const batch = async rows => {
53
+ try {
54
+ const checked = rows.map(row => ({ ...row, ...check(row.sql) }));
55
+ for (const { target } of checked.filter(row => !row.readOnly)) {
56
+ const table = await db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=? COLLATE NOCASE").bind(target).first();
57
+ if (!table) return refuse();
58
+ }
59
+ const protectedTables = checked.some(row => !row.readOnly)
60
+ ? (await db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND lower(substr(name,1,11))='agent_door_'").all()).results : [];
61
+ const guards = protectedTables.flatMap(({ name }) => ['INSERT', 'UPDATE', 'DELETE'].map(event => ({
62
+ name: 'rer_fixture_' + randomBytes(16).toString('hex'), table: name.replaceAll('"', '""'), event,
63
+ })));
64
+ const results = await db.batch([
65
+ ...guards.map(guard => db.prepare(`CREATE TRIGGER "${guard.name}" BEFORE ${guard.event} ON "${guard.table}" BEGIN SELECT RAISE(ABORT, 'fixture protocol write refused'); END`)),
66
+ ...checked.map(({ sql, values }) => db.prepare(sql).bind(...values)),
67
+ ...guards.map(guard => db.prepare(`DROP TRIGGER "${guard.name}"`)),
68
+ ]);
69
+ return results.slice(guards.length, guards.length + checked.length);
70
+ } catch { return refuse(); }
71
+ };
72
+ const statement = (sql, values = []) => {
73
+ check(sql);
74
+ const execute = async () => (await batch([{ sql, values }]))[0];
75
+ return Object.freeze({
76
+ bind: (...bound) => statement(sql, bound), all: execute, run: execute,
77
+ first: async column => { const row = (await execute()).results[0] ?? null; return column === undefined ? row : row?.[column] ?? null; },
78
+ raw: async () => (await execute()).results.map(row => Object.values(row)),
79
+ });
80
+ };
81
+ return { database: Object.freeze({ prepare: sql => statement(sql) }), batch };
82
+ }
@@ -0,0 +1,68 @@
1
+ import { readdir, readFile, realpath } from 'node:fs/promises';
2
+ import { readFileSync, realpathSync, statSync } from 'node:fs';
3
+ import { join, relative, resolve } from 'node:path';
4
+ import { testSource } from '../composition/git-source.mjs';
5
+ import { dependencies } from '../edition-authoring/module-edges.mjs';
6
+ import { resolve as resolveModule } from '../edition-authoring/module-resolution.mjs';
7
+ import { RerError } from './errors.mjs';
8
+
9
+ const productionHistory = (name, target = name) => new RerError('agent_history_invalid', `agent_history_invalid: Production file ${name} references verification-only history module ${target}.`, 1);
10
+
11
+ // Check runtime import edges without executing test code. Scan source as well as
12
+ // unbundled output, following literal imports/re-exports, import.meta.url
13
+ // resources (including Workers), TS paths and symlinks.
14
+ // Shared native helpers remain usable by production; only the fixture's tests/
15
+ // modules are barred. An undeclared seed performs no additional scan.
16
+ export async function refuseHistoryImports(root, source) {
17
+ const files = new Set(), physical = new Map();
18
+ const ignored = new Set(['node_modules', '.git', '.rer', '.wrangler', '.seed', '.cache', 'test-results', 'playwright-report']);
19
+ async function walk(directory = '') {
20
+ for (const entry of await readdir(join(root, directory), { withFileTypes: true })) {
21
+ if (ignored.has(entry.name)) continue;
22
+ const name = directory ? directory + '/' + entry.name : entry.name;
23
+ if (entry.isDirectory()) await walk(name);
24
+ else if (entry.isFile() || entry.isSymbolicLink()) {
25
+ files.add(name);
26
+ physical.set(name, await realpath(join(root, name)));
27
+ }
28
+ }
29
+ }
30
+ await walk();
31
+ const forbidden = new Set(await Promise.all(source.modules.filter(module => relative(source.root, module.path).startsWith('tests/')).map(module => realpath(module.path))));
32
+ const tree = { has: name => {
33
+ if (files.has(name)) return true;
34
+ if (name.startsWith('../') || name.split('/').some(part => ignored.has(part))) return false;
35
+ // Literal imports may cross a contained directory symlink that the directory
36
+ // walk deliberately does not follow. Resolve that file physically too.
37
+ try {
38
+ if (!statSync(join(root, name)).isFile()) return false;
39
+ files.add(name); physical.set(name, realpathSync(join(root, name)));
40
+ return true;
41
+ } catch { return false; }
42
+ }, read: name => readFileSync(join(root, name), 'utf8') };
43
+ let paths = {};
44
+ try { paths = JSON.parse(await readFile(join(root, 'tsconfig.json'), 'utf8')).compilerOptions?.paths ?? {}; } catch { /* Relative resolution still applies. */ }
45
+ const queue = [...files].filter(name => !testSource(name) && !name.startsWith('scripts/') && /\.[cm]?[jt]sx?$/.test(name)), seen = new Set();
46
+ while (queue.length) {
47
+ const name = queue.shift();
48
+ if (seen.has(name)) continue;
49
+ seen.add(name);
50
+ if (forbidden.has(physical.get(name))) throw productionHistory(name);
51
+ for (const request of dependencies(await readFile(join(root, name), 'utf8'), name, {resources: true}).filter(request => !request.types)) {
52
+ const candidates = [request.specifier];
53
+ for (const [alias, targets] of Object.entries(paths)) {
54
+ const [prefix, suffix = ''] = alias.split('*');
55
+ if (alias.includes('*') ? request.specifier.startsWith(prefix) && request.specifier.endsWith(suffix) : request.specifier === alias) {
56
+ for (const target of targets) candidates.push('./' + relative(resolve(root, name, '..'), resolve(root, target.replace('*', request.specifier.slice(prefix.length, suffix ? -suffix.length : undefined)))));
57
+ }
58
+ }
59
+ for (const specifier of candidates) {
60
+ const target = resolveModule(tree, name, specifier, request);
61
+ if (typeof target === 'string') {
62
+ if (forbidden.has(physical.get(target))) throw productionHistory(name, target);
63
+ if (/\.[cm]?[jt]sx?$/.test(target)) queue.push(target);
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,58 @@
1
+ import { relative } from 'node:path';
2
+ import { isolatedNetworkAdapter } from '../modules/agent-door/conformance/network-runtime.mjs';
3
+ import { networkAdapterModules } from '../modules/agent-door/conformance/network-modules.mjs';
4
+ import { RerError } from './errors.mjs';
5
+ import { refuseHistoryImports } from './agent-history-isolation.mjs';
6
+
7
+ export const historyInvalid = () => new RerError('agent_history_invalid', 'agent_history_invalid: Verification history requires contained test-only modules and named, insert-only statements on declared disposable tables without triggers. No fixture values or SQL are logged.', 1);
8
+ const declarations = new WeakSet();
9
+ export const isHistoryDeclaration = value => declarations.has(value);
10
+ const identifier = '[A-Za-z_][A-Za-z0-9_]*';
11
+ // A deliberately small SQL language: no SELECT, expressions, conflict updates,
12
+ // schema qualifiers, comments, pragmas or multiple statements. Literals let a
13
+ // shared native records table constrain each statement to one history kind.
14
+ const insert = new RegExp(`^INSERT INTO (${identifier})\\s*\\((${identifier}(?:\\s*,\\s*${identifier})*)\\)\\s*VALUES\\s*\\((\\?|[0-9]+|'[A-Za-z0-9_-]*')(?:\\s*,\\s*(\\?|[0-9]+|'[A-Za-z0-9_-]*'))*\\)$`, 'i');
15
+
16
+ export async function historyDeclaration(root, config) {
17
+ if (config === undefined) return;
18
+ try {
19
+ if (!config || Object.keys(config).sort().join(',') !== 'module,statements,steps'
20
+ || typeof config.module !== 'string' || !config.module.startsWith('tests/')
21
+ || !Array.isArray(config.steps) || !config.steps.length || config.steps.some(step => typeof step !== 'string' || !step)
22
+ || !config.statements || Array.isArray(config.statements) || typeof config.statements !== 'object' || !Object.keys(config.statements).length) throw historyInvalid();
23
+ const statements = new Map();
24
+ for (const [name, value] of Object.entries(config.statements)) {
25
+ if (!value || Object.keys(value).sort().join(',') !== 'binding,sql' || typeof value.binding !== 'string' || typeof value.sql !== 'string') throw historyInvalid();
26
+ const match = insert.exec(value.sql);
27
+ if (!match || /^(?:agent_door_|sqlite_|oa_)/i.test(match[1])) throw historyInvalid();
28
+ statements.set(name, { ...value, table: match[1], parameters: (value.sql.match(/\?/g) ?? []).length });
29
+ }
30
+ const source = await networkAdapterModules(root, config.module);
31
+ if (!relative(source.root, source.modules[0].path).startsWith('tests/')) throw historyInvalid();
32
+ await refuseHistoryImports(root, source);
33
+ const declaration = { module: config.module, steps: new Set(config.steps), statements };
34
+ declarations.add(declaration);
35
+ return declaration;
36
+ } catch (error) {
37
+ if (error instanceof RerError && error.code === 'agent_history_invalid') throw error;
38
+ throw historyInvalid();
39
+ }
40
+ }
41
+
42
+ /** Only checkAgentDoor starts this isolated test Worker. No application binding,
43
+ * request credentials, Node API, filesystem or external network is supplied. */
44
+ export async function verificationHistory(root, config) {
45
+ const declaration = await historyDeclaration(root, config);
46
+ if (!declaration) return;
47
+ const adapter = await isolatedNetworkAdapter(root, declaration.module, () => new Response(null, { status: 502 }));
48
+ return { declaration, close: () => adapter.close(), run: async (context, write) => {
49
+ if (context.stage !== 'beforeStep' || !['autonomous', 'held'].includes(context.phase) || !declaration.steps.has(context.step.id)) return;
50
+ try {
51
+ const { phase, step, args, outputs, now } = context;
52
+ const response = await adapter.fetch(new Request('https://history.invalid/', { method: 'POST', body: JSON.stringify({ phase, step, args, outputs, now }) }));
53
+ const body = await response.text();
54
+ if (!response.ok || body.length > 2 * 1024 * 1024) throw historyInvalid();
55
+ await write(declaration, JSON.parse(body));
56
+ } catch { throw historyInvalid(); }
57
+ } };
58
+ }
@@ -4,7 +4,7 @@ import { contained } from './project.mjs';
4
4
  import { RerError } from './errors.mjs';
5
5
  import { FixtureDatabaseError } from '../modules/agent-door/conformance/fixtures.mjs';
6
6
 
7
- /** Test tooling receives the disposable installation's doors and state reader. */
7
+ /** Test tooling receives the disposable installation's preparation context. */
8
8
  export async function journeyFixtures(root, declaration, context) {
9
9
  if (declaration === undefined) return undefined;
10
10
  if (!declaration || Object.keys(declaration).join(',') !== 'module' || typeof declaration.module !== 'string') throw new RerError('agent_fixtures_invalid', 'verification.fixtures must name a contained test module.', 1);
@@ -12,9 +12,9 @@ export async function journeyFixtures(root, declaration, context) {
12
12
  const prepare = (await import(pathToFileURL(resolve(root, declaration.module)))).default;
13
13
  if (typeof prepare !== 'function') throw new RerError('agent_fixtures_invalid', 'verification.fixtures module must default-export a phase preparation function.', 1);
14
14
  return async ({ phase, tool, now }) => {
15
- try { return await prepare({ ...context, phase, tool, now }); }
15
+ try { context.queues?.release(); await context.queues?.restore(); return await prepare({ ...context, phase, tool, now }); }
16
16
  catch (error) {
17
- if (error?.code === 'hosted_database_invalid') throw new FixtureDatabaseError();
17
+ if (error instanceof FixtureDatabaseError || error?.code === 'hosted_database_invalid') throw new FixtureDatabaseError();
18
18
  throw new Error('Native phase preparation failed; no captured mail, credentials or fixture values are logged.');
19
19
  }
20
20
  };
@@ -30,8 +30,14 @@ export async function agentConformanceNetwork(request) {
30
30
 
31
31
  /** Extend the paid network edge for declared origins, without running seed
32
32
  * modules in this process or giving them any route to the real network. */
33
- export async function createAgentConformanceNetwork(root, config, { email } = {}) {
34
- const mail = createCapturedMail(email);
33
+ export async function createAgentConformanceNetwork(root, config, { email, sponsorEmails = [] } = {}) {
34
+ const mailboxes = new Map([email, ...sponsorEmails].map(email => [email, createCapturedMail(email)]));
35
+ const closeMail = () => { for (const mail of mailboxes.values()) mail.close(); };
36
+ const mailFor = email => {
37
+ const mail = mailboxes.get(email);
38
+ if (!mail) throw new TypeError('Captured mail requires a declared verification principal.');
39
+ return { read: options => mail.read(options) };
40
+ };
35
41
  let declaration, adapter, fixtures;
36
42
  try {
37
43
  if (config !== undefined) {
@@ -40,22 +46,22 @@ export async function createAgentConformanceNetwork(root, config, { email } = {}
40
46
  }
41
47
  fixtures = createFixtureControl(declaration?.origins ?? new Set());
42
48
  } catch (error) {
43
- mail.close();
49
+ closeMail();
44
50
  if (error instanceof NetworkAdapterError) throw new RerError('agent_network_invalid', error.message, 1);
45
51
  throw error;
46
52
  }
47
- return { mail: { read: options => mail.read(options) }, fixtures: { set: (origin, value) => fixtures.set(origin, value) },
53
+ return { mail: mailFor(email), mailFor, fixtures: { set: (origin, value) => fixtures.set(origin, value) },
48
54
  provision: async hosted => {
49
55
  if (config?.secretSource === undefined) return {};
50
56
  const secrets = await verificationSecrets(root, config.secretSource, hosted.database);
51
57
  await adapter.provision(secrets);
52
58
  return secrets;
53
59
  },
54
- close: async () => { mail.close(); fixtures.close(); await adapter?.close(); }, fetch: async request => {
60
+ close: async () => { closeMail(); fixtures.close(); await adapter?.close(); }, fetch: async request => {
55
61
  const capture = request.clone(), origin = new URL(request.url).origin;
56
62
  try {
57
63
  const response = declaration?.origins.has(origin) ? await adapter.fetch(request, fixtures.get(origin)) : await agentConformanceNetwork(request);
58
- if (response.ok) await mail.capture(capture);
64
+ if (response.ok) await Promise.all([...mailboxes.values()].map((mail, index) => mail.capture(index === mailboxes.size - 1 ? capture : capture.clone())));
59
65
  else void capture.body?.cancel();
60
66
  return response;
61
67
  }
package/cli/authoring.mjs CHANGED
@@ -10,7 +10,7 @@ export function authoringCommand(options) {
10
10
  ...(options.editionMigration ? options.editionMigration.split(',').flatMap(id => ['--edition-migration', id]) : []),
11
11
  options.ref, '--record', options.record ?? 'base-copies.json',
12
12
  ...(options.base ? ['--base', resolve(options.cwd, options.base)] : []),
13
- ...(options.baseArchive ? ['--base-archive', resolve(options.cwd, options.baseArchive)] : []), ...(options.offline ? ['--offline'] : []),
13
+ ...(options.baseArchive ? ['--base-archive', resolve(options.cwd, options.baseArchive)] : []), ...(options.offline ? ['--offline'] : []), ...(options.fromApi ? ['--from-api'] : []),
14
14
  ], 1));
15
15
  } catch (error) { throw refuse(error.message); }
16
16
  }
@@ -0,0 +1,115 @@
1
+ import { chmod, lstat, mkdir, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises';
2
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
3
+ import { randomUUID } from 'node:crypto';
4
+ import { loadState, stateDirectory } from './state.mjs';
5
+ import { serversDirectory } from '../server-registry.mjs';
6
+ import { alive } from '../../modules/job-queue/identity.mjs';
7
+
8
+ const carryPath = checkout => join(checkout, '.rer/local/business.json');
9
+ const registryPath = () => join(stateDirectory(), 'carried.json');
10
+ async function readJson(path) {
11
+ try { return JSON.parse(await readFile(path, 'utf8')); }
12
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
13
+ }
14
+ async function recordedCheckouts() {
15
+ const recorded = await readJson(registryPath()) ?? [];
16
+ if (!Array.isArray(recorded)) throw Error('the carry registry is invalid');
17
+ return recorded;
18
+ }
19
+ async function atomicJson(path, value) {
20
+ const directory = dirname(path), temporary = join(directory, `${randomUUID()}.tmp`);
21
+ await mkdir(directory, { recursive: true, mode: 0o700 });
22
+ await chmod(directory, 0o700);
23
+ try {
24
+ await writeFile(temporary, JSON.stringify(value) + '\n', { flag: 'wx', mode: 0o600 });
25
+ await chmod(temporary, 0o600);
26
+ await rename(temporary, path);
27
+ } finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
28
+ }
29
+
30
+ async function regular(path, directory = false, optional = false) {
31
+ let info;
32
+ try { info = await lstat(path); }
33
+ catch (error) { if (optional && error.code === 'ENOENT') return false; throw error; }
34
+ if (info.isSymbolicLink() || !(directory ? info.isDirectory() : info.isFile())) throw Error('the carry path is linked or not a regular file');
35
+ return true;
36
+ }
37
+ export async function firstParty(checkout) {
38
+ if (typeof checkout !== 'string' || !isAbsolute(checkout)) return false;
39
+ await regular(checkout, true);
40
+ await regular(join(checkout, 'OPERATIONS.json'));
41
+ return (await readJson(join(checkout, 'OPERATIONS.json')))?.seedContract?.profile === 'first-party';
42
+ }
43
+
44
+ // This file is generated Business carry, not an app's own settings. Its supported
45
+ // profile values remain carry-owned; unknown documents and foreign carries do not.
46
+ function ownsCarry(prior, carry) {
47
+ return prior && !Array.isArray(prior) && prior.version === carry.version
48
+ && prior.origin === carry.origin && prior.business_id === carry.business_id && prior.business_url === carry.business_url
49
+ && typeof prior.name === 'string' && (prior.legal_name === null || typeof prior.legal_name === 'string')
50
+ && (prior.accent === null || typeof prior.accent === 'string' && /^#[0-9a-f]{6}$/i.test(prior.accent))
51
+ && (prior.logo_url === null || typeof prior.logo_url === 'string') && typeof prior.refreshed_at === 'string'
52
+ && Object.keys(prior).length === Object.keys(carry).length + 1 && Object.keys(carry).every(key => Object.hasOwn(prior, key));
53
+ }
54
+
55
+ export async function writeCarry(checkout, carry, { existingOnly = false } = {}) {
56
+ if (!await firstParty(checkout)) throw Error('the checkout is not first-party');
57
+ const root = resolve(checkout), path = carryPath(root);
58
+ for (const directory of [join(root, '.rer'), dirname(path)]) await regular(directory, true, !existingOnly);
59
+ const exists = await regular(path, false, !existingOnly);
60
+ const prior = exists ? await readJson(path) : null;
61
+ if (exists && !ownsCarry(prior, carry)) throw Error('the existing file is not a carry for this paired Business');
62
+ const checkouts = await recordedCheckouts();
63
+ // Record custody before the write, so disconnect can clean up even after a crash.
64
+ if (!checkouts.includes(root)) await atomicJson(registryPath(), [...checkouts, root]);
65
+ if (prior && Object.keys(carry).every(key => prior[key] === carry[key])) {
66
+ for (const [file, mode] of [[dirname(path), 0o700], [path, 0o600]]) {
67
+ if (((await stat(file)).mode & 0o777) !== mode) await chmod(file, mode);
68
+ }
69
+ return false;
70
+ }
71
+ await atomicJson(path, { ...carry, refreshed_at: new Date().toISOString() });
72
+ return true;
73
+ }
74
+
75
+ // The same read-only liveness check as the installed launcher's liveServers.
76
+ // Keep it portable: vendored Business commands have no global launcher files.
77
+ async function liveCheckouts() {
78
+ const registry = await readJson(join(serversDirectory(), 'state.json'));
79
+ const checkouts = [];
80
+ for (const record of registry?.running ?? []) {
81
+ let live = true;
82
+ try { live = await alive(record.owner); } catch { /* Unverifiable identities stay live, as in the registry. */ }
83
+ if (live) checkouts.push(record.checkout);
84
+ }
85
+ return [...new Set(checkouts)];
86
+ }
87
+
88
+ export async function carryCheckouts(refused) {
89
+ const checkouts = new Map();
90
+ try { for (const checkout of await recordedCheckouts()) checkouts.set(checkout, { checkout, live: false }); }
91
+ catch (error) { refused(error); }
92
+ try { for (const checkout of await liveCheckouts()) checkouts.set(checkout, { checkout, live: true }); }
93
+ catch (error) { refused(error); }
94
+ return [...checkouts.values()];
95
+ }
96
+
97
+ export async function removeCarries() {
98
+ const state = await loadState();
99
+ if (!state?.origin || !state.businessId) return;
100
+ const carry = { version: 1, origin: state.origin, business_id: state.businessId,
101
+ business_url: `${state.origin}/business/${encodeURIComponent(state.businessId)}`,
102
+ name: null, legal_name: null, accent: null, logo_url: null };
103
+ const recorded = await recordedCheckouts();
104
+ for (const checkout of new Set([...recorded, ...await liveCheckouts()])) {
105
+ try {
106
+ if (!await firstParty(checkout)) continue;
107
+ for (const directory of [join(checkout, '.rer'), dirname(carryPath(checkout))]) await regular(directory, true);
108
+ if (await regular(carryPath(checkout), false, true) && ownsCarry(await readJson(carryPath(checkout)), carry)) await unlink(carryPath(checkout));
109
+ } catch (error) {
110
+ // Disconnect must still clean eligible checkouts when an old one was removed,
111
+ // linked, repurposed or became unreadable. Leave unrelated files untouched.
112
+ if (!['ENOENT', 'ENOTDIR'].includes(error.code) && error.message !== 'the carry path is linked or not a regular file' && !(error instanceof SyntaxError)) throw error;
113
+ }
114
+ }
115
+ }