cursedops 0.2.0 → 0.2.1

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.
package/README.md CHANGED
@@ -10,8 +10,8 @@ bun add cursedops
10
10
  |---|---|
11
11
  | `cursedops/roots` | finding a generation's roots, and a checkout's package root, without knowing a path |
12
12
  | `cursedops/launchd` | installing, replacing and removing a macOS launchd user agent |
13
- | `cursedops/smoke` | the scaffolding of a deployed smoke — the ledger, the fetch, the DNS hint, the exit code |
14
- | `cursedops/serve` | the static tier's four helpers — the path-traversal guard, the MIME table, the hashed-asset test, the crash handlers |
13
+ | `cursedops/smoke` | the scaffolding of a deployed smoke — the ledger, the fetch, the DNS hint, the exit code — and the one check no app owns: every address of a deployment serving the same built client |
14
+ | `cursedops/serve` | the static tier's four helpers — the path-traversal guard, the MIME table, the hashed-asset test, the crash handlers — and the API floor: the predicates, the trailing-slash normaliser and the default 404 body that keep an unmatched `/api/...` from ever being answered with the app shell |
15
15
 
16
16
  Bun, zero runtime dependencies, ships TypeScript source. Nothing here knows an app's
17
17
  name, a hostname, a port or a route.
@@ -42,6 +42,15 @@ So the entry rule here is arithmetic, not judgement:
42
42
  A fourth rule follows from the first three: **an addition that only one app will use
43
43
  does not go in.** That is a library that exists to be refactored later.
44
44
 
45
+ 🔴 **`sameDeployedShell` is the one entry that rule 1 did not let in, and it is recorded
46
+ here rather than quietly excepted.** It was written once, by `patterns`, not twice —
47
+ what admits it is rule 3 at a size rule 1 cannot measure: the fault it catches was
48
+ invisible for six days on the app that had it, and the other five apps behind or moving
49
+ behind an edge Worker still cannot tell a working deploy from a working cache. A check
50
+ told ONE NAME is a check about that name; this one is about a property every deployment
51
+ with two addresses has. It buys the exception by being pinned to a failure it has
52
+ actually caught — `src/smoke.test.ts` runs it against the genuine 2026-09-12 shell.
53
+
45
54
  ### What was deliberately left in the apps
46
55
 
47
56
  The comparison that produced this library found three areas that are genuinely
@@ -111,7 +120,26 @@ await guarded(smoke, "gate", async () => {
111
120
  smoke.finish(); // exits 0, 1 or 2
112
121
  ```
113
122
 
114
- No checks, ever. The checks are the part `desk` and `flix` wrote differently on purpose,
123
+ One check, and it is about the ADDRESSES of a deployment rather than about any app:
124
+
125
+ ```ts
126
+ import { sameDeployedShell } from "cursedops/smoke";
127
+
128
+ // every address of one deployment, not "edge vs origin" — an app may have no
129
+ // origin left. A Worker's second address is its uncached *.workers.dev preview.
130
+ await sameDeployedShell(smoke, [smoke.base, `http://127.0.0.1:${port}`]);
131
+ ```
132
+
133
+ 🔴 It refuses when the addresses name different built assets, and it records that as an
134
+ **edge fault (exit 2), never a rollback** — the origin is serving what this checkout
135
+ built, and reverting it cannot reach the Worker, cache rule or Assets binding in front.
136
+ Measured 2026-09-18 on `patterns`: the edge served a shell built 2026-09-12 out of a
137
+ Worker nobody knew about, and every automated surface stayed green for **six days**.
138
+ Only same-origin `.js`/`.css` references are compared, because the zone injects a Web
139
+ Analytics beacon in front of the edge and not on loopback — a check that reds on a
140
+ healthy deploy is one somebody turns off.
141
+
142
+ Nothing else. The checks are the part `desk` and `flix` wrote differently on purpose,
115
143
  and a shared kit that starts absorbing route lists and health-payload shapes is how the
116
144
  last one reached 277 files.
117
145
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "cursedops",
3
- "version": "0.2.0",
4
- "description": "The build-and-ops answers this generation's apps wrote independently and identically: finding a generation's roots without knowing a path, macOS launchd agent install/replace/remove, the scaffolding of a deployed smoke, and the static-serving helpers eight apps copied — the path-traversal guard among them. Mechanism only — no app knows its name from here. Bun, zero dependencies, ships source.",
3
+ "version": "0.2.1",
4
+ "description": "The build-and-ops answers this generation's apps wrote independently and identically: finding a generation's roots without knowing a path, macOS launchd agent install/replace/remove, the scaffolding of a deployed smoke, and the static-serving helpers eight apps copied — the path-traversal guard and the API floor among them. Mechanism only — no app knows its name from here. Bun, zero dependencies, ships source.",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "typecheck": "tsc -p tsconfig.json --noEmit",
8
8
  "lint": "biome check .",
9
9
  "test": "bun test src",
10
- "paths": "bun ../../tools/check-paths.ts .",
10
+ "paths": "bun run scripts/paths.ts",
11
11
  "verify": "bun run paths && bun run typecheck && bun run lint && bun run test",
12
12
  "prepublishOnly": "bun run verify"
13
13
  },
package/src/serve.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * The four helpers every app's `src/server/serve.ts` wrote the same way — the
3
- * path-traversal guard among them.
2
+ * The helpers every app's `src/server/serve.ts` wrote the same way — the
3
+ * path-traversal guard among them, and the API floor.
4
4
  *
5
5
  * ## Why this is a library and not eight copies
6
6
  *
@@ -47,6 +47,38 @@
47
47
  *
48
48
  * `serve.test.ts` drives both halves, including the symlink escape, because a guard
49
49
  * that has only ever been seen passing is a guard nobody has proved can fail.
50
+ *
51
+ * ## 🔴 The API floor, added 2026-09-18 — and why it is here rather than in an app
52
+ *
53
+ * The second family in this file, and it arrives by the same arithmetic. `station` and
54
+ * `patterns` each wrote a floor — a rule that an unmatched `/api/...` is answered with a
55
+ * phrase and never with the single-page shell — independently, in different shapes, for
56
+ * the same incident. That is entry rule 1 twice over. Rule 2 holds because nothing below
57
+ * looks anything up: the app's name, its commit and the body it chooses to send all
58
+ * arrive as arguments, and there is no Hono type in this file, so the three lines of
59
+ * `app.all(...)` wiring stay in the app that owns the router. Rule 3 is the measurement:
60
+ *
61
+ * > 2026-09-17. `station`'s Openclaw page had never loaded on any commit. The client
62
+ * > asked for `/api/openclaw/`; Hono mounts that sub-app's `get("/")` at
63
+ * > `/api/openclaw` — `mergePath("/api/openclaw", "/")` drops the trailing slash — and
64
+ * > then matches strictly, so the request matched nothing, fell into `app.get("*")`,
65
+ * > and came back as 1.7 KB of `index.html` with a **200**. The client's honest reading
66
+ * > of an HTML body was *"that is what an API older than this page looks like — deploy
67
+ * > this commit and reload"*, which sent the owner at a deploy that could not have
68
+ * > helped.
69
+ *
70
+ * Both halves of that are here, because a floor without the normaliser fixes the symptom
71
+ * the owner did not have: {@link canonicalApiPath} makes the trailing slash a non-event,
72
+ * and {@link isApiPath} + {@link apiNotFoundBody} make every remaining miss a JSON 404
73
+ * that names the route, the app and the commit answering. A wrong path says "wrong path";
74
+ * a stale process says which commit it is.
75
+ *
76
+ * 🔴 **{@link apiNotFoundBody} is the DEFAULT body, not the only one.** `patterns` sends
77
+ * `{status, code, error}` through its own `reply.refuse` and deliberately says less —
78
+ * its floor faces the public internet, where the app's name and commit are one bit more
79
+ * than a scanner should get. An app that knows something this library cannot keeps its
80
+ * own body and takes the two predicates; that is the `cwip/asset-budget` shape, and it is
81
+ * what stops this growing into the kit it replaced.
50
82
  */
51
83
 
52
84
  import { existsSync, realpathSync, statSync } from "node:fs";
@@ -141,6 +173,86 @@ export function fileWithin(root: string, urlPath: string): string | null {
141
173
  * the whole of rule 2: the only app-shaped thing these four helpers touch arrives
142
174
  * as an argument.
143
175
  */
176
+ /** The API namespace. See the module header: nothing under it may answer with the shell. */
177
+ export const API_PREFIX = "/api";
178
+
179
+ /**
180
+ * Is this path inside the API namespace? `/api` itself counts; `/apiary` does not.
181
+ *
182
+ * 🔴 The namespace ITSELF is the case every hand-rolled floor missed. A Hono
183
+ * `app.all("/api/*")` does not match a bare `/api` — the pattern requires the slash — so
184
+ * the one path most likely to be typed by hand fell through to the shell in the two apps
185
+ * that already believed they had a floor.
186
+ */
187
+ export const isApiPath = (urlPath: string): boolean =>
188
+ urlPath === API_PREFIX || urlPath.startsWith(`${API_PREFIX}/`);
189
+
190
+ /**
191
+ * The path an API request SHOULD have asked for, or `null` when it already did.
192
+ *
193
+ * 🔴 One rule, and it is the one that broke the Openclaw page: a trailing slash on an API
194
+ * path is never meaningful. Hono mounts a sub-app's `get("/")` at the mount point WITHOUT
195
+ * a trailing slash and then matches strictly, so `/api/openclaw/` is a different path
196
+ * from `/api/openclaw` and matched nothing at all. Normalising once, in front of the
197
+ * router, means no caller anywhere can spell it the losing way again — which a fix in the
198
+ * one client module that happened to do it would not have bought.
199
+ *
200
+ * The query string is untouched: only the path is rewritten.
201
+ */
202
+ export function canonicalApiPath(urlPath: string): string | null {
203
+ if (!isApiPath(urlPath) || !urlPath.endsWith("/")) return null;
204
+ const trimmed = urlPath.replace(/\/+$/, "");
205
+ return trimmed === urlPath ? null : trimmed;
206
+ }
207
+
208
+ /**
209
+ * The same normalisation, applied to a whole request. Returns the request unchanged when
210
+ * there is nothing to normalise, so the hot path allocates nothing.
211
+ *
212
+ * 🔴 Wrap it around the ONE function every response leaves through — the `fetch` a host
213
+ * hands `Bun.serve` or exports from a Worker — and never as a Hono middleware. Hono runs
214
+ * middleware in registration order against handlers registered AFTER it, and an app's
215
+ * routes are mounted before the host is built; a middleware added at that point runs for
216
+ * none of them. It also cannot change which route matches, which is the entire job here.
217
+ */
218
+ export function canonicalApiRequest(request: Request): Request {
219
+ const url = new URL(request.url);
220
+ const canonical = canonicalApiPath(url.pathname);
221
+ if (canonical === null) return request;
222
+ url.pathname = canonical;
223
+ return new Request(url, request);
224
+ }
225
+
226
+ /**
227
+ * What an unmatched API path answers — a JSON 404 that names itself.
228
+ *
229
+ * 🔴 The commit is in the body on purpose. The two reasons a route is missing are "this
230
+ * path never existed" and "this process is older than the page that asked", and they need
231
+ * different hands; without the commit a client can only guess, and the guess one printed
232
+ * for the owner sent him to deploy a commit that would not have fixed anything.
233
+ *
234
+ * 🔴 Safe to publish only where the floor sits BEHIND the app's gate, which is where every
235
+ * caller puts it — an unauthenticated request is answered 401 by the gate, matched or not.
236
+ * An app whose API namespace is open sends its own, terser body instead; see the module
237
+ * header.
238
+ */
239
+ export function apiNotFoundBody(
240
+ method: string,
241
+ urlPath: string,
242
+ app: string,
243
+ commit: string | null | undefined,
244
+ ): { error: string; route: string; app: string; commit: string | null } {
245
+ return {
246
+ error:
247
+ `no such route: ${method} ${urlPath}. ${app} is serving ${commit ?? "an unreadable commit"} ` +
248
+ "and has no such API route — either the path is wrong, or this process is older than the page " +
249
+ "that asked for it. It is NOT the app shell: nothing under /api/ is ever answered with HTML.",
250
+ route: `${method} ${urlPath}`,
251
+ app,
252
+ commit: commit ?? null,
253
+ };
254
+ }
255
+
144
256
  export function installCrashHandlers(name: string): void {
145
257
  process.on("uncaughtException", (error) => {
146
258
  console.error(`[${name}] uncaught exception — exiting`, error);
package/src/smoke.ts CHANGED
@@ -39,6 +39,22 @@
39
39
  * exactly where it is. {@link Smoke.finish} is the single place that code is
40
40
  * decided, so the two apps reading it cannot disagree about what a `2` meant.
41
41
  *
42
+ * ## The ONE check that lives here, and why it is not an app's to write
43
+ *
44
+ * {@link sameDeployedShell} is the exception to "no checks", and the paragraph
45
+ * above is its argument rather than an argument against it: every incident in that
46
+ * list is *the deploy said yes and the visitor got something else*, and asking one
47
+ * address can never see that. It is a check about the ADDRESSES of a deployment,
48
+ * which every app has and no app owns — not about any app's routes, payloads or
49
+ * titles, which stay out.
50
+ *
51
+ * Measured 2026-09-18 on `patterns`: the origin served the shell this checkout
52
+ * built, `patterns.cursedalchemy.com` served one built on 2026-09-12 out of a
53
+ * Worker nobody knew about, and **every automated surface was green for six days.**
54
+ * The app-local implementation that found it — `apps/patterns/scripts/worker-smoke.ts`,
55
+ * `CROSS_CHECKS` — is what this generalises, because five more apps are moving
56
+ * behind an edge Worker and a check told ONE NAME is a check about that name.
57
+ *
42
58
  * ## What this module will never grow
43
59
  *
44
60
  * No route lists, no health-payload shapes, no upload sizes, no titles. Those are
@@ -224,3 +240,184 @@ export async function guarded(smoke: Smoke, check: string, body: () => Promise<v
224
240
  smoke.record(check, false, `could not complete against ${smoke.base}: ${failed.message}${smoke.dnsHint(failed)}`);
225
241
  }
226
242
  }
243
+
244
+ /** What one address said when it was asked for the shell. */
245
+ export interface AddressShell {
246
+ /** The address as it was passed in. */
247
+ address: string;
248
+ /** Same-origin built files the shell references, de-duplicated and sorted. */
249
+ assets: string[];
250
+ }
251
+
252
+ export interface ShellAgreement {
253
+ /** True only when every address named the same non-empty set of built files. */
254
+ agreed: boolean;
255
+ /** One line naming which address served what — the whole point on a failure. */
256
+ detail: string;
257
+ shells: AddressShell[];
258
+ }
259
+
260
+ export interface ShellOptions {
261
+ /** The document to compare. The shell, so `/` unless an app's entry is elsewhere. */
262
+ path?: string;
263
+ /** Per-request ceiling, matching {@link SmokeOptions.timeoutMs}'s default. */
264
+ timeoutMs?: number;
265
+ /** The cache-busting query value. Injected so a test can be deterministic. */
266
+ cacheBust?: () => string;
267
+ /** Injected for tests. Defaults to the global `fetch`. */
268
+ fetch?: (url: string, init: RequestInit) => Promise<Response>;
269
+ }
270
+
271
+ /**
272
+ * `src="…"` and `href="…"`, either quote. Attribute scan, not a parser — see
273
+ * {@link builtAssetsIn}. The leading whitespace is what keeps `data-src` and
274
+ * `xlink:href` out of the comparison.
275
+ */
276
+ const REFERENCE = /\s(?:src|href)\s*=\s*(?:"([^"]*)"|'([^']*)')/gi;
277
+ /** What a bundler emits and a shell names. Extensions, because hashes are not universal. */
278
+ const BUILT_FILE = /\.(?:js|mjs|cjs|css)$/i;
279
+
280
+ function hostOf(address: string): string | null {
281
+ try {
282
+ return new URL(address).host;
283
+ } catch {
284
+ return null;
285
+ }
286
+ }
287
+
288
+ /**
289
+ * The built files a shell references, as paths — the stable part of an HTML body.
290
+ *
291
+ * 🔴 **Same-origin only, and that is load-bearing rather than tidy.** The two
292
+ * addresses of one deployment legitimately differ in what a third party injected:
293
+ * on the app this check was built for, the zone's Web Analytics Automatic Setup
294
+ * appends `static.cloudflareinsights.com/beacon.min.js` at the edge and not on
295
+ * loopback. Comparing bodies, or comparing every `.js` a shell names, fails on that
296
+ * every time — a check that reds on a healthy deploy is one somebody turns off.
297
+ *
298
+ * Nothing here parses HTML: a bundler-emitted shell is machine-written, and the
299
+ * comparison only has to be STABLE between two copies of the same one.
300
+ */
301
+ export function builtAssetsIn(html: string, address: string): string[] {
302
+ const host = hostOf(address);
303
+ const found = new Set<string>();
304
+ for (const match of html.matchAll(REFERENCE)) {
305
+ const raw = (match[1] ?? match[2] ?? "").trim();
306
+ if (!raw) continue;
307
+ let url: URL;
308
+ try {
309
+ url = new URL(raw, address);
310
+ } catch {
311
+ continue;
312
+ }
313
+ if (url.host !== host) continue;
314
+ if (!BUILT_FILE.test(url.pathname)) continue;
315
+ found.add(url.pathname);
316
+ }
317
+ return [...found].sort();
318
+ }
319
+
320
+ /**
321
+ * Ask every address of ONE deployment for the shell, and say whether they agree.
322
+ *
323
+ * 🔴 The assertion is **"every address agrees"**, not "the edge matches the
324
+ * origin" — which is the shape that survives an app having no origin left to
325
+ * compare against, and the reason this takes a list rather than a pair. Two is the
326
+ * usual list (the public hostname and `http://127.0.0.1:<port>`); a Worker's is the
327
+ * custom domain and its `*.workers.dev` preview, because a preview URL is not
328
+ * behind the cache.
329
+ *
330
+ * Every request is cache-busted, because the question is what that address HAS,
331
+ * not what it hands a repeat visitor.
332
+ */
333
+ export async function compareDeployedShells(addresses: string[], options: ShellOptions = {}): Promise<ShellAgreement> {
334
+ const path = options.path ?? "/";
335
+ const timeoutMs = options.timeoutMs ?? 20_000;
336
+ const cacheBust = options.cacheBust ?? (() => Math.random().toString(36).slice(2));
337
+ const send = options.fetch ?? ((url: string, init: RequestInit) => fetch(url, init));
338
+
339
+ // 🔴 Loud, not silent. One address cannot disagree with itself, so a run that
340
+ // quietly passed here would be a skip wearing a pass — the exact reading that
341
+ // let a six-day-old shell sit behind six green surfaces.
342
+ if (addresses.length < 2) {
343
+ return {
344
+ agreed: false,
345
+ detail:
346
+ `needs at least two addresses of the same deployment and got ${addresses.length}` +
347
+ ` (${addresses.join(", ") || "none"}) — one address cannot disagree with itself,` +
348
+ " so there is nothing here that could have failed.",
349
+ shells: [],
350
+ };
351
+ }
352
+
353
+ const shells: AddressShell[] = [];
354
+ const problems: string[] = [];
355
+ for (const address of addresses) {
356
+ const url = new URL(path, address);
357
+ url.searchParams.set("cb", cacheBust());
358
+ // 🔴 Re-thrown WITH the address and with the original message intact: which
359
+ // of the addresses could not be reached is the whole diagnosis, and
360
+ // {@link Smoke.dnsHint} still has to recognise an ENOTFOUND inside it.
361
+ const response = await send(url.toString(), {
362
+ redirect: "manual",
363
+ signal: AbortSignal.timeout(timeoutMs),
364
+ headers: { accept: "text/html" },
365
+ }).catch((thrown: unknown) => {
366
+ const failed = thrown as Error;
367
+ throw new Error(`${address} could not be reached — ${failed.message}`);
368
+ });
369
+ if (response.status !== 200) {
370
+ problems.push(`${address} answered ${response.status} for ${path}`);
371
+ shells.push({ address, assets: [] });
372
+ continue;
373
+ }
374
+ const assets = builtAssetsIn(await response.text(), address);
375
+ if (assets.length === 0) problems.push(`${address} served a shell naming no built assets`);
376
+ shells.push({ address, assets });
377
+ }
378
+
379
+ const named = (shell: AddressShell) => `${shell.address} → ${shell.assets.join(", ") || "(none)"}`;
380
+ if (problems.length > 0) {
381
+ return { agreed: false, detail: `${problems.join("; ")} — ${shells.map(named).join(" | ")}`, shells };
382
+ }
383
+
384
+ const distinct = new Set(shells.map((shell) => shell.assets.join(",")));
385
+ if (distinct.size > 1) {
386
+ return {
387
+ agreed: false,
388
+ detail:
389
+ `the addresses of one deployment serve DIFFERENT built assets — ${shells.map(named).join(" | ")}.` +
390
+ " Something in front of this app is answering with bytes this checkout did not build.",
391
+ shells,
392
+ };
393
+ }
394
+
395
+ return { agreed: true, detail: `${shells.length} addresses agree — ${shells[0]?.assets.join(", ")}`, shells };
396
+ }
397
+
398
+ /** The check's name in the ledger. Exported so a caller can talk about its own result. */
399
+ export const SAME_SHELL_CHECK = "every address serves the same built client";
400
+
401
+ /**
402
+ * {@link compareDeployedShells}, recorded — and an EDGE fault when it disagrees.
403
+ *
404
+ * 🔴 **Code 2, deliberately: a disagreement is never a reason to roll back.** The
405
+ * origin in that comparison is serving what this checkout built; what is wrong sits
406
+ * in FRONT of it — a shadowing Worker, a cache rule, a stale Assets binding — and
407
+ * reverting the application code cannot reach any of them. It would discard the
408
+ * good deploy and leave the visitor on the same old bytes, which is the failure the
409
+ * exit-code contract at the top of this file exists to prevent.
410
+ *
411
+ * ```ts
412
+ * await sameDeployedShell(smoke, [smoke.base, `http://127.0.0.1:${port}`]);
413
+ * ```
414
+ */
415
+ export async function sameDeployedShell(smoke: Smoke, addresses: string[], options: ShellOptions = {}): Promise<boolean> {
416
+ let agreed = false;
417
+ await guarded(smoke, SAME_SHELL_CHECK, async () => {
418
+ const agreement = await compareDeployedShells(addresses, options);
419
+ agreed = smoke.record(SAME_SHELL_CHECK, agreement.agreed, agreement.detail);
420
+ if (!agreement.agreed) smoke.markEdgeFault();
421
+ });
422
+ return agreed;
423
+ }