Public: anyone can read this; the crew can change it.
An edge-kept finding hands the owner the exact edge cache rule to paste, on the issue and on /maintenance #17054
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/17016-an-edge-kept-finding-hands-the-owner-the → main 0 commits fork 3 threads · 3 unresolved
6 files
changed +143
−15
docs/features/maintenance-duties.md +1 −1
| @@ -34,7 +34,7 @@ Tests: `test/maintain-wait-split.test.ts`. | ||
| 34 | 34 | |
| 35 | 35 | ## Edge kept (#14663) |
| 36 | 36 | |
| 37 | The `edge-kept` duty notices when the edge is not keeping the pages the forge marks shareable. Hourly, it sends two signed-out GETs (no cookie) to each of `/` and `/explore`, and only at `config.url` (the forge's own public origin), never a host from a request. If the forge marked the answer shareable (`Cache-Control: public` with a positive `s-maxage`) and the `cf-cache-status` header says the edge went to the forge both times (`DYNAMIC`, `MISS` or `BYPASS`), it files one issue per page, once, and leaves it open. A `HIT`, `STALE` or `REVALIDATED` on either read counts as kept. No edge header, an unshared page, a non-200 answer, a host the forge's gate refuses, or a forge with no `FORGE_URL` say nothing but an action line; so does a forge without its own repository row, which has nowhere to file. The one fix is the owner's, outside this repository: an edge cache rule that caches signed-out HTML and keys on `Cookie, Authorization, X-Plat-User, X-Plat-Edge-Token` (`EDGE_VARY`). The issue is assigned to the repository's owner, so the crew's dispatcher (which starts only agent assignees) hands no builder a fix that lives outside the repository; an open one with no assignee is assigned to the owner on the next run, with an action line saying so. The duty's lines (one per page) show on the maintenance runs lists (`/api/v1/maintenance/runs`, the terminal) after its first run, an hour after boot. | |
| 37 | The `edge-kept` duty notices when the edge is not keeping the pages the forge marks shareable. Hourly, it sends two signed-out GETs (no cookie) to each of `/` and `/explore`, and only at `config.url` (the forge's own public origin), never a host from a request. If the forge marked the answer shareable (`Cache-Control: public` with a positive `s-maxage`) and the `cf-cache-status` header says the edge went to the forge both times (`DYNAMIC`, `MISS` or `BYPASS`), it files one issue per page, once, and leaves it open. A `HIT`, `STALE` or `REVALIDATED` on either read counts as kept. No edge header, an unshared page, a non-200 answer, a host the forge's gate refuses, or a forge with no `FORGE_URL` say nothing but an action line; so does a forge without its own repository row, which has nowhere to file. The one fix is the owner's, outside this repository, and the issue holds it as one copyable block: match a signed-out GET of HTML with no session cookie, cache eligible, respect origin `Cache-Control`, and a cache key varying on each header of `EDGE_VARY` (`Cookie, Authorization, X-Plat-User, X-Plat-Edge-Token`; the block is built from it, never typed). The duty closes its own finding: when both reads of a later run are served from the edge (`HIT`, `STALE` or `REVALIDATED`; one kept read alone does not close, so it cannot flap) it comments with the header it saw and closes the issue, and its action line says `kept`. The edge-kept row on `/maintenance` (and the terminal's duty list) links to the open finding. The issue is assigned to the repository's owner, so the crew's dispatcher (which starts only agent assignees) hands no builder a fix that lives outside the repository; an open one with no assignee is assigned to the owner on the next run, with an action line saying so. The duty's lines (one per page) show on the maintenance runs lists (`/api/v1/maintenance/runs`, the terminal) after its first run, an hour after boot. | |
| 38 | 38 | |
| 39 | 39 | Tests: `test/maintain-edge-kept.test.ts`. |
| 40 | 40 |
src/maintain/duties/edge_kept.ts +14 −8
| @@ -13,15 +13,14 @@ | ||
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | import { fileIssueOnce } from "../../checks/flake.ts"; |
| 16 | import { getIssue, setAssignee } from "../../core/issues.ts"; | |
| 16 | import { addComment, getIssue, setAssignee, setIssueState } from "../../core/issues.ts"; | |
| 17 | 17 | import { getRepo } from "../../core/repos.ts"; |
| 18 | import { EDGE_VARY } from "../../http/edge_cache.ts"; | |
| 19 | 18 | import { ownRepoPath } from "../../kernel/own_repo.ts"; |
| 20 | 19 | import { cellProxy } from "../../apps/egress.ts"; |
| 21 | 20 | import type { Duty, Finding } from "../duty.ts"; |
| 22 | import { edgeHeader, edgeVerdict, marked } from "../edge_kept.ts"; | |
| 21 | import { bothKept, edgeHeader, edgeRule, edgeVerdict, findingFor, marked, PATHS, title } from "../edge_kept.ts"; | |
| 23 | 22 | |
| 24 | export const PATHS = ["/", "/explore"] as const; | |
| 23 | export { PATHS, title }; | |
| 25 | 24 | const TIMEOUT_MS = 8_000; |
| 26 | 25 | |
| 27 | 26 | /** One signed-out GET; the test hands in its own. */ |
| @@ -33,13 +32,12 @@ const get: Get = async (url) => { | ||
| 33 | 32 | return res.headers.has("x-forge-egress") || res.status !== 200 ? undefined : res.headers; |
| 34 | 33 | }; |
| 35 | 34 | |
| 36 | export const title = (path: string): string => `The edge is not keeping ${path}, which the forge marks shareable`; | |
| 37 | ||
| 38 | 35 | const body = (path: string, seen: string): string => |
| 39 | 36 | `A signed-out GET of ${path} was marked shareable by the forge (\`Cache-Control: public, s-maxage\`), but twice in a row the edge did not serve it from its copy (${seen}).\n\n` + |
| 40 | 37 | "This is the owner's to do; a builder has nothing to change here.\n\n" + |
| 41 | `The one fix is the owner's, outside this repository: an edge cache rule that caches signed-out HTML and keys on \`${EDGE_VARY}\`.\n\n` + | |
| 42 | "This issue is filed once per page and left open until it is fixed; the hourly duty `edge-kept` checks again."; | |
| 38 | "The one fix is the owner's, outside this repository: set this edge cache rule (copy it as it stands):\n\n" + | |
| 39 | `\`\`\`\n${edgeRule()}\n\`\`\`\n\n` + | |
| 40 | "This issue is filed once per page. The hourly duty `edge-kept` checks again and closes it when both reads of a run are served from the edge."; | |
| 43 | 41 | |
| 44 | 42 | /** Whether this forge runs in a cell (hosted as an app): its way out is its parent's gate. */ |
| 45 | 43 | const inACell = (): boolean => cellProxy() !== undefined; |
| @@ -80,6 +78,14 @@ export function edgeKept(read: Get = get, inCell: () => boolean = inACell): Duty | ||
| 80 | 78 | const verdict = edgeVerdict(first, second); |
| 81 | 79 | const seen = edgeHeader(second) ?? "no edge header"; |
| 82 | 80 | actions.push(`${path}: ${verdict} (${seen})`); |
| 81 | if (verdict === "kept" && repo !== undefined && bothKept(first, second)) { | |
| 82 | const issue = findingFor(db, repo, path); | |
| 83 | if (issue !== undefined) { | |
| 84 | addComment(db, { issueId: issue.id, authorId: repo.ownerId, body: `Both reads of ${path} were served from the edge (${seen}); the rule is in. Closing this finding.` }); | |
| 85 | setIssueState(db, issue.id, "closed"); | |
| 86 | actions.push(`${path}: closed issue #${String(issue.number)}, the edge keeps it`); | |
| 87 | } | |
| 88 | } | |
| 83 | 89 | if (verdict !== "not") continue; |
| 84 | 90 | const summary = `${path} is marked shareable but the edge did not keep it (${seen})`; |
| 85 | 91 | const probe = { kind: "measurement", what: `GET ${origin.origin}${path} twice, signed out: 1 is not served from the edge`, number: 1, at: now.toISOString() } as const; |
src/maintain/edge_kept.ts +48
| @@ -4,6 +4,12 @@ | ||
| 4 | 4 | * speak. A forge with no edge in front sends no edge header, which is unknown, never a fault. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { getIssue } from "../core/issues.ts"; | |
| 9 | import { getRepo } from "../core/repos.ts"; | |
| 10 | import { EDGE_VARY } from "../http/edge_cache.ts"; | |
| 11 | import { ownRepoPath } from "../kernel/own_repo.ts"; | |
| 12 | ||
| 7 | 13 | export type Kept = "kept" | "not" | "unknown"; |
| 8 | 14 | |
| 9 | 15 | /** `cf-cache-status` values that mean the edge answered from its copy. */ |
| @@ -40,3 +46,45 @@ export function edgeVerdict(first: Headers, second: Headers): Kept { | ||
| 40 | 46 | if (a === "kept" || b === "kept") return "kept"; |
| 41 | 47 | return a === "not" && b === "not" ? "not" : "unknown"; |
| 42 | 48 | } |
| 49 | ||
| 50 | /** The pages the duty reads. */ | |
| 51 | export const PATHS = ["/", "/explore"] as const; | |
| 52 | ||
| 53 | export const title = (path: string): string => `The edge is not keeping ${path}, which the forge marks shareable`; | |
| 54 | ||
| 55 | /** | |
| 56 | * The edge cache rule the owner sets, as one copyable block; the key list is EDGE_VARY, never | |
| 57 | * typed. | |
| 58 | */ | |
| 59 | export function edgeRule(): string { | |
| 60 | return [ | |
| 61 | "Match: request method is GET, and the response is HTML, and the request has no session cookie (signed out)", | |
| 62 | "Cache: eligible for cache", | |
| 63 | "Edge TTL: respect origin Cache-Control", | |
| 64 | "Cache key: vary on each of these request headers:", | |
| 65 | ...EDGE_VARY.split(",").map((h) => ` - ${h.trim()}`), | |
| 66 | ].join("\n"); | |
| 67 | } | |
| 68 | ||
| 69 | /** Both reads of a run were served from the edge's copy: the only reading that closes a finding. */ | |
| 70 | export const bothKept = (first: Headers, second: Headers): boolean => edgeKept(first) === "kept" && edgeKept(second) === "kept"; | |
| 71 | ||
| 72 | /** The one way to find the open finding for a page in a repository; undefined when none. */ | |
| 73 | export function findingFor(db: Database, repo: { id: string }, path: string): { id: string; number: number } | undefined { | |
| 74 | const row = db.query<{ number: number }, [string, string]>("SELECT number FROM issue WHERE repo_id = ?1 AND title = ?2 AND state = 'open' LIMIT 1").get(repo.id, title(path)); | |
| 75 | return row === null ? undefined : getIssue(db, repo.id, row.number); | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * The open finding for any page in this forge's own repository, for a link; undefined when none. | |
| 80 | */ | |
| 81 | export function openFinding(db: Database): { number: number; repo: string } | undefined { | |
| 82 | const [owner = "", name = ""] = ownRepoPath().split("/"); | |
| 83 | const repo = getRepo(db, owner, name); | |
| 84 | if (repo === undefined) return undefined; | |
| 85 | for (const path of PATHS) { | |
| 86 | const issue = findingFor(db, repo, path); | |
| 87 | if (issue !== undefined) return { number: issue.number, repo: `${owner}/${name}` }; | |
| 88 | } | |
| 89 | return undefined; | |
| 90 | } |
src/tui/screens/dutyswitch.ts +3 −1
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { dismiss, dismissals, dismissalWords } from "../../maintain/dismiss.ts"; |
| 10 | 10 | import { dutyStates, switchDuty } from "../../maintain/pause.ts"; |
| 11 | import { openFinding } from "../../maintain/edge_kept.ts"; | |
| 11 | 12 | import { dutySchedules, scheduleWords } from "../../maintain/schedule.ts"; |
| 12 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| 13 | 14 | import { binding } from "../app/keys.ts"; |
| @@ -27,7 +28,8 @@ export function dutyLines(session: Session, chosen: number): string[] { | ||
| 27 | 28 | return states.map((d, i) => { |
| 28 | 29 | const dm = kept.get(d.name); |
| 29 | 30 | const sched = schedules.get(d.name); |
| 30 | return `${i === chosen ? ">" : " "} ${d.says}${dm === undefined ? "" : `; ${dismissalWords(dm)}`}${sched === undefined ? "" : ` (${scheduleWords(sched)}${sched.late ? "; late" : ""})`}`; | |
| 31 | const finding = d.name === "edge-kept" ? openFinding(session.db) : undefined; | |
| 32 | return `${i === chosen ? ">" : " "} ${d.says}${dm === undefined ? "" : `; ${dismissalWords(dm)}`}${sched === undefined ? "" : ` (${scheduleWords(sched)}${sched.late ? "; late" : ""})`}${finding === undefined ? "" : `; open finding ${finding.repo}#${String(finding.number)}`}`; | |
| 31 | 33 | }); |
| 32 | 34 | } |
| 33 | 35 |
src/web/pages/maintenance.ts +3 −1
| @@ -28,6 +28,7 @@ import { badge, btn, disclosure, field, seg } from "../ui.ts"; | ||
| 28 | 28 | import { STALE_DAYS, STALE_WORDS, closeStale, nudge, stalePulls } from "../../maintain/stale_pulls.ts"; |
| 29 | 29 | import { PREVIEW_DAYS, PREVIEW_WORDS, previewDirs, previewLine, removePreviewDirs, sizeWords, totalWords, type PreviewDir } from "../../maintain/previews.ts"; |
| 30 | 30 | import { MERGED_DAYS, MERGED_WORDS, deleteMerged, lineOf, mergedBranches, type MergedBranch } from "../../maintain/duties/merged_branches.ts"; |
| 31 | import { openFinding } from "../../maintain/edge_kept.ts"; | |
| 31 | 32 | import { dutySchedules, scheduleWords } from "../../maintain/schedule.ts"; |
| 32 | 33 | import { isKind, quiet, quieted, quietKeys, unquiet, when, type QuietKind, type Quieted } from "../../maintenance/quiet.ts"; |
| 33 | 34 | import { liveLabel } from "../../maintenance/quiet_live.ts"; |
| @@ -152,7 +153,8 @@ async function maintenanceAnswer(ctx: Ctx): Promise<Response> { | ||
| 152 | 153 | const dm = dismissed.get(d.name); |
| 153 | 154 | const detail = dm === undefined ? d.detail : dm.active && !d.paused ? `on; ${dismissalWords(dm)}` : `${d.detail}; ${dismissalWords(dm)}`; |
| 154 | 155 | const sched = schedules.get(d.name); |
| 155 | return fact(d.name, html`${detail}${sched === undefined ? null : html`; ${scheduleWords(sched)}`}${sched?.late === true ? html` ${badge("late", { tone: "attn" })}` : null} | |
| 156 | const finding = d.name === "edge-kept" ? openFinding(ctx.app.db) : undefined; | |
| 157 | return fact(d.name, html`${detail}${sched === undefined ? null : html`; ${scheduleWords(sched)}`}${finding === undefined ? null : html`; <a href="/${finding.repo}/issues/${String(finding.number)}" data-filed="link">open finding ${finding.repo}#${String(finding.number)}</a>`}${sched?.late === true ? html` ${badge("late", { tone: "attn" })}` : null} | |
| 156 | 158 | <form method="post" action="/maintenance/duties/${d.name}" class="inline"> |
| 157 | 159 | <input type="hidden" name="paused" value="${d.paused ? "no" : "yes"}">${btn(d.paused ? "Resume" : "Pause", { variant: "outline" })} |
| 158 | 160 | </form>`); |
test/maintain-edge-kept.test.ts +74 −4
| @@ -7,9 +7,12 @@ import { startMaintain } from "../src/maintain/runner.ts"; | ||
| 7 | 7 | import { latest } from "../src/maintain/runs.ts"; |
| 8 | 8 | import { duties } from "../src/maintain/registry.ts"; |
| 9 | 9 | import { edgeKept as edgeDuty, PATHS, title, type Get } from "../src/maintain/duties/edge_kept.ts"; |
| 10 | import { edgeKept, edgeVerdict, marked } from "../src/maintain/edge_kept.ts"; | |
| 11 | import { sharedCache } from "../src/http/edge_cache.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { edgeKept, edgeRule, edgeVerdict, marked, openFinding } from "../src/maintain/edge_kept.ts"; | |
| 11 | import { EDGE_VARY, sharedCache } from "../src/http/edge_cache.ts"; | |
| 12 | import { createUser, getUserById } from "../src/core/users.ts"; | |
| 13 | import { serve } from "../src/http/server.ts"; | |
| 14 | import { Session } from "../src/tui/session.ts"; | |
| 15 | import { dutyLines } from "../src/tui/screens/dutyswitch.ts"; | |
| 13 | 16 | import { createRepo } from "../src/actions/repos.ts"; |
| 14 | 17 | import { getIssue } from "../src/core/issues.ts"; |
| 15 | 18 | import { getRepo } from "../src/core/repos.ts"; |
| @@ -89,7 +92,9 @@ test("not kept files one issue per page, and a second run adds nothing", async ( | ||
| 89 | 92 | const n = app.db.query("SELECT COUNT(*) AS n FROM issue WHERE title = ?1").get(title("/")) as { n: number }; |
| 90 | 93 | expect(n.n).toBe(1); |
| 91 | 94 | const body = app.db.query("SELECT body FROM issue WHERE title = ?1").get(title("/")) as { body: string }; |
| 92 | expect(body.body).toContain("Cookie, Authorization"); | |
| 95 | for (const v of EDGE_VARY.split(",")) expect(body.body).toContain(`- ${v.trim()}`); | |
| 96 | expect(body.body).toContain("respect origin"); | |
| 97 | expect(body.body).toContain("```"); | |
| 93 | 98 | }); |
| 94 | 99 | |
| 95 | 100 | test("a new filing is assigned to the owner, says so, and the crew starts no run on it (#17014)", async () => { |
| @@ -148,3 +153,68 @@ test("a run through the maintainer lands its action lines where /maintenance run | ||
| 148 | 153 | expect(row?.actions.some((a) => a.startsWith("/: not (cf-cache-status: DYNAMIC)"))).toBe(true); |
| 149 | 154 | expect(row?.findings.length).toBe(2); |
| 150 | 155 | }); |
| 156 | ||
| 157 | test("a page kept on both reads closes the open finding with a comment naming the header; the action says kept (#17016)", async () => { | |
| 158 | let edge = "DYNAMIC"; | |
| 159 | const { app, once } = await run(async () => h({ "cache-control": "public, s-maxage=60", "cf-cache-status": edge })); | |
| 160 | await once(); | |
| 161 | const state = () => (app.db.query("SELECT state FROM issue WHERE title = ?1").get(title("/")) as { state: string }).state; | |
| 162 | expect(state()).toBe("open"); | |
| 163 | edge = "HIT"; | |
| 164 | const r = await once(); | |
| 165 | expect(r.actions.some((a) => a.startsWith("/: kept"))).toBe(true); | |
| 166 | expect(state()).toBe("closed"); | |
| 167 | const c = app.db.query("SELECT body FROM comment WHERE body LIKE '%cf-cache-status: HIT%'").get(); | |
| 168 | expect(c).not.toBeNull(); | |
| 169 | }); | |
| 170 | ||
| 171 | test("a page still not kept, or kept on one read only, leaves the finding open", async () => { | |
| 172 | const { app, once } = await run(shareable({ "cf-cache-status": "DYNAMIC" })); | |
| 173 | await once(); | |
| 174 | await once(); | |
| 175 | const state = () => (app.db.query("SELECT state FROM issue WHERE title = ?1").get(title("/")) as { state: string }).state; | |
| 176 | expect(state()).toBe("open"); | |
| 177 | const heads = [cf("MISS"), cf("HIT")].map((x) => new Headers({ "cache-control": "public, s-maxage=60", "cf-cache-status": x.get("cf-cache-status")! })); | |
| 178 | let i = 0; | |
| 179 | const flap = edgeDuty(async () => heads[i++ % 2]!, () => false); | |
| 180 | await flap.run({ db: app.db, bus: app.bus, now: new Date(), app }); | |
| 181 | expect(state()).toBe("open"); | |
| 182 | }); | |
| 183 | ||
| 184 | test("the rule block's key list equals EDGE_VARY, and the open finding is found for the /maintenance link", async () => { | |
| 185 | expect(edgeRule()).toContain("respect origin Cache-Control"); | |
| 186 | const keys = edgeRule().split("\n").filter((l) => l.trim().startsWith("- ")).map((l) => l.trim().slice(2)); | |
| 187 | expect(keys.join(", ")).toBe(EDGE_VARY); | |
| 188 | const { app, once } = await run(shareable({ "cf-cache-status": "DYNAMIC" })); | |
| 189 | expect(openFinding(app.db)).toBeUndefined(); | |
| 190 | await once(); | |
| 191 | expect(openFinding(app.db)?.number).toBeGreaterThan(0); | |
| 192 | }); | |
| 193 | ||
| 194 | test("/maintenance and the terminal link the open finding, and stop once it is closed", async () => { | |
| 195 | const { app, once } = await run(shareable({ "cf-cache-status": "DYNAMIC" })); | |
| 196 | const servers = await serve(app); | |
| 197 | try { | |
| 198 | const res = await fetch(`${servers.url}/signin`, { | |
| 199 | method: "POST", redirect: "manual", | |
| 200 | headers: { "content-type": "application/x-www-form-urlencoded", origin: "https://forge.example" }, | |
| 201 | body: new URLSearchParams({ username: ownRepoPath().split("/")[0]!, password: "a-good-passphrase" }), | |
| 202 | }); | |
| 203 | const cookie = (res.headers.get("set-cookie") ?? "").split(";")[0] ?? ""; | |
| 204 | const page = async () => (await fetch(`${servers.url}/maintenance`, { headers: { cookie } })).text(); | |
| 205 | const user = getUserById(app.db, (app.db.query("SELECT id FROM user LIMIT 1").get() as { id: string }).id)!; | |
| 206 | const lines = () => dutyLines(new Session(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", user }), 0).join("\n"); | |
| 207 | expect(await page()).not.toContain("open finding"); | |
| 208 | expect(lines()).not.toContain("open finding"); | |
| 209 | await once(); | |
| 210 | const open = openFinding(app.db)!; | |
| 211 | const ref = `${open.repo}#${String(open.number)}`; | |
| 212 | expect(await page()).toContain(`<a href="/${open.repo}/issues/${String(open.number)}" data-filed="link">open finding ${ref}</a>`); | |
| 213 | expect(lines()).toContain(`; open finding ${ref}`); | |
| 214 | app.db.query("UPDATE issue SET state = 'closed'").run(); | |
| 215 | expect(await page()).not.toContain("open finding"); | |
| 216 | expect(lines()).not.toContain("open finding"); | |
| 217 | } finally { | |
| 218 | servers.stop(); | |
| 219 | } | |
| 220 | }); |
new above
offline
This repeats the open-finding query that
openFindingin src/maintain/edge_kept.ts runs. Keep one helper and call it from both, so there is only one way to find the finding.