@runeditrun/rer 0.2.91 → 0.2.94
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.
- package/cli/README.md +1 -1
- package/cli/agent-door.mjs +20 -10
- package/cli/agent-history-database.mjs +82 -0
- package/cli/agent-history-isolation.mjs +68 -0
- package/cli/agent-history.mjs +58 -0
- package/cli/agent-journey-fixtures.mjs +3 -3
- package/cli/agent-network.mjs +12 -6
- package/cli/business/choices.mjs +3 -3
- package/cli/business/command.mjs +19 -0
- package/cli/business/setup-command.mjs +14 -1
- package/cli/deploy-command.mjs +19 -8
- package/cli/deploy.mjs +1 -1
- package/cli/export.mjs +1 -1
- package/cli/hosted-hosts.mjs +21 -0
- package/cli/hosted-output.mjs +91 -8
- package/cli/ledger-refusal.mjs +1 -1
- package/cli/lifecycle.mjs +5 -0
- package/cli/local.mjs +1 -1
- package/cli/port.mjs +1 -2
- package/cli/portability-evidence.d.mts +5 -0
- package/cli/portability-evidence.mjs +85 -0
- package/cli/portability-migrations.mjs +21 -9
- package/cli/portability-worker.mjs +4 -2
- package/edition-authoring/module-edges.mjs +14 -1
- package/foundation/runtime/contract-anchor.mjs +87 -0
- package/foundation/runtime/contract-docs.mjs +46 -16
- package/global/CHANGELOG.md +63 -0
- package/install.sh +4 -1
- package/modules/agent-door/conformance/crypto.mjs +14 -2
- package/modules/agent-door/conformance/injection.mjs +5 -3
- package/modules/agent-door/conformance/journey.mjs +27 -3
- package/modules/agent-door/conformance/negative.mjs +4 -1
- package/modules/agent-door/conformance/policy-phases.mjs +43 -1
- package/modules/agent-door/conformance/protocol.mjs +42 -7
- package/modules/agent-door/conformance/sponsors.mjs +4 -0
- package/modules/agent-door/conformance/surface.mjs +10 -0
- package/modules/deploy-kit/README.md +1 -1
- package/modules/deploy-kit/owner-cli.mjs +9 -1
- package/modules/deploy-kit/prepare.mjs +5 -2
- package/modules/erasure/README.md +4 -2
- package/modules/operator-auth/identity/oidc/README.md +2 -1
- package/modules/portability/README.md +84 -2
- package/modules/portability/backfills.mjs +96 -3
- package/modules/portability/bridge.mjs +3 -1
- package/modules/portability/catalogue.mjs +11 -1
- package/modules/portability/checkout-history.mjs +14 -0
- package/modules/portability/format.mjs +1 -0
- package/modules/portability/import.mjs +58 -3
- package/modules/portability/index.d.mts +3 -1
- package/modules/portability/index.mjs +16 -5
- package/modules/portability/library-history.mjs +147 -0
- package/modules/portability/migration-history.mjs +3 -2
- package/modules/portability/restore-safety.mjs +6 -3
- package/modules/portability/snapshot-objects.mjs +16 -0
- package/modules/portability/worker-history.d.mts +9 -0
- package/modules/portability/worker-history.mjs +29 -0
- package/onboarding/adapter.mjs +28 -14
- package/package.json +9 -1
package/cli/hosted-output.mjs
CHANGED
|
@@ -9,8 +9,13 @@ import { migrate } from '../modules/container/migrations.mjs';
|
|
|
9
9
|
import { readMigrationRewrites, withRewrites } from '../modules/deploy-kit/migrations.mjs';
|
|
10
10
|
import { RerError } from './errors.mjs';
|
|
11
11
|
import { hostedMailCredentials } from './hosted-mail.mjs';
|
|
12
|
+
import { deliveryDeclarationSources } from './delivery-declarations.mjs';
|
|
13
|
+
import { sourcePath } from '../modules/delivery-producer/migrations.mjs';
|
|
12
14
|
import { shared } from './shared.mjs';
|
|
15
|
+
import { registerHistoryInstallation, preparationDatabase } from './agent-history-database.mjs';
|
|
13
16
|
import { verificationRpc } from './agent-rpc.mjs';
|
|
17
|
+
import { hostedOrigins } from './hosted-hosts.mjs';
|
|
18
|
+
import { FixtureDatabaseError } from '../modules/agent-door/conformance/fixtures.mjs';
|
|
14
19
|
|
|
15
20
|
// Serve the production build the way rer deploy hosts it (assets behind the
|
|
16
21
|
// Worker, deploy identity bound, a public https origin) on loopback, with no
|
|
@@ -43,9 +48,42 @@ async function miniflareFor(root) {
|
|
|
43
48
|
const standIn = (item, field) => typeof item.verifyStandIn === 'string' && item.verifyStandIn ? item.verifyStandIn : field?.kind === 'email' ? 'owner@example.com' : typeof field?.default === 'string' && field.default ? field.default : randomBytes(24).toString('hex');
|
|
44
49
|
const links = html => [...html.matchAll(/(?:src|href)=["'](\/[^"'#?\s]+)/g)].map(match => match[1].slice(1)).filter(path => !path.startsWith('/') && /\.[A-Za-z0-9]+$/.test(path));
|
|
45
50
|
|
|
51
|
+
// Verification-only runtime bindings. Never write defaults back to the source
|
|
52
|
+
// declaration or build: deploy and packaging must still require real inputs.
|
|
53
|
+
async function hostedPublicVariables(root, config, declaration, manifest) {
|
|
54
|
+
const present = value => typeof value === 'string' ? Boolean(value.trim()) : value !== undefined && value !== null;
|
|
55
|
+
const vars = { ...declaration.vars }, names = new Set([...Object.keys(config.vars ?? {}), ...Object.keys(vars)]);
|
|
56
|
+
const defaults = source => { for (const [name, value] of Object.entries(source ?? {})) if (!present(vars[name])) vars[name] = value; };
|
|
57
|
+
defaults(config.vars);
|
|
58
|
+
for (const [candidate, select] of deliveryDeclarationSources) {
|
|
59
|
+
const path = await sourcePath(root, candidate, { optional: true });
|
|
60
|
+
if (!path) continue;
|
|
61
|
+
const spec = select(JSON.parse(await readFile(path, 'utf8')));
|
|
62
|
+
if (!spec) continue;
|
|
63
|
+
for (const name of spec.publicVariableNames ?? Object.keys(spec.publicVariables ?? {})) names.add(name);
|
|
64
|
+
defaults(spec.publicVariables);
|
|
65
|
+
}
|
|
66
|
+
const { setupConditionMatches } = await shared('schema/setup-validation.mjs');
|
|
67
|
+
const fields = (manifest.fields ?? []).filter(field => field.kind !== 'secret' && names.has(field.name));
|
|
68
|
+
// A required field may depend on another public input declared after it.
|
|
69
|
+
let changed = true;
|
|
70
|
+
while (changed) {
|
|
71
|
+
changed = false;
|
|
72
|
+
for (const field of fields) {
|
|
73
|
+
if (present(vars[field.name]) || field.when && !setupConditionMatches(field.when, vars)) continue;
|
|
74
|
+
const presentation = field.presentations?.find(item => setupConditionMatches(item.when, vars));
|
|
75
|
+
if (field.presentations && !presentation || !(presentation?.required ?? field.required)) continue;
|
|
76
|
+
vars[field.name] = field.default?.trim() ? field.default : field.kind === 'url' ? `${HOSTED_ORIGIN}/`
|
|
77
|
+
: field.kind === 'select' ? field.options?.find(option => option.value)?.value || standIn({}, field) : standIn({}, field);
|
|
78
|
+
changed = true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return vars;
|
|
82
|
+
}
|
|
83
|
+
|
|
46
84
|
/** The build served as rer deploy hosts it, with declared vars, origin and
|
|
47
|
-
*
|
|
48
|
-
export async function serveHosted(root, { declaration: declarationPath = 'deploy.json', bindings = {}, outboundService, rpc, workerEntrypoints = [] } = {}) {
|
|
85
|
+
* required public inputs and deploy/Managed mail secret stand-ins. `fetch(path, init)` uses the deployed origin. */
|
|
86
|
+
export async function serveHosted(root, { declaration: declarationPath = 'deploy.json', bindings = {}, outboundService, rpc, workerEntrypoints = [], publicHosts, fixturePreparation = false } = {}) {
|
|
49
87
|
const { loopbackFetch } = await shared('loopback-fetch.mjs');
|
|
50
88
|
const build = join(root, 'dist/server'), client = join(root, 'dist/client');
|
|
51
89
|
let config;
|
|
@@ -54,6 +92,7 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
54
92
|
let declaration;
|
|
55
93
|
try { declaration = await read(join(root, declarationPath)); }
|
|
56
94
|
catch { throw new RerError('hosted_output_missing', `No readable deploy declaration at ${declarationPath}. A seed whose deploy reads another file names it with the commandAdapter option deployDeclaration.`, 1); }
|
|
95
|
+
const origins = hostedOrigins(declaration, publicHosts, HOSTED_ORIGIN);
|
|
57
96
|
const manifest = await optional(join(root, 'rer-setup.json'));
|
|
58
97
|
const { Miniflare, convertV4MiniflareOptions = options => options } = await miniflareFor(root);
|
|
59
98
|
const entry = join(build, config.main);
|
|
@@ -70,9 +109,10 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
70
109
|
for (const binding of await hostedMailCredentials(root)) {
|
|
71
110
|
if (!Object.hasOwn(secrets, binding)) secrets[binding] = standIn({}, manifest.fields?.find(field => field.name === binding));
|
|
72
111
|
}
|
|
73
|
-
const
|
|
112
|
+
const publicVariables = await hostedPublicVariables(root, config, declaration, manifest);
|
|
113
|
+
const options = { host: '127.0.0.1', port: 0, cf: false, workers: [{ name: 'ingress', modules: true, script: ingress, serviceBindings: { APP: 'app', ...nativeBindings } }, ...rpcServices.workers, {
|
|
74
114
|
name: 'app', ...(outboundService ? { outboundService } : {}), modules, modulesRoot: build, compatibilityDate: config.compatibility_date, compatibilityFlags: config.compatibility_flags,
|
|
75
|
-
bindings: { ...
|
|
115
|
+
bindings: { ...publicVariables, ...secrets, ...(declaration.originVar ? { [declaration.originVar]: HOSTED_ORIGIN } : {}),
|
|
76
116
|
RER_DEPLOY_SOURCE: identity.sourceSha, RER_DEPLOY_FINGERPRINT: identity.fingerprint, RER_VERSION: { id: identity.versionId, tag: '', timestamp: new Date().toISOString() }, ...bindings },
|
|
77
117
|
d1Databases: Object.fromEntries((config.d1_databases ?? []).map(item => [item.binding, item.database_id ?? item.database_name])),
|
|
78
118
|
r2Buckets: Object.fromEntries((config.r2_buckets ?? []).map(item => [item.binding, item.bucket_name])),
|
|
@@ -85,7 +125,24 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
85
125
|
serviceBindings: rpcServices.serviceBindings,
|
|
86
126
|
durableObjects: Object.fromEntries((config.durable_objects?.bindings ?? []).filter(item => !item.script_name).map(item => [item.name, { className: item.class_name, useSQLite: true }])),
|
|
87
127
|
...(config.assets ? { assets: { directory: client, binding: config.assets.binding ?? 'ASSETS', routerConfig: { has_user_worker: true, invoke_user_worker_ahead_of_assets: RUN_WORKER_FIRST } } } : {}),
|
|
88
|
-
}] }
|
|
128
|
+
}] };
|
|
129
|
+
let preparation, held = false;
|
|
130
|
+
const waiting = new Set();
|
|
131
|
+
const release = () => { held = false; for (const resume of waiting) resume(); waiting.clear(); };
|
|
132
|
+
// Install the gate before owner hooks retain any D1 handles; replacing
|
|
133
|
+
// Miniflare options later would disconnect those existing handles.
|
|
134
|
+
if (fixturePreparation) {
|
|
135
|
+
const app = options.workers.find(worker => worker.name === 'app');
|
|
136
|
+
const wrapper = join(build, '__rer_preparation.mjs');
|
|
137
|
+
const native = './' + config.main;
|
|
138
|
+
app.modules = [{ type: 'ESModule', path: wrapper, contents:
|
|
139
|
+
`import app from ${JSON.stringify(native)};export * from ${JSON.stringify(native)};export default {...app,async queue(batch,env,ctx){await env.RER_PREPARATION_QUEUE_GATE.fetch('https://queue.invalid');return app.queue(batch,env,ctx)}}` }, ...modules];
|
|
140
|
+
app.serviceBindings = { ...app.serviceBindings, RER_PREPARATION_QUEUE_GATE: async () => {
|
|
141
|
+
if (held) await new Promise(resume => waiting.add(resume));
|
|
142
|
+
return new Response(null, { status: 204 });
|
|
143
|
+
} };
|
|
144
|
+
}
|
|
145
|
+
const mf = new Miniflare(convertV4MiniflareOptions(options));
|
|
89
146
|
let loopback;
|
|
90
147
|
try {
|
|
91
148
|
loopback = await mf.ready;
|
|
@@ -99,11 +156,32 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
99
156
|
await migrate(await mf.getD1Database(item.binding, 'app'), withRewrites(plan, await readMigrationRewrites(root), { partial: true }));
|
|
100
157
|
}
|
|
101
158
|
} catch (error) { await mf.dispose(); throw error; }
|
|
102
|
-
|
|
159
|
+
const preparedDatabase = async binding => {
|
|
160
|
+
if (!(config.d1_databases ?? []).some(item => item.binding === binding)) throw new RerError('hosted_database_invalid', new FixtureDatabaseError().message, 1);
|
|
161
|
+
return preparationDatabase(await mf.getD1Database(binding, 'app'));
|
|
162
|
+
};
|
|
163
|
+
const hosted = { client, identity, origin: HOSTED_ORIGIN, origins,
|
|
103
164
|
worker: async entrypoint => {
|
|
104
165
|
if (!Object.hasOwn(nativeWorkers, entrypoint)) throw new RerError('agent_invoke_invalid', 'Verification can invoke only its declared native Worker entrypoint.', 1);
|
|
105
166
|
return (await mf.getBindings('ingress'))[nativeWorkers[entrypoint]];
|
|
106
167
|
},
|
|
168
|
+
// Only the conformance call sites request these powers. Ordinary hosted
|
|
169
|
+
// checks and owner initialization keep their SELECT-only reader below.
|
|
170
|
+
preparation: async () => preparation ??= (async () => {
|
|
171
|
+
if (!fixturePreparation) throw new RerError('hosted_database_invalid', 'Native preparation requires a disposable conformance host.', 1);
|
|
172
|
+
return Object.freeze({
|
|
173
|
+
database: async binding => (await preparedDatabase(binding)).database,
|
|
174
|
+
scheduled: async ({ scheduledTime = Date.now() } = {}) => {
|
|
175
|
+
if (!Number.isFinite(scheduledTime)) throw new Error('Native scheduled preparation requires a finite Unix millisecond time.');
|
|
176
|
+
try {
|
|
177
|
+
const result = await (await mf.getWorker('app')).scheduled({ scheduledTime });
|
|
178
|
+
if (result.outcome !== 'ok') throw new Error();
|
|
179
|
+
}
|
|
180
|
+
catch { throw new Error('Native scheduled preparation failed; fixture values are not logged.'); }
|
|
181
|
+
},
|
|
182
|
+
queues: Object.freeze({ hold: () => { held = true; }, release }),
|
|
183
|
+
});
|
|
184
|
+
})(),
|
|
107
185
|
database: async binding => {
|
|
108
186
|
if (!(config.d1_databases ?? []).some(item => item.binding === binding)) throw new RerError('hosted_database_invalid', 'Verification can read only a declared disposable database binding.', 1);
|
|
109
187
|
const db = await mf.getD1Database(binding, 'app');
|
|
@@ -111,8 +189,13 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
111
189
|
if (typeof sql !== 'string' || !/^\s*SELECT\b/i.test(sql) || sql.includes(';')) throw new RerError('hosted_database_invalid', 'Verification state access is read-only SELECT.', 1);
|
|
112
190
|
return db.prepare(sql);
|
|
113
191
|
} };
|
|
114
|
-
}, close: () => mf.dispose(),
|
|
115
|
-
fetch: (path, { headers, ...init } = {}) =>
|
|
192
|
+
}, close: () => { release(); return mf.dispose(); },
|
|
193
|
+
fetch: (path, { headers, origin = HOSTED_ORIGIN, ...init } = {}) => {
|
|
194
|
+
if (!origins.includes(origin)) throw new RerError('agent_door_origin', 'Agent conformance request must use a declared hosted installation origin.', 1);
|
|
195
|
+
return loopbackFetch(loopback, { ...init, redirect: 'manual', headers: { ...headers, 'x-rer-original-url': `${origin}/${path}` } });
|
|
196
|
+
} };
|
|
197
|
+
if (fixturePreparation) registerHistoryInstallation(hosted, preparedDatabase);
|
|
198
|
+
return hosted;
|
|
116
199
|
}
|
|
117
200
|
|
|
118
201
|
export async function checkHostedOutput(root, options) {
|
package/cli/ledger-refusal.mjs
CHANGED
|
@@ -14,5 +14,5 @@ const fixes = {
|
|
|
14
14
|
export function ledgerRefusal(receipt) {
|
|
15
15
|
const [, kind, ...name] = receipt.reason.split(':');
|
|
16
16
|
const named = receipt.unapplied ?? (name.length ? name.join(':') : undefined);
|
|
17
|
-
return `Migration history refused (${receipt.reason}): ${named ?
|
|
17
|
+
return `Migration history refused (${receipt.reason}): ${named ? `${kind === 'multiple' ? 'ledger' : 'migration'} ${named}: ` : ''}${fixes[kind] ?? 'its history does not match this checkout.'}`;
|
|
18
18
|
}
|
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}).
|
|
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/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
|
-
|
|
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
|
+
}
|
|
@@ -16,13 +16,21 @@ export function platformMigrationDeclaration(input={}) {
|
|
|
16
16
|
return {prefix,dataOnly};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
// Native archive/migration checks and deploy packaging use the same seed-owned
|
|
20
|
+
// directory declaration. Only packaging supplies its historical fallback.
|
|
21
|
+
export async function migrationDirectory(root,binding,{fallback}={}) {
|
|
22
|
+
let config;
|
|
23
|
+
try { config=parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8')); }
|
|
24
|
+
catch(error) { if(error.code!=='ENOENT' || fallback===undefined)throw error; return join(root,fallback); }
|
|
25
|
+
const bindings=config.d1_databases?.filter(entry=>entry.binding===binding) ?? [];
|
|
26
|
+
insist(bindings.length===1 || bindings.length===0 && fallback!==undefined,'migration_ledger:binding',409);
|
|
27
|
+
return join(root,bindings[0]?.migrations_dir ?? fallback ?? 'migrations');
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
// Reproduce the runner's stored SQL text, then recover SQLite's statement
|
|
20
31
|
// boundaries (migration-sql.mjs).
|
|
21
32
|
export async function migrationInventory(root,binding,{prefix},split) {
|
|
22
|
-
const
|
|
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');
|
|
33
|
+
const directory=await migrationDirectory(root,binding);
|
|
26
34
|
return Promise.all((await readdir(directory)).filter(name=>name.endsWith('.sql')).sort().map(async name=>{
|
|
27
35
|
const bytes=await readFile(join(directory,name));
|
|
28
36
|
return {name,...(split?{statements:migrationStatements(bytes.toString('utf8'),split)}:{}),sha256:createHash('sha256').update(bytes).digest('hex'),...(prefix===undefined?{}:{aliases:[prefix+name]})};
|
|
@@ -37,11 +45,15 @@ export async function appliedMigrations(root,db,binding,declaration,split) {
|
|
|
37
45
|
|
|
38
46
|
// Ledger contents can contain private values. Report constant categories,
|
|
39
47
|
// retaining the transfer's canonical compatibility and hash-matching policy.
|
|
40
|
-
function constantRefusal(error) {
|
|
48
|
+
function constantRefusal(error,files) {
|
|
41
49
|
if(!(error instanceof PortabilityError))return error;
|
|
42
50
|
const code=error.code.startsWith('migration_ledger:')?error.code.split(':').slice(0,2).join(':')
|
|
43
51
|
:error.code==='schema_mismatch:uncertain_migrations'?'migration_ledger:uncertain':'migration_ledger:incompatible';
|
|
44
|
-
|
|
52
|
+
// Only checkout-owned names may accompany the constant category. A ledger
|
|
53
|
+
// entry with no matching file or declared alias can contain private values.
|
|
54
|
+
const name=error.code.split(':').slice(2).join(':');
|
|
55
|
+
const identity=['migration_ledger:unknown','migration_ledger:ambiguous','migration_ledger:conflict'].includes(code) && name && files.some(file=>file.name===name || file.aliases?.includes(name))?name:undefined;
|
|
56
|
+
return Object.assign(new PortabilityError(code,409),error.migration===undefined?{}:{migration:error.migration},identity?{identity}:{});
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
// For adapters with their own archive format: the entries of a migration ledger
|
|
@@ -59,7 +71,7 @@ export async function mapMigrationLedger({root,binding='DB',platformMigrations,e
|
|
|
59
71
|
const mapped=entries.map(({name,sha256})=>({name:matchMigrationLedger([{name,sha256}],files,declaration)[0]??name,sha256}));
|
|
60
72
|
insist(new Set(mapped.map(entry=>entry.name)).size===mapped.length,'migration_ledger:ambiguous',409);
|
|
61
73
|
return mapped;
|
|
62
|
-
} catch(error) { throw constantRefusal(error); }
|
|
74
|
+
} catch(error) { throw constantRefusal(error,files); }
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
// Older runners leave Wrangler's bookkeeping beside their authoritative hashed
|
|
@@ -89,7 +101,7 @@ export async function nativeMigrationPreflight({root,db,binding='DB',platformMig
|
|
|
89
101
|
const declaration=platformMigrationDeclaration(platformMigrations);
|
|
90
102
|
const tables=await all(db,`SELECT type,name FROM sqlite_master WHERE lower(name) IN (${ledgerNames.map(name=>`'${name}'`).join(',')})`);
|
|
91
103
|
const legacyPair=tables.length===2 && ['_rer_deploy_migrations','d1_migrations'].every(name=>tables.some(row=>row.name===name));
|
|
92
|
-
|
|
104
|
+
if(tables.length>1 && !legacyPair)throw Object.assign(new PortabilityError('migration_ledger:multiple',409),tables.some(row=>row.name==='rer_migrations')?{identity:'rer_migrations'}:{});
|
|
93
105
|
insist(tables.every(row=>row.type==='table' && ledgerNames.includes(row.name)),'migration_ledger:incompatible',409);
|
|
94
106
|
const table=legacyPair?'_rer_deploy_migrations':tables[0]?.name;
|
|
95
107
|
if(!table)return {nativeApply:true};
|
|
@@ -105,6 +117,6 @@ export async function nativeMigrationPreflight({root,db,binding='DB',platformMig
|
|
|
105
117
|
nativeApply=table==='d1_migrations' && files.some(file=>!applied.includes(file.name));
|
|
106
118
|
// The checkout file is named: it is the seed's own, never a ledger value.
|
|
107
119
|
if(table!=='d1_migrations')for(const file of files)if(!applied.includes(file.name))throw Object.assign(new PortabilityError('migration_ledger:unapplied',409),{migration:file.name});
|
|
108
|
-
} catch(error) { throw constantRefusal(error); }
|
|
120
|
+
} catch(error) { throw constantRefusal(error,files); }
|
|
109
121
|
return {nativeApply};
|
|
110
122
|
}
|
|
@@ -79,8 +79,10 @@ catch (error) {
|
|
|
79
79
|
// Erasure-fence refusals name the fence: an older archive is refused, not a failed transfer.
|
|
80
80
|
const refusal = error instanceof PortabilityError && (error.status === 409 || ['wrong_product', 'schema_mismatch', 'migration_ledger', 'table_inventory', 'configuration_inventory', 'erased_row', 'erasure_epoch', 'later_journal', 'invalid_journal', 'staged_validation'].includes(error.code.split(':')[0]));
|
|
81
81
|
const exitCode = error.message === 'archive_json' ? 2 : refusal || error.code === 'EEXIST' ? 4 : 1;
|
|
82
|
-
// Report
|
|
83
|
-
|
|
82
|
+
// Report symbolic codes and identities verified against the checkout or the
|
|
83
|
+
// known Platform ledger name; native database errors can contain private data.
|
|
84
|
+
const ledgerIdentity = error instanceof PortabilityError && error.code.startsWith('migration_ledger:') ? error.migration ?? error.identity : undefined;
|
|
85
|
+
const reason = error instanceof PortabilityError ? (error.code.startsWith('migration_ledger:') ? error.code + (ledgerIdentity ? ':' + ledgerIdentity : '') : error.code.split(':')[0]) : /^[A-Z_]+$/.test(error.code ?? '') ? error.code : error.message === 'archive_json' ? 'archive_json' : 'native_error';
|
|
84
86
|
// A later migration that changes restorable data is named: it is a file of this installation.
|
|
85
87
|
const migration = error instanceof PortabilityError && error.code.startsWith('schema_mismatch:migration_data:') ? error.code.slice('schema_mismatch:migration_data:'.length) : undefined;
|
|
86
88
|
// An unapplied migration is a file of this checkout: it is named so it can be applied.
|
|
@@ -22,9 +22,16 @@ function* nodes(root) {
|
|
|
22
22
|
|
|
23
23
|
const parsed = new Map();
|
|
24
24
|
|
|
25
|
+
// Authoring follows module imports. History isolation also follows URL resources
|
|
26
|
+
// that bundlers can publish without importing them into the authoring tool.
|
|
27
|
+
export function dependencies(source, filename = 'module.mjs', {resources = false} = {}) {
|
|
28
|
+
const result = edges(source, filename);
|
|
29
|
+
return resources ? result : result.filter(request => request.kind !== 'resource');
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
// Parsing, including erased TS edges and calls nested in templates/JSX, precedes
|
|
26
33
|
// resolution. Every resulting edge uses the same virtual Node resolver.
|
|
27
|
-
|
|
34
|
+
function edges(source, filename) {
|
|
28
35
|
let versions = parsed.get(filename);
|
|
29
36
|
if (versions?.has(source)) return versions.get(source);
|
|
30
37
|
const plugins = ['decorators-legacy', 'deprecatedImportAssert'];
|
|
@@ -82,6 +89,12 @@ export function dependencies(source, filename = 'module.mjs') {
|
|
|
82
89
|
else if (node.type === 'ImportExpression') add(node.source, 'import');
|
|
83
90
|
else if (call(node)) {
|
|
84
91
|
let callee = unwrap(node.callee), argument = node.arguments[0];
|
|
92
|
+
// Vite emits literal import.meta.url resources, including the URL nested
|
|
93
|
+
// in Worker/SharedWorker constructors, without an import declaration.
|
|
94
|
+
const base = unwrap(node.arguments[1]), meta = unwrap(base?.object);
|
|
95
|
+
if (node.type === 'NewExpression' && callee?.type === 'Identifier' && callee.name === 'URL'
|
|
96
|
+
&& member(base) && property(base) === 'url' && meta?.type === 'MetaProperty'
|
|
97
|
+
&& meta.meta.name === 'import' && meta.property.name === 'meta') add(argument, 'resource');
|
|
85
98
|
if (member(callee) && property(callee) === 'bind' && requireFunction(callee.object)) { add(node.arguments[1], 'require'); continue; }
|
|
86
99
|
if (member(callee) && callee.object?.type === 'Identifier' && callee.object.name === 'Reflect' && ['apply', 'construct'].includes(property(callee)) && requireFunction(node.arguments[0])) {
|
|
87
100
|
add(node.arguments[property(callee) === 'apply' ? 2 : 1]?.elements?.[0], 'require'); continue;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { createHash } from 'node:crypto'
|
|
3
|
+
import { execFileSync } from 'node:child_process'
|
|
4
|
+
|
|
5
|
+
export const anchorPath = 'docs/contract/ANCHOR.json'
|
|
6
|
+
const sourceFiles = ['CONTRACT.md', 'ACCEPTANCE.md', 'docs/CLAIMS.md', 'CLAIMS.md']
|
|
7
|
+
const sha = /^[0-9a-f]{40}$/
|
|
8
|
+
const fail = message => { throw Error(`${anchorPath}: contract_anchor_invalid: ${message}`) }
|
|
9
|
+
const objectId = (type, bytes) => createHash('sha1').update(`${type} ${bytes.length}\0`).update(bytes).digest('hex')
|
|
10
|
+
|
|
11
|
+
// The commit authenticates the root tree, which authenticates each source blob
|
|
12
|
+
// (and absence of optional sources). No hash supplied only by the record is trusted.
|
|
13
|
+
function resolveSnapshot(commit, load) {
|
|
14
|
+
if (!sha.test(commit)) fail('expected a full anchor commit SHA.')
|
|
15
|
+
const object = (type, id) => {
|
|
16
|
+
const bytes = load(type, id)
|
|
17
|
+
if (objectId(type, bytes) !== id) fail(`${type} ${id} has mismatching bytes.`)
|
|
18
|
+
return bytes
|
|
19
|
+
}
|
|
20
|
+
const treeId = /^tree ([0-9a-f]{40})\n/.exec(object('commit', commit).toString('utf8'))?.[1]
|
|
21
|
+
if (!treeId) fail('anchor commit has no root tree.')
|
|
22
|
+
const trees = new Map()
|
|
23
|
+
function tree(id) {
|
|
24
|
+
if (trees.has(id)) return trees.get(id)
|
|
25
|
+
const bytes = object('tree', id), entries = new Map()
|
|
26
|
+
let offset = 0
|
|
27
|
+
while (offset < bytes.length) {
|
|
28
|
+
const end = bytes.indexOf(0, offset)
|
|
29
|
+
if (end < 0 || end + 21 > bytes.length) fail(`malformed tree ${id}.`)
|
|
30
|
+
const entry = /^(40000|100644|100755|120000|160000) ([^/\0]+)$/.exec(bytes.subarray(offset, end).toString('utf8'))
|
|
31
|
+
if (!entry || entries.has(entry[2])) fail(`malformed or duplicate entry in tree ${id}.`)
|
|
32
|
+
entries.set(entry[2], { mode: entry[1], id: bytes.subarray(end + 1, end + 21).toString('hex') })
|
|
33
|
+
offset = end + 21
|
|
34
|
+
}
|
|
35
|
+
trees.set(id, entries)
|
|
36
|
+
return entries
|
|
37
|
+
}
|
|
38
|
+
const files = {}
|
|
39
|
+
for (const name of sourceFiles) {
|
|
40
|
+
const parts = name.split('/')
|
|
41
|
+
let directory = treeId, entry
|
|
42
|
+
for (const [index, part] of parts.entries()) {
|
|
43
|
+
entry = tree(directory).get(part)
|
|
44
|
+
if (!entry) break
|
|
45
|
+
if (index < parts.length - 1) {
|
|
46
|
+
if (entry.mode !== '40000') { entry = undefined; break }
|
|
47
|
+
directory = entry.id
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (!entry) continue
|
|
51
|
+
if (!['100644', '100755', '120000'].includes(entry.mode)) fail(`${name} is not a blob.`)
|
|
52
|
+
files[name] = object('blob', entry.id).toString('utf8')
|
|
53
|
+
}
|
|
54
|
+
if (!files['CONTRACT.md']) fail('anchor has no CONTRACT.md.')
|
|
55
|
+
return { commit, files }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function readAnchor(commit) {
|
|
59
|
+
if (!fs.existsSync(anchorPath)) return undefined
|
|
60
|
+
let record
|
|
61
|
+
try { record = JSON.parse(fs.readFileSync(anchorPath, 'utf8')) }
|
|
62
|
+
catch { fail('record must be valid JSON.') }
|
|
63
|
+
if (record?.schema !== 1 || record.commit !== commit || !record.objects || typeof record.objects !== 'object' || Array.isArray(record.objects))
|
|
64
|
+
fail(`expected schema 1 and anchor commit ${commit}.`)
|
|
65
|
+
const used = new Set()
|
|
66
|
+
const snapshot = resolveSnapshot(commit, (type, id) => {
|
|
67
|
+
used.add(id)
|
|
68
|
+
const entry = Object.hasOwn(record.objects, id) ? record.objects[id] : undefined
|
|
69
|
+
if (entry?.type !== type || typeof entry.data !== 'string') fail(`missing ${type} ${id}.`)
|
|
70
|
+
const bytes = Buffer.from(entry.data, 'base64')
|
|
71
|
+
if (bytes.toString('base64') !== entry.data) fail(`${type} ${id} is not canonical base64.`)
|
|
72
|
+
return bytes
|
|
73
|
+
})
|
|
74
|
+
if (Object.keys(record.objects).some(id => !used.has(id))) fail('record contains objects outside the anchor proof.')
|
|
75
|
+
return snapshot
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Run once while the anchor is available, then commit this portable proof.
|
|
79
|
+
export function createAnchor(commit) {
|
|
80
|
+
const objects = {}
|
|
81
|
+
resolveSnapshot(commit, (type, id) => {
|
|
82
|
+
const bytes = execFileSync('git', ['cat-file', type, id], { stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 64 * 1024 * 1024 })
|
|
83
|
+
objects[id] = { type, data: bytes.toString('base64') }
|
|
84
|
+
return bytes
|
|
85
|
+
})
|
|
86
|
+
return { schema: 1, commit, objects }
|
|
87
|
+
}
|