diff --git a/AGENTS.md b/AGENTS.md index a9e0e01..535c305 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -10,6 +10,8 @@ The public page at https://live.setkernel.com: a Cloudflare Worker with static a Durable Object. sk-obs01 pushes filtered, delayed events to it; visitors watch over a WebSocket. It also holds `workers/meter`, sk-meter, the Cloudflare billing guard (an hourly cron, no route; `workers/meter/README.md`), moved here on 2026-11-09 (setkernel/hq#1449). +`workers/status` is sk-status's code (the `/status` read model on ctl.setkernel.net), moved the same +day; it has never been deployed and nothing here deploys it (`workers/status/README.md`). Public repo: everything here is visible to anyone. ## Commands @@ -24,8 +26,9 @@ Public repo: everything here is visible to anyone. # and repo names against GitHub's public list, live) # worker: npm run test:worker (vitest in workerd: the real Worker and Hub, see test/worker/) # meter: npm run test:meter (node --test: sk-meter's guard, usage queries, alert, baseline) +# status: npm run test:status (node --test: sk-status's board, views, Access check, routes) # lint: npm run check (node --check on every script) -# validate: npm run validate (check + test + test:worker + test:meter + a wrangler deploy --dry-run +# validate: npm run validate (check + test + test:worker + test:meter + test:status + a wrangler deploy --dry-run # of each Worker; what CI runs) ``` @@ -102,6 +105,9 @@ or nothing live to roll back to, gives the issue `owner` too. - `workers/meter/`: sk-meter, its own Worker (`wrangler.toml`, not `wrangler.jsonc`): `src/` the hourly run and the guard flags, `allowances.json`, `tools/` the baseline and the offline drill, `test/` its `node --test` suites. Its settings come from the Infisical -> Workers sync (`keep_vars = true`). +- `workers/status/`: sk-status (`wrangler.toml`): `src/` the routes, the `Board` Durable Object and the + views, `public/status/fonts/`, `test/` (its `setup.js` resolves `cloudflare:workers` to a stand-in). + Not deployed; its writer, the control Worker, is being retired. - `test/`: `node --test` suites for the pure modules and the page (`day.test.mjs` for Day). - `test/worker/`: vitest suites run in workerd by `@cloudflare/vitest-plugin` (`vitest.config.mjs` reads `wrangler.jsonc`). They fake Access's certs with a throwaway key and drive every route diff --git a/package.json b/package.json index cdcc6a4..b7a69da 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,9 @@ "test": "node --test test/*.test.mjs", "test:worker": "vitest run", "test:meter": "node --test workers/meter/test/*.test.js", - "check": "for f in src/*.js public/*.js public/lib/*.js public/usage/*.js scripts/*.mjs workers/meter/src/*.js workers/meter/tools/*.mjs; do node --check \"$f\" || exit 1; done", - "validate": "npm run check && npm test && npm run test:worker && npm run test:meter && wrangler deploy --dry-run --outdir .wrangler/dry-run && wrangler deploy --dry-run --config workers/meter/wrangler.toml --outdir .wrangler/dry-run-meter" + "test:status": "node --import ./workers/status/test/setup.js --test workers/status/test/*.test.js", + "check": "for f in src/*.js public/*.js public/lib/*.js public/usage/*.js scripts/*.mjs workers/meter/src/*.js workers/meter/tools/*.mjs workers/status/src/*.js; do node --check \"$f\" || exit 1; done", + "validate": "npm run check && npm test && npm run test:worker && npm run test:meter && npm run test:status && wrangler deploy --dry-run --outdir .wrangler/dry-run && wrangler deploy --dry-run --config workers/meter/wrangler.toml --outdir .wrangler/dry-run-meter && wrangler deploy --dry-run --config workers/status/wrangler.toml --outdir .wrangler/dry-run-status" }, "dependencies": { "@noble/secp256k1": "3.2.0" diff --git a/workers/status/README.md b/workers/status/README.md new file mode 100644 index 0000000..f53c85c --- /dev/null +++ b/workers/status/README.md @@ -0,0 +1,70 @@ +# sk-status: the read model of the org + +A page for Raghav, the internal wall and the showcase's publisher: sk-meter's guard flags and +the last 24 hours of merges and failures, on `ctl.setkernel.net/status` behind Access. + +It moved here from a private repository on 2026-11-09 (setkernel/hq#1449, plan v6 F3). Its +history up to then is that repository's `status/` at commit `c1e3253`. + +**It has never been deployed, and nothing deploys it from here.** Its only writer was the +control Worker, which publishes over a service binding (the `Feed` entrypoint) and is +being retired in hq#1449. Without it the board stays empty and the page shows only the guard. +Deploying it is Raghav's call ("The first deploy", below); until then this is code and tests +only, checked by `npm run validate`. + +## How it works +- `src/board.js` `Board`: one SQLite Durable Object (`BOARD`, name `org`). Each writer publishes + its own view here after it changes, and `/status` reads this one object. + - An item applies only with a higher `rev` than the board holds. Reaching `merged` or `failed` + adds one row to `events`; `closed` and `new` take an item off the board without one. + - Each publish prunes ended items and events older than 24 hours. No alarms, no WebSockets. +- `src/model.js`: checks a published batch against the state list (`STATES`) and builds the two + views. One bad entry is rejected with its index and reason; the rest of the batch applies. +- `src/index.js`: the routes, the Access check, the guard, and the `Feed` entrypoint. +- `src/access.js`: who may read (`STATUS_CLIENTS` scopes) and the Access token check. +- `src/html.js`: the phone page, no script. Manrope is self-hosted under `public/status/fonts/` + with its licence; the last fallback is `sans-serif`. +- It imports one module from outside this folder: `../../meter/src/flags.js` (the guard). + +## Routes +All on `ctl.setkernel.net`, behind an Access app for `ctl.setkernel.net/status` and +`ctl.setkernel.net/status/*` (not in setkernel-cloudflare tofu yet). The Worker routes are the +same two patterns. + +| Route | Who | What | +|---|---|---| +| `GET /status` | a person | HTML when the browser asks for it, JSON otherwise | +| `GET /status/internal.json` | a person, or an `internal` service token | `{ generated_at, snapshot_at, guard, last_24h }` | +| `GET /status/public.json` | anyone let in, including a `public` token | `{ generated_at, last_24h: { merged, failed } }`, built field by field | +| `/status/fonts/*` | (static assets) | the two font files and `OFL-Manrope.txt` | + +The Worker verifies `Cf-Access-Jwt-Assertion` itself (RS256, setkernel's team, audience +`STATUS_ACCESS_AUD`). A token with an `email` is a person and sees everything. A service token +gets in only if `STATUS_CLIENTS` lists its client id as `internal` or `public`; a `public` token +gets a 403 on anything but `/status/public.json`. With `STATUS_ACCESS_AUD` unset, every request +is a 401. + +## The guard +`/status` reads sk-meter's flags at most once a minute per isolate. Under `guard:degrade` +snapshots are reused longer (person 60 s, service token 600 s; normally 15 s and 120 s). Under +`guard:refuse`, `/status/public.json` answers 503 with `retry-after`; people still see the page. + +## What it costs +Estimated with two pollers at one a minute: about 176,000 Workers requests (1.8% of the +included 10M) and up to 190,000 Durable Object requests (19% of 1M at worst) a month +(https://developers.cloudflare.com/workers/platform/pricing/, +https://developers.cloudflare.com/durable-objects/platform/pricing/). + +## Commands +```sh +npm run test:status # node --test, with cloudflare:workers resolved to test/cloudflare-workers.js +``` + +## The first deploy (Raghav, only if the page is wanted) +1. setkernel-cloudflare: the Access app for the two paths (allow Raghav; two service tokens for + the wall and the publisher), `setkernel.net` in `deploy_tokens["setkernel-live"].zones`, and + `workers_admin` for one deploy (Workers Editor can't create a Worker). +2. Infisical `sk-ctl-workers/prod`, folder `/sk-status`: `STATUS_ACCESS_AUD` and `STATUS_CLIENTS` + from tofu outputs, and a Workers sync from that folder (never `/`) to the Worker `sk-status`. +3. A deploy job here, like `meter` in `.github/workflows/deploy.yml`. +4. Remove `workers_admin` again. diff --git a/workers/status/public/status/fonts/OFL-Manrope.txt b/workers/status/public/status/fonts/OFL-Manrope.txt new file mode 100644 index 0000000..462280f --- /dev/null +++ b/workers/status/public/status/fonts/OFL-Manrope.txt @@ -0,0 +1,93 @@ +Copyright 2019 The Manrope Project Authors (https://github.com/sharanda/manrope) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/workers/status/public/status/fonts/manrope-latin-400-normal.woff2 b/workers/status/public/status/fonts/manrope-latin-400-normal.woff2 new file mode 100644 index 0000000..7eaaa95 Binary files /dev/null and b/workers/status/public/status/fonts/manrope-latin-400-normal.woff2 differ diff --git a/workers/status/public/status/fonts/manrope-latin-600-normal.woff2 b/workers/status/public/status/fonts/manrope-latin-600-normal.woff2 new file mode 100644 index 0000000..7cc09a3 Binary files /dev/null and b/workers/status/public/status/fonts/manrope-latin-600-normal.woff2 differ diff --git a/workers/status/src/access.js b/workers/status/src/access.js new file mode 100644 index 0000000..77241b6 --- /dev/null +++ b/workers/status/src/access.js @@ -0,0 +1,85 @@ +// Who may read /status. Access admits only its policy's people and service tokens at the edge +// (setkernel-cloudflare, access stack); index.js checks the token again with verifyAccessJwt +// below, so a mistake in the Access config still can't open the page. This file verifies the +// token and maps the verified claims to a viewer and a scope. jwksFetcher and verifyAccessJwt +// began as a copy of the control Worker's access.js (setkernel-live's before it); status keeps its own. + +export const ISSUER = "https://setkernel.cloudflareaccess.com"; +export const CERTS = `${ISSUER}/cdn-cgi/access/certs`; + +function b64urlBytes(s) { + const b64 = s.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((s.length + 3) % 4); + return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); +} +const b64urlJson = (s) => JSON.parse(new TextDecoder().decode(b64urlBytes(s))); + +// Access's signing keys, cached for an hour (they rotate rarely). An unknown kid refetches, but +// not more than once every minRefetchMs, so junk tokens can't turn every request into a fetch. +export function jwksFetcher(url = CERTS, ttlMs = 3600_000, fetchFn = (u) => fetch(u), minRefetchMs = 60_000) { + let keys = null; + let at = 0; + return async (kid) => { + const age = Date.now() - at; + const unknown = kid && keys && !keys.some((k) => k.kid === kid); + if (!keys || age >= ttlMs || (unknown && age >= minRefetchMs)) { + const r = await fetchFn(url); + if (!r.ok) throw new Error(`certs: HTTP ${r.status}`); + keys = (await r.json()).keys ?? []; + at = Date.now(); + } + return keys; + }; +} + +// The claims of an unexpired RS256 token from setkernel's Access, for this app (aud), signed by +// one of Access's current keys; null for anything else. +export async function verifyAccessJwt(token, { aud, getKeys, issuer = ISSUER, now = Date.now() }) { + try { + const parts = (token || "").split("."); + if (parts.length !== 3 || !aud) return null; + const header = b64urlJson(parts[0]); + const claims = b64urlJson(parts[1]); + if (header.alg !== "RS256" || claims.iss !== issuer) return null; + const auds = Array.isArray(claims.aud) ? claims.aud : [claims.aud]; + if (!auds.includes(aud)) return null; + if (!Number.isFinite(claims.exp) || claims.exp * 1000 <= now) return null; + if (Number.isFinite(claims.nbf) && claims.nbf * 1000 > now + 60_000) return null; + const jwk = (await getKeys(header.kid)).find((k) => k.kid === header.kid); + if (!jwk) return null; + const key = await crypto.subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["verify"]); + const ok = await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key, b64urlBytes(parts[2]), new TextEncoder().encode(`${parts[0]}.${parts[1]}`)); + return ok ? claims : null; + } catch { + return null; + } +} + + +export const SCOPES = ["internal", "public"]; + +// STATUS_CLIENTS: { "": "internal" | "public" }. The wall's token gets +// internal; the showcase publisher's gets public, so it can only ever read the sanitized view. +// Answers a Map; an unset or malformed variable is an empty one (no service token is let in). +export function clientScopes(raw) { + try { + const parsed = typeof raw === "string" ? JSON.parse(raw) : null; + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return new Map(); + const entries = Object.entries(parsed); + if (entries.some(([id, scope]) => !id || !SCOPES.includes(scope))) return new Map(); + return new Map(entries); + } catch { + return new Map(); + } +} + +// Verified claims -> who is asking: { who: "person", email } for someone who signed in through +// Access, { who: "service", scope } for a listed service token, or null. A service token's +// claims carry common_name (its client id) and no email. +export function viewer(claims, scopes) { + if (!claims) return null; + if (typeof claims.common_name === "string" && claims.common_name) { + const scope = scopes.get(claims.common_name); + return scope ? { who: "service", scope } : null; + } + return typeof claims.email === "string" && claims.email ? { who: "person", email: claims.email } : null; +} diff --git a/workers/status/src/board.js b/workers/status/src/board.js new file mode 100644 index 0000000..33e506e --- /dev/null +++ b/workers/status/src/board.js @@ -0,0 +1,93 @@ +// Board: the one object that holds the read model. The control Worker's objects are one per +// item and can't be listed, so each publishes its view here after it changes (README.md, +// "Feeding it"); /status reads this one object instead of hundreds. Seats are no longer kept: +// a deployed object's old `seats` table stays, unread. +// +// SQLite storage, no alarms and no WebSockets, so it is idle between requests and bills no +// duration then. Old rows are pruned inside each publish, not by a timer. + +import { DurableObject } from "cloudflare:workers"; +import { DAY_MS, ENDED, LOGGED, checkBatch } from "./model.js"; + +export const BOARD_NAME = "org"; + +const SCHEMA = [ + `CREATE TABLE IF NOT EXISTS items ( + ref TEXT PRIMARY KEY, repo TEXT NOT NULL, number INTEGER NOT NULL, kind TEXT NOT NULL, + state TEXT NOT NULL, rev INTEGER NOT NULL, seat TEXT, pr INTEGER, session_url TEXT, + updated_at TEXT NOT NULL)`, + // one row each time an item reaches merged or failed: the "last 24 h" list + `CREATE TABLE IF NOT EXISTS events ( + seq INTEGER PRIMARY KEY AUTOINCREMENT, at TEXT NOT NULL, ref TEXT NOT NULL, repo TEXT NOT NULL, + number INTEGER NOT NULL, kind TEXT NOT NULL, state TEXT NOT NULL, seat TEXT, pr INTEGER)`, + "CREATE INDEX IF NOT EXISTS events_at ON events (at)", +]; + +export class Board extends DurableObject { + constructor(ctx, env) { + super(ctx, env); + this.sql = ctx.storage.sql; + for (const s of SCHEMA) this.sql.exec(s); + this.now = () => new Date(); + } + + // { items } (model.js checkBatch) -> { ok, applied, stale, rejected }. An item applies only + // with a higher rev than the board holds, so a late or repeated publish changes nothing. Two + // exceptions, both writes the control Worker makes without raising rev: a fired dispatch after the move to + // dispatched, so at the same rev a session link the board doesn't hold yet still applies; and + // another PR taking over (WorkItem.github()), so at the same rev a newer updated_at applies + // too. Neither takes a held link away. + async publish(batch) { + const checked = checkBatch(batch); + if (checked.error) return { ok: false, status: 400, error: checked.error }; + const now = this.now(); + const cutoff = new Date(now.getTime() - DAY_MS).toISOString(); + let applied = 0; + let stale = 0; + this.ctx.storage.transactionSync(() => { + for (const r of checked.items) { + const held = this.sql.exec("SELECT rev, state, session_url, updated_at FROM items WHERE ref = ?", r.ref).toArray()[0]; + const same = held && held.rev === r.rev; + const link = same && held.session_url == null && r.session_url != null; + const newer = same && r.updated_at > held.updated_at; + if (held && held.rev >= r.rev && !link && !newer) { + stale++; + continue; + } + const session = same ? r.session_url ?? held.session_url : r.session_url; + this.sql.exec( + `INSERT INTO items (ref, repo, number, kind, state, rev, seat, pr, session_url, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT (ref) DO UPDATE SET kind = excluded.kind, state = excluded.state, rev = excluded.rev, + seat = excluded.seat, pr = excluded.pr, session_url = excluded.session_url, updated_at = excluded.updated_at`, + r.ref, r.repo, r.number, r.kind, r.state, r.rev, r.seat, r.pr, session, r.updated_at, + ); + if (LOGGED.includes(r.state) && held?.state !== r.state) { + this.sql.exec( + "INSERT INTO events (at, ref, repo, number, kind, state, seat, pr) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + r.updated_at, r.ref, r.repo, r.number, r.kind, r.state, r.seat, r.pr, + ); + } + applied++; + } + // the page shows a day of merges and failures; ended items and events older than that go + this.sql.exec("DELETE FROM events WHERE at < ?", cutoff); + this.sql.exec(`DELETE FROM items WHERE state IN (${ENDED.map(() => "?").join(", ")}) AND updated_at < ?`, ...ENDED, cutoff); + }); + return { ok: true, applied, stale, rejected: checked.rejected }; + } + + // Everything the board holds, in one call: the open items and a day of events, and + // when they were read (the Worker reuses a snapshot for up to FRESH seconds). + async snapshot() { + const now = this.now(); + const cutoff = new Date(now.getTime() - DAY_MS).toISOString(); + const items = this.sql.exec( + `SELECT ref, repo, number, kind, state, rev, seat, pr, session_url, updated_at FROM items + WHERE state NOT IN (${ENDED.map(() => "?").join(", ")}) ORDER BY ref`, + ...ENDED, + ).toArray(); + const events = this.sql.exec("SELECT at, ref, repo, number, kind, state, seat, pr FROM events WHERE at >= ? ORDER BY seq", cutoff).toArray(); + return { ok: true, at: now.toISOString(), items, events }; + } +} diff --git a/workers/status/src/html.js b/workers/status/src/html.js new file mode 100644 index 0000000..1c7fbbf --- /dev/null +++ b/workers/status/src/html.js @@ -0,0 +1,50 @@ +// The phone view of the internal read model: one small page, no script, nothing loaded from +// anywhere but this hostname. Manrope (Google Fonts, OFL) is self-hosted as a static asset at +// /status/fonts/ (public/status/fonts, with its licence); sans-serif is only the last fallback. + +const ESC = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }; +export const esc = (v) => String(v ?? "").replace(/[&<>"']/g, (c) => ESC[c]); + +// "4 min", "3 h", "2 d" since (or until) iso, from now +export function ago(iso, now) { + const s = Math.abs(now.getTime() - Date.parse(iso)) / 1000; + if (!Number.isFinite(s)) return "?"; + if (s < 90) return `${Math.round(s)} s`; + if (s < 5400) return `${Math.round(s / 60)} min`; + if (s < 172800) return `${Math.round(s / 3600)} h`; + return `${Math.round(s / 86400)} d`; +} + +const link = (href, text) => (href ? `${esc(text)}` : esc(text)); + +const STYLE = ` +@font-face{font-family:Manrope;src:url(/status/fonts/manrope-latin-400-normal.woff2) format("woff2");font-weight:400;font-display:swap} +@font-face{font-family:Manrope;src:url(/status/fonts/manrope-latin-600-normal.woff2) format("woff2");font-weight:600;font-display:swap} +:root{color-scheme:light dark;--fg:#1b1f24;--muted:#5b6470;--bg:#fbfbfa;--line:#d9dde3;--warn:#8a4b00;--bad:#a1101a;--ok:#1d6b3a} +@media (prefers-color-scheme:dark){:root{--fg:#e8eaed;--muted:#a3abb5;--bg:#14171a;--line:#2e343b;--warn:#f0b35a;--bad:#ff8a8f;--ok:#7fd49a}} +*{box-sizing:border-box}body{margin:0 auto;max-width:40rem;padding:1rem;font:400 1rem/1.45 Manrope,sans-serif;color:var(--fg);background:var(--bg)} +h1{font-size:1.25rem;margin:0 0 .25rem}h2{font-size:1rem;font-weight:600;margin:1.5rem 0 .5rem;border-bottom:1px solid var(--line);padding-bottom:.25rem} +a{color:inherit}ul{list-style:none;margin:0;padding:0}li{padding:.35rem 0;border-bottom:1px solid var(--line)} +.m{color:var(--muted);font-size:.875rem}.g{padding:.5rem .75rem;border:1px solid currentColor;border-radius:.25rem;margin:.75rem 0} +.alert{color:var(--warn)}.degrade,.refuse,.bad{color:var(--bad)}.ok{color:var(--ok)}.n{font-weight:600}`; + +function guardBlock(g, now) { + if (g.level === "ok") return `

Guard: ok

`; + const what = g.unreadable ? "flags unreadable, treated as degrade" : g.metrics.join(", ") || "no metric named"; + const until = g.until ? `, until ${esc(g.until.slice(0, 16).replace("T", " "))} UTC (${ago(g.until, now)})` : ""; + return `

Guard: ${esc(g.level)} (${esc(what)}${until})

`; +} + +// internal: model.js internalView() +export function renderHtml(v) { + const now = new Date(v.generated_at); + const events = v.last_24h.events.length + ? v.last_24h.events.map((e) => `
  • ${esc(e.state)} ${link(e.pr_url ?? e.url, e.ref)} ${ago(e.at, now)} ago${e.seat ? ` · ${esc(e.seat)}` : ""}
  • `).join("") + : `
  • None.
  • `; + return `` + + `setkernel status` + + `

    setkernel status

    ${esc(v.generated_at.slice(0, 19).replace("T", " "))} UTC · JSON

    ` + + guardBlock(v.guard, now) + + `

    Last 24 h: ${v.last_24h.merged} merged, ${v.last_24h.failed} failed

    ` + + ``; +} diff --git a/workers/status/src/index.js b/workers/status/src/index.js new file mode 100644 index 0000000..8f4bc0a --- /dev/null +++ b/workers/status/src/index.js @@ -0,0 +1,111 @@ +// sk-status: the read model of the org (hq#208), for Raghav, the wall and Polaris. +// +// GET /status the internal view: HTML for a browser, JSON otherwise +// GET /status/internal.json the internal view as JSON (the wall, hq#127; Polaris) +// GET /status/public.json the sanitized view (the showcase's publisher, hq#213) +// /status/fonts/* static assets: served by Cloudflare before this code runs +// +// The routes are ctl.setkernel.net/status and /status/*, behind their own Access app (README.md, +// "Routes"), and every request's Access token is checked again here. A person sees everything; +// a service token sees what STATUS_CLIENTS gives it. The control Worker writes through the Feed entrypoint, over a service +// binding, so there is no write route at all. + +import { WorkerEntrypoint } from "cloudflare:workers"; +import { readGuard, refusal } from "../../meter/src/flags.js"; +import { clientScopes, jwksFetcher, verifyAccessJwt, viewer } from "./access.js"; +import { BOARD_NAME } from "./board.js"; +import { renderHtml } from "./html.js"; +import { internalView, publicView } from "./model.js"; + +export { Board } from "./board.js"; + +// How long one isolate reuses a board snapshot, in seconds. The Cache API isn't available +// behind Access, so this is per isolate. Pollers get the longer wait (twice their one-a-minute +// interval, so at most every other poll reads the object); under guard:degrade every wait goes up. +export const FRESH = { person: 15, service: 120 }; +export const DEGRADED_FRESH = { person: 60, service: 600 }; +export const GUARD_SECONDS = 60; + +const fail = (status, error) => Response.json({ ok: false, error }, { status, headers: { "cache-control": "no-store" } }); + +const board = (env) => env.BOARD.get(env.BOARD.idFromName(BOARD_NAME)); + +const HTML_HEADERS = { + "content-type": "text/html; charset=utf-8", + "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'; font-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'", + "x-content-type-options": "nosniff", + "referrer-policy": "no-referrer", +}; + +// One handler with its own caches; tests make their own with a fake clock and fake keys. +export function createHandler({ getKeys = jwksFetcher(), now = () => new Date() } = {}) { + let guardMemo = null; + let snapMemo = null; + + async function guardOf(env) { + const t = now().getTime(); + if (!guardMemo || t - guardMemo.at >= GUARD_SECONDS * 1000) guardMemo = { at: t, value: await readGuard(env.GUARD) }; + return guardMemo.value; + } + + async function snapshotOf(env, maxAgeS) { + const t = now().getTime(); + if (!snapMemo || t - snapMemo.at >= maxAgeS * 1000) { + const snap = await board(env).snapshot(); + if (!snap?.ok) throw new Error("board snapshot failed"); + snapMemo = { at: t, value: snap }; + } + return snapMemo.value; + } + + return async function handle(request, env) { + const { pathname } = new URL(request.url); + const views = { "/status": "page", "/status/internal.json": "internal", "/status/public.json": "public" }; + const view = views[pathname]; + if (!view) return fail(404, "no such route"); + if (request.method !== "GET" && request.method !== "HEAD") return fail(405, "GET only"); + + // fails closed: with no audience configured, nothing is served + const claims = await verifyAccessJwt(request.headers.get("cf-access-jwt-assertion"), { aud: env.STATUS_ACCESS_AUD, getKeys, now: now().getTime() }); + const who = viewer(claims, clientScopes(env.STATUS_CLIENTS)); + if (!who) return fail(401, "unauthorized"); + if (who.who === "service" && who.scope === "public" && view !== "public") return fail(403, "this token reads /status/public.json only"); + + const guard = await guardOf(env); + // the public feed is the one that exists for polling from outside: it stops first + if (view === "public") { + const paused = refusal(guard); + if (paused) return paused; + } + const wait = (guard.degrade ? DEGRADED_FRESH : FRESH)[who.who]; + const internal = internalView(await snapshotOf(env, wait), guard, now()); + // /status answers HTML or JSON by Accept, so a cache must key on it + const headers = { "cache-control": `private, max-age=${wait}`, vary: "accept" }; + if (view === "public") return Response.json(publicView(internal), { headers }); + const wantsHtml = view === "page" && who.who === "person" && /text\/html/.test(request.headers.get("accept") ?? ""); + if (wantsHtml) return new Response(renderHtml(internal), { headers: { ...headers, ...HTML_HEADERS } }); + return Response.json(internal, { headers }); + }; +} + + +// The control Worker's service binding: [[services]] binding = "STATUS", service = "sk-status", entrypoint = "Feed". +// publish({ items: [WorkItem.get() results] }) -> { ok, applied, stale, rejected }. Items feed only +// the "last 24 h" list now; a batch's `seats` is ignored. +export class Feed extends WorkerEntrypoint { + async publish(batch) { + return board(this.env).publish(batch); + } +} + +// A 500 that logs the error and tells the caller nothing more. +export const guarded = (handle) => async (request, env) => { + try { + return await handle(request, env); + } catch (e) { + console.error(`sk-status: ${request.method} ${new URL(request.url).pathname}: ${e?.stack ?? e}`); + return fail(500, "internal error"); + } +}; + +export default { fetch: guarded(createHandler()) }; diff --git a/workers/status/src/model.js b/workers/status/src/model.js new file mode 100644 index 0000000..de1e247 --- /dev/null +++ b/workers/status/src/model.js @@ -0,0 +1,129 @@ +// The read model (hq#208): what the control Worker publishes to the board, checked, and the views +// built from the board's rows. Pure functions; board.js stores, index.js serves. +// +// The control Worker publishes what its objects already answer (the control Worker's data model): +// items: WorkItem.get() results, { item: { ref, kind, state, rev, seat, pr, updated_at }, dispatch } +// (dispatch is the control Worker's last routine fire, with session_url; it may be absent) +// Anything else is dropped with a reason, so one bad entry never blocks the rest of a batch. +// The queue, running work and seats are no longer shown (hq#1330): a batch's `seats` +// is ignored, and items only feed the "last 24 h" list. + +// The control Worker's work item states, so an item is never rejected for a state the control Worker can reach. Kept in +// step by hand with STATES in the control Worker's model.js; status has no import from the control Worker. +export const STATES = ["new", "queued", "dispatched", "running", "pr_open", "reviewing", "merged", "failed", "closed"]; +// off the board: finished, closed without a merge, or back to new (out of the agent flow, +// the control Worker). These rows are pruned after a day; they would otherwise stay forever. +export const ENDED = ["merged", "failed", "closed", "new"]; +// the ends the "last 24 h" list counts: one event each time an item reaches one +export const LOGGED = ["merged", "failed"]; +export const DAY_MS = 24 * 3600_000; +export const BATCH_MAX = { items: 100 }; +export const EVENTS_SHOWN = 50; + +const OWNER = /^[a-z0-9](?:[a-z0-9-]{0,38})$/; +const REPO = /^[a-z0-9._-]{1,100}$/; +const LOGIN = /^[a-z0-9](?:[a-z0-9-]{0,38})$/; +const REF = /^([^/#]+)\/([^/#]+)#([1-9][0-9]{0,9})$/; + +const iso = (v) => { + if (typeof v !== "string" && typeof v !== "number") return null; + const t = new Date(v).getTime(); + return Number.isFinite(t) ? new Date(t).toISOString() : null; +}; +const login = (v) => (typeof v === "string" && LOGIN.test(v.toLowerCase()) ? v.toLowerCase() : null); +const posInt = (v) => (Number.isInteger(v) && v > 0 && v < 1e10 ? v : null); + +// "owner/repo#N" -> { repo, number }, or null. Lower-cased, as the control Worker keys its objects. +export function parseRef(ref) { + const m = typeof ref === "string" ? REF.exec(ref.toLowerCase()) : null; + if (!m || !OWNER.test(m[1]) || !REPO.test(m[2]) || m[2] === "." || m[2] === "..") return null; + return { ref: `${m[1]}/${m[2]}#${m[3]}`, repo: `${m[1]}/${m[2]}`, number: Number(m[3]) }; +} + +// Only a claude.ai session link is ever shown or linked (the control Worker keeps the same rule). +export const sessionUrl = (v) => (typeof v === "string" && v.length <= 200 && /^https:\/\/claude\.ai\/[A-Za-z0-9/_?=&.-]*$/.test(v) ? v : null); + +// One WorkItem.get() result -> a board row, or { error }. +export function itemRow(entry) { + const item = entry?.item ?? null; + if (!item || typeof item !== "object") return { error: "no item" }; + const name = parseRef(item.ref); + if (!name) return { error: "ref must be owner/repo#N" }; + if (!STATES.includes(item.state)) return { error: `state must be one of ${STATES.join(", ")}` }; + const rev = posInt(item.rev); + if (!rev) return { error: "rev must be a positive integer" }; + const updated = iso(item.updated_at); + if (!updated) return { error: "updated_at must be a time" }; + const seat = item.seat == null ? null : login(item.seat); + if (item.seat != null && !seat) return { error: "seat must be a GitHub login" }; + // the session link belongs to the dispatch that put the item on its current seat + const d = entry.dispatch; + const session = d && d.outcome === "fired" && (d.seat == null || login(d.seat) === seat) ? sessionUrl(d.session_url) : null; + return { + row: { + ref: name.ref, repo: name.repo, number: name.number, + kind: item.kind === "pr" ? "pr" : "issue", + state: item.state, rev, seat, pr: posInt(item.pr), session_url: session, updated_at: updated, + }, + }; +} + +// { items } -> { items: rows, rejected: [{ kind, index, error }] } +export function checkBatch(batch) { + if (!batch || typeof batch !== "object" || Array.isArray(batch)) return { error: "batch must be { items }" }; + const items = batch.items ?? []; + if (!Array.isArray(items)) return { error: "items must be an array" }; + if (items.length > BATCH_MAX.items) return { error: `at most ${BATCH_MAX.items} items a batch` }; + const out = { items: [], rejected: [] }; + items.forEach((e, index) => { + const r = itemRow(e); + if (r.error) out.rejected.push({ kind: "item", index, error: r.error }); + else out.items.push(r.row); + }); + return out; +} + +const github = (repo, kind, n) => `https://github.com/${repo}/${kind === "pr" ? "pull" : "issues"}/${n}`; +const byTime = (a, b) => (a < b ? -1 : a > b ? 1 : 0); + +// The guard as the page shows it: the levels, and the metrics and reset time of the highest one. +export function guardView(guard) { + const level = guard.refuse ? "refuse" : guard.degrade ? "degrade" : guard.alert ? "alert" : "ok"; + const d = guard.detail?.[level]; + return { + level, alert: guard.alert, degrade: guard.degrade, refuse: guard.refuse, + metrics: Array.isArray(d?.metrics) ? d.metrics.filter((m) => typeof m === "string") : [], + until: iso(d?.until), + ...(guard.error ? { unreadable: true } : {}), + }; +} + +// The internal view: for Raghav, the wall (hq#127) and Polaris. snapshot is Board.snapshot(). +export function internalView(snapshot, guard, now) { + const since = now.getTime() - DAY_MS; + const events = snapshot.events.filter((e) => Date.parse(e.at) > since).sort((a, b) => byTime(b.at, a.at)); + return { + generated_at: now.toISOString(), + // the board's data as of this time: a reused snapshot can be up to FRESH seconds older + snapshot_at: iso(snapshot.at), + guard: guardView(guard), + last_24h: { + merged: events.filter((e) => e.state === "merged").length, + failed: events.filter((e) => e.state === "failed").length, + events: events.slice(0, EVENTS_SHOWN).map((e) => ({ + at: e.at, state: e.state, ref: e.ref, seat: e.seat, pr: e.pr, + url: github(e.repo, e.kind, e.number), pr_url: e.pr ? github(e.repo, "pr", e.pr) : null, + })), + }, + }; +} + +// The sanitized view for the public showcase's publisher (hq#213). Built field by field from the +// internal view, never by deleting from it: counts only. No repo, ref, URL, login, session or guard +// detail can reach it, because none is copied. +export function publicView(internal) { + return { + generated_at: internal.generated_at, + last_24h: { merged: internal.last_24h.merged, failed: internal.last_24h.failed }, + }; +} diff --git a/workers/status/test/board.test.js b/workers/status/test/board.test.js new file mode 100644 index 0000000..834f6dc --- /dev/null +++ b/workers/status/test/board.test.js @@ -0,0 +1,122 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { Board } from "../src/board.js"; +import { fakeState } from "./fakes.js"; + +const T0 = Date.parse("2026-10-16T12:00:00Z"); + +function board(state = fakeState(), at = T0) { + const b = new Board(state, {}); + b.clock = at; + b.now = () => new Date(b.clock); + return b; +} + +const item = (ref, state, rev, extra = {}) => ({ item: { ref, kind: "issue", state, rev, seat: null, pr: null, updated_at: new Date(T0).toISOString(), ...extra } }); +const seat = (login, seen, utilization = 0.5) => ({ seat: { login, active: true, updated_at: seen, windows: [{ name: "five_hour", utilization, resets_at: "2026-10-16T15:00:00Z", observed_at: seen }] } }); + +test("publish then snapshot: open items, and seats are neither written nor read", async () => { + const b = board(); + const r = await b.publish({ items: [item("a/b#1", "queued", 1), item("a/b#2", "running", 3, { seat: "vega-setkernel" })], seats: [seat("vega-setkernel", "2026-10-16T11:00:00Z")] }); + assert.deepEqual(r, { ok: true, applied: 2, stale: 0, rejected: [] }); + const s = await b.snapshot(); + assert.deepEqual(s.items.map((i) => [i.ref, i.state, i.seat]), [["a/b#1", "queued", null], ["a/b#2", "running", "vega-setkernel"]]); + assert.equal(s.seats, undefined); + assert.deepEqual(b.sql.exec("SELECT name FROM sqlite_master WHERE name = 'seats'").toArray(), []); + assert.deepEqual(s.events, []); +}); + +test("the rev fence: an older or repeated item view changes nothing", async () => { + const b = board(); + await b.publish({ items: [item("a/b#1", "running", 3)] }); + const r = await b.publish({ items: [item("a/b#1", "queued", 1), item("a/b#1", "running", 3)] }); + assert.equal(r.applied, 0); + assert.equal(r.stale, 2); + assert.equal((await b.snapshot()).items[0].state, "running"); +}); + +test("the session link written after the move to dispatched applies at the same rev, and stays", async () => { + const b = board(); + const moved = item("a/b#1", "dispatched", 2, { seat: "vega-setkernel" }); + const fired = { ...moved, dispatch: { at: new Date(T0).toISOString(), seat: "vega-setkernel", rev: 2, outcome: "fired", session_url: "https://claude.ai/code/session_01abc" } }; + assert.equal((await b.publish({ items: [moved] })).applied, 1); + assert.deepEqual(await b.publish({ items: [fired] }), { ok: true, applied: 1, stale: 0, rejected: [] }); + assert.equal((await b.snapshot()).items[0].session_url, "https://claude.ai/code/session_01abc"); + const r = await b.publish({ items: [moved, fired] }); // late or repeated: no change either way + assert.equal(r.stale, 2); + assert.equal((await b.snapshot()).items[0].session_url, "https://claude.ai/code/session_01abc"); + assert.deepEqual((await b.snapshot()).events, []); +}); + +test("another PR taking over at the same rev applies when newer, and a late replay doesn't undo it", async () => { + const b = board(); + const at = (m) => new Date(T0 + m * 60_000).toISOString(); + const fired = { ...item("a/b#1", "pr_open", 4, { seat: "vega-setkernel", pr: 10, updated_at: at(0) }), dispatch: { at: at(0), seat: "vega-setkernel", rev: 4, outcome: "fired", session_url: "https://claude.ai/code/session_01abc" } }; + const first = item("a/b#1", "pr_open", 4, { seat: "vega-setkernel", pr: 10, updated_at: at(0) }); + const took = item("a/b#1", "pr_open", 4, { seat: "vega-setkernel", pr: 11, updated_at: at(30) }); + assert.equal((await b.publish({ items: [fired] })).applied, 1); + assert.deepEqual(await b.publish({ items: [took] }), { ok: true, applied: 1, stale: 0, rejected: [] }); + let s = await b.snapshot(); + assert.deepEqual(s.items.map((i) => [i.ref, i.rev, i.pr, i.session_url]), [["a/b#1", 4, 11, "https://claude.ai/code/session_01abc"]]); + const r = await b.publish({ items: [first, fired, took] }); // late or repeated: no change + assert.equal(r.stale, 3); + s = await b.snapshot(); + assert.deepEqual(s.items.map((i) => [i.pr, i.updated_at]), [[11, at(30)]]); + assert.deepEqual(s.events, []); +}); + +test("reaching merged or failed records one event; a retry records the next failure too", async () => { + const b = board(); + await b.publish({ items: [item("a/b#1", "pr_open", 4, { pr: 9, seat: "vega-setkernel" })] }); + await b.publish({ items: [item("a/b#1", "merged", 5, { pr: 9, seat: "vega-setkernel" })] }); + await b.publish({ items: [item("a/b#1", "merged", 5, { pr: 9 })] }); // the same view again + await b.publish({ items: [item("c/d#2", "failed", 2), item("c/d#2", "queued", 3), item("c/d#2", "failed", 4)] }); + const s = await b.snapshot(); + assert.deepEqual(s.events.map((e) => [e.ref, e.state, e.pr]), [["a/b#1", "merged", 9], ["c/d#2", "failed", null], ["c/d#2", "failed", null]]); + assert.deepEqual(s.items, [], "ended items aren't open"); +}); + +test("a day later, ended items and their events are pruned on the next publish", async () => { + const b = board(); + await b.publish({ items: [item("a/b#1", "merged", 2), item("a/b#2", "queued", 1)] }); + b.clock = T0 + 25 * 3600_000; + assert.equal((await b.snapshot()).events.length, 0, "the snapshot hides events past a day"); + await b.publish({ items: [item("a/b#3", "queued", 1, { updated_at: new Date(b.clock).toISOString() })] }); + const rows = b.sql.exec("SELECT ref FROM items ORDER BY ref").toArray().map((r) => r.ref); + assert.deepEqual(rows, ["a/b#2", "a/b#3"], "an open item stays however old"); + assert.equal(b.sql.exec("SELECT count(*) AS n FROM events").toArray()[0].n, 0); +}); + +test("closed and new leave the board without an event, and are pruned a day later", async () => { + const b = board(); + await b.publish({ items: [item("a/b#1", "queued", 1), item("a/b#2", "running", 2, { seat: "vega-setkernel" })] }); + await b.publish({ items: [item("a/b#1", "closed", 2), item("a/b#2", "new", 3)] }); + const s = await b.snapshot(); + assert.deepEqual(s.items, [], "neither is queued or running any more"); + assert.deepEqual(s.events, [], "only merged and failed are counted"); + b.clock = T0 + 25 * 3600_000; + await b.publish({ items: [] }); + assert.equal(b.sql.exec("SELECT count(*) AS n FROM items").toArray()[0].n, 0); +}); + +test("a bad entry is rejected with its reason and the rest of the batch applies", async () => { + const b = board(); + const r = await b.publish({ items: [item("bad ref", "queued", 1), item("a/b#1", "queued", 1)] }); + assert.equal(r.applied, 1); + assert.deepEqual(r.rejected, [{ kind: "item", index: 0, error: "ref must be owner/repo#N" }]); + assert.deepEqual(await b.publish("nope"), { ok: false, status: 400, error: "batch must be { items }" }); +}); + +test("a failed write rolls the whole batch back", async () => { + const state = fakeState(); + const b = board(state); + state.db.exec("CREATE TRIGGER boom BEFORE INSERT ON events BEGIN SELECT RAISE(ABORT, 'disk full'); END"); + await assert.rejects(b.publish({ items: [item("a/b#1", "queued", 1), item("a/b#2", "merged", 2)] }), /disk full/); + assert.deepEqual((await b.snapshot()).items, []); +}); + +test("state survives the object being rebuilt on the same storage", async () => { + const state = fakeState(); + await board(state).publish({ items: [item("a/b#1", "queued", 1)] }); + assert.equal((await board(state).snapshot()).items.length, 1); +}); diff --git a/workers/status/test/cloudflare-workers.js b/workers/status/test/cloudflare-workers.js new file mode 100644 index 0000000..bbfe1ba --- /dev/null +++ b/workers/status/test/cloudflare-workers.js @@ -0,0 +1,14 @@ +// The part of `cloudflare:workers` sk-status uses: both base classes keep ctx and env. +export class DurableObject { + constructor(ctx, env) { + this.ctx = ctx; + this.env = env; + } +} + +export class WorkerEntrypoint { + constructor(ctx, env) { + this.ctx = ctx; + this.env = env; + } +} diff --git a/workers/status/test/fakes.js b/workers/status/test/fakes.js new file mode 100644 index 0000000..722a9b8 --- /dev/null +++ b/workers/status/test/fakes.js @@ -0,0 +1,81 @@ +// Test doubles: a namespace that hands out one object per name as the runtime does, and Access +// tokens signed by a key the test owns. fakeState is Durable Object storage on a real SQLite; +// memoryKV is sk-meter's. +import { DatabaseSync } from "node:sqlite"; + +export { memoryKV } from "../../meter/test/fakes.js"; + +// ctx.storage.sql.exec(query, ...bindings) returns a cursor; transactionSync rolls back on throw. +export function fakeState() { + const db = new DatabaseSync(":memory:"); + const sql = { + exec(query, ...bindings) { + const st = db.prepare(query); + const rows = st.columns().length ? st.all(...bindings) : (st.run(...bindings), []); + return { toArray: () => rows, one: () => rows[0] }; + }, + }; + // One alarm, as the runtime keeps it: setAlarm replaces it, and storage.alarm is the time it is + // set for (epoch ms) or null. A test runs it with object.alarm(). + const storage = { + sql, + alarm: null, + async setAlarm(at) { storage.alarm = typeof at === "number" ? at : at.getTime(); }, + async getAlarm() { return storage.alarm; }, + async deleteAlarm() { storage.alarm = null; }, + transactionSync(fn) { + db.exec("BEGIN"); + try { + const r = fn(); + db.exec("COMMIT"); + return r; + } catch (e) { + db.exec("ROLLBACK"); + throw e; + } + }, + }; + // ctx.waitUntil: the promises it was given, for a test to await (waits) + const waits = []; + return { storage, db, waits, waitUntil: (p) => { waits.push(p); } }; +} + +// init(object) runs once on each new object, e.g. to point a Board's clock at the test's. +export function fakeNamespace(Class, env = {}, init = () => {}) { + const objects = new Map(); + const ns = { + objects, + gets: 0, + idFromName: (name) => ({ name, toString: () => name }), + get(id) { + ns.gets++; + if (!objects.has(id.name)) { + const o = new Class(fakeState(), env); + init(o); + objects.set(id.name, o); + } + return objects.get(id.name); + }, + }; + return ns; +} + +const b64url = (bytes) => Buffer.from(bytes).toString("base64url"); + +// An RS256 signer standing in for setkernel's Access: sign(claims) -> a token, getKeys -> its JWKS. +export async function fakeAccess({ kid = "k1" } = {}) { + const pair = await crypto.subtle.generateKey( + { name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" }, + true, ["sign", "verify"], + ); + const jwk = { ...(await crypto.subtle.exportKey("jwk", pair.publicKey)), kid }; + return { + getKeys: async () => [jwk], + async sign(claims, header = { alg: "RS256", kid }) { + const enc = (o) => b64url(new TextEncoder().encode(JSON.stringify(o))); + const body = `${enc(header)}.${enc(claims)}`; + const sig = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", pair.privateKey, new TextEncoder().encode(body)); + return `${body}.${b64url(new Uint8Array(sig))}`; + }, + }; +} diff --git a/workers/status/test/model.test.js b/workers/status/test/model.test.js new file mode 100644 index 0000000..2c9c340 --- /dev/null +++ b/workers/status/test/model.test.js @@ -0,0 +1,91 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { ENDED, LOGGED, STATES, checkBatch, internalView, itemRow, parseRef, publicView, sessionUrl } from "../src/model.js"; +import { esc } from "../src/html.js"; + +const NOW = new Date("2026-10-16T12:00:00Z"); +const OK = { alert: false, degrade: false, refuse: false, detail: {} }; +const item = (ref, state, rev = 1, extra = {}, dispatch) => ({ item: { ref, kind: "issue", state, rev, seat: null, pr: null, updated_at: "2026-10-16T11:00:00Z", ...extra }, dispatch }); + +test("parseRef lower-cases and rejects what isn't owner/repo#N", () => { + assert.deepEqual(parseRef("SetKernel/SK-demo#14"), { ref: "setkernel/sk-demo#14", repo: "setkernel/sk-demo", number: 14 }); + for (const bad of ["setkernel/sk-demo", "setkernel#1", "a/b/c#1", "a/b#0", "a/..#1", "-a/b#1", "a/b c#1", 7, null]) assert.equal(parseRef(bad), null, String(bad)); +}); + +test("itemRow keeps a fired dispatch's claude.ai link only for the item's own seat", () => { + const fired = { outcome: "fired", seat: "vega-setkernel", session_url: "https://claude.ai/code/session_01ab" }; + assert.equal(itemRow(item("a/b#1", "running", 3, { seat: "Vega-Setkernel" }, fired)).row.session_url, fired.session_url); + assert.equal(itemRow(item("a/b#1", "running", 3, { seat: "lyra-setkernel" }, fired)).row.session_url, null); + assert.equal(itemRow(item("a/b#1", "running", 3, { seat: "vega-setkernel" }, { ...fired, outcome: "failed" })).row.session_url, null); + for (const url of ["javascript:alert(1)", "https://claude.ai.evil.example/x", "http://claude.ai/x", "https://claude.ai/\">"]) { + assert.equal(sessionUrl(url), null, url); + } +}); + +test("itemRow rejects bad fields with a reason", () => { + assert.match(itemRow(item("nope", "queued")).error, /ref/); + assert.match(itemRow(item("a/b#1", "lost")).error, /state/); + assert.match(itemRow(item("a/b#1", "queued", 0)).error, /rev/); + assert.match(itemRow(item("a/b#1", "queued", 1, { seat: "not a login" })).error, /seat/); + assert.match(itemRow(item("a/b#1", "queued", 1, { updated_at: "soon" })).error, /updated_at/); + assert.match(itemRow({}).error, /no item/); +}); + +test("the ends the board logs and prunes are states the control Worker can reach", () => { + for (const state of ENDED) assert.ok(STATES.includes(state), state); + for (const state of LOGGED) assert.ok(ENDED.includes(state), state); +}); + +test("checkBatch drops bad entries with their index, keeps the rest and ignores seats", () => { + const out = checkBatch({ items: [item("a/b#1", "queued"), item("bad", "queued")], seats: [{ seat: { login: "x", active: true, updated_at: NOW.toISOString() } }] }); + assert.equal(out.items.length, 1); + assert.equal(out.seats, undefined); + assert.deepEqual(out.rejected, [{ kind: "item", index: 1, error: "ref must be owner/repo#N" }]); + assert.match(checkBatch([]).error, /batch/); + assert.match(checkBatch({ items: Array(101).fill(item("a/b#1", "queued")) }).error, /at most/); +}); + +const SNAPSHOT = { + items: [ + { ref: "acme-client/secret-app#3", repo: "acme-client/secret-app", number: 3, kind: "issue", state: "queued", rev: 1, seat: null, pr: null, session_url: null, updated_at: "2026-10-16T09:00:00Z" }, + { ref: "setkernel/sk-demo#14", repo: "setkernel/sk-demo", number: 14, kind: "issue", state: "queued", rev: 1, seat: null, pr: null, session_url: null, updated_at: "2026-10-16T10:00:00Z" }, + { ref: "setkernel/sk-demo#12", repo: "setkernel/sk-demo", number: 12, kind: "issue", state: "queued", rev: 1, seat: null, pr: null, session_url: null, updated_at: "2026-10-16T08:00:00Z" }, + { ref: "acme-client/secret-app#9", repo: "acme-client/secret-app", number: 9, kind: "issue", state: "pr_open", rev: 4, seat: "vega-setkernel", pr: 10, session_url: "https://claude.ai/code/session_01ab", updated_at: "2026-10-16T11:00:00Z" }, + { ref: "setkernel/hq#1", repo: "setkernel/hq", number: 1, kind: "issue", state: "running", rev: 3, seat: "realraghavgupta", pr: null, session_url: null, updated_at: "2026-10-16T10:30:00Z" }, + ], + events: [ + { at: "2026-10-16T11:30:00Z", ref: "acme-client/secret-app#7", repo: "acme-client/secret-app", number: 7, kind: "issue", state: "merged", seat: "vega-setkernel", pr: 8 }, + { at: "2026-10-16T07:00:00Z", ref: "setkernel/hq#2", repo: "setkernel/hq", number: 2, kind: "issue", state: "failed", seat: "lyra-setkernel", pr: null }, + { at: "2026-10-15T11:00:00Z", ref: "setkernel/hq#3", repo: "setkernel/hq", number: 3, kind: "issue", state: "merged", seat: null, pr: null }, + ], +}; + +test("internalView: the guard and a day of events, with no queue, running or seats", () => { + const v = internalView(SNAPSHOT, OK, NOW); + assert.deepEqual(Object.keys(v), ["generated_at", "snapshot_at", "guard", "last_24h"]); + assert.equal(v.last_24h.events[0].pr_url, "https://github.com/acme-client/secret-app/pull/8"); + assert.equal(v.last_24h.merged, 1); + assert.equal(v.last_24h.failed, 1); + assert.deepEqual(v.last_24h.events.map((e) => e.ref), ["acme-client/secret-app#7", "setkernel/hq#2"]); + assert.equal(v.guard.level, "ok"); +}); + +test("internalView names the guard's highest level, its metrics and reset", () => { + const g = { alert: true, degrade: true, refuse: false, detail: { degrade: { since: "x", metrics: ["workers_cpu_ms"], until: "2026-11-13T00:00:00Z" } } }; + assert.deepEqual(internalView(SNAPSHOT, g, NOW).guard, { level: "degrade", alert: true, degrade: true, refuse: false, metrics: ["workers_cpu_ms"], until: "2026-11-13T00:00:00.000Z" }); + assert.equal(internalView(SNAPSHOT, { alert: true, degrade: true, refuse: false, error: "kv down" }, NOW).guard.unreadable, true); +}); + +test("publicView: counts only; no repo, ref, URL, login or usage", () => { + const v = publicView(internalView(SNAPSHOT, OK, NOW)); + assert.deepEqual(v, { generated_at: NOW.toISOString(), last_24h: { merged: 1, failed: 1 } }); + const text = JSON.stringify(v).toLowerCase(); + for (const leak of ["acme", "secret-app", "sk-demo", "setkernel/", "http", "claude.ai", "raghav", "-setkernel", "five_hour", "headroom", "guard"]) { + assert.ok(!text.includes(leak), `public view leaks ${leak}`); + } +}); + +test("esc escapes everything HTML gives meaning to", () => { + assert.equal(esc(`&`), "<a href="x" onclick='y'>&</a>"); + assert.equal(esc(null), ""); +}); diff --git a/workers/status/test/setup.js b/workers/status/test/setup.js new file mode 100644 index 0000000..73acad3 --- /dev/null +++ b/workers/status/test/setup.js @@ -0,0 +1,11 @@ +// Loaded before the tests (package.json: node --import): `cloudflare:workers` exists only on the +// Workers runtime, so the tests resolve it to a stand-in with the same base classes. +import { registerHooks } from "node:module"; + +const STAND_IN = new URL("./cloudflare-workers.js", import.meta.url).href; + +registerHooks({ + resolve(specifier, context, next) { + return specifier === "cloudflare:workers" ? { url: STAND_IN, shortCircuit: true } : next(specifier, context); + }, +}); diff --git a/workers/status/test/worker.test.js b/workers/status/test/worker.test.js new file mode 100644 index 0000000..cb9cd33 --- /dev/null +++ b/workers/status/test/worker.test.js @@ -0,0 +1,182 @@ +// End to end in one process: the control Worker's views go in through the Feed entrypoint, land in the real +// Board class on a real SQLite, and come out of /status as HTML and JSON behind the Access check. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import worker, { Board, Feed, createHandler, guarded } from "../src/index.js"; +import { fakeAccess, fakeNamespace, memoryKV } from "./fakes.js"; + +const AUD = "aud-sk-status"; +const T0 = Date.parse("2026-10-16T12:00:00Z"); +const WALL = "wall-client-id.access"; +const PUBLISHER = "publisher-client-id.access"; + +async function setup({ guard = {}, env: extra = {} } = {}) { + const access = await fakeAccess(); + const kv = memoryKV(Object.fromEntries(Object.entries(guard).map(([k, v]) => [`guard:${k}`, JSON.stringify(v)]))); + const env = { GUARD: kv, STATUS_ACCESS_AUD: AUD, STATUS_CLIENTS: JSON.stringify({ [WALL]: "internal", [PUBLISHER]: "public" }), ...extra }; + const clock = { t: T0 }; + // the board prunes and filters by its own clock, so it runs on the test's, not the real date + env.BOARD = fakeNamespace(Board, env, (b) => { b.now = () => new Date(clock.t); }); + const handle = createHandler({ getKeys: access.getKeys, now: () => new Date(clock.t) }); + const base = { iss: "https://setkernel.cloudflareaccess.com", aud: [AUD], exp: T0 / 1000 + 3600 }; + const tokens = { + raghav: await access.sign({ ...base, email: "raghav@example.com", sub: "u1" }), + wall: await access.sign({ ...base, common_name: WALL, sub: "" }), + publisher: await access.sign({ ...base, common_name: PUBLISHER, sub: "" }), + stranger: await access.sign({ ...base, common_name: "someone-else.access", sub: "" }), + otherApp: await access.sign({ ...base, aud: ["another-app"], email: "raghav@example.com" }), + expired: await access.sign({ ...base, exp: T0 / 1000 - 1, email: "raghav@example.com" }), + }; + const get = (path, token, headers = {}) => handle(new Request(`https://ctl.setkernel.net${path}`, { headers: { ...(token ? { "cf-access-jwt-assertion": token } : {}), ...headers } }), env); + return { env, kv, clock, tokens, get, feed: new Feed({}, env) }; +} + +// What the control Worker's WorkItem.get() answers +const item = (ref, state, rev, extra = {}, dispatch = null) => ({ item: { ref, kind: "issue", state, rev, seat: null, pr: null, created_at: "2026-10-16T08:00:00Z", updated_at: "2026-10-16T11:00:00Z", ...extra }, dispatch }); +const fired = { at: "2026-10-16T10:59:00Z", seat: "vega-setkernel", rev: 2, outcome: "fired", status: 200, session_id: "session_01ab", session_url: "https://claude.ai/code/session_01ab", error: null }; + +async function fill(feed) { + const r = await feed.publish({ + items: [ + item("acme-client/secret-app#3", "queued", 1), + item("setkernel/sk-demo#14", "queued", 1), + item("setkernel/sk-demo#12", "running", 3, { seat: "vega-setkernel" }, fired), + item("setkernel/hq#7", "merged", 6, { pr: 8, seat: "lyra-setkernel" }), + ], + }); + assert.equal(r.applied, 4); +} + +test("GET /status renders the board for Raghav's phone, from the Durable Object's state", async () => { + const { feed, get, tokens } = await setup(); + await fill(feed); + const res = await get("/status", tokens.raghav, { accept: "text/html,application/xhtml+xml" }); + assert.equal(res.status, 200); + assert.match(res.headers.get("content-type"), /text\/html/); + assert.match(res.headers.get("content-security-policy"), /default-src 'none'/); + const html = await res.text(); + assert.doesNotMatch(html, /Running|Queue|Seats|sk-demo#12|acme-client|session_01ab/, "no running, queue or seats parts"); + assert.match(html, /Last 24 h: 1 merged, 0 failed/); + assert.match(html, /setkernel\/hq#7<\/a>/); + assert.match(html, /Guard: ok/); + assert.match(html, /url\(\/status\/fonts\/manrope-latin-400-normal.woff2\)/); + assert.doesNotMatch(html, / { + const { feed, get, tokens } = await setup(); + await fill(feed); + const res = await get("/status/internal.json", tokens.wall); + assert.equal(res.status, 200); + assert.equal(res.headers.get("cache-control"), "private, max-age=120"); + const v = await res.json(); + assert.deepEqual(Object.keys(v), ["generated_at", "snapshot_at", "guard", "last_24h"], "no queue, running or seats"); + assert.deepEqual(v.last_24h.events.map((e) => [e.ref, e.state, e.pr_url]), [["setkernel/hq#7", "merged", "https://github.com/setkernel/hq/pull/8"]]); + // a browser asking /status with a service token still gets JSON + assert.match((await get("/status", tokens.wall, { accept: "text/html" })).headers.get("content-type"), /json/); +}); + +test("the publisher's token reads the sanitized view and nothing else", async () => { + const { feed, get, tokens } = await setup(); + await fill(feed); + const res = await get("/status/public.json", tokens.publisher); + assert.equal(res.status, 200); + const body = await res.text(); + assert.deepEqual(JSON.parse(body), { generated_at: new Date(T0).toISOString(), last_24h: { merged: 1, failed: 0 } }); + for (const leak of ["acme", "secret-app", "sk-demo", "http", "-setkernel", "session"]) assert.ok(!body.includes(leak), leak); + assert.equal((await get("/status/internal.json", tokens.publisher)).status, 403); + assert.equal((await get("/status", tokens.publisher)).status, 403); + assert.equal((await get("/status/public.json", tokens.raghav)).status, 200); +}); + +test("no valid Access token for this app, no status", async () => { + const { get, tokens, env } = await setup(); + for (const [why, token] of [["none", null], ["garbage", "a.b.c"], ["another app", tokens.otherApp], ["expired", tokens.expired], ["unlisted service token", tokens.stranger]]) { + assert.equal((await get("/status/internal.json", token)).status, 401, why); + } + assert.equal(env.BOARD.gets, 0, "the board is never read before auth"); +}); + +test("fails closed: with no audience configured, even a good token gets a 401", async () => { + const { get, tokens } = await setup({ env: { STATUS_ACCESS_AUD: undefined } }); + assert.equal((await get("/status/internal.json", tokens.raghav)).status, 401); +}); + +test("a malformed STATUS_CLIENTS lets no service token in, and people still get in", async () => { + const { get, tokens } = await setup({ env: { STATUS_CLIENTS: JSON.stringify({ [WALL]: "admin" }) } }); + assert.equal((await get("/status/internal.json", tokens.wall)).status, 401); + assert.equal((await get("/status/internal.json", tokens.raghav)).status, 200); +}); + +test("guard:refuse stops the public feed first; Raghav and the wall still see the guard", async () => { + const until = new Date(T0 + 3 * 86400_000).toISOString(); + const { get, tokens } = await setup({ guard: { refuse: { since: "x", metrics: ["workers_cpu_ms"], until } } }); + const pub = await get("/status/public.json", tokens.publisher); + assert.equal(pub.status, 503); + assert.ok(Number(pub.headers.get("retry-after")) > 0); + const internal = await get("/status/internal.json", tokens.wall); + assert.equal(internal.status, 200); + assert.equal(internal.headers.get("cache-control"), "private, max-age=600"); + assert.deepEqual((await internal.json()).guard, { level: "refuse", alert: true, degrade: true, refuse: true, metrics: ["workers_cpu_ms"], until }); + const html = await (await get("/status", tokens.raghav, { accept: "text/html" })).text(); + assert.match(html, /Guard: refuse<\/span> \(workers_cpu_ms, until/); +}); + +test("pollers reuse a snapshot for 120 s (600 s under degrade), people for 15 s", async () => { + const { get, tokens, env, clock, kv } = await setup(); + await get("/status/internal.json", tokens.wall); + await get("/status/internal.json", tokens.wall); + assert.equal(env.BOARD.gets, 1); + clock.t += 119_000; + const reused = await get("/status/internal.json", tokens.wall); + assert.equal(env.BOARD.gets, 1); + assert.equal(reused.headers.get("vary"), "accept"); + const body = await reused.json(); + assert.equal(body.generated_at, new Date(clock.t).toISOString()); + assert.equal(body.snapshot_at, new Date(T0).toISOString(), "the data's age shows"); + await get("/status/internal.json", tokens.raghav); // older than a person's 15 s + assert.equal(env.BOARD.gets, 2); + await kv.put("guard:degrade", JSON.stringify({ since: "x", metrics: ["do_requests"], until: null })); + clock.t += 121_000; // the guard is re-read after 60 s: degrade; the snapshot is 121 s old + await get("/status/internal.json", tokens.wall); + assert.equal(env.BOARD.gets, 2, "under degrade the wall waits 600 s, not 120"); + clock.t += 480_000; + await get("/status/internal.json", tokens.wall); + assert.equal(env.BOARD.gets, 3); +}); + +test("unknown paths are 404, writes are 405, and an error hides its detail", async () => { + const { get, tokens, env } = await setup(); + assert.equal((await get("/status/../work-items", tokens.raghav)).status, 404); + assert.equal((await get("/statusx", tokens.raghav)).status, 404); + const post = await createHandler()(new Request("https://ctl.setkernel.net/status", { method: "POST" }), env); + assert.equal(post.status, 405); + const broken = { ...env, BOARD: { idFromName: () => ({}), get: () => ({ snapshot: async () => { throw new Error("secret detail"); } }) } }; + const logged = []; + const quiet = console.error; + console.error = (m) => logged.push(m); + try { + const access = await fakeAccess(); + const handle = guarded(createHandler({ getKeys: access.getKeys, now: () => new Date(T0) })); + const token = await access.sign({ iss: "https://setkernel.cloudflareaccess.com", aud: AUD, exp: T0 / 1000 + 60, email: "raghav@example.com" }); + const res = await handle(new Request("https://ctl.setkernel.net/status/internal.json", { headers: { "cf-access-jwt-assertion": token } }), broken); + assert.equal(res.status, 500); + assert.doesNotMatch(await res.text(), /secret detail/); + assert.match(logged.join("\n"), /secret detail/); + } finally { + console.error = quiet; + } +}); + +test("the default export is a fetch handler that fails closed without an audience", async () => { + const res = await worker.fetch(new Request("https://ctl.setkernel.net/status/internal.json", { headers: { "cf-access-jwt-assertion": "a.b.c" } }), { STATUS_ACCESS_AUD: "" }); + assert.equal(res.status, 401); +}); + +test("Feed.publish answers what the board did, so the control Worker can log a rejected view", async () => { + const { feed } = await setup(); + const r = await feed.publish({ items: [item("setkernel/sk-demo#1", "queued", 1), item("bad", "queued", 1)] }); + assert.equal(r.ok, true); + assert.equal(r.applied, 1); + assert.equal(r.rejected[0].error, "ref must be owner/repo#N"); +}); diff --git a/workers/status/wrangler.toml b/workers/status/wrangler.toml new file mode 100644 index 0000000..2659c99 --- /dev/null +++ b/workers/status/wrangler.toml @@ -0,0 +1,47 @@ +# sk-status: the read model of the org, as one Durable Object (hq#208). Routes on +# ctl.setkernel.net/status only, behind its own Access app; no workers.dev or preview URL. The control Worker +# writes to it over a service binding (the Feed entrypoint), never over HTTP (README.md). +name = "sk-status" +main = "src/index.js" +compatibility_date = "2026-10-01" +workers_dev = false +preview_urls = false +# Every var reaches the Worker through the Infisical -> Cloudflare Workers secret sync (project +# sk-ctl-workers, env prod, folder /sk-status only: / holds sk-meter's and the control Worker's credentials), +# never through this file or CI (decision 0003). +keep_vars = true + +# /status and everything under /status/, nothing else on the hostname. The DNS record and the +# Access app are tofu in setkernel/setkernel-cloudflare; the routes need them first (README.md). +routes = [ + { pattern = "ctl.setkernel.net/status", zone_name = "setkernel.net" }, + { pattern = "ctl.setkernel.net/status/*", zone_name = "setkernel.net" }, +] + +# The page's two font files and their licence. Static assets are served before any Worker code +# and aren't billed as Worker requests. +[assets] +directory = "public" + +[observability] +enabled = true + +# sk-meter's guard flags. The `sk-meter` namespace, owned by tofu in setkernel/setkernel-cloudflare +# (output sk_meter_kv_namespace_id): bind it, never create it. sk-status only reads it. +[[kv_namespaces]] +binding = "GUARD" +id = "f79ca12aeb9347f9851f5dfd53313f39" + +[[durable_objects.bindings]] +name = "BOARD" +class_name = "Board" + +# Migration tags are permanent once deployed: never edit or reuse "v1"; a change is a new tag. +[[migrations]] +tag = "v1" +new_sqlite_classes = ["Board"] + +# Set by the sync from Infisical sk-ctl-workers/prod, folder /sk-status (never in this file): +# STATUS_ACCESS_AUD the audience tag of the Access app `sk-status`. Unset, every request is a 401. +# STATUS_CLIENTS JSON { "": "internal" | "public" }: the wall's +# token and the showcase publisher's. Unset, only people get in.