@runeditrun/rer 0.2.25 → 0.2.27

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 (38) hide show
  1. package/cli/README.md +12 -5
  2. package/cli/contract.mjs +1 -1
  3. package/cli/demo-host.mjs +31 -5
  4. package/cli/hosted-mail.mjs +23 -0
  5. package/cli/hosted-output.mjs +6 -2
  6. package/cli/lifecycle.mjs +7 -4
  7. package/cli/local.mjs +25 -14
  8. package/cli/managed-server.mjs +1 -1
  9. package/cli/release.mjs +4 -2
  10. package/global/CHANGELOG.md +27 -0
  11. package/modules/container/README.md +9 -4
  12. package/modules/container/demo-egress.mjs +15 -2
  13. package/modules/container/image-variables.mjs +1 -1
  14. package/modules/container/worker-engine.mjs +2 -2
  15. package/modules/demo-host/README.md +9 -2
  16. package/modules/demo-host/certificate.mjs +56 -0
  17. package/modules/demo-host/measurement.mjs +19 -0
  18. package/modules/demo-host/operator.mjs +11 -6
  19. package/modules/demo-host/plan.mjs +1 -1
  20. package/modules/demo-host/worker.mjs +2 -0
  21. package/modules/mail/README.md +18 -2
  22. package/modules/mail/src/adapters/rer/contract.mjs +5 -2
  23. package/modules/mail/src/adapters/rer/list-headers.mjs +44 -0
  24. package/modules/mail/src/core/validation.mjs +5 -2
  25. package/modules/mail/src/sender.mjs +1 -1
  26. package/modules/mail/src/unsubscribe/index.mjs +5 -5
  27. package/modules/operator-auth/policy.d.mts +3 -0
  28. package/modules/release/README.md +25 -2
  29. package/modules/release/produce.mjs +8 -4
  30. package/modules/release/public-base.mjs +29 -0
  31. package/onboarding/local/sqlite-custody.mjs +34 -0
  32. package/onboarding/process-cleanup.mjs +9 -10
  33. package/onboarding/process-diagnostics.mjs +5 -4
  34. package/onboarding/process-scope.mjs +5 -3
  35. package/onboarding/process-table.mjs +22 -10
  36. package/package.json +1 -1
  37. package/scripts/publish-check/git_history.py +14 -6
  38. package/scripts/publish-check/history_scan.py +1 -1
package/cli/README.md CHANGED
@@ -126,13 +126,20 @@ rebuild preview. A build that regenerates its own files declares them as
126
126
  paths are not inputs, while any other file touched during the build is refused by name. Both use the same `.wrangler/state` and saved owner. Dev opens signed in through `RER_OPEN`, including with the legacy
127
127
  `--login` flag. Stop before changing modes.
128
128
 
129
- Local startup holds the checkout's exclusive operation lock until serving stops.
129
+ Setup, local import/restore, reset and local startup hold the checkout's exclusive
130
+ operation lock through completion (or until serving stops).
130
131
  Its state-holder inspection selects only regular files under `.wrangler/state`,
131
- including SQLite sidecars. Dev and demo give `lsof` three seconds to answer. A confirmed
132
+ including SQLite sidecars. Each local custody check gives inspection three seconds to answer. A confirmed
132
133
  holder is refused with its PID and process name. If inspection cannot answer,
133
- dev (including demo) continues under the held checkout lock; another
134
- cooperating launch cannot acquire it. This lock does not inventory unmanaged
135
- processes. Setup, reset and preview still require their state inspection to complete.
134
+ canonical-state commands probe existing SQLite databases with an immediate exclusive transaction,
135
+ then continue under the held checkout lock; another cooperating operation cannot
136
+ acquire it. A conflicting SQLite transaction is refused even when its PID could not
137
+ be inspected. The probe rolls back without changing rows. Neither proof inventories
138
+ idle unmanaged file handles or WAL readers. Keep the bounded inspection even when
139
+ the launch lock is free: skipping it would miss unmanaged state holders and saved
140
+ origin listeners that a completed inspection currently detects.
141
+ Demo uses the launch lock without opening canonical SQLite: probes can checkpoint
142
+ idle WAL or change read marks, and demo preserves every owner state byte.
136
143
  Stop dev or preview before build, verification or data transfer; use the
137
144
  documented `--stop` flag or Ctrl-C.
138
145
 
package/cli/contract.mjs CHANGED
@@ -26,5 +26,5 @@ export const demoVerb = Object.freeze({ flags: [...commonFlags, '--port', '--hos
26
26
  // sign the owner in to, so no seed declares it and check does not ask for it.
27
27
  export const signinVerb = Object.freeze({ flags: [...commonFlags, '--environment', '--env-file'], effects: 'A one-time sign-in link for the hosted owner, without mail. Uses the Cloudflare credentials that deployed --environment (default production; from the environment or --env-file) to write a single-use grant for the recorded owner into that deployment\'s auth database, then prints the link once to this terminal; it is never saved. The link works once, only for that owner, expires within 15 minutes, and its use is recorded in the owner\'s audit log. Signed in this way, the owner can correct a wrong email in settings.' });
28
28
  export const aliases = Object.fromEntries([...Object.keys(verbs).filter(v => v !== 'install'), 'release', 'deps', 'rer'].map(name => [name, name === 'deps' ? 'install' : name === 'rer' ? null : name]));
29
- export const releaseFlags = ['--repo', '--project', '--repository', '--output', '--ref', '--dry-run', '--tag', '--clean-history', '--onto', '--env-file', '--no-queue'];
29
+ export const releaseFlags = ['--repo', '--project', '--repository', '--output', '--ref', '--public-base', '--dry-run', '--tag', '--clean-history', '--onto', '--env-file', '--no-queue'];
30
30
  export const requiredHooks = ['preview', 'dependencies', 'build', 'migrate', 'start', 'health', 'owner', 'signIn', 'verify', 'deploy', 'portability', 'state', 'settings', 'input'];
package/cli/demo-host.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { randomBytes } from 'node:crypto';
2
3
  import { createRequire } from 'node:module';
3
4
  import { dirname, join } from 'node:path';
4
5
  import { contained } from './project.mjs';
@@ -6,14 +7,18 @@ import { invalid, missing, refuse, RerError } from './errors.mjs';
6
7
  import { imageFolder } from './container.mjs';
7
8
  import { prepareDemoImage } from '../modules/demo-host/image.mjs';
8
9
  import { DEMO_FILES, INSTANCE_TYPES, demoPlan } from '../modules/demo-host/plan.mjs';
10
+ import { awaitCertificates } from '../modules/demo-host/certificate.mjs';
11
+ import { MEASUREMENT_COOKIE, MEASUREMENT_HEADER, TESTING_SECRET } from '../modules/demo-host/measurement.mjs';
9
12
 
10
13
  // `rer deploy --demo [--dry-run]`: the seed's hosted demo at demo.<domain>
11
14
  // (modules/demo-host), from deploy-demo.json. It writes .rer/demo-host/: the
12
15
  // front Worker, its wrangler.json and the container image folder (workerd
13
16
  // engine). --dry-run stops there, so `wrangler dev` can serve that folder with
14
17
  // Docker; a deploy runs `wrangler deploy` on it with the Turnstile secret and
15
- // the demo's record as the version message. `--remove --yes` removes what that
16
- // record proves is the demo's (modules/demo-host/remove.mjs).
18
+ // the demo's record as the version message, then waits for every demo host's
19
+ // certificate. On Turnstile's testing secret (a test host) it also sets the
20
+ // measurement token starts must carry, and says where it is. `--remove --yes` removes
21
+ // what that record proves is the demo's (modules/demo-host/remove.mjs).
17
22
  export async function demoHostCommand(project, options, store, signal, out, operation) {
18
23
  const read = async path => { try { return JSON.parse(await readFile(await contained(project.root, path), 'utf8')); } catch (error) { if (error instanceof RerError) throw error; throw invalid(`Missing or malformed ${path}.`); } };
19
24
  const app = await read(project.adapter.deploy?.declaration ?? 'deploy.json'), declaration = await read('deploy-demo.json');
@@ -23,11 +28,13 @@ export async function demoHostCommand(project, options, store, signal, out, oper
23
28
  catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds {"version": 1, "domain": "<run site apex>", "name": "<product name>"}, optionally "maxInstances", "instanceType" (${INSTANCE_TYPES.join(', ')}; default basic), "ownerEmail" (the demo owner's address; default owner@demo.invalid) and plain "settings".`); }
24
29
  if (options.remove) return removeDemo(plan, store, signal);
25
30
  if (process.env.RER_DEMO_OPERATOR_TOKEN && (process.env.RER_DEMO_OPERATOR_TOKEN.length < 32 || process.env.RER_DEMO_OPERATOR_TOKEN.length > 512)) throw invalid('RER_DEMO_OPERATOR_TOKEN must contain 32 to 512 characters.');
31
+ const supplied = process.env.RER_DEMO_MEASUREMENT_TOKEN?.trim();
32
+ if (supplied && (supplied.length < 32 || supplied.length > 512 || /[\s;,]/.test(supplied))) throw invalid('RER_DEMO_MEASUREMENT_TOKEN must contain 32 to 512 characters, with no spaces, semicolons or commas.');
26
33
  const folder = join(project.root, '.rer', 'demo-host');
27
34
  await rm(folder, { recursive: true, force: true });
28
35
  await mkdir(folder, { recursive: true, mode: 0o700 });
29
36
  await store.step('Building the demo container image (workerd engine)');
30
- await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
37
+ await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'RER_DEMO_MEASUREMENT_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
31
38
  await prepareDemoImage(join(folder, 'image'));
32
39
  for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
33
40
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
@@ -43,15 +50,34 @@ export async function demoHostCommand(project, options, store, signal, out, oper
43
50
  const names = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN', 'TURNSTILE_SITE_KEY', 'TURNSTILE_SECRET_KEY'];
44
51
  if (names.some(name => !process.env[name]?.trim())) throw missing(`${names.join(', ')} are required; see DEMO.md "Hosting the demo".`);
45
52
  if (!options.json) for (const line of human) out.human(line);
53
+ // Production hosts keep the real human check and never take a measurement token.
54
+ const measurement = TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY.trim()) ? supplied || randomBytes(32).toString('base64url') : null;
46
55
  const secrets = join(folder, '.secrets.json');
47
- await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim(), ...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}) }), { mode: 0o600 });
56
+ await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim(), ...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}),
57
+ ...(measurement ? { RER_DEMO_MEASUREMENT_TOKEN: measurement } : {}) }), { mode: 0o600 });
48
58
  try {
49
59
  const require = createRequire(join(project.root, 'package.json'));
50
60
  const manifest = require.resolve('wrangler/package.json');
51
61
  const wrangler = join(dirname(manifest), JSON.parse(await readFile(manifest, 'utf8')).bin.wrangler);
52
62
  await store.run('Deploying the demo Worker and its container image', process.execPath, [wrangler, 'deploy', '--config', 'wrangler.json', '--secrets-file', '.secrets.json', '--message', plan.record], { cwd: folder, signal });
53
63
  } finally { await rm(secrets, { force: true }); }
54
- return { dryRun: false, deployed: true, origin: plan.origin, demo: summary };
64
+ await store.step(`Waiting for the certificate on ${plan.hostnames.join(', ')}`);
65
+ try { await awaitCertificates({ hostnames: plan.hostnames, report: line => store.report(line) }); }
66
+ catch (error) {
67
+ if (error.message !== 'demo-certificate-timeout') throw error;
68
+ throw new RerError('deploy_refused', `The demo is deployed, but its certificate is not active 10 minutes later (${error.hosts.join('; ')}). Run the deploy again to wait again; nothing needs removing.`, 1);
69
+ }
70
+ let measured;
71
+ if (measurement) {
72
+ // Output redaction masks credentials, so a token rer made goes to a private file.
73
+ const file = '.rer/demo-host/measurement-token';
74
+ if (!supplied) await writeFile(join(project.root, file), measurement + '\n', { mode: 0o600 });
75
+ measured = { cookie: MEASUREMENT_COOKIE, header: MEASUREMENT_HEADER, token: supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file };
76
+ const how = `send it as the cookie ${MEASUREMENT_COOKIE}=<token> on ${plan.hostname} or the header ${MEASUREMENT_HEADER}: <token>`;
77
+ if (options.json) out.event('demo-measurement', measured);
78
+ else out.human(`Test host: it starts demos only with the measurement token in ${supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file}; ${how}.`);
79
+ }
80
+ return { dryRun: false, deployed: true, origin: plan.origin, demo: summary, ...(measured ? { measurement: measured } : {}) };
55
81
  }
56
82
 
57
83
  async function removeDemo(plan, store, signal) {
@@ -0,0 +1,23 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { deliveryDeclarationSources } from './delivery-declarations.mjs';
3
+ import { sourcePath } from '../modules/delivery-producer/migrations.mjs';
4
+ import { usesManagedMail } from '../modules/delivery-producer/mail.mjs';
5
+
6
+ // Managed-capable seeds can retain direct-provider adapters. Only declared
7
+ // mail credentials receive stand-ins; other application secrets still need
8
+ // their own deploy declaration, even when packaging lists them.
9
+ const credentials = new Set(['MAIL_API_KEY', 'MAIL_PROVIDER_API_KEY', 'RESEND_API_KEY', 'SENDGRID_API_KEY']);
10
+
11
+ export async function hostedMailCredentials(root) {
12
+ const names = new Set();
13
+ for (const [candidate, select] of deliveryDeclarationSources) {
14
+ const path = await sourcePath(root, candidate, { optional: true });
15
+ if (!path) continue;
16
+ const spec = select(JSON.parse(await readFile(path, 'utf8')));
17
+ if (!spec || !usesManagedMail(spec.mail)) continue;
18
+ for (const name of [...(spec.secretNames ?? []), ...(spec.optionalSecretNames ?? [])]) {
19
+ if (credentials.has(name)) names.add(name);
20
+ }
21
+ }
22
+ return [...names];
23
+ }
@@ -8,6 +8,7 @@ import { publicAssetPaths } from '../modules/deploy-kit/artifact.mjs';
8
8
  import { migrate } from '../modules/container/migrations.mjs';
9
9
  import { readMigrationRewrites, withRewrites } from '../modules/deploy-kit/migrations.mjs';
10
10
  import { RerError } from './errors.mjs';
11
+ import { hostedMailCredentials } from './hosted-mail.mjs';
11
12
 
12
13
  // Serve the production build the way rer deploy hosts it (assets behind the
13
14
  // Worker, deploy identity bound, a public https origin) on loopback, with no
@@ -40,8 +41,8 @@ async function miniflareFor(root) {
40
41
  const standIn = (item, field) => typeof item.verifyStandIn === 'string' && item.verifyStandIn ? item.verifyStandIn : field?.kind === 'email' ? 'owner@example.com' : typeof field?.default === 'string' && field.default ? field.default : randomBytes(24).toString('hex');
41
42
  const links = html => [...html.matchAll(/(?:src|href)=["'](\/[^"'#?\s]+)/g)].map(match => match[1].slice(1)).filter(path => !path.startsWith('/') && /\.[A-Za-z0-9]+$/.test(path));
42
43
 
43
- /** The build served as rer deploy hosts it, with the vars, origin and (stand-in) secrets of its
44
- * deploy declaration. `fetch(path, init)` answers for the deployed origin. */
44
+ /** The build served as rer deploy hosts it, with declared vars, origin and
45
+ * stand-in deploy/Managed mail secrets. `fetch(path, init)` uses the deployed origin. */
45
46
  export async function serveHosted(root, { declaration: declarationPath = 'deploy.json' } = {}) {
46
47
  const build = join(root, 'dist/server'), client = join(root, 'dist/client');
47
48
  let config;
@@ -56,6 +57,9 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
56
57
  const modules = [entry, ...(await files(build)).map(path => join(build, path)).filter(path => path !== entry && moduleTypes[extname(path)])].map(path => ({ type: moduleTypes[extname(path)], path }));
57
58
  const identity = { sourceSha: createHash('sha1').update(randomBytes(16)).digest('hex'), fingerprint: createHash('sha256').update(randomBytes(16)).digest('hex'), versionId: crypto.randomUUID() };
58
59
  const secrets = Object.fromEntries((declaration.secrets ?? []).map(item => [item.binding, standIn(item, manifest.fields?.find(field => field.name === item.binding))]));
60
+ for (const binding of await hostedMailCredentials(root)) {
61
+ if (!Object.hasOwn(secrets, binding)) secrets[binding] = standIn({}, manifest.fields?.find(field => field.name === binding));
62
+ }
59
63
  const mf = new Miniflare(convertV4MiniflareOptions({ host: '127.0.0.1', port: 0, cf: false, workers: [{ name: 'ingress', modules: true, script: ingress, serviceBindings: { APP: 'app' } }, {
60
64
  name: 'app', modules, modulesRoot: build, compatibilityDate: config.compatibility_date, compatibilityFlags: config.compatibility_flags,
61
65
  bindings: { ...config.vars, ...declaration.vars, ...secrets, ...(declaration.originVar ? { [declaration.originVar]: ORIGIN } : {}),
package/cli/lifecycle.mjs CHANGED
@@ -49,16 +49,19 @@ async function runLifecycle(project, options, out, deployRedaction) {
49
49
  if (!project.adapter?.run) throw refuse('The first-party command adapter is unavailable. Restore the shared lifecycle adapter.');
50
50
  if (options.forwarded.length) throw refuse('First-party commands do not accept arbitrary forwarded arguments. Run rer VERB --help.');
51
51
  await stateSafety(project);
52
- if(options.verb==='setup')await refuseLegacyProcess(project,{verb:options.verb});
52
+ const { localOperation } = await shared('local/operation.mjs');
53
+ // The run site reads no local state: it builds beside a running rer dev.
54
+ if (!options.site) try { operation = await localOperation(project.root, process.env.RER_LOCAL_OPERATION); } catch (error) { throw refuse(error.message); }
55
+ if(options.verb==='setup'||options.verb==='import'&&(options.target??'local')==='local'){
56
+ const ownership=await refuseLegacyProcess(project,{verb:options.verb,operation});
57
+ if(ownership?.lockProof)out.progress('Local process inspection did not answer; continuing with the exclusive checkout launch lock.\n');
58
+ }
53
59
  if (options.verb === 'setup') {
54
60
  selected = await selectedOrigin(project, options, { setup: options.verb === 'setup' });
55
61
  if (options.verb === 'setup') {input = await setupInput(project, options, selected.vars, signal, { origin: selected.url });await dependencyInputs({project,input,origin:selected.url});}
56
62
  if (!selected.door) await availablePort(selected.host, selected.port, { root: project.root, saved: !selected.explicitPort && selected.savedOrigin !== undefined, verb:options.verb });
57
63
  if (options.verb === 'setup') announcePort(out, options, selected);
58
64
  }
59
- const { localOperation } = await shared('local/operation.mjs');
60
- // The run site reads no local state: it builds beside a running rer dev.
61
- if (!options.site) try { operation = await localOperation(project.root, process.env.RER_LOCAL_OPERATION); } catch (error) { throw refuse(error.message); }
62
65
  const { recoverInterrupted } = await shared('local/recovery.mjs');
63
66
  if (operation) await recoverInterrupted(project.root, operation);
64
67
  if (project.ownerReplacement) await replaceOwner(project, options, out);
package/cli/local.mjs CHANGED
@@ -66,7 +66,15 @@ async function safeTree(project, path) {
66
66
 
67
67
  export async function reset(project, options, out) {
68
68
  await stateSafety(project);
69
- await refuseLegacyProcess(project,{reset:true,verb:'reset'});
69
+ const { localOperation } = await shared('local/operation.mjs');
70
+ let operation;
71
+ try { operation = await localOperation(project.root); } catch (error) { throw refuse(error.message); }
72
+ try { return await resetHeld(project,options,out,operation); }
73
+ finally { await operation.release(); }
74
+ }
75
+
76
+ async function resetHeld(project,options,out,operation) {
77
+ await refuseLegacyProcess(project,{reset:true,verb:'reset',operation});
70
78
  const { strandedOriginals } = await shared('local/recovery.mjs');
71
79
  if ((await strandedOriginals(project.root)).length) throw refuse('An interrupted build holds private settings. Run rer build to recover them before reviewing reset.');
72
80
  // The seed's own safety check (legacy data it still has to recover, for
@@ -89,29 +97,32 @@ export async function reset(project, options, out) {
89
97
  }
90
98
  if (!confirmed) throw refuse('Reset requires explicit confirmation. Review the inventory, then pass --yes.');
91
99
  // The shared lock lives outside every removal path and remains held throughout.
92
- const { localOperation } = await shared('local/operation.mjs');
93
- let operation;
94
- try { operation = await localOperation(project.root); } catch (error) { throw refuse(error.message); }
95
- try {
96
- for (const path of inventory) { const absolute = await safeTree(project, path); if (absolute) await rm(absolute, { recursive: true, force: true }); }
97
- } finally { await operation.release(); }
100
+ for (const path of inventory) { const absolute = await safeTree(project, path); if (absolute) await rm(absolute, { recursive: true, force: true }); }
98
101
  return { paths: inventory };
99
102
  }
100
103
 
101
- export async function refuseLegacyProcess(project,{reset=false,verb='dev',operation}={}) {
104
+ export async function refuseLegacyProcess(project,{reset=false,verb='dev',operation,isolated=false}={}) {
105
+ // Every caller must keep this exclusive checkout proof through its operation.
106
+ const {localOperation}=await shared('local/operation.mjs');
107
+ if(!operation?.token)throw refuse('Local ownership inspection requires the checkout operation lock.');
108
+ await localOperation(project.root,operation.token);
102
109
  let selected;try{selected=await selectedOrigin(project,{});}catch(error){if(!reset)throw error;}
103
110
  // Confirmed reset can recover malformed settings; checkout/state custody is
104
111
  // still checked even when there is no usable saved origin.
105
112
  const {localProcessConflict,inspectionContext,uncheckedInspection}=await shared('process-diagnostics.mjs');
106
113
  // The front door holds a door origin's port, not this checkout.
107
114
  let conflict;
108
- try{conflict=await localProcessConflict(project.root,selected?.door?undefined:selected?.savedOrigin,inspectionContext({verb}));}
115
+ try{conflict=await localProcessConflict(project.root,selected?.door?undefined:selected?.savedOrigin,inspectionContext({verb,budgetMs:3000}));}
109
116
  catch(error){
110
- // Startup already holds the exclusive checkout operation through teardown.
111
- // An unanswered observation must not defeat that cooperating-launch proof.
112
- // Setup/reset retain their destructive-operation inspection requirement.
113
- if(operation?.token&&verb==='dev'&&uncheckedInspection(error))return {lockProof:true};
114
- throw error;
117
+ if(!uncheckedInspection(error))throw error;
118
+ // Demo serves isolated data. Even a read-only SQLite connection can update
119
+ // canonical WAL read marks, so its owner-state byte preservation forbids it.
120
+ if(!isolated){
121
+ const {sqliteCustody}=await shared('local/sqlite-custody.mjs');
122
+ const database=await sqliteCustody(project.root);
123
+ if(database)throw refuse(`Another process holds an active SQLite lock on ${database}. Process inspection did not identify its PID. Stop unmanaged local servers (including Wrangler), then retry. Use rer dev --stop or rer preview --stop for a registered server.`);
124
+ }
125
+ return {lockProof:true};
115
126
  }
116
127
  if(conflict)throw refuse(`A local process holds ${conflict.reason}: PID ${conflict.pid} (${conflict.command}). Stop it with kill -TERM ${conflict.pid}, then retry. Use rer dev --stop or rer preview --stop for a registered server.`);
117
128
  }
@@ -87,7 +87,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
87
87
  await stateSafety(project);
88
88
  const {localOperation}=await shared('local/operation.mjs');
89
89
  try{operation=await localOperation(project.root);}catch(error){throw refuse(error.message);}
90
- const ownership=await refuseLegacyProcess(project,{verb:options.verb,operation});
90
+ const ownership=await refuseLegacyProcess(project,{verb:options.verb,operation,isolated:options.demo});
91
91
  if(ownership?.lockProof)out.progress('Local process inspection did not answer; continuing with the exclusive checkout launch lock.\n');
92
92
  const {recoverInterrupted}=await shared('local/recovery.mjs');await recoverInterrupted(project.root,operation);
93
93
  const {loadInstallerSeed}=await shared('setup-manifest.mjs');
package/cli/release.mjs CHANGED
@@ -11,6 +11,8 @@ Without --tag, write a preview. This command never pushes, uploads or deploys.
11
11
  Credential-free https://github.com/<owner>/<repository> destination.
12
12
  --output NEW_DIR New output directory outside the source repository.
13
13
  --ref REF Selected committed source (default: HEAD).
14
+ --public-base REF Public history as GIT_URL#BRANCH for exact-source scans;
15
+ defaults to this public repository's default branch.
14
16
  --tag [vVERSION] Prepare source artifacts and create a local annotated tag;
15
17
  an explicit tag must match the committed release version.
16
18
  --dry-run Run the real gates and suppress local tag creation.
@@ -43,8 +45,8 @@ export async function dispatchRelease(args) {
43
45
  else if (args[index] === '--tag') options.tag = args[index + 1]?.startsWith('v') ? args[++index] : true;
44
46
  else if (args[index] === '--clean-history') options.cleanHistory = true;
45
47
  else if (args[index] === '--dry-run') options.dryRun = true;
46
- else if (['--repo', '--project', '--repository', '--output', '--ref', '--env-file', '--onto'].includes(args[index]) && args[index + 1] && !args[index + 1].startsWith('--'))
47
- options[args[index] === '--env-file' ? 'envFile' : args[index].slice(2)] = args[++index];
48
+ else if (['--repo', '--project', '--repository', '--output', '--ref', '--env-file', '--onto', '--public-base'].includes(args[index]) && args[index + 1] && !args[index + 1].startsWith('--'))
49
+ options[args[index] === '--env-file' ? 'envFile' : args[index] === '--public-base' ? 'publicBase' : args[index].slice(2)] = args[++index];
48
50
  else throw Error('Invalid release arguments');
49
51
  }
50
52
  if(args.includes('--no-queue')){const {shared}=await import('./shared.mjs');const {commandPolicy}=await shared('queue.mjs');const policy=await commandPolicy();try{if(!policy.ci)throw Error('Original CI required');}finally{await policy.close();}}
@@ -1,5 +1,32 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.27
4
+
5
+ - `rer release` of an exact source commit scans source history against the repository's public history. When the checkout's origin is a confirmed public GitHub repository, its default branch and published tags are the scanner's public base, so findings already public are classified pre-existing and unpublished content still blocks. `--public-base GIT_URL#BRANCH` selects public history explicitly; it cannot be combined with `--onto` or `--clean-history`.
6
+ - On a busy machine, a process-table read that outlives its ordinary bound is awaited for up to 25 more seconds with a `RER_PROCESS_INSPECTION_SLOW` warning, instead of failing a passing run's cleanup. Cleanup still fails only on owned processes observed alive.
7
+ - Setup, local import/restore, reset and local startup hold the checkout's exclusive operation lock through completion, and every local ownership check falls back to that lock proof when `lsof` does not answer within three seconds. Canonical-state commands then probe existing SQLite databases with an immediate exclusive transaction that rolls back, and refuse when another process holds a SQLite lock. Demo uses the lock proof without opening canonical SQLite.
8
+ - `modules/operator-auth/policy.d.mts` ships client-safe types for `normalizeEmail`, `emailDomain` and `safeReturnTo`.
9
+ - `DEMO.md` says that new visitors may get the previous demo version for several minutes after a deploy (about 7–9 minutes measured), so measure about 10 minutes after a demo deploy.
10
+
11
+ ### Upgrade notes
12
+
13
+ - Update the exact rer package pin, or refresh vendored operating commands, to receive these changes.
14
+
15
+ ## 0.2.26
16
+
17
+ - The `rer` mail adapter carries `List-Id`, `List-Archive`, `List-Help`, `In-Reply-To` and `References` to the Managed relay as its `listId`, `listArchive`, `listHelp`, `inReplyTo` and `references` fields, and refuses invalid values before sending. Other raw headers stay refused.
18
+ - Hosted output takes Managed-mail credential stand-ins from the seed's mail spec when `mail.providers` includes `rer`, so a Managed seed no longer has to list them in `deploy.json`. `export/SEED-COMMAND-CONTRACT.md` documents the own-host and Managed contract.
19
+ - In a hosted demo, the app's fetches to its own demo hosts (primary and declared secondary) are served by its own container as the same visitor, with no network egress. Every other destination still goes through the demo egress.
20
+ - `rer deploy --demo` waits up to 10 minutes for each demo hostname to present a verified TLS certificate, and fails naming what each host last answered.
21
+ - Demo hosts on Turnstile testing keys start demos only when the start carries the deploy's measurement token, as the cookie `rer_demo_measure` or the header `x-rer-demo-measurement`. `rer deploy --demo` uses `RER_DEMO_MEASUREMENT_TOKEN` when it is set; otherwise it writes a token per deploy to `.rer/demo-host/measurement-token` (0600) and prints that path, not the token.
22
+ - Demo image builds accept `MAIL_RELAY_URL` as public Managed-mail configuration, alongside `MAIL_PROVIDER` and `MAIL_FROM`.
23
+
24
+ ### Upgrade notes
25
+
26
+ - Update the exact rer package pin, or refresh vendored operating commands, to receive these changes.
27
+ - A demo deployed to a test host (Turnstile testing keys) needs its measurement token to start demos: set the repository secret `RER_DEMO_MEASUREMENT_TOKEN`, which `run-demo.yml` passes to `rer deploy --demo`, and send it with each measurement start.
28
+ - Remove `MAIL_RELAY_URL` from `deploy.json` secrets and keep it as a public spec or Wrangler variable.
29
+
3
30
  ## 0.2.25
4
31
 
5
32
  - Invitations stay valid for seven days (`invitationSeconds`), separately from sign-in and step-up links, which keep their short lifetime.
@@ -35,6 +35,11 @@ in both directions, preserving text and binary messages, selected protocols,
35
35
  cookies and close codes. Refused upgrades retain their HTTP response; other
36
36
  upgrade protocols receive 400. Hosted demo routing selects the visitor's own
37
37
  container before forwarding the upgrade, including on declared secondary hosts.
38
+ In a hosted demo (`RER_DEMO=1` with `RER_INGRESS_ORIGIN`), the workerd demo
39
+ egress serves the app's own fetch of any of its ingress origins through this
40
+ container's ingress, never the network: the front admits only the visitor's
41
+ cookie, so this is how the app reads its own public pages. Other destinations
42
+ keep the demo egress and its refusals.
38
43
 
39
44
  The workerd source boundary permits Durable Object definitions, including export
40
45
  aliases and indirect inheritance. Unbound definitions create no namespace. The
@@ -68,10 +73,10 @@ SQLite stays on the Docker host's local filesystem; host-shared checkout mounts
68
73
  are not used for database storage. Back up with export before deleting a volume. Build
69
74
  uses the ordinary private-file custody and command admission boundary. Secrets
70
75
  are provided at runtime, never baked into the image or transferred in archives.
71
- Managed mail's `MAIL_PROVIDER` and `MAIL_FROM` are public configuration. Keep
72
- them in the mail spec's public variables and Wrangler `vars`, and remove them
73
- from `deploy.json` secrets. For migration, the image builder accepts those two
74
- legacy secret declarations when the managed-mail spec declares them public,
76
+ Managed mail's `MAIL_PROVIDER`, `MAIL_FROM` and `MAIL_RELAY_URL` are public
77
+ configuration. Keep them in the mail spec's public variables and Wrangler `vars`,
78
+ and remove them from `deploy.json` secrets. For migration, the image builder
79
+ accepts those legacy secret declarations when the managed-mail spec declares them public,
75
80
  and prints that correction. Demo runtime settings override their image defaults.
76
81
  API keys, tokens and other secrets in image variables remain refused.
77
82
 
@@ -6,6 +6,18 @@ const transports = new Set([
6
6
  ]);
7
7
  const mailPorts = new Set(['25', '465', '587', '2525']);
8
8
 
9
+ // The demo's own hosts (RER_INGRESS_ORIGIN and RER_INGRESS_ORIGINS, from
10
+ // deploy-demo.json) reach the front only with the visitor's cookie, so the
11
+ // app's own read of one would leave the container and meet the start page.
12
+ // SELF is this container's ingress: the same visitor's demo, no network.
13
+ const STRIPPED = ['cf-connecting-ip', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-for', 'forwarded', 'x-rer-original-url', 'x-rer-ingress-origin'];
14
+ function local(request, url) {
15
+ const headers = new Headers(request.headers);
16
+ for (const name of STRIPPED) headers.delete(name);
17
+ headers.set('cf-connecting-ip', '127.0.0.1'); headers.set('x-rer-original-url', url.href);
18
+ return new Request(request, { headers, redirect: 'manual' });
19
+ }
20
+
9
21
  function check(url) {
10
22
  const host = url.hostname.toLowerCase().replace(/\.$/, '');
11
23
  if (transports.has(host) || mailPorts.has(url.port)) throw new TypeError('Demo direct messaging transport refused');
@@ -17,10 +29,11 @@ export default {
17
29
  const redirect = request.redirect;
18
30
  for (let hops = 0; ; hops++) {
19
31
  const url = new URL(request.url);
20
- check(url);
32
+ const own = JSON.parse(env.RER_DEMO_ORIGINS ?? '[]').includes(url.origin);
33
+ if (!own) check(url);
21
34
  // Check every redirect before contacting its destination, including when
22
35
  // an otherwise public URL redirects into a messaging provider.
23
- const response = await env.NETWORK.fetch(new Request(request.clone(), { redirect: 'manual' }));
36
+ const response = own ? await env.SELF.fetch(local(request.clone(), url)) : await env.NETWORK.fetch(new Request(request.clone(), { redirect: 'manual' }));
24
37
  const location = response.headers.get('location');
25
38
  if (![301, 302, 303, 307, 308].includes(response.status) || location === null || redirect === 'manual') return response;
26
39
  await response.body?.cancel();
@@ -18,7 +18,7 @@ export async function checkImageVariables(root,vars,secrets) {
18
18
  const names=spec.publicVariableNames ?? Object.keys(spec.publicVariables ?? {});
19
19
  const publicVariables=Object.fromEntries(Object.entries(vars).filter(([name])=>names.includes(name)));
20
20
  validateMailSpec({...spec,publicVariables,secretNames:spec.secretNames ?? []});
21
- for (const name of ['MAIL_PROVIDER','MAIL_FROM']) if (typeof publicVariables[name] === 'string') publicMail.add(name);
21
+ for (const name of ['MAIL_PROVIDER','MAIL_FROM','MAIL_RELAY_URL']) if (typeof publicVariables[name] === 'string') publicMail.add(name);
22
22
  }
23
23
  for (const {binding} of overlaps) if (!publicMail.has(binding) || privateNames.has(binding)) {
24
24
  throw new Error('Secret declared as an image variable: '+binding);
@@ -20,8 +20,8 @@ export async function workerEngine({ root, data, manifest, settings, edge }) {
20
20
  const mf = new Miniflare(convertV4MiniflareOptions({ host:'127.0.0.1',port:0,cf:false,resourcePersistencePath:data,unsafeLocalExplorer:false,
21
21
  workers:[{ name:'ingress',modules:true,script:`export default {fetch(request,env){const observed=request.headers.get('x-rer-original-url');if(!observed)return new Response(null,{status:400});const url=new URL(observed),headers=new Headers(request.headers);headers.delete('x-rer-original-url');headers.set('host',url.host);return env.APP.fetch(new Request(url,{method:request.method,headers,redirect:'manual',...(!['GET','HEAD'].includes(request.method)?{body:request.body}:{})}))}}`,serviceBindings:{ APP:'app' } }, app,
22
22
  { name:'egress',modules:[{ type:'ESModule',path:join(import.meta.dirname,'demo-egress.mjs') }],modulesRoot:import.meta.dirname,
23
- bindings:{ RER_DEMO:settings.RER_DEMO ?? '' },
24
- serviceBindings:{ NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
23
+ bindings:{ RER_DEMO:settings.RER_DEMO ?? '', RER_DEMO_ORIGINS:JSON.stringify(settings.RER_DEMO === '1' && settings.RER_INGRESS_ORIGIN ? [settings.RER_INGRESS_ORIGIN, ...JSON.parse(settings.RER_INGRESS_ORIGINS ?? '[]')] : []) },
24
+ serviceBindings:{ SELF:'ingress', NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
25
25
  }));
26
26
  await mf.ready; const databases = {}, objects = {};
27
27
  for (const [binding, plan] of Object.entries(manifest.migrations)) { const db = await mf.getD1Database(binding, 'app'); databases[binding] = db; await migrate(db, plan); await ensurePortabilitySchema(db, manifest.portabilityMigrations); }
@@ -26,8 +26,11 @@ start page.
26
26
  held (at most 1.5 seconds) until the start changes, so the page learns of the
27
27
  sign-in at once; a slow status read answers pending. Each admission logs
28
28
  `demo_admitted` with the request's user agent and network owner, so a held
29
- place can be joined to whoever started it (automated browsers pass
30
- Turnstile's always-pass testing keys on test hosts). A request
29
+ place can be joined to whoever started it. On Turnstile's testing secret (a
30
+ test host) a start must also carry the deploy's measurement token
31
+ (`measurement.mjs`: cookie `rer_demo_measure` or header
32
+ `x-rer-demo-measurement`), since automated browsers pass those keys; a
33
+ test host without a token admits nobody. A request
31
34
  with that cookie goes to the visitor's Durable Object; nothing else reaches
32
35
  a container.
33
36
  - `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
@@ -48,6 +51,10 @@ start page.
48
51
  exact idle/absolute deadline without keeping a timer in flight. Requests
49
52
  check both deadlines before refreshing activity. A container found stopped
50
53
  ends the demo and answers the ended page (410).
54
+ - `certificate.mjs`: after `wrangler deploy`, the CLI waits for each demo
55
+ hostname's certificate, a TLS handshake that verifies for that name at the
56
+ address public DNS over HTTPS gives (10 minutes at most), and logs each
57
+ attempt. A failed or unverified handshake never counts as active.
51
58
  - `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
52
59
  storage transaction, excluding diagnostics and expired rows. A lost start
53
60
  handoff expires after one minute; a successful claim extends the reservation
@@ -0,0 +1,56 @@
1
+ import { connect } from 'node:tls';
2
+
3
+ // A new Custom Domain answers TLS only once Cloudflare has issued its
4
+ // certificate: about 153 s on a fresh zone (dm-lanyard-1, October 2026), with
5
+ // browsers seeing ERR_SSL_VERSION_OR_CIPHER_MISMATCH meanwhile. After the
6
+ // deploy, wait on every demo hostname for a handshake that verifies the
7
+ // certificate for that name, so the deploy's readback and first visitors
8
+ // meet a working host. Addresses come from public DNS over HTTPS, so a local
9
+ // resolver's cached "no such host" for a new name cannot stall the wait. A
10
+ // failed or unverified handshake is never success: the wait ends in an error
11
+ // naming the last thing each host answered.
12
+ export async function awaitCertificates({ hostnames, report, resolve = publicAddress, port = 443, ca, timeout = 10 * 60_000, interval = 10_000, attemptTimeout = 10_000 }) {
13
+ const deadline = Date.now() + timeout, pending = new Set(hostnames), last = new Map();
14
+ for (let attempt = 1; ; attempt++) {
15
+ for (const hostname of pending) {
16
+ let seen;
17
+ try {
18
+ const address = await resolve(hostname, attemptTimeout);
19
+ if (!address) seen = 'not in public DNS yet';
20
+ else {
21
+ const certificate = await handshake({ address, hostname, port, ca, timeout: attemptTimeout });
22
+ pending.delete(hostname);
23
+ report(`TLS on ${hostname} verified (${certificate.protocol}, certificate valid to ${certificate.validTo}).`);
24
+ continue;
25
+ }
26
+ } catch (error) { seen = error.code ?? error.message; }
27
+ last.set(hostname, seen);
28
+ report(`Waiting for the certificate on ${hostname}: ${seen} (attempt ${attempt}).`);
29
+ }
30
+ if (!pending.size) return;
31
+ if (Date.now() + interval >= deadline) {
32
+ throw Object.assign(new Error('demo-certificate-timeout'), { hosts: [...pending].map(hostname => `${hostname}: ${last.get(hostname)}`) });
33
+ }
34
+ await new Promise(done => setTimeout(done, interval));
35
+ }
36
+ }
37
+
38
+ function handshake({ address, hostname, port, ca, timeout }) {
39
+ return new Promise((resolve, reject) => {
40
+ const socket = connect({ host: address, port, servername: hostname, rejectUnauthorized: true, ...(ca ? { ca } : {}), timeout });
41
+ socket.once('secureConnect', () => {
42
+ const certificate = socket.getPeerCertificate(), protocol = socket.getProtocol(), authorized = socket.authorized;
43
+ socket.destroy();
44
+ if (!authorized) reject(Object.assign(new Error('certificate not verified'), { code: socket.authorizationError ?? 'UNVERIFIED' }));
45
+ else resolve({ protocol, validTo: certificate.valid_to });
46
+ });
47
+ socket.once('timeout', () => { socket.destroy(); reject(Object.assign(new Error('handshake timed out'), { code: 'ETIMEDOUT' })); });
48
+ socket.once('error', reject);
49
+ });
50
+ }
51
+
52
+ async function publicAddress(hostname, timeout) {
53
+ const response = await fetch(`https://cloudflare-dns.com/dns-query?name=${encodeURIComponent(hostname)}&type=A`, { headers: { accept: 'application/dns-json' }, signal: AbortSignal.timeout(timeout) });
54
+ if (!response.ok) throw new Error(`public DNS answered ${response.status}`);
55
+ return (await response.json()).Answer?.find(record => record.type === 1)?.data ?? null;
56
+ }
@@ -0,0 +1,19 @@
1
+ import { equalSecret } from './operator.mjs';
2
+
3
+ // A test host runs on one of Cloudflare's Turnstile testing secrets, which pass
4
+ // (or fail) every visitor alike, so automated browsers took real places within
5
+ // minutes of a new certificate (gr-base-164). There a start must carry the
6
+ // deploy's measurement token, which `rer deploy --demo` hands the operator
7
+ // (RER_DEMO_MEASUREMENT_TOKEN, or .rer/demo-host/measurement-token): the cookie
8
+ // rer_demo_measure or the header x-rer-demo-measurement. A host with
9
+ // a real Turnstile secret never asks for it.
10
+ export const TESTING_SECRET = /^[123]x0{31}AA$/;
11
+ export const MEASUREMENT_COOKIE = 'rer_demo_measure', MEASUREMENT_HEADER = 'x-rer-demo-measurement';
12
+
13
+ export const testHost = env => TESTING_SECRET.test(env.TURNSTILE_SECRET_KEY ?? '');
14
+
15
+ /** Whether a start on a test host carries this deploy's token; a test host without one admits nobody. */
16
+ export async function measured(request, env) {
17
+ const cookie = (request.headers.get('cookie') ?? '').split(';').map(part => part.trim()).find(part => part.startsWith(`${MEASUREMENT_COOKIE}=`))?.slice(MEASUREMENT_COOKIE.length + 1);
18
+ return equalSecret(env.RER_DEMO_MEASUREMENT_TOKEN, request.headers.get(MEASUREMENT_HEADER) ?? cookie);
19
+ }
@@ -2,16 +2,21 @@ import { startPage } from './pages.mjs';
2
2
 
3
3
  // This credential belongs to the demo HOST operator, never a demo's signed-in
4
4
  // owner. It is supplied only as an Authorization header, never a URL/cookie.
5
- export async function operatorPage(request, env, correlation) {
6
- const expected = env.RER_DEMO_OPERATOR_TOKEN;
7
- const authorization = request.headers.get('authorization') ?? '';
8
- const supplied = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
9
- if (!expected || expected.length < 32 || !supplied || supplied.length > 512) return new Response('Unauthorized', { status: 401 });
5
+ /** A supplied credential against its secret, compared in constant time over their digests. */
6
+ export async function equalSecret(expected, supplied) {
7
+ if (!expected || expected.length < 32 || !supplied || supplied.length > 512) return false;
10
8
  const digest = value => crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
11
9
  const [a, b] = await Promise.all([digest(expected), digest(supplied)]);
12
10
  let unequal = 0; const av = new Uint8Array(a), bv = new Uint8Array(b);
13
11
  for (let i = 0; i < av.length; i++) unequal |= av[i] ^ bv[i];
14
- if (unequal) return new Response('Unauthorized', { status: 401 });
12
+ return unequal === 0;
13
+ }
14
+
15
+ export async function operatorPage(request, env, correlation) {
16
+ const expected = env.RER_DEMO_OPERATOR_TOKEN;
17
+ const authorization = request.headers.get('authorization') ?? '';
18
+ const supplied = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
19
+ if (!await equalSecret(expected, supplied)) return new Response('Unauthorized', { status: 401 });
15
20
  const record = await env.CAPACITY.get(env.CAPACITY.idFromName('diagnostics')).diagnostic(correlation);
16
21
  return startPage(env, { status: record ? 200 : 404, notice: record ? `Operator diagnostic: ${JSON.stringify(record)}` : 'No retained diagnostic for this start.' });
17
22
  }
@@ -9,7 +9,7 @@ import { siteDeclaration } from '../deploy-kit/site.mjs';
9
9
  * running the seed's own image (`rer build --platform container`, workerd
10
10
  * engine), deployed by `wrangler deploy` with the image beside it. */
11
11
 
12
- export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'startup.mjs', 'placement.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs'];
12
+ export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'startup.mjs', 'placement.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs', 'measurement.mjs'];
13
13
  const FRONT_SET = /^(?:RER_.*|SESSION_SECRET|SETUP_TOKEN|OWNER_EMAIL)$/;
14
14
  const SECRET = /SECRET|TOKEN|PASSWORD|_KEY$/;
15
15
  // Cloudflare Containers' predefined instance types, smallest first (developers.cloudflare.com/containers/platform-details/limits).
@@ -6,6 +6,7 @@
6
6
  // show its progress and failures; a plain form post gets the 303 or the page. Every request with that cookie goes to the visitor's
7
7
  // container; nothing else on this host is reachable.
8
8
  import { operatorPage } from './operator.mjs';
9
+ import { measured, testHost } from './measurement.mjs';
9
10
  import { bounded, category, report } from './diagnostics.mjs';
10
11
  import { startPage, demoCookie, clearCookie } from './pages.mjs';
11
12
 
@@ -80,6 +81,7 @@ async function start(request, env, ctx) {
80
81
  const correlation = crypto.randomUUID();
81
82
  const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, { 'x-rer-demo-correlation': correlation, ...headers });
82
83
  if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
84
+ if (testHost(env) && !await measured(request, env)) return refuse(403, 'This is a test host: it starts demos only for its operator\'s measurement runs.');
83
85
  let demo;
84
86
  const started = Date.now();
85
87
  try {
@@ -161,8 +161,24 @@ values are refused. `List-Unsubscribe` (one or two `<https:…>`/`<mailto:…>`
161
161
  entries, at most 2,000 characters) travels as `listUnsubscribe`, and
162
162
  `List-Unsubscribe-Post: List-Unsubscribe=One-Click` as `listUnsubscribePost:
163
163
  true`, which requires an HTTPS entry. The structured `unsubscribe` request
164
- assembles the same headers. Other recipients, attachments, reply-to and every
165
- other header are unsupported and fail before dispatch.
164
+ assembles the same headers.
165
+
166
+ `List-Id`, `List-Archive`, `List-Help`, `In-Reply-To` and `References` travel as
167
+ `listId`, `listArchive`, `listHelp`, `inReplyTo` and `references`, respectively.
168
+ They match platform's relay validation at `098ba40` (`src/managed-mail/headers.ts`):
169
+ each is a string of at most 2,000 characters, without newlines or controls.
170
+ `List-Id` accepts an optional ASCII phrase or quoted display name followed by an
171
+ angle-bracketed ASCII dot-atom identifier containing a dot (at most 255
172
+ characters). `List-Archive` and `List-Help` accept one or two angle-bracketed
173
+ HTTPS or mailto URLs, using the relay's URL and mailbox checks. These three
174
+ headers allow separator tabs. `In-Reply-To` accepts exactly one `<local@domain>`
175
+ message ID with ASCII dot-atoms or a bracketed domain literal. `References`
176
+ accepts 1–20 such IDs separated by ASCII spaces, without leading/trailing
177
+ spaces, tabs or commas. Invalid values fail locally with `invalid_list_id`,
178
+ `invalid_list_archive`, `invalid_list_help`, `invalid_in_reply_to` or
179
+ `invalid_references`. Values are preserved; raw headers are never forwarded.
180
+ Other recipients, attachments, reply-to and every other header are unsupported
181
+ and fail before dispatch.
166
182
 
167
183
  HTTP 202 returns `accepted` with no provider message ID. HTTP 400, 401, 402,
168
184
  403 and 413 return `rejected`, and so does any 3xx (`provider_redirected`);
@@ -1,4 +1,5 @@
1
1
  import { fail } from '../../core/errors.mjs';
2
+ import { rerListHeaderField } from './list-headers.mjs';
2
3
 
3
4
  // The relay's own List-Unsubscribe rule: one or two <https:…>/<mailto:…> entries.
4
5
  const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
@@ -17,7 +18,9 @@ export function rerHeaderFields(headers = {}) {
17
18
  const fields = {};
18
19
  for (const [name, value] of Object.entries(headers)) {
19
20
  const lower = name.toLowerCase();
20
- if (lower === 'auto-submitted') {
21
+ const field = rerListHeaderField(lower, value);
22
+ if (field) fields[field] = value;
23
+ else if (lower === 'auto-submitted') {
21
24
  if (value.trim().toLowerCase() !== 'auto-generated') fail('unsupported_provider_setting');
22
25
  } else if (lower === 'list-unsubscribe') fields.listUnsubscribe = value;
23
26
  else if (lower === 'list-unsubscribe-post') {
@@ -41,7 +44,7 @@ export function rerHeaderFields(headers = {}) {
41
44
  export function validateRerMessage(message) {
42
45
  if (message.to.length !== 1) fail('rer_one_recipient_required');
43
46
  // The relay cannot preserve these features. Refuse them rather than silently
44
- // dropping recipients, attachments or threading.
47
+ // dropping recipients or attachments.
45
48
  if (['cc', 'bcc', 'replyTo', 'attachments'].some(field => message[field] !== undefined)) fail('unsupported_provider_setting');
46
49
  rerHeaderFields(message.headers);
47
50
  if (!message.subject.trim() || message.subject.length > 200) fail('invalid_subject');
@@ -0,0 +1,44 @@
1
+ import { fail } from '../../core/errors.mjs';
2
+
3
+ // Match platform 098ba4034b80c1d704cf96398a41da94f2b879af:
4
+ // src/managed-mail/headers.ts:3-8,21-40,44-50 and src/platform/mail.ts:20.
5
+ const atom = "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+";
6
+ const dotAtom = atom + '(?:\\.' + atom + ')*';
7
+ const word = atom + '|"(?:[\\x20-\\x21\\x23-\\x5b\\x5d-\\x7e]|\\\\[\\x20-\\x7e])*"';
8
+ const listIdPattern = new RegExp('^[ \\t]*(?:(?:' + word + ')(?:[ \\t]+(?:' + word + '))*[ \\t]*)?<(' + atom + '(?:\\.' + atom + ')+)>[ \\t]*$');
9
+ const messageIdPattern = new RegExp('^<' + dotAtom + '@(?:' + dotAtom + '|\\[[\\x21-\\x5a\\x5e-\\x7e]*\\])>$');
10
+ const controls = /[\r\n\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/;
11
+ const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
12
+ const mailbox = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
13
+ const fields = new Map([
14
+ ['list-id', ['listId', 'invalid_list_id']],
15
+ ['list-archive', ['listArchive', 'invalid_list_archive']],
16
+ ['list-help', ['listHelp', 'invalid_list_help']],
17
+ ['in-reply-to', ['inReplyTo', 'invalid_in_reply_to']],
18
+ ['references', ['references', 'invalid_references']],
19
+ ]);
20
+
21
+ /** Validate a supported list/thread header and return its relay field, or null. */
22
+ export function rerListHeaderField(name, value) {
23
+ const mapping = fields.get(name.toLowerCase());
24
+ if (!mapping) return null;
25
+ const [field, code] = mapping;
26
+ if (typeof value !== 'string' || value.length > 2000 || controls.test(value)) fail(code);
27
+ if (field === 'listId') {
28
+ const match = listIdPattern.exec(value);
29
+ if (!match || match[1].length > 255) fail(code);
30
+ } else if (field === 'listArchive' || field === 'listHelp') {
31
+ const entries = value.match(listEntries);
32
+ if (!entries) fail(code);
33
+ for (const entry of entries.slice(1).filter(Boolean)) {
34
+ let url;
35
+ try { url = new URL(entry); } catch { fail(code); }
36
+ if (/^https:\/\//i.test(entry) && url.protocol === 'https:' && url.hostname && !entry.includes('\\')) continue;
37
+ if (url.protocol !== 'mailto:' || !mailbox.test(url.pathname)) fail(code);
38
+ }
39
+ } else {
40
+ const ids = field === 'references' ? value.split(/ +/) : [value];
41
+ if (!value || ids.length > (field === 'references' ? 20 : 1) || ids.some(id => !messageIdPattern.test(id))) fail(code);
42
+ }
43
+ return field;
44
+ }
@@ -1,6 +1,7 @@
1
1
  import { fail } from './errors.mjs';
2
2
  import { validateMessagingConfig, validateMessagingResult, identity, rfc3339 } from '../adapters/messaging/contract.mjs';
3
3
  import { validateRerConfig, validateRerMessage } from '../adapters/rer/contract.mjs';
4
+ import { rerListHeaderField } from '../adapters/rer/list-headers.mjs';
4
5
 
5
6
  const providers = ['sendgrid', 'resend', 'cloudflare', 'rer', 'development', 'messaging'];
6
7
  const controls = /[\x00-\x1f\x7f]/;
@@ -62,7 +63,7 @@ export function parseAddress(value) {
62
63
  return name ? { email, name } : { email };
63
64
  }
64
65
 
65
- export function validateHeaders(headers = {}) {
66
+ export function validateHeaders(headers = {}, provider) {
66
67
  record(headers, 'invalid_headers');
67
68
  const names = new Set();
68
69
  for (const name of Reflect.ownKeys(headers)) {
@@ -70,6 +71,8 @@ export function validateHeaders(headers = {}) {
70
71
  const lower = name.toLowerCase();
71
72
  if (reservedHeaders.has(lower) || names.has(lower)) fail('reserved_or_duplicate_header');
72
73
  names.add(lower);
74
+ // These relay grammars allow separator tabs and report field-specific errors.
75
+ if (provider === 'rer' && rerListHeaderField(lower, headers[name])) continue;
73
76
  bounded(headers[name], 8192, 'invalid_header_value', true);
74
77
  }
75
78
  return headers;
@@ -128,7 +131,7 @@ export function validateMessage(message, provider) {
128
131
  bounded(message.subject, 998, 'invalid_subject', true);
129
132
  for (const field of ['text', 'html']) if (message[field] !== undefined && typeof message[field] !== 'string') fail('invalid_body');
130
133
  if (!message.text?.trim() && !message.html?.trim()) fail('body_required');
131
- if (message.headers !== undefined) validateHeaders(message.headers);
134
+ if (message.headers !== undefined) validateHeaders(message.headers, provider);
132
135
  if (message.attachments !== undefined) {
133
136
  if (!Array.isArray(message.attachments)) fail('invalid_attachments');
134
137
  for (const attachment of message.attachments) validateAttachment(attachment);
@@ -26,7 +26,7 @@ export function createMailSender(config, dependencies = {}) {
26
26
  validateMessage(message, configured.provider);
27
27
  let snapshot;
28
28
  try { snapshot = structuredClone(message); } catch { fail('invalid_message'); }
29
- const prepared = applyUnsubscribeHeaders(snapshot);
29
+ const prepared = applyUnsubscribeHeaders(snapshot, configured.provider);
30
30
  validateMessage(prepared, configured.provider);
31
31
  return validateSendResult(await transport(configured, prepared, host), configured.provider);
32
32
  },
@@ -104,8 +104,8 @@ function addOrCheckHeader(headers, name, expected) {
104
104
  headers[name] = expected;
105
105
  }
106
106
 
107
- function assemble(message) {
108
- validateMessage(message);
107
+ function assemble(message, provider) {
108
+ validateMessage(message, provider);
109
109
 
110
110
  const headers = { ...(message.headers ?? {}) };
111
111
  validateExistingListHeaders(headers);
@@ -127,14 +127,14 @@ function assemble(message) {
127
127
 
128
128
  const prepared = { ...message, headers };
129
129
  delete prepared.unsubscribe;
130
- validateMessage(prepared);
130
+ validateMessage(prepared, provider);
131
131
  return prepared;
132
132
  }
133
133
 
134
134
  /** Return a copy with validated unsubscribe headers assembled and the request consumed. */
135
- export function applyUnsubscribeHeaders(message) {
135
+ export function applyUnsubscribeHeaders(message, provider) {
136
136
  try {
137
- return assemble(message);
137
+ return assemble(message, provider);
138
138
  } catch (error) {
139
139
  if (error instanceof MailValidationError) throw error;
140
140
  fail('invalid_message');
@@ -0,0 +1,3 @@
1
+ export function normalizeEmail(value: unknown): string;
2
+ export function emailDomain(email: string): string;
3
+ export function safeReturnTo(value: unknown, fallback?: string): string;
@@ -48,8 +48,12 @@ accept independent project and repository inputs, retain strict reference checks
48
48
  and cannot receive this INFO classification.
49
49
 
50
50
  It installs frozen dependencies and runs `pnpm run verify` in a disposable clone
51
- of the exact selected commit, then scans the original repository's complete
52
- local-ref history with publish-check. A verify that mutates tracked source fails.
51
+ of the exact selected commit, then scans source history with publish-check.
52
+ When the checkout's origin is a confirmed public repository, the scanner uses
53
+ that source repository's advertised default branch and published tags as its public
54
+ base. Already-public findings are classified pre-existing; unpublished content
55
+ still blocks. Other sources retain complete local-ref history inspection.
56
+ A verify that mutates tracked source fails.
53
57
  When declared in the committed package, it runs `pnpm run verify:release` after
54
58
  default verification succeeds. Keep capacity, maximum-size memory and load cases
55
59
  in that release-only script. Both dry runs and releases require it to pass;
@@ -120,6 +124,25 @@ the source tag. Release notes pass the same content rules as the public tree.
120
124
 
121
125
  ## Releasing a seed that is already public
122
126
 
127
+ Release the exact green source commit with the ordinary command:
128
+
129
+ ```sh
130
+ pnpm rer release --repo /path/to/public-seed --ref GREEN_COMMIT \
131
+ --project "$PUBLIC_PROJECT" --repository "$PUBLIC_REPOSITORY" \
132
+ --tag v1.2.3 --output /path/to/new-release-directory
133
+ ```
134
+
135
+ Both source-history and staged-annotation scans receive the repository's public
136
+ base. Public annotated tags, including the previous release, are fetched into
137
+ the scanner's isolated namespace and recognized by their exact object identity.
138
+ The selected source commit and final tag target are preserved.
139
+
140
+ Use `--public-base GIT_URL#BRANCH` to select public history explicitly, including
141
+ when the checkout has no public origin. The scanner fetches that remote branch
142
+ and its public tags; local refs and commit IDs cannot establish public history.
143
+ This option applies to exact-source releases and cannot be combined with
144
+ `--onto` or `--clean-history`.
145
+
123
146
  Use `--onto` to create one public child of the destination's current branch from
124
147
  an independently developed private source tree:
125
148
 
@@ -7,14 +7,16 @@ import { publicExport, cleanHistory, annotate, releaseMessage } from './export.m
7
7
  import { publicationScan, validatePublicationDestination } from './publication-scan.mjs';
8
8
  import { publicDestination, requirePublicDestination, remotePublicHead, publicChild, assertPublicTree } from './public-child.mjs';
9
9
  import { writePublishHandoff } from './publish-handoff.mjs';
10
+ import { releasePublicBase } from './public-base.mjs';
10
11
 
11
12
  // Failure output belongs to the private receipt, not the site preview.
12
13
  const withoutOutput = gates => JSON.parse(JSON.stringify(gates, (key, value) => key === 'output' ? undefined : value));
13
14
 
14
- export async function release({ repo, ref = 'HEAD', output, project, repository, dryRun = false, tag = false, cleanHistory: clean = false, envFile, onto }) {
15
+ export async function release({ repo, ref = 'HEAD', output, project, repository, dryRun = false, tag = false, cleanHistory: clean = false, envFile, onto, publicBase }) {
15
16
  if (!/^[\w.-]+\/[\w.-]+$/.test(project ?? '')) throw Error('--project must be publisher/name');
16
17
  if (!/^https:\/\/github\.com\/[\w.-]+\/[\w.-]+(?:\.git)?$/i.test(repository ?? '')) throw Error('--repository must be a credential-free GitHub repository URL');
17
18
  if (onto !== undefined && clean) throw Error('--onto and --clean-history are mutually exclusive');
19
+ if (publicBase !== undefined && (onto !== undefined || clean)) throw Error('--public-base selects exact-source history; cannot combine with export history modes');
18
20
  const preview = dryRun || !tag;
19
21
  const source = inspectSource(repo, ref);
20
22
  validatePublicationDestination(source, project, repository);
@@ -25,6 +27,7 @@ export async function release({ repo, ref = 'HEAD', output, project, repository,
25
27
  // Validate metadata before installs or output writes.
26
28
  catalogue(source, project, repository, { verify: { status: 'PENDING' }, publishCheck: { status: 'PENDING' } }, preview);
27
29
  const destination = onto !== undefined ? publicDestination(onto, repository, source.repo) : null;
30
+ const scanBase = destination?.spec ?? (clean ? undefined : releasePublicBase(source, publicBase));
28
31
  let publicParent = null;
29
32
  if (destination) {
30
33
  requirePublicDestination(destination);
@@ -54,12 +57,13 @@ export async function release({ repo, ref = 'HEAD', output, project, repository,
54
57
  verify.status = 'FAIL'; verify.reason = 'verify-changed-tracked-source';
55
58
  }
56
59
  // Explicit clean-history publication scans every byte and ref of the actual
57
- // parentless export. Ordinary releases retain the private full-history gate.
60
+ // parentless export. Exact-source releases classify against public history
61
+ // when available, retaining the selected source commit and tag identity.
58
62
  const exported = destination ? publicChild(source, temporary, destination, publicParent, Boolean(tag))
59
63
  : clean ? cleanHistory(source, temporary, Boolean(tag)) : null;
60
64
  const scanRepo = exported?.repo ?? source.repo;
61
65
  const scanRef = exported?.commit ?? source.commit;
62
- let report = publicationScan(scanRepo, scanRef, envFile, project, repository, destination?.spec);
66
+ let report = publicationScan(scanRepo, scanRef, envFile, project, repository, scanBase);
63
67
  if (!exported && tag && report.status === 'PASS') {
64
68
  // Stage the exact final annotation before any source tag or distributable
65
69
  // artifact exists, in a fresh clone of the selected commit: verification
@@ -70,7 +74,7 @@ export async function release({ repo, ref = 'HEAD', output, project, repository,
70
74
  git(candidate, 'checkout', '--detach', source.commit);
71
75
  const staged = git(candidate, 'tag', '--list', source.tag).toString().trim();
72
76
  annotate(candidate, source, source.commit, staged ? `${source.tag}-publication-candidate` : source.tag);
73
- const annotations = publicationScan(candidate, source.commit, envFile, project, repository);
77
+ const annotations = publicationScan(candidate, source.commit, envFile, project, repository, scanBase);
74
78
  report.annotationInspection = annotations;
75
79
  if (annotations.status !== 'PASS') {
76
80
  report.status = annotations.status;
@@ -0,0 +1,29 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { resolve } from 'node:path';
3
+ import { git } from './source.mjs';
4
+ import { publicGit } from './public-git.mjs';
5
+
6
+ const identity = url => /^(?:https:\/\/github\.com\/|ssh:\/\/git@github\.com\/|git@github\.com:)([\w.-]+\/[\w.-]+)$/i
7
+ .exec(url)?.[1].replace(/\.git$/i, '');
8
+
9
+ // Publication targets do not establish whether the source is already public.
10
+ // Infer a base from the source repository's own confirmed public origin.
11
+ export function releasePublicBase(source, explicit) {
12
+ if (explicit !== undefined) return explicit;
13
+ if (!git(source.repo, 'remote').toString().trim().split('\n').includes('origin')) return undefined;
14
+ const origin = git(source.repo, 'config', '--get', 'remote.origin.url').toString().trim();
15
+ const sourceIdentity = identity(origin);
16
+ if (!sourceIdentity) return undefined;
17
+ const repository = `https://github.com/${sourceIdentity}`;
18
+ const script = 'import sys; sys.path.insert(0, sys.argv[1]); from github_refs import GitHubResolver; print(GitHubResolver().resolve(sys.argv[2]))';
19
+ const visibility = execFileSync('python3', ['-B', '-c', script,
20
+ resolve(import.meta.dirname, '../../scripts/publish-check'), sourceIdentity],
21
+ { stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim();
22
+ if (['private', 'missing'].includes(visibility)) return undefined;
23
+ if (visibility !== 'public') throw Error('Source repository visibility could not be established');
24
+ const advertised = publicGit(source.repo, ['--git-dir=/dev/null', 'ls-remote', '--symref', '--', repository, 'HEAD']).toString();
25
+ const branches = [...advertised.matchAll(/^ref: refs\/heads\/(.+)\tHEAD$/gm)].map(match => match[1]);
26
+ if (branches.length !== 1) throw Error('Public source default branch is missing or ambiguous');
27
+ publicGit(source.repo, ['check-ref-format', `refs/heads/${branches[0]}`]);
28
+ return `${repository}#${branches[0]}`;
29
+ }
@@ -0,0 +1,34 @@
1
+ import {open} from 'node:fs/promises';
2
+ import {join,relative} from 'node:path';
3
+ import {stateFiles} from './state-files.mjs';
4
+
5
+ // A launch lock excludes cooperating commands. When process inspection fails,
6
+ // SQLite can still positively identify a conflicting transaction without a
7
+ // process-table walk. This cannot detect idle file handles or WAL readers.
8
+ export async function sqliteCustody(root) {
9
+ for(const path of await stateFiles(join(root,'.wrangler/state'))){
10
+ // Sidecars are not databases; state can disappear after this snapshot.
11
+ if(/-(?:wal|shm|journal)$/.test(path))continue;
12
+ let file;
13
+ try{file=await open(path,'r');}catch(error){if(error.code==='ENOENT')continue;throw error;}
14
+ let sqlite;
15
+ try{
16
+ const header=Buffer.alloc(16),{bytesRead}=await file.read(header,0,16,0);
17
+ sqlite=bytesRead===16&&header.toString()==='SQLite format 3\0';
18
+ }finally{await file.close();}
19
+ if(!sqlite)continue;
20
+ // Lazy import keeps the database engine out of early launcher registration.
21
+ const {DatabaseSync}=await import('node:sqlite');
22
+ let db;
23
+ try{
24
+ // This must be a writable connection: read-only WAL transactions do not
25
+ // exclude writers. Closing it may checkpoint idle WAL, so never use it for
26
+ // demo, whose canonical owner-state bytes must remain unchanged.
27
+ db=new DatabaseSync(path);
28
+ db.exec('PRAGMA busy_timeout=0; BEGIN EXCLUSIVE; ROLLBACK;');
29
+ }catch(error){
30
+ if(error.errcode===5||error.errcode===6)return relative(root,path);
31
+ throw new Error('Could not check the local SQLite lock. Preserve local state and resolve the database error before retrying.');
32
+ }finally{db?.close();}
33
+ }
34
+ }
@@ -32,19 +32,19 @@ export async function stopOwnedGroup(child, signal, ownership) {
32
32
  const stopped = new Map()
33
33
  let lastGroup = []
34
34
 
35
- const snapshot = async (limit = deadline) => {
36
- const current = await inspect(Math.min(inspectionMs, limit - Date.now()))
37
- if (Date.now() >= limit) throw new Error('Process inspection deadline expired.')
35
+ // Every inspection gets its full bound (and process-table's extension): a
36
+ // slow but complete read is still a valid observation. The cleanup deadline
37
+ // ends the loop below, and only processes observed alive at it fail cleanup.
38
+ const snapshot = async () => {
39
+ const current = await inspect(inspectionMs)
38
40
  lastGroup = current
39
41
  return current
40
42
  }
41
- const send = async (member, name, requireLeaf = true, limit = deadline) => {
43
+ const send = async (member, name, requireLeaf = true) => {
42
44
  // Recheck both identity and children immediately before each numeric PID
43
45
  // signal. Never send a negative/group signal that can kill parents first.
44
- const timeout = Math.min(inspectionMs, limit - Date.now())
45
- const current = requireLeaf ? await readProcessTable(timeout) : []
46
- const target = await readProcessIdentity(member.pid, Math.min(inspectionMs, limit - Date.now()))
47
- if (Date.now() >= limit) throw new Error('Process inspection deadline expired.')
46
+ const current = requireLeaf ? await readProcessTable(inspectionMs) : []
47
+ const target = await readProcessIdentity(member.pid, inspectionMs)
48
48
  if (!sameProcess(member, target) || isZombie(target)) return false
49
49
  if (requireLeaf && !isLeaf(target, current)) return false
50
50
  if (name === 'SIGKILL' && !isStopped(target)) return false
@@ -122,9 +122,8 @@ export async function stopOwnedGroup(child, signal, ownership) {
122
122
  } finally {
123
123
  // Inspection/signalling failures must not leave a process suspended.
124
124
  const errors = []
125
- const resumeDeadline = Math.min(deadline + 1000, Date.now() + 1000)
126
125
  for (const member of stopped.values()) {
127
- try { await send(member, 'SIGCONT', false, resumeDeadline) } catch (error) { errors.push(error) }
126
+ try { await send(member, 'SIGCONT', false) } catch (error) { errors.push(error) }
128
127
  }
129
128
  // Preserve the original failure while reporting a failure to resume an owned process.
130
129
  // eslint-disable-next-line no-unsafe-finally
@@ -215,9 +215,7 @@ export async function localProcessConflict(root,savedOrigin,context=inspectionCo
215
215
  return {pid:held.pid,command:command||'process',reason:'the canonical local state'};
216
216
  }
217
217
  }
218
- let all;
219
- try{all=await listeners(undefined,context);}
220
- catch(error){if(!uncheckedInspection(error)||context.verb==='reset')throw error;return;}
218
+ const all=await listeners(undefined,context);
221
219
  const cwdRecords=process.platform!=='linux'&&all.length?records(await run(lsof,['-nP','-b','-w','-a','-p',all.map(row=>row.pid).join(','),'-d','cwd','-Fpn'],context)):[];
222
220
  let port;try{const url=new URL(savedOrigin);port=Number(url.port||80);}catch{}
223
221
  for(const row of all){
@@ -228,6 +226,9 @@ export async function localProcessConflict(root,savedOrigin,context=inspectionCo
228
226
  // A listener running somewhere else is another program's or another checkout's
229
227
  // server: it does not use this checkout's state, so it only blocks the port.
230
228
  if(onSavedPort&&places.length&&!places.some(place=>place===root||place.startsWith(root+'/')))continue;
231
- if(onSavedPort||inCheckout)return {pid:row.pid,command:await processName(row.pid,context),reason:onSavedPort?`saved local port ${port}`:'this checkout'};
229
+ if(onSavedPort||inCheckout){
230
+ let command;try{command=await processName(row.pid,context);}catch(error){if(!uncheckedInspection(error))throw error;}
231
+ return {pid:row.pid,command:command||'process',reason:onSavedPort?`saved local port ${port}`:'this checkout'};
232
+ }
232
233
  }
233
234
  }
@@ -3,7 +3,7 @@ import { readFile } from 'node:fs/promises'
3
3
  import { randomUUID } from 'node:crypto'
4
4
  import { promisify } from 'node:util'
5
5
  import { setTimeout as delay } from 'node:timers/promises'
6
- import { inspectionMs, readProcessIdentity, readProcessTable, sameBirth } from './process-table.mjs'
6
+ import { extensionMs, inspectionMs, readProcessIdentity, readProcessTable, sameBirth } from './process-table.mjs'
7
7
 
8
8
  const execute = promisify(execFile)
9
9
  const scopes = new WeakMap()
@@ -35,7 +35,9 @@ export function processScopeEnvironment(scope, environment) {
35
35
  }
36
36
  export async function scopedProcesses(scope, table, timeout = inspectionMs, previous = []) {
37
37
  if (!scopes.has(scope)) throw new Error('Unknown process scope.')
38
- const inspectionEnd = Date.now() + timeout
38
+ // Reading environments is the slowest inspection on a busy Mac; it shares
39
+ // process-table's bounded extension rather than failing at the ordinary bound.
40
+ const inspectionEnd = Date.now() + timeout + extensionMs
39
41
  // Inspect same-user candidates in one bounded call. A child can change cwd,
40
42
  // detach and lose its parent between captures; the exclusive inherited marker
41
43
  // still proves ownership. Arguments/environment never leave this function.
@@ -52,7 +54,7 @@ export async function scopedProcesses(scope, table, timeout = inspectionMs, prev
52
54
  && (member.uid === undefined || member.uid === process.getuid())
53
55
  )
54
56
  if (!candidates.length) return []
55
- const environments = process.platform === 'linux' ? new Map() : await readEnvironments(candidates.map(member => member.pid), timeout)
57
+ const environments = process.platform === 'linux' ? new Map() : await readEnvironments(candidates.map(member => member.pid), Math.max(1, inspectionEnd - Date.now()))
56
58
  const matches = []
57
59
  inspectCandidate: for (const member of candidates) {
58
60
  let environment
@@ -13,14 +13,26 @@ export const inspectionMs = 5000
13
13
  // launch whose own processes are accounted for.
14
14
  const transient = (message) => Object.assign(new Error(message), { transient: true })
15
15
  const retryMs = 10
16
- async function withinBound(timeout, attempt) {
17
- const deadline = Date.now() + timeout
18
- for (;;) {
19
- try { return await attempt(Math.max(1, deadline - Date.now())) } catch (error) {
20
- if (!error.transient || Date.now() + retryMs >= deadline) throw error
21
- await delay(retryMs)
16
+ // A busy machine can starve ps, or this process, past the ordinary bound while
17
+ // the read itself is healthy; its finished output can even be queued behind
18
+ // the deadline timer. Such a read is not a failure of the caller's work: the
19
+ // same in-flight read is awaited for a bounded extension, with a warning, and
20
+ // only an inspection that also outlives the extension fails.
21
+ export const extensionMs = 25000
22
+ async function withinBound(timeout, attempt, extension = extensionMs) {
23
+ const deadline = Date.now() + timeout + extension
24
+ const slow = setTimeout(() => process.emitWarning(
25
+ `Process inspection is taking longer than ${timeout} ms on a busy machine; waiting up to ${extension} ms more.`,
26
+ { code: 'RER_PROCESS_INSPECTION_SLOW' }), timeout)
27
+ slow.unref()
28
+ try {
29
+ for (;;) {
30
+ try { return await attempt(Math.max(1, deadline - Date.now())) } catch (error) {
31
+ if (!error.transient || Date.now() + retryMs >= deadline) throw error
32
+ await delay(retryMs)
33
+ }
22
34
  }
23
- }
35
+ } finally { clearTimeout(slow) }
24
36
  }
25
37
  export const validPid = (pid) => Number.isSafeInteger(pid) && pid > 0 && pid <= 0x7fffffff
26
38
 
@@ -103,7 +115,7 @@ async function processGroup(pgid, signal) {
103
115
  // cleanup slower than the existing bounded shutdown contract.
104
116
  export async function readProcessIdentity(pid, timeout = inspectionMs) {
105
117
  if (!validPid(pid) || !(timeout > 0)) throw new Error('Invalid process identity deadline.')
106
- if (process.platform === 'linux') return linuxProcess(pid, undefined, AbortSignal.timeout(timeout))
118
+ if (process.platform === 'linux') return withinBound(timeout, (remaining) => linuxProcess(pid, undefined, AbortSignal.timeout(remaining)))
107
119
  return withinBound(timeout, (remaining) => psIdentity(pid, remaining))
108
120
  }
109
121
 
@@ -122,7 +134,7 @@ async function psIdentity(pid, timeout) {
122
134
 
123
135
  // `read` is the process edge: it returns the table or throws; tests supply a
124
136
  // failing one.
125
- export async function readProcessTable(timeout = inspectionMs, pgid, read = processGroup) {
137
+ export async function readProcessTable(timeout = inspectionMs, pgid, read = processGroup, extension = extensionMs) {
126
138
  if (!(timeout > 0)) throw new Error('Process inspection deadline expired.')
127
139
  return withinBound(timeout, async (remaining) => {
128
140
  const controller = new AbortController()
@@ -135,7 +147,7 @@ export async function readProcessTable(timeout = inspectionMs, pgid, read = proc
135
147
  }, remaining)
136
148
  })])
137
149
  } finally { clearTimeout(timer) }
138
- })
150
+ }, extension)
139
151
  }
140
152
 
141
153
  export const sameBirth = (left, right) => validPid(left?.pid) && left.pid === right?.pid && left.birth === right?.birth
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.25",
3
+ "version": "0.2.27",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -20,6 +20,7 @@ class History:
20
20
  self.repo = str(Path(repo).resolve())
21
21
  self.env = git_environment()
22
22
  self.public_commit = self.public_label = None
23
+ self.public_tags = set()
23
24
  self.reader = None
24
25
  self.commit = self.run('rev-parse', '--verify', '--end-of-options', ref + '^{commit}').decode().strip()
25
26
  if self.run('rev-parse', '--is-shallow-repository').strip() != b'false':
@@ -27,15 +28,19 @@ class History:
27
28
  if public_base is not None:
28
29
  url, branch = parse(public_base, self.repo)
29
30
  try:
30
- self.run('fetch', '--quiet', '--no-tags', '--no-recurse-submodules', '--no-write-fetch-head', '--',
31
- url, 'refs/heads/' + branch + ':refs/publish-check/public/base')
31
+ self.run('fetch', '--quiet', '--prune', '--no-tags', '--no-recurse-submodules', '--no-write-fetch-head', '--',
32
+ url, 'refs/heads/' + branch + ':refs/publish-check/public/base',
33
+ 'refs/tags/*:refs/publish-check/public/tags/*')
32
34
  self.public_commit = self.run('rev-parse', 'refs/publish-check/public/base^{commit}').decode().strip()
33
35
  self.public_label = display(url) + '#' + branch
36
+ self.public_tags = set(self.run('for-each-ref', '--format=%(objectname)',
37
+ 'refs/publish-check/public/tags').decode().splitlines())
34
38
  except InspectionError:
35
39
  raise PublicBaseError('public-base.fetch-failed') from None
36
- self.revisions = [self.commit, self.public_commit] if public_base else ['--all', self.commit]
40
+ public_revisions = [self.public_commit, *sorted(self.public_tags)] if public_base else []
41
+ self.revisions = [self.commit, *public_revisions] if public_base else ['--all', self.commit]
37
42
  self.commits = self.run('rev-list', *self.revisions).decode().splitlines()
38
- self.public_commits = set(self.run('rev-list', self.public_commit).decode().splitlines()) if self.public_commit else set()
43
+ self.public_commits = set(self.run('rev-list', *public_revisions).decode().splitlines()) if public_base else set()
39
44
  self.trees = {}
40
45
  self.tree_ids = dict(line.split() for line in self.run('log', '--format=%H %T', *self.revisions).decode().splitlines())
41
46
  self.reader = subprocess.Popen(['git', '-C', self.repo, 'cat-file', '--batch'],
@@ -97,9 +102,12 @@ class History:
97
102
  for index in range(0, len(records) - 1, 2):
98
103
  if records[index]:
99
104
  yield 'commit', records[index].decode().strip(), records[index + 1]
100
- for record in self.run('for-each-ref', '--format=%(objecttype) %(objectname)', 'refs/tags').splitlines():
105
+ seen = set()
106
+ for record in self.run('for-each-ref', '--format=%(objecttype) %(objectname)', 'refs/tags',
107
+ 'refs/publish-check/public/tags').splitlines():
101
108
  kind, oid = record.decode().split()
102
- if kind == 'tag':
109
+ if kind == 'tag' and oid not in seen:
110
+ seen.add(oid)
103
111
  raw = self.run('cat-file', 'tag', oid)
104
112
  yield 'tag', oid, raw.partition(b'\n\n')[2]
105
113
 
@@ -161,7 +161,7 @@ def scan(repo, ref='HEAD', public_base=None, env_file=None, project=None, reposi
161
161
  report['history']['tagMessages' if message_type == 'tag' else 'commitMessages'] += 1
162
162
  rules, _ = text_rules(data, path, selected_context, resolver, hashes,
163
163
  commit_message=message_type == 'commit')
164
- scope = 'release-tree' if commit == history.commit else 'pre-existing' if commit in history.public_commits else 'newly-public'
164
+ scope = 'pre-existing' if commit in history.public_commits or commit in history.public_tags else 'release-tree' if commit == history.commit else 'newly-public'
165
165
  for rule in rules:
166
166
  add(rule, path, commit, scope=scope)
167
167
  messages.append(data + b'\n')