@runeditrun/rer 0.2.68 → 0.2.69

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/inspect.mjs CHANGED
@@ -141,8 +141,12 @@ export async function check(project, explicitProfile) {
141
141
  // command still calling one.
142
142
  async function packageClauses(root, pkg) {
143
143
  const declared = declaredPackage(pkg), locked = await lockedPackage(root), clauses = [];
144
+ const patchHash = /\(patch_hash=([^)]*)\)/.exec(locked?.version ?? '')?.[1];
145
+ const patchPath = patchHash ? await rerPatchPath(root, pkg, locked.version, patchHash) : undefined;
144
146
  clauses.push(['SC-13.package.pin', pkg.devDependencies?.[packageName] === declared && exactVersion.test(declared), `Pin ${packageName} in devDependencies to an exact version (for example "0.2.0"), not ${JSON.stringify(declared)} or a runtime dependency.`]);
145
- clauses.push(['SC-13.package.lockfile', locked?.locked && locked.specifier === declared && locked.version === declared, `Run pnpm install so pnpm-lock.yaml locks ${packageName} ${declared}${locked?.version ? ` (it locks ${locked.version})` : ''}.`]);
147
+ clauses.push(['SC-13.package.lockfile', locked?.locked && locked.specifier === declared && locked.version === declared, patchHash
148
+ ? `${packageName} is patched locally${patchPath ? ` by ${patchPath}` : ' (see pnpm patchedDependencies metadata)'}. This change belongs upstream in Base; adopt a published rer version containing it and remove the local patch before checking the first-party contract again.`
149
+ : `Run pnpm install so pnpm-lock.yaml locks ${packageName} ${declared}${locked?.version ? ` (it locks ${locked.version})` : ''}.`]);
146
150
  for (const path of vendoredPaths) {
147
151
  let present = true; try { await lstat(resolve(root, path)); } catch { present = false; }
148
152
  clauses.push(['SC-13.package.vendored.' + path, !present, `Remove ${path}: a checkout that pins ${packageName} runs the package, not a vendored copy (node scripts/export-cli.mjs --to-package converts one).`]);
@@ -153,6 +157,30 @@ async function packageClauses(root, pkg) {
153
157
  return clauses;
154
158
  }
155
159
 
160
+ // pnpm's lockfile records the selected patch hash and path, including when
161
+ // patchedDependencies lives in pnpm-workspace.yaml rather than package.json.
162
+ async function rerPatchPath(root, pkg, version, hash) {
163
+ const selector = `${packageName}@${version.split('(')[0]}`;
164
+ let text;
165
+ try { text = await readFile(resolve(root, 'pnpm-lock.yaml'), 'utf8'); } catch {}
166
+ const scalar = value => value.trim().replace(/^(['"])(.*)\1$/, (_, quote, content) => quote === "'" ? content.replace(/''/g, "'") : content);
167
+ const patches = [];
168
+ let reading = false, entry;
169
+ for (const line of (text ?? '').split('\n')) {
170
+ if (line === 'patchedDependencies:') { reading = true; continue; }
171
+ if (!reading || !line.trim()) continue;
172
+ if (/^\S/.test(line)) break;
173
+ const key = /^ (\S.*):\s*$/.exec(line);
174
+ if (key) { entry = { selector: scalar(key[1]) }; patches.push(entry); continue; }
175
+ const field = /^ (hash|path):\s*(.+)$/.exec(line);
176
+ if (entry && field) entry[field[1]] = scalar(field[2]);
177
+ }
178
+ const selected = patches.find(patch => (patch.selector === packageName || patch.selector.startsWith(packageName + '@')) && patch.hash === hash);
179
+ if (selected?.path) return selected.path;
180
+ const declared = pkg.pnpm?.patchedDependencies;
181
+ return declared?.[selector] ?? declared?.[packageName];
182
+ }
183
+
156
184
  // Without the pinned pnpm on PATH, rer provides it for this command only
157
185
  // (cli/pnpm-runtime.mjs); it never changes global tools.
158
186
  export async function tools(root, store, signal) {
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.69
4
+
5
+ - **Renewable hosted import leases (#328).** A hosted import holds a five-minute renewable lease (30-second heartbeat while active) with fenced request claims; the next restore safely aborts stale staging and owned blobs through the existing recovery, and owner active/status/abort routes stay authenticated.
6
+ - **One-click sign-in offers providers (#324).** One-click owner entry also offers the visible provider forms, carrying returnTo, with provider origins in the CSP; SC-13 names a local rer patch and points it upstream to Base.
7
+ - **Run site dates and command (#326).** /contract shows US short UTC dates, and the /run command's `npx` start stays whole, breaking only before the repository.
8
+ - **CLI setup tests (#325).** Five CLI setup tests no longer pre-probe a --port, and shared fixture cleanup tolerates a late .DS_Store.
9
+
3
10
  ## 0.2.68
4
11
 
5
12
  - **Retained-archive restores (#316, #318).** Placing an older archive treats a CREATE TRIGGER or DROP TRIGGER statement bounded as exactly one trigger as schema-only, and restore drops and recreates the destination's triggers around insertion; a later write that reaches an archived table through a trigger is still refused. Base reads RER Platform's rer_migrations ledger, places an archive without a migration record by the ledger it carries (every archived name and hash must match an applied migration), and admits a later UPDATE or DELETE of a table the archive holds no rows of.
@@ -58,7 +58,8 @@ export function withDevIdentity(worker, { bindings = env => env, config: authCon
58
58
  ...person, displayName: (await context.store.byEmail(person.email))?.displayName ?? declared.find(p => p.as === person.as)?.displayName ?? person.email,
59
59
  href: `/auth/dev/enter?as=${encodeURIComponent(person.as)}&returnTo=${encodeURIComponent(returnTo)}`,
60
60
  })));
61
- return decorateLabel(await oneClickPage({ branding: context.config.branding, people, method: request.method }), env.RER_DEMO === '1' ? 'Demo' : 'Dev');
61
+ const providers = context.auth.providers ? (await context.auth.providers.list()).filter(provider => provider.visible) : [];
62
+ return decorateLabel(await oneClickPage({ branding: context.config.branding, people, providers, returnTo, method: request.method }), env.RER_DEMO === '1' ? 'Demo' : 'Dev');
62
63
  }
63
64
  if (path === '/auth/dev/enter' || path === '/__dev/personas' || path === '/auth/local/cli-session') {
64
65
  if (!gate || env.RER_DEV_IDENTITY === 'off' || (path !== '/auth/local/cli-session' && !await gate.admits(request))) return rejectedResponse(request, devNotFound());
@@ -51,13 +51,13 @@ export function createSignInHandler(auth, { setup, links, defaultReturnTo = '/ap
51
51
  const branding = auth.presentation;
52
52
  const returnTo = safeReturnTo(url.searchParams.get('returnTo'), defaultReturnTo);
53
53
  const hint = emailHint(url.searchParams.get('login_hint'));
54
+ const providers = auth.providers ? (await auth.providers.list()).filter(p => p.visible) : [];
54
55
  const people = await auth.oneClick?.people(request);
55
- if (people?.length) return oneClickPage({ branding, method: request.method, people: people.map(person => {
56
+ if (people?.length) return oneClickPage({ branding, method: request.method, providers, returnTo, people: people.map(person => {
56
57
  const href = new URL(person.href, url.origin);
57
58
  href.searchParams.set('returnTo', returnTo);
58
59
  return { ...person, href: href.pathname + href.search };
59
60
  }) });
60
- const providers = auth.providers ? (await auth.providers.list()).filter(p => p.visible) : [];
61
61
  const parts = [];
62
62
  const emailAvailable = auth.emailSignInAvailable;
63
63
  const notice = noticeFor(url.searchParams.get('notice'), emailAvailable)
@@ -1,7 +1,12 @@
1
1
  import { escapeHtml } from '../http.mjs';
2
2
  import { pageResponse } from '../page.mjs';
3
3
 
4
- export function oneClickPage({ branding, people, method = 'GET' }) {
5
- const content = `<div class="stack">${people.map(person => `<a class="action" href="${escapeHtml(person.href)}">Sign in as ${escapeHtml(person.displayName || person.email)}</a>`).join('')}</div>`;
6
- return pageResponse({ branding, heading: `Sign in to ${escapeHtml(branding.productName)}`, content, method });
4
+ export function oneClickPage({ branding, people, providers = [], returnTo = '/app', method = 'GET' }) {
5
+ const owners = people.map(person => `<a class="action" href="${escapeHtml(person.href)}">Sign in as ${escapeHtml(person.displayName || person.email)}</a>`).join('');
6
+ const choices = providers.map(provider => `<form method="post" action="${escapeHtml(provider.startPath)}"><input type="hidden" name="returnTo" value="${escapeHtml(returnTo)}"><button class="secondary" type="submit">Continue with ${escapeHtml(provider.label)}</button></form>`).join('');
7
+ const content = `<div class="stack">${owners}${choices}</div>`;
8
+ const formTargets = [...new Set(providers.flatMap(provider => provider.authorizationOrigins))];
9
+ // Native provider forms need their Origin and permission for the redirect.
10
+ return pageResponse({ branding, heading: `Sign in to ${escapeHtml(branding.productName)}`, content, method,
11
+ ...(providers.length ? { referrer: 'strict-origin', formTargets } : {}) });
7
12
  }
@@ -434,8 +434,32 @@ bridge). The batch repeats the check. Any other row refuses with
434
434
  export, reset and import commands to run. A Worker import has no replay, so a
435
435
  hosted restore still needs such tables in `replaceableTables`.
436
436
 
437
- An empty destination must be reserved for restore: pause destination writers and
438
- serialize finalize/abort calls. Product writes to source tables can continue.
437
+ An empty destination must be reserved for restore: pause destination writers.
438
+ Product writes to source tables can continue. Import upload, finalize and abort
439
+ requests are serialized by a durable request claim.
440
+
441
+ Imports hold a **five-minute inactivity lease**, created with the job and renewed
442
+ by every upload/finalize request, then every 30 seconds while that request is
443
+ working (including verification, R2 transfers and product callbacks). This is a
444
+ recovery window, not a total restore limit: the measured 20,000-row round trip
445
+ below already takes 171 seconds, and callbacks have no wall-time bound. A fresh
446
+ import returns `409 import_running` with `jobId` and ISO `abortableAt`. The owner
447
+ can find its active job with `GET /api/portability/imports` (`{active:status}`
448
+ or `{active:null}`), and inspect `GET .../imports/:id`: `abortableAt` and `stale` describe
449
+ its lease. The existing authenticated `POST .../imports/:id/abort` (or CLI
450
+ `abort --resume ID`) discards stale staging and owned objects. A live request
451
+ cannot be aborted through that route. An expired upload/finalize returns
452
+ `409 import_lease_expired`; start the restore again.
453
+
454
+ The next import automatically aborts a stale job through that same cleanup path
455
+ before claiming the destination. Applying jobs use the existing owned-object
456
+ recovery: product rows publish in one fenced D1 transaction, so an interrupted
457
+ pre-commit apply has no product rows to undo; a committed job stays complete,
458
+ including after a lost acknowledgement. Cleanup never deletes foreign objects.
459
+ Lease bookkeeping is created privately on existing destinations without a product
460
+ migration; an older unfinished job receives one five-minute grace period. The
461
+ local CLI's `abortAbandoned` remains restricted to a sole operator holding its
462
+ checkout lock; hosted callers must use the lease-aware create/status/abort paths.
439
463
  R2 has no compare-and-swap transaction covering bucket emptiness and writes.
440
464
  An interrupted applying job can be finalized again; ownership survives restart.
441
465
  Keep job status until the client acknowledges completion. Abort completed exports
@@ -461,7 +485,8 @@ are `{error:code}`. Request bodies are bounded; `maxBodyBytes` can lower the bou
461
485
  | `POST imports` | Create with `{confirmed:true,manifest}`. |
462
486
  | `PUT imports/:id/parts/:index` | Upload part. |
463
487
  | `POST imports/:id/finalize` | Apply with `{archiveHash,contentHash?,laterJournal?}`. |
464
- | `GET imports/:id` | Status. |
488
+ | `GET imports` | Active import belonging to this actor, or null. |
489
+ | `GET imports/:id` | Status including lease expiry and stale flag. |
465
490
  | `POST imports/:id/abort` | Clean up unfinished import. |
466
491
 
467
492
  ```sh
@@ -564,6 +589,7 @@ modules/portability/migration-statements.mjs
564
589
  modules/portability/legacy-safety.mjs
565
590
  modules/portability/export.mjs
566
591
  modules/portability/import.mjs
592
+ modules/portability/import-lease.mjs
567
593
  modules/portability/import-staging.mjs
568
594
  modules/portability/import-safety.mjs
569
595
  modules/portability/payloads.mjs
@@ -25,6 +25,7 @@ export async function createPortabilityHandler({db,buckets,policy,appOrigin,base
25
25
  if(id)insist(await service.actor(id)===actor,'job_not_found',404);
26
26
  insist(['exports','imports'].includes(collection) && route.length<=4,'route_not_found',404);
27
27
  const api=service[collection];
28
+ if(collection==='imports' && request.method==='GET' && !id)return json({active:await api.active(actor)});
28
29
  if(request.method==='POST' && !id) {
29
30
  const body=await boundedJson(request,maxBodyBytes);
30
31
  if(collection==='imports'){insist(body.confirmed===true,'confirmation_required');return json({id:await api.create(body.manifest,actor)},201);}
@@ -37,6 +38,6 @@ export async function createPortabilityHandler({db,buckets,policy,appOrigin,base
37
38
  if(collection==='imports' && request.method==='PUT' && id && action==='parts' && /^(0|[1-9]\d*)$/.test(index??''))return json(await api.uploadPart(id,Number(index),await boundedJson(request,maxBodyBytes)));
38
39
  if(collection==='imports' && request.method==='POST' && id && action==='finalize' && !index)return json(await api.finalize(id,await boundedJson(request,maxBodyBytes)));
39
40
  throw new PortabilityError('route_not_found',404);
40
- }catch(e){return json({error:e instanceof PortabilityError?e.code:'operation_failed'},e instanceof PortabilityError?e.status:500);}
41
+ }catch(e){return json({error:e instanceof PortabilityError?e.code:'operation_failed',...(e instanceof PortabilityError && e.code==='import_running'?{jobId:e.jobId,abortableAt:e.abortableAt}:{})},e instanceof PortabilityError?e.status:500);}
41
42
  };
42
43
  }
@@ -0,0 +1,99 @@
1
+ import { statement, guard, job, PortabilityError } from './storage.mjs';
2
+
3
+ // Inactivity, not total restore time: the measured 20k-row round trip takes
4
+ // 171s (README). Finalize also awaits unbounded product hooks and multipart R2
5
+ // work, so the owning request renews every 30s throughout those awaits.
6
+ export const IMPORT_LEASE_MS=5*60*1000;
7
+ const RENEW_MS=30*1000;
8
+ const active=row=>['staging','applying'].includes(row.phase);
9
+
10
+ // Private transfer bookkeeping can be added to already-installed destinations
11
+ // without changing product migrations or the archive's schema identity. Keep
12
+ // it separate from metadata: staging/normalization compare that JSON by CAS.
13
+ export async function ensureImportLeases(db) {
14
+ await db.prepare(`CREATE TABLE IF NOT EXISTS rer_portability_import_leases (
15
+ job TEXT PRIMARY KEY REFERENCES rer_portability_jobs(id) ON DELETE CASCADE,
16
+ expires_at INTEGER NOT NULL, holder TEXT
17
+ )`).run();
18
+ // An import left by an older runtime gets one conservative grace period.
19
+ await statement(db,`INSERT OR IGNORE INTO rer_portability_import_leases(job,expires_at)
20
+ SELECT j.id,? FROM rer_portability_jobs j JOIN rer_portability_state s ON s.import_job=j.id
21
+ WHERE j.kind='import' AND j.phase IN ('staging','applying')`,Date.now()+IMPORT_LEASE_MS).run();
22
+ }
23
+ export const createImportLease=(db,id)=>statement(db,'INSERT INTO rer_portability_import_leases(job,expires_at) VALUES(?,?)',id,Date.now()+IMPORT_LEASE_MS);
24
+ const leaseRow=(db,id)=>statement(db,'SELECT * FROM rer_portability_import_leases WHERE job=?',id).first();
25
+ export async function importLeaseStatus(db,row) {
26
+ const lease=await leaseRow(db,row.id);
27
+ return {abortableAt:active(row)&&lease?new Date(lease.expires_at).toISOString():null,stale:active(row)&&!!lease&&lease.expires_at<=Date.now()};
28
+ }
29
+ export async function importRunning(db,id) {
30
+ const error=new PortabilityError('import_running',409);
31
+ error.jobId=id;error.abortableAt=(await importLeaseStatus(db,await job(db,id,'import'))).abortableAt;
32
+ return error;
33
+ }
34
+
35
+ // Upload, finalize and abort each claim one request. Renewal and takeover are
36
+ // conditional writes on that claim; an expired request cannot revive itself.
37
+ // Every mutating D1 batch is fenced by the same claim, including publication.
38
+ export async function withImportLease(context,id,operation,{abort=false,staleOnly=false,soleOperator=false}={}) {
39
+ const db=context.db,holder=crypto.randomUUID(),now=Date.now(),lease=await leaseRow(db,id);
40
+ if(!lease)throw new PortabilityError('import_lease_expired',409);
41
+ if(staleOnly && lease.expires_at>now)throw await importRunning(db,id);
42
+ if(!abort && lease.expires_at<=now)throw new PortabilityError('import_lease_expired',409);
43
+ if(!soleOperator && lease.holder && lease.expires_at>now)throw await importRunning(db,id);
44
+ // Like staging/publication, claim by a transactional SQL guard. Transport
45
+ // adapters need not expose provider-specific UPDATE result metadata.
46
+ try {
47
+ await db.batch([guard(db,`EXISTS(SELECT 1 FROM rer_portability_import_leases WHERE job=? AND expires_at=? AND holder IS ?)
48
+ AND EXISTS(SELECT 1 FROM rer_portability_state WHERE id=1 AND import_job=?)`,id,lease.expires_at,lease.holder,id),
49
+ statement(db,'UPDATE rer_portability_import_leases SET holder=?,expires_at=? WHERE job=?',holder,now+IMPORT_LEASE_MS,id),
50
+ db.prepare('DELETE FROM rer_portability_guard')]);
51
+ }catch(error){
52
+ const current=await leaseRow(db,id);
53
+ if(current?.holder!==lease.holder || current?.expires_at!==lease.expires_at)throw await importRunning(db,id);
54
+ throw error;
55
+ }
56
+ let stopped=false,timer,pending=Promise.resolve(),failure;
57
+ const owned=async()=>{
58
+ const current=await leaseRow(db,id);
59
+ return current?.holder===holder && current.expires_at>Date.now();
60
+ };
61
+ const renew=async()=>{
62
+ const at=Date.now();
63
+ try {
64
+ await db.batch([guard(db,'EXISTS(SELECT 1 FROM rer_portability_import_leases WHERE job=? AND holder=? AND expires_at>?)',id,holder,at),
65
+ statement(db,'UPDATE rer_portability_import_leases SET expires_at=? WHERE job=?',at+IMPORT_LEASE_MS,id),
66
+ db.prepare('DELETE FROM rer_portability_guard')]);
67
+ }catch(error){if(!await owned())throw new PortabilityError('import_lease_expired',409);throw error;}
68
+ };
69
+ const schedule=()=>{
70
+ timer=setTimeout(()=>{
71
+ pending=renew().catch(error=>{failure=error;}).finally(()=>{if(!stopped && !failure)schedule();});
72
+ return pending;
73
+ },RENEW_MS);
74
+ };
75
+ const fence=()=>guard(db,`EXISTS(SELECT 1 FROM rer_portability_import_leases l
76
+ JOIN rer_portability_state s ON s.import_job=l.job WHERE s.id=1 AND l.job=? AND l.holder=? AND l.expires_at>?)`,id,holder,Date.now());
77
+ const native=new WeakMap();
78
+ const batch=async statements=>{
79
+ if(failure)throw failure;
80
+ try {
81
+ const result=await db.batch([fence(),...statements.map(s=>native.get(s)??s),db.prepare('DELETE FROM rer_portability_guard')]);
82
+ return result.slice(1,-1);
83
+ }catch(error){if(!await owned())throw new PortabilityError('import_lease_expired',409);throw error;}
84
+ };
85
+ const prepare=sql=>{
86
+ const wrap=raw=>{const wrapped=new Proxy(raw,{get(target,key){
87
+ if(key==='bind')return (...values)=>wrap(target.bind(...values));
88
+ if(key==='run')return async()=> (await batch([target]))[0];
89
+ const value=target[key];return typeof value==='function'?value.bind(target):value;
90
+ }});native.set(wrapped,raw);return wrapped;};
91
+ return wrap(db.prepare(sql));
92
+ };
93
+ schedule();
94
+ try{return await operation({...context,db:{prepare,batch},importLease:{owned}});}
95
+ finally {
96
+ stopped=true;clearTimeout(timer);await pending;
97
+ await statement(db,'UPDATE rer_portability_import_leases SET holder=NULL WHERE job=? AND holder=?',id,holder).run();
98
+ }
99
+ }
@@ -1,6 +1,7 @@
1
1
  import { all, statement, state, guard, job, save, partQueries, parts, readPart, cleanCopies, quote, stream, insist, run } from './storage.mjs';
2
2
  import { stagePart } from './import-staging.mjs';
3
3
  import { normalizeStaged } from './import-safety.mjs';
4
+ import { ensureImportLeases, createImportLease, importLeaseStatus, importRunning, withImportLease } from './import-lease.mjs';
4
5
  import { refuseProviderData } from './provider-data.mjs';
5
6
  import { refuseDevData } from './dev-data.mjs';
6
7
  import { catalogue, validateRow, bindCell, writableColumns } from './catalogue.mjs';
@@ -39,20 +40,29 @@ export async function createImport(context,manifest,actor='direct',legacyRecover
39
40
  insist(canonicalJson(manifest.requiredConfiguration)===canonicalJson(config.requiredConfiguration),'configuration_inventory');
40
41
  for(const o of manifest.objects) insist(buckets[o.bucket] && config.buckets[o.bucket] && o.key.startsWith(config.buckets[o.bucket].prefix??''),'bucket_inventory');
41
42
  insist(canonicalJson(manifest.payloads.map(x=>x.name))===canonicalJson(Object.keys(config.extraPayloads).sort()),'payload_inventory');
43
+ await ensureImportLeases(db);
44
+ const previous=(await state(db)).import_job;
45
+ if(previous) {
46
+ if(!(await importLeaseStatus(db,await job(db,previous,'import'))).stale)throw await importRunning(db,previous);
47
+ await abortImport(context,previous,{staleOnly:true});
48
+ }
42
49
  const id=crypto.randomUUID(),copies=Object.fromEntries(targets.map((t,i)=>[t.name,'rer_portability_stage_'+id.replaceAll('-','')+'_'+i]));
43
50
  const row={id,kind:'import',actor,phase:'staging',completed:0,metadata:{manifest:original,...(skippedTables.length?{restoreManifest:manifest,skippedTables}:{}),copies,targets,triggers:cat.triggers,...(bridge?{bridge}:{}),...(legacyRecovery?{legacyRecovery}: {}),...(ledgers?{ledgers}:{})}};
44
51
  try {
45
52
  await db.batch([guard(db,'(SELECT import_job FROM rer_portability_state WHERE id=1) IS NULL'),
46
53
  statement(db,'UPDATE rer_portability_state SET import_job=? WHERE id=1',id),
47
54
  ...targets.flatMap(t=>createCopy(db,t,copies[t.name])),
48
- statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'import',actor,row.phase,canonicalJson(row.metadata)),db.prepare('DELETE FROM rer_portability_guard')]);
49
- }catch(e){if((await state(db)).import_job)throw new (await import('./storage.mjs')).PortabilityError('import_running',409);throw e;}
55
+ statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'import',actor,row.phase,canonicalJson(row.metadata)),createImportLease(db,id),db.prepare('DELETE FROM rer_portability_guard')]);
56
+ }catch(e){const running=(await state(db)).import_job;if(running)throw await importRunning(db,running);throw e;}
50
57
  return id;
51
58
  }
52
59
  export async function uploadPart(context,id,index,part) {
60
+ return withImportLease(context,id,leased=>uploadLeasedPart(leased,id,index,part));
61
+ }
62
+ async function uploadLeasedPart(context,id,index,part) {
53
63
  const {db,config}=context,row=await job(db,id,'import');await checkPart(part,index,config.partBytes);
54
64
  const previous=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();
55
- if(previous){insist(previous.sha256===part.sha256,'part_conflict',409);return importStatus(context,id);}
65
+ if(previous){insist(previous.sha256===part.sha256,'part_conflict',409);return importStatus(context,id,{includeLease:false});}
56
66
  insist(row.phase==='staging','import_phase',409);insist(index===row.completed && index<row.metadata.manifest.partCount,'part_order',409);
57
67
  const inserts=[],groups=new Map(),manifest=row.metadata.manifest,upgrade=config.acceptedSchemas[manifest.schema];
58
68
  for(const item of part.items) {
@@ -93,7 +103,7 @@ export async function uploadPart(context,id,index,part) {
93
103
  }
94
104
  try{await stagePart(context,row,part,[...inserts,...partQueries(id,part).slice(1)],stagedCounts);}
95
105
  catch(e){const duplicate=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();if(!duplicate || duplicate.sha256!==part.sha256)throw e;}
96
- return importStatus(context,id);
106
+ return importStatus(context,id,{includeLease:false});
97
107
  }
98
108
  const binaryKey=entry=>canonicalJson(entry.bucket===undefined?['payload',entry.name]:['object',entry.bucket,entry.key]);
99
109
  function binaryStream(context,id,entry,indexes) {
@@ -134,7 +144,11 @@ async function removeOwned(context,id) {
134
144
  for(const o of owned)await context.buckets[o.bucket].delete(o.key);
135
145
  await run(context.db,'DELETE FROM rer_portability_owned WHERE job=?',id);
136
146
  }
137
- export async function finalizeImport(context,id,{contentHash,archiveHash,laterJournal}={}) {
147
+ export async function finalizeImport(context,id,options={}) {
148
+ if((await job(context.db,id,'import')).phase==='complete')return importStatus(context,id);
149
+ return withImportLease(context,id,leased=>finalizeLeasedImport(leased,id,options));
150
+ }
151
+ async function finalizeLeasedImport(context,id,{contentHash,archiveHash,laterJournal}={}) {
138
152
  const {db,config}=context,row=await job(db,id,'import');
139
153
  if(row.phase==='complete')return importStatus(context,id);
140
154
  insist(['staging','applying'].includes(row.phase),'import_phase',409);
@@ -193,16 +207,27 @@ export async function finalizeImport(context,id,{contentHash,archiveHash,laterJo
193
207
  }catch(e){
194
208
  // D1 may have committed even when its acknowledgement failed.
195
209
  if((await job(db,id)).phase==='complete')return importStatus(context,id);
210
+ if(!await context.importLease.owned())throw new (await import('./storage.mjs')).PortabilityError('import_lease_expired',409);
196
211
  await removeOwned(context,id);row.phase='staging';await db.batch([save(db,row),db.prepare('UPDATE rer_portability_state SET applying=0 WHERE id=1')]);throw e;
197
212
  }
198
213
  return importStatus(context,id);
199
214
  }
200
- export async function importStatus(context,id) {
201
- const row=await job(context.db,id,'import');return {id,kind:'import',phase:row.phase,completed:row.completed,total:row.metadata.manifest.partCount,
215
+ // Upload receipts stay idempotent; the status door exposes the moving lease.
216
+ export async function importStatus(context,id,{includeLease=true}={}) {
217
+ const row=await job(context.db,id,'import');return {id,kind:'import',phase:row.phase,completed:row.completed,total:row.metadata.manifest.partCount,...(includeLease?await importLeaseStatus(context.db,row):{}),
202
218
  contentHash:row.metadata.contentHash??null,requiredConfiguration:row.phase==='complete'?row.metadata.manifest.requiredConfiguration:[],...(row.metadata.bridge?{bridged:row.metadata.bridge}:{}),...(row.metadata.skippedTables?{skippedTables:row.metadata.skippedTables}:{}),...(row.metadata.safetyChanges?{safetyChanges:row.metadata.safetyChanges}:{})};
203
219
  }
204
- export async function abortImport(context,id) {
220
+ export async function activeImport(context,actor='direct') {
221
+ const id=(await state(context.db)).import_job;
222
+ return id && (await job(context.db,id,'import')).actor===actor?importStatus(context,id):null;
223
+ }
224
+ export async function abortImport(context,id,options={}) {
205
225
  const row=await job(context.db,id,'import');insist(row.phase!=='complete','import_complete',409);
226
+ if(row.phase==='aborted')return importStatus(context,id);
227
+ return withImportLease(context,id,leased=>abortLeasedImport(leased,row),{abort:true,...options});
228
+ }
229
+ async function abortLeasedImport(context,row) {
230
+ const id=row.id;
206
231
  await removeOwned(context,id);await cleanCopies(context.db,row);row.phase='aborted';
207
232
  await context.db.batch([save(context.db,row),statement(context.db,'UPDATE rer_portability_state SET import_job=NULL,applying=0 WHERE id=1 AND import_job=?',id)]);
208
233
  return importStatus(context,id);
@@ -213,5 +238,5 @@ export async function abortImport(context,id) {
213
238
  export async function abortAbandonedImport(context) {
214
239
  const id=(await state(context.db)).import_job;
215
240
  if(!id)return null;
216
- await abortImport(context,id);return id;
241
+ await abortImport(context,id,{soleOperator:true});return id;
217
242
  }
@@ -34,7 +34,7 @@ export interface ArchivePolicy {
34
34
  }
35
35
  /** Actual changed rows grouped by table, column and safe replacement value; never original values. */
36
36
  export interface SafetyChange {table:string;column:string;value:Cell;rows:number}
37
- export interface Status {id:string;kind:'export'|'import';phase:'building'|'staging'|'applying'|'complete'|'aborted';completed:number;total:number|null;/** Export only: bytes of the parts built so far. */bytes?:number;contentHash:string|null;requiredConfiguration?:string[];bridged?:Bridge;/** Platform bookkeeping ignored in an older archive. */skippedTables?:string[];/** Durable summary of bounded import normalization. */safetyChanges?:SafetyChange[]}
37
+ export interface Status {id:string;kind:'export'|'import';phase:'building'|'staging'|'applying'|'complete'|'aborted';completed:number;total:number|null;/** Import only: inactivity deadline and whether it has passed. */abortableAt?:string|null;stale?:boolean;/** Export only: bytes of the parts built so far. */bytes?:number;contentHash:string|null;requiredConfiguration?:string[];bridged?:Bridge;/** Platform bookkeeping ignored in an older archive. */skippedTables?:string[];/** Durable summary of bounded import normalization. */safetyChanges?:SafetyChange[]}
38
38
  /** Later additive migrations an older archive was restored across. */
39
39
  export interface Bridge {fromSchema:string;toSchema:string;migrations:string[];tables:string[];columns:Record<string,string[]>}
40
40
  /** A runner's replay of the destination's applied migrations: the archive schema identity after a chosen set, and each migration's statements. */
@@ -42,7 +42,7 @@ export interface MigrationReplay {names:string[];statements(name:string):string[
42
42
  export interface PortabilityOptions {db:D1Database;buckets?:Record<string,R2Bucket>;policy:ArchivePolicy;migrations?:()=>Promise<MigrationReplay>}
43
43
  export interface Portability {
44
44
  exports: {create(actor?:string):Promise<string>;advance(id:string,options?:{expectedPart?:number;maxParts?:number}):Promise<Status>;status(id:string):Promise<Status>;download(id:string):Promise<ReadableStream<Uint8Array>>;abort(id:string):Promise<Status>};
45
- imports: {/** ledgers: the rows of each migration ledger table an old archive carries, as read from it; they place it in the destination's history and must equal its parts. */create(manifest:Manifest,actor?:string,options?:{ledgers?:Record<string,Row[]>}):Promise<string>;uploadPart(id:string,index:number,part:Part):Promise<Status>;finalize(id:string,options:{archiveHash:string;contentHash?:string;laterJournal?:LaterJournal}):Promise<Status>;status(id:string):Promise<Status>;abort(id:string):Promise<Status>;/** Aborts the import left staged by a crashed restore and returns its id; call only as the destination's sole operator. */abortAbandoned():Promise<string|null>};
45
+ imports: {active(actor?:string):Promise<Status|null>;/** ledgers: the rows of each migration ledger table an old archive carries, as read from it; they place it in the destination's history and must equal its parts. */create(manifest:Manifest,actor?:string,options?:{ledgers?:Record<string,Row[]>}):Promise<string>;uploadPart(id:string,index:number,part:Part):Promise<Status>;finalize(id:string,options:{archiveHash:string;contentHash?:string;laterJournal?:LaterJournal}):Promise<Status>;status(id:string):Promise<Status>;abort(id:string):Promise<Status>;/** Aborts the import left staged by a crashed restore and returns its id; call only as the destination's sole operator. */abortAbandoned():Promise<string|null>};
46
46
  status(id:string):Promise<Status>;actor(id:string):Promise<string>;
47
47
  erasureStatements(table:string,primaryKey:Record<string,Cell>):Promise<D1PreparedStatement[]>;
48
48
  redactionStatements():Promise<D1PreparedStatement[]>;
@@ -65,4 +65,4 @@ export const excludedTables:readonly string[];
65
65
  /** True when no archive under this policy carries `table`. */
66
66
  export function excludedTable(table:string,policy?:Pick<ArchivePolicy,'excludeTables'|'includeTables'>):boolean;
67
67
  export const MAX_PART_BYTES:number;
68
- export class PortabilityError extends Error {code:string;status:number;constructor(code:string,status?:number)}
68
+ export class PortabilityError extends Error {code:string;status:number;jobId?:string;abortableAt?:string|null;constructor(code:string,status?:number)}
@@ -1,8 +1,9 @@
1
+ import { ensureImportLeases } from './import-lease.mjs';
1
2
  import { policy, catalogue } from './catalogue.mjs';
2
3
  import { wireDatabase } from '../d1-transport/index.mjs';
3
4
  import { insist, job } from './storage.mjs';
4
5
  import { createExport, advanceExport, exportStatus, downloadExport, abortExport } from './export.mjs';
5
- import { createImport, uploadPart, finalizeImport, importStatus, abortImport, abortAbandonedImport } from './import.mjs';
6
+ import { createImport, uploadPart, finalizeImport, importStatus, activeImport, abortImport, abortAbandonedImport } from './import.mjs';
6
7
  import { erasureStatements, redactionStatements, invalidateTransfersStatements } from './journal.mjs';
7
8
  export { archiveLines } from './lines.mjs';
8
9
  export { excludedTables, excludedTable } from './catalogue.mjs';
@@ -14,10 +15,11 @@ export async function createPortability({db,buckets={},policy:input,migrations})
14
15
  db=wireDatabase(db);
15
16
  const config=policy(input);await catalogue(db,config);
16
17
  for(const name of Object.keys(config.buckets))insist(buckets[name] && typeof buckets[name].get==='function','missing_bucket:'+name);
18
+ await ensureImportLeases(db);
17
19
  const context={db,buckets,config,migrations};
18
20
  return {
19
21
  exports:{create:actor=>createExport(context,actor),advance:(id,options)=>advanceExport(context,id,options),status:id=>exportStatus(context,id),download:id=>downloadExport(context,id),abort:id=>abortExport(context,id)},
20
- imports:{create:(manifest,actor,options={})=>createImport(context,manifest,actor,undefined,options.ledgers),uploadPart:(id,index,part)=>uploadPart(context,id,index,part),finalize:(id,options)=>finalizeImport(context,id,options),status:id=>importStatus(context,id),abort:id=>abortImport(context,id),abortAbandoned:()=>abortAbandonedImport(context)},
22
+ imports:{active:actor=>activeImport(context,actor),create:(manifest,actor,options={})=>createImport(context,manifest,actor,undefined,options.ledgers),uploadPart:(id,index,part)=>uploadPart(context,id,index,part),finalize:(id,options)=>finalizeImport(context,id,options),status:id=>importStatus(context,id),abort:id=>abortImport(context,id),abortAbandoned:()=>abortAbandonedImport(context)},
21
23
  status:async id=>(await job(db,id)).kind==='export'?exportStatus(context,id):importStatus(context,id),
22
24
  actor:async id=>(await job(db,id)).actor,
23
25
  erasureStatements:(table,key)=>erasureStatements(db,table,key),redactionStatements:()=>redactionStatements(db),invalidateTransfersStatements:()=>invalidateTransfersStatements(db),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.68",
3
+ "version": "0.2.69",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",