feat(admin): finish the review and edit screens, and test what a browser can't

/admin had four API endpoints with no UI at all and a set of controls that
lied about what they would do. This closes both.

## Views that did not exist

/api/admin/audit, /api/admin/health and /api/admin/me had no consumer, and
/api/admin/stats returned commit_jobs counts the page fetched and discarded -
so the one table that records a commit attempt BEFORE it is made, the evidence
that a commit died mid-flight, was only readable through `wrangler d1
execute`. Added a view switcher with Queue / Commit jobs / Audit log / Health,
plus a /api/admin/jobs endpoint to back it.

## Controls that lied

- Approve and Reject stayed enabled on the approved/committed/rejected tabs,
  where DECIDABLE guards both the server-side read and every UPDATE, so the
  click always returned 409 with no hint why. Now disabled, with the bar
  saying those rows are already decided.
- Reconcile had no busy state, so a double-click fired two ~6MB runs.
- Retire (edit page) had no guard at all, and the page had no busy flag: tab
  switching, Discard, Load and the genre picker all stayed live mid-commit.
- Retiring a field with no override is a server-side no-op, and the page still
  said "Retired genre". It now says nothing was committed.
- setMode() never cleared the other mode's state, so One -> Bulk -> One left
  `dirty` populated while the counter reported bulk.sel.size - the count and
  the payload disagreed.
- loadGenres() swallowed every error into an empty list with no message, so a
  failure left both pickers empty and bulk mode silently inert.
- No beforeunload guard: an unsaved edit vanished on navigate.

## Search was a lie too

The filter box filtered the 60 loaded rows, so an appid on page 3 reported
"Nothing on this page matches that filter" - indistinguishable from "not in
the queue". Search is now server-side, with LIKE ... ESCAPE so a name
containing % or _ is matched literally. Verified against a local D1: the
escaped form matches 1 row where the unescaped form matches 2.

Also added sort (newest/oldest/players/reviews/name) as a closed set mapped to
fixed SQL, and rendered reviews_pct and current_players_num - both selected by
the API since the schema was written, with a comment saying they exist "so the
review screen can sort", and never once displayed. NULLS LAST verified in D1.
payload_json, stored verbatim on every insert and never surfaced, now has a
details dialog. MAX_DECIDE comes from the API instead of a client literal.

## The "Session expired" lie, and the third console error

Both pages treated ANY non-JSON response as an expired session. Combined with
the Worker having had no top-level try/catch, that meant every server crash
was reported to the reviewer as a login problem. They now distinguish the
cases, and use `redirect: "manual"` so an expired Access session is OBSERVED
as a redirect rather than followed to cloudflareaccess.com - which is exactly
what produced the reported

  Connecting to 'https://polished-hall-48fd.cloudflareaccess.com/cdn-cgi/...'

console error: the CSP is connect-src 'self', so the cross-origin hop from
fetch() was blocked. Re-auth is now a full-page reload, which is the only way
Access can run its login flow.

## Tests

/admin cannot be loaded locally - it is behind Access and there is no way to
mint a JWT offline - so admin-ui.test.ts asserts everything checkable without
a browser: the nonce appears in both the header and the script tag and differs
per response, the actor is escaped (a hostile actor string does not survive as
markup), and crucially the emitted inline script PARSES. That last one is not
theoretical: the page lives inside a TypeScript template literal, so a
backtick in a comment silently ends it and a literal newline inside a JS
string is a parse error tsc cannot see. Both mistakes were made while writing
this change and caught by these tests; mutation-checked afterwards by
re-introducing each.

39 tests, up from 19.

## docs/ADMIN.md

Four corrections, all places the doc described something that does not exist:
the edit-drafts table (no such table - also fixed in wrangler.jsonc), the
`f2p-discovery` service-token NAME check (impossible - Cloudflare reports the
opaque Client ID - and it contradicted the accurate account earlier in the
same file), the parity test (now real, so it says what it actually is), and
"what is not built yet", which listed only audit pruning and now lists the
Worker's inability to delete an override file, single-field bulk mode,
selection loss across pages, un-rejecting being raw SQL, and the absence of a
cross-isolate reconcile lock.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5 committed 2026-09-12 12:37:37 +07:00
1 parent 43623e79d2
commit 290b1ad715
6 files changed
+740 -107

No files matched your search

+49 -15
View File
@@ -139,8 +139,10 @@ D1, remains the source of truth.
## 6. The database
`f2p-admin` (D1, region APAC) already exists and is bound as `DB` in
`web/wrangler.jsonc`. It holds the new-games queue, edit drafts and an audit
log — never game records. Migrations live in `web/worker/migrations/` and are
`web/wrangler.jsonc`. It holds four tables — `ingest_queue`, `ingest_decisions`, `commit_jobs`
and `audit_log` — and never game records. (This used to say "edit drafts";
there is no such table. `/api/admin/edit` commits straight to Git with no
draft stage.) Migrations live in `web/worker/migrations/` and are
applied out of band:
```bash
@@ -273,10 +275,20 @@ token buys an attacker a cluttered review screen. The workflows reinforce this
with `permissions: contents: read` and a grep step that fails the run if a
discovery script so much as references a dataset-writing helper.
The two credentials are also pinned apart: `verifyAccessJwt` accepts any
configured AUD, so the Worker additionally checks that `/api/ingest/*` is a
service token named `f2p-discovery` and that `/api/admin/*` is not. Without
that pinning, either credential would satisfy the other's routes.
The two credentials are pinned apart in two independent ways, and neither is
a name check — Cloudflare reports a service token's identity as its opaque
Client ID, not its friendly name, so a name check is not possible:
1. **Per-route AUD.** `verifyAccessJwt` is called with only the AUD group a
route accepts (`ACCESS_AUD_INGEST` or `ACCESS_AUD_ADMIN`), never a pooled
list, so a token minted for one application cannot satisfy the other.
2. **Credential class.** `worker/index.ts` additionally refuses a service
token on the admin surface and a human session on ingest
(`isIngestApi !== who.isServiceToken`).
An earlier version of this paragraph claimed a `f2p-discovery` name check
and a pooled AUD list. Both were wrong, and it contradicted the accurate
account higher up this page.
## 8. Working through the backlog
@@ -469,9 +481,15 @@ re-added. Your text is combined with them rather than replacing them.
### The same thing from `/admin/edit`
`/admin/edit` (linked from the review queue) does exactly what the command
above does, and writes a byte-identical file — that equality is asserted by a
parity test, because a formatting difference between the two producers would
turn every alternating edit into a whole-file diff.
above does, and writes a byte-identical file. A formatting difference between
the two producers would turn every alternating edit into a whole-file diff, so
the equality is asserted by `web/worker/lib/override-doc.test.ts`, which
round-trips every committed `data/overrides/*.json` — all Python-written —
through the TypeScript serialiser and compares bytes. It runs in `web-ci.yml`
and in `check-overrides.yml`.
(Until 2026-09-12 this paragraph asserted that test existed. It did not; there
were no tests in the repository at all.)
Load a game by appid, change the fields you want, add a reason, Save. A field
already carrying an override is marked, shows the value from before the edit,
@@ -495,10 +513,26 @@ Two safeguards worth knowing:
## What is not built yet
`audit_log` pruning — see below. Otherwise the loop is closed: new games
arrive by discovery and are approved in `/admin`, corrections are made in
`/admin/edit` or from the command line, and both land in Git.
The loop is closed: new games arrive by discovery and are approved in
`/admin`, corrections are made in `/admin/edit` or from the command line, and
both land in Git. What remains:
`audit_log` has no pruning job yet. It grows only with admin actions, so it is
not urgent, but it is unbounded — decide a retention window and make it agree
with `docs/PRIVACY_POLICY.md` before that matters.
- **`audit_log` pruning.** It grows only with admin actions, so it is not
urgent, but it is unbounded — decide a retention window and make it agree
with `docs/PRIVACY_POLICY.md` before that matters.
- **The Worker cannot DELETE an override file.** `scripts/edit_game.py`
removes it once the last entry is retired; the Worker's `build()` returns
`null` ("leave the file alone") in the same state, and `createCommitOnBranch`
is only ever given `fileChanges.additions`. So `/admin/edit` leaves an empty
override document where the CLI would tidy it away. Harmless — an empty
document applies nothing — but the two tools diverge here.
- **Bulk mode sets one field.** `set: { genre }` is hardcoded; there is no
bulk retire and no multi-field bulk set.
- **Bulk selection does not survive pagination** (`loadBulk()` clears it).
- **Un-rejecting is raw SQL.** Removing a row from `ingest_decisions` has no
endpoint and writes no audit row, and there is no `suppress_until`, so a
rejection is permanent until someone runs `wrangler d1 execute`.
- **No cross-isolate reconcile lock.** The in-flight guard in
`lib/reconcile.ts` covers one isolate; the cron and a manual click in
different isolates can still overlap. Left alone deliberately — every write
there is idempotent, so the only cost is duplicated fetching.
+149
View File
@@ -0,0 +1,149 @@
/**
* The two Worker-rendered admin pages.
*
* These cannot be exercised by loading them: `/admin` is behind Cloudflare
* Access, so a local `wrangler dev` answers 404 and there is no way to obtain a
* valid Access JWT offline. Everything that CAN be checked without a browser is
* checked here instead — which is more than it sounds, because the pages are
* one big string and the two ways they break are a template-literal syntax
* error and an escaping mistake.
*
* The syntax case is not hypothetical: the whole document lives inside a
* TypeScript template literal, so a single backtick in a comment silently ends
* it, and a literal newline inside a JS string is a parse error the TypeScript
* compiler cannot see because to it the whole thing is just text.
*/
import { describe, it, expect } from "vitest";
import { adminPage } from "./admin-ui";
import { editPage } from "./edit-ui";
const BACKTICK = String.fromCharCode(96);
async function render(page: (actor: string) => Response, actor = "reviewer@example.com") {
const res = page(actor);
return { res, html: await res.text() };
}
/** Pull the inline script out of the rendered page. */
function inlineScript(html: string): string {
const m = /<script nonce="[a-f0-9]+">([\s\S]*?)<\/script>/.exec(html);
expect(m, "no nonced <script> found in the rendered page").not.toBeNull();
return m![1];
}
for (const [name, page] of [
["admin-ui", adminPage],
["edit-ui", editPage],
] as const) {
describe(name, () => {
it("renders an HTML document with a nonce CSP", async () => {
const { res, html } = await render(page);
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toContain("text/html");
// Per-identity and mutable: it must never sit in a cache.
expect(res.headers.get("Cache-Control")).toContain("no-store");
expect(html.startsWith("<!doctype html>")).toBe(true);
const csp = res.headers.get("Content-Security-Policy") ?? "";
expect(csp).toContain("default-src 'none'");
expect(csp).toContain("object-src 'none'");
expect(csp).toContain("frame-ancestors 'none'");
// 'unsafe-inline' on script-src would defeat the nonce entirely, on the
// one origin that holds a repository-write credential.
expect(csp).not.toContain("script-src 'unsafe-inline'");
});
it("uses the same nonce in the header and on the script tag", async () => {
const { res, html } = await render(page);
const nonce = /'nonce-([a-f0-9]+)'/.exec(res.headers.get("Content-Security-Policy") ?? "")?.[1];
expect(nonce, "no nonce in the CSP").toBeTruthy();
expect(html).toContain(`<script nonce="${nonce}">`);
});
it("issues a fresh nonce per response", async () => {
const a = page("a@example.com").headers.get("Content-Security-Policy");
const b = page("a@example.com").headers.get("Content-Security-Policy");
expect(a).not.toBe(b);
});
it("escapes the actor rather than interpolating it raw", async () => {
const { html } = await render(page, '"><img src=x onerror=alert(1)>');
expect(html).not.toContain("<img src=x onerror=alert(1)>");
expect(html).toContain("&lt;img src=x onerror=alert(1)&gt;");
});
// The failure mode this file exists for. A backtick or a literal newline
// inside a string is invisible to tsc — the page is just text to it — and
// ships a blank admin screen.
it("emits an inline script that actually parses", async () => {
const { html } = await render(page);
const js = inlineScript(html);
expect(js.length).toBeGreaterThan(500);
expect(() => new Function(js)).not.toThrow();
});
it("contains no backtick, which would end the outer template literal", async () => {
const { html } = await render(page);
expect(html.includes(BACKTICK)).toBe(false);
});
});
}
describe("admin-ui specifics", () => {
it("offers the views that previously had no UI at all", async () => {
const { html } = await render(adminPage);
// /api/admin/audit, /api/admin/health and commit_jobs existed as endpoints
// with no consumer; stats returned commit counts the page discarded.
for (const label of ["Commit jobs", "Audit log", "Health"]) {
expect(html).toContain(label);
}
});
it("knows which statuses are decidable", async () => {
const js = inlineScript((await render(adminPage)).html);
// Must mirror DECIDABLE in routes/admin.ts. If these drift, the buttons are
// enabled on tabs where every action returns 409.
expect(js).toContain('const DECIDABLE = ["pending","deferred","failed"]');
});
it("observes an Access redirect instead of following it", async () => {
const js = inlineScript((await render(adminPage)).html);
// Following the 302 from fetch() is what produced the
// cloudflareaccess.com connect-src console error.
expect(js).toContain('redirect: "manual"');
expect(js).toContain("SESSION_EXPIRED");
});
it("no longer claims every non-JSON response is an expired session", async () => {
const js = inlineScript((await render(adminPage)).html);
expect(js).not.toContain("Session expired or Access refused the request");
});
});
describe("edit-ui specifics", () => {
it("has a single page-wide busy flag", async () => {
const js = inlineScript(await editPage("a@b.c").text());
// There was none: save/saveBulk toggled only the Save button, retire()
// toggled nothing, so Retire could be double-clicked into two concurrent
// commits and tab switching stayed live mid-commit.
expect(js).toContain("function setBusy(");
expect(js).toContain("if (busy) return;");
});
it("warns before discarding an unsaved edit", async () => {
const js = inlineScript(await editPage("a@b.c").text());
expect(js).toContain("beforeunload");
});
it("clears the other mode's pending state when switching tabs", async () => {
const js = inlineScript(await editPage("a@b.c").text());
// One -> Bulk -> One used to leave `dirty` populated while recount()
// reported bulk.sel.size.
expect(js).toMatch(/if \(m !== mode\) \{[\s\S]*?dirty = \{\};[\s\S]*?bulk\.sel\.clear\(\);/);
});
it("does not report a no-op retire as success", async () => {
const js = inlineScript(await editPage("a@b.c").text());
expect(js).toContain("to retire - nothing was committed");
});
});
+360 -67
View File
@@ -96,37 +96,75 @@ input[type=text], input[type=search] { font: inherit; font-size: 13px; padding:
.empty { color: var(--muted); padding: 40px 0; text-align: center; }
.pager { display: flex; gap: 10px; align-items: center; justify-content: center;
margin-top: 20px; color: var(--muted); font-size: 13px; }
select { font: inherit; font-size: 13px; padding: 6px 9px; border-radius: 7px;
border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.note { color: var(--muted); font-size: 12px; }
/* reject_reason is unbounded in the schema, so an essay-length one used to
stretch the card instead of truncating. The full text is in the title. */
.tag { max-width: 190px; overflow: hidden; text-overflow: ellipsis;
white-space: nowrap; vertical-align: bottom; margin-left: 8px; }
.linky { background: none; border: 0; color: var(--accent); cursor: pointer;
font: inherit; font-size: 12px; padding: 0 0 0 8px; text-decoration: underline; }
/* The three read-only views (commit jobs, audit log, health). */
.tbl { width: 100%; border-collapse: collapse; font-size: 12.5px;
table-layout: fixed; }
.tbl th, .tbl td { text-align: left; padding: 6px 9px; border-bottom: 1px solid var(--border);
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tbl th { color: var(--muted); font-weight: 600; background: var(--surface); }
.tbl tr:hover td { background: var(--surface-2); }
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border);
border-radius: 10px; padding: 16px; max-width: min(760px, 92vw); width: 760px; }
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog pre { margin: 0; max-height: 60vh; overflow: auto; font-size: 12px;
background: var(--bg); border: 1px solid var(--border); border-radius: 7px; padding: 10px;
white-space: pre-wrap; word-break: break-word; }
</style>
</head>
<body>
<header>
<div class="row">
<h1>Review queue</h1>
<input type="search" id="q" placeholder="Filter this page by name or appid" style="width:270px">
<input type="search" id="q" placeholder="Search every row by name or appid" style="width:260px">
<select id="sort" title="Sort order">
<option value="newest">Newest first</option>
<option value="oldest">Oldest first</option>
<option value="players">Most players</option>
<option value="reviews">Best reviews</option>
<option value="name">Name A-Z</option>
</select>
<button class="act" id="reconcile" title="Check whether approved games have appeared in data/">Reconcile</button>
<a class="act" href="/admin/edit" style="text-decoration:none" title="Correct a game already in the catalogue">Edit a game</a>
<span class="who">${esc(actor)}</span>
</div>
<nav id="views"></nav>
<nav id="tabs"></nav>
</header>
<main>
<div id="msg"></div>
<div class="row" style="margin-bottom:12px">
<label class="row" style="gap:7px;cursor:pointer">
<input type="checkbox" id="all" style="width:17px;height:17px;accent-color:var(--accent)">
<span style="font-size:13px;color:var(--muted)">Select all on this page</span>
</label>
<span id="shown" style="font-size:13px;color:var(--muted);margin-left:auto"></span>
</div>
<div class="grid" id="grid"></div>
<div class="pager" id="pager"></div>
<section id="queueView">
<div class="row" style="margin-bottom:12px">
<label class="row" style="gap:7px;cursor:pointer">
<input type="checkbox" id="all" style="width:17px;height:17px;accent-color:var(--accent)">
<span style="font-size:13px;color:var(--muted)">Select all on this page</span>
</label>
<span id="shown" style="font-size:13px;color:var(--muted);margin-left:auto"></span>
</div>
<div class="grid" id="grid"></div>
<div class="pager" id="pager"></div>
</section>
<section id="tableView" hidden>
<div id="tableWrap"></div>
</section>
</main>
<div class="bar" id="bar">
<div class="row">
<strong id="count">0 selected</strong>
<input type="text" id="reason" placeholder="Reason (kept with a rejection)" style="width:290px">
<span id="barNote" class="note"></span>
<span style="margin-left:auto"></span>
<button class="act" id="defer">Defer</button>
<button class="act no" id="reject">Reject</button>
@@ -134,11 +172,35 @@ input[type=text], input[type=search] { font: inherit; font-size: 13px; padding:
</div>
</div>
<dialog id="detail">
<div class="row" style="margin-bottom:10px">
<strong id="detailTitle">Candidate</strong>
<button class="act" id="detailClose" style="margin-left:auto">Close</button>
</div>
<pre id="detailBody"></pre>
</dialog>
<script nonce="${nonce}">
"use strict";
const PAGE = 60;
const TABS = ["pending","deferred","approved","committed","failed","rejected"];
let status = "pending", offset = 0, items = [], sel = new Set(), busy = false;
// Mirrors DECIDABLE in routes/admin.ts. Selecting rows on any other tab and
// pressing Approve or Reject always returned 409 "no decidable rows in that
// selection", because the server guards both the read and every UPDATE with
// this set - but the buttons stayed enabled and gave no hint why.
const DECIDABLE = ["pending","deferred","failed"];
const VIEWS = [
{ id: "queue", label: "Queue" },
{ id: "jobs", label: "Commit jobs" },
{ id: "audit", label: "Audit log" },
{ id: "health", label: "Health" },
];
let view = "queue";
let status = "pending", offset = 0, items = [], sel = new Set();
let busy = false, reconciling = false;
let sortKey = "newest", query = "";
let maxDecide = 100;
// Click handlers call load() without awaiting it, so two loads can be in
// flight at once. Whoever responds LAST would otherwise win, which on a slow
// connection means clicking pending then committed can leave pending's rows
@@ -146,6 +208,11 @@ let status = "pending", offset = 0, items = [], sel = new Set(), busy = false;
let seq = 0;
const $ = (id) => document.getElementById(id);
// String.fromCharCode(10) rather than an escape: this whole script lives
// inside a TypeScript template literal, where a backslash escape has to be
// doubled and is easy to get silently wrong.
const NL = String.fromCharCode(10);
const fmt = (n) => (n === null || n === undefined ? "" : Number(n).toLocaleString("en-US"));
function say(text, kind) {
$("msg").innerHTML = "";
@@ -156,33 +223,86 @@ function say(text, kind) {
$("msg").appendChild(d);
}
/**
* One fetch helper for the whole page.
*
* The old version treated ANY non-JSON response as an expired session. Since
* the Worker had no top-level try/catch, every server-side crash arrived as a
* Cloudflare 1101 HTML page and was therefore reported as "Session expired" -
* the most misleading diagnostic in this screen, and it hid real D1 and GitHub
* failures for as long as it existed.
*
* Two things fixed it. The Worker now always answers JSON, and this function
* distinguishes the cases: redirect:'manual' means an expired Access
* session is OBSERVED as a redirect rather than followed to
* cloudflareaccess.com - which the page CSP blocks as a connect-src violation,
* the third of the reported console errors.
*/
async function api(path, init) {
const res = await fetch("/api/admin/" + path, {
credentials: "same-origin",
headers: { "Accept": "application/json", ...(init && init.body ? { "Content-Type": "application/json" } : {}) },
...init,
});
let res;
try {
res = await fetch("/api/admin/" + path, {
credentials: "same-origin",
redirect: "manual",
headers: {
"Accept": "application/json",
...(init && init.body ? { "Content-Type": "application/json" } : {}),
},
...init,
});
} catch (e) {
throw new Error("Network error reaching the admin API. Check your connection, then reload.");
}
// An opaque redirect is Access sending us to the login page. Following it
// from fetch() is what produced the cloudflareaccess.com CSP error; a
// full-page navigation is the only way to re-authenticate interactively.
if (res.type === "opaqueredirect" || res.status === 0 || res.status === 401 || res.status === 403) {
throw new Error("SESSION_EXPIRED");
}
const ct = res.headers.get("Content-Type") || "";
if (!ct.includes("application/json")) {
// Access sessions expire. Without this the page would report a JSON parse
// error when the real answer is "log in again".
throw new Error("Session expired or Access refused the request. Reload the page.");
throw new Error("Server returned " + res.status + " and a non-JSON body. Check wrangler tail.");
}
const body = await res.json();
if (!res.ok) throw new Error(body.error || ("HTTP " + res.status));
return body;
}
function reportError(e) {
const m = String((e && e.message) || e);
if (m === "SESSION_EXPIRED") {
say("Your Cloudflare Access session expired. Reloading to sign in again...", "err");
// Full-page navigation, not fetch: Access must be able to redirect the
// document to its login flow.
setTimeout(() => window.location.reload(), 1200);
return;
}
say(m, "err");
}
function renderViews() {
$("views").innerHTML = "";
for (const v of VIEWS) {
const b = document.createElement("button");
b.textContent = v.label;
if (v.id === view) b.setAttribute("aria-current", "true");
b.addEventListener("click", () => { view = v.id; sel.clear(); load(); });
$("views").appendChild(b);
}
}
async function loadStats() {
const mine = seq;
let s = { queue: {} };
let s = { queue: {}, commits: {} };
try { s = await api("stats"); } catch (e) { /* tabs still render with no counts */ }
if (mine !== seq) return;
$("tabs").innerHTML = "";
for (const t of TABS) {
const b = document.createElement("button");
b.textContent = t;
const n = s.queue[t] || 0;
const n = (s.queue || {})[t] || 0;
const c = document.createElement("span");
c.className = "n";
c.textContent = n;
@@ -228,7 +348,15 @@ function card(it) {
const sub = document.createElement("div");
sub.className = "sub";
sub.textContent = [it.release_date, it.app_type, it.source].filter(Boolean).join(" · ");
// reviews_pct and current_players_num have been selected by the API since
// the schema was written and were never rendered, so the reviewer had no
// signal at all about whether a candidate was worth publishing.
const bits = [it.release_date, it.app_type, it.source];
if (it.reviews_pct !== null && it.reviews_pct !== undefined) bits.push(it.reviews_pct + "%");
if (it.current_players_num !== null && it.current_players_num !== undefined) {
bits.push(fmt(it.current_players_num) + " playing");
}
sub.textContent = bits.filter(Boolean).join(" · ");
meta.appendChild(sub);
const line = document.createElement("div");
@@ -239,18 +367,22 @@ function card(it) {
a.rel = "noopener noreferrer";
a.textContent = it.appid;
line.appendChild(a);
if (it.reject_reason) {
const why = document.createElement("button");
why.className = "linky";
why.textContent = "details";
why.title = "The full candidate as it was proposed";
why.addEventListener("click", () => showCandidate(it.id, it.name));
line.appendChild(why);
for (const [val, title] of [[it.reject_reason, "reason"], [it.decided_by, "decided by"]]) {
if (!val) continue;
const t = document.createElement("span");
t.className = "tag";
t.style.marginLeft = "8px";
t.textContent = it.reject_reason;
line.appendChild(t);
}
if (it.decided_by) {
const t = document.createElement("span");
t.className = "tag";
t.style.marginLeft = "8px";
t.textContent = it.decided_by;
// title as well as text: reject_reason is unbounded in the schema, so a
// long one used to stretch the card instead of truncating.
t.title = title + ": " + val;
t.textContent = val;
line.appendChild(t);
}
meta.appendChild(line);
@@ -259,16 +391,26 @@ function card(it) {
return el;
}
async function showCandidate(id, name) {
try {
const d = await api("candidate?id=" + encodeURIComponent(id));
$("detailTitle").textContent = (name || "Candidate") + " - " + d.appid;
let payload = d.payload_json;
try { payload = JSON.stringify(JSON.parse(d.payload_json), null, 2); } catch (e) { /* show raw */ }
$("detailBody").textContent =
"seen " + d.seen_count + "x, first " + d.first_seen_at + ", last " + d.last_seen_at + NL +
"health " + d.health_status + ", type " + d.app_type + ", free " + d.is_free +
", source " + d.source + NL + NL + payload;
$("detail").showModal();
} catch (e) { reportError(e); }
}
// The rows actually on screen. render() and the select-all box MUST agree on
// this: the box used to iterate the unfiltered page, so filtering to one game
// and ticking "select all" selected all 60 loaded rows while showing one card
// - and the next click approved 59 games the reviewer never saw.
function visible() {
const q = $("q").value.trim().toLowerCase();
return q
? items.filter((i) => (i.name || "").toLowerCase().includes(q) || String(i.appid).includes(q))
: items;
}
// - and the next click approved 59 games the reviewer never saw. Filtering is
// now done by the server, so this is simply the loaded page.
function visible() { return items; }
function render() {
const show = visible();
@@ -277,16 +419,12 @@ function render() {
if (!show.length) {
const d = document.createElement("div");
d.className = "empty";
d.textContent = items.length ? "Nothing on this page matches that filter." : "Nothing here.";
d.textContent = query
? "No row in '" + status + "' matches " + JSON.stringify(query) + "."
: "Nothing here.";
$("grid").appendChild(d);
}
for (const it of show) $("grid").appendChild(card(it));
$("shown").textContent = show.length === items.length
? show.length + " shown"
: show.length + " of " + items.length + " shown";
// Keep the master box honest. After a decision clears the selection it would
// otherwise stay ticked while nothing is selected, so the next click would
// untick it, select nothing, and need a second click to do anything.
$("all").checked = show.length > 0 && show.every((i) => sel.has(i.id));
paintBar();
}
@@ -294,13 +432,16 @@ function render() {
function paintBar() {
$("bar").classList.toggle("on", sel.size > 0);
$("count").textContent = sel.size + " selected";
$("approve").disabled = busy;
$("reject").disabled = busy;
// On the 'failed' tab this button re-opens a row the pipeline bounced, which
// is the opposite of deferring - so it must not keep saying "Defer".
const decidable = DECIDABLE.includes(status);
const requeueing = status === "failed";
$("approve").disabled = busy || !decidable;
$("reject").disabled = busy || !decidable;
$("defer").textContent = requeueing ? "Send back to pending" : "Defer";
$("defer").disabled = busy || (!requeueing && status === "deferred");
$("defer").disabled = busy || !decidable || (!requeueing && status === "deferred");
$("barNote").textContent = decidable
? (sel.size > maxDecide ? "Over the server's limit of " + maxDecide : "")
: "Rows in '" + status + "' are already decided - nothing to do here.";
}
// keepMsg: a decision leaves a result worth reading ("commit abc1234 - 60
@@ -309,21 +450,144 @@ function paintBar() {
async function load(keepMsg) {
if (!keepMsg) say("");
const mine = ++seq;
renderViews();
const isQueue = view === "queue";
$("queueView").hidden = !isQueue;
$("tableView").hidden = isQueue;
$("tabs").hidden = !isQueue;
$("q").hidden = !isQueue;
$("sort").hidden = !isQueue;
if (!isQueue) { sel.clear(); paintBar(); }
try {
if (!isQueue) { await loadTable(mine); return; }
const d = await api("queue?status=" + encodeURIComponent(status) +
"&limit=" + PAGE + "&offset=" + offset);
"&limit=" + PAGE + "&offset=" + offset +
"&sort=" + encodeURIComponent(sortKey) +
(query ? "&q=" + encodeURIComponent(query) : ""));
if (mine !== seq) return;
items = d.items || [];
if (typeof d.maxDecide === "number") maxDecide = d.maxDecide;
renderPager(d.total || 0);
$("shown").textContent = (d.total || 0) + " row(s)" + (query ? " matching" : "");
render();
await loadStats();
} catch (e) {
// A superseded request that fails is not worth reporting: the banner would
// describe a view the reviewer already navigated away from.
if (mine === seq) say(String(e.message || e), "err");
if (mine === seq) reportError(e);
}
}
/** Simple read-only tables for the three views that previously had no UI. */
async function loadTable(mine) {
const wrap = $("tableWrap");
wrap.innerHTML = "";
const loading = document.createElement("div");
loading.className = "empty";
loading.textContent = "Loading...";
wrap.appendChild(loading);
if (view === "health") {
const h = await api("health");
if (mine !== seq) return;
wrap.innerHTML = "";
wrap.appendChild(kv([
["overall", h.ok ? "ok" : "PROBLEM"],
["D1", h.d1],
["GitHub App", h.github],
["actor", h.actor || ""],
]));
const note = document.createElement("p");
note.className = "note";
note.textContent = "d1/github report 'ok' or 'error'. The message stays in Workers Logs on purpose - run 'wrangler tail' for the detail.";
wrap.appendChild(note);
const q = document.createElement("div");
q.className = "row";
q.style.marginTop = "14px";
for (const [k, v] of Object.entries(h.queue || {})) {
const t = document.createElement("span");
t.className = "tag";
t.textContent = k + ": " + v;
q.appendChild(t);
}
wrap.appendChild(q);
return;
}
if (view === "audit") {
const d = await api("audit?limit=200");
if (mine !== seq) return;
wrap.innerHTML = "";
wrap.appendChild(table(
["when", "actor", "action", "target", "detail"],
(d.items || []).map((r) => [r.created_at, r.actor, r.action, r.target || "", r.detail_json || ""]),
"No admin actions recorded yet.",
));
return;
}
// jobs
const d = await api("jobs?limit=200");
if (mine !== seq) return;
wrap.innerHTML = "";
wrap.appendChild(table(
["created", "kind", "status", "target", "commit", "error", "finished"],
(d.items || []).map((r) => [
r.created_at, r.kind, r.status, r.target_path,
r.commit_sha ? String(r.commit_sha).slice(0, 10) : "",
r.error || "", r.finished_at || "",
]),
"No commit jobs yet. A row is written here BEFORE each approval commit is attempted, so a 'pending' row that never finished is the evidence a commit died mid-flight.",
));
}
function kv(pairs) {
const t = document.createElement("table");
t.className = "tbl";
for (const [k, v] of pairs) {
const tr = document.createElement("tr");
const th = document.createElement("th");
th.textContent = k;
const td = document.createElement("td");
td.textContent = String(v);
tr.appendChild(th); tr.appendChild(td);
t.appendChild(tr);
}
return t;
}
function table(headers, rows, emptyText) {
if (!rows.length) {
const d = document.createElement("div");
d.className = "empty";
d.textContent = emptyText;
return d;
}
const t = document.createElement("table");
t.className = "tbl";
const hr = document.createElement("tr");
for (const h of headers) {
const th = document.createElement("th");
th.textContent = h;
hr.appendChild(th);
}
t.appendChild(hr);
for (const r of rows) {
const tr = document.createElement("tr");
for (const c of r) {
const td = document.createElement("td");
// textContent throughout: detail_json and error carry server text, and
// reject_reason carries reviewer text.
td.textContent = String(c);
td.title = String(c);
tr.appendChild(td);
}
t.appendChild(tr);
}
return t;
}
function renderPager(total) {
$("pager").innerHTML = "";
if (total <= PAGE) return;
@@ -344,19 +608,19 @@ function renderPager(total) {
async function decide(action) {
if (!sel.size || busy) return;
if (!DECIDABLE.includes(status)) return;
const ids = [...sel];
if (ids.length > 100) {
say("Select at most 100 at a time — that is the server's batch limit.", "err");
if (ids.length > maxDecide) {
say("Select at most " + maxDecide + " at a time - that is the server's batch limit.", "err");
return;
}
if (action === "approve" &&
!confirm("Approve " + ids.length + " game(s)?\\n\\nThis commits them to " +
"scripts/temp_info.jsonl and starts the ingest workflow.")) return;
!confirm("Approve " + ids.length + " game(s)? This commits them to scripts/temp_info.jsonl and starts the ingest workflow.")) return;
if (action === "reject" &&
!confirm("Reject " + ids.length + " game(s)?\\n\\nThey will not be offered again by future sweeps.")) return;
!confirm("Reject " + ids.length + " game(s)? They will not be offered again by future sweeps.")) return;
busy = true; paintBar();
say("Working…");
say("Working...");
try {
const out = await api("decide", {
method: "POST",
@@ -364,22 +628,22 @@ async function decide(action) {
});
let m = action + ": " + out.decided + " row(s)";
if (out.commit) {
m += "\\ncommit " + out.commit.slice(0, 10) +
" — " + out.appended + " appended" +
m += NL + "commit " + out.commit.slice(0, 10) +
" - " + out.appended + " appended" +
(out.already_queued ? ", " + out.already_queued + " already queued" : "") +
"\\nIngest New Game Links will pick them up; they move to 'committed' once they appear in data/.";
NL + "Ingest New Game Links will pick them up; they move to 'committed' once they appear in data/.";
} else if (action === "approve") {
// commit === null means every link was already in the queue file, so no
// commit was made. Saying "committed" here would be a lie the reviewer
// could only catch by opening the repository.
m += "\\nAlready queued in scripts/temp_info.jsonl — no new commit was needed.";
m += NL + "Already queued in scripts/temp_info.jsonl - no new commit was needed.";
}
say(m, "ok");
sel.clear();
$("reason").value = "";
await load(true);
} catch (e) {
say(String(e.message || e), "err");
reportError(e);
} finally {
busy = false; paintBar();
}
@@ -388,14 +652,38 @@ async function decide(action) {
$("approve").addEventListener("click", () => decide("approve"));
$("reject").addEventListener("click", () => decide("reject"));
$("defer").addEventListener("click", () => decide(status === "failed" ? "requeue" : "defer"));
$("q").addEventListener("input", render);
$("detailClose").addEventListener("click", () => $("detail").close());
// Debounced, because every keystroke is now a server round-trip rather than an
// array filter.
let qTimer = 0;
$("q").addEventListener("input", () => {
clearTimeout(qTimer);
qTimer = setTimeout(() => {
query = $("q").value.trim();
offset = 0;
sel.clear();
load();
}, 250);
});
$("sort").addEventListener("change", () => {
sortKey = $("sort").value;
offset = 0;
sel.clear();
load();
});
$("all").addEventListener("change", () => {
const on = $("all").checked;
for (const it of visible()) { if (on) sel.add(it.id); else sel.delete(it.id); }
render();
});
$("reconcile").addEventListener("click", async () => {
say("Checking data/ for approved games…");
// A full run fetches index.json, every shard and removed_games.jsonl - about
// 6 MB. This button had no busy state at all, so a double-click fired two.
if (reconciling) return;
reconciling = true;
$("reconcile").disabled = true;
say("Checking data/ for approved games...");
try {
const out = await api("reconcile", { method: "POST" });
// "ok" was outside the ternary, so a hard dependency failure ("index
@@ -411,7 +699,12 @@ $("reconcile").addEventListener("click", async () => {
(out.stale ? ", " + out.stale + " aged out to failed - approve again to retry" : ""),
bad ? "err" : "ok");
await load(true);
} catch (e) { say(String(e.message || e), "err"); }
} catch (e) {
reportError(e);
} finally {
reconciling = false;
$("reconcile").disabled = false;
}
});
load();
+70 -7
View File
@@ -128,8 +128,34 @@ export async function handleAdminApi(
const limit = clampLimit(url.searchParams.get("limit"), 50, 200);
const offset = clampOffset(url.searchParams.get("offset"));
// ORDER BY first_seen_at DESC, id: first_seen_at alone is not unique -- a
// whole sweep shares one timestamp -- so paging by it would let rows swap
// Search is SERVER-side. The page used to filter only the 60 rows it had
// loaded, so typing an appid that sat on page 3 reported "Nothing on this
// page matches that filter" - indistinguishable from "not in the queue".
const q = (url.searchParams.get("q") ?? "").trim().slice(0, 80);
// LIKE with an escaped pattern, so a reviewer pasting a name containing %
// or _ searches for those characters rather than wildcards.
const like = q ? `%${q.replace(/[\\%_]/g, (c) => "\\" + c)}%` : null;
const where = like
? "status = ? AND (name LIKE ? ESCAPE '\\' OR appid LIKE ? ESCAPE '\\')"
: "status = ?";
const whereArgs: unknown[] = like ? [status, like, like] : [status];
// ORDER BY is a closed set mapped to fixed SQL, never interpolated from
// the caller. reviews_pct and current_players_num have been selected since
// the schema was written - the comment there says they exist "so the
// review screen can sort" - but nothing ever offered the sort.
const SORTS: Record<string, string> = {
newest: "first_seen_at DESC, id",
oldest: "first_seen_at ASC, id",
players: "current_players_num DESC NULLS LAST, id",
reviews: "reviews_pct DESC NULLS LAST, id",
name: "name COLLATE NOCASE ASC, id",
};
const sortKey = url.searchParams.get("sort") ?? "newest";
const orderBy = SORTS[sortKey] ?? SORTS.newest;
// first_seen_at alone is not unique -- a whole sweep shares one timestamp
// -- so every ordering above breaks the tie on id, or rows would swap
// places between pages and a game could be shown twice or never.
const rows = await env.DB.prepare(
`SELECT id, appid, link, name, header_image, release_date, app_type,
@@ -138,29 +164,50 @@ export async function handleAdminApi(
decided_by, decided_at, reject_reason,
first_seen_at, last_seen_at, seen_count
FROM ingest_queue
WHERE status = ?
ORDER BY first_seen_at DESC, id
WHERE ${where}
ORDER BY ${orderBy}
LIMIT ? OFFSET ?`,
)
.bind(status, limit, offset)
.bind(...whereArgs, limit, offset)
.all();
const total = await env.DB.prepare(
"SELECT COUNT(*) AS n FROM ingest_queue WHERE status = ?",
`SELECT COUNT(*) AS n FROM ingest_queue WHERE ${where}`,
)
.bind(status)
.bind(...whereArgs)
.first<{ n: number }>();
return json({
status,
offset,
limit,
q,
sort: sortKey in SORTS ? sortKey : "newest",
total: total?.n ?? 0,
count: rows.results?.length ?? 0,
items: rows.results ?? [],
// So the page stops hardcoding 100 as a client-side literal.
maxDecide: MAX_DECIDE,
});
}
// The full candidate as it arrived, for one row. payload_json is stored
// verbatim on every insert and was never surfaced anywhere, so the only way
// to see why a game was proposed was `wrangler d1 execute`.
if (route === "candidate" && request.method === "GET") {
const id = (url.searchParams.get("id") ?? "").slice(0, 64);
if (!id) return jsonError(400, "id is required");
const row = await env.DB.prepare(
`SELECT id, appid, name, payload_json, first_seen_at, last_seen_at, seen_count,
health_status, app_type, is_free, source
FROM ingest_queue WHERE id = ?`,
)
.bind(id)
.first();
if (!row) return jsonError(404, "not found");
return json(row);
}
// Recent admin actions.
if (route === "audit" && request.method === "GET") {
const limit = clampLimit(url.searchParams.get("limit"), 50, 200);
@@ -173,6 +220,22 @@ export async function handleAdminApi(
return json({ items: rows.results ?? [] });
}
// commit_jobs, which had no UI at all. /api/admin/stats already returned
// these counts and the page discarded them, so the one table that records a
// commit attempt BEFORE it is made - the evidence that a commit died
// mid-flight - was only readable through `wrangler d1 execute`.
if (route === "jobs" && request.method === "GET") {
const limit = clampLimit(url.searchParams.get("limit"), 50, 200);
const rows = await env.DB.prepare(
`SELECT id, kind, status, target_path, commit_sha, error,
requested_by, created_at, finished_at
FROM commit_jobs ORDER BY created_at DESC LIMIT ?`,
)
.bind(limit)
.all();
return json({ items: rows.results ?? [] });
}
if (route === "ping" && request.method === "POST") {
await audit(env, who.email, "ping", null, { at: new Date().toISOString() });
return json({ ok: true });
+110 -17
View File
@@ -202,21 +202,68 @@ function say(text, kind) {
$("msg").appendChild(d);
}
const NL = String.fromCharCode(10);
/**
* See the matching helper in admin-ui.ts. The old version reported EVERY
* non-JSON response as an expired session, so a server-side crash - which
* arrived as a Cloudflare 1101 HTML page, the Worker having had no top-level
* try/catch - was indistinguishable from needing to log in again.
*
* redirect:'manual' observes an Access redirect rather than following it to
* cloudflareaccess.com, which the page CSP blocks as a connect-src violation.
*/
async function api(path, init) {
const res = await fetch("/api/admin/" + path, {
credentials: "same-origin",
headers: { "Accept": "application/json", ...(init && init.body ? {"Content-Type":"application/json"} : {}) },
...init,
});
let res;
try {
res = await fetch("/api/admin/" + path, {
credentials: "same-origin",
redirect: "manual",
headers: { "Accept": "application/json", ...(init && init.body ? {"Content-Type":"application/json"} : {}) },
...init,
});
} catch (e) {
throw new Error("Network error reaching the admin API. Check your connection, then reload.");
}
if (res.type === "opaqueredirect" || res.status === 0 || res.status === 401 || res.status === 403) {
throw new Error("SESSION_EXPIRED");
}
const ct = res.headers.get("Content-Type") || "";
if (!ct.includes("application/json")) {
throw new Error("Session expired or Access refused the request. Reload the page.");
throw new Error("Server returned " + res.status + " and a non-JSON body. Check wrangler tail.");
}
const body = await res.json();
if (!res.ok) throw new Error(body.error || ("HTTP " + res.status));
return body;
}
function reportError(e) {
const m = String((e && e.message) || e);
if (m === "SESSION_EXPIRED") {
say("Your Cloudflare Access session expired. Reloading to sign in again...", "err");
setTimeout(() => window.location.reload(), 1200);
return;
}
say(m, "err");
}
/**
* One busy flag for the whole page.
*
* There was none. save() and saveBulk() toggled only $("save").disabled, and
* retire() did not even do that - so Retire could be double-clicked into two
* concurrent commits, and tab switching, Discard, Load and the genre picker
* all stayed live while a commit was in flight.
*/
let busy = false;
function setBusy(on) {
busy = on;
for (const id of ["save", "reset", "load", "tab-one", "tab-bulk", "bgenre", "bnew", "appid"]) {
const el = $(id);
if (el) el.disabled = on;
}
}
const toWire = (k, v) => k === "is_kernel_ac"
? (v === "true" ? true : v === "false" ? false : null) : v;
const toForm = (k, v) => k === "is_kernel_ac"
@@ -389,8 +436,9 @@ async function save() {
const set = {};
for (const [k, v] of Object.entries(dirty)) set[k] = toWire(k, v);
if (!Object.keys(set).length) return;
$("save").disabled = true;
say("Saving…");
if (busy) return;
setBusy(true);
say("Saving...");
try {
const out = await api("edit", {
method: "POST",
@@ -403,35 +451,61 @@ async function save() {
await load(data.appid, true);
await loadGenres();
} catch (e) {
say(String(e.message || e), "err");
reportError(e);
} finally {
$("save").disabled = false;
setBusy(false);
}
}
async function retire(field, ov) {
if (!confirm("Retire the override on " + field + "?\\n\\nThe value from before the edit (" +
JSON.stringify(ov.was) + ") is restored once, then the entry stops acting.")) return;
say("Retiring…");
if (busy) return;
setBusy(true);
say("Retiring...");
try {
const out = await api("edit", {
method: "POST",
body: JSON.stringify({ appids: [data.appid], retire: [field], reason: $("reason").value }),
});
say("Retired " + field + (out.commit ? "\\ncommit " + out.commit.slice(0, 10) : ""), "ok");
// Retiring a field that has no override is a no-op server-side: build()
// returns null, commitFiles() returns null, and the old message still read
// "Retired genre" - success wording for nothing having happened.
if (out.commit) {
say("Retired " + field + NL + "commit " + out.commit.slice(0, 10) +
NL + "The pre-edit value is written back on the next pipeline run.", "ok");
} else {
say("There was no override on " + field + " to retire - nothing was committed.", "err");
}
await load(data.appid, true);
} catch (e) {
say(String(e.message || e), "err");
reportError(e);
} finally {
setBusy(false);
}
}
// ─────────────────────────── bulk mode ───────────────────────────
/**
* The genre vocabulary, built server-side by regex-scanning ~6 MB of shards.
*
* This used to swallow every error into an empty list with no message, so a
* failure left both pickers empty, bulk mode silently doing nothing, and
* nothing on screen to say why.
*/
async function loadGenres() {
try {
const out = await api("genres");
genres = out.genres || [];
return true;
} catch (e) {
genres = [];
const m = String((e && e.message) || e);
say(m === "SESSION_EXPIRED"
? "Session expired while loading the genre list. Reload to sign in again."
: "Could not load the genre list (" + m + "). Bulk retag is unavailable until that works; one-game edits still are.",
"err");
return false;
}
}
@@ -570,8 +644,9 @@ async function saveBulk() {
" to " + JSON.stringify(target) +
"?\\n\\nOne commit, one override file per game.")) return;
$("save").disabled = true;
say("Saving…");
if (busy) return;
setBusy(true);
say("Saving...");
try {
const out = await api("edit", {
method: "POST",
@@ -586,14 +661,24 @@ async function saveBulk() {
(out.commit ? "\\ncommit " + out.commit.slice(0, 10) : "\\nno commit needed") +
"\\ndata/ is unchanged until the next pipeline run applies it.", "ok");
} catch (e) {
say(String(e.message || e), "err");
reportError(e);
} finally {
$("save").disabled = false;
setBusy(false);
}
}
// ─────────────────────────── wiring ───────────────────────────
function setMode(m) {
if (busy) return;
// dirty belongs to one-game mode and bulk.sel to bulk mode, and neither was
// cleared on a switch. One -> Bulk -> One left dirty populated while
// recount() reported bulk.sel.size, so the counter and what Save would
// actually send disagreed.
if (m !== mode) {
dirty = {};
bulk.sel.clear();
if (data) render();
}
mode = m;
$("tab-one").setAttribute("aria-current", String(m === "one"));
$("tab-bulk").setAttribute("aria-current", String(m === "bulk"));
@@ -629,6 +714,14 @@ $("bnew").addEventListener("change", () => {
const initial = new URLSearchParams(location.search).get("appid");
if (initial) { $("appid").value = initial; load(initial); }
})();
// An edit in progress was lost with no warning on navigate or close. The
// browser shows its own generic prompt; the text is not ours to choose.
window.addEventListener("beforeunload", (e) => {
const pending = mode === "one" ? Object.keys(dirty).length : bulk.sel.size;
if (!pending && !busy) return;
e.preventDefault();
e.returnValue = "";
});
</script>
</body>
</html>`;
+2 -1
View File
@@ -55,7 +55,8 @@
"head_sampling_rate": 1
},
// Admin-only state: the new-games queue, edit drafts, audit log. Game data
// Admin-only state: the new-games queue, decisions, commit jobs and the
// audit log. (Not "edit drafts" - there is no such table.) Game data
// stays canonical in Git — D1 never holds game records.
"d1_databases": [
{