@runeditrun/rer 0.2.85 → 0.2.86

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 (144) hide show
  1. package/README.md +138 -29
  2. package/cli/README.md +61 -1
  3. package/cli/agent-door.mjs +149 -0
  4. package/cli/agent-fixtures.mjs +16 -0
  5. package/cli/agent-mail.mjs +54 -0
  6. package/cli/agent-network.mjs +64 -0
  7. package/cli/agent-reviewers.mjs +110 -0
  8. package/cli/agent-secrets.mjs +23 -0
  9. package/cli/agent-sponsors.mjs +43 -0
  10. package/cli/agent-surface.mjs +34 -0
  11. package/cli/agent-webhooks.mjs +19 -0
  12. package/cli/authoring.mjs +2 -1
  13. package/cli/business/archive-task.mjs +32 -0
  14. package/cli/business/carry.mjs +3 -1
  15. package/cli/business/catchup-task.mjs +42 -0
  16. package/cli/business/choices.mjs +97 -0
  17. package/cli/business/command.mjs +23 -5
  18. package/cli/business/connect.mjs +19 -7
  19. package/cli/business/detector.mjs +4 -1
  20. package/cli/business/device.mjs +13 -0
  21. package/cli/business/export-inputs.mjs +69 -0
  22. package/cli/business/export-task.mjs +17 -0
  23. package/cli/business/invocation.mjs +8 -1
  24. package/cli/business/keychain.mjs +8 -1
  25. package/cli/business/lease.mjs +11 -1
  26. package/cli/business/local-commands.mjs +79 -31
  27. package/cli/business/outbound.mjs +9 -4
  28. package/cli/business/parsers-config.mjs +8 -7
  29. package/cli/business/parsers-syntax.mjs +7 -3
  30. package/cli/business/permissions.mjs +2 -0
  31. package/cli/business/questions.mjs +89 -9
  32. package/cli/business/setup-command.mjs +49 -0
  33. package/cli/business/setup-company.json +15 -0
  34. package/cli/business/setup-demo.mjs +95 -0
  35. package/cli/business/state.mjs +44 -10
  36. package/cli/business/supervisor.mjs +94 -35
  37. package/cli/business/vendor-door.mjs +230 -0
  38. package/cli/business/vendors/README.md +29 -3
  39. package/cli/business/vendors/common.mjs +8 -6
  40. package/cli/business/vendors/index.mjs +22 -11
  41. package/cli/business/vendors/sorry.mjs +106 -0
  42. package/cli/contract.mjs +2 -2
  43. package/cli/demo-deploy-error.mjs +14 -0
  44. package/cli/demo-native.mjs +4 -1
  45. package/cli/deploy-config.mjs +1 -1
  46. package/cli/deploy-owner-input.mjs +3 -1
  47. package/cli/deploy-variables.mjs +58 -0
  48. package/cli/door-clauses.mjs +157 -0
  49. package/cli/export.mjs +16 -4
  50. package/cli/hosted-output.mjs +17 -7
  51. package/cli/inspect.mjs +4 -0
  52. package/cli/jsonc.mjs +8 -4
  53. package/cli/lifecycle.mjs +3 -1
  54. package/cli/main.mjs +1 -0
  55. package/cli/managed-data-schema.mjs +14 -1
  56. package/cli/portability-empty.mjs +6 -0
  57. package/cli/portability-migrations.mjs +26 -4
  58. package/cli/production-auth.mjs +1 -1
  59. package/cli/production-mail.mjs +8 -2
  60. package/edition-authoring/archive-source.mjs +59 -0
  61. package/edition-authoring/base-copies.mjs +92 -18
  62. package/edition-authoring/cli.mjs +1 -1
  63. package/edition-authoring/edition-migrations.mjs +7 -3
  64. package/edition-authoring/module-closure.mjs +2 -16
  65. package/edition-authoring/workflow-copies.mjs +1 -1
  66. package/edition-authoring/workflow-scripts.mjs +12 -3
  67. package/foundation/runtime/contract-docs.mjs +215 -32
  68. package/foundation/runtime/contract-prose-patterns.mjs +14 -0
  69. package/foundation/runtime/contract-prose.mjs +88 -0
  70. package/global/CHANGELOG.md +93 -0
  71. package/global/README.md +138 -29
  72. package/global/door/command.mjs +3 -1
  73. package/global/main.mjs +1 -1
  74. package/modules/agent-door/CLAUSES.md +215 -0
  75. package/modules/agent-door/conformance/audit.mjs +66 -0
  76. package/modules/agent-door/conformance/authority.d.mts +2 -0
  77. package/modules/agent-door/conformance/authority.mjs +17 -0
  78. package/modules/agent-door/conformance/crypto.mjs +36 -0
  79. package/modules/agent-door/conformance/discovery.mjs +61 -0
  80. package/modules/agent-door/conformance/index.mjs +41 -0
  81. package/modules/agent-door/conformance/invocation.mjs +32 -0
  82. package/modules/agent-door/conformance/journey.mjs +112 -0
  83. package/modules/agent-door/conformance/narrowing.d.mts +1 -0
  84. package/modules/agent-door/conformance/narrowing.mjs +21 -0
  85. package/modules/agent-door/conformance/negative.mjs +47 -0
  86. package/modules/agent-door/conformance/network-modules.mjs +86 -0
  87. package/modules/agent-door/conformance/network-runtime.mjs +46 -0
  88. package/modules/agent-door/conformance/policy-phases.mjs +75 -0
  89. package/modules/agent-door/conformance/protocol.mjs +145 -0
  90. package/modules/agent-door/conformance/replay.mjs +38 -0
  91. package/modules/agent-door/conformance/schema.mjs +71 -0
  92. package/modules/agent-door/conformance/security.mjs +33 -0
  93. package/modules/agent-door/conformance/sponsors.mjs +68 -0
  94. package/modules/agent-door/conformance/surface.mjs +89 -0
  95. package/modules/agent-door/conformance/target.mjs +18 -0
  96. package/modules/agent-door/conformance/tool-name.d.mts +1 -0
  97. package/modules/agent-door/conformance/tool-name.mjs +7 -0
  98. package/modules/agent-door/conformance/webhooks.mjs +30 -0
  99. package/modules/container/image-variables.mjs +8 -2
  100. package/modules/demo-host/README.md +6 -0
  101. package/modules/demo-host/ownership.mjs +39 -0
  102. package/modules/demo-host/plan.mjs +2 -1
  103. package/modules/demo-host/remove.mjs +6 -1
  104. package/modules/import-kit/schema/importer-verification-receipt.schema.json +171 -0
  105. package/modules/import-kit/schema/importers.schema.json +107 -0
  106. package/modules/import-kit/src/door/ADOPTING.md +20 -0
  107. package/modules/import-kit/src/door/README.md +2 -1
  108. package/modules/import-kit/src/door/admission.ts +5 -3
  109. package/modules/import-kit/src/door/archive.ts +5 -2
  110. package/modules/import-kit/src/door/authority.ts +1 -0
  111. package/modules/import-kit/src/door/grants.ts +1 -1
  112. package/modules/import-kit/src/door/handler.ts +14 -8
  113. package/modules/import-kit/src/door/index.ts +1 -1
  114. package/modules/import-kit/src/door/jobs.ts +40 -0
  115. package/modules/import-kit/src/door/operator.ts +3 -3
  116. package/modules/import-kit/src/door/planning.ts +10 -6
  117. package/modules/import-kit/src/door/protocol.ts +2 -2
  118. package/modules/import-kit/src/door/raw.ts +25 -0
  119. package/modules/import-kit/src/door/refusals.ts +1 -0
  120. package/modules/import-kit/src/door/types.ts +26 -6
  121. package/modules/import-kit/src/receipt/encoding.ts +37 -0
  122. package/modules/import-kit/src/receipt/index.ts +69 -0
  123. package/modules/import-kit/src/receipt/policy.ts +24 -0
  124. package/modules/import-kit/src/receipt/types.ts +64 -0
  125. package/modules/import-kit/src/receipt/uri.ts +45 -0
  126. package/modules/import-kit/src/receipt/validation.ts +87 -0
  127. package/modules/import-kit/src/status/encoding.ts +13 -0
  128. package/modules/import-kit/src/status/evidence.ts +218 -0
  129. package/modules/import-kit/src/status/index.ts +56 -0
  130. package/modules/import-kit/src/status/schema.ts +208 -0
  131. package/modules/import-kit/src/status/types.ts +86 -0
  132. package/modules/operator-auth/accounts/delegated.mjs +48 -0
  133. package/modules/operator-auth/index.d.mts +7 -0
  134. package/modules/operator-auth/service.mjs +2 -0
  135. package/modules/portability/README.md +32 -11
  136. package/modules/portability/bridge.mjs +22 -2
  137. package/modules/portability/copies.mjs +8 -1
  138. package/modules/portability/derived.mjs +33 -0
  139. package/modules/portability/http-errors.mjs +1 -1
  140. package/modules/portability/import.mjs +14 -7
  141. package/package.json +4 -2
  142. package/scripts/publish-check/copy_identities.py +3 -1
  143. package/scripts/publish-check/github_refs.py +32 -13
  144. package/cli/business/opt-ins.mjs +0 -37
@@ -15,12 +15,15 @@ with their own supported readers and native adapters.
15
15
 
16
16
  `exportVendor(vendor, {key, inputs, checkpoint, signal})` is an async generator.
17
17
  It yields `{name, bytes: Buffer, checkpoint}`. Keys remain in memory and are
18
- attached only to GET requests on the adapter's fixed HTTPS origin. Redirects,
18
+ attached only to GET requests on the adapter's fixed HTTPS origin. Sorry reads a
19
+ public status page and takes no key (`key` must be absent); `KEYED_VENDORS` lists
20
+ the vendors that need one. Redirects,
19
21
  caller origins, arbitrary paths and credential-bearing pagination are refused.
20
22
  No export runs merely by importing these modules. No account export was run
21
23
  during implementation.
22
24
 
23
- `VENDORS` is the closed vendor enum. `VENDOR_CAPABILITIES` describes the adapter,
25
+ `VENDORS` is the closed vendor enum (`sentry`, `uptimerobot`, `pagerduty`,
26
+ `calendly`, `sorry`). `VENDOR_CAPABILITIES` describes the adapter,
24
27
  required vendor-key capability, native importer format, layout and read scopes.
25
28
  These adapters cannot verify that a supplied key itself has read-only scope;
26
29
  the owner must create the documented read-only key where the vendor offers one.
@@ -51,12 +54,19 @@ import. Never treat a partial stream as a completed bundle.
51
54
  Every item is at most 1 MiB. Exports are bounded to 1,000 requests, one request
52
55
  per second, 30 seconds per request, 2 MiB/499 API records for Sentry,
53
56
  512 KiB/500 records for PagerDuty, and 8 MiB/2,500 records for UptimeRobot and
54
- Calendly. The output bound includes JSON framing and Calendly's manifest.
57
+ Calendly. Sorry follows Lamp's reader: at most 200 requests (one saved file
58
+ each), one every 125 ms, 8 MiB of responses, 5,000 records and a 16 MiB bundle. The output bound includes JSON framing and Calendly's manifest.
55
59
  Limits stop rather than truncate. AbortSignal stops waits, HTTP reads and
56
60
  emission. HTTP errors contain only a fixed code and numeric status; network,
57
61
  parsing and unexpected errors never expose response text, URLs or key values.
58
62
  429 stops with `RATE_LIMITED`; there is no hidden retry loop.
59
63
 
64
+ The Business CLI supervisor currently captures a complete private bundle before
65
+ uploading it. After a lease handover, its next run captures and uploads the export
66
+ again under a fresh lease; it does not resume a partially uploaded bundle in
67
+ place. Only a completed upload and the door's matching plan are reported as a
68
+ preview. Calendly's folder layout remains outside this single-bundle path.
69
+
60
70
  ## Credentials in responses
61
71
 
62
72
  Every adapter strips credential values from a parsed response before it emits
@@ -110,9 +120,23 @@ pagination link is still refused.
110
120
  * `pagerduty`: `start`/`end` (or `since`/`until`). Exports users, schedule details
111
121
  with layers/final coverage, overrides for the window and escalation policies.
112
122
  Schedule listing only discovers IDs and is not duplicated in the bundle.
123
+ Oncall compares a schedule's final timeline over at most 31 days, so the
124
+ Business CLI passes a window of at most 31 whole UTC days ending two weeks
125
+ ahead.
113
126
  Overrides retain full boundaries with `overflow=true` and add `schedule_id`,
114
127
  as Oncall's documented bundle requires. Incident history and paging
115
128
  configuration are not fetched.
129
+ * `sorry`: `page: {host}`, the public status page's host name (a public DNS
130
+ name; no scheme, port, path, IP address or local name). Reads, anonymously,
131
+ `/api/v1/status`, every `components` and `notices` page (`meta.next_page`) and
132
+ each notice's detail with its components and full update history, like Lamp's
133
+ public Move in. The bundle is Lamp's door `sorry` source: `{source: 'sorry',
134
+ publicUrl, publicAccessAcknowledged: true, files: [{name, text}]}`, each file
135
+ the original response text under the name Lamp's reader requires
136
+ (`sorry-status.json`, `sorry-components[-N].json`, `sorry-notices[-N].json`,
137
+ `sorry-notice-<id>.json`). Every response came from the page's own public API,
138
+ so it is public by construction; Lamp still refuses private or
139
+ audience-scoped records, and public responses carry no subscribers.
116
140
  * `calendly`: optional `asOf` timestamp (recommended for a repeatable catch-up).
117
141
  An omitted timestamp is captured at first invocation and retained on resume.
118
142
  Downloads the current user, event types, availability schedules, per-type
@@ -129,6 +153,8 @@ their file-writing command wrappers.
129
153
 
130
154
  * [Dial downloader at 6014c99875dd002545e194ab23a7852d30e94e01](https://github.com/just-every/run-dial/blob/6014c99875dd002545e194ab23a7852d30e94e01/scripts/move-in/download.mjs).
131
155
  * [Uptime guide at 157686b52ecc652d64250a30ec224f1a47355a6c](https://github.com/just-every/run-uptime/blob/157686b52ecc652d64250a30ec224f1a47355a6c/docs/moving-from-uptimerobot.md).
156
+ * [Lamp's Sorry public reader at a0179bfe](https://github.com/just-every/run-lamp/blob/a0179bfe/src/core/features/statuspage-import/sorry/fetch.ts)
157
+ and its door source (`door-plan.ts` `doorExport`).
132
158
  * [Oncall guide at 4f928effc3ce735f3d246bb8a3c4bbb8436cae6b](https://github.com/just-every/run-oncall/blob/4f928effc3ce735f3d246bb8a3c4bbb8436cae6b/docs/move-in.md).
133
159
  * Spool's proposal-cited `app/docs/SENTRY-SWITCHING.md` and
134
160
  `app/src/domain/sentry-import/source.ts`, local source commit
@@ -162,8 +162,9 @@ export function client({ origin, headers, route, signal, limits, counters, key }
162
162
  const url = new URL(path, origin);
163
163
  if (url.origin !== origin || url.username || url.password || url.hash || !route(url)) fail('INVALID_REFERENCE');
164
164
  if (++counters.requests > limits.requests) fail('REQUEST_LIMIT');
165
- // At most one request per second; no automatic retry or rate-limit hammering.
166
- const pause = 1000 - (Date.now() - lastRequest);
165
+ // At most one request per `spacing` (a second unless the adapter's limits say
166
+ // otherwise); no automatic retry or rate-limit hammering.
167
+ const pause = (limits.spacing ?? 1000) - (Date.now() - lastRequest);
167
168
  if (pause > 0) await delay(pause, undefined, { signal });
168
169
  lastRequest = Date.now();
169
170
  const timeout = AbortSignal.timeout(30_000);
@@ -189,9 +190,10 @@ export function client({ origin, headers, route, signal, limits, counters, key }
189
190
  }
190
191
  const bytes = Buffer.concat(chunks);
191
192
  // A reflected authentication key must never become a vendor artifact.
192
- if (bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
193
- let body;
194
- try { body = JSON.parse(bytes.toString('utf8')); } catch { fail('INVALID_JSON'); }
195
- return { body, headers: response.headers };
193
+ if (key && bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
194
+ let text, body;
195
+ try { text = new TextDecoder('utf-8', { fatal: true }).decode(bytes); body = JSON.parse(text); } catch { fail('INVALID_JSON'); }
196
+ // `text` is the response exactly as received, for adapters whose bundle keeps original bytes.
197
+ return { body, text, headers: response.headers };
196
198
  };
197
199
  }
@@ -3,22 +3,31 @@ import * as sentry from './sentry.mjs';
3
3
  import * as uptimerobot from './uptimerobot.mjs';
4
4
  import * as pagerduty from './pagerduty.mjs';
5
5
  import * as calendly from './calendly.mjs';
6
+ import * as sorry from './sorry.mjs';
6
7
 
7
8
  export { VendorExportError } from './common.mjs';
8
- export const VENDORS = Object.freeze(['sentry','uptimerobot','pagerduty','calendly']);
9
- const adapters = { sentry, uptimerobot, pagerduty, calendly };
9
+ export const VENDORS = Object.freeze(['sentry','uptimerobot','pagerduty','calendly','sorry']);
10
+ const adapters = { sentry, uptimerobot, pagerduty, calendly, sorry };
10
11
  export const VENDOR_CAPABILITIES = Object.freeze({
11
12
  sentry: Object.freeze({ vendor: 'sentry', adapter: 'rer:sentry', capability: 'vendor_key:sentry', format: 'spool-sentry-org-export-v1', layout: 'concatenate', output: 'sentry-export.json', readOnly: true, resume: true, scopes: ['org:read','project:read','event:read'], origins: Object.values(sentry.origins) }),
12
13
  uptimerobot: Object.freeze({ vendor: 'uptimerobot', adapter: 'rer:uptimerobot', capability: 'vendor_key:uptimerobot', format: 'uptimerobot-v3-monitors', layout: 'concatenate', output: 'uptimerobot-monitors.json', readOnly: true, resume: true, scopes: ['read-only API key'], origins: [uptimerobot.origin] }),
13
14
  pagerduty: Object.freeze({ vendor: 'pagerduty', adapter: 'rer:pagerduty', capability: 'vendor_key:pagerduty', format: 'pagerduty-saved-responses', layout: 'concatenate', output: 'pagerduty-export.json', readOnly: true, resume: true, scopes: ['read-only REST API key'], origins: [pagerduty.origin] }),
14
15
  calendly: Object.freeze({ vendor: 'calendly', adapter: 'rer:calendly', capability: 'vendor_key:calendly', format: 'dial-calendly-api-folder-v1', layout: 'files', output: 'manifest.json', readOnly: true, resume: true, scopes: ['personal access token with read access'], origins: [calendly.origin] }),
16
+ // A public status page: read anonymously from the page's own address, so it takes no key.
17
+ sorry: Object.freeze({ vendor: 'sorry', adapter: 'rer:sorry', capability: null, format: 'lamp-sorry-public-responses', layout: 'concatenate', output: 'sorry-export.json', readOnly: true, resume: true, scopes: [], origins: ['https://<page host>'] }),
15
18
  });
19
+ /** The vendors whose export needs the owner's read-only key. */
20
+ export const KEYED_VENDORS = Object.freeze(VENDORS.filter(vendor => VENDOR_CAPABILITIES[vendor].capability !== null));
16
21
  const LIMITS = Object.freeze({
17
22
  // Spool's 500-record bound also counts its synthesized manifest record.
18
23
  sentry: { bytes: 2 * 1024 * 1024, records: 499 },
19
24
  uptimerobot: { bytes: 8 * 1024 * 1024, records: 2500 },
20
25
  pagerduty: { bytes: 512 * 1024, records: 500 },
21
26
  calendly: { bytes: 8 * 1024 * 1024, records: 2500 },
27
+ // Lamp's Sorry reader: 8 MiB of responses in at most 200 files and 5,000
28
+ // records; its door takes twice that, as the bundle carries each response as
29
+ // JSON text. Lamp reads a public page at most every 125 ms.
30
+ sorry: { bytes: 8 * 1024 * 1024, emitted: 16 * 1024 * 1024, records: 5000, requests: 200, spacing: 125 },
22
31
  });
23
32
 
24
33
  /** Persist each checkpoint only after the corresponding bytes have been acknowledged.
@@ -27,27 +36,29 @@ const LIMITS = Object.freeze({
27
36
  export async function* exportVendor(vendor, { key, inputs = {}, checkpoint, signal } = {}) {
28
37
  try {
29
38
  if (!VENDORS.includes(vendor)) fail('UNSUPPORTED_VENDOR');
30
- if (typeof key !== 'string' || !key || key.length > 8192 || /[\u0000-\u0020\u007f]/u.test(key)) fail('INVALID_KEY');
39
+ const keyed = KEYED_VENDORS.includes(vendor);
40
+ if (keyed ? typeof key !== 'string' || !key || key.length > 8192 || /[\u0000-\u0020\u007f]/u.test(key) : key != null) fail('INVALID_KEY');
31
41
  object(inputs);
32
42
  const fingerprint = hash(JSON.stringify(stable({ vendor, inputs })));
33
- const limits = { ...LIMITS[vendor], requests: 1000, pageBytes: 1024 * 1024 - 16 * 1024 };
43
+ const limits = { requests: 1000, ...LIMITS[vendor], pageBytes: 1024 * 1024 - 16 * 1024 };
34
44
  let state, sequence = 0;
35
45
  let counters = { requests: 0, received: 0, emitted: 0, records: 0 };
36
46
  if (checkpoint != null) {
37
47
  const encoded = JSON.stringify(checkpoint);
38
- if (Buffer.byteLength(encoded) >= 128 * 1024 || encoded.includes(key)) fail('INVALID_CHECKPOINT');
48
+ if (Buffer.byteLength(encoded) >= 128 * 1024 || key && encoded.includes(key)) fail('INVALID_CHECKPOINT');
39
49
  const cp = JSON.parse(encoded);
40
50
  shape(cp, ['version','vendor','fingerprint','sequence','counters','state']);
41
51
  if (cp.version !== 1 || cp.vendor !== vendor || cp.fingerprint !== fingerprint) fail('INVALID_CHECKPOINT');
42
52
  sequence = assertCount(cp.sequence, 2000);
43
53
  shape(cp.counters, ['requests','received','emitted','records']); object(cp.state);
44
- counters = Object.fromEntries(Object.keys(counters).map(name => [name, assertCount(cp.counters[name], name === 'requests' ? limits.requests : name === 'records' ? limits.records : limits.bytes)]));
54
+ counters = Object.fromEntries(Object.keys(counters).map(name => [name, assertCount(cp.counters[name], name === 'requests' ? limits.requests : name === 'records' ? limits.records : name === 'emitted' ? limits.emitted ?? limits.bytes : limits.bytes)]));
45
55
  state = cp.state;
46
56
  }
47
57
  const adapter = adapters[vendor];
48
- const origin = vendor === 'sentry' ? adapter.origins[inputs.organization?.region] : adapter.origin;
58
+ const origin = vendor === 'sentry' ? adapter.origins[inputs.organization?.region] : vendor === 'sorry' ? adapter.originOf(inputs) : adapter.origin;
49
59
  if (!origin) fail('INVALID_INPUT');
50
- const headers = { Accept: vendor === 'pagerduty' ? 'application/vnd.pagerduty+json;version=2' : 'application/json', Authorization: vendor === 'pagerduty' ? `Token token=${key}` : `Bearer ${key}` };
60
+ const headers = { Accept: vendor === 'pagerduty' ? 'application/vnd.pagerduty+json;version=2' : 'application/json',
61
+ ...(keyed ? { Authorization: vendor === 'pagerduty' ? `Token token=${key}` : `Bearer ${key}` } : {}) };
51
62
  const ctx = {
52
63
  inputs, state,
53
64
  get: client({ origin, headers, route: adapter.route, signal, limits, counters, key }),
@@ -58,12 +69,12 @@ export async function* exportVendor(vendor, { key, inputs = {}, checkpoint, sign
58
69
  item(value, nextState, name) {
59
70
  signal?.throwIfAborted();
60
71
  const bytes = Buffer.isBuffer(value) ? value : Buffer.from(value);
61
- if (bytes.length > 1024 * 1024 || (counters.emitted += bytes.length) > limits.bytes) fail('BYTE_LIMIT');
62
- if (bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
72
+ if (bytes.length > 1024 * 1024 || (counters.emitted += bytes.length) > (limits.emitted ?? limits.bytes)) fail('BYTE_LIMIT');
73
+ if (key && bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
63
74
  if (++sequence > 2000) fail('ITEM_LIMIT');
64
75
  const cp = { version: 1, vendor, fingerprint, sequence, counters: { ...counters }, state: nextState };
65
76
  const encoded = JSON.stringify(cp);
66
- if (Buffer.byteLength(encoded) >= 128 * 1024 || encoded.includes(key)) fail('CHECKPOINT_LIMIT');
77
+ if (Buffer.byteLength(encoded) >= 128 * 1024 || key && encoded.includes(key)) fail('CHECKPOINT_LIMIT');
67
78
  return { name: name ?? `${String(sequence).padStart(6,'0')}.part`, bytes, checkpoint: JSON.parse(encoded) };
68
79
  },
69
80
  };
@@ -0,0 +1,106 @@
1
+ import { object, list, assertCount, shape, fail } from './common.mjs';
2
+
3
+ // Sorry's public status page API, read anonymously from the page's own HTTPS
4
+ // address, as Lamp's Move in reads it (statuspage-import/sorry/fetch.ts,
5
+ // fetchSorryPublicResponses): /api/v1/status, the components and notices lists
6
+ // (25 a page, `meta.next_page`), then every notice's detail with its components
7
+ // and full update history. No key is sent or accepted.
8
+
9
+ const label = /^(?!-)[a-z0-9-]{1,63}(?<!-)$/u;
10
+ /** The page's public HTTPS origin from its host name alone: no scheme, port,
11
+ * path, credentials, IP address or local name can come from the inputs. */
12
+ export function originOf(inputs) {
13
+ const page = object(inputs.page);
14
+ if (Object.keys(page).some(key => key !== 'host')) fail('INVALID_INPUT');
15
+ const host = page.host;
16
+ if (typeof host !== 'string' || host.length > 253) fail('INVALID_INPUT');
17
+ const labels = host.split('.');
18
+ if (labels.length < 2 || !labels.every(part => label.test(part)) || /^[0-9]+$/u.test(labels.at(-1))
19
+ || ['localhost', 'local', 'internal', 'invalid', 'test'].includes(labels.at(-1))) fail('INVALID_INPUT');
20
+ return `https://${host}`;
21
+ }
22
+ export const route = url => /^\/api\/v1\/(?:status|components|notices(?:\/(?:0|[1-9][0-9]{0,127}))?)$/u.test(url.pathname)
23
+ && [...url.searchParams.keys()].every(key => key === 'page');
24
+
25
+ const resources = ['components', 'notices'];
26
+ /** The documented next page of one public list: the same collection, page n+1, or null. */
27
+ function nextPage(value, path, current) {
28
+ if (value == null) return null;
29
+ if (typeof value !== 'string' || value.length > 2048) fail('INVALID_PAGINATION');
30
+ let next;
31
+ try { next = new URL(value, 'https://sorry.invalid'); } catch { fail('INVALID_PAGINATION'); }
32
+ const params = [...next.searchParams];
33
+ if (next.username || next.password || next.hash || ![path, path.replace('/api/v1/', '/v1/')].includes(next.pathname)
34
+ || params.length !== 1 || params[0][0] !== 'page' || params[0][1] !== String(current + 1)) fail('INVALID_PAGINATION');
35
+ return `${path}?page=${current + 1}`;
36
+ }
37
+ /** Notice ids exactly as Sorry wrote them (large integers never pass through Number). */
38
+ function noticeIds(text) {
39
+ const ids = [];
40
+ JSON.parse(text, function (key, value, context) {
41
+ // A notice is the object with a subject; its components and updates have none.
42
+ if (key === 'id' && this && !Array.isArray(this) && Object.hasOwn(this, 'subject')) {
43
+ const lexeme = typeof value === 'string' ? value : context?.source;
44
+ if (typeof lexeme !== 'string' || !/^(?:0|[1-9][0-9]{0,127})$/u.test(lexeme)) fail('INVALID_RESPONSE');
45
+ ids.push(lexeme);
46
+ }
47
+ return value;
48
+ });
49
+ return ids;
50
+ }
51
+
52
+ /** The bundle Lamp's import door reads for its `sorry` source (door-plan.ts
53
+ * `doorExport`): the public page address, the saved responses under the file
54
+ * names Lamp's Sorry reader requires, each with its original text, and the
55
+ * acknowledgement that they are public. Every response here came from the
56
+ * page's own unauthenticated API, so it is public by construction; Lamp still
57
+ * refuses any private, unpublished or audience-scoped record, and the owner
58
+ * reviews the plan before anything is imported. */
59
+ export async function* run(ctx) {
60
+ const origin = originOf(ctx.inputs);
61
+ const s = ctx.state ?? { phase: 0, page: 1, total: null, rows: 0, notices: [], index: 0 };
62
+ shape(s, ['phase', 'page', 'total', 'rows', 'notices', 'index']);
63
+ assertCount(s.phase, 4); assertCount(s.page, 50); assertCount(s.rows, 5000); assertCount(s.index, 5000);
64
+ if (s.total !== null) assertCount(s.total, 5000);
65
+ if (!Array.isArray(s.notices) || s.notices.length > 1250 || s.notices.some(id => typeof id !== 'string' || !/^(?:0|[1-9][0-9]{0,127})$/u.test(id))) fail('INVALID_CHECKPOINT');
66
+ const file = (name, text, first = false) => (first ? '' : ',') + JSON.stringify({ name, text });
67
+ while (s.phase < 4) {
68
+ let bytes;
69
+ if (s.phase === 0) {
70
+ const { body, text } = await ctx.get('/api/v1/status');
71
+ const page = object(object(body).page);
72
+ if (typeof page.name !== 'string' || typeof page.url !== 'string') fail();
73
+ ctx.records(1);
74
+ s.phase = 1;
75
+ bytes = JSON.stringify({ source: 'sorry', publicUrl: origin, publicAccessAcknowledged: true }).slice(0, -1) + ',"files":[' + file('sorry-status.json', text, true);
76
+ } else if (s.phase <= 2) {
77
+ const resource = resources[s.phase - 1], path = `/api/v1/${resource}`;
78
+ const { body, text } = await ctx.get(s.page === 1 ? path : `${path}?page=${s.page}`);
79
+ object(body); const rows = list(body[resource], 25), meta = object(body.meta);
80
+ if (meta.count !== rows.length || !Number.isSafeInteger(meta.total_count) || s.total !== null && meta.total_count !== s.total) fail('INVALID_PAGINATION');
81
+ ctx.records(rows.length);
82
+ if (resource === 'notices') {
83
+ const ids = noticeIds(text);
84
+ if (ids.length !== rows.length || ids.some(id => s.notices.includes(id))) fail('DUPLICATE_ID');
85
+ s.notices.push(...ids);
86
+ }
87
+ s.rows += rows.length; s.total = meta.total_count;
88
+ const next = nextPage(meta.next_page, path, s.page);
89
+ if (s.rows > s.total || Boolean(next) !== s.rows < s.total || next && !rows.length) fail('INVALID_PAGINATION');
90
+ bytes = file(`sorry-${resource}${s.page === 1 ? '' : `-${s.page}`}.json`, text);
91
+ if (next) { if (++s.page > 50) fail('PAGE_LIMIT'); }
92
+ else { s.phase++; s.page = 1; s.total = null; s.rows = 0; }
93
+ } else {
94
+ if (s.index === s.notices.length) { s.phase = 4; yield ctx.item(']}\n', s); break; }
95
+ const id = s.notices[s.index];
96
+ const { body, text } = await ctx.get(`/api/v1/notices/${id}`);
97
+ const notice = object(object(body).notice);
98
+ const ids = noticeIds(text);
99
+ if (ids.length !== 1 || ids[0] !== id || !Array.isArray(notice.components) || !Array.isArray(notice.updates)) fail('INVALID_RESPONSE');
100
+ ctx.records(1 + notice.updates.length);
101
+ bytes = file(`sorry-notice-${id}.json`, text);
102
+ s.index++;
103
+ }
104
+ yield ctx.item(bytes, s);
105
+ }
106
+ }
package/cli/contract.mjs CHANGED
@@ -11,7 +11,7 @@ const definitions = {
11
11
  build: [['--site'], 'Production build with private local settings excluded and restored. --site instead writes the static run site (homepage, /run, docs) to its declared folder and prints that folder.'],
12
12
  verify: [[], 'Run the complete internal verification target.'],
13
13
  reset: [['--yes'], 'Confirmed removal of the declared local inventory only.'],
14
- deploy: [['--owner-name', '--environment', '--dry-run', '--site', '--demo', '--host', '--env-file', '--image', '--bind-address', '--http-port', '--https-port', '--remove', '--yes', '--set-secret', '--unset-secret', '--rotate-secret', '--reopen-installation-key'], 'Owner-account deployment and health; dry run builds and validates the local deployment plan, printing target names, bindings and migration names. No Cloudflare credentials or requests are needed and nothing changes remotely. Target secrets, permissions and remote readiness are checked only by a real deploy. Builds and deploy hooks run with the checkout\'s local settings files moved aside, and a build output holding one (.dev.vars, .env) is refused. Declared secrets come from the environment or --env-file (an owned 0600 KEY=VALUE file); a new Worker gets the local setup owner and fresh generated secrets when they are not given. rer keeps the secrets it generates in .rer/deploy/ENVIRONMENT.secrets.env (0600) and reuses them on every redeploy; it refuses to generate a replacement for one a live Worker holds, and --rotate-secret NAME[,NAME] replaces them on purpose. After a healthy deploy rer claims the setup owner on the hosted app with the saved setup owner name and the one-time setup token, then removes the token. If the installer declares an owner-name field and no name is saved, deploy asks once and saves it; --non-interactive requires --owner-name NAME before any upload. --set-secret NAME[,NAME] (each value from the environment or --env-file; repeat the flag or join names with commas) and --unset-secret NAME[,NAME] change those secrets of the deployed Worker without a build, all of them in one new Worker version or none, and record that version; a missing value refuses before any change. --reopen-installation-key KID temporarily binds a retired installation key from private custody, reseals restored dependency keys under the current key, verifies them and removes the previous binding. --remove --yes deletes what this environment\'s deploy record says rer created (Worker, D1, R2, KV, queues), reads back that each is gone, and touches nothing else. --site deploys the run site instead: an assets-only Worker on its custom domain with www redirected; with --dry-run it builds the site and prints the plan with no provider request. --demo deploys the hosted demo instead (deploy-demo.json): a front Worker on demo.<domain> giving each visitor their own signed-in, throwaway native Worker isolate of this seed with real public reads/checks and simulated external writes; --dry-run writes it to .rer/demo-host with no provider request; --demo --remove --yes deletes that demo (its Worker, demo.<domain> Custom Domain, each only while the record its deploy left proves it the demo\'s), reads back that each is gone, and is a no-op once it is.'],
14
+ deploy: [['--owner-name', '--environment', '--dry-run', '--site', '--demo', '--host', '--env-file', '--image', '--bind-address', '--http-port', '--https-port', '--remove', '--yes', '--set-secret', '--unset-secret', '--rotate-secret', '--reopen-installation-key'], 'Owner-account deployment and health; dry run builds and validates the local deployment plan, printing target names, bindings and migration names. No Cloudflare credentials or requests are needed and nothing changes remotely. Valid fixture senders on .test, .example, .invalid, .localhost and example.com, example.net or example.org (including subdomains) are accepted only in a dry run; real deploys refuse placeholder senders. Dry run never prompts for or requires a missing owner name, saves no name and bypasses custom deploy hooks and their owner-email preflight. Target secrets, permissions and remote readiness are checked only by a real deploy. Builds and deploy hooks run with the checkout\'s local settings files moved aside, and a build output holding one (.dev.vars, .env) is refused. Declared secrets come from the environment or --env-file (an owned 0600 KEY=VALUE file); a new Worker gets the local setup owner and fresh generated secrets when they are not given. rer keeps the secrets it generates in .rer/deploy/ENVIRONMENT.secrets.env (0600) and reuses them on every redeploy; it refuses to generate a replacement for one a live Worker holds, and --rotate-secret NAME[,NAME] replaces them on purpose. After a healthy deploy rer claims the setup owner on the hosted app with the saved setup owner name and the one-time setup token, then removes the token. For a real deploy, the managed adapter checks its hosted owner email before rer resolves the owner name, so its placeholder-email refusal comes first. If the installer declares an owner-name field and no name is saved, deploy asks once and saves it; --non-interactive requires --owner-name NAME before any upload. --set-secret NAME[,NAME] (each value from the environment or --env-file; repeat the flag or join names with commas) and --unset-secret NAME[,NAME] change those secrets of the deployed Worker without a build, all of them in one new Worker version or none, and record that version; a missing value refuses before any change. --reopen-installation-key KID temporarily binds a retired installation key from private custody, reseals restored dependency keys under the current key, verifies them and removes the previous binding. --remove --yes deletes what this environment\'s deploy record says rer created (Worker, D1, R2, KV, queues), reads back that each is gone, and touches nothing else. --site deploys the run site instead: an assets-only Worker on its custom domain with www redirected; with --dry-run it builds the site and prints the plan with no provider request. --demo deploys the hosted demo instead (deploy-demo.json): a front Worker on demo.<domain> giving each visitor their own signed-in, throwaway native Worker isolate of this seed with real public reads/checks and simulated external writes; --dry-run writes it to .rer/demo-host with no provider request; --demo --remove --yes deletes that demo (its Worker, demo.<domain> Custom Domain, each only while the record its deploy left proves it the demo\'s), reads back that each is gone, and is a no-op once it is.'],
15
15
  export: [['--target', '--output', '--origin', '--session-file'], 'For owner move-in, run rer dev and open /app/import: export people there, then upload through the destination door. Developer-only installation backup: complete supported archive of the local copy on this computer, with integrity and exclusions, written to --output. The hosted install cannot be exported yet: --origin or --target remote refuses (exit 2) before any effect and names the command that saves the hosted database.'],
16
16
  import: [['--target', '--input', '--journal', '--origin', '--session-file', '--yes', '--legacy-v1', '--acknowledge-unverified-metadata', '--acknowledge-unverified-schema-objects', '--output'], 'For owner move-in, run rer dev and open /app/import: upload, review and apply through the door. Developer-only installation recovery: confirmed atomic restore into an empty destination; --journal LATER_ARCHIVE also fences rows that installation erased since. Backup-only local v1 recovery requires --legacy-v1 --acknowledge-unverified-metadata and a new --output v2 archive; --journal requires normal v2 import. A v2 archive without schemaObjects requires --acknowledge-unverified-schema-objects, an empty destination and compatible destination migration history; its receipt records schemaUnverified. A fresh export from current rer is preferred; current archives need no acknowledgment.'],
17
17
  doctor: [[], 'Read-only installed tool, configuration and state diagnostics.', 'never'],
@@ -29,7 +29,7 @@ export const signinVerb = Object.freeze({ bootstrap: 'before', flags: [...common
29
29
  // Their project-package bootstrap uses the same contract as every checkout verb.
30
30
  export const authoringVerbs = Object.freeze({
31
31
  'adopt-edition': {bootstrap: 'before', flags: [...commonFlags], effects: 'Add edition.json and sources.lock.json for the existing tracked local source, preserving declared project identity and existing copy/composition records. Create an empty base-copies.json only when none exists. Review and stage the new authoring inputs; no source regeneration, install setup or commit.'},
32
- 'refresh-base': {bootstrap: 'before', flags: [...commonFlags, '--ref', '--base', '--record', '--offline', '--edition-migration'], effects: 'Consent to each declared edition edit with repeatable --edition-migration ID; refresh leaves the edit owed. Refresh recorded Base copies to --ref FULL_COMMIT, preserving acknowledged adaptations and applied migrations; announce removed singleton retirement. Deliver invoked workflow helpers atomically; adopt tracked known historical Verify templates and preserve divergent workflows with adoption notices. Acquire committed source from the declared Base repository (default https://github.com/runeditrun/base.git), or use --base CHECKOUT. --offline requires --base and never acquires remote source. Refuse incomplete Git checkouts without changing their records. Review the diff and run edition tests before committing.'},
32
+ 'refresh-base': {bootstrap: 'before', flags: [...commonFlags, '--ref', '--base', '--base-archive', '--record', '--offline', '--edition-migration'], effects: 'Consent to each declared edition edit with repeatable --edition-migration ID; refresh leaves the edit owed. Refresh recorded Base copies to --ref FULL_COMMIT, preserving acknowledged adaptations and applied migrations; announce removed singleton retirement. Deliver invoked workflow helpers atomically; adopt tracked known historical Verify templates and preserve divergent workflows with adoption notices. Acquire committed source from the declared Base repository (default https://github.com/runeditrun/base.git), or use --base CHECKOUT or --base-archive DIRECTORY with --ref FULL_COMMIT for an exported source tree. --offline requires --base and never acquires remote source. Refuse incomplete Git checkouts without changing their records. Review the diff and run edition tests before committing.'},
33
33
  });
34
34
  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]));
35
35
  export const releaseFlags = ['--repo', '--project', '--repository', '--output', '--ref', '--public-base', '--dry-run', '--local-verify', '--verify-workflow', '--scan-timeout-seconds', '--tag', '--clean-history', '--onto', '--env-file', '--allow-placeholder-mark', '--no-queue'];
@@ -0,0 +1,14 @@
1
+ import { RerError } from './errors.mjs';
2
+
3
+ // Approve only known ownership failures; arbitrary provider error text stays
4
+ // behind the usual redacted capture boundary.
5
+ export function demoDeployFailure(error, store) {
6
+ const codes = ['resource-name-collision', 'owned-resource-replaced', 'resource-not-in-record', 'uncertain-resource-create'];
7
+ if (!codes.includes(error.message)) return error;
8
+ const resource = typeof error.resource === 'string' && /^[a-z-]+:[a-z0-9.-]+$/.test(error.resource) ? ` ${error.resource}` : '';
9
+ const reason = error.ownershipReason === 'missing-annotation'
10
+ ? ': served version has no workers/message ownership annotation; unchanged secret-only history matching the previous rer deployment record could not be verified'
11
+ : error.ownershipReason === 'different-annotation' ? ': served version workers/message ownership annotation does not match this demo' : '';
12
+ const recovery = 'Nothing was changed. For your own demo, restore the verified previous rer deployment and its workers/message ownership annotation, then rerun the same command. For a foreign resource, choose another --environment name or remove it yourself.';
13
+ return new RerError('deploy_refused', store.redact(`Demo deploy refused: ${error.message}${resource}${reason}. ${recovery}`), 1);
14
+ }
@@ -10,6 +10,7 @@ import { cloudflare } from '../modules/deploy-kit/provider.mjs';
10
10
  import { RUNTIME_FILES } from '../modules/demo-host/native/config.mjs';
11
11
  import { prepareNative } from '../modules/demo-host/native/prepare.mjs';
12
12
  import { TESTING_SECRET } from '../modules/demo-host/measurement.mjs';
13
+ import { demoDeployFailure } from './demo-deploy-error.mjs';
13
14
  import { awaitCertificates } from '../modules/demo-host/certificate.mjs';
14
15
 
15
16
  export async function nativeDemoCommand(project, options, store, signal, out, operation, app, plan) {
@@ -52,7 +53,9 @@ export async function nativeDemoCommand(project, options, store, signal, out, op
52
53
  const required = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN', 'TURNSTILE_SITE_KEY', 'TURNSTILE_SECRET_KEY'];
53
54
  if (required.some(name => !process.env[name]?.trim())) throw missing(`${required.join(', ')} are required`);
54
55
  const api = cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN });
55
- config.migrations = await upgradeDemo({ api, plan, report: line => store.report(line) });
56
+ await store.step(`Checking ownership of the demo Worker ${plan.worker}`);
57
+ try { config.migrations = await upgradeDemo({ api, plan, report: line => store.report(line) }); }
58
+ catch (error) { throw demoDeployFailure(error, store); }
56
59
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(config, null, 2) + '\n');
57
60
  const testing = TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY);
58
61
  const measurement = testing ? process.env.RER_DEMO_MEASUREMENT_TOKEN || randomBytes(32).toString('base64url') : undefined;
@@ -28,7 +28,7 @@ export function tomlMain(source) {
28
28
  }
29
29
  }
30
30
  export function jsoncMain(source) {
31
- try { return JSON.parse(source.replace(/\/\*[\s\S]*?\*\/|^\s*\/\/.*$/gm, '')).main; } catch { return undefined; }
31
+ try { return parseJsonc(source).main; } catch { return undefined; }
32
32
  }
33
33
 
34
34
  // The deployed Worker entry (`main`) from the first of those files that exists.
@@ -22,10 +22,12 @@ export async function deployOwnerInput({ root, adapter, setupSeed, options, sign
22
22
  }
23
23
  const advice = 'Deploy needs your owner name; run rer deploy --owner-name "Your name" (or rer setup --owner-name "Your name"). Nothing was uploaded.';
24
24
  if (!displayName?.trim()) {
25
+ // A local plan never bootstraps an owner or asks for their name.
26
+ if (options.dryRun) return { input };
25
27
  // A recorded bootstrap keeps its owner; resolving inputs needs no provider read.
26
28
  const held = (await heldDeployments(root)).find(item => item.environment === (options.environment ?? 'production'));
27
29
  if (['claimed', 'kept'].includes(held?.record.ownerClaim?.outcome)) return { input };
28
- if (options.dryRun || options.nonInteractive || options.json || !process.stdin.isTTY) throw missing(advice);
30
+ if (options.nonInteractive || options.json || !process.stdin.isTTY) throw missing(advice);
29
31
  const prompt = createInterface({ input: process.stdin, output: process.stderr });
30
32
  const closed = new Promise((_, reject) => prompt.once('close', () => reject(missing(advice))));
31
33
  try { displayName = await Promise.race([prompt.question('Your name (hosted owner): ', { signal }), closed]); }
@@ -0,0 +1,58 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { contained } from './project.mjs';
3
+ import { deployConfigPaths } from './deploy-config.mjs';
4
+ import { parseJsonc } from './jsonc.mjs';
5
+ import { comments } from './business/parsers-syntax.mjs';
6
+ import { tomlEntries } from './business/parsers-config.mjs';
7
+ import { imageVariableSecrets } from '../modules/container/image-variables.mjs';
8
+
9
+ const id = 'deploy.vars-secrets';
10
+ const lineAt = (source, offset) => source.slice(0, offset).split('\n').length;
11
+ const variablePath = path => path[0] === 'vars' && path.length === 2 ||
12
+ path[0] === 'env' && path[2] === 'vars' && path.length === 4;
13
+
14
+ async function sourceFile(root, path) {
15
+ try { return await readFile(await contained(root, path, { optional: true }), 'utf8'); }
16
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
17
+ }
18
+
19
+ // Reuse the image builder's JSONC reader and binding comparison. Source checks
20
+ // deliberately reject even empty placeholders and legacy public-mail overlaps:
21
+ // a declaration belongs in vars or secrets, before any build normalizes it.
22
+ export async function deployVariableClause(root, adapter) {
23
+ try {
24
+ const declarationPath = adapter?.deploy?.declaration ?? 'deploy.json';
25
+ const declarationSource = await sourceFile(root, declarationPath);
26
+ if (declarationSource === null) return { id, ok: true };
27
+ const secretLocations = new Map();
28
+ const declaration = parseJsonc(declarationSource, (path, offset) => {
29
+ if (path.length === 3 && path[0] === 'secrets' && path[2] === 'binding') secretLocations.set(path[1], lineAt(declarationSource, offset));
30
+ });
31
+ // deploy.json uses strict JSON in both native and container builds.
32
+ try { JSON.parse(declarationSource); } catch { throw Error(`${declarationPath}: invalid JSON declaration.`); }
33
+ const secrets = declaration.secrets ?? [];
34
+ if (!Array.isArray(secrets) || secrets.some(secret => typeof secret?.binding !== 'string')) throw Error(`${declarationPath}: secrets must be an array of binding declarations.`);
35
+ const overlaps = [];
36
+ for (const path of deployConfigPaths(adapter)) {
37
+ const source = await sourceFile(root, path);
38
+ if (source === null) continue;
39
+ const entries = [];
40
+ let config;
41
+ try {
42
+ if (path.endsWith('.toml')) entries.push(...tomlEntries(comments(source).clean, { locations: true }));
43
+ else config = parseJsonc(source, (path, offset) => entries.push({ path, offset }));
44
+ } catch (error) { throw Error(`${path}: ${error.message}`); }
45
+ for (const entry of entries.filter(entry => variablePath(entry.path))) {
46
+ const binding = entry.path.at(-1);
47
+ // TOML entries describe declared keys; JSONC values come from the very
48
+ // same reader the node image builder uses (including escaped key names).
49
+ const vars = config ? entry.path.slice(0, -1).reduce((value, key) => value?.[key], config) : { [binding]: entry.value };
50
+ const matched = new Set(imageVariableSecrets(vars ?? {}, secrets).map(secret => secret.binding));
51
+ if (!matched.has(binding)) continue;
52
+ for (const [index, secret] of secrets.entries()) if (secret.binding === binding) overlaps.push(
53
+ `${binding}: ${path}:${lineAt(source, entry.offset)} (${entry.path.slice(0, -1).join('.')}) and ${declarationPath}:${secretLocations.get(index)} (secrets)`);
54
+ }
55
+ }
56
+ return overlaps.length ? { id, ok: false, message: overlaps.join('\n') + '\nremove it from wrangler vars; the deploy sets it as a secret' } : { id, ok: true };
57
+ } catch (error) { return { id, ok: false, message: error.message }; }
58
+ }
@@ -0,0 +1,157 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { spawnSync } from 'node:child_process';
3
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
4
+ import { resolve, relative, isAbsolute } from 'node:path';
5
+ import { parseContract } from '../foundation/runtime/seed-contract.mjs';
6
+ import { RerError } from './errors.mjs';
7
+
8
+ export const clausesHelp = `Usage: rer door clauses (--write | --check) [--cwd DIR]
9
+
10
+ Synchronize BASE-AGENT-001 through BASE-AGENT-013 in CONTRACT.md.
11
+ --write adds or updates Base's requirements, preserving existing status,
12
+ evidence and gaps, then runs the seed's contract-docs write and check.
13
+ --check is read-only and names missing or stale clauses (exit 1).
14
+ Seed-owned clauses and the judged revision stay unchanged. Newly added clauses
15
+ start missing. Regenerate the seed's source lock last, before committing.
16
+ `;
17
+ const fail = message => { throw new RerError('agent_clauses_invalid', message, 1); };
18
+ const read = file => readFileSync(file, 'utf8');
19
+ const area = '### Agent surface (Base-delivered)';
20
+
21
+ // Directory records deliver new non-code files on refresh, too. A recorded
22
+ // clause copy selects its immutable text; older runtime pins without this file
23
+ // can still sync their contract using the CLI's independently delivered source.
24
+ function canonical(root) {
25
+ const record = resolve(root, 'base-copies.json');
26
+ if (existsSync(record)) {
27
+ const origin = 'modules/agent-door/CLAUSES.md';
28
+ const candidates = JSON.parse(read(record)).copies?.filter(copy => origin === copy.originPath || origin.startsWith(copy.originPath + '/')) ?? [];
29
+ if (candidates.length > 1) fail('Overlapping agent-door copy records; repair base-copies.json first.');
30
+ if (candidates.length) {
31
+ const copy = candidates[0], name = copy.path + origin.slice(copy.originPath.length), file = resolve(root, name);
32
+ const inside = relative(root, file);
33
+ if (isAbsolute(inside) || inside === '..' || inside.startsWith('../')) fail('Agent clause copy must stay inside this seed.');
34
+ if (!copy.files?.[name]) return read(new URL('../modules/agent-door/CLAUSES.md', import.meta.url));
35
+ if (!existsSync(file)) fail(`Restore the recorded agent clause source ${name} before syncing.`);
36
+ const bytes = readFileSync(file);
37
+ if (createHash('sha256').update(bytes).digest('hex') !== copy.files[name]) fail(`Recorded agent clause source ${name} has local edits; restore or refresh it first.`);
38
+ return bytes.toString('utf8');
39
+ }
40
+ }
41
+ return read(new URL('../modules/agent-door/CLAUSES.md', import.meta.url));
42
+ }
43
+
44
+ function definitions(source) {
45
+ const blocks = [...source.matchAll(/^#### (BASE-AGENT-\d{3}) — ([^\n]+)\n([\s\S]*?)(?=^#{1,4} |$(?![\s\S]))/gm)];
46
+ const result = blocks.map(([, id, title, body]) => ({ id, title, text: `#### ${id} — ${title}\n${body.trimEnd()}`, row: source.split('\n').find(line => line.startsWith(`| \`${id}\` |`)) }));
47
+ for (const clause of result) {
48
+ const acceptance = clause.text.split('\n**Acceptance.**\n')[1];
49
+ const points = [...(acceptance ?? '').matchAll(/^(\d+)\. /gm)];
50
+ if (!points.length || points.length > 8 || points.some((point, index) => Number(point[1]) !== index + 1)) fail(`${clause.id} needs one to eight consecutive acceptance points in its canonical source.`);
51
+ }
52
+ const ids = Array.from({ length: 13 }, (_, i) => `BASE-AGENT-${String(i + 1).padStart(3, '0')}`);
53
+ if (result.map(item => item.id).join() !== ids.join() || result.some(item => !item.row)) fail('Canonical agent clauses must define BASE-AGENT-001 through 013 with initial Status rows.');
54
+ return result;
55
+ }
56
+
57
+ function headings(lines) {
58
+ let fence;
59
+ return lines.flatMap((line, index) => {
60
+ const mark = /^\s*(`{3,}|~{3,})/.exec(line)?.[1];
61
+ if (mark) { if (!fence) fence = mark[0]; else if (fence === mark[0]) fence = undefined; return []; }
62
+ const match = !fence && /^(#{1,6}) ([^\r\n]+)/.exec(line);
63
+ return match ? [{ index, level: match[1].length, title: match[2] }] : [];
64
+ });
65
+ }
66
+
67
+ // contract-docs owns summary generation. Suppress only its three derived rows
68
+ // for structural validation while a contract is awaiting regeneration.
69
+ function withoutSummary(text) {
70
+ return text.replace(/^(\| Clauses \| Done \| Partial \| Missing \| Total \|\r?\n[^\n]*\n)((?:\|[^\n]*\n)+)/m, (_, header, rows) => header + rows.replace(/[^\r\n]+/g, ''));
71
+ }
72
+
73
+ function synchronize(original, clauses) {
74
+ const parsed = parseContract(withoutSummary(original));
75
+ const lines = original.match(/[^\n]*\n|[^\n]+$/g) ?? [], outline = headings(lines);
76
+ const newline = original.includes('\r\n') ? '\r\n' : '\n';
77
+ const edits = [], missing = [], changed = [];
78
+ const target = outline.find(item => item.level === 2 && item.title === 'Target clauses');
79
+ if (!target) fail('CONTRACT.md needs the standard ## Target clauses section. Convert the contract before syncing.');
80
+ const targetEnd = outline.find(item => item.index > target.index && item.level <= 2)?.index ?? lines.length;
81
+ for (const clause of clauses) {
82
+ const old = parsed.clauses.find(item => item.id === clause.id);
83
+ if (!old) { missing.push(clause); changed.push(`${clause.id} missing`); continue; }
84
+ if (!lines[old.line - 1].startsWith('#### ') || old.kind !== 'target') fail(`${clause.id} must be a standard level-four Target clause before syncing; preserve its judgment when converting.`);
85
+ const start = old.line - 1, end = outline.find(item => item.index > start)?.index ?? lines.length;
86
+ const before = lines.slice(start, end).join(''), trailing = /\s*$/.exec(before)[0];
87
+ const after = clause.text.replaceAll('\n', newline) + trailing;
88
+ const row = lines[old.row - 1];
89
+ // A changed canonical title updates only the title cell, never the seed's
90
+ // kind, status, evidence or gap cells (or their original spacing).
91
+ const updatedRow = row.replace(/^(\s*\|[^|]*\|)([^|]*)(\|.*)/, (_, first, title, rest) => first + title.replace(/\S(?:[\s\S]*\S)?/, clause.title) + rest);
92
+ if (before !== after || row !== updatedRow) {
93
+ changed.push(`${clause.id} stale`);
94
+ edits.push({ start, end, text: after }, { start: old.row - 1, end: old.row, text: updatedRow });
95
+ }
96
+ }
97
+ if (missing.length) {
98
+ const delivered = outline.find(item => item.index > target.index && item.index < targetEnd && lines[item.index].trimEnd() === area);
99
+ const agent = outline.find(item => item.index > target.index && item.index < targetEnd && item.level === 3 && item.title === 'Agent surface');
100
+ const at = delivered ? outline.find(item => item.index > delivered.index && item.level <= 3)?.index ?? targetEnd : agent?.index ?? targetEnd;
101
+ const text = (delivered ? '' : area + '\n\n') + missing.map(item => item.text).join('\n\n') + '\n\n';
102
+ edits.push({ start: at, end: at, text: (at && !lines[at - 1].trim() ? '' : newline) + text.replaceAll('\n', newline) });
103
+ const status = outline.find(item => item.level === 2 && item.title === 'Status');
104
+ const header = lines.findIndex((line, index) => index > status?.index && /^\| Clause \| Title \| Kind \| Status \| Evidence \| Gap \|/.test(line));
105
+ if (header < 0) fail('CONTRACT.md needs the standard Status clause table.');
106
+ let end = header + 2;
107
+ while (lines[end]?.trimStart().startsWith('|')) end++;
108
+ edits.push({ start: end, end, text: (end && !lines[end - 1].endsWith('\n') ? newline : '') + missing.map(item => item.row).join(newline) + newline });
109
+ }
110
+ for (const edit of edits.sort((a, b) => b.start - a.start)) lines.splice(edit.start, edit.end - edit.start, edit.text);
111
+ const text = lines.join('');
112
+ parseContract(withoutSummary(text));
113
+ return { text, changed };
114
+ }
115
+
116
+ function generator(root) {
117
+ const agents = resolve(root, 'AGENTS.md');
118
+ const declared = existsSync(agents) ? [...read(agents).matchAll(/^Lander-Regenerate:\s*(.+)$/gm)].map(match => match[1].trim()) : [];
119
+ const command = declared.find(line => /contract-docs\.mjs\s+write\b/.test(line)) ?? 'node src/base/contract-docs.mjs write';
120
+ if (!declared.some(line => /contract-docs\.mjs\s+write\b/.test(line)) && !existsSync(resolve(root, 'src/base/contract-docs.mjs'))) fail('No contract-docs generator found. Restore the seed\'s Lander-Regenerate contract generator before writing.');
121
+ return { write: command, check: command.replace(/(contract-docs\.mjs\s+)write\b/, '$1check') };
122
+ }
123
+ function generate(command, root, env) {
124
+ const result = spawnSync(command, { cwd: root, env, shell: true, stdio: 'inherit' });
125
+ if (result.error || result.status !== 0) fail(`Contract generation failed: ${command}. Inspect the diff and repair the generator before committing.`);
126
+ }
127
+
128
+ export async function doorClausesCommand(args, env = process.env) {
129
+ if (args.includes('--help') || args.includes('-h')) { console.log(clausesHelp); return 0; }
130
+ let root = process.cwd(), mode;
131
+ try {
132
+ for (let i = 0; i < args.length; i++) {
133
+ const arg = args[i];
134
+ if (arg === '--cwd' && args[i + 1] && !args[i + 1].startsWith('-')) root = resolve(args[++i]);
135
+ else if (['--write', '--check'].includes(arg) && !mode) mode = arg;
136
+ else throw new RerError('invalid_arguments', clausesHelp, 2);
137
+ }
138
+ if (!mode) throw new RerError('invalid_arguments', clausesHelp, 2);
139
+ root = resolve(root);
140
+ const file = resolve(root, 'CONTRACT.md');
141
+ if (!existsSync(file)) fail('No CONTRACT.md found. Run rer door clauses from the seed root or pass --cwd DIR.');
142
+ const original = read(file), { text, changed } = synchronize(original, definitions(canonical(root)));
143
+ if (changed.length) console.log(`Agent clauses: ${changed.join(', ')}.`);
144
+ if (mode === '--check' && changed.length) return 1;
145
+ const commands = generator(root);
146
+ if (mode === '--write') {
147
+ if (text !== original) writeFileSync(file, text);
148
+ generate(commands.write, root, env);
149
+ }
150
+ generate(commands.check, root, env);
151
+ console.log(changed.length ? `Synchronized ${changed.length} Base-delivered agent clauses; existing judgments preserved. Regenerate the source lock last.` : 'Base-delivered agent clauses are current; no clause changes.');
152
+ return 0;
153
+ } catch (error) {
154
+ if (!(error instanceof RerError)) throw error;
155
+ console.error(error.message); return error.exitCode;
156
+ }
157
+ }