@runeditrun/rer 0.2.90 → 0.2.93

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 (38) hide show
  1. package/cli/README.md +1 -1
  2. package/cli/agent-door.mjs +15 -4
  3. package/cli/agent-initializer.mjs +27 -0
  4. package/cli/agent-journey-fixtures.mjs +8 -4
  5. package/cli/agent-verifier.mjs +5 -1
  6. package/cli/deploy-command.mjs +19 -8
  7. package/cli/deploy.mjs +1 -1
  8. package/cli/ledger-refusal.mjs +1 -1
  9. package/cli/portability-migrations.mjs +9 -5
  10. package/cli/portability-worker.mjs +4 -2
  11. package/foundation/runtime/contract-docs.mjs +26 -12
  12. package/global/CHANGELOG.md +47 -0
  13. package/modules/agent-door/conformance/audit.mjs +82 -7
  14. package/modules/agent-door/conformance/crypto.mjs +14 -2
  15. package/modules/agent-door/conformance/fixtures.mjs +22 -3
  16. package/modules/agent-door/conformance/index.mjs +9 -2
  17. package/modules/agent-door/conformance/journey.mjs +26 -2
  18. package/modules/agent-door/conformance/negative.mjs +29 -1
  19. package/modules/agent-door/conformance/policy-phases.mjs +43 -1
  20. package/modules/agent-door/conformance/protocol.mjs +44 -8
  21. package/modules/agent-door/conformance/sponsors.mjs +12 -1
  22. package/modules/agent-door/conformance/step-hooks.mjs +3 -1
  23. package/modules/agent-door/conformance/surface.mjs +10 -0
  24. package/modules/deploy-kit/owner-cli.mjs +9 -1
  25. package/modules/erasure/README.md +4 -2
  26. package/modules/operator-auth/identity/oidc/README.md +2 -1
  27. package/modules/portability/README.md +52 -2
  28. package/modules/portability/backfills.mjs +96 -3
  29. package/modules/portability/bridge.mjs +3 -1
  30. package/modules/portability/format.mjs +1 -0
  31. package/modules/portability/import.mjs +58 -3
  32. package/modules/portability/index.d.mts +2 -0
  33. package/modules/portability/index.mjs +16 -5
  34. package/modules/portability/library-history.mjs +152 -0
  35. package/modules/portability/migration-history.mjs +3 -2
  36. package/modules/portability/snapshot-objects.mjs +16 -0
  37. package/onboarding/adapter.mjs +3 -1
  38. package/package.json +1 -1
@@ -26,11 +26,23 @@ export async function accessToken(context, overrides = {}) {
26
26
  }
27
27
  export const autonomousPolicy = () => ({ schema: 'rer-agent-policy/1', catalogue: CATALOGUE, rules: [{ id: 'all', class: '*', do: 'autonomous' }], spend: { none: true } });
28
28
  export const heldPolicy = () => ({ schema: 'rer-agent-policy/1', catalogue: CATALOGUE, rules: [{ id: 'all', class: '*', do: 'approval', approver: ['sponsor'], wait: 'PT1H' }, { id: 'standard', class: 'standard', do: 'review' }], reviewers: ['sponsor'], spend: { none: true } });
29
+ // A revision is immutable evidence. Reuse its signature when a helper caller
30
+ // refreshes that revision, including after rejected intermediate proposals.
31
+ const revisionSignatures = new WeakMap();
29
32
  export async function policyEnvelope(context, document, { paused = false, revoked, revision = context.revision + 1, seq = context.seq + 1, ...overrides } = {}) {
30
33
  const iat = seconds(context.now);
31
- const revision_signature = document === null ? null : await signJws(context.signer.privateKey, context.signer.kid, 'rer-agent-policy-revision+jwt', {
34
+ const claims = {
32
35
  business: context.business, revision, document_sha256: createHash('sha256').update(canonical(document)).digest('hex'),
33
- });
36
+ };
37
+ let signatures = revisionSignatures.get(context.signer.privateKey);
38
+ if (!signatures) revisionSignatures.set(context.signer.privateKey, signatures = new Map());
39
+ const key = canonical({ kid: context.signer.kid, ...claims });
40
+ if (document !== null && !signatures.has(key)) signatures.set(key, signJws(context.signer.privateKey, context.signer.kid, 'rer-agent-policy-revision+jwt', claims));
41
+ const revision_signature = document === null ? null : await signatures.get(key);
34
42
  const payload = { v: 1, iss: context.signer.issuer, aud: context.origin, sub: context.signer.installationId, business: context.business, seq, revision, paused, catalogue: CATALOGUE, document, revision_signature, iat, exp: iat + 300, ...(revoked === undefined ? {} : { revoked }), ...overrides };
35
43
  return { payload, policy: await signJws(context.signer.privateKey, context.signer.kid, 'rer-agent-policy+jwt', payload) };
36
44
  }
45
+ export async function renewalEnvelope(context, adopted) {
46
+ const iat = seconds(context.now), payload = { ...adopted, iat, exp: iat + 300 };
47
+ return { payload, policy: await signJws(context.signer.privateKey, context.signer.kid, 'rer-agent-policy+jwt', payload) };
48
+ }
@@ -1,4 +1,12 @@
1
1
  import { auditRows } from './audit-rows.mjs';
2
+ import { seconds } from './crypto.mjs';
3
+
4
+ export class FixtureDatabaseError extends Error {
5
+ constructor() {
6
+ super('agent_fixtures_database_unavailable: Disposable database access refused; fixture values, records and SQL are not logged.');
7
+ this.code = 'agent_fixtures_database_unavailable';
8
+ }
9
+ }
2
10
 
3
11
  const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
4
12
  const text = value => typeof value === 'string' && value.trim().length > 0;
@@ -6,17 +14,24 @@ const text = value => typeof value === 'string' && value.trim().length > 0;
6
14
  /** Each round gets new native state, never a previous round's consumed values. */
7
15
  export function phaseFixtures(c, prepare) {
8
16
  c.fixtureValues = {};
17
+ c.fixtureSponsors = {};
9
18
  c.clients = new Map();
10
19
  c.nativeAgents = new Set();
11
20
  c.fixtureAuditIds = new Set();
12
21
  c.prepare = async phase => {
13
22
  c.fixtureValues = {};
23
+ c.fixtureSponsors = {};
14
24
  c.clients.clear();
15
25
  if (!prepare) return;
16
26
  try {
17
27
  const before = new Set((await auditRows(c, { agents: [null], includeFixtures: true })).rows.map(row => row.id));
18
- const result = await prepare({ phase, tool: c.tool });
19
- if (!object(result) || Object.keys(result).some(key => !['values', 'clients'].includes(key)) || !object(result.values ?? {}) || !object(result.clients ?? {})) throw new Error('Native phase preparation must return {values,clients}.');
28
+ const result = await prepare({ phase, tool: c.tool, now: seconds(c.now) * 1000 });
29
+ if (!object(result) || Object.keys(result).some(key => !['values', 'clients', 'sponsors'].includes(key)) || !object(result.values ?? {}) || !object(result.clients ?? {}) || !object(result.sponsors ?? {})) throw new Error('Native phase preparation must return {values,clients,sponsors}.');
30
+ for (const [kind, sponsor] of Object.entries(result.sponsors ?? {})) {
31
+ if (!['inactive', 'excluded'].includes(kind) || !object(sponsor) || Object.keys(sponsor).sort().join(',') !== 'email,sub'
32
+ || !text(sponsor.sub) || !text(sponsor.email) || sponsor.email !== sponsor.email.trim().toLowerCase()
33
+ || !/^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/.test(sponsor.email)) throw new Error('Native fixture sponsors require a subject and canonical email for an inactive or excluded person.');
34
+ }
20
35
  for (const [label, client] of Object.entries(result.clients ?? {})) {
21
36
  if (!text(label) || !object(client) || Object.keys(client).sort().join(',') !== 'agent,clientId,token'
22
37
  || !text(client.token) || !text(client.clientId) || !object(client.agent) || !text(client.agent.id) || !text(client.agent.name)
@@ -26,14 +41,18 @@ export function phaseFixtures(c, prepare) {
26
41
  c.nativeAgents.add(client.agent.id);
27
42
  }
28
43
  c.fixtureValues = structuredClone(result.values ?? {});
44
+ c.fixtureSponsors = structuredClone(result.sponsors ?? {});
29
45
  const setup = await auditRows(c, { agents: [...c.clients.values()].map(client => client.agent.id), includeFixtures: true });
30
46
  for (const row of setup.rows) if (!before.has(row.id)) {
31
47
  const client = [...c.clients.values()].find(client => client.agent.id === row.agent?.id);
32
48
  if (row.sponsor?.id !== c.sponsor.id || row.client_id !== client.clientId || row.agent?.name !== client.agent.name || row.agent?.specialist !== client.agent.specialist || row.consent !== null) throw new Error('Native fixture preparation identity differs from its declared client.');
33
49
  c.fixtureAuditIds.add(row.id);
34
50
  }
35
- } catch {
51
+ } catch (error) {
36
52
  c.clients.clear();
53
+ c.fixtureValues = {};
54
+ c.fixtureSponsors = {};
55
+ if (error instanceof FixtureDatabaseError) throw error;
37
56
  throw new Error('Native phase preparation failed; fixture values are not logged.');
38
57
  }
39
58
  };
@@ -19,13 +19,20 @@ import { invocationPhase } from './invocation.mjs';
19
19
  export { validateAgentSurface } from './surface.mjs';
20
20
 
21
21
  /** Run against a fresh, real installation using its HTTP and person doors. */
22
- export async function runAgentDoorConformance({ fetch, invoke, origin, surface, signer, person, sponsors = [person], webhooks, business = 'rer-agent-conformance', consent = randomUUID(), now, prove, nativeJourney, nativeStep, prepare }) {
22
+ export async function runAgentDoorConformance({ fetch, invoke, origin, surface, signer, person, sponsors = [person], webhooks, business = 'rer-agent-conformance', consent = randomUUID(), now, prove, nativeJourney, nativeStep, prepare, initializeAgent }) {
23
23
  const validation = validateAgentSurface(surface), points = [...validation.points];
24
24
  if (!validation.ok) return { ok: false, points };
25
25
  const capabilities = capabilitySet(surface.capabilities);
26
26
  surface = scopedSurface(surface);
27
27
  if (typeof fetch !== 'function' || !signer?.privateKey || !signer.issuer || !signer.installationId || !signer.kid || !person?.email || !person.cookie || typeof person.sub !== 'string' || !person.sub.trim() || typeof consent !== 'string' || !consent.trim() || new URL(origin).origin !== origin) throw new TypeError('Conformance requires fetch, canonical origin, signer, consent and signed-in person {email,cookie,sub}');
28
28
  const c = createProtocol({ fetch, origin, surface, signer, person, business, consent, now, prove, nativeStep, prepare, points, agentId: `rer-conformance-${randomUUID()}`, seq: 0, revision: 0, listed: new Map() });
29
+ if (initializeAgent) {
30
+ try { await initializeAgent({ agentId: c.agentId, person }); }
31
+ catch {
32
+ c.add('BASE-AGENT-001.agent-initializer', false, 'Native agent initialization failed; no captured mail, credentials or fixture values are logged.');
33
+ return { ok: false, points, capabilities };
34
+ }
35
+ }
29
36
  await c.phase('BASE-AGENT-001.transport', () => discoverDoor(c));
30
37
  await c.phase('BASE-AGENT-007.phase0', () => unsetPolicy(c, true));
31
38
  await c.phase('BASE-AGENT-007.phase1', () => removalPhase(c));
@@ -43,6 +50,6 @@ export async function runAgentDoorConformance({ fetch, invoke, origin, surface,
43
50
  await c.phase('BASE-AGENT-subset.refusals', () => refusalPhase(c));
44
51
  if (capabilities.mode === 'declared' && offers(surface.capabilities, 'subscriptions')) await c.phase('BASE-AGENT-subset.subscriptions', () => subscriptionPhase(c));
45
52
  await c.phase('BASE-AGENT-009.phase7', () => auditPhase(c));
46
- if (sponsors.length > 1) await c.phase('BASE-AGENT-004.cross-sponsor', () => crossSponsorPhase(c, sponsors));
53
+ if (sponsors.length > 1) await c.phase('BASE-AGENT-004.cross-sponsor', () => crossSponsorPhase(c, sponsors, initializeAgent));
47
54
  return { ok: points.every(point => point.ok), points, capabilities };
48
55
  }
@@ -17,8 +17,23 @@ function checkOutput(c, step, answer, phase) {
17
17
  const errors = schema === undefined ? ['missing outputSchema'] : schemaErrors(schema, answer.result?.structuredContent);
18
18
  c.add(`BASE-AGENT-002.2.${phase}.${step.id}`, errors.length === 0, `${step.tool} output does not validate: ${errors.slice(0, 8).join('; ')}.`);
19
19
  }
20
- async function checkResources(c, step, answer, phase, held) {
20
+ function fixtureLink(c, template, uri) {
21
+ if (typeof uri !== 'string') return false;
22
+ try {
23
+ const parsed = new URL(uri), declared = new URL(template);
24
+ if (parsed.protocol !== declared.protocol || parsed.host !== declared.host || parsed.username || parsed.password) return false;
25
+ if (['http:', 'https:'].includes(parsed.protocol) && parsed.origin !== c.origin) return false;
26
+ const pattern = template.split(/\{[^}]+\}/).map(part => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('[^/?#]+');
27
+ return new RegExp(`^${pattern}$`).test(uri);
28
+ } catch { return false; }
29
+ }
30
+ async function checkResources(c, step, answer, phase, held, outputs) {
21
31
  const links = answer.result?.content?.filter(item => item.type === 'resource_link') ?? [];
32
+ const fixture = offers(c.surface.capabilities, 'resources') && step.resource;
33
+ const expected = fixture && c.resolve(fixture.state, outputs);
34
+ const matching = fixture ? links.filter(link => fixtureLink(c, fixture.uriTemplate, link.uri)) : [];
35
+ if (fixture) c.add(`BASE-AGENT-003.4.links.${phase}.${step.id}`, matching.length > 0, `${step.tool} must return a resource_link matching its native fixture URI template.`);
36
+ let written = false, withheld = false;
22
37
  for (const [i, link] of links.entries()) {
23
38
  const read = await c.mcp('resources/read', { uri: link.uri }, step.client === undefined ? {} : { client: step.client });
24
39
  const classes = read.decision?.classes, needsApproval = held && classes?.some(item => item.class !== 'standard');
@@ -26,6 +41,13 @@ async function checkResources(c, step, answer, phase, held) {
26
41
  c.add(`BASE-AGENT-003.4.${phase}.${step.id}.${i}`, read.status === 200 && (needsApproval
27
42
  ? errorCode(read) === 'approval_required' && read.decision?.disposition === 'approval' && !read.result?.contents?.length
28
43
  : !read.message?.error && read.decision?.disposition === (held ? 'review' : 'autonomous') && Array.isArray(read.result?.contents) && read.result.contents.length > 0), `${step.tool} resource_link ${link.uri} must ${needsApproval ? 'withhold content pending approval' : 'read back under the current policy'}.`);
44
+ if (fixture && matching.includes(link)) {
45
+ withheld ||= needsApproval;
46
+ written ||= read.status === 200 && !read.message?.error && !errorCode(read) && read.result?.contents?.some(content => {
47
+ if (content.uri !== link.uri || typeof content.text !== 'string') return false;
48
+ try { return subset(JSON.parse(content.text), expected); } catch { return false; }
49
+ });
50
+ }
29
51
  if (needsApproval) {
30
52
  const pending = read.body?.error?.data?.approval;
31
53
  c.add(`BASE-AGENT-007.5.resource.${phase}.${step.id}.${i}`, pending?.id && typeof pending.url === 'string' && new URL(pending.url, c.origin).origin === c.origin && Number.isFinite(Date.parse(pending.expires_at)), `${link.uri} must expose a pending approval on the native person door.`);
@@ -35,6 +57,8 @@ async function checkResources(c, step, answer, phase, held) {
35
57
  }
36
58
  }
37
59
  }
60
+ // Approval-gated content must remain withheld; autonomous reads prove its state.
61
+ if (fixture && !withheld) c.add(`BASE-AGENT-003.4.state.${phase}.${step.id}`, written, `${step.tool} resource_link must read back the fixture's written state through this door's resources/read.`);
38
62
  }
39
63
  export async function runJourney(c, phase, { held = false, injections = false, replay = false } = {}) {
40
64
  const outputs = new Map(); let replayed = false;
@@ -84,7 +108,7 @@ export async function runJourney(c, phase, { held = false, injections = false, r
84
108
  if (answer.result?.isError || answer.result?.structuredContent === undefined) continue;
85
109
  outputs.set(step.id, answer.result.structuredContent);
86
110
  checkOutput(c, step, answer, phase);
87
- await checkResources(c, step, answer, phase, held);
111
+ await checkResources(c, step, answer, phase, held, outputs);
88
112
  if (narrowing) {
89
113
  const witnessArgs = c.resolve(narrowing.witness.args, outputs);
90
114
  c.add(`BASE-AGENT-005.narrowing.${step.tool}.target`, narrowingTargets(declared).every(target => isDeepStrictEqual(witnessArgs[target], args[target])), `${step.tool} must witness every identifier of the same targeted subject.`);
@@ -20,6 +20,31 @@ export async function negativePhase(c) {
20
20
  const answer = await c.mcp('tools/list', {}, { token: await accessToken(c, overrides) });
21
21
  c.add(`BASE-AGENT-004.${name === 'audience' ? '2' : '3'}.${name}`, answer.status === 401, `${name} token returned HTTP ${answer.status}, expected 401.`);
22
22
  }
23
+ const sponsorProbes = [
24
+ ['unknown', { sub: crypto.randomUUID() }],
25
+ ['changed-email', { email: `changed-${crypto.randomUUID()}@example.invalid` }],
26
+ ...Object.entries(c.fixtureSponsors),
27
+ ];
28
+ const undeclared = ['inactive', 'excluded'].filter(kind => !Object.hasOwn(c.fixtureSponsors, kind));
29
+ if (undeclared.length) console.error(`Agent sponsor probes: undeclared=${undeclared.join(',')}; no points run for these states.`);
30
+ for (const [kind, sponsor] of sponsorProbes) {
31
+ // Give each probe an unused agent, so a prior agent/sponsor binding cannot
32
+ // mask missing sponsor admission. Only sponsor claims override its valid
33
+ // token. Signing authority stays in the verifier.
34
+ let ok = false, status;
35
+ try {
36
+ const native = await c.witness();
37
+ const token = await accessToken({ ...c, agentId: `rer-sponsor-probe-${crypto.randomUUID()}` }, sponsor);
38
+ const answer = await c.mcp('tools/list', {}, { token });
39
+ const after = await c.witness(), code = errorCode(answer);
40
+ status = answer.status;
41
+ ok = [401, 403].includes(status)
42
+ && (answer.message?.error?.code === -32001 || typeof code === 'string' && code.length > 0)
43
+ && answer.result === undefined && isDeepStrictEqual(native, after);
44
+ } catch { /* Native and fixture diagnostics may contain private identities. */ }
45
+ c.add(`BASE-AGENT-004.sponsor.${kind}`, ok,
46
+ `Sponsor ${kind} must return HTTP 401 or 403 with a typed error and leave native state unchanged${status === undefined ? '' : ` (HTTP ${status})`}.`);
47
+ }
23
48
  const write = c.surface.journey.find(step => !c.surface.tools.find(tool => tool.name === step.tool)?.readOnly);
24
49
  if (write) {
25
50
  const answer = await c.tool(write.tool, c.args(write, new Map(), { unresolved: true }), { token: await accessToken(c, { scope: 'events:read' }) });
@@ -44,6 +69,9 @@ export async function negativePhase(c) {
44
69
  const foreign = await c.request('/api/rer/agent-policy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ policy: foreignBusiness.policy }) });
45
70
  c.add('BASE-AGENT-007.8.business', !foreign.ok, 'An envelope whose Business does not match its revision signature was accepted.');
46
71
  await foreign.body?.cancel();
47
- c.add('BASE-AGENT-007.8.state', isDeepStrictEqual(state, await c.personJson('/agents/api/state')), 'An invalid envelope changed the adopted policy state or revision.');
72
+ // Slow requests may renew the adopted envelope before the second state read.
73
+ // Only its acknowledged expiry may change; every other field stays strict.
74
+ const sameState = actual => isDeepStrictEqual({ ...state, policy: { ...state.policy, expires_at: new Date(c.adoptedPolicy.exp * 1000).toISOString() } }, actual);
75
+ c.add('BASE-AGENT-007.8.state', sameState(await c.personJson('/agents/api/state')), 'An invalid envelope changed the adopted policy state or revision.');
48
76
  c.add('BASE-AGENT-004.negative-no-effect', isDeepStrictEqual(before, await c.witness()), 'An authentication/person-door/policy-envelope negative changed native state.');
49
77
  }
@@ -1,5 +1,6 @@
1
1
  import { isDeepStrictEqual } from 'node:util';
2
- import { autonomousPolicy } from './crypto.mjs';
2
+ import { webcrypto } from 'node:crypto';
3
+ import { autonomousPolicy, policyEnvelope, signJws, seconds } from './crypto.mjs';
3
4
  import { errorCode } from './protocol.mjs';
4
5
  import { nativeStepHook } from './step-hooks.mjs';
5
6
 
@@ -29,6 +30,47 @@ export async function removalPhase(c) {
29
30
  await unsetPolicy(c);
30
31
  await c.adopt(autonomousPolicy());
31
32
  c.add('BASE-AGENT-007.2.unset-no-effect', isDeepStrictEqual(before, await c.witness()), 'Calls under the removed policy changed native state.');
33
+ await renewalRefusals(c);
34
+ }
35
+ async function renewalRefusals(c) {
36
+ const state = await c.personJson('/agents/api/state'), approvals = await c.personJson('/agents/api/approvals'), before = await c.witness();
37
+ const sign = payload => signJws(c.signer.privateKey, c.signer.kid, 'rer-agent-policy+jwt', payload);
38
+ const changed = autonomousPolicy(); changed.rules = [{ id: 'all', class: '*', do: 'blocked' }];
39
+ const foreign = await webcrypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']);
40
+ const cases = [
41
+ ['expired', (_adopted, later) => sign({ ...later, exp: seconds(c.now) - 1 }), 'policy_invalid', 400],
42
+ ['untrusted', (_adopted, later) => signJws(foreign.privateKey, c.signer.kid, 'rer-agent-policy+jwt', later), 'policy_invalid', 400],
43
+ ['revision-mismatch', (_adopted, later) => sign({ ...later, document: changed }), 'policy_revision_invalid', 400],
44
+ ['equal-iat', (adopted, later) => sign({ ...later, iat: adopted.iat }), 'policy_stale', 409],
45
+ ['earlier-iat', (adopted, later) => sign({ ...later, iat: adopted.iat - 1 }), 'policy_stale', 409],
46
+ ['document', (_adopted, _later, options) => policyEnvelope(c, changed, options).then(signed => signed.policy), 'policy_stale', 409],
47
+ ['revision', (adopted, _later, options) => policyEnvelope(c, adopted.document, { ...options, revision: adopted.revision + 1 }).then(signed => signed.policy), 'policy_stale', 409],
48
+ ['business', (adopted, _later, options) => policyEnvelope({ ...c, business: `${c.business}-other` }, adopted.document, options).then(signed => signed.policy), 'policy_stale', 409],
49
+ ['paused', (adopted, later) => sign({ ...later, paused: !adopted.paused }), 'policy_stale', 409],
50
+ ['revoked', (_adopted, later) => sign({ ...later, revoked: { agents: [c.agentId], consents: [c.consent] } }), 'policy_stale', 409],
51
+ ['revision-signature', async (adopted, later) => {
52
+ const claims = JSON.parse(Buffer.from(adopted.revision_signature.split('.')[1], 'base64url').toString());
53
+ return sign({ ...later, revision_signature: await signJws(c.signer.privateKey, c.signer.kid, 'rer-agent-policy-revision+jwt', claims) });
54
+ }, 'policy_stale', 409],
55
+ ['untrusted-revision', async (adopted, later, options) => {
56
+ const signed = await policyEnvelope({ ...c, signer: { ...c.signer, privateKey: foreign.privateKey } }, adopted.document, options);
57
+ return sign({ ...later, revision_signature: signed.payload.revision_signature });
58
+ }, 'policy_revision_invalid', 400],
59
+ ];
60
+ for (const [name, candidate, code, status] of cases) {
61
+ await c.renewPolicy();
62
+ const adopted = c.adoptedPolicy, later = { ...adopted, iat: adopted.iat + 1, exp: seconds(c.now) + 300 };
63
+ const policy = await candidate(adopted, later, { seq: adopted.seq, revision: adopted.revision, iat: later.iat, exp: later.exp });
64
+ const response = await c.request('/api/rer/agent-policy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ policy }) });
65
+ const body = await response.json();
66
+ const call = await c.tool(c.surface.witness.tool, c.resolve(c.surface.witness.args));
67
+ // Slow requests may legitimately renew between probes. Only the verifier's
68
+ // acknowledged renewal may change the expected expiry, never authority.
69
+ const sameState = actual => isDeepStrictEqual({ ...state, policy: { ...state.policy, expires_at: new Date(c.adoptedPolicy.exp * 1000).toISOString() } }, actual);
70
+ c.add(`BASE-AGENT-007.7.renew.${name}`, response.status === status && body.code === code &&
71
+ sameState(await c.personJson('/agents/api/state')) && isDeepStrictEqual(approvals, await c.personJson('/agents/api/approvals')) &&
72
+ !call.result?.isError && isDeepStrictEqual(before, call.result?.structuredContent), `${name} must refuse ${code} without changing policy, approvals or native state.`);
73
+ }
32
74
  }
33
75
  export async function blockedPhases(c) {
34
76
  const nonstandard = [...new Set(c.surface.tools.flatMap(tool => tool.classes))].filter(cls => cls !== 'standard');
@@ -1,8 +1,9 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { isDeepStrictEqual } from 'node:util';
3
- import { accessToken, policyEnvelope } from './crypto.mjs';
3
+ import { accessToken, policyEnvelope, renewalEnvelope, seconds } from './crypto.mjs';
4
4
  import { AGENT_CLASSES } from './surface.mjs';
5
5
  import { phaseFixtures } from './fixtures.mjs';
6
+ import { validToolName } from './tool-name.mjs';
6
7
 
7
8
  export const VERSION = '2026-07-28';
8
9
  export const DECISION = 'com.runeditrun/decision';
@@ -62,6 +63,7 @@ export function createProtocol(context) {
62
63
  // A 429 is a capacity refusal, not an action result. Honor only a bounded,
63
64
  // explicit Retry-After; never replay an application or policy refusal.
64
65
  for (let attempt = 0; ; attempt++) {
66
+ if (new URL(path, context.origin).pathname !== '/api/rer/agent-policy') await context.renewPolicy();
65
67
  const response = await context.fetch(new URL(path, `${context.origin}/`).href, { redirect: 'manual', ...init });
66
68
  if (response.status !== 429) return response;
67
69
  const header = response.headers.get('retry-after'), delay = header && /^\d+(?:\.\d+)?$/.test(header) ? Number(header) * 1000 : header ? Date.parse(header) - Date.now() : NaN;
@@ -81,8 +83,9 @@ export function createProtocol(context) {
81
83
  if (!valid) throw new Error('Cannot decide an approval without its exact summary evidence');
82
84
  const response = await context.personRequest(`${path}/${action}`, { method: 'POST', body: JSON.stringify({ revision: approval.revision, summary_sha256: approval.summary_sha256 }) });
83
85
  const body = await responseBody(response);
84
- if (response.ok && action === 'approve' && body.approval?.state === 'executed') context.calls.push({
86
+ if (response.ok && action === 'approve' && approval.state === 'pending' && ['executed', 'refused'].includes(body.approval?.state)) context.calls.push({
85
87
  kind: 'approval/execute', name: approval.tool, decision: structuredClone(pending.decision), approvalId: handle.id, principal: pending.principal,
88
+ state: body.approval.state, code: body.approval.code ?? null,
86
89
  });
87
90
  return { ok: response.ok, status: response.status, body };
88
91
  };
@@ -133,19 +136,52 @@ export function createProtocol(context) {
133
136
  if (answer.result?.isError || answer.result?.structuredContent === undefined) throw new Error(`Witness ${context.surface.witness.tool} failed (${errorCode(answer) ?? answer.status})`);
134
137
  return answer.result.structuredContent;
135
138
  };
136
- context.adopt = async (document, options = {}) => {
137
- const signed = await policyEnvelope(context, document, options);
139
+ const postPolicy = async signed => {
138
140
  const response = await context.request('/api/rer/agent-policy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ policy: signed.policy }) });
139
141
  const body = await responseBody(response);
140
142
  if (!response.ok) throw new Error(`Policy adoption failed: HTTP ${response.status} (${typeof body.code === 'string' ? body.code : 'invalid response'})`);
143
+ if (!isDeepStrictEqual(body.adopted, { seq: signed.payload.seq, revision: signed.payload.revision })) throw new Error('Policy adoption acknowledgement differs from its envelope');
144
+ context.adoptedPolicy = structuredClone(signed.payload);
145
+ return body;
146
+ };
147
+ let renewing;
148
+ context.renewPolicy = async () => {
149
+ if (renewing) return renewing;
150
+ const adopted = context.adoptedPolicy, now = seconds(context.now);
151
+ if (!adopted || adopted.exp - now >= 60 || now <= adopted.iat) return;
152
+ renewing = (async () => { await postPolicy(await renewalEnvelope(context, adopted)); })();
153
+ try { await renewing; } finally { renewing = undefined; }
154
+ };
155
+ context.adopt = async (document, options = {}) => {
156
+ const signed = await policyEnvelope(context, document, options);
157
+ const body = await postPolicy(signed);
141
158
  context.seq = signed.payload.seq; context.revision = signed.payload.revision; context.document = document;
142
- const expectedClasses = [...new Set(context.surface.tools.flatMap(tool => tool.classes))];
143
159
  const actualClasses = body.door?.classes;
144
- const validClasses = Array.isArray(actualClasses) && new Set(actualClasses.map(row => row?.class)).size === actualClasses.length && actualClasses.every(row =>
160
+ const declared = new Set(context.surface.tools.map(tool => tool.name));
161
+ const shaped = Array.isArray(actualClasses) && new Set(actualClasses.map(row => row?.class)).size === actualClasses.length && actualClasses.every(row =>
145
162
  row && Object.keys(row).sort().join(',') === 'class,tools' && AGENT_CLASSES.includes(row.class) && Array.isArray(row.tools) &&
146
- isDeepStrictEqual([...row.tools].sort(), context.surface.tools.filter(tool => tool.classes.includes(row.class)).map(tool => tool.name).sort().slice(0, 20))) &&
163
+ row.tools.length <= 20 && new Set(row.tools).size === row.tools.length && row.tools.every(name => validToolName(name, context.surface.seed)));
164
+ // The policy answer declares native-only tools too. Retain that declaration
165
+ // across revisions; it must never expand the edition's public MCP surface.
166
+ const invokeTools = context.invokeTools ?? (shaped ? [...new Set(actualClasses.flatMap(row => row.tools).filter(name => !declared.has(name)))].map(name => ({
167
+ name, classes: actualClasses.filter(row => row.tools.includes(name)).map(row => row.class),
168
+ })) : []);
169
+ const policyTools = [...context.surface.tools, ...invokeTools];
170
+ const expectedClasses = [...new Set(policyTools.flatMap(tool => tool.classes))];
171
+ const validClasses = shaped && actualClasses.every(row =>
172
+ isDeepStrictEqual([...row.tools].sort(), policyTools.filter(tool => tool.classes.includes(row.class)).map(tool => tool.name).sort().slice(0, 20))) &&
147
173
  expectedClasses.every(cls => actualClasses.some(row => row.class === cls));
148
- context.add(`BASE-AGENT-007.7.adopt.${context.seq}`, Object.keys(body).sort().join(',') === 'adopted,door' && isDeepStrictEqual(body.adopted, { seq: context.seq, revision: context.revision }) && validClasses, 'Policy answer must contain exactly adopted {seq,revision} and catalogue classes with the exact declared tool mappings; resource-only classes have no tools.');
174
+ let hidden = true;
175
+ if (invokeTools.length) {
176
+ const listed = await context.mcp('tools/list');
177
+ hidden = listed.status === 401 && listed.result === undefined || Array.isArray(listed.result?.tools) && invokeTools.every(tool => !listed.result.tools.some(item => item.name === tool.name));
178
+ for (const tool of invokeTools) {
179
+ const answer = await context.tool(tool.name, {});
180
+ hidden &&= answer.status === 400 && errorCode(answer) === 'tool_not_found' && answer.result === undefined && !answer.decision;
181
+ }
182
+ }
183
+ if (validClasses && hidden) context.invokeTools = invokeTools;
184
+ context.add(`BASE-AGENT-007.7.adopt.${context.seq}`, Object.keys(body).sort().join(',') === 'adopted,door' && isDeepStrictEqual(body.adopted, { seq: context.seq, revision: context.revision }) && validClasses && hidden, 'Policy answer must contain exactly adopted {seq,revision} and catalogue classes with the exact declared tool mappings; native-only tools must remain absent from MCP discovery and calls, and resource-only classes have no tools.');
149
185
  return signed;
150
186
  };
151
187
  phaseFixtures(context, context.prepare);
@@ -6,12 +6,23 @@ import { createProtocol, errorCode, APPROVAL } from './protocol.mjs';
6
6
 
7
7
  /** Additional principals use real native sessions and signed credentials. All
8
8
  * record and approval probes cross the same public doors as the main journey. */
9
- export async function crossSponsorPhase(c, sponsors) {
9
+ export async function crossSponsorPhase(c, sponsors, initializeAgent) {
10
10
  const declaration = c.surface.verification?.crossSponsor;
11
11
  if (!declaration || !Array.isArray(declaration.prepare) || !declaration.prepare.length || !declaration.read || !declaration.change) throw new Error('Multiple sponsors require verification.crossSponsor {prepare,read,change} fixtures.');
12
12
  for (const person of sponsors.slice(1)) {
13
13
  const b = createProtocol({ fetch: c.fetch, origin: c.origin, surface: c.surface, signer: c.signer, person,
14
14
  business: c.business, consent: randomUUID(), now: c.now, points: c.points, agentId: `rer-conformance-${randomUUID()}`, seq: c.seq, revision: c.revision, listed: c.listed });
15
+ if (initializeAgent) {
16
+ try { await initializeAgent({ agentId: b.agentId, person }); }
17
+ catch {
18
+ c.add('BASE-AGENT-001.agent-initializer', false, 'Native agent initialization failed; no captured mail, credentials or fixture values are logged.');
19
+ return;
20
+ }
21
+ }
22
+ b.renewPolicy = async () => {
23
+ await c.renewPolicy();
24
+ b.seq = c.seq; b.revision = c.revision; b.document = c.document;
25
+ };
15
26
  const prefix = `BASE-AGENT-004.cross-sponsor.${person.label}`;
16
27
  await c.adopt(autonomousPolicy());
17
28
  b.revision = c.revision;
@@ -1,4 +1,6 @@
1
+ import { seconds } from './crypto.mjs';
2
+
1
3
  export async function nativeStepHook(c, stage, phase, step, args, outputs, result) {
2
4
  if (!c.nativeStep) return;
3
- await c.nativeStep({ stage, phase, step: structuredClone(step), args: structuredClone(args), outputs: structuredClone(Object.fromEntries(outputs)), ...(result === undefined ? {} : { result: structuredClone(result) }), tool: c.tool });
5
+ await c.nativeStep({ stage, phase, now: seconds(c.now) * 1000, step: structuredClone(step), args: structuredClone(args), outputs: structuredClone(Object.fromEntries(outputs)), ...(result === undefined ? {} : { result: structuredClone(result) }), tool: c.tool });
4
6
  }
@@ -63,6 +63,12 @@ export function validateAgentSurface(surface, { contract } = {}) {
63
63
  if (step?.client !== undefined) add(`journey.${id}.client`, text(step.client) && text(surface.verification?.fixtures?.module), `${id} requires a named native client and verification.fixtures.`);
64
64
  for (const item of expect) coverage.get(step?.tool)?.add(item?.class);
65
65
  checkReferences(step?.args, `journey.${id}`, steps);
66
+ if (step?.resource !== undefined && offers(surface.capabilities, 'resources')) {
67
+ const fixture = step.resource;
68
+ const resourcePoint = (suffix, ok, detail) => points.push({ id: `BASE-AGENT-003.4.fixture.${id}.${suffix}`, ok: Boolean(ok), ...(!ok ? { detail } : {}) });
69
+ resourcePoint('state', tool?.readOnly === false && object(fixture) && Object.keys(fixture).sort().join(',') === 'state,uriTemplate' && surface.resources?.includes(fixture.uriTemplate) && object(fixture.state) && Object.keys(fixture.state).length > 0, `${id}.resource must declare a native URI template and nonempty expected written state on a mutating tool.`);
70
+ checkReferences(fixture?.state, `journey.${id}.resource`, new Set([...steps, id]), resourcePoint);
71
+ }
66
72
  if (step?.target !== undefined) {
67
73
  const target = step.target;
68
74
  add(`journey.${id}.target`, object(target) && Object.keys(target).sort().join(',') === 'id,kind' && text(target.kind) && (text(target.id) || object(target.id) && ('$from' in target.id || '$sponsor' in target.id || '$fixture' in target.id)), `${id}.target must contain only a nonempty kind and an id string or journey reference.`);
@@ -92,6 +98,10 @@ export function validateAgentSurface(surface, { contract } = {}) {
92
98
  for (const [name, tool] of tools) for (const cls of Array.isArray(tool?.classes) ? tool.classes : []) add(`${name}.coverage.${cls}`, coverage.get(name).has(cls), `${name} has no journey fixture for declared class ${cls}.`);
93
99
  if (offers(surface.capabilities, 'events')) add('events', tools.has(`${surface.seed}.events.list`), `Declare built-in ${surface.seed}.events.list in the Agent surface.`);
94
100
  if (set.mode === 'declared' && offers(surface.capabilities, 'resources')) add('resources.offered', surface.resources.length > 0, 'Declared resources must offer at least one resource template.');
101
+ if (offers(surface.capabilities, 'resources') && surface.resources?.length && [...tools.values()].some(tool => tool.readOnly === false)) {
102
+ const ok = surface.journey?.some(step => tools.get(step.tool)?.readOnly === false && step.resource !== undefined);
103
+ points.push({ id: 'BASE-AGENT-003.4.fixture', ok: Boolean(ok), ...(!ok ? { detail: 'A writable door with resources must declare a record-writing journey resource fixture and its expected written state.' } : {}) });
104
+ }
95
105
  add('witness', object(surface.witness) && tools.get(surface.witness.tool)?.readOnly === true && object(surface.witness.args), 'witness must name a declared readOnly tool and arguments.');
96
106
  checkReferences(surface.witness?.args, 'witness', new Set());
97
107
  return { ok: points.every(point => point.ok), points };
@@ -4,6 +4,7 @@ import { execFileSync } from 'node:child_process';
4
4
  import { resolve, join } from 'node:path';
5
5
  import { pathToFileURL } from 'node:url';
6
6
  import { parseArgs } from 'node:util';
7
+ import { AsyncLocalStorage } from 'node:async_hooks';
7
8
  import { assert, hexId } from './validation.mjs';
8
9
  import { regularBytes } from './artifact.mjs';
9
10
  import { packageBuild } from './prepare.mjs';
@@ -21,6 +22,11 @@ import { publicOrigin } from './public-origin.mjs';
21
22
  export const cronNotice = 'This Worker has a Cron Trigger. Cloudflare may take up to about two hours to start its first scheduled run on some accounts.';
22
23
  const usage = 'usage: owner-cli.mjs deploy|dry-run|local-plan|health|remove|inspect|record-owner|signin|rekey --declaration <file> --environment <name> [--product <dir>] [--state <file>] [--outcome claimed|kept] | secret ... --set NAME [--set NAME]|--unset NAME [--unset NAME]';
23
24
 
25
+ // The public offline command also contains legacy hooks that call kit dry-run.
26
+ // Keep their plan local without changing direct kit callers or real deploys.
27
+ const localPlans = new AsyncLocalStorage();
28
+ export const withLocalDeployPlan = run => localPlans.run(true, run);
29
+
24
30
  /** The owner's committed source and existing local build. Run the seed's build first. */
25
31
  async function ownerArtifact(root, product) {
26
32
  const git = (...args) => { try { return execFileSync('git',args,{cwd:root,encoding:'utf8',stdio:['ignore','pipe','pipe']}).trim(); } catch { throw new Error('owner-source-not-a-git-checkout'); } };
@@ -34,8 +40,10 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
34
40
  let parsed;
35
41
  try { parsed = parseArgs({args:argv,allowPositionals:true,strict:true,options:{declaration:{type:'string'},environment:{type:'string'},product:{type:'string',default:''},state:{type:'string'},set:{type:'string',multiple:true},unset:{type:'string',multiple:true},outcome:{type:'string'}}}); }
36
42
  catch { throw new Error('usage'); }
37
- const {positionals:[command,...rest],values} = parsed;
43
+ const {positionals:[requestedCommand,...rest],values} = parsed;
44
+ const command = localPlans.getStore() && requestedCommand === 'dry-run' ? 'local-plan' : requestedCommand;
38
45
  if (!['deploy','dry-run','local-plan','health','secret','remove','inspect','record-owner','signin','rekey'].includes(command) || rest.length || !values.declaration || !values.environment) throw new Error('usage');
46
+ if (localPlans.getStore() && command !== 'local-plan') throw new Error('offline-deploy-refuses-provider-operation');
39
47
  if ((command === 'secret') !== (values.set !== undefined || values.unset !== undefined) || values.set !== undefined && values.unset !== undefined) throw new Error('usage');
40
48
  if (values.outcome !== undefined && command !== 'record-owner' || command === 'record-owner' && !['claimed','kept'].includes(values.outcome)) throw new Error('usage');
41
49
  await refuseLocalModels(resolve(cwd));
@@ -25,12 +25,13 @@ retained exceptions and effects outside D1 and R2. Clauses are in
25
25
 
26
26
  ```js
27
27
  import { defineErasure, createErasure } from './vendor/base/modules/erasure/index.mjs';
28
+ const fileColumn = 'storage_key';
28
29
  export const person = defineErasure({
29
30
  entity: 'person',
30
31
  root: { table: 'people' },
31
32
  edges: [
32
33
  { table: 'person_notes', from: 'people', on: { person_id: 'id' } },
33
- { table: 'person_files', from: 'people', on: { person_id: 'id' }, blob: { bucket: 'FILES', keyColumn: 'storage_key' } },
34
+ { table: 'person_files', from: 'people', on: { person_id: 'id' }, blob: { bucket: 'FILES', keyColumn: fileColumn } },
34
35
  { table: 'signals', from: 'people', where: 'instr(payload_json,?)>0', params: ['email'] },
35
36
  ],
36
37
  redact: [{ table: 'messages', from: 'people', on: { author_id: 'id' }, set: { body: '' }, reason: 'transcript order is kept' }],
@@ -122,8 +123,9 @@ the declaration erases both together.
122
123
  ```js
123
124
  import { rateEdges, rateWrite } from './vendor/base/modules/erasure/index.mjs';
124
125
  const root = { table: 'people', column: 'id' };
126
+ const counterColumn = 'key';
125
127
  // in the declaration
126
- edges: [...rateEdges({ entity: 'person', root, counters: [{ table: 'rate_limits', keyColumn: 'key' }] })],
128
+ edges: [...rateEdges({ entity: 'person', root, counters: [{ table: 'rate_limits', keyColumn: counterColumn }] })],
127
129
  // at every write whose key identifies a known person
128
130
  const [counted] = await rateWrite(db, { entity: 'person', root, subject: person.id, rateTable: 'rate_limits', rateKey: key },
129
131
  [db.prepare('INSERT INTO rate_limits(key,window,count) VALUES(?,?,1) ON CONFLICT(key) DO UPDATE SET count=count+1 RETURNING count').bind(key, window)]);
@@ -10,6 +10,7 @@ again as its next migration.
10
10
 
11
11
  ```js
12
12
  import { createOperatorAuth, createAuthHandler, createSignInHandler } from './modules/operator-auth/index.mjs';
13
+ const clientAuthMethod = 'client_secret_basic';
13
14
  const auth = createOperatorAuth({
14
15
  db: env.DB, clientIp, mailSender,
15
16
  // The runtime's fetch; on workerd use redirect 'manual' and refuse every 3xx.
@@ -22,7 +23,7 @@ const auth = createOperatorAuth({
22
23
  providers: [
23
24
  // The app's private key (a Worker secret) authenticates it: no shared secret exists.
24
25
  { id: 'auth', preset: 'auth', issuer: env.IDENTITY_PROVIDER, clientId: env.IDENTITY_CLIENT_ID, privateKey: JSON.parse(env.IDENTITY_PRIVATE_KEY) },
25
- { id: 'google', preset: 'google', clientId: env.GOOGLE_CLIENT_ID, tokenAuthMethod: 'client_secret_basic', clientSecret: env.GOOGLE_CLIENT_SECRET },
26
+ { id: 'google', preset: 'google', clientId: env.GOOGLE_CLIENT_ID, tokenAuthMethod: clientAuthMethod, clientSecret: env.GOOGLE_CLIENT_SECRET },
26
27
  { id: 'corp', preset: 'okta', issuer: 'https://corp.okta.com', clientId: env.OKTA_CLIENT_ID, privateKey: JSON.parse(env.OKTA_PRIVATE_KEY),
27
28
  subjectEmails: { '00u1abcd': 'operator@corp.example' } },
28
29
  ],
@@ -249,7 +249,15 @@ provider operation.
249
249
  | Unknown hash/name, ambiguous hash/alias or conflicting name/hash | `migration_ledger:unknown`, `:ambiguous` or `:conflict`; use the matching checkout and declaration. |
250
250
 
251
251
  Refusals throw `PortabilityError` with status 409 and a constant `code`;
252
- ledger values never enter these diagnostics. Propagate failures and stop before
252
+ ledger values never enter these diagnostics. `migration` names a pending
253
+ checkout file, and `identity` may name a checkout
254
+ file, a declared alias or the known `rer_migrations` table. The worker appends
255
+ these safe identities to its public refusal token, retaining
256
+ `migration_ledger:unapplied:<file>`, `migration_ledger:multiple:rer_migrations`
257
+ and named `unknown`, `ambiguous` or `conflict` tokens only when the name exactly
258
+ equals a checkout filename or declared alias. Historical
259
+ `migration_ledger:unknown:initial-page.sql` stays category-only without that
260
+ provenance. Propagate failures and stop before
253
261
  native apply. IO and provider failures also propagate. A successful check
254
262
  creates no ledger, changes no row and does not attest unrecorded historical
255
263
  bytes in a plain Wrangler ledger. It never silently skips pending migrations.
@@ -367,7 +375,7 @@ extra-payload recovery responsibilities and transfer erasure behavior still appl
367
375
  The UTF-8 JSON-lines archive has a manifest, indexed checksummed parts and a
368
376
  closing record. `readArchive(stream)` uses the shared bounded, fatal UTF-8 line
369
377
  reader. The default maximum line size is 24 MiB; callers can set a smaller bound.
370
- No runtime or CLI materializes the whole archive. The manifest opens the archive and holds the final part count and payload hashes, so an export builds all its parts in the job before any byte is written; `exportArchive` and `importArchive` take an optional `progress` callback (`progress.mjs` turns its events into throttled lines with a time-left estimate). Readers enforce a 16 MiB
378
+ Current-archive paths do not materialize the whole archive; the legacy ledger collector described below buffers its initial upload. The manifest opens the archive and holds the final part count and payload hashes, so an export builds all its parts in the job before any byte is written; `exportArchive` and `importArchive` take an optional `progress` callback (`progress.mjs` turns its events into throttled lines with a time-left estimate). Readers enforce a 16 MiB
371
379
  canonical UTF-8 part limit; `readArchive(stream,{partBytes})` can lower it.
372
380
  Import uploads enforce the policy's part limit before any staging write.
373
381
  A part has at most 1,000 rows;
@@ -400,6 +408,14 @@ Comments and harmless inter-token whitespace therefore compare equally, while
400
408
  `x '31'` and `x'31'`, separated minus signs, numbers and combined operators stay
401
409
  distinct. No regular expression rewrites SQL.
402
410
 
411
+ Restore may omit an archived schema object from its compatibility comparison
412
+ only when its table is in the archive's excluded inventory, the destination
413
+ policy also excludes it, neither side restores it, and the destination lacks
414
+ it. This permits transient export snapshots without installing source SQL.
415
+ Objects on restorable tables and excluded tables present at the destination
416
+ remain verified against the unchanged live and migration-replay catalogues;
417
+ the original manifest and parts still supply the archive digest.
418
+
403
419
  Quoted identifiers (double quotes, backticks, brackets, or a single-quoted string
404
420
  used as a name), strings, blobs, numbers, variables and ordinary identifiers keep
405
421
  their exact spelling. Only unambiguous reserved keywords fold ASCII case.
@@ -548,6 +564,37 @@ archive's tables restore with their archived columns, later columns take their
548
564
  defaults, and later tables stay empty unless their eligible seed or backfill is
549
565
  replayed below. This applies to normal v2 import and to backup-only v1 recovery.
550
566
 
567
+ The Node in-process library door also verifies old v2 archives carrying a
568
+ hash-bearing migration ledger without caller-supplied replay or ledger rows.
569
+ `createPortability({db,policy})` reads the checkout's single D1 migration directory
570
+ from `wrangler.jsonc` (or use `migrationRoot` to name the checkout), matches its
571
+ files to the destination ledger, and collects the original parts in memory until
572
+ finalize authenticates their hashes and closing binding. The archived ledger must
573
+ be a hash-matched prefix; recorded replay must reproduce the archived tables and
574
+ full replay the complete destination identity before any durable import job is
575
+ created. Keep the same service instance through this initial upload; restarting
576
+ it requires uploading again. Acknowledged recovery and runner-supplied evidence
577
+ retain their existing paths.
578
+
579
+ Worker evidence remains pending: a Worker has neither checkout files nor Node
580
+ SQLite, and `db` plus `policy` cannot reveal migration SQL that is absent from the
581
+ live schema. The required refresh/lock integration should generate a seed-owned
582
+ module containing ordered file hashes, SQL statements, complete sqlite_master
583
+ inventories and seeded row images for every migration prefix. Bind that artifact
584
+ to the destination's hashed ledger, apply the runtime policy to each inventory,
585
+ and verify both historical and complete live identities before admitting the
586
+ same collector. Do not import archive SQL or substitute the live schema for a
587
+ historical replay. Until that artifact is wired into seed builds, this automatic
588
+ library path refuses in Workers with `schema_mismatch:legacy_evidence`.
589
+
590
+ Later data statements also qualify when a surviving trigger from that same
591
+ migration repeats the complete statement block, in order, on Base's restore
592
+ commit transition. Those statements run through that destination-owned trigger,
593
+ not a second finalize replay.
594
+ Absent tables count as empty only with an empty full replay and proof that every
595
+ later insert and possible trigger writer preserves emptiness; a bounded empty
596
+ copy/drop/rename rebuild and a SELECT from such an empty source carry no row.
597
+
551
598
  The Base CLI runners (local Workers and container) pass `createPortability` a
552
599
  `migrations` replayer (`migration-history.mjs`, Node only) built from the
553
600
  destination's applied migration ledger and migration files, with each file's
@@ -1018,6 +1065,7 @@ modules/portability/schema-identity.mjs
1018
1065
  modules/portability/schema-legacy.mjs
1019
1066
  modules/portability/sqlite-tokens.mjs
1020
1067
  modules/portability/bookkeeping.mjs
1068
+ modules/portability/snapshot-objects.mjs
1021
1069
  modules/portability/storage.mjs
1022
1070
  modules/portability/copies.mjs
1023
1071
  modules/portability/derived.mjs
@@ -1027,6 +1075,8 @@ modules/portability/policy-data.mjs
1027
1075
  modules/portability/generated.mjs
1028
1076
  modules/portability/restore-safety.mjs
1029
1077
  modules/portability/bridge.mjs
1078
+ modules/portability/library-history.mjs
1079
+ modules/portability/migration-history.mjs
1030
1080
  modules/portability/ledgers.mjs
1031
1081
  modules/portability/migration-statements.mjs
1032
1082
  modules/portability/sql-text.mjs