@runeditrun/rer 0.2.44 → 0.2.46

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 (51) hide show
  1. package/README.md +87 -1
  2. package/cli/README.md +8 -0
  3. package/cli/companion/api.mjs +27 -0
  4. package/cli/companion/auth.mjs +34 -0
  5. package/cli/companion/command.mjs +73 -0
  6. package/cli/companion/commands.mjs +22 -0
  7. package/cli/companion/event-payload.mjs +7 -0
  8. package/cli/companion/grants.mjs +30 -0
  9. package/cli/companion/no-turn-evidence.mjs +49 -0
  10. package/cli/companion/providers/claude.mjs +31 -0
  11. package/cli/companion/providers/codex.mjs +55 -0
  12. package/cli/companion/providers/jsonl-rpc.mjs +95 -0
  13. package/cli/companion/providers/sandbox.mjs +58 -0
  14. package/cli/companion/providers/secrets.mjs +36 -0
  15. package/cli/companion/recovery.mjs +67 -0
  16. package/cli/companion/runner.mjs +121 -0
  17. package/cli/companion/setup.mjs +71 -0
  18. package/cli/companion/state.mjs +10 -0
  19. package/cli/companion/workspace.mjs +101 -0
  20. package/cli/deploy.mjs +1 -1
  21. package/cli/local.mjs +6 -2
  22. package/cli/main.mjs +1 -0
  23. package/cli/port.mjs +10 -11
  24. package/cli/server-limit-file.mjs +21 -0
  25. package/cli/server-registry.mjs +13 -6
  26. package/cli/server-resources.mjs +13 -2
  27. package/global/CHANGELOG.md +14 -0
  28. package/global/README.md +87 -1
  29. package/global/launch/capacity.mjs +9 -4
  30. package/global/launch/facts.mjs +11 -19
  31. package/global/main.mjs +3 -0
  32. package/modules/demo-host/README.md +3 -0
  33. package/modules/demo-host/build.mjs +3 -1
  34. package/modules/demo-host/native/config.mjs +2 -0
  35. package/modules/demo-host/native/prepare.mjs +3 -1
  36. package/modules/demo-host/native/visitor.mjs +20 -7
  37. package/modules/demo-host/native/warm.mjs +49 -0
  38. package/modules/demo-host/native/worker.mjs +4 -2
  39. package/modules/demo-host/pages.mjs +6 -1
  40. package/modules/demo-host/worker.mjs +3 -2
  41. package/modules/deploy-kit/README.md +15 -1
  42. package/modules/deploy-kit/deploy.mjs +5 -3
  43. package/modules/deploy-kit/diagnostics.mjs +8 -0
  44. package/modules/deploy-kit/migration-ledger.mjs +28 -8
  45. package/modules/deploy-kit/migration-retry.mjs +32 -16
  46. package/modules/deploy-kit/migrations.mjs +10 -4
  47. package/modules/deploy-kit/owner-cli.mjs +1 -0
  48. package/modules/deploy-kit/owner.mjs +2 -1
  49. package/onboarding/port-probe.mjs +23 -1
  50. package/onboarding/process-diagnostics.mjs +3 -2
  51. package/package.json +1 -1
@@ -8,6 +8,8 @@ import { literal } from './migration-ledger.mjs';
8
8
  // end of the file proves it: its effect absent now means no attempt committed, and
9
9
  // an earlier attempt that commits late makes the re-run fail whole instead of
10
10
  // applying twice. Every such effect must be absent; any present one is refused.
11
+ // A name the file drops or renames away existed before it ran, so an object it
12
+ // makes again under that name (a rebuilt table, its indexes) proves nothing.
11
13
 
12
14
  const word = token => token?.kind === 'word' ? token.value.toUpperCase() : '';
13
15
  const lower = token => token?.kind === 'word' || token?.kind === 'identifier' ? token.value.toLowerCase() : undefined;
@@ -43,11 +45,13 @@ function create(statement) {
43
45
  /** The effects of a migration whose absence proves it never applied. */
44
46
  export function retryGuards(sql) {
45
47
  let guards = [];
48
+ const before = new Set();
49
+ const guard = item => { if (!before.has(item.object) && !before.has(item.table) && !before.has(`${item.table}.${item.column}`)) guards.push(item); };
46
50
  for (const statement of statements(sql)) {
47
51
  const first = word(statement[0]);
48
52
  if (first === 'CREATE') {
49
- const guard = create(statement);
50
- if (guard) guards.push(guard);
53
+ const created = create(statement);
54
+ if (created) guard(created);
51
55
  } else if (first === 'ALTER' && word(statement[1]) === 'TABLE') {
52
56
  const { name:table, next } = named(statement, 2);
53
57
  let index = next;
@@ -55,21 +59,26 @@ export function retryGuards(sql) {
55
59
  if (word(statement[index]) === 'COLUMN') index++;
56
60
  const column = lower(statement[index]);
57
61
  if (!table) guards = [];
58
- else if (action === 'ADD' && column) guards.push({ table, column });
62
+ else if (action === 'ADD' && column) guard({ table, column });
59
63
  else if (action === 'RENAME' && word(statement[next + 1]) === 'TO') {
60
64
  const to = named(statement, next + 2).name;
61
- guards = guards.filter(guard => guard.table !== table);
62
- if (to) guards.push({ type:'table', object:to, table:to });
65
+ guards = guards.filter(item => item.table !== table);
66
+ before.add(table);
67
+ if (to) guard({ type:'table', object:to, table:to });
63
68
  } else if (action === 'RENAME' && column && word(statement[index + 1]) === 'TO' && lower(statement[index + 2])) {
64
- guards = guards.filter(guard => !(guard.table === table && guard.column === column));
65
- guards.push({ table, column:lower(statement[index + 2]) });
66
- } else if (action === 'DROP' && column) guards = guards.filter(guard => !(guard.table === table && guard.column === column));
67
- else guards = [];
69
+ guards = guards.filter(item => !(item.table === table && item.column === column));
70
+ before.add(`${table}.${column}`);
71
+ guard({ table, column:lower(statement[index + 2]) });
72
+ } else if (action === 'DROP' && column) {
73
+ guards = guards.filter(item => !(item.table === table && item.column === column));
74
+ before.add(`${table}.${column}`);
75
+ } else guards = [];
68
76
  } else if (first === 'DROP') {
69
77
  const type = word(statement[1]);
70
78
  const { name } = named(statement, word(statement[2]) === 'IF' ? 4 : 2);
71
79
  // A later drop removes the effect it would prove; an unreadable one proves nothing.
72
- guards = name ? guards.filter(guard => guard.object !== name && !(['TABLE', 'VIEW'].includes(type) && guard.table === name)) : [];
80
+ guards = name ? guards.filter(item => item.object !== name && !(['TABLE', 'VIEW'].includes(type) && item.table === name)) : [];
81
+ if (name) before.add(name);
73
82
  }
74
83
  }
75
84
  return guards;
@@ -78,14 +87,21 @@ export function retryGuards(sql) {
78
87
  const label = guard => guard.column === undefined ? `${guard.type} ${guard.object}` : `column ${guard.table}.${guard.column}`;
79
88
 
80
89
  /** Refuse unless the claimed migration's effects are provably absent: it then
81
- * may run again. `read(sql)` returns one SELECT's rows. */
90
+ * may run again. `read(sql)` returns one SELECT's rows. D1 accepts at most five
91
+ * terms in a compound SELECT, so objects and columns are each one plain query. */
82
92
  export async function assertRetryable(read, migration) {
83
93
  const guards = retryGuards(migration.sql), present = [];
84
- for (let start = 0; start < guards.length; start += 50) {
85
- const part = guards.slice(start, start + 50).map((guard, index) => guard.column === undefined
86
- ? `SELECT ${index} AS guard FROM sqlite_master WHERE type=${literal(guard.type)} AND lower(name)=${literal(guard.object)}`
87
- : `SELECT ${index} AS guard FROM pragma_table_info(${literal(guard.table)}) WHERE lower(name)=${literal(guard.column)}`);
88
- for (const row of await read(part.join(' UNION ALL '))) present.push(label(guards[start + row.guard]));
94
+ const names = list => [...new Set(list)].map(literal).join(',');
95
+ const objects = guards.filter(guard => guard.column === undefined), columns = guards.filter(guard => guard.column !== undefined);
96
+ for (let start = 0; start < objects.length; start += 100) {
97
+ const part = objects.slice(start, start + 100);
98
+ const found = new Set((await read(`SELECT type, lower(name) AS name FROM sqlite_master WHERE lower(name) IN (${names(part.map(guard => guard.object))})`)).map(row => `${row.type} ${row.name}`));
99
+ for (const guard of part) if (found.has(label(guard))) present.push(label(guard));
100
+ }
101
+ for (let start = 0; start < columns.length; start += 100) {
102
+ const part = columns.slice(start, start + 100);
103
+ const found = new Set((await read(`SELECT lower(m.name) AS tbl, lower(p.name) AS col FROM sqlite_master AS m, pragma_table_info(m.name) AS p WHERE m.type='table' AND lower(m.name) IN (${names(part.map(guard => guard.table))})`)).map(row => `column ${row.tbl}.${row.col}`));
104
+ for (const guard of part) if (found.has(label(guard))) present.push(label(guard));
89
105
  }
90
106
  if (!guards.length || present.length) throw Object.assign(new Error('uncertain-migration-claim'), { migrationClaim:{ name:migration.name, present:[...new Set(present)] } });
91
107
  }
@@ -53,9 +53,10 @@ export async function query(api, id, sql) {
53
53
  assert(Array.isArray(rows) && rows.length > 0 && rows.every(x => x.success === true && Array.isArray(x.results)), 'd1-query-failed');
54
54
  return rows;
55
55
  }
56
- export async function migrate(api, id, migrations, { apply = true, upgrade = apply } = {}) {
56
+ /** `locked`: the caller holds the install's deploy lock (owner.mjs withStateLock). */
57
+ export async function migrate(api, id, migrations, { apply = true, upgrade = apply, locked = false } = {}) {
57
58
  validateMigrations(migrations);
58
- const { applied, transition, exists, claimed } = await readLedger(api, id, migrations, query);
59
+ const { applied, transition, exists, claimed, claimedFrom } = await readLedger(api, id, migrations, query, { rewritten:locked });
59
60
  const pending = migrations.slice(applied.length);
60
61
  assert(apply || pending.length === 0,'pending-migrations');
61
62
  if (transition && upgrade) {
@@ -73,12 +74,17 @@ export async function migrate(api, id, migrations, { apply = true, upgrade = app
73
74
  // A second terminator after trailing comments creates an empty D1 statement.
74
75
  // Keep the newline even when terminated: a final line comment must end.
75
76
  const separator = last?.kind === 'symbol' && last.value === ';' ? '' : '; ';
76
- await api.importSql(id,`${migration.sql}\n${separator}UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)};`)
77
+ // A claim on bytes a reviewed rewrite replaced moves to these bytes only when this
78
+ // import commits; an earlier attempt that commits first leaves it applied on those.
79
+ const done = claimedFrom && index === 0
80
+ ? `UPDATE ${ledger} SET sha256=${literal(migration.sha256)}, state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(claimedFrom)} AND state='applying'`
81
+ : `UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)}`;
82
+ await api.importSql(id,`${migration.sql}\n${separator}${done};`)
77
83
  .catch(error => { error.migration ??= migration.name; throw error; });
78
84
  }
79
85
  if (pending.length) {
80
86
  const final = (await query(api,id,`SELECT name, sha256, state FROM ${ledger} ORDER BY id`))[0].results;
81
87
  assert(final.length === migrations.length && final.every((x,i) => x.state === 'applied' && recorded(x, migrations[i])), 'migration-readback-mismatch');
82
88
  }
83
- return { applied:pending.map(x => x.name), history:migrations.map(({name,sha256}) => ({name,sha256})) };
89
+ return { applied:pending.map(x => x.name), retried:claimed ? [pending[0].name] : [], history:migrations.map(({name,sha256}) => ({name,sha256})) };
84
90
  }
@@ -116,6 +116,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
116
116
  api.annotateFailure(error,error.message.startsWith('missing-secret-') ? 'secrets' : undefined);
117
117
  throw error;
118
118
  });
119
+ for (const {binding,retried = []} of receipt.migrations) for (const name of retried) notice(`Migrations: retried ${name}: the earlier attempt was rolled back (D1 ${plan.d1.find(item => item.binding === binding).name}).`);
119
120
  // Optional secrets the Worker does not hold: never supplied, or removed with --unset.
120
121
  const optionalUnset = declaredSecrets.filter(item => item?.optional && (unsetSecrets.includes(item.binding) || retiredSecrets.includes(item.binding))).map(item => item.binding);
121
122
  const result = {ok:true,changed:receipt.changed,worker:target.worker,origin:receipt.origin,versionId:receipt.provider.versionId,created,sentSecrets,...(optionalUnset.length ? {optionalUnset} : {}),retiredSecrets,...(state.ownerClaim ? {ownerClaim:state.ownerClaim} : {}),state:statePath};
@@ -415,7 +415,8 @@ async function deployLocked({api, target, plan, artifact, statePath, env, fetch,
415
415
  return result;
416
416
  }};
417
417
  const receipt = await deploy({api:observed, plan, artifact, artifactSha256:hash(artifact), previousReceipt:state.receipt ?? undefined,
418
- createResources:false, uploadWithoutReceipt:true, env, fetch, healthOptions, readiness, declaredSecrets,
418
+ // Migrations run inside this run's state lock: no other deploy of this install is running.
419
+ createResources:false, uploadWithoutReceipt:true, locked:true, env, fetch, healthOptions, readiness, declaredSecrets,
419
420
  onReceipt: async candidate => { if (candidate.changed || !state.receipt) { state.receipt = candidate; delete state.pendingUpload; delete state.pendingSecret; await save(); } },
420
421
  // Keep each first-health attempt beside the identity it belongs to.
421
422
  onHealth: async candidate => { if (candidate.changed) { state.receipt = candidate; await save(); } }});
@@ -1,6 +1,6 @@
1
1
  import { createServer } from 'node:net';
2
2
  import { setTimeout as delay } from 'node:timers/promises';
3
- import { inspectionContext, portListener, uncheckedInspection } from './process-diagnostics.mjs';
3
+ import { inspectionContext, portListener, searchContext, uncheckedInspection } from './process-diagnostics.mjs';
4
4
 
5
5
  async function bind(host, port) {
6
6
  const socket = createServer();
@@ -38,3 +38,25 @@ export async function probePort(host, port, { context = inspectionContext({ port
38
38
  await delay(Math.min(100, remaining));
39
39
  } while (true);
40
40
  }
41
+
42
+ // How many ports a free-port search tries, counting from its first one. The
43
+ // search is bounded by this count and by nothing else: a busy computer makes
44
+ // each bind slower, and the search waits for every bind's answer.
45
+ export const searchCandidates = 1000;
46
+
47
+ // The first port from `from` that probePort finds available and `claim` secures
48
+ // (claim may be omitted), among the next `searchCandidates` ports. A port in
49
+ // `skip` counts as tried and in use. Each candidate's ownership check has its
50
+ // own load-scaled budget, so no candidate can spend another's. Without a free
51
+ // port, the result says what was tried: { range: [first, last], tried }.
52
+ export async function searchPort({ host, from, hosts, skip = () => false, claim = async () => ({}), verb }) {
53
+ const last = Math.min(65535, from + searchCandidates - 1);
54
+ for (let port = from; port <= last; port++) {
55
+ if (skip(port)) continue;
56
+ const context = searchContext({ port, verb });
57
+ if (!(await probePort(host, port, { context, hosts, anyAddress: true, searching: true })).available) continue;
58
+ const claimed = await claim(port);
59
+ if (claimed) return { port, ...claimed };
60
+ }
61
+ return { range: [from, last], tried: last - from + 1 };
62
+ }
@@ -13,8 +13,9 @@ const exec=promisify(execFile);
13
13
  const lsof=process.platform==='darwin'?'/usr/sbin/lsof':'lsof';
14
14
  // One monotonic budget covers every command in an ownership decision.
15
15
  export const inspectionContext=({budgetMs=15000,...details}={})=>({deadline:performance.now()+budgetMs,budgetMs,...details});
16
- // A free-port search on a busy computer gets longer, still bounded: the budget
17
- // grows with the 1-minute load per CPU, up to four times.
16
+ // The ownership check of one free-port candidate on a busy computer gets longer,
17
+ // still bounded: the budget grows with the 1-minute load per CPU, up to four
18
+ // times. The search itself is bounded by its candidate count, not by this.
18
19
  export const searchContext=(details={})=>inspectionContext({budgetMs:Math.round(15000*Math.min(4,Math.max(1,loadavg()[0]/availableParallelism()))),...details});
19
20
  export const uncheckedInspection=error=>approvedFailure(error)?.ownershipUnchecked===true;
20
21
  const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,noMatch=false,positiveResult}={})=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.44",
3
+ "version": "0.2.46",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",