@runeditrun/rer 0.2.94 → 0.2.96
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/README.md +16 -2
- package/cli/README.md +5 -1
- package/cli/agent-door.mjs +4 -0
- package/cli/agent-journey-fixtures.mjs +1 -1
- package/cli/authoring.mjs +1 -1
- package/cli/business/carry-files.mjs +142 -0
- package/cli/business/carry.mjs +37 -83
- package/cli/business/choices.mjs +17 -8
- package/cli/business/connect.mjs +6 -5
- package/cli/business/invocation.mjs +3 -2
- package/cli/business/local-commands.mjs +6 -6
- package/cli/business/questions.mjs +4 -1
- package/cli/business/state.mjs +4 -1
- package/cli/checkout-folders.mjs +14 -0
- package/cli/checkout-preparation.mjs +83 -0
- package/cli/contract.mjs +1 -1
- package/cli/door-clauses.mjs +29 -2
- package/cli/hosted-output.mjs +14 -3
- package/cli/hosted-queue-producers.mjs +43 -0
- package/cli/jsonc.mjs +1 -115
- package/cli/parse.mjs +1 -1
- package/cli/portability-migrations.mjs +2 -12
- package/edition-authoring/archive-source.mjs +86 -1
- package/edition-authoring/base-copies.mjs +15 -45
- package/edition-authoring/cli.mjs +1 -1
- package/edition-authoring/edition-migrations.mjs +2 -2
- package/edition-authoring/module-closure.mjs +119 -50
- package/edition-authoring/module-packages.mjs +12 -5
- package/edition-authoring/module-syntax.mjs +13 -50
- package/edition-authoring/runtime-dependencies.mjs +4 -4
- package/foundation/runtime/contract-docs.mjs +1 -1
- package/global/CHANGELOG.md +70 -0
- package/global/README.md +16 -2
- package/global/cli/layout.mjs +3 -0
- package/global/launch/facts.mjs +1 -29
- package/global/launch/state.mjs +3 -6
- package/global/list.mjs +9 -15
- package/modules/agent-door/CLAUSES.md +12 -0
- package/modules/agent-door/conformance/audit.mjs +22 -4
- package/modules/agent-door/conformance/discovery.mjs +32 -1
- package/modules/agent-door/conformance/erasure.mjs +103 -0
- package/modules/agent-door/conformance/index.mjs +13 -4
- package/modules/agent-door/conformance/journey.mjs +19 -1
- package/modules/agent-door/conformance/policy-phases.mjs +34 -1
- package/modules/agent-door/conformance/protocol.mjs +13 -3
- package/modules/agent-door/conformance/relay.mjs +63 -0
- package/modules/agent-door/conformance/unpair.mjs +47 -0
- package/modules/deploy-kit/jsonc.mjs +115 -0
- package/modules/deploy-kit/migration-directory.mjs +16 -0
- package/modules/deploy-kit/prepare.mjs +1 -1
- package/modules/deploy-kit/templates.mjs +9 -4
- package/modules/erasure/cascade.mjs +4 -4
- package/modules/erasure/index.d.mts +1 -1
- package/modules/erasure/index.mjs +1 -1
- package/modules/import-kit/src/d1/README.md +16 -1
- package/modules/import-kit/src/d1/preserved-reverse.ts +5 -2
- package/modules/import-kit/src/d1/read-fence.ts +14 -2
- package/modules/import-kit/src/d1/reverse.ts +4 -1
- package/modules/import-kit/src/d1/sets.ts +8 -0
- package/modules/import-kit/src/d1/snapshot-text.ts +68 -0
- package/modules/import-kit/src/d1/undo-report.ts +4 -1
- package/modules/import-kit/src/door/ADOPTING.md +9 -4
- package/modules/portability/README.md +28 -2
- package/modules/portability/bookkeeping.mjs +8 -4
- package/modules/portability/checkout-history.mjs +12 -6
- package/modules/portability/import.mjs +1 -1
- package/modules/portability/storage.mjs +7 -1
- package/modules/portability/worker-history.d.mts +1 -1
- package/modules/portability/worker-history.mjs +1 -1
- package/onboarding/operator-migrations.mjs +5 -1
- package/package.json +1 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { randomUUID, webcrypto } from 'node:crypto';
|
|
2
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
+
import { seconds, signJws } from './crypto.mjs';
|
|
4
|
+
import { auditRows } from './audit-rows.mjs';
|
|
5
|
+
|
|
6
|
+
const liveKeys = ['id','seq','at','tool','disposition','outcome','code','revision','classes','agent','consent','execution_of','replay_of','approval','review','resources','resource_count'].sort();
|
|
7
|
+
const erasedKeys = ['id','seq','at','erased','tool','classes','disposition','agent'].sort();
|
|
8
|
+
const keys = (value, expected) => value && typeof value === 'object' && !Array.isArray(value) && isDeepStrictEqual(Object.keys(value).sort(), expected);
|
|
9
|
+
|
|
10
|
+
/** Exercise the signed pull against the same hosted door and its native audit. */
|
|
11
|
+
export async function relayPhase(c) {
|
|
12
|
+
const path = '/api/rer/agent-decisions', iat = seconds(c.now);
|
|
13
|
+
const payload = { v:1, iss:c.signer.issuer, aud:c.origin, sub:c.signer.installationId, business:c.business, after:0, limit:200, iat, exp:iat+60, jti:randomUUID() };
|
|
14
|
+
const sign = value => signJws(c.signer.privateKey, c.signer.kid, 'rer-agent-decisions+jwt', value);
|
|
15
|
+
const request = pull => c.request(path, { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({pull}) });
|
|
16
|
+
const pull = await sign(payload), first = await request(pull), raw = await first.text();
|
|
17
|
+
c.add('BASE-AGENT-009.relay.route', first.status === 200, `Signed decision pull must return 200, got ${first.status}.`);
|
|
18
|
+
if (first.status !== 200) return;
|
|
19
|
+
let page = JSON.parse(raw), after = 0;
|
|
20
|
+
const projections = [];
|
|
21
|
+
for (let count = 0; ; count++) {
|
|
22
|
+
const shaped = keys(page, ['cursor','decisions','head','more']) && Array.isArray(page.decisions) && page.decisions.length <= 200 &&
|
|
23
|
+
Number.isSafeInteger(page.head) && page.head >= 0 && Number.isSafeInteger(page.cursor) && page.cursor >= after &&
|
|
24
|
+
page.cursor === (page.decisions.at(-1)?.seq ?? after) && page.more === (page.cursor < page.head) &&
|
|
25
|
+
Buffer.byteLength(JSON.stringify(page)) <= 65536 && page.decisions.every((row, index, rows) =>
|
|
26
|
+
keys(row, row.erased === true ? erasedKeys : liveKeys) && Number.isSafeInteger(row.seq) && row.seq > (rows[index-1]?.seq ?? after));
|
|
27
|
+
c.add(`BASE-AGENT-009.relay.page.${count}`, shaped, 'Decision page must be closed, ordered, bounded and carry its exact cursor and head.');
|
|
28
|
+
if (!shaped) return;
|
|
29
|
+
projections.push(...page.decisions);
|
|
30
|
+
if (!page.more) break;
|
|
31
|
+
if (count >= 99 || page.cursor <= after) throw new Error('Decision relay did not advance within 100 pages');
|
|
32
|
+
after = page.cursor;
|
|
33
|
+
const response = await request(await sign({...payload, after}));
|
|
34
|
+
if (response.status !== 200) throw new Error(`Decision page HTTP ${response.status}`);
|
|
35
|
+
page = await response.json();
|
|
36
|
+
}
|
|
37
|
+
const audit = (await auditRows(c, {agents:[null], includeFixtures:true})).rows;
|
|
38
|
+
const expected = audit.filter(row => !c.unattributedAuditIds?.has(row.id) && (row.door === 'mcp' && row.consent != null || row.tombstone && projections.some(item => item.id === row.id)));
|
|
39
|
+
c.add('BASE-AGENT-009.relay.unattributed', projections.every(row => !c.unattributedAuditIds?.has(row.id)), 'Calls before the first adopted envelope must stay out of the Business feed.');
|
|
40
|
+
const reviews = new Map((expected.some(row => row.review) ? (await c.personJson('/agents/api/reviews')).reviews : []).map(row => [row.id,row]));
|
|
41
|
+
c.add('BASE-AGENT-009.relay.count', projections.length > 0 && projections.length === expected.length && new Set(projections.map(row => row.id)).size === projections.length, 'Exactly one projection must exist per Business MCP audit, including erased decisions.');
|
|
42
|
+
for (const row of expected) {
|
|
43
|
+
const projected = projections.find(item => item.id === row.id);
|
|
44
|
+
const common = {id:row.id,seq:projected?.seq,at:row.at,tool:row.tool,classes:row.classes.map(item=>item.class),disposition:row.decision,agent:row.agent.id};
|
|
45
|
+
let value;
|
|
46
|
+
if (row.tombstone) value = {...common,erased:true};
|
|
47
|
+
else {
|
|
48
|
+
const links = row.tool === 'resources/read' && typeof row.resource === 'string' ? [row.resource] : row.resources ?? [];
|
|
49
|
+
const approval = row.approval ? (await c.personJson(`/agents/api/approvals/${encodeURIComponent(row.approval.id)}`)).approval : null;
|
|
50
|
+
value = {...common,outcome:row.outcome,code:row.code && /^[a-z][a-z0-9_]{0,63}$/.test(row.code)?row.code:null,revision:row.revision,consent:row.consent,
|
|
51
|
+
execution_of:row.decision_id,replay_of:row.replay_of,approval:approval?{state:approval.state,expires_at:approval.expires_at}:null,
|
|
52
|
+
review:row.review?{state:reviews.get(row.review.id)?.state}:null,resources:links.filter(uri=>typeof uri==='string' && uri.length<=512).slice(0,8),resource_count:row.tool==='resources/read'?links.length:row.resource_count??links.length};
|
|
53
|
+
}
|
|
54
|
+
c.add(`BASE-AGENT-009.relay.decision.${row.id}`, isDeepStrictEqual(projected,value), 'Projection must equal only the public audit fields and current approval/review state.');
|
|
55
|
+
}
|
|
56
|
+
const replay = await request(pull);
|
|
57
|
+
c.add('BASE-AGENT-009.relay.replay', replay.status === 200 && await replay.text() === raw, 'The same pull must return byte-identical content.');
|
|
58
|
+
const empty = await request(await sign({...payload,after:page.cursor}));
|
|
59
|
+
c.add('BASE-AGENT-009.relay.cursor', empty.status === 200 && isDeepStrictEqual(await empty.json(),{decisions:[],cursor:page.cursor,head:page.head,more:false}), 'Pulling after the final cursor must return an empty page.');
|
|
60
|
+
const foreign = await webcrypto.subtle.generateKey({name:'ECDSA',namedCurve:'P-256'},true,['sign','verify']);
|
|
61
|
+
const refused = await request(await signJws(foreign.privateKey,c.signer.kid,'rer-agent-decisions+jwt',payload));
|
|
62
|
+
c.add('BASE-AGENT-009.relay.foreign', refused.status === 401 && isDeepStrictEqual(await refused.json(),{code:'relay_unauthenticated'}), 'A foreign signature must return only 401 relay_unauthenticated.');
|
|
63
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
+
import { accessToken, autonomousPolicy } from './crypto.mjs';
|
|
4
|
+
|
|
5
|
+
const challenged = response => response.status === 401 && /resource_metadata="/.test(response.headers.get('www-authenticate') ?? '');
|
|
6
|
+
const version = '2026-07-28';
|
|
7
|
+
|
|
8
|
+
/** After the driver unpairs the installation, Business tokens end and native access stays.
|
|
9
|
+
* Probes go straight to the origin: policy renewal is no longer possible once unpaired. */
|
|
10
|
+
export async function unpairPhase(c, unpair, sponsors) {
|
|
11
|
+
if (typeof unpair !== 'function') throw new TypeError('Unpair conformance requires the driver to provide unpair().');
|
|
12
|
+
await c.adopt(autonomousPolicy());
|
|
13
|
+
await c.prepare('unpair');
|
|
14
|
+
const raw = (path, init = {}) => c.fetch(new URL(path, `${c.origin}/`).href, { redirect: 'manual', ...init });
|
|
15
|
+
let id = 0;
|
|
16
|
+
const rpc = async (method, token, params = {}) => {
|
|
17
|
+
const response = await raw('/mcp', { method: 'POST', headers: {
|
|
18
|
+
Accept: 'application/json, text/event-stream', 'Content-Type': 'application/json', Origin: c.origin, 'MCP-Protocol-Version': version, 'Mcp-Method': method,
|
|
19
|
+
...(method === 'tools/call' ? { 'Mcp-Name': params.name } : {}), Authorization: `Bearer ${token}`,
|
|
20
|
+
}, body: JSON.stringify({ jsonrpc: '2.0', id: ++id, method, params: { ...params, _meta: { 'io.modelcontextprotocol/protocolVersion': version, 'io.modelcontextprotocol/clientCapabilities': {} } } }) });
|
|
21
|
+
const body = await response.json().catch(() => ({}));
|
|
22
|
+
return { response, body };
|
|
23
|
+
};
|
|
24
|
+
const witness = { name: c.surface.witness.tool, arguments: c.resolve(c.surface.witness.args) };
|
|
25
|
+
const principals = [['owner', c.person], ...sponsors.slice(1).map(person => [`cross-sponsor.${person.label}`, person])];
|
|
26
|
+
const tokens = new Map();
|
|
27
|
+
for (const [label, person] of principals) tokens.set(label, await accessToken({ ...c, person, agentId: `rer-conformance-${randomUUID()}`, consent: randomUUID() }));
|
|
28
|
+
const metadata = async () => (await (await raw('/.well-known/oauth-protected-resource/mcp')).json().catch(() => ({}))).authorization_servers;
|
|
29
|
+
const clients = [...c.clients.entries()];
|
|
30
|
+
for (const [label, token] of tokens) {
|
|
31
|
+
const listed = await rpc('tools/list', token);
|
|
32
|
+
c.add(`BASE-AGENT-004.7.paired.${label}`, listed.response.status === 200 && Array.isArray(listed.body.result?.tools), 'A Business token must be admitted while the installation is paired.');
|
|
33
|
+
}
|
|
34
|
+
c.add('BASE-AGENT-004.7.paired.metadata', isDeepStrictEqual(await metadata(), [c.signer.issuer]), 'Paired metadata must name the Business issuer.');
|
|
35
|
+
await unpair();
|
|
36
|
+
for (const [label, token] of tokens) {
|
|
37
|
+
const listed = await rpc('tools/list', token), called = await rpc('tools/call', token, witness);
|
|
38
|
+
c.add(`BASE-AGENT-004.7.token.${label}`, challenged(listed.response) && challenged(called.response), 'After unpairing, a Business token from the same signer must receive 401 with a resource_metadata challenge on every method.');
|
|
39
|
+
}
|
|
40
|
+
c.add('BASE-AGENT-004.7.metadata', isDeepStrictEqual(await metadata(), []), 'After unpairing, protected resource metadata must name no authorization server.');
|
|
41
|
+
const state = await raw('/agents/api/state', { headers: { Origin: c.origin, Cookie: c.person.cookie } }), body = await state.json().catch(() => ({}));
|
|
42
|
+
c.add('BASE-AGENT-004.7.session', state.status === 200 && body.paired === false, 'After unpairing, the owner session must still read the door state, now unpaired.');
|
|
43
|
+
for (const [label, client] of clients) {
|
|
44
|
+
const listed = await rpc('tools/list', client.token);
|
|
45
|
+
c.add(`BASE-AGENT-004.7.native.${label}`, listed.response.status === 200 && Array.isArray(listed.body.result?.tools), 'After unpairing, a declared native fixture client must still be admitted.');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
function location(text, position) {
|
|
2
|
+
const lines = text.slice(0, position).split('\n');
|
|
3
|
+
return `position ${position} (line ${lines.length} column ${lines.at(-1).length + 1})`;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Validate JSONC before JSON.parse so diagnostics do not depend on the Node
|
|
7
|
+
// version's error wording. An explicit stack also handles deeply nested input.
|
|
8
|
+
// Only comments and commas accepted by the grammar are masked; offsets stay in
|
|
9
|
+
// the original source and comment removal can never join separate tokens.
|
|
10
|
+
export function parseJsonc(text, onProperty) {
|
|
11
|
+
const result = text.split(''), stack = [{ kind: 'root', state: 'value', path: [] }];
|
|
12
|
+
let index = 0;
|
|
13
|
+
const fail = (message, position = index) => {
|
|
14
|
+
throw new SyntaxError(`${position === text.length ? 'Unexpected end of JSON input' : message} at ${location(text, position)}`);
|
|
15
|
+
};
|
|
16
|
+
const unterminated = (kind, start) => {
|
|
17
|
+
throw new SyntaxError(`Unterminated ${kind} at ${location(text, start)}; close the ${kind}.`);
|
|
18
|
+
};
|
|
19
|
+
const blank = start => {
|
|
20
|
+
for (let i = start; i < index; i++) if (text[i] !== '\n' && text[i] !== '\r') result[i] = ' ';
|
|
21
|
+
};
|
|
22
|
+
const space = () => {
|
|
23
|
+
while (index < text.length) {
|
|
24
|
+
if (' \t\r\n'.includes(text[index])) { index++; continue; }
|
|
25
|
+
const start = index;
|
|
26
|
+
if (text[index] === '/' && text[index + 1] === '/') {
|
|
27
|
+
index += 2;
|
|
28
|
+
while (index < text.length && text[index] !== '\n' && text[index] !== '\r') index++;
|
|
29
|
+
} else if (text[index] === '/' && text[index + 1] === '*') {
|
|
30
|
+
const end = text.indexOf('*/', index + 2);
|
|
31
|
+
if (end === -1) unterminated('block comment', start);
|
|
32
|
+
index = end + 2;
|
|
33
|
+
} else break;
|
|
34
|
+
blank(start);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const string = () => {
|
|
38
|
+
const start = index++;
|
|
39
|
+
let end = index;
|
|
40
|
+
while (end < text.length && text[end] !== '"') end = Math.min(text.length, end + (text[end] === '\\' ? 2 : 1));
|
|
41
|
+
if (end === text.length) unterminated('string', start);
|
|
42
|
+
while (index < end) {
|
|
43
|
+
if (text.charCodeAt(index) < 32) fail('Unescaped control character in string');
|
|
44
|
+
if (text[index++] !== '\\') continue;
|
|
45
|
+
if (text[index] === 'u') {
|
|
46
|
+
index++;
|
|
47
|
+
for (let digit = 0; digit < 4; digit++, index++) {
|
|
48
|
+
if (index >= end || !/[0-9a-fA-F]/.test(text[index])) fail('Invalid Unicode escape');
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
if (!'"\\/bfnrt'.includes(text[index])) fail('Invalid string escape');
|
|
52
|
+
index++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
index++;
|
|
56
|
+
};
|
|
57
|
+
const digit = () => index < text.length && text[index] >= '0' && text[index] <= '9';
|
|
58
|
+
const digits = () => {
|
|
59
|
+
if (!digit()) fail('Expected a digit');
|
|
60
|
+
do { index++; } while (digit());
|
|
61
|
+
};
|
|
62
|
+
const number = () => {
|
|
63
|
+
if (text[index] === '-') index++;
|
|
64
|
+
if (text[index] === '0') index++;
|
|
65
|
+
else digits();
|
|
66
|
+
if (text[index] === '.') { index++; digits(); }
|
|
67
|
+
if (text[index] === 'e' || text[index] === 'E') {
|
|
68
|
+
index++;
|
|
69
|
+
if (text[index] === '+' || text[index] === '-') index++;
|
|
70
|
+
digits();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
while (stack.length) {
|
|
74
|
+
space();
|
|
75
|
+
const frame = stack.at(-1), char = text[index], closing = frame.kind === 'object' ? '}' : ']';
|
|
76
|
+
if (frame.state === 'end') {
|
|
77
|
+
if (index < text.length) fail('Unexpected token after JSON value');
|
|
78
|
+
stack.pop();
|
|
79
|
+
} else if ((frame.state === 'keyOrEnd' || frame.state === 'valueOrEnd' || frame.state === 'comma') && char === closing) {
|
|
80
|
+
index++; stack.pop();
|
|
81
|
+
} else if (frame.state === 'keyOrEnd') {
|
|
82
|
+
if (char !== '"') fail('Expected a property name');
|
|
83
|
+
const start = index; string();
|
|
84
|
+
frame.key = onProperty ? JSON.parse(text.slice(start, index)) : undefined;
|
|
85
|
+
onProperty?.([...frame.path, frame.key], start);
|
|
86
|
+
frame.state = 'colon';
|
|
87
|
+
} else if (frame.state === 'colon') {
|
|
88
|
+
if (char !== ':') fail("Expected ':' after property name");
|
|
89
|
+
index++; frame.state = 'value';
|
|
90
|
+
} else if (frame.state === 'comma') {
|
|
91
|
+
if (char !== ',') fail(`Expected ',' or '${closing}'`);
|
|
92
|
+
const comma = index++;
|
|
93
|
+
space();
|
|
94
|
+
if (text[index] === closing) result[comma] = ' ';
|
|
95
|
+
frame.state = frame.kind === 'object' ? 'keyOrEnd' : 'valueOrEnd';
|
|
96
|
+
} else {
|
|
97
|
+
const path = !onProperty ? undefined : frame.kind === 'root' ? [] : [...frame.path, frame.kind === 'array' ? frame.next++ : frame.key];
|
|
98
|
+
frame.state = frame.kind === 'root' ? 'end' : 'comma';
|
|
99
|
+
if (char === '{' || char === '[') {
|
|
100
|
+
index++;
|
|
101
|
+
stack.push({ kind: char === '{' ? 'object' : 'array', state: char === '{' ? 'keyOrEnd' : 'valueOrEnd', path, next: 0 });
|
|
102
|
+
} else if (char === '"') string();
|
|
103
|
+
else if (char === '-' || digit()) number();
|
|
104
|
+
else {
|
|
105
|
+
const literal = { t: 'true', f: 'false', n: 'null' }[char];
|
|
106
|
+
if (!literal) fail('Unexpected token in JSON value');
|
|
107
|
+
for (const expected of literal) {
|
|
108
|
+
if (text[index] !== expected) fail(`Expected '${literal}'`);
|
|
109
|
+
index++;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return JSON.parse(result.join(''));
|
|
115
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {join} from 'node:path';
|
|
2
|
+
import {readFile} from 'node:fs/promises';
|
|
3
|
+
import {insist} from '../portability/storage.mjs';
|
|
4
|
+
import {parseJsonc} from './jsonc.mjs';
|
|
5
|
+
|
|
6
|
+
// Native archive/migration checks and deploy packaging use the same seed-owned
|
|
7
|
+
// directory declaration. Only packaging supplies its historical fallback.
|
|
8
|
+
export async function migrationDirectory(root,binding,{fallback}={}) {
|
|
9
|
+
let config;
|
|
10
|
+
try { config=parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8')); }
|
|
11
|
+
catch(error) { if(error.code!=='ENOENT' || fallback===undefined)throw error; return join(root,fallback); }
|
|
12
|
+
const bindings=config.d1_databases?.filter(entry=>entry.binding===binding) ?? [];
|
|
13
|
+
insist(bindings.length===1 || bindings.length===0 && fallback!==undefined,'migration_ledger:binding',409);
|
|
14
|
+
return join(root,bindings[0]?.migrations_dir ?? fallback ?? 'migrations');
|
|
15
|
+
}
|
|
16
|
+
|
|
@@ -7,7 +7,7 @@ import { assetsIgnore, collectFiles, regularBytes, validateArtifact, MAX_ARTIFAC
|
|
|
7
7
|
import { collectBuildModules } from './build-modules.mjs';
|
|
8
8
|
import { refuseLocalEnvFiles, refuseLocalModels } from './local-only.mjs';
|
|
9
9
|
import { readMigrationRewrites, withRewrites } from './migrations.mjs';
|
|
10
|
-
import { migrationDirectory } from '
|
|
10
|
+
import { migrationDirectory } from './migration-directory.mjs';
|
|
11
11
|
|
|
12
12
|
export function assertCredentialFree(env) {
|
|
13
13
|
for (const [key,value] of Object.entries(env)) if (value && /(?:TOKEN|SECRET|PASSWORD|API_KEY|PRIVATE_KEY|AUTHORIZATION|COOKIE|CREDENTIAL)/i.test(key)) throw new Error('prepare-credential-environment-forbidden');
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { access, mkdir, copyFile, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { access, mkdir, copyFile, writeFile, readdir } from 'node:fs/promises';
|
|
2
2
|
import { constants } from 'node:fs';
|
|
3
3
|
import { execFileSync } from 'node:child_process';
|
|
4
|
-
import { resolve, join, dirname } from 'node:path';
|
|
4
|
+
import { resolve, join, dirname, relative } from 'node:path';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { assert } from './validation.mjs';
|
|
7
|
-
import { toolFiles } from '../../edition-authoring/files.mjs';
|
|
8
7
|
const base = resolve(dirname(fileURLToPath(import.meta.url)),'../..');
|
|
9
8
|
/** Generate alongside an edition. The existing edition tree is never an output directory. */
|
|
10
9
|
export async function createTemplates({destination,project,sourcePaths,commands,loops=[]}) {
|
|
@@ -15,7 +14,13 @@ export async function createTemplates({destination,project,sourcePaths,commands,
|
|
|
15
14
|
// A new directory only: callers add actual edition sources then run the lock command.
|
|
16
15
|
const scripts = join(root,'scripts'); await mkdir(scripts);
|
|
17
16
|
for (const name of ['operations.mjs','project-operations.mjs']) await copyFile(join(base,'operations',name),join(scripts,name),constants.COPYFILE_EXCL);
|
|
18
|
-
|
|
17
|
+
const tools = join(base, 'edition-authoring');
|
|
18
|
+
const modules = (await readdir(tools)).filter(name => name.endsWith('.mjs') && !name.endsWith('.test.mjs'));
|
|
19
|
+
const vendored = (await readdir(join(tools, 'vendor'), {recursive: true, withFileTypes: true})).filter(entry => {
|
|
20
|
+
if (!entry.isFile() && !entry.isDirectory()) throw Error(`Unsupported vendored tool entry: ${entry.name}`);
|
|
21
|
+
return entry.isFile();
|
|
22
|
+
}).map(entry => relative(tools, join(entry.parentPath, entry.name)));
|
|
23
|
+
for (const name of [...modules, ...vendored].sort()) { await mkdir(dirname(join(scripts,'edition-authoring',name)),{recursive:true}); await copyFile(join(base,'edition-authoring',name),join(scripts,'edition-authoring',name),constants.COPYFILE_EXCL); }
|
|
19
24
|
await writeFile(join(root,'OPERATIONS.json'),JSON.stringify({version:2,project,runtime:{requirements:[{name:'node',version:'>=22.12.0'}]},requiredEnvironment:[],commands,loops},null,2)+'\n',{flag:'wx'});
|
|
20
25
|
await writeFile(join(root,'edition.json'),JSON.stringify({schemaVersion:1,id:project,toolchain:{id:'runeditrun/edition-authoring',version:'1.1.0'},parent:null,components:[{id:project,role:'edition',source:{kind:'local',paths:sourcePaths}}]},null,2)+'\n',{flag:'wx'});
|
|
21
26
|
return {root,lock:['node','scripts/edition-authoring/cli.mjs','lock'],check:['node','scripts/edition-authoring/cli.mjs','check']};
|
|
@@ -35,8 +35,8 @@ export async function deletionStatements(ctx, d, plan, entries, id, deleted) {
|
|
|
35
35
|
}
|
|
36
36
|
return out;
|
|
37
37
|
}
|
|
38
|
-
export async function firstStatements(ctx, d, plan, label) {
|
|
39
|
-
return d.beforeStatements ? await d.beforeStatements({ db: ctx.db, subject: plan.subject, label }) : [];
|
|
38
|
+
export async function firstStatements(ctx, d, plan, label, operation) {
|
|
39
|
+
return d.beforeStatements ? await d.beforeStatements({ db: ctx.db, subject: plan.subject, label, operation }) : [];
|
|
40
40
|
}
|
|
41
41
|
// `relabel: false` (a slice of the plan) leaves the table-wide relabel of earlier audit rows to the final batch.
|
|
42
42
|
export const cleanupStatements = (ctx, d, plan, label, options) => [...redactStatements(ctx.db, d, plan), ...auditStatements(ctx.db, d, plan, label, options)];
|
|
@@ -72,7 +72,7 @@ async function sliceStep(ctx, d, op, entries, { first = null, rest = null } = {}
|
|
|
72
72
|
guard(db, "EXISTS(SELECT 1 FROM rer_erasure_operations WHERE id=? AND step=? AND phase='deleting')", id, op.step),
|
|
73
73
|
...await fence.transfers()];
|
|
74
74
|
batch.push(...await scopeStatements(db, d, first ?? plan, id, first ? [...entries, first.root] : entries, { whole: Boolean(first) }));
|
|
75
|
-
if (first) batch.push(...await firstStatements(ctx, d, plan, op.label));
|
|
75
|
+
if (first) batch.push(...await firstStatements(ctx, d, plan, op.label, { id: op.id, at: op.requested_at }));
|
|
76
76
|
batch.push(...await verificationStatements(db, d, plan, id, op.label));
|
|
77
77
|
batch.push(...cleanupStatements(ctx, d, plan, op.label, { relabel: false }));
|
|
78
78
|
batch.push(...await deletionStatements(ctx, d, plan, entries, id, deleted));
|
|
@@ -117,7 +117,7 @@ export async function step(ctx, d, id, { rows = 500 } = {}) {
|
|
|
117
117
|
guard(db, "EXISTS(SELECT 1 FROM rer_erasure_operations WHERE id=? AND step=? AND phase='deleting')", id, op.step),
|
|
118
118
|
...await fence.transfers()];
|
|
119
119
|
batch.push(...await scopeStatements(db, d, plan, id, [...take, plan.root]));
|
|
120
|
-
if (op.step === 0) batch.push(...await firstStatements(ctx, d, plan, op.label));
|
|
120
|
+
if (op.step === 0) batch.push(...await firstStatements(ctx, d, plan, op.label, { id: op.id, at: op.requested_at }));
|
|
121
121
|
batch.push(...await verificationStatements(db, d, plan, id, op.label));
|
|
122
122
|
batch.push(...cleanupStatements(ctx, d, plan, op.label));
|
|
123
123
|
batch.push(...await deletionStatements(ctx, d, plan, take, id, deleted));
|
|
@@ -5,7 +5,7 @@ export type Edge = Match & { table: string; blob?: { bucket: string; keyColumn:
|
|
|
5
5
|
export type Redaction = Match & { table: string; set: Record<string, Cell>; reason: string };
|
|
6
6
|
export interface AuditReference { table: string; column: string; of: string; where?: string; clear?: Record<string, Cell> }
|
|
7
7
|
export interface Retained { table: string; reason: string }
|
|
8
|
-
export type PolicyInput = { db: D1Database; subject: Key; label: string };
|
|
8
|
+
export type PolicyInput = { db: D1Database; subject: Key; label: string; operation?: { id: string; at: string } };
|
|
9
9
|
export type RootRedaction = { identityColumns?: string[]; set: Record<string, Cell> | ((input: { subject: Key; anonymousId: string }) => Record<string, Cell> | Promise<Record<string, Cell>>); reason: string };
|
|
10
10
|
export interface Declaration {
|
|
11
11
|
entity: string;
|
|
@@ -75,7 +75,7 @@ export function createErasure({ db, buckets = {}, portability, declarations }) {
|
|
|
75
75
|
insist(!reason, 'held:' + reason, 409);
|
|
76
76
|
const id = randomHex(16), label = newLabel(), now = new Date().toISOString(), deleted = {};
|
|
77
77
|
const statements = [...await ctx.fence.transfers(), openStatement(db, d, { id, subject: plan.subject, label, planned: totals.deleted, blobs: totals.blobs, now }),
|
|
78
|
-
...await scopeStatements(db, d, plan, id), ...await firstStatements(ctx, d, plan, label), ...await verificationStatements(db, d, plan, id, label), ...cleanupStatements(ctx, d, plan, label), ...await deletionStatements(ctx, d, plan, plan.order, id, deleted),
|
|
78
|
+
...await scopeStatements(db, d, plan, id), ...await firstStatements(ctx, d, plan, label, { id, at: now }), ...await verificationStatements(db, d, plan, id, label), ...cleanupStatements(ctx, d, plan, label), ...await deletionStatements(ctx, d, plan, plan.order, id, deleted),
|
|
79
79
|
...await finalStatements(ctx, d, plan, id, label, now, deleted), progressStatement(db, id, 0, deleted), db.prepare('DELETE FROM rer_erasure_guard')];
|
|
80
80
|
return { operationId: id, statements: statements.map(toD1Sql) };
|
|
81
81
|
},
|
|
@@ -90,7 +90,10 @@ SELECT-only batches at the smaller of the caller limit and 2 MiB. Each batch
|
|
|
90
90
|
checks the same monotonic custody revisions, including tombstone changes;
|
|
91
91
|
the original plan revision is captured before decoding. This requires the
|
|
92
92
|
additive custody-state schema. Ordinary reads without a transport policy retain
|
|
93
|
-
their existing atomic admission
|
|
93
|
+
their existing atomic admission, except preserved undo-report reads whose kit-only
|
|
94
|
+
preconditions exceed 2 MiB: those use the bounded SELECT checks and final revision
|
|
95
|
+
fence described under "Adopting preserved-conflict undo" below.
|
|
96
|
+
`readD1PersistedImport` keeps one complete
|
|
94
97
|
atomic native snapshot capped at 2 MiB even when its caller omits a limit. It
|
|
95
98
|
reads each destination job's `ik_d1_custody_states` revision (the same additive
|
|
96
99
|
schema) before any custody and fences those revisions instead of resending
|
|
@@ -878,6 +881,18 @@ provenance IDs, owner IDs and reversal flags, including SQL storage types. Exact
|
|
|
878
881
|
report bytes, positions and a scoped row count remain fenced in the same batch;
|
|
879
882
|
the report hash is not a substitute for those comparisons. Resuming undo and
|
|
880
883
|
reading terminal pages therefore do not add one SQL predicate per prior effect.
|
|
884
|
+
|
|
885
|
+
Exact row snapshots may use chunk-local lexical templates to share repeated
|
|
886
|
+
JSON structure and evidence. SQLite reconstructs the original text slices;
|
|
887
|
+
comparison still covers every original byte, field, position and scoped count.
|
|
888
|
+
Without an explicit transport policy, preserved undo checks kit-only
|
|
889
|
+
preconditions larger than 2 MiB in bounded SELECT batches under monotonic
|
|
890
|
+
custody revisions, then fences those same revisions before any mutation.
|
|
891
|
+
Report reads use the same kit-only checks and final revision fence.
|
|
892
|
+
The existing custody-state schema is required for that large-snapshot path.
|
|
893
|
+
Native observations, indivisible reversal groups, report postconditions and
|
|
894
|
+
all native, attribution, disposition and progress writes remain together in
|
|
895
|
+
one atomic batch. No native mutation is split across RPCs.
|
|
881
896
|
For large native sets, a snapshot can supply `nativeRows`, the complete
|
|
882
897
|
destination set returned by `targets.list`. Kit requires `targets.matchesMany`,
|
|
883
898
|
rejects duplicate or differently bound rows, and compares each observation's
|
|
@@ -15,6 +15,7 @@ import { readOriginals } from './originals.ts';
|
|
|
15
15
|
import { reversalState, reportHash, reversalGuards, undoEvidence } from './undo-state.ts';
|
|
16
16
|
import { classifyUndo } from './undo-snapshot.ts';
|
|
17
17
|
import { insertJsonRows } from './sets.ts';
|
|
18
|
+
import { compactCustodyGuards } from './read-fence.ts';
|
|
18
19
|
|
|
19
20
|
/** Called after public authority, binding, reversal-mode and custody checks. */
|
|
20
21
|
export async function preserveReverse(current: { job: ImportJob; plan: ImportPlan }, context: D1ImportContext, action: 'undo' | 'rollback'): Promise<ImportJob> {
|
|
@@ -23,7 +24,9 @@ export async function preserveReverse(current: { job: ImportJob; plan: ImportPla
|
|
|
23
24
|
const { database: db, targets } = context.storage;
|
|
24
25
|
if (!targets.preparePreservedUndo) throw new Error('IMPORT_PRESERVED_UNDO_ADAPTER_REQUIRED');
|
|
25
26
|
if (current.job.state !== reversing) {
|
|
26
|
-
|
|
27
|
+
const scope = nativeStagingScope(current.job, current.plan);
|
|
28
|
+
const guards = await compactCustodyGuards(context, [...boundary(current.job, current.plan, action), await noSuccessors(id, context), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context)], scope, action);
|
|
29
|
+
await atomic(context, action, [...guards, changed(sql('UPDATE ik_jobs SET state=?,fence=fence+1 WHERE id=?', reversing, id),updateRows('ik_jobs',row=>row.id===id,row=>({...row,state:reversing,fence:current.job.fence+1})))], storedReads(current.job),scope);
|
|
27
30
|
current = await stored(id, context, action);
|
|
28
31
|
}
|
|
29
32
|
let published: ImportJob | undefined;
|
|
@@ -33,7 +36,7 @@ export async function preserveReverse(current: { job: ImportJob; plan: ImportPla
|
|
|
33
36
|
if (job.state !== reversing) throw new Error('REVERSAL_MODE_CONFLICT');
|
|
34
37
|
await readOriginals(plan, id, context);
|
|
35
38
|
const scope=nativeStagingScope(job,plan);
|
|
36
|
-
const statements: D1Sql[] = [...boundary(job, plan, action), await noSuccessors(id, context), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)];
|
|
39
|
+
const statements: D1Sql[] = await compactCustodyGuards(context, [...boundary(job, plan, action), await noSuccessors(id, context), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], scope, action);
|
|
37
40
|
const { results: rows } = await db.prepare('SELECT * FROM ik_provenance WHERE job_id=? AND reversed=0 ORDER BY ordinal DESC,effect_ordinal DESC').bind(id).all();
|
|
38
41
|
const retained = new Set(state.dispositions.filter(d => d.disposition === 'retained').map(d => d.effectId));
|
|
39
42
|
const effects: D1UndoEffect[] = [];
|
|
@@ -9,11 +9,19 @@ import { batchRows, checkBatchResults } from './read-results.ts';
|
|
|
9
9
|
import { erasedIndexGuards } from './erasure-index.ts';
|
|
10
10
|
import { jsonChunks } from './sets.ts';
|
|
11
11
|
|
|
12
|
+
/** Transfer oversized kit-only preconditions into an exact, revision-fenced
|
|
13
|
+
* read. The returned predicates must precede every mutation in one atomic
|
|
14
|
+
* batch. Native snapshots and postconditions never use this transfer. */
|
|
15
|
+
export async function compactCustodyGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, action: ImportAction): Promise<D1Sql[]> {
|
|
16
|
+
if (context.maxBatchBytes !== undefined || batchBytes(packGuards(guards, true)) <= 2 * 1024 * 1024) return guards;
|
|
17
|
+
return readBoundedGuards(context, guards, scope, 2 * 1024 * 1024, action);
|
|
18
|
+
}
|
|
19
|
+
|
|
12
20
|
/** Check exact custody without writing private copies merely to read them.
|
|
13
21
|
* Every batch sees the same monotonic revisions, captured before any exact
|
|
14
22
|
* check. The final fence also retains the plan revision captured before decode.
|
|
15
23
|
* Native target reads have no such custody fence and remain one atomic batch. */
|
|
16
|
-
export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, limit: number, action: ImportAction = 'read'): Promise<
|
|
24
|
+
export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[], scope: NativeStagingScope, limit: number, action: ImportAction = 'read'): Promise<D1Sql[]> {
|
|
17
25
|
const ids = new Set([scope.job.id]);
|
|
18
26
|
for (const query of guards) {
|
|
19
27
|
if (query.retainedValues?.privacyJobId) ids.add(query.retainedValues.privacyJobId);
|
|
@@ -40,7 +48,10 @@ export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[
|
|
|
40
48
|
}
|
|
41
49
|
if (revisions.length !== ids.size || new Set(revisions.map(row => row[0])).size !== ids.size) throw new Error('IMPORT_ATOMIC_GUARD');
|
|
42
50
|
const fences = jsonChunks(revisions).map(body => guard(sql("NOT EXISTS(SELECT 1 FROM json_each(?) e WHERE NOT EXISTS(SELECT 1 FROM ik_d1_custody_states s JOIN ik_d1_custody_revisions r ON r.job_id=s.job_id WHERE s.job_id=json_extract(e.value,'$[0]') AND s.revision=json_extract(e.value,'$[1]') AND r.revision=json_extract(e.value,'$[2]')))", body)));
|
|
43
|
-
|
|
51
|
+
// Explicitly bounded callers captured the staging plan revision before
|
|
52
|
+
// decode. Ordinary callers instead validate their exact plan guards below
|
|
53
|
+
// (or the large-plan fence), under the revisions just captured here.
|
|
54
|
+
const prefix = [authority(context, action), ...fences, ...context.maxBatchBytes === undefined ? [] : nativeStagingPostBinding(scope)];
|
|
44
55
|
const batches: D1Sql[][] = []; let pending: D1Sql[] = [];
|
|
45
56
|
const packed = (members: D1Sql[]) => packGuards([...prefix, ...members], true);
|
|
46
57
|
for (const member of [...guards, ...erasedIndexGuards(scope.plan)]) {
|
|
@@ -61,4 +72,5 @@ export async function readBoundedGuards(context: D1ImportContext, guards: D1Sql[
|
|
|
61
72
|
if (rows.length !== 1 || rows[0].ok !== 1) throw new Error('IMPORT_ATOMIC_GUARD');
|
|
62
73
|
}
|
|
63
74
|
}
|
|
75
|
+
return prefix;
|
|
64
76
|
}
|
|
@@ -14,6 +14,7 @@ import { noSuccessors, restorePredecessor } from './history.ts';
|
|
|
14
14
|
import { readOriginals } from './originals.ts';
|
|
15
15
|
import { reversalState } from './undo-state.ts';
|
|
16
16
|
import { preserveReverse } from './preserved-reverse.ts';
|
|
17
|
+
import { compactCustodyGuards } from './read-fence.ts';
|
|
17
18
|
import { fencedReversal, recordReversalFence, reversalProgress } from './reversal-fence.ts';
|
|
18
19
|
|
|
19
20
|
export async function reverseD1Import(id: string, action: 'undo' | 'rollback', context: D1ImportContext): Promise<ImportJob> {
|
|
@@ -32,7 +33,9 @@ export async function reverseD1Import(id: string, action: 'undo' | 'rollback', c
|
|
|
32
33
|
if ((action === 'undo' && current.job.state === 'rolled_back') || (action === 'rollback' && current.job.state === 'undone')) throw new Error('REVERSAL_MODE_CONFLICT');
|
|
33
34
|
if (['undone', 'rolled_back'].includes(current.job.state)) {
|
|
34
35
|
await readOriginals(current.plan, id, context);
|
|
35
|
-
|
|
36
|
+
const scope = nativeStagingScope(current.job, current.plan);
|
|
37
|
+
const guards = [...boundary(current.job, current.plan, action), ...custodyGuards(current.plan, id), ...await inventoryGuards(current.job, current.plan, context)];
|
|
38
|
+
await atomic(context, action, reversalState(current.job).policy === 'preserve-conflicts' ? await compactCustodyGuards(context, guards, scope, action) : guards, 0, scope);
|
|
36
39
|
return current.job;
|
|
37
40
|
}
|
|
38
41
|
if (current.job.state === (action === 'undo' ? 'rolling_back' : 'undoing')) throw new Error('REVERSAL_MODE_CONFLICT');
|
|
@@ -4,6 +4,7 @@ import type { D1Database, D1Sql } from './types.ts';
|
|
|
4
4
|
import { guard, sql } from './database.ts';
|
|
5
5
|
import { retainedSql } from './retained-values.ts';
|
|
6
6
|
import { changedLazy, putRow } from './expected-state.ts';
|
|
7
|
+
import { snapshotText, snapshotTextRows, snapshotTextSaves } from './snapshot-text.ts';
|
|
7
8
|
|
|
8
9
|
// JSON snapshots are split by UTF-8 size, never by source count alone. Each
|
|
9
10
|
// statement compares the original column values, not their recorded hashes.
|
|
@@ -59,6 +60,13 @@ export function exactRows(table: string, columns: string[], rows: any[], scope?:
|
|
|
59
60
|
} else ordinary.push(picked);
|
|
60
61
|
}
|
|
61
62
|
for (const chunk of jsonChunks(ordinary, maxBytes)) {
|
|
63
|
+
const encoded = snapshotText(JSON.parse(chunk).map((row: any) => columns.map(column => row[column])));
|
|
64
|
+
if (snapshotTextSaves(chunk, encoded)) {
|
|
65
|
+
const parts = [snapshotTextRows.slice(0, snapshotTextRows.indexOf('?')), snapshotTextRows.slice(snapshotTextRows.indexOf('?') + 1) + ` WHERE NOT EXISTS(SELECT 1 FROM ${table} t WHERE ${columns.map((c, i) => `t.${c} IS json_extract(e.value,'$[${i}]')`).join(' AND ')})`];
|
|
66
|
+
parts[0] = 'NOT EXISTS(' + parts[0]; parts[1] += ')';
|
|
67
|
+
result.push(guard(retain ? retainedSql(parts, [encoded], [0]) : sql(parts.join('?'), encoded)));
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
62
70
|
const parts=['NOT EXISTS(SELECT 1 FROM json_each(',`) e WHERE NOT EXISTS(SELECT 1 FROM ${table} t WHERE ${columns.map(c => `t.${c} IS json_extract(e.value,'$.${c}')`).join(' AND ')}))`];
|
|
63
71
|
result.push(guard(retain?retainedSql(parts,[chunk],[0]):sql(parts.join('?'),chunk)));
|
|
64
72
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
|
|
3
|
+
type Piece = string | { value: string };
|
|
4
|
+
|
|
5
|
+
/** Split JSON text at value tokens, retaining its original lexical bytes.
|
|
6
|
+
* Keys, whitespace, number spelling and escaping are never reserialized. */
|
|
7
|
+
function textPieces(text: string): Piece[] {
|
|
8
|
+
try { JSON.parse(text); } catch { return [{ value: JSON.stringify(text) }]; }
|
|
9
|
+
const pieces: Piece[] = ['"'];
|
|
10
|
+
const escape = (value: string) => JSON.stringify(value).slice(1, -1);
|
|
11
|
+
let position = 0;
|
|
12
|
+
for (const match of text.matchAll(/"(?:\\.|[^"\\])*"|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g)) {
|
|
13
|
+
const end = match.index + match[0].length;
|
|
14
|
+
if (/^\s*:/.test(text.slice(end))) continue;
|
|
15
|
+
pieces.push(escape(text.slice(position, match.index)), { value: escape(match[0]) });
|
|
16
|
+
position = end;
|
|
17
|
+
}
|
|
18
|
+
pieces.push(escape(text.slice(position)), '"');
|
|
19
|
+
return pieces;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A chunk-local template dictionary for exact row snapshots. Repeated JSON
|
|
23
|
+
* structure and identical evidence travel once; unique values remain literal
|
|
24
|
+
* text slices. SQLite concatenates those slices, never interprets them as SQL
|
|
25
|
+
* or regenerates the JSON whose exact bytes the guard must compare. */
|
|
26
|
+
export function snapshotText(rows: unknown[][]): string {
|
|
27
|
+
const pieces = rows.map(row => row.flatMap((value, index): Piece[] => [
|
|
28
|
+
index ? ',' : '[',
|
|
29
|
+
...(typeof value === 'string' && /^\s*[\[{]/.test(value) ? textPieces(value) : [{ value: JSON.stringify(value) ?? 'null' }]),
|
|
30
|
+
...index === row.length - 1 ? [']'] : [],
|
|
31
|
+
]));
|
|
32
|
+
const counts = new Map<string, number>();
|
|
33
|
+
for (const row of pieces) for (const piece of row) if (typeof piece !== 'string') counts.set(piece.value, (counts.get(piece.value) ?? 0) + 1);
|
|
34
|
+
const templates: (string | number)[][] = [], known = new Map<string, number>();
|
|
35
|
+
const encoded = pieces.map(row => {
|
|
36
|
+
const template: (string | number)[] = [], values: string[] = [], slots = new Map<string, number>();
|
|
37
|
+
const literal = (text: string) => {
|
|
38
|
+
if (typeof template.at(-1) === 'string') template[template.length - 1] = String(template.at(-1)) + text;
|
|
39
|
+
else template.push(text);
|
|
40
|
+
};
|
|
41
|
+
for (const piece of row) {
|
|
42
|
+
if (typeof piece === 'string') literal(piece);
|
|
43
|
+
else if (counts.get(piece.value)! > rows.length / 2) literal(piece.value);
|
|
44
|
+
else {
|
|
45
|
+
let slot = slots.get(piece.value);
|
|
46
|
+
if (slot === undefined) { slot = values.length + 1; slots.set(piece.value, slot); values.push(piece.value); }
|
|
47
|
+
template.push(slot);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const key = JSON.stringify(template);
|
|
51
|
+
let index = known.get(key);
|
|
52
|
+
if (index === undefined) { index = templates.length; known.set(key, index); templates.push(template); }
|
|
53
|
+
return [index, ...values];
|
|
54
|
+
});
|
|
55
|
+
return JSON.stringify([templates, encoded]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const snapshotTextRows = `WITH snapshot(body) AS (SELECT ?), decoded(value) AS MATERIALIZED (
|
|
59
|
+
SELECT (SELECT group_concat(piece,'') FROM (
|
|
60
|
+
SELECT CASE WHEN p.type='integer' THEN json_extract(r.value,'$['||p.value||']') ELSE p.value END AS piece
|
|
61
|
+
FROM json_each(snapshot.body,'$[0]['||json_extract(r.value,'$[0]')||']') p ORDER BY CAST(p.key AS INTEGER)
|
|
62
|
+
)) FROM snapshot,json_each(snapshot.body,'$[1]') r
|
|
63
|
+
) SELECT 1 FROM decoded e`;
|
|
64
|
+
|
|
65
|
+
// Include JSON escaping in the choice, as the binding itself is a JSON string
|
|
66
|
+
// in D1's request. Small snapshots keep the simpler direct representation.
|
|
67
|
+
export const snapshotTextSaves = (plain: string, encoded: string): boolean =>
|
|
68
|
+
Buffer.byteLength(JSON.stringify(encoded)) + Buffer.byteLength(snapshotTextRows) < Buffer.byteLength(JSON.stringify(plain));
|
|
@@ -7,6 +7,7 @@ import { custodyGuards, inventoryGuards } from './integrity.ts';
|
|
|
7
7
|
import { readOriginals } from './originals.ts';
|
|
8
8
|
import { reversalState } from './undo-state.ts';
|
|
9
9
|
import { nativeStagingScope } from './native-staging-context.ts';
|
|
10
|
+
import { compactCustodyGuards } from './read-fence.ts';
|
|
10
11
|
|
|
11
12
|
export async function readD1ImportUndoReport(id: string, context: D1ImportContext, page: { offset?: number; limit?: number } = {}): Promise<D1UndoReport> {
|
|
12
13
|
const offset = page.offset ?? 0, limit = page.limit ?? 100;
|
|
@@ -15,7 +16,9 @@ export async function readD1ImportUndoReport(id: string, context: D1ImportContex
|
|
|
15
16
|
await authorize(context, 'read');
|
|
16
17
|
const { job, plan } = await stored(id, context);
|
|
17
18
|
await readOriginals(plan, id, context);
|
|
18
|
-
|
|
19
|
+
const scope = nativeStagingScope(job, plan);
|
|
20
|
+
const guards = await compactCustodyGuards(context, [...boundary(job, plan), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], scope, 'read');
|
|
21
|
+
await readAtomic(context, guards, scope);
|
|
19
22
|
const state = reversalState(job);
|
|
20
23
|
const outcome = !['undone','rolled_back'].includes(job.state) ? 'pending' : state.dispositions.some(d => d.disposition === 'retained') ? 'compensated' : 'undone';
|
|
21
24
|
return { job, policy: state.policy, cursor: state.cursor, total: state.dispositions.length, nextOffset: offset + limit < state.dispositions.length ? offset + limit : null, dispositions: state.dispositions.slice(offset, offset + limit), outcome, ...(state.conversion ? { conversion: state.conversion } : {}) };
|
|
@@ -32,10 +32,15 @@ Owner history and undo must include jobs from either authority.
|
|
|
32
32
|
Unadapted recorded dependencies advance together. If a dependency is marked
|
|
33
33
|
adapted or is unrecorded seed source, refresh preserves it and refuses before
|
|
34
34
|
writing when an incoming module needs an export it lacks. Use the printed
|
|
35
|
-
comparison command
|
|
36
|
-
|
|
37
|
-
`
|
|
38
|
-
|
|
35
|
+
comparison command and save the seed file plus its original Base merge
|
|
36
|
+
ancestor. Restore the retained file from the incoming Base copy and
|
|
37
|
+
`record-base` that file at the pin which imports it. Add all named packages
|
|
38
|
+
at Base's listed versions with the seed's package manager, then refresh to
|
|
39
|
+
deliver the new Base dependencies. Only after that refresh, use
|
|
40
|
+
`git merge-file` to merge the saved seed edits onto the new Base file,
|
|
41
|
+
resolve conflicts, run `adapt-base` on the reconciled file, and refresh
|
|
42
|
+
again. Remove the comparison and backup files after reconciliation.
|
|
43
|
+
Acknowledging the old bytes alone does not resolve a missing export.
|
|
39
44
|
Apply these in new numbered seed migrations, in this order,
|
|
40
45
|
skipping any the seed already applied:
|
|
41
46
|
- the D1 kit schemas, in the order `initializeD1ImportStorage` in
|