Public: anyone can read this; the crew can change it.
Defect of #11595: production has no schedule, so the morning paper's one unattended morning cannot be proven #15501
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15499-defect-of-11595-production-has-no-schedu → main 0 commits fork
5 files
changed +146
−0
docs/OPERATING.md +2
| @@ -366,6 +366,8 @@ and an accessibility tree, which nothing dependency-free gives. | ||
| 366 | 366 | `bun scripts/v1-probe.ts https://forge.open-platform.sh` reads production (GETs and one MCP initialize, never signed in) and prints PASS, FAIL or UNKNOWN per v1.0.0 item with its URL and evidence. |
| 367 | 367 | The `schedules ran in 24 h` line reads the newest `<time datetime>` on `<base-url>/edition` by default (#15474): PASS within 24 h, FAIL older, UNKNOWN when the page is not public, and UNKNOWN with `this release predates /edition` when it answers 404, as a release without the page does. `--edition <url>` replaces it and is only needed for a page on another host (the morning paper's latest edition printed by a scheduled app); it is the only extra host read. |
| 368 | 368 | That page (`src/http/edition.ts`) prints when the newest morning paper ran and nothing of the thread it was posted in; before any paper has run it prints no `<time>`. Its ETag holds the newest run and the build, so a deploy never answers 304 to older markup; signed-in and flash responses are never tagged. The paper is written by the routine clock (`startRoutines` in `src/crew/routines.ts`, a pass every minute, the paper at 07:00 UTC), which starts only when the crew does: a preview, where the crew is held off, never prints one and its `/edition` stays without a `<time>`. Probe production, not a preview (#15405). |
| 369 | ||
| 370 | The production forge's `/api/v1/health` `schedules` counts are proven by this repository's own schedule (#15499): its `forge.json` declares `heartbeat`, `5 7 * * *` UTC, production only, a signed `POST /jobs/heartbeat` (`src/http/heartbeat.ts`) that writes nothing and calls no model. The production forge runs this repository as an app, so once the commit is on `main` it syncs that schedule (`total` 1), wakes the app if it sleeps, and after 07:05 UTC the first 200 makes `ran` 1. A 401 from it names why (unsigned, forged, expired, or no `FORGE_APP_IDENTITY_KEY`: a forge not run by another) and is final. | |
| 369 | 371 | The release it checks is the newest the MCP `releases` tool lists for the repository; when none is listed but the answer names the running `version` and `commit` (production live, `"live":[]`), that running build is the release, read from `GET /api/v1/repos/<repo>/commits/<sha>/check` and compared to what production announces, and its lines say `running v…`. Only an answer that lists none and names no running commit FAILs for a missing release (#15473). |
| 370 | 372 | The owner tags v1.0.0 only when every line is PASS; an UNKNOWN line names what a person must check, and is not a pass. |
| 371 | 373 |
forge.json +3
| @@ -15,6 +15,9 @@ | ||
| 15 | 15 | "api.typesafe.ai", |
| 16 | 16 | "graph.instagram.com" |
| 17 | 17 | ], |
| 18 | "schedules": [ | |
| 19 | { "name": "heartbeat", "cron": "5 7 * * *", "timezone": "UTC", "path": "/jobs/heartbeat", "timeout": 120, "retries": 3, "catchUp": "6h" } | |
| 20 | ], | |
| 18 | 21 | "app": { |
| 19 | 22 | "looks": ["/", "/docs", "/explore", "/board", "/notes", "/review", "/changelog", "/apps", "/security", "/system", "/admin/limits", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space", "/looks/missing", "/looks/failed", "/nope/nothing"], |
| 20 | 23 | "looksAsOwner": ["/", "/board", "/flow", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/talk", "/system", "/admin/limits", "/maintenance", "/resilience", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space"], |
src/http/heartbeat.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** | |
| 2 | * The heartbeat schedule's route (#15499): `POST /jobs/heartbeat`, the call this repository's own | |
| 3 | * `forge.json` `schedules` asks its host forge to make once a day. This forge runs as an app of | |
| 4 | * the production forge, so that one declared schedule is the production forge's proof that its | |
| 5 | * clock fires with no person involved (`/api/v1/health` `schedules.total` and `.ran`). | |
| 6 | * | |
| 7 | * It does nothing but answer: no state written, no model called, no money spent. It answers 200 | |
| 8 | * only to a call the host forge signed (`timer_fire.ts` `callHeaders`: `x-forge-identity` is | |
| 9 | * `forge-timer|<idempotency key>|timer|<expiry ms>`, `x-forge-signature` its HMAC-SHA256 in | |
| 10 | * base64url under `FORGE_APP_IDENTITY_KEY`), unexpired; anything else is a 401 that says why. | |
| 11 | * Plain data in, a Response out. | |
| 12 | */ | |
| 13 | ||
| 14 | import { createHmac, timingSafeEqual } from "node:crypto"; | |
| 15 | ||
| 16 | export const HEARTBEAT_PATH = "/jobs/heartbeat"; | |
| 17 | ||
| 18 | type Env = Record<string, string | undefined>; | |
| 19 | ||
| 20 | /** The key the host forge signs its calls with; undefined when this forge has no host. */ | |
| 21 | export function heartbeatKey(env: Env = process.env): string | undefined { | |
| 22 | const key = (env.FORGE_APP_IDENTITY_KEY ?? "").trim(); | |
| 23 | return key === "" ? undefined : key; | |
| 24 | } | |
| 25 | ||
| 26 | /** Why a call is not the host forge's, or undefined when it is. */ | |
| 27 | export function heartbeatRefusal(headers: Headers, key: string | undefined, now: number): string | undefined { | |
| 28 | if (key === undefined) return "this forge is not run by another, so no schedule of a host forge can reach it"; | |
| 29 | const identity = headers.get("x-forge-identity"); | |
| 30 | const signature = headers.get("x-forge-signature"); | |
| 31 | if (identity === null || signature === null) return "a heartbeat is the host forge's signed call; this one is unsigned"; | |
| 32 | if (identity.length > 300) return "the identity is too long"; | |
| 33 | const expected = Buffer.from(createHmac("sha256", key).update(identity).digest("base64url")); | |
| 34 | const given = Buffer.from(signature); | |
| 35 | if (given.length !== expected.length || !timingSafeEqual(given, expected)) return "the signature does not match"; | |
| 36 | const parts = identity.split("|"); | |
| 37 | if (parts.length !== 4 || parts[0] !== "forge-timer" || parts[2] !== "timer") return "the identity is not a timer's"; | |
| 38 | const expires = parts[3] ?? ""; | |
| 39 | if (!/^\d{1,15}$/.test(expires) || Number(expires) <= now) return "the call has expired"; | |
| 40 | return undefined; | |
| 41 | } | |
| 42 | ||
| 43 | const json = (status: number, body: unknown, extra: Record<string, string> = {}): Response => | |
| 44 | new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store", ...extra } }); | |
| 45 | ||
| 46 | /** The answer to any request on `HEARTBEAT_PATH`. */ | |
| 47 | export function heartbeatAnswer(request: Request, key: string | undefined, now = Date.now()): Response { | |
| 48 | if (request.method === "OPTIONS") return new Response(null, { status: 204, headers: { allow: "POST, OPTIONS" } }); | |
| 49 | if (request.method !== "POST") return json(405, { error: "the heartbeat is a POST" }, { allow: "POST, OPTIONS" }); | |
| 50 | const refused = heartbeatRefusal(request.headers, key, now); | |
| 51 | // A refusal is final: delivering the same unsigned or expired call again cannot make it signed. | |
| 52 | if (refused !== undefined) return json(401, { error: refused }, { "x-forge-no-retry": "1" }); | |
| 53 | return json(200, { ok: true, at: new Date(now).toISOString() }); | |
| 54 | } |
src/http/server.ts +6
| @@ -112,6 +112,7 @@ import { activeEdgeToken, edgeIdentity } from "./edge.ts"; | ||
| 112 | 112 | import { shieldState } from "../actions/shield.ts"; |
| 113 | 113 | import { crawlRefused, familyOf, finishedStrangerDeep, isCrawler, isDeepPath, isShieldedPath, noteCrawler, noteDeepStranger, noteShielded, shieldRefused, takeCrawl, wasCrawler, wasDeepStranger, wasShielded } from "./crawl.ts"; |
| 114 | 114 | import { liveResponse, readyResponse } from "./health.ts"; |
| 115 | import { HEARTBEAT_PATH, heartbeatAnswer, heartbeatKey } from "./heartbeat.ts"; | |
| 115 | 116 | import { isRead, readOnlyRefusal } from "./read_only.ts"; |
| 116 | 117 | import { fullRefusal, isFullError, noteFull } from "../kernel/full_disk.ts"; |
| 117 | 118 | import { running } from "../kernel/version.ts"; |
| @@ -3419,6 +3420,11 @@ async function answer( | ||
| 3419 | 3420 | noteUsage(request, { pattern: "/readyz", role: "anon" }); |
| 3420 | 3421 | return readyResponse(); |
| 3421 | 3422 | } |
| 3423 | // The host forge's daily heartbeat schedule (#15499): signed, server to server, writes nothing. | |
| 3424 | if (url.pathname === HEARTBEAT_PATH) { | |
| 3425 | noteUsage(request, { pattern: HEARTBEAT_PATH, role: "anon" }); | |
| 3426 | return heartbeatAnswer(request, heartbeatKey()); | |
| 3427 | } | |
| 3422 | 3428 | const routeMethod = request.method === "HEAD" ? "GET" : request.method; |
| 3423 | 3429 | // The one place a path decides its own shape of miss: anything under |
| 3424 | 3430 | // `/api` matches only against the API's own routes, so a studio route as |
test/heartbeat-schedule.test.ts added +81
| @@ -0,0 +1,81 @@ | ||
| 1 | /** | |
| 2 | * This repository's own schedule (#15499): its forge.json declares the daily heartbeat, the host | |
| 3 | * forge loads it, and the route answers 2xx only to the host's signed call, so the production | |
| 4 | * forge's `/api/v1/health` counts one schedule that ran with no person involved. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { readFileSync } from "node:fs"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { parseManifest } from "../src/apps/manifest.ts"; | |
| 14 | import { callHeaders } from "../src/apps/timer_fire.ts"; | |
| 15 | import { HEARTBEAT_PATH, heartbeatAnswer, heartbeatKey } from "../src/http/heartbeat.ts"; | |
| 16 | import { serve } from "../src/http/server.ts"; | |
| 17 | ||
| 18 | const KEY = "k".repeat(43); | |
| 19 | const NOW = Date.UTC(2026, 10, 22, 7, 5); | |
| 20 | const signed = (key = KEY, expires = NOW + 60_000): Record<string, string> => | |
| 21 | callHeaders(key, "heartbeat", NOW, 1, expires, `app:heartbeat:${NOW}`); | |
| 22 | const post = (headers: Record<string, string>, method = "POST"): Request => | |
| 23 | new Request(`http://forge.test${HEARTBEAT_PATH}`, { method, headers }); | |
| 24 | ||
| 25 | test("this repository's forge.json declares the heartbeat schedule, production, on the route", () => { | |
| 26 | const root = join(import.meta.dir, ".."); | |
| 27 | const manifest = parseManifest(readFileSync(join(root, "forge.json"), "utf8"), readFileSync(join(root, "package.json"), "utf8")); | |
| 28 | if (!manifest.ok) throw new Error(JSON.stringify(manifest.error)); | |
| 29 | const beat = manifest.value.schedules?.find((s) => s.name === "heartbeat"); | |
| 30 | expect(beat).toMatchObject({ path: HEARTBEAT_PATH, env: "production", cron: "5 7 * * *", timezone: "UTC" }); | |
| 31 | }); | |
| 32 | ||
| 33 | test("the host forge's signed call answers 200; unsigned, forged, expired or without a key, 401 and final", async () => { | |
| 34 | const good = heartbeatAnswer(post(signed()), KEY, NOW); | |
| 35 | expect(good.status).toBe(200); | |
| 36 | expect(await good.json()).toEqual({ ok: true, at: new Date(NOW).toISOString() }); | |
| 37 | for (const [req, key] of [ | |
| 38 | [post({}), KEY], | |
| 39 | [post(signed("x".repeat(43))), KEY], | |
| 40 | [post(signed(KEY, NOW - 1)), KEY], | |
| 41 | [post(signed()), undefined], | |
| 42 | ] as const) { | |
| 43 | const res = heartbeatAnswer(req, key, NOW); | |
| 44 | expect(res.status).toBe(401); | |
| 45 | expect(res.headers.get("x-forge-no-retry")).toBe("1"); | |
| 46 | } | |
| 47 | expect(heartbeatAnswer(post(signed(), "GET"), KEY, NOW).status).toBe(405); | |
| 48 | }); | |
| 49 | ||
| 50 | test("the key is FORGE_APP_IDENTITY_KEY, in production as in a preview", () => { | |
| 51 | expect(heartbeatKey({ FORGE_APP_IDENTITY_KEY: ` ${KEY} `, FORGE_APP_ENV: "production" })).toBe(KEY); | |
| 52 | expect(heartbeatKey({})).toBeUndefined(); | |
| 53 | }); | |
| 54 | ||
| 55 | let dataDir: string; | |
| 56 | let app: App; | |
| 57 | beforeAll(async () => { | |
| 58 | dataDir = await mkdtemp(join(tmpdir(), "forge-heartbeat-")); | |
| 59 | app = createApp({ dataDir, port: 0 }); | |
| 60 | }); | |
| 61 | afterAll(async () => { | |
| 62 | app.close(); | |
| 63 | await rm(dataDir, { recursive: true, force: true }); | |
| 64 | }); | |
| 65 | ||
| 66 | test("the served route takes the cross-origin server call without a session and keeps the floor", async () => { | |
| 67 | const servers = await serve(app); | |
| 68 | const before = process.env.FORGE_APP_IDENTITY_KEY; | |
| 69 | process.env.FORGE_APP_IDENTITY_KEY = KEY; | |
| 70 | try { | |
| 71 | const at = Date.now(); | |
| 72 | const res = await fetch(`${servers.url}${HEARTBEAT_PATH}`, { method: "POST", headers: callHeaders(KEY, "heartbeat", at, 1, at + 60_000, `a:heartbeat:${at}`) }); | |
| 73 | expect(res.status).toBe(200); | |
| 74 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 75 | expect((await fetch(`${servers.url}${HEARTBEAT_PATH}`, { method: "POST" })).status).toBe(401); | |
| 76 | } finally { | |
| 77 | if (before === undefined) delete process.env.FORGE_APP_IDENTITY_KEY; | |
| 78 | else process.env.FORGE_APP_IDENTITY_KEY = before; | |
| 79 | servers.stop(); | |
| 80 | } | |
| 81 | }); |
new above
offline