@runeditrun/rer 0.2.68 → 0.2.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/cli/README.md +2 -0
  2. package/cli/deploy-command.mjs +5 -1
  3. package/cli/deploy-product.mjs +30 -0
  4. package/cli/deploy.mjs +3 -1
  5. package/cli/inspect.mjs +29 -1
  6. package/global/CHANGELOG.md +16 -0
  7. package/modules/deploy-kit/README.md +2 -0
  8. package/modules/deploy-kit/owner.mjs +1 -1
  9. package/modules/operator-auth/README.md +31 -10
  10. package/modules/operator-auth/SCHEDULED-POLICY.md +130 -0
  11. package/modules/operator-auth/accounts/admin.mjs +2 -10
  12. package/modules/operator-auth/accounts/schema/last-signin.sql +11 -0
  13. package/modules/operator-auth/accounts/schema/operator-auth.sql +25 -1
  14. package/modules/operator-auth/accounts/schema/scheduled-policy-hardening.sql +8 -0
  15. package/modules/operator-auth/accounts/schema/scheduled-policy.sql +18 -0
  16. package/modules/operator-auth/accounts/status.mjs +18 -0
  17. package/modules/operator-auth/accounts/store.mjs +3 -0
  18. package/modules/operator-auth/branding.mjs +5 -2
  19. package/modules/operator-auth/conformance/README.md +7 -3
  20. package/modules/operator-auth/conformance/index.d.mts +2 -2
  21. package/modules/operator-auth/conformance/index.mjs +6 -4
  22. package/modules/operator-auth/conformance/presentation.mjs +8 -3
  23. package/modules/operator-auth/dev/index.mjs +2 -1
  24. package/modules/operator-auth/dev-gate.mjs +5 -3
  25. package/modules/operator-auth/index.d.mts +6 -2
  26. package/modules/operator-auth/page.mjs +5 -3
  27. package/modules/operator-auth/routes.mjs +6 -1
  28. package/modules/operator-auth/scheduled/definition.mjs +37 -0
  29. package/modules/operator-auth/scheduled/fence.mjs +27 -0
  30. package/modules/operator-auth/scheduled/history.mjs +30 -0
  31. package/modules/operator-auth/scheduled/index.d.mts +21 -0
  32. package/modules/operator-auth/scheduled/index.mjs +83 -0
  33. package/modules/operator-auth/scheduled/runner.mjs +65 -0
  34. package/modules/operator-auth/service.mjs +2 -0
  35. package/modules/operator-auth/signin/index.mjs +2 -2
  36. package/modules/operator-auth/signin/one-click.mjs +8 -3
  37. package/modules/portability/README.md +75 -7
  38. package/modules/portability/backfills.mjs +89 -0
  39. package/modules/portability/bridge.mjs +18 -12
  40. package/modules/portability/export.mjs +5 -1
  41. package/modules/portability/http.mjs +2 -1
  42. package/modules/portability/import-lease.mjs +99 -0
  43. package/modules/portability/import.mjs +45 -16
  44. package/modules/portability/index.d.mts +4 -4
  45. package/modules/portability/index.mjs +4 -2
  46. package/modules/portability/migration-statements.mjs +96 -9
  47. package/modules/portability/policy-data.mjs +10 -0
  48. package/onboarding/adapter.mjs +5 -2
  49. package/onboarding/process-diagnostics.mjs +7 -2
  50. package/package.json +1 -1
@@ -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),
@@ -30,20 +30,107 @@ export function changesData(sql,reached,empty=[]) {
30
30
  return names(list,new Set(reached.map(folded).filter(name=>!skipped.has(name))));
31
31
  }
32
32
  // The restorable tables and every table whose writes reach one through a
33
- // trigger any of `statements` creates: a write to such a table during a later
34
- // migration changes restorable rows the archive lacks. A trigger counts when
35
- // its statement names a reached table anywhere; one this parser cannot bound
36
- // adds every name it holds.
33
+ // trigger any of `statements` creates, or through a foreign key action (ON
34
+ // DELETE/UPDATE CASCADE or SET) declared by a reached table: a write to such a
35
+ // table during a later migration changes restorable rows the archive lacks. A
36
+ // trigger counts when its statement names a reached table anywhere; one this
37
+ // parser cannot bound adds every name it holds.
37
38
  export function reachingTables(statements,restorable) {
38
- const reached=new Set(restorable.map(folded));
39
- const triggers=statements.map(sql=>tokens(sql)).filter(list=>kindOf(list.map(t=>t.quoted?undefined:folded(t.text)))==='create trigger')
40
- .map(list=>({list,on:createdTrigger(list)?.on}));
39
+ const reached=new Set(restorable.map(folded)),parsed=statements.map(sql=>tokens(sql)).map(list=>({list,words:list.map(t=>t.quoted?undefined:folded(t.text))}));
40
+ const triggers=parsed.filter(({words})=>kindOf(words)==='create trigger').map(({list})=>({list,on:createdTrigger(list)?.on}));
41
+ const actions=parsed.filter(({words})=>['create table','alter table'].includes(kindOf(words)) && words.some((w,i)=>w==='cascade' || (w==='set' && ['delete','update'].includes(words[i-1])))).map(({list})=>referencing(list));
41
42
  for(let grew=true;grew;) {
42
43
  grew=false;
43
- for(const {list,on} of triggers)if(names(list,reached))for(const name of on===undefined?identifiers(list):[on])if(!reached.has(name)){reached.add(name);grew=true;}
44
+ const add=name=>{if(!reached.has(name)){reached.add(name);grew=true;}};
45
+ for(const {list,on} of triggers)if(names(list,reached))(on===undefined?identifiers(list):[on]).forEach(add);
46
+ for(const {table,parents} of actions)if(table===undefined?parents.some(name=>reached.has(name)):reached.has(table))parents.forEach(add);
44
47
  }
45
48
  return [...reached];
46
49
  }
50
+ // The table a CREATE TABLE or ALTER TABLE statement declares and the tables its
51
+ // REFERENCES clauses name (table undefined when it cannot be read).
52
+ function referencing(list) {
53
+ const parts=atoms(list),at=word(parts[2],'if') && word(parts[3],'not') && word(parts[4],'exists')?5:2;
54
+ return {table:tableName(parts.slice(at,at+1)),parents:parts.flatMap((a,i)=>word(a,'references') && parts[i+1]?[tableName([parts[i+1]])].filter(Boolean):[])};
55
+ }
56
+ // The last part of a [schema.]table name read from one to three atoms.
57
+ function tableName(name) {
58
+ const last=name.at(-1);
59
+ if(!last || name.length>3 || name.some(a=>!a.quoted && !/^[A-Za-z0-9_$.\u0080-\uffff]+$/.test(a.text)))return;
60
+ return folded(last.quoted?identifier(last):last.text.split('.').at(-1))||undefined;
61
+ }
62
+ // The one table a bounded INSERT, REPLACE, UPDATE or DELETE statement writes
63
+ // directly, after an optional WITH clause, with what a replay must know of it:
64
+ // {verb, target, conflict (an OR clause's resolution, 'replace' for REPLACE),
65
+ // columns (an explicit insert column list), upsert (ON CONFLICT ... DO UPDATE),
66
+ // star (a * or t.* result column), names (every identifier it holds)}. Undefined
67
+ // for any other statement or one this parser cannot bound. Triggers on the
68
+ // target and foreign key actions are the only further writes such a statement
69
+ // makes; reachingTables accounts for both.
70
+ export function dataStatement(sql) {
71
+ const parts=atoms(tokens(sql));
72
+ if(parts.some(semicolon))return;
73
+ let i=0;
74
+ if(word(parts[0],'with')) {
75
+ i=word(parts[1],'recursive')?2:1;
76
+ for(;;) {
77
+ i++;// the table name
78
+ if(parts[i]?.text==='(')i=closing(parts,i);
79
+ if(!word(parts[i],'as'))return;
80
+ i++;
81
+ if(word(parts[i],'not'))i++;
82
+ if(word(parts[i],'materialized'))i++;
83
+ if(parts[i]?.text!=='(')return;
84
+ i=closing(parts,i);
85
+ if(parts[i]?.text!==',')break;
86
+ i++;
87
+ }
88
+ }
89
+ const verb=['insert','replace','update','delete'].find(v=>word(parts[i],v));
90
+ if(!verb)return;
91
+ let conflict=verb==='replace'?'replace':undefined;
92
+ i++;
93
+ if(['insert','update'].includes(verb) && word(parts[i],'or')){conflict=parts[i+1] && !parts[i+1].quoted?folded(parts[i+1].text):undefined;if(!conflict)return;i+=2;}
94
+ if(verb!=='update'){if(!word(parts[i],verb==='delete'?'from':'into'))return;i++;}
95
+ const name=[];
96
+ const joined=a=>!name.length || (!a.quoted && a.text.startsWith('.')) || (!name.at(-1).quoted && name.at(-1).text.endsWith('.'));
97
+ for(;i<parts.length && !['(',',',')'].includes(parts[i].text) && joined(parts[i]);i++)name.push(parts[i]);
98
+ const target=tableName(name);
99
+ if(!target)return;
100
+ if(word(parts[i],'as'))i+=2;
101
+ const star=parts.some((a,k)=>!a.quoted && /(^|\.)\*$/.test(a.text) && !(parts[k-1]?.text==='(' && parts[k+1]?.text===')'));
102
+ const upsert=parts.some((a,k)=>word(a,'do') && word(parts[k+1],'update'));
103
+ return {verb,target,conflict,columns:verb==='insert' || verb==='replace'?parts[i]?.text==='(':undefined,upsert,star,names:identifiers(parts)};
104
+ }
105
+ // The index after the parenthesized group opening at `start`, or the end when unbalanced.
106
+ function closing(parts,start) {
107
+ let depth=0;
108
+ for(let i=start;i<parts.length;i++){if(parts[i].quoted)continue;if(parts[i].text==='(')depth++;else if(parts[i].text===')' && !--depth)return i+1;}
109
+ return parts.length;
110
+ }
111
+ // The names in the bodies of triggers on `table` that `statements` create, and
112
+ // every name in a trigger this parser cannot bound that names the table.
113
+ export function triggerBodies(statements,table) {
114
+ const result=[];
115
+ for(const list of statements.map(sql=>tokens(sql))) {
116
+ if(kindOf(list.map(t=>t.quoted?undefined:folded(t.text)))!=='create trigger')continue;
117
+ const created=createdTrigger(list);
118
+ if(!created){if(identifiers(list).includes(table))result.push(...identifiers(list));}
119
+ else if(created.on===table)result.push(...identifiers(atoms(list).slice(created.begin+1)));
120
+ }
121
+ return result;
122
+ }
123
+ // What a statement that changes only the schema does: 'create table' (not AS
124
+ // SELECT), 'create index', 'drop index', 'create view', 'drop view', 'add
125
+ // column' or 'trigger' (a bounded CREATE or DROP TRIGGER); undefined otherwise.
126
+ export function schemaChange(sql) {
127
+ const list=tokens(sql),words=list.map(t=>t.quoted?undefined:folded(t.text)),kind=kindOf(words);
128
+ if(kind==='create table')return topLevelWords(list,words).includes('as')?undefined:kind;
129
+ if(['create index','drop index','create view','drop view'].includes(kind))return kind;
130
+ if(kind==='alter table' && words.find((w,i)=>i>2 && ['add','rename','drop'].includes(w))==='add')return 'add column';
131
+ if(kind==='create trigger' && createdTrigger(list) || kind==='drop trigger' && droppedTrigger(list))return 'trigger';
132
+ }
133
+ export const statementNames=sql=>identifiers(tokens(sql));
47
134
  const kindOf=words=>words.slice(0,4).filter(w=>!['temp','temporary','unique'].includes(w)).slice(0,2).join(' ');
48
135
  const identifiers=list=>list.flatMap(t=>t.quoted?(t.text.startsWith("'")?[]:[folded(identifier(t))]):folded(t.text).split(/[^a-z0-9_$\u0080-\uffff]+/).filter(Boolean));
49
136
  const names=(list,reached)=>identifiers(list).some(name=>reached.has(name));
@@ -79,7 +166,7 @@ function createdTrigger(list) {
79
166
  let start=true,statements=0;
80
167
  for(let i=begin+1;i<parts.length;i++) {
81
168
  const a=parts[i];
82
- if(start && word(a,'end'))return i===parts.length-1 && statements && !depth?{on}:undefined;
169
+ if(start && word(a,'end'))return i===parts.length-1 && statements && !depth?{on,begin}:undefined;
83
170
  if(semicolon(a)){if(start || depth)return;start=true;statements++;continue;}
84
171
  if(!a.quoted){if(a.text==='(')depth++;else if(a.text===')' && --depth<0)return;}
85
172
  start=false;
@@ -0,0 +1,10 @@
1
+ import { all, quote, insist } from './storage.mjs';
2
+
3
+ // Keep approval provenance and run references, but never transfer a live delegation.
4
+ export async function revokePolicyApprovals(db,copies) {
5
+ if(copies.oa_policy_approvals)await db.prepare(`UPDATE ${quote(copies.oa_policy_approvals)} SET revoked_at=MAX(approved_at,CAST(strftime('%s','now') AS INTEGER)*1000) WHERE revoked_at IS NULL`).run();
6
+ }
7
+
8
+ export async function refusePolicyApprovals(db,copies) {
9
+ if(copies.oa_policy_approvals)insist(!(await all(db,`SELECT 1 FROM ${quote(copies.oa_policy_approvals)} WHERE revoked_at IS NULL LIMIT 1`)).length,'live_policy_approval',409);
10
+ }
@@ -238,8 +238,11 @@ async function start(context, hooks) {
238
238
  // its /health would pass. Claim and sign in only against the process this
239
239
  // setup started.
240
240
  let ownership;
241
- try { ownership=await listenerOwnership(child.pid,expectedPort,context.runtimePort?'127.0.0.1':new URL(url).hostname,inspectionContext({port:expectedPort,verb:context.verb??'setup'})); }
242
- catch(error){throw installationFailure('command_failed',{step:'start',diagnostic:error});}
241
+ try { ownership=await listenerOwnership(child.pid,expectedPort,context.runtimePort?'127.0.0.1':new URL(url).hostname,inspectionContext({port:expectedPort,verb:context.verb??'setup',signal})); }
242
+ catch(error){
243
+ if (signal?.aborted) throw error;
244
+ throw installationFailure('command_failed',{step:'start',diagnostic:error});
245
+ }
243
246
  if (ownership.status !== 'owned') {
244
247
  const message = (store.redact??(value=>value))(ownership.status==='other'
245
248
  ? `Another process answered on port ${expectedPort}; the application did not bind it. Listener PID ${ownership.pid}. Nothing was sent. Stop that process or choose another port, then run setup again.`
@@ -25,8 +25,10 @@ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,no
25
25
  const remaining=Math.floor(context.deadline-performance.now());
26
26
  if(remaining<=0)break;
27
27
  const timeout=attempt===0?Math.min(3000,remaining):remaining;
28
- try{return (await exec(command,args,{timeout,maxBuffer})).stdout;}
28
+ try{return (await exec(command,args,{timeout,maxBuffer,signal:context.signal})).stdout;}
29
29
  catch(error){
30
+ // A cancelled caller stops inspecting: the abort is never an ownership verdict.
31
+ if(context.signal?.aborted)throw context.signal.reason;
30
32
  // A selected state file positively observed before a timeout still has a
31
33
  // holder. Partial output can never establish the absence of a holder.
32
34
  if(typeof error.stdout==='string'&&positiveResult?.(error.stdout))return error.stdout;
@@ -166,6 +168,9 @@ export async function listenerPorts(pid,context=inspectionContext()){
166
168
  export async function listenerOwnership(pid,port,host,context=inspectionContext()){
167
169
  context.port=port;
168
170
  do {
171
+ // A cancelled launch has stopped its app, so its listener never appears:
172
+ // end now rather than polling ps and lsof until the inspection budget.
173
+ context.signal?.throwIfAborted();
169
174
  const owned=await descendants(pid,context);
170
175
  const ownRows=await listeners([...owned],context);
171
176
  const own=ownRows.find(row=>owned.has(row.pid)&&row.sockets.some(socket=>serves(socket,port,host)===true));
@@ -180,7 +185,7 @@ export async function listenerOwnership(pid,port,host,context=inspectionContext(
180
185
  if(row)return {status:owned.has(row.pid)?'owned':'other',pid:row.pid};
181
186
  const remaining=context.deadline-performance.now();
182
187
  if(remaining<=0)return {status:'none'};
183
- await delay(Math.min(100,remaining));
188
+ await delay(Math.min(100,remaining),undefined,{signal:context.signal});
184
189
  }while(performance.now()<context.deadline);
185
190
  return {status:'none'};
186
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.68",
3
+ "version": "0.2.70",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",