@runeditrun/rer 0.2.98 → 0.2.100

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 (78) hide show
  1. package/README.md +1 -1
  2. package/cli/README.md +88 -2
  3. package/cli/business/api.mjs +4 -2
  4. package/cli/business/bounded-waves.d.mts +23 -0
  5. package/cli/business/bounded-waves.mjs +57 -0
  6. package/cli/business/catchup-task.mjs +9 -2
  7. package/cli/business/command.mjs +16 -4
  8. package/cli/business/connect.mjs +39 -33
  9. package/cli/business/export-inputs.mjs +12 -2
  10. package/cli/business/export-task.mjs +9 -2
  11. package/cli/business/legacy-package.mjs +117 -0
  12. package/cli/business/local-commands.mjs +10 -1
  13. package/cli/business/package-transfer.mjs +127 -0
  14. package/cli/business/questions.mjs +2 -2
  15. package/cli/business/state.mjs +47 -5
  16. package/cli/business/supervisor.mjs +46 -7
  17. package/cli/business/vendor-door.mjs +86 -13
  18. package/cli/business/vendors/README.md +41 -2
  19. package/cli/business/vendors/common.mjs +81 -0
  20. package/cli/business/vendors/index.mjs +12 -2
  21. package/cli/business/vendors/zendesk-attachments.mjs +57 -0
  22. package/cli/business/vendors/zendesk-encode.mjs +133 -0
  23. package/cli/business/vendors/zendesk-guide.mjs +34 -0
  24. package/cli/business/vendors/zendesk-package.mjs +28 -0
  25. package/cli/business/vendors/zendesk-pages.mjs +61 -0
  26. package/cli/business/vendors/zendesk-people.mjs +7 -0
  27. package/cli/business/vendors/zendesk-store.mjs +154 -0
  28. package/cli/business/vendors/zendesk-tickets.mjs +21 -0
  29. package/cli/business/vendors/zendesk-token.mjs +31 -0
  30. package/cli/business/vendors/zendesk-transport.mjs +48 -0
  31. package/cli/business/vendors/zendesk.mjs +61 -0
  32. package/cli/deploy-command.mjs +5 -4
  33. package/cli/deploy-inputs.mjs +1 -1
  34. package/cli/deploy.mjs +6 -4
  35. package/cli/production-auth.mjs +16 -3
  36. package/cli/production-mail.mjs +18 -1
  37. package/cli/site.mjs +1 -1
  38. package/global/CHANGELOG.md +42 -0
  39. package/global/README.md +1 -1
  40. package/modules/deploy-kit/README.md +10 -0
  41. package/modules/deploy-kit/provider.mjs +18 -2
  42. package/modules/import-kit/src/d1/jobs.ts +3 -1
  43. package/modules/import-kit/src/d1/retirement-scope.ts +2 -2
  44. package/modules/import-kit/src/d1/retirement-tally.ts +1 -0
  45. package/modules/import-kit/src/d1/retirement.ts +15 -5
  46. package/modules/import-kit/src/d1/types.ts +3 -0
  47. package/modules/import-kit/src/d1/wave-boundary.ts +33 -0
  48. package/modules/import-kit/src/d1/wave-retirement.ts +47 -0
  49. package/modules/import-kit/src/door/ADOPTING.md +37 -0
  50. package/modules/import-kit/src/door/README.md +66 -0
  51. package/modules/import-kit/src/door/admission.ts +13 -3
  52. package/modules/import-kit/src/door/authority.ts +1 -0
  53. package/modules/import-kit/src/door/failures.ts +1 -1
  54. package/modules/import-kit/src/door/grants.ts +19 -5
  55. package/modules/import-kit/src/door/handler.ts +12 -1
  56. package/modules/import-kit/src/door/index.ts +6 -0
  57. package/modules/import-kit/src/door/package.ts +121 -0
  58. package/modules/import-kit/src/door/planning.ts +44 -19
  59. package/modules/import-kit/src/door/protocol.ts +4 -2
  60. package/modules/import-kit/src/door/schema.ts +2 -0
  61. package/modules/import-kit/src/door/store.ts +2 -2
  62. package/modules/import-kit/src/door/types.ts +24 -1
  63. package/modules/import-kit/src/door/waves/README.md +158 -0
  64. package/modules/import-kit/src/door/waves/digests.ts +38 -0
  65. package/modules/import-kit/src/door/waves/inventory.ts +84 -0
  66. package/modules/import-kit/src/door/waves/journal.ts +19 -0
  67. package/modules/import-kit/src/door/waves/lifecycle.ts +242 -0
  68. package/modules/import-kit/src/door/waves/preparation.ts +190 -0
  69. package/modules/import-kit/src/door/waves/schema.ts +73 -0
  70. package/modules/import-kit/src/door/waves/store.ts +112 -0
  71. package/modules/import-kit/src/door/waves/types.ts +76 -0
  72. package/onboarding/adapter.mjs +7 -1
  73. package/onboarding/operator-installer.mjs +3 -1
  74. package/package.json +1 -1
  75. package/scripts/publish-check/README.md +20 -0
  76. package/scripts/publish-check/diagnostics.mjs +73 -0
  77. package/scripts/publish-check/slots.py +37 -7
  78. package/scripts/publish-check.mjs +64 -37
@@ -0,0 +1,31 @@
1
+ import { fail, object } from './common.mjs';
2
+
3
+ export const SCOPES = Object.freeze(['tickets:read', 'users:read', 'organizations:read', 'hc:read', 'ticket_attachments:read']);
4
+ export function clientCredential(key) {
5
+ let value;
6
+ try { value = JSON.parse(key); } catch { fail('INVALID_KEY'); }
7
+ if (!value || Array.isArray(value) || typeof value !== 'object' || Object.keys(value).sort().join() !== 'client_id,client_secret'
8
+ || Object.values(value).some(part => typeof part !== 'string' || !part || part.length > 4096 || /[\u0000-\u0020\u007f]/u.test(part))) fail('INVALID_KEY');
9
+ return value;
10
+ }
11
+ export function originOf(inputs) {
12
+ const account = inputs?.account;
13
+ if (!account || Object.keys(account).join() !== 'subdomain' || typeof account.subdomain !== 'string'
14
+ || !/^(?!-)[a-z0-9-]{1,63}(?<!-)$/u.test(account.subdomain)) fail('INVALID_INPUT');
15
+ if (!['snapshot', 'sweep', 'census', 'retire'].includes(inputs.mode ?? 'snapshot')
16
+ || inputs.highWater !== undefined && (!Number.isSafeInteger(inputs.highWater) || inputs.highWater < 0)) fail('INVALID_INPUT');
17
+ return `https://${account.subdomain}.zendesk.com`;
18
+ }
19
+ export async function mintToken(request, key) {
20
+ const credential = clientCredential(key);
21
+ const { body } = await request('/oauth/tokens', { method: 'POST', json: { ...credential, grant_type: 'client_credentials', scope: SCOPES.join(' '), expires_in: 1800 }, authenticated: false, tokenResponse: true });
22
+ object(body);
23
+ if (typeof body.access_token !== 'string' || !body.access_token || body.access_token.length > 8192 || /[\u0000-\u0020\u007f]/u.test(body.access_token)
24
+ || body.token_type?.toLowerCase() !== 'bearer') fail('INVALID_TOKEN');
25
+ request.authorize(body.access_token);
26
+ const current = await request('/api/v2/oauth/tokens/current', { tokenResponse: true });
27
+ const scopes = current.body?.token?.scopes ?? current.body?.scopes;
28
+ const values = typeof scopes === 'string' ? scopes.split(/\s+/u) : scopes;
29
+ if (!Array.isArray(values) || !values.length || values.some(scope => !['read', ...SCOPES].includes(scope))) fail('KEY_NOT_READ_ONLY');
30
+ return body.access_token;
31
+ }
@@ -0,0 +1,48 @@
1
+ import { isIP } from 'node:net';
2
+ import { lookup } from 'node:dns/promises';
3
+ import { pacedClient, fail } from './common.mjs';
4
+ import { clientCredential, originOf, mintToken } from './zendesk-token.mjs';
5
+
6
+ const routes = /^\/(?:oauth\/tokens|api\/v2\/(?:oauth\/tokens\/current|incremental\/(?:tickets\/cursor|users\/cursor|organizations)(?:\.json)?|tickets\/\d+\/comments(?:\.json)?|ticket_fields(?:\.json)?|help_center\/(?:[a-z]{2,3}(?:-[a-z0-9]{2,8})?\/articles\/\d+|(?:user_segments|categories|sections|articles)(?:\/\d+\/(?:translations|attachments))?(?:\.json)?)))$/u;
7
+ const queries = new Set(['start_time', 'per_page', 'cursor', 'page', 'page[size]', 'page[after]', 'include_inline_images']);
8
+ export const attachmentPath = path => /^\/(?:attachments\/|api\/v2\/attachments\/)/u.test(path);
9
+ const privateAddress = address => {
10
+ if (isIP(address) === 4) {
11
+ const [a,b] = address.split('.').map(Number);
12
+ return a === 0 || a === 10 || a === 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && (b === 168 || b === 0) || a === 100 && b >= 64 && b <= 127 || a >= 224 || a === 198 && (b === 18 || b === 19 || b === 51) || a === 203 && b === 0 || a === 192 && (b === 0 || b === 88);
13
+ }
14
+ // Only global unicast IPv6; mapped IPv4, loopback, multicast and ULA fail.
15
+ return !/^2[0-9a-f]{3}:/iu.test(address) || /^2001:(?:db8|0):/iu.test(address);
16
+ };
17
+ export async function publicLocation(value, origin) {
18
+ let url;
19
+ try { url = new URL(value, origin); } catch { return null; }
20
+ if (/^https:\/\/[^/?#]*:\d+(?:[/?#]|$)/iu.test(String(value)) || url.protocol !== 'https:' || url.port || url.username || url.password || url.hash || isIP(url.hostname) || url.hostname.startsWith('[')
21
+ || url.hostname.length > 253 || !url.hostname.split('.').every(part => /^(?!-)[a-z0-9-]{1,63}(?<!-)$/u.test(part)) || !url.hostname.includes('.')
22
+ || /\.(?:localhost|local|internal|invalid|test|home|lan)$/u.test(url.hostname) || /^\d+$/u.test(url.hostname.split('.').at(-1))) return null;
23
+ if (url.origin !== origin) {
24
+ let addresses;
25
+ try { addresses = await lookup(url.hostname, { all: true }); } catch { return null; }
26
+ if (!addresses.length || addresses.some(item => privateAddress(item.address))) return null;
27
+ }
28
+ return url;
29
+ }
30
+ export function pagination(value, origin, pathname, current) {
31
+ if (value === null) return null;
32
+ if (typeof value !== 'string' || !value || value.length > 4096) fail('INVALID_PAGINATION');
33
+ let next;
34
+ try { next = new URL(value, origin); } catch { fail('INVALID_PAGINATION'); }
35
+ if (next.origin !== origin || next.username || next.password || next.hash || next.pathname.replace(/\.json$/u, '') !== pathname.replace(/\.json$/u, '')
36
+ || [...next.searchParams.keys()].some(key => !queries.has(key) || next.searchParams.getAll(key).length !== 1) || next.href === new URL(current, origin).href) fail('INVALID_PAGINATION');
37
+ return pathname + next.search;
38
+ }
39
+ export async function transport({ inputs, key, signal, beforeRequest }) {
40
+ const origin = originOf(inputs), credentials = clientCredential(key);
41
+ const request = pacedClient({ origin, signal, beforeRequest, secrets: [key, credentials.client_id, credentials.client_secret], validate: async (url, options) => {
42
+ if (options.attachment) {
43
+ if (!await publicLocation(url.href, origin) || url.origin === origin && !attachmentPath(url.pathname)) fail('REFUSED_LOCATION');
44
+ } else if (url.origin !== origin || url.username || url.password || url.hash || !routes.test(url.pathname) || [...url.searchParams.keys()].some(key => !queries.has(key))) fail('INVALID_REFERENCE');
45
+ } });
46
+ await mintToken(request, key);
47
+ return { request, origin };
48
+ }
@@ -0,0 +1,61 @@
1
+ import { VendorExportError, fail } from './common.mjs';
2
+ import { originOf } from './zendesk-token.mjs';
3
+ import { transport } from './zendesk-transport.mjs';
4
+ import { ZendeskStore, materialize, ordered } from './zendesk-store.mjs';
5
+ import { tickets, comments } from './zendesk-tickets.mjs';
6
+ import { people } from './zendesk-people.mjs';
7
+ import { guide } from './zendesk-guide.mjs';
8
+ import { attachments } from './zendesk-attachments.mjs';
9
+ import { encodeSnapshot } from './zendesk-encode.mjs';
10
+
11
+ export { originOf } from './zendesk-token.mjs';
12
+ export { clientCredential } from './zendesk-token.mjs';
13
+ export { storePath, removeZendeskStores } from './zendesk-store.mjs';
14
+ /** The durable local snapshot is independent of door transfer. A new task
15
+ * starts a fresh snapshot or extends the complete retained store for a sweep;
16
+ * retries of the same task resume its last acknowledged page. */
17
+ export async function snapshot({ key, inputs, directory, runId, signal }) {
18
+ try {
19
+ const origin = originOf(inputs);
20
+ if (!directory || typeof runId !== 'string' || !runId || inputs.mode === 'retire') fail('INVALID_INPUT');
21
+ const store = await new ZendeskStore(directory).start();
22
+ const mode = inputs.mode ?? 'snapshot', baseline = await store.baseline();
23
+ if (mode === 'sweep' && store.state?.runId !== runId && (!baseline || inputs.highWater !== baseline.highWater)) fail('INVALID_HIGH_WATER');
24
+ if (store.state?.runId !== runId) {
25
+ if (mode === 'sweep' && store.state?.phase !== 'done') fail('INCOMPLETE_SNAPSHOT');
26
+ await store.reset({ phase: 'A', path: null, index: 0, attachmentIndex: 0, stage: 0, runId, origin, mode, inputHighWater: inputs.highWater ?? null,
27
+ exportedAt: new Date().toISOString(), bytes: mode === 'sweep' ? store.state.bytes : 0, requests: 0 }, { retain: mode === 'sweep' });
28
+ } else if (store.state.origin !== origin || store.state.mode !== mode || store.state.inputHighWater !== (inputs.highWater ?? null)) fail('INVALID_CHECKPOINT');
29
+ store.requestTotal = store.state.requests;
30
+ const ctx = { store, inputs, ...await transport({ inputs, key, signal, beforeRequest: async () => {
31
+ if (store.requestTotal >= 250_000) fail('REQUEST_LIMIT');
32
+ store.requestTotal++;
33
+ await store.commit(store.state);
34
+ } }) };
35
+ while (store.state.phase !== 'done') {
36
+ signal?.throwIfAborted();
37
+ const phase = store.state.phase;
38
+ if (phase === 'A') await tickets(ctx);
39
+ else if (phase === 'B') await comments(ctx);
40
+ else if (['C','D','E'].includes(phase)) await people(ctx);
41
+ else if (phase === 'F') await guide(ctx);
42
+ else if (['attachments', 'guide_attachments'].includes(phase)) await attachments(ctx);
43
+ else fail('INVALID_CHECKPOINT');
44
+ }
45
+ if (mode === 'census') {
46
+ const data = await materialize(store), unswept = [...data.tickets.values()].filter(({ row }) => row.generated_timestamp > (inputs.highWater ?? 0)).length;
47
+ const census = { format: 'rer-zendesk-census/1', tickets: { unique: data.tickets.size, deleted: [...data.tickets.values()].filter(({row}) => row.status === 'deleted' || row.deleted).length,
48
+ maxId: ordered(data.tickets.keys()).at(-1) ?? null }, unswept, highWater: data.highWater };
49
+ await store.write('census.json', census);
50
+ return { kind: 'census', census, store };
51
+ }
52
+ const result = await encodeSnapshot(store, { origin });
53
+ if (store.state.highWater !== result.census.highWater) await store.commit({ ...store.state, highWater: result.census.highWater });
54
+ await store.write('census.json', result.census);
55
+ await store.write('baseline.json', { highWater: result.census.highWater, head: store.head });
56
+ return { ...result, store };
57
+ } catch (error) {
58
+ if (error instanceof VendorExportError) throw error;
59
+ throw new VendorExportError(signal?.aborted ? 'CANCELLED' : 'INVALID_EXPORT');
60
+ }
61
+ }
@@ -1,4 +1,4 @@
1
- import { requireProductionAuth } from './production-auth.mjs';
1
+ import { requireProductionAuth, mailOffNotice } from './production-auth.mjs';
2
2
  import { observedReadiness } from '../modules/deploy-kit/readiness-window.mjs';
3
3
  import { readFile, readdir } from 'node:fs/promises';
4
4
  import { contained } from './project.mjs';
@@ -85,7 +85,8 @@ export async function deployCommand(context) {
85
85
  const production = await deployEnvironment({ root, options, declaration, setupManifest, settings, localOnlyKeys });
86
86
  const plan = result?.plan ?? (await owner(['local-plan', '--declaration', path, '--environment', options.environment ?? 'production', ...await deployProductArguments(root, deployProductDirectory(declaration))], production.env, { cwd: root, localOnlyKeys })).plan;
87
87
  result = { ...result, dryRun: true, deployed: false, providerChecked: false, plan };
88
- const { signIn } = await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed: context.setupSeed, adapter, declarationPath: path, options }) ?? {};
88
+ const { signIn, mailOff } = await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed: context.setupSeed, adapter, declarationPath: path, options }) ?? {};
89
+ if (mailOff) store.report(mailOffNotice(mailOff, options.environment ?? 'production'));
89
90
  const needed = [...new Set([...neededForRealDeploy({ declaration, env: production.env, signIn, secretsChecked: false }), ...(result.neededForRealDeploy ?? [])])];
90
91
  reportNeeded(store, needed);
91
92
  if (declaration.secrets?.some(item => item.binding === 'INSTALLATION_KEY')) store.report('deploy: INSTALLATION_KEY is a private generated binding; INSTALLATION_KEY_PREVIOUS is optional and managed only during rotation or an explicit retired-key reopen. Values are withheld.');
@@ -110,7 +111,7 @@ export async function deployCommand(context) {
110
111
  if (adapter.deploy.managesHostedOwner) { const result = await runHook(context); return options.dryRun ? dryRun(result) : result; }
111
112
  const productDirectory = deployProductDirectory(declaration);
112
113
  const production = await deployEnvironment({ root, options, declaration, setupManifest, settings, localOnlyKeys });
113
- await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed: context.setupSeed, adapter, declarationPath: path, options });
114
+ const { mailOff } = await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed: context.setupSeed, adapter, declarationPath: path, options }) ?? {};
114
115
  const sourceSha = options.dryRun ? undefined : committedSource(root, options);
115
116
  const ownerInput = await deployOwnerInput(context);
116
117
  const { env, supplied, generated, reused, rotated } = await deployEnvironment({ root, options, declaration, setupManifest, settings, localOnlyKeys });
@@ -180,7 +181,7 @@ export async function deployCommand(context) {
180
181
  const { setupToken: _, ...result } = deployed;
181
182
  return { ...result, ...(claimed ? { owner: claimed } : {}), dependenciesLinked: true };
182
183
  });
183
- if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${recorded ? '; the one-time setup token was removed' : ''}. Sign in by email at ${result.origin}.`);
184
+ if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${recorded ? '; the one-time setup token was removed' : ''}. ${mailOff ? mailOffNotice(mailOff, environment) : `Sign in by email at ${result.origin}.`}`);
184
185
  return result;
185
186
  } catch (error) {
186
187
  if (error instanceof DeployProductError) throw error;
@@ -79,7 +79,7 @@ export async function withDeployEnvironment(project, options, out, run) {
79
79
  const redaction = createRedaction();
80
80
  // Provider names are public enum values. Masking "resend" would also hide
81
81
  // RESEND_API_KEY in the preflight's instruction for the missing credential.
82
- const publicProvider = (key, value) => key === 'MAIL_PROVIDER' && /^(resend|sendgrid|cloudflare|rer|later)$/.test(value);
82
+ const publicProvider = (key, value) => key === 'MAIL_PROVIDER' && /^(resend|sendgrid|cloudflare|rer|later|none)$/.test(value);
83
83
  const supplied = Object.entries(env).filter(([key, value]) => saved[key] !== value);
84
84
  redaction.addSecrets([...kinds.privateValues(env), ...supplied.filter(([key, value]) => !kinds.shown(key, value) && !publicProvider(key, value)).map(([, value]) => value)]);
85
85
  const undeclared = declared.declaration ? kinds.undeclared(supplied.map(([key]) => key)) : [];
package/cli/deploy.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { requireProductionAuth } from './production-auth.mjs';
1
+ import { requireProductionAuth, mailOffNotice } from './production-auth.mjs';
2
+ import { mailOffSignInCommand } from './production-mail.mjs';
2
3
  import { readFile } from 'node:fs/promises';
3
4
  import { deployDiagnostic, migrationFailure, providerFailure, assetFailure } from '../modules/deploy-kit/diagnostics.mjs';
4
5
  import { shared } from './shared.mjs';
@@ -41,10 +42,10 @@ export function ownerDeploy(declarationPath, lifecycle) {
41
42
  const recordedState = change.length ? await deploymentRecord(root, options) : undefined;
42
43
  // A dry run reads the settings file too, so a refused key fails here and not only on the real deploy.
43
44
  if (options.dryRun && options.envFile) await deployEnvironment({ root, options, declaration, localOnlyKeys });
44
- let production, ownerInput, signIn;
45
+ let production, ownerInput, signIn, mailOff;
45
46
  if (!change.length) {
46
47
  production = await deployEnvironment({ root, options, declaration, setupManifest, settings: lifecycle.settings, localOnlyKeys });
47
- ({signIn} = await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed, adapter: lifecycle, declarationPath, options }) ?? {});
48
+ ({signIn, mailOff} = await requireProductionAuth({ root, declaration, env: production.env, setupManifest, setupSeed, adapter: lifecycle, declarationPath, options, mailOffSignIn: mailOffSignInCommand(adapter) ?? mailOffSignInCommand(lifecycle) }) ?? {});
48
49
  if (!options.dryRun) committedSource(root, options);
49
50
  ownerInput = await deployOwnerInput({ root, adapter, setupSeed, options, signal });
50
51
  await withoutLocalSettings(root, { settings: lifecycle.settings, operationToken }, () => lifecycle.run(root, {}, store, () => {}, signal, { verb: 'build', operationToken }));
@@ -132,7 +133,8 @@ export function ownerDeploy(declarationPath, lifecycle) {
132
133
  // The app claims the OWNER_EMAIL the Worker holds: the local one only when this run uploaded it.
133
134
  const email = declared(claim?.emailField);
134
135
  const named = !email ? '' : deployed.sentSecrets.includes(email.binding) && env[email.environment] ? ` (${env[email.environment]})` : ` (the ${email.binding} already set on the Worker)`;
135
- if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${named}; the one-time setup token was removed. Sign in by email at ${deployed.origin}.`);
136
+ if (claimed) store.report(`${claimed === 'claimed' ? 'Claimed the hosted owner' : 'The hosted owner was already claimed and was kept'}${named}; the one-time setup token was removed. ${mailOff ? mailOffNotice(mailOff, environment) : `Sign in by email at ${deployed.origin}.`}`);
137
+ else if (mailOff) store.report(mailOffNotice(mailOff, environment));
136
138
  return { deployed: true, origin: deployed.origin, ...(claimed ? { owner: claimed } : {}), dependenciesLinked: true };
137
139
  } catch (error) {
138
140
  if (error instanceof RerError) throw error;
@@ -2,16 +2,29 @@ import { refuse } from './errors.mjs';
2
2
  import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
3
3
  import { hookState } from './deploy-hook-record.mjs';
4
4
  import { savedSetupAnswers } from './setup-answers.mjs';
5
- import { productionMail } from './production-mail.mjs';
5
+ import { productionMail, mailOffSignInCommand } from './production-mail.mjs';
6
+
7
+ // What a deploy says when outbound email is deliberately off: the state and the way in.
8
+ export function mailOffNotice({ signIn }, environment = 'production') {
9
+ return `Outbound email is off (MAIL_PROVIDER=none): no mail credential is bound and this installation sends nothing. Sign in without mail with \`${signIn}\` for the ${environment} environment, from this checkout with the Cloudflare credentials that deployed it. Turn mail on later with rer deploy --environment ${environment} --set-secret and the provider inputs.`;
10
+ }
6
11
 
7
12
  // Validate the shared email/identity contract using the bindings that will reach
8
13
  // the runtime. Independently authored authentication remains the adapter's job.
9
- export async function requireProductionAuth({ root, declaration, env, setupManifest, setupSeed, adapter, declarationPath, options = {} }) {
14
+ // `mailOffSignIn` names the seed's mail-less sign-in command when the caller holds it apart
15
+ // from `adapter` (the owner deploy runs on the lifecycle, the seed declares on its command adapter).
16
+ export async function requireProductionAuth({ root, declaration, env, setupManifest, setupSeed, adapter, declarationPath, options = {}, mailOffSignIn: declaredSignIn = mailOffSignInCommand(adapter) }) {
10
17
  const dependencies = setupManifest?.dependencies?.filter(item => item.interface === 'oidc-provider@1') ?? [];
11
18
  const bindings = { ...declaration.vars };
12
19
  for (const secret of declaration.secrets ?? []) if (env[secret.environment]?.trim()) bindings[secret.binding] = env[secret.environment];
13
- const mail = await productionMail(root, declaration, env, declarationPath, adapter);
20
+ const mail = await productionMail(root, declaration, env, declarationPath, adapter, declaredSignIn);
14
21
  if (!setupManifest?.steps?.some(step => step.type === 'owner-claim') && !dependencies.length && !mail.declared) return;
22
+ // MAIL_PROVIDER=none is accepted only for a seed that declares how its owner signs in without mail.
23
+ const mailOffSignIn = mail.off ? declaredSignIn : undefined;
24
+ if (mailOffSignIn) {
25
+ if (mail.offConflicts.length) throw refuse(`MAIL_PROVIDER=none turns outbound email off, and ${mail.offConflicts.join(', ')} would bind a way to send. Remove ${mail.offConflicts.length > 1 ? 'them' : 'it'} or choose a mail provider. Nothing was built or uploaded.`);
26
+ return { mailOff: { signIn: mailOffSignIn } };
27
+ }
15
28
  if (mail.senderIssue && options.verb === 'deploy' && !(options.dryRun && mail.fixtureSender)) throw refuse(`${mail.senderIssue} Nothing was built or uploaded.`);
16
29
  if (mail.configured()) return;
17
30
  if (mail.canInspect && !options.dryRun && declarationPath) {
@@ -8,6 +8,8 @@ import { validateMailSenders } from '../modules/delivery-producer/mail.mjs';
8
8
  import { sourceMailSenders } from './deploy-variables.mjs';
9
9
 
10
10
  const providers = ['resend', 'sendgrid', 'cloudflare', 'rer'];
11
+ // Bindings that can send mail. MAIL_PROVIDER=none contradicts every one of them.
12
+ const sendingBindings = ['MAIL_API_KEY', 'SENDGRID_API_KEY', 'RESEND_API_KEY', 'MAIL_PROVIDER_API_KEY', 'MAIL_ACCOUNT_ID', 'MAIL_RELAY_URL'];
11
13
  const nativeCredential = { sendgrid: 'SENDGRID_API_KEY', resend: 'RESEND_API_KEY' };
12
14
  const present = value => typeof value === 'string' && Boolean(value.trim());
13
15
  // The sender forms the mail runtime sends from: a bare address or "Display Name <address>".
@@ -27,10 +29,17 @@ const placeholderSender = value => {
27
29
  || /^(?:fake|placeholder|change-?me|replace-?me|your-?email|your-?name|you)(?:[+._-]|$)/.test(local);
28
30
  };
29
31
 
32
+ // A seed that signs its owner in without mail declares the command on its adapter
33
+ // (`mailOffSignIn`: one line naming a command the person runs from the checkout).
34
+ export function mailOffSignInCommand(adapter) {
35
+ const command = adapter?.mailOffSignIn;
36
+ return typeof command === 'string' && command.trim() && command.length <= 300 && !/[\u0000-\u001f\u007f]/.test(command) ? command.trim() : undefined;
37
+ }
38
+
30
39
  // Packaging can declare inputs that a product's deploy adapter adds to the
31
40
  // root declaration later. Read that source contract, never arbitrary ambient
32
41
  // keys or the adapter's executable source, before any build or provider write.
33
- export async function productionMail(root, declaration, env, declarationPath = 'deploy.json', adapter) {
42
+ export async function productionMail(root, declaration, env, declarationPath = 'deploy.json', adapter, mailOffSignIn = mailOffSignInCommand(adapter)) {
34
43
  const bindings = { ...declaration.vars }, secrets = new Set(), publicInputs = new Set(), declaredProviders = new Set();
35
44
  const senders = [{ source: `${declarationPath} vars.MAIL_FROM`, value: declaration.vars?.MAIL_FROM }];
36
45
  senders.push(...await sourceMailSenders(root, adapter));
@@ -76,6 +85,12 @@ export async function productionMail(root, declaration, env, declarationPath = '
76
85
  bindings[name] = env[name];
77
86
  if (name === 'MAIL_FROM') senderFix = environmentFix(name);
78
87
  }
88
+ // Outbound email is off only when the deployment says so in words: a seed that
89
+ // declares a mail-less sign-in command and a MAIL_PROVIDER setting, with an
90
+ // explicit `none` for it. A missing or deferred value ('later', 'direct') never
91
+ // turns mail off.
92
+ if (mailOffSignIn && present(env.MAIL_PROVIDER) && env.MAIL_PROVIDER.trim() === 'none' && 'MAIL_PROVIDER' in bindings) bindings.MAIL_PROVIDER = 'none';
93
+ const off = bindings.MAIL_PROVIDER === 'none';
79
94
  try { validateMailSenders(senders); }
80
95
  catch (error) { throw refuse(`${error.message} Nothing was built or uploaded.`); }
81
96
  let senderIssue;
@@ -112,6 +127,8 @@ export async function productionMail(root, declaration, env, declarationPath = '
112
127
  };
113
128
  });
114
129
  return {
130
+ off,
131
+ offConflicts: off ? sendingBindings.filter(key => present(bindings[key])) : [],
115
132
  declared: 'MAIL_PROVIDER' in bindings || publicInputs.has('MAIL_PROVIDER') || secrets.has('MAIL_API_KEY') || selected.length > 0,
116
133
  senderIssue,
117
134
  fixtureSender: fixtureSender(bindings.MAIL_FROM),
package/cli/site.mjs CHANGED
@@ -149,7 +149,7 @@ export async function siteCommand(project, options, store, signal, out) {
149
149
  const { siteDeploy, siteReadbackSummary } = await import('../modules/deploy-kit/site.mjs');
150
150
  const environment = options.environment ?? 'production';
151
151
  try {
152
- const api = cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN });
152
+ const api = cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN, settleLists: true, requestSignal: () => signal });
153
153
  // The receipt is for the person; ownership is read from the provider, so a fresh checkout deploys too.
154
154
  const result = await siteDeploy({ api, plan, files, receiptPath: join(project.root, '.rer', 'deploy', `site-${environment}.json`) });
155
155
  return { dryRun: false, deployed: true, origin: result.origin, site: summary };
@@ -1,5 +1,47 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ ## 0.2.100
6
+
7
+ ### Deploy with outbound email explicitly off
8
+
9
+ - `rer deploy` and `rer deploy --dry-run` accept explicit `MAIL_PROVIDER=none` only when the seed's adapter declares `mailOffSignIn`, naming its sign-in command without mail. They bind no mail credential and name the hosted sign-in command and environment. Missing mail configuration is still refused; `none` with a sending credential, account or relay binding is refused before build or upload. The seed owns its behavior with mail off; turning mail on later uses the documented provider inputs and `rer deploy --set-secret`.
10
+
11
+ ### Import transport and opt-in bounded waves
12
+
13
+ - The import door supports a logical package whose files travel in bounded, verified segments. Completed-file hashes, declared aggregate counts and existing source/native quotas are checked before planning; refused packages withdraw their segment custody. Segment transport does not approve an import or raise a quota.
14
+ - Opt-in complete-parent imports can prepare, seal and approve ordered bounded waves, preserving parent lineage, complete membership and native before-images. Unchanged catch-up dispositions and completed reversal retries remain stable; concurrent parent preparation fences reversal. Adopters supply their source/native integrations and a new numbered migration for the additive bounded-wave schema. The reference includes the schema; no edition adoption, live migration, import or cutover is claimed.
15
+ - Business can register a verified local legacy package and transfer its exact files to the granted import door. The accepted Zendesk exporter captures paginated people, tickets, comments, attachments and Guide content into the documented package, with bounded read-only transport, credential removal and resumable checkpoints. These are the inherited accepted package, bounded-wave and Zendesk changes; this release adds no further Business command changes.
16
+
17
+ ### Startup, approval replay and deployment recovery
18
+
19
+ - Setup and dev let the managed Vite listener use its finite startup budget instead of stopping at 60 seconds. Custom start targets retain their declared deadlines; a missing listener announcement reports elapsed and allowed time with startup-log guidance.
20
+ - Legacy MCP tool calls held for approval return their generated `requestState` alongside `approval_required`, so retrying an approved read resumes the original operation without asking for a second approval.
21
+ - `rer deploy --site` retries a changing or partial Cloudflare list from page one up to three times, waiting 20 seconds between failed reads. It discards incomplete snapshots, never replays deploy writes, preserves other refusals and stops waiting on cancellation.
22
+ - Publication scans retain value-free worker, report, version-stamping and cleanup failure diagnostics. Cleanup and version-stamping failures still refuse publication; diagnostic handling does not turn a failed scan into a pass.
23
+
24
+ ## 0.2.99
25
+
26
+ ### Agents wait for the owner instead of failing
27
+
28
+ - `rer business run` now waits when a catch-up needs the owner's approval, or when an export needs an input the owner has not confirmed yet. It prints the platform's message and where the owner acts in Business (for a catch-up, the approval; for an export, the vendor's step, such as confirming the address or the export details), and it does not hand the task back.
29
+ - `rer business run --once` exits 0 when it stops at one of these waits. A continuous `rer business run` keeps running, checks in again every 25 seconds, and picks the work up after the owner acts. A wait clears once Business no longer lists the task as waiting (cancelled, completed or taken by another computer).
30
+ - Every other refusal behaves as before: a 403, a 500 and any other 409 fail or retry exactly as before.
31
+
32
+ ### Plainer words
33
+
34
+ - A vendor export that is missing an input now says it is waiting for the owner (it used to say the task was released as paused) and names the right thing per vendor: Sentry's organization and export window, PagerDuty's export window, Sorry's status page address, and for Calendly that its folder of files needs to be shared with Business. The owner step printed by `rer business run` follows the vendor too: confirm the address for Sorry, the export details for Sentry and PagerDuty.
35
+ - `rer business next` now tells you to run `rer business run --once` when there is work ready, and after a vendor key step.
36
+
37
+ ### Small connect fixes
38
+
39
+ - A pairing response whose code is not text is refused.
40
+ - Two `rer business connect --business ID` runs in the same state folder can no longer start two pairings: the second waits, then resumes the saved pairing.
41
+ - A failure before a pairing starts no longer says the pairing code still works when no code exists yet; the `--code` flow keeps that line.
42
+ - `rer business connect --origin URL` with an origin that is not https, and without `--code`, names the origin rule again.
43
+ - The README and the code comment no longer promise a date for removing `--code`; it is planned for a later release. Nothing in 0.2.99 changes the `--code` path.
44
+
3
45
  ## 0.2.98
4
46
 
5
47
  ### Connecting a computer to a Business
package/global/README.md CHANGED
@@ -501,7 +501,7 @@ agent and OS (including WSL) for the owner to recognise.
501
501
  For older Business pages, `rer business connect --code ABCD-1234 --origin URL`
502
502
  keeps the browser-created code and six-digit confirmation-number flow unchanged
503
503
  in 0.2.98. It cannot be combined with `--business` or `--timeout`. Removal is
504
- planned for 0.2.99 after the production rollback target uses device pairing.
504
+ planned for a later release after the production rollback target uses device pairing.
505
505
  The install.sh form still passes either connect command through unchanged.
506
506
 
507
507
  The owner chooses three things on the computer's row in the browser: "Add rules
@@ -207,6 +207,16 @@ No zone ruleset is read or written. Every request now invokes the Worker: one pa
207
207
 
208
208
  A read-only preflight reads the zone (it must be in this account, else `site-zone-missing`), the Worker and the zone's Custom Domains, and refuses before any write: `resource-name-collision` for a Worker of the name whose live version the kit did not upload, or for a hostname attached to another service (or to a Worker that is not the kit's); `owned-resource-replaced` for a kit Worker whose latest deployment is not the kit's upload (a dashboard rollback or a split deployment). It then uploads the assets and the site Worker, checks that the new version is live, turns workers.dev off, attaches each missing hostname, and reads the Worker and both domains back. Both post-upload Worker checks share a 90-second upload window: missing visibility or the exact preflight version waits with backoff until the upload response's version serves alone as newest with its expected fingerprint. A foreign version, changed fingerprint or split deployment refuses immediately. A timeout names the last serving/newest versions and fingerprint. A repeat attaches nothing new. `.rer/deploy/site-<environment>.json` receives a receipt for the person; it is never read. Provider propagation cases are tested against the fake Cloudflare API in `tests/site-provider.mjs`, where every zone ruleset path answers 403.
209
209
 
210
+ Site deploy retries only `provider-partial-list` and `provider-pagination-changed`
211
+ from read-only list collection: three total attempts, with 20 seconds between
212
+ failed reads. Each attempt discards the incomplete snapshot and restarts at page
213
+ one, retaining the original filters; pages from different attempts are never
214
+ combined. This covers zone, Custom Domain and Worker version/deployment lists,
215
+ including post-upload readback. It never retries the whole deploy or replays a
216
+ write. Other refusals stop immediately; persistent inconsistency keeps the final
217
+ original refusal and its GET diagnostic. Cancellation stops the wait and prevents
218
+ the next read. Owner-mode and teardown settling defaults are unchanged.
219
+
210
220
  A seed can opt into docs search through its command adapter's `site.runtime`:
211
221
 
212
222
  ```ts
@@ -1,3 +1,4 @@
1
+ import timers from 'node:timers/promises';
1
2
  import { providerPages, providerListShape } from './pagination.mjs';
2
3
  import { deleteR2Object } from './r2-object-transport.mjs';
3
4
  import { isR2ObjectDelete } from './r2-objects.mjs';
@@ -40,7 +41,7 @@ const redactValues = (text, values) => values.filter(Boolean).flatMap(value=>[va
40
41
  return at >= 0 && secret.startsWith(value.slice(at)) ? value.slice(0,at)+'[REDACTED]' : value;
41
42
  },text);
42
43
  /** Only this fixed origin and the explicitly selected account ever receive the token. */
43
- export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, objectDelete = deleteR2Object, timeoutMs = 30000, maxRequests = 500, removal = false, budgetMigrations = false, requestSignal = () => undefined }) {
44
+ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, objectDelete = deleteR2Object, timeoutMs = 30000, maxRequests = 500, removal = false, budgetMigrations = false, requestSignal = () => undefined, settleLists = false }) {
44
45
  assert(hexId(accountId), 'invalid-cloudflare-account-id');
45
46
  assert(typeof token === 'string' && token.trim() && !/[\r\n]/.test(token), 'invalid-cloudflare-api-token');
46
47
  // Capture an outer deadline once per operation, before transport retries.
@@ -137,7 +138,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
137
138
  throw error;
138
139
  }
139
140
  }
140
- async function collect(path, options) {
141
+ async function collectOnce(path, options) {
141
142
  let result, rows = [];
142
143
  for await (const page of providerPages(envelope, path, options)) {
143
144
  result ??= page.data.result;
@@ -146,6 +147,21 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
146
147
  }
147
148
  return options?.field ? {...result,[options.field]:rows} : rows;
148
149
  }
150
+ // Site deploy opts in here: both request/zoneRequest and list must discard
151
+ // every page of an inconsistent snapshot. Only this read is replayed; owner
152
+ // and teardown callers retain their existing settling policies and defaults.
153
+ async function collect(path, options) {
154
+ if (!settleLists) return collectOnce(path, options);
155
+ const signal = deadlineSignal(options?.signal);
156
+ for (let attempt = 1; ; attempt++) {
157
+ signal?.throwIfAborted();
158
+ try { return await collectOnce(path, options); }
159
+ catch (error) {
160
+ if (attempt === 3 || !['provider-partial-list','provider-pagination-changed'].includes(error.message)) throw error;
161
+ await timers.setTimeout(20000, undefined, {signal});
162
+ }
163
+ }
164
+ }
149
165
  async function list(path, options = {}) {
150
166
  const shape = {...providerListShape(path),...options};
151
167
  const result = await collect(path,shape);
@@ -2,6 +2,7 @@ import { captureJobLayout, jobLayoutGuards, jobLayoutReads } from './stream-layo
2
2
  import { needsStreamOrigin } from './stream-origin.ts';
3
3
  import { nativeStagingScope } from './native-staging-context.ts';
4
4
  import { streamBoundary } from './stream-boundary.ts';
5
+ import { captureWaveBoundary, waveBoundary } from './wave-boundary.ts';
5
6
  import { requestContext } from './budget.ts';
6
7
  import { loadPlans, storedPlanSnapshot, planBody, planGuards, planChunkReads, retainPlan, creationCodec, LARGE_PLAN_BYTES } from './plans.ts';
7
8
  import { randomUUID } from 'node:crypto';
@@ -38,10 +39,11 @@ export async function stored(id: string, context: D1ImportContext, action: Impor
38
39
  job.erasedRecords = plan.entries.filter(entry => entry.erased).length;
39
40
  attachReversal(job, await loadReversalState(row, plan, context));
40
41
  await captureJobLayout(job, row, context, action);
42
+ await captureWaveBoundary(job, context);
41
43
  return { job, plan };
42
44
  }
43
45
  export function boundary(job: ImportJob, plan: ImportPlan, action: ImportAction = 'read'): D1Sql[] {
44
- return [...jobLayoutGuards(job), ...streamBoundary(job.id, action), ...planGuards(plan, job.id), ...reversalGuards(job.id, reversalState(job)), guard(sql('EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND fence=? AND checkpoint=? AND state=? AND fingerprint=? AND plan_json=? AND principal_id=? AND destination=? AND namespace=? AND mapping_version=? AND total=? AND error IS ?)', job.id, job.fence, job.checkpoint, job.state, job.fingerprint, planBody(plan), job.principalId, job.destination, job.namespace, job.mappingVersion, job.total, job.error))];
46
+ return [...waveBoundary(job, action), ...jobLayoutGuards(job), ...streamBoundary(job.id, action), ...planGuards(plan, job.id), ...reversalGuards(job.id, reversalState(job)), guard(sql('EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND fence=? AND checkpoint=? AND state=? AND fingerprint=? AND plan_json=? AND principal_id=? AND destination=? AND namespace=? AND mapping_version=? AND total=? AND error IS ?)', job.id, job.fence, job.checkpoint, job.state, job.fingerprint, planBody(plan), job.principalId, job.destination, job.namespace, job.mappingVersion, job.total, job.error))];
45
47
  }
46
48
  export async function planD1Import(source: SourceBundle, mapping: ImportMapping, context: D1ImportContext): Promise<ImportPlan> {
47
49
  context = requestContext(context);
@@ -21,8 +21,8 @@ export interface RetirementScope { jobs: string[]; streams: string[]; stages: st
21
21
  /** Jobs, streams and private stages whose headers hold one of the exact values,
22
22
  * closed over everything their deletion would break: whole streams, jobs whose
23
23
  * entries reuse a retired job's sources, and preserved history in both directions. */
24
- export async function retirementScope(db: D1Database, destination: string, values: string[]): Promise<RetirementScope> {
25
- const jobs = new Set<string>(), streams = new Set<string>(), stages = new Set<string>(), anchors = new Set<string>(), preparations = new Set<string>();
24
+ export async function retirementScope(db: D1Database, destination: string, values: string[], seedJobs: string[] = []): Promise<RetirementScope> {
25
+ const jobs = new Set<string>(seedJobs), streams = new Set<string>(), stages = new Set<string>(), anchors = new Set<string>(), preparations = new Set<string>();
26
26
  const exact = JSON.stringify(values);
27
27
  const read = async (query: string, ...bound: unknown[]) => (await db.prepare(query).bind(...bound).all<{ id: string }>()).results.map(row => row.id);
28
28
  if (values.length) {
@@ -5,6 +5,7 @@ import { doorRetirementStore } from './retirement-door.ts';
5
5
  // Bookkeeping that every custody change bumps, plus the erasure's own records.
6
6
  const SKIPPED = new Set(['ik_d1_assertions', 'ik_d1_custody_states', 'ik_d1_custody_revisions', 'ik_d1_publication_fences',
7
7
  'ik_d1_erasure_tally', 'ik_d1_erasure_journal', 'ik_d1_erasure_progress', 'ik_d1_retirements', 'ik_d1_erased_sources', 'ik_d1_erased_targets', 'ik_targets',
8
+ 'ik_door_wave_erased_aliases', 'ik_door_wave_origins', 'ik_door_wave_roots', 'ik_door_wave_children', 'ik_door_wave_members', 'ik_door_wave_approvals',
8
9
  'ik_door_settings', // Installation opt-in only; no uploaded record content.
9
10
  'ik_door_revoked_keys', // Persistent key revocations must survive erasure.
10
11
  'ik_door_grants', // Grant nonce replay protection; no uploaded record content.
@@ -1,3 +1,4 @@
1
+ import { waveRetirementSeeds } from './wave-retirement.ts';
1
2
  import { randomUUID } from 'node:crypto';
2
3
  import type { D1CustodyRetirement, D1ErasureJournalEntry, D1ImportContext, D1ImportPerson, D1RetirementOptions, D1RetirementProgress, D1Sql } from './types.ts';
3
4
  import { requestContext, limitStatements, packGuards, remaining, D1RequestLimitError } from './budget.ts';
@@ -37,7 +38,14 @@ export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements:
37
38
  export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1ErasureStepOptions): Promise<D1CustodyRetirement | D1ErasurePending>;
38
39
  export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1RetirementOptions): Promise<D1CustodyRetirement | D1RetirementProgress>;
39
40
  export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1RetirementOptions | D1ErasureStepOptions = {}): Promise<D1CustodyRetirement | D1RetirementProgress | D1ErasurePending> {
40
- if ('operationKey' in options) return retireD1ImportCustodyStep(person, nativeStatements, input, checkpoint, options);
41
+ if ('operationKey' in options) {
42
+ const admission=requestContext(input);
43
+ await readAtomic(admission,[],undefined,'erase');
44
+ const db=admission.storage.database;
45
+ const installed=await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_roots'").first();
46
+ if (person.aliases?.length || installed && await db.prepare('SELECT 1 FROM ik_door_wave_roots WHERE destination=? LIMIT 1').bind(input.destination).first()) throw new Error('IMPORT_WAVE_JOURNAL_REQUIRED');
47
+ return retireD1ImportCustodyStep(person,nativeStatements,admission,checkpoint,options);
48
+ }
41
49
  person = { ...person, related: person.related ? { ...person.related, fields: fieldRedactions(person.related.fields) } : undefined };
42
50
  const context = limitStatements(requestContext({ ...input, originalErasurePolicy: input.originalErasurePolicy ?? 'withdraw-affected-artifacts' }), 1000);
43
51
  if (context.maxBatchBytes !== undefined) throw new Error('IMPORT_RETIREMENT_OPERATION_KEY_REQUIRED');
@@ -45,16 +53,18 @@ export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatem
45
53
  for (const value of values) requireText(value, 'person value');
46
54
  if (person.target) { requireText(person.target.kind, 'target kind'); requireText(person.target.id, 'target id'); }
47
55
  const fields = person.related?.fields ?? [];
48
- if (!person.target && !values.length && !fields.length) throw new Error('IMPORT_RETIREMENT_PERSON_REQUIRED');
56
+ if (!person.target && !values.length && !fields.length && !person.aliases?.length) throw new Error('IMPORT_RETIREMENT_PERSON_REQUIRED');
57
+ for (const alias of person.aliases ?? []) for (const value of [alias.installationId,alias.namespace,alias.kind,alias.sourceId]) requireText(value,'source alias');
49
58
  // Without a native target only field redaction applies: whole-record erasure
50
59
  // (`source`, `original`) binds what it severs to the erased target.
51
60
  if (person.related && !person.target && (person.related.source || person.related.original || !fields.length)) throw new Error('IMPORT_RETIREMENT_TARGET_REQUIRED');
52
61
  await readAtomic(context, [], undefined, 'erase');
53
62
  const db = context.storage.database, destination = context.destination;
54
- const scope = await retirementScope(db, destination, values), retiring = new Set(scope.jobs);
63
+ const wave = await waveRetirementSeeds(db,destination,person,context.storage.targets.planCodec);
64
+ const scope = await retirementScope(db, destination, values,wave.jobs), retiring = new Set(scope.jobs);
55
65
  // Withdrawn door content is custody too: a values-only erasure that removes it is fenced.
56
66
  let stores: Store[] | undefined;
57
- const fenced = Boolean(person.target) || fields.length > 0 || scope.jobs.length + scope.streams.length + scope.stages.length + scope.anchors.length + scope.preparations.length > 0
67
+ const fenced = Boolean(person.target) || !!person.aliases?.length || fields.length > 0 || scope.jobs.length + scope.streams.length + scope.stages.length + scope.anchors.length + scope.preparations.length > 0
58
68
  || await doorCustodyInScope(db, destination, stores = await tallyStores(db));
59
69
  if (fenced && !checkpoint.length) throw new Error('IMPORT_RETIREMENT_CHECKPOINT_REQUIRED');
60
70
  const fence = fenced ? checkpoint : [];
@@ -94,7 +104,7 @@ export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatem
94
104
  touched.streams = [...new Set([...scope.anchors, ...await pageStreams(db, touched.jobs)])].sort();
95
105
  stores ??= await tallyStores(db);
96
106
  const entry = { recordedAt, recordsErased: erasure.decisions.length, jobsRetired: scope.jobs.length, streamsRetired: scope.anchors.length };
97
- prefix = packGuards([authority(context, 'erase'), ...tallyBefore(stores, receiptId, touched), ...tallyWithdraw(stores, touched), ...erasure.before, ...scopeGuards(scope), ...lineage.fence.map(guard)]);
107
+ prefix = packGuards([authority(context, 'erase'), ...tallyBefore(stores, receiptId, touched), ...(wave.roots.length ? [sql("UPDATE ik_door_wave_roots SET state='withdrawn' WHERE id IN (SELECT value FROM json_each(?))",JSON.stringify(wave.roots))] : []), ...wave.tombstones.map(alias=>sql('INSERT OR IGNORE INTO ik_door_wave_erased_aliases(installation_id,namespace,destination,identity) VALUES(?,?,?,?)',alias.installation,alias.namespace,destination,alias.identity)), ...tallyWithdraw(stores, touched), ...erasure.before, ...scopeGuards(scope), ...wave.guards.map(guard), ...lineage.fence.map(guard)]);
98
108
  suffix = [...packGuards([...erasure.after, ...retirementWrites(scope, destination, receiptId, recordedAt), ...headerAbsence(destination, values),
99
109
  ...tallyJournal(stores, receiptId, touched, entry, fenced), authority(context, 'erase'), sql('DELETE FROM ik_d1_assertions')]), ...readonlyChecks(erasure.final), finalAuthority(context, 'erase')];
100
110
  // One read after the batch returns its journal row.
@@ -123,6 +123,9 @@ export interface D1ProvenanceRow extends ProvenanceRow { predecessorId?: string
123
123
  * sit whole in a job or stream header, an original's name or a private stage.
124
124
  * Nothing is ever matched as a substring. */
125
125
  export interface D1ImportPerson {
126
+ /** Server-derived source subjects, including context/metadata subjects that
127
+ * never became native targets. Never accept this selector from a client. */
128
+ aliases?: readonly { installationId: string; namespace: string; kind: string; sourceId: string }[];
126
129
  target?: { kind: string; id: string };
127
130
  values?: string[];
128
131
  related?: D1RelatedErasure;
@@ -0,0 +1,33 @@
1
+ import type { ImportAction, ImportJob } from '../types.ts';
2
+ import type { D1ImportContext, D1Sql } from './types.ts';
3
+ import { guard, sql } from './database.ts';
4
+
5
+ const contexts = new WeakMap<D1ImportContext['authorization'], {parent:string;mode:string;database:D1ImportContext['storage']['database']}>();
6
+ const jobs = new WeakMap<ImportJob, {parent:string;ordinal:number;allowed:boolean;mode:string}>();
7
+ /** A direct ordinary apply/undo must not promote a prepared wave's child. */
8
+ export function bindWaveContext(context: D1ImportContext, parent: string, mode: string) {
9
+ contexts.set(context.authorization,{parent,mode,database:context.storage.database});
10
+ }
11
+ export async function captureWaveBoundary(job: ImportJob, context: D1ImportContext) {
12
+ if (!job.namespace.startsWith('door-wave:')) return;
13
+ const db=context.storage.database;
14
+ if (!await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_children'").first()) return;
15
+ const row=await db.prepare(`SELECT c.parent_id,c.ordinal FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
16
+ WHERE p.job_id=? OR ('door:'||p.id)=(SELECT operation_key FROM ik_jobs WHERE id=?)`).bind(job.id,job.id).first<any>();
17
+ if (!row) {jobs.set(job,{parent:'',ordinal:-1,allowed:false,mode:''});return;}
18
+ const scope=contexts.get(context.authorization);
19
+ jobs.set(job,{parent:row.parent_id,ordinal:row.ordinal,allowed:scope?.parent===row.parent_id && scope?.database===db,mode:scope?.mode ?? ''});
20
+ }
21
+ export function waveBoundary(job: ImportJob, action: ImportAction): D1Sql[] {
22
+ const row=jobs.get(job);
23
+ if (!row || !['apply','undo','rollback'].includes(action)) return [];
24
+ const forward=action==='apply';
25
+ if (!row.allowed || row.mode!==(forward?'apply':'reverse')) return [guard(sql('0'))];
26
+ return [guard(sql(`EXISTS(SELECT 1 FROM ik_door_wave_roots r JOIN ik_door_wave_children c ON c.parent_id=r.id
27
+ JOIN ik_door_plans p ON p.id=c.plan_id WHERE r.id=? AND c.ordinal=? AND p.job_id=?
28
+ AND EXISTS(SELECT 1 FROM ik_door_wave_origins o WHERE o.job_id=p.job_id AND o.parent_id=r.id AND o.ordinal=c.ordinal AND o.plan_id=p.id AND o.fingerprint=p.fingerprint)
29
+ AND r.state IN (${forward?"'applying','completed'":"'reversing'"})
30
+ AND EXISTS(SELECT 1 FROM ik_door_wave_approvals a WHERE a.parent_id=r.id AND a.ordinal=-1 AND a.revoked=0 AND a.fingerprint=r.fingerprint)
31
+ AND NOT EXISTS(SELECT 1 FROM ik_door_wave_children peer JOIN ik_door_plans pp ON pp.id=peer.plan_id LEFT JOIN ik_jobs j ON j.id=pp.job_id
32
+ WHERE peer.parent_id=r.id AND peer.ordinal ${forward?'<':'>'} c.ordinal AND ${forward?"(j.id IS NULL OR j.state<>'completed')":"(j.id IS NOT NULL AND j.state NOT IN ('undone','rolled_back'))"}))`,row.parent,row.ordinal,job.id))];
33
+ }