Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

site: a Stable update feed, a release a week and an urgent one at once

yetone8f4469954e72
diff --git a/site/stable.test.mjs b/site/stable.test.mjs
new file mode 100644
index 000000000..5c88c8c9f
--- /dev/null
+++ b/site/stable.test.mjs
@@ -0,0 +1,369 @@
+// node --test site/: the Stable update channel (ssharpcc on X) is chosen
+// from the release ledger, a week at a time, an urgent release at once.
+// GitHub, the edge cache and KV are stood in for; every clock is held.
+import { test, beforeEach } from "node:test";
+import assert from "node:assert/strict";
+import worker, { stableOf, weekStart, urgentIn, keepLedger, URGENT_MARK } from "./worker.js";
+
+const DAY = 86400e3;
+const at = (iso) => Date.parse(iso);
+const iso = (ms) => new Date(ms).toISOString();
+
+test("weekStart: the Monday 00:00 UTC on or before", () => {
+ assert.equal(iso(weekStart(at("2026-10-28T12:00:00Z"))), "2026-10-26T00:00:00.000Z"); // a Wednesday
+ assert.equal(iso(weekStart(at("2026-10-26T00:00:00Z"))), "2026-10-26T00:00:00.000Z"); // the Monday itself
+ assert.equal(iso(weekStart(at("2026-11-01T23:59:59Z"))), "2026-10-26T00:00:00.000Z"); // the Sunday
+ assert.equal(iso(weekStart(at("2026-11-02T00:00:00Z"))), "2026-11-02T00:00:00.000Z");
+ // across a month and a year
+ assert.equal(iso(weekStart(at("2027-01-01T05:00:00Z"))), "2026-12-28T00:00:00.000Z");
+});
+
+test("urgentIn: a line starting with the marker, quoted or not, in any case", () => {
+ assert.equal(URGENT_MARK, "**Urgent release:**");
+ assert.ok(urgentIn("**Urgent release:** Claude sign-ins could be banned\n\n### Fixes\n- one"));
+ assert.ok(urgentIn("### Fixes\n- one\n\n> **Urgent release:** lost settings"));
+ assert.ok(urgentIn(" **URGENT RELEASE:** why"));
+ assert.ok(!urgentIn("- not an **Urgent release:** in a bullet's middle"));
+ assert.ok(!urgentIn("Urgent release: without the bold"));
+ assert.ok(!urgentIn(""));
+ assert.ok(!urgentIn(null));
+});
+
+// a Wednesday: this week began Monday 10-26, so its cut is Friday 10-23
+const WED = at("2026-10-28T12:00:00Z");
+const rel = (version, published, urgent = false) => ({ version, published, urgent });
+const LIST = [
+ rel("0.1.10", "2026-10-27T09:00:00Z"),
+ rel("0.1.9", "2026-10-23T00:00:00Z"), // at the cut itself
+ rel("0.1.8", "2026-10-20T08:00:00Z"),
+];
+
+test("stableOf: the newest release out three days before the week began", () => {
+ assert.equal(stableOf(LIST, WED).version, "0.1.9");
+ // a second after the cut is too new
+ const late = [rel("0.1.9", "2026-10-23T00:00:01Z"), rel("0.1.8", "2026-10-20T08:00:00Z")];
+ assert.equal(stableOf(late, WED).version, "0.1.8");
+ // the list's order doesn't matter
+ assert.equal(stableOf([...LIST].reverse(), WED).version, "0.1.9");
+});
+
+test("stableOf: held the whole week, and moves on as the next begins", () => {
+ for (const now of ["2026-10-26T00:00:00Z", "2026-10-29T00:00:00Z", "2026-11-01T23:59:59Z"])
+ assert.equal(stableOf(LIST, at(now)).version, "0.1.9", now);
+ // Monday 11-02: its cut is Friday 10-30, and 0.1.10 was out by then
+ assert.equal(stableOf(LIST, at("2026-11-02T00:00:00Z")).version, "0.1.10");
+ // a release out the day before the cut that week (10-29) is picked too
+ const more = [rel("0.1.12", "2026-10-31T00:00:00Z"), rel("0.1.11", "2026-10-29T00:00:00Z"), ...LIST];
+ assert.equal(stableOf(more, at("2026-11-04T00:00:00Z")).version, "0.1.11");
+});
+
+test("stableOf: an urgent release is Stable at once, until a weekly pick passes it", () => {
+ const urgent = [rel("0.1.11", "2026-10-28T10:00:00Z", true), ...LIST];
+ assert.equal(stableOf(urgent, WED).version, "0.1.11");
+ assert.equal(stableOf(urgent, WED).urgent, true);
+ // not before it is out
+ assert.equal(stableOf(urgent, at("2026-10-28T09:59:59Z")).version, "0.1.9");
+ // an urgent one older than the weekly pick changes nothing
+ const old = [rel("0.1.10", "2026-10-27T09:00:00Z"), rel("0.1.9", "2026-10-23T00:00:00Z"), rel("0.1.8", "2026-10-20T08:00:00Z", true)];
+ assert.equal(stableOf(old, WED).version, "0.1.9");
+ // releases after an urgent one wait for the week as any do
+ const after = [rel("0.1.12", "2026-10-28T11:00:00Z"), ...urgent];
+ assert.equal(stableOf(after, WED).version, "0.1.11");
+ // a release with no date isn't picked
+ assert.equal(stableOf([rel("0.1.13", null, true), ...LIST], WED).version, "0.1.9");
+});
+
+test("stableOf: unknown when the list doesn't reach back to the cut", () => {
+ const short = [rel("0.1.10", "2026-10-27T09:00:00Z"), rel("0.1.9", "2026-10-24T00:00:00Z")];
+ assert.equal(stableOf(short, WED), null);
+ assert.equal(stableOf([], WED), null);
+ // an urgent one is known all the same
+ assert.equal(stableOf([rel("0.1.11", "2026-10-28T10:00:00Z", true), ...short], WED).version, "0.1.11");
+});
+
+// --- the ledger, the cron and the feeds ---
+
+// kv stands in for the LEDGER namespace; writes counts its puts, broken
+// makes every read throw.
+function kv() {
+ const m = new Map();
+ const ns = {
+ m,
+ writes: 0,
+ broken: false,
+ async get(key, opts = {}) {
+ if (ns.broken) throw new Error("kv down");
+ const v = m.has(key) ? m.get(key) : null;
+ return v !== null && (opts.type === "json" || opts === "json") ? JSON.parse(v) : v;
+ },
+ async put(key, v) {
+ ns.writes++;
+ m.set(key, String(v));
+ },
+ };
+ return ns;
+}
+
+// GITHUB is the release list GitHub has, newest first; apiDown stands in
+// for its API limiting the worker, asks counts the API's asks.
+let GITHUB = [], apiDown = false, asks = 0;
+const store = new Map();
+globalThis.caches = {
+ default: {
+ match: async (req) => (store.has(req.url) ? new Response(store.get(req.url)) : undefined),
+ put: async (req, res) => void store.set(req.url, await res.text()),
+ },
+};
+const gh = (r) => ({ tag_name: "v" + r.version, body: r.notes, html_url: "https://github.com/r/v" + r.version, published_at: r.published, draft: false, prerelease: false, assets: [{ name: "magpie-linux-amd64", size: 7, browser_download_url: "https://dl/" + r.version }] });
+const esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
+globalThis.fetch = async (u) => {
+ u = String(u);
+ if (u.startsWith("https://api.github.com/")) {
+ asks++;
+ if (apiDown) return new Response('{"message":"API rate limit exceeded"}', { status: 403 });
+ const path = u.split("/repos/yetone/magpie-releases/")[1];
+ const tag = path.match(/^releases\/tags\/v(.+)$/);
+ if (tag) {
+ const r = GITHUB.find((r) => r.version === tag[1]);
+ return r ? Response.json(gh(r)) : new Response("{}", { status: 404 });
+ }
+ if (path === "releases/latest") return Response.json(gh(GITHUB[0]));
+ const page = path.match(/^releases\?per_page=100(?:&page=(\d+))?$/);
+ if (page) {
+ const n = +(page[1] || 1);
+ return Response.json(GITHUB.slice((n - 1) * 100, n * 100).map(gh));
+ }
+ }
+ if (u.endsWith("/releases.atom")) {
+ // the feed: the ten newest, their notes as HTML, the date the last edit's
+ const entries = GITHUB.slice(0, 10).map(
+ (r) => `<entry><updated>2030-01-01T00:00:00Z</updated><link rel="alternate" type="text/html" href="https://github.com/yetone/magpie-releases/releases/tag/v${r.version}"/><content type="html">${esc(r.html || `<p>${r.notes} (as the feed has it)</p>`)}</content></entry>`,
+ );
+ return new Response(`<feed>${entries.join("")}</feed>`);
+ }
+ const sums = u.match(/\/releases\/download\/v([^/]+)\/SHA256SUMS$/);
+ if (sums && GITHUB.some((r) => r.version === sums[1])) return new Response(`sum${sums[1]} magpie-linux-amd64\n`);
+ return new Response("not found", { status: 404 });
+};
+
+// releases makes n releases, one every gap ms back from newest, the newest
+// first: 0.1.(n) down to 0.1.1.
+function releases(n, newest, gap, urgent = []) {
+ return Array.from({ length: n }, (_, i) => {
+ const version = `0.1.${n - i}`;
+ const marked = urgent.includes(version);
+ return { version, published: iso(newest - i * gap), notes: (marked ? `${URGENT_MARK} lost settings\n\n` : "") + `- change ${version}` };
+ });
+}
+
+beforeEach(() => {
+ store.clear();
+ apiDown = false;
+ asks = 0;
+});
+
+test("keepLedger: a new ledger is filled from GitHub's pages, three weeks back at most four asks a run", async () => {
+ const NOW = at("2026-10-28T12:00:00Z");
+ // 600 releases, one every 2h: page 3 ends 25 days back
+ GITHUB = releases(600, NOW - 3600e3, 2 * 3600e3, ["0.1.590"]);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, NOW);
+ assert.ok(asks <= 4, `${asks} asks`);
+ let ix = JSON.parse(env.LEDGER.m.get("index"));
+ assert.equal(ix.list[0].v, "0.1.600");
+ assert.equal(ix.list[0].p, GITHUB[0].published);
+ assert.equal(ix.list.find((r) => r.v === "0.1.590").u, true);
+ assert.equal(ix.list.filter((r) => r.u).length, 1);
+ // pages 1 to 3 reach 25 days back, past the 21 it wants: no fourth
+ assert.equal(ix.list.length, 300);
+ assert.equal(asks, 3);
+ // each release's notes by its day
+ const day = GITHUB[0].published.slice(0, 10);
+ assert.equal(JSON.parse(env.LEDGER.m.get("notes:" + day))["0.1.600"], "- change 0.1.600");
+
+ // the next run: nothing new, nothing written
+ const w = env.LEDGER.writes;
+ asks = 0;
+ await keepLedger(env, NOW + 15 * 60e3);
+ assert.equal(env.LEDGER.writes, w);
+ assert.equal(asks, 1);
+
+ // a new release: the index and its day's notes, and nothing else
+ GITHUB = [{ version: "0.1.601", published: iso(NOW + 20 * 60e3), notes: "- new" }, ...GITHUB];
+ await keepLedger(env, NOW + 30 * 60e3);
+ assert.equal(env.LEDGER.writes, w + 2);
+ ix = JSON.parse(env.LEDGER.m.get("index"));
+ assert.equal(ix.list[0].v, "0.1.601");
+ assert.equal(ix.list.length, 301);
+});
+
+test("keepLedger: four asks a run at most, the next run reaching on", async () => {
+ const NOW = at("2026-10-28T12:00:00Z");
+ // one an hour: three weeks back is 504 releases, six pages
+ GITHUB = releases(1000, NOW, 3600e3);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, NOW);
+ assert.equal(asks, 4);
+ assert.equal(JSON.parse(env.LEDGER.m.get("index")).list.length, 400);
+ // the first page again, then on from where the ledger ends (pages 5, 6)
+ asks = 0;
+ await keepLedger(env, NOW);
+ assert.equal(asks, 3);
+ const ix = JSON.parse(env.LEDGER.m.get("index"));
+ assert.ok(Date.parse(ix.list.at(-1).p) <= NOW - 21 * DAY, ix.list.at(-1).p);
+ assert.equal(new Set(ix.list.map((r) => r.v)).size, ix.list.length);
+ // there: one ask a run from then on
+ asks = 0;
+ await keepLedger(env, NOW);
+ assert.equal(asks, 1);
+});
+
+test("keepLedger: a ledger that can't be read is never written over", async () => {
+ const NOW = at("2026-10-28T12:00:00Z");
+ GITHUB = releases(50, NOW, 3600e3);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, NOW);
+ const before = env.LEDGER.m.get("index");
+ env.LEDGER.broken = true;
+ GITHUB = releases(51, NOW + 3600e3, 3600e3);
+ await keepLedger(env, NOW + 3600e3);
+ assert.equal(env.LEDGER.m.get("index"), before);
+ // no ledger at all: nothing to keep
+ await keepLedger({}, NOW);
+});
+
+test("keepLedger: with the API limited, the feed's ten, their dates and the API's notes kept", async () => {
+ const NOW = at("2026-10-28T12:00:00Z");
+ GITHUB = releases(30, NOW - 3600e3, 3600e3);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, NOW);
+ // two new ones, one marked urgent; the API says no
+ GITHUB = [
+ { version: "0.1.32", published: iso(NOW + 3600e3), notes: "", html: "<blockquote><p><strong>Urgent release:</strong> a ban risk</p></blockquote><ul><li>fix</li></ul>" },
+ { version: "0.1.31", published: iso(NOW), notes: "- plain" },
+ ...GITHUB,
+ ];
+ apiDown = true;
+ await keepLedger(env, NOW + 2 * 3600e3);
+ const ix = JSON.parse(env.LEDGER.m.get("index"));
+ assert.deepEqual(ix.list.slice(0, 2).map((r) => [r.v, r.u]), [["0.1.32", true], ["0.1.31", false]]);
+ // one the ledger had keeps the date it came out, not the feed's edit
+ assert.equal(ix.list.find((r) => r.v === "0.1.30").p, GITHUB[2].published);
+ // and its notes the API's markdown
+ const day = GITHUB[2].published.slice(0, 10);
+ assert.equal(JSON.parse(env.LEDGER.m.get("notes:" + day))["0.1.30"], "- change 0.1.30");
+ // and in no other day's
+ const feedDay = JSON.parse(env.LEDGER.m.get("notes:2030-01-01") || "{}");
+ assert.equal(feedDay["0.1.30"], undefined);
+});
+
+test("keepLedger: a ledger left a day behind is mended past its hole", async () => {
+ const NOW = at("2026-10-28T12:00:00Z");
+ GITHUB = releases(400, NOW - 40 * DAY, 3600e3);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, NOW - 40 * DAY);
+ // 150 more while the cron was stopped: the first page shares none
+ GITHUB = [...releases(550, NOW, 3600e3).slice(0, 150), ...GITHUB];
+ asks = 0;
+ await keepLedger(env, NOW);
+ const ix = JSON.parse(env.LEDGER.m.get("index"));
+ const have = new Set(ix.list.map((r) => r.v));
+ for (let v = 401; v <= 550; v++) assert.ok(have.has(`0.1.${v}`), `0.1.${v}`);
+ assert.ok(asks <= 4);
+ // nothing older than 45 days stays
+ assert.ok(ix.list.every((r) => Date.parse(r.p) >= NOW - 45 * DAY));
+});
+
+const ctx = { waitUntil: (p) => p };
+const ask = async (path, env) => {
+ const res = await worker.fetch(new Request("https://usemagpie.ai" + path), env, ctx);
+ return { status: res.status, body: await res.json() };
+};
+
+test("latest?channel=stable: the week's pick from the ledger; without channel, the newest as before", async () => {
+ // dates from the real clock (the route reads Date.now()): 0.1.2 is out
+ // twelve days, older than any week's cut; the rest are an hour old
+ const now = Date.now();
+ GITHUB = [
+ { version: "0.1.4", published: iso(now - 3600e3), notes: "- newest" },
+ { version: "0.1.3", published: iso(now - 2 * 3600e3), notes: "- newer" },
+ { version: "0.1.2", published: iso(now - 12 * DAY), notes: "### Fixes\n\n- stable one\n\n<!-- lang:zh -->\n\n- 稳定的" },
+ { version: "0.1.1", published: iso(now - 13 * DAY), notes: "- old" },
+ ];
+ const env = { LEDGER: kv() };
+ await keepLedger(env, now);
+ let got = await ask("/api/latest?channel=stable", env);
+ assert.equal(got.status, 200);
+ assert.equal(got.body.version, "0.1.2");
+ assert.equal(got.body.channel, "stable");
+ assert.equal(got.body.urgent, false);
+ assert.equal(got.body.notes, "### Fixes\n\n- stable one");
+ assert.equal(got.body.assets["magpie-linux-amd64"].sha256, "sum0.1.2");
+ assert.equal((await ask("/api/latest?channel=stable&lang=zh", env)).body.notes, "- 稳定的");
+ // every other ask is every version, as it was
+ for (const q of ["", "?channel=every", "?channel=beta"]) {
+ got = await ask("/api/latest" + q, env);
+ assert.equal(got.body.version, "0.1.4", q);
+ assert.equal(got.body.channel, undefined, q);
+ }
+
+ // an urgent release goes to Stable at once
+ GITHUB = [{ version: "0.1.5", published: iso(now - 60e3), notes: `${URGENT_MARK} a security hole\n\n- fix` }, ...GITHUB];
+ await keepLedger(env, now);
+ store.clear();
+ got = await ask("/api/latest?channel=stable", env);
+ assert.equal(got.body.version, "0.1.5");
+ assert.equal(got.body.urgent, true);
+
+ // the API limited: the files from SHA256SUMS, the notes from the ledger
+ store.clear();
+ apiDown = true;
+ got = await ask("/api/latest?channel=stable", env);
+ assert.equal(got.status, 200);
+ assert.equal(got.body.version, "0.1.5");
+ assert.match(got.body.notes, /a security hole/);
+ assert.equal(got.body.assets["magpie-linux-amd64"].sha256, "sum0.1.5");
+});
+
+test("latest?channel=stable: unknown (503, never kept) without a ledger that reaches the cut", async () => {
+ const now = Date.now();
+ GITHUB = [{ version: "0.1.2", published: iso(now - 3600e3), notes: "- new" }];
+ for (const env of [{}, { LEDGER: kv() }]) {
+ const res = await worker.fetch(new Request("https://usemagpie.ai/api/latest?channel=stable"), env, ctx);
+ assert.equal(res.status, 503);
+ assert.equal(res.headers.get("Cache-Control"), "no-store");
+ }
+ const env = { LEDGER: kv() };
+ await keepLedger(env, now);
+ assert.equal((await ask("/api/latest?channel=stable", env)).status, 503);
+ const broken = { LEDGER: kv() };
+ broken.LEDGER.broken = true;
+ assert.equal((await ask("/api/latest?channel=stable", broken)).status, 503);
+});
+
+test("notes: a range older than the hundred newest is read on from the ledger, one list", async () => {
+ const now = Date.now();
+ // 250 releases an hour apart: the API's first page has the newest 100
+ GITHUB = releases(250, now, 3600e3);
+ const env = { LEDGER: kv() };
+ await keepLedger(env, now);
+ store.clear();
+ const got = await ask("/api/notes?after=0.1.20&upto=0.1.250&lang=en", env);
+ assert.equal(got.status, 200);
+ const vs = got.body.releases.map((r) => r.version);
+ assert.equal(vs.length, 230);
+ assert.equal(vs[0], "0.1.250");
+ assert.equal(vs.at(-1), "0.1.21");
+ assert.equal(new Set(vs).size, vs.length);
+ assert.equal(got.body.releases.find((r) => r.version === "0.1.30").notes, "- change 0.1.30");
+ // limit still caps, reaching past the hundred
+ const back = await ask("/api/notes?upto=0.1.250&limit=120", env);
+ assert.equal(back.body.releases.length, 120);
+ assert.equal(back.body.releases.at(-1).version, "0.1.131");
+ // a range the hundred cover never reads the ledger
+ const reads = env.LEDGER.get;
+ let read = 0;
+ env.LEDGER.get = (...a) => (read++, reads(...a));
+ assert.equal((await ask("/api/notes?after=0.1.240&upto=0.1.250", env)).body.releases.length, 10);
+ assert.equal(read, 0);
+});
diff --git a/site/worker.js b/site/worker.js
index 91c7595fd..9bfb9b7fa 100644
--- a/site/worker.js
+++ b/site/worker.js
@@ -3,6 +3,10 @@
// download links that never go stale.
//
// /api/latest {version, notes, url, published, assets: {name: {url, size, sha256}}}
+// /api/latest?channel=stable
+// the same for Settings' Stable channel: a release a
+// week, an urgent one at once ("The Stable channel"
+// below), with channel and urgent; 503 while unknown
// /api/notes?after=&upto=&limit= {releases: [{version, notes, url, published}]}, newest
// first: what changed since the version an app last ran
// Both take ?lang=zh for the notes in Chinese, where a
@@ -64,9 +68,16 @@ export default {
return Response.redirect(url.toString(), 301);
}
if (url.pathname === "/api/latest") {
+ const lang = url.searchParams.get("lang");
+ // Settings' Stable channel; any other channel, or none, is every
+ // version as it comes out
+ if (url.searchParams.get("channel") === "stable") {
+ const got = await stable(ctx, env, Date.now());
+ if (!got) return json({ error: "the stable version isn't known yet" }, 503, NO_STORE);
+ return json({ ...got.rel, notes: inLang(got.rel.notes, lang) }, 200, cacheFor(got));
+ }
const got = await latest(ctx, env);
if (!got) return json({ error: "no release yet" }, 503, NO_STORE);
- const lang = url.searchParams.get("lang");
return json({ ...got.rel, notes: inLang(got.rel.notes, lang) }, 200, cacheFor(got));
}
if (url.pathname === "/api/partners") {
@@ -94,10 +105,15 @@ export default {
// limit: the newest that many alone (Settings' "this version and the
// 10 before it", Linka), so an app asking for 11 isn't sent 100
const limit = parseInt(url.searchParams.get("limit"), 10);
- const pick = got.list
- .filter((r) => (!after || newer(r.version, after)) && (!upto || !newer(r.version, upto)))
- .slice(0, limit > 0 ? limit : undefined)
- .map((r) => ({ ...r, notes: inLang(r.notes, lang) }));
+ const within = (r) => (!after || newer(r.version, after)) && (!upto || !newer(r.version, upto));
+ let pick = got.list.filter(within);
+ // the hundred newest are a day of releases: a range reaching further
+ // back (a Stable app's week, an app long unopened) is read on from
+ // the ledger
+ const oldest = got.list.at(-1);
+ const short = after ? !oldest || newer(oldest.version, after) : limit > 0 ? pick.length < limit : !pick.some((r) => r.version === upto);
+ if (short && upto) pick = [...pick, ...(await fromLedger(env, oldest && oldest.version, after, upto, limit > 0 ? limit - pick.length : LEDGER_MOST))];
+ pick = pick.slice(0, limit > 0 ? limit : undefined).map((r) => ({ ...r, notes: inLang(r.notes, lang) }));
return json({ releases: pick }, 200, cacheFor(got));
}
if (url.pathname === "/api/icon") return icon(url.searchParams.get("url"));
@@ -145,6 +161,11 @@ export default {
}
return beacon(await env.ASSETS.fetch(req));
},
+ // the cron (wrangler.jsonc's triggers) keeps the ledger the Stable
+ // channel is chosen from
+ async scheduled(event, env, ctx) {
+ ctx.waitUntil(keepLedger(env, Date.now()));
+ },
};
// A partner's reach and clicks are counted here, not in the app: the app's
@@ -369,10 +390,12 @@ function api(path, env) {
return fetch(`https://api.github.com/repos/${REPO}/${path}`, { headers });
}
-async function fromAPI(env) {
+// fromAPI is a release from GitHub's API: the newest, or the one path names
+// (releases/tags/v0.1.2), or null.
+async function fromAPI(env, path = "releases/latest") {
let res;
try {
- res = await api("releases/latest", env);
+ res = await api(path, env);
} catch (e) {
console.log("github api", e);
return null;
@@ -406,10 +429,17 @@ async function fromPages() {
console.log("github latest redirect", res.status);
return null;
}
- const [sums, feed] = await Promise.all([checksums(tag), fromFeed()]);
+ const feed = await fromFeed();
+ return fromSums(tag, (feed || []).find((r) => r.version === tag.replace(/^v/, "")));
+}
+
+// fromSums is tag's release from its SHA256SUMS alone, with its notes and
+// date from entry ({notes, published}) where there is one: every file, its
+// size unknown (0). null when SHA256SUMS can't be read.
+async function fromSums(tag, entry) {
+ const sums = await checksums(tag);
if (!sums) return null;
const url = `https://github.com/${REPO}/releases/tag/${tag}`;
- const entry = (feed || []).find((r) => r.version === tag.replace(/^v/, ""));
const rel = { version: tag.replace(/^v/, ""), notes: entry ? entry.notes : "", url, published: entry ? entry.published : null, assets: {} };
for (const [name, sha256] of Object.entries(sums)) {
rel.assets[name] = { url: `https://github.com/${REPO}/releases/download/${tag}/${name}`, size: 0, sha256 };
@@ -454,6 +484,228 @@ async function releases(ctx, env) {
return got;
}
+// The Stable channel (Settings' Update channel, ssharpcc on X): magpie
+// releases many times a day, and someone who reads every changelog before
+// updating wants fewer, older ones. /api/latest?channel=stable is the
+// update feed for them, chosen here from the release list and never by
+// hand:
+//
+// - once a week, as the week turns (Monday 00:00 UTC), Stable becomes
+// the newest release that was out SOAK (three days) by then;
+// - an urgent release (a security hole, a ban risk, lost data) is Stable
+// as soon as it is out, until a weekly pick passes it.
+//
+// A release is urgent when its notes have a line that starts with
+// URGENT_MARK: "**Urgent release:** why". magpie-releases' release run
+// writes it from an `Urgent:` trailer on any commit since the version
+// before, and it can be added to a published release's notes by hand
+// (gh release edit) within a day, while the release is on the ledger's
+// first page. GitHub releases are never marked pre-release for this:
+// /download and the Latest mark go by them.
+//
+// A hundred releases are a day or so of magpie, so the list GitHub's API
+// gives in one ask doesn't reach a week back. The ledger does: a KV
+// namespace (LEDGER) the cron fills every quarter hour, its "index" every
+// release of the last LEDGER_DAYS ({v, p: published, u: urgent}, newest
+// first) and "notes:<UTC day>" each day's notes by version, which
+// /api/notes reads for a range older than the hundred newest. Without the
+// ledger, or one that doesn't reach back to the week's cut, Stable isn't
+// known and the feed says so (503) rather than guess.
+const DAY = 86400e3;
+const SOAK = 3 * DAY;
+const LEDGER_DAYS = 45;
+const LEDGER_REACH = 21 * DAY; // how far back the cron fills the ledger
+const LEDGER_PAGES = 4; // GitHub API asks one cron run makes at most
+const LEDGER_MOST = 1500; // releases /api/notes reads from the ledger at most
+export const URGENT_MARK = "**Urgent release:**";
+
+// urgentIn says whether a release's notes mark it urgent: a line (or a
+// quoted one) that starts with URGENT_MARK, in any case.
+export function urgentIn(notes) {
+ return /^[ \t]*(?:>[ \t]*)?\*\*urgent release:\*\*/im.test(notes || "");
+}
+
+// weekStart is the Monday 00:00 UTC on or before now, in ms.
+export function weekStart(now) {
+ const d = new Date(now);
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) - ((d.getUTCDay() + 6) % 7) * DAY;
+}
+
+// stableOf is the Stable release at now among list ([{version, published,
+// urgent}], any order): the newer of the newest release out SOAK before
+// this week began and the newest urgent one. null when the list doesn't
+// reach back to that cut and has no urgent release either: unknown.
+export function stableOf(list, now) {
+ const cut = weekStart(now) - SOAK;
+ let weekly = null, rush = null, reaches = false;
+ for (const r of list) {
+ const at = Date.parse(r.published);
+ if (!(at <= now)) continue; // a date not known, or not yet
+ if (at <= cut) {
+ reaches = true;
+ if (!weekly || newer(r.version, weekly.version)) weekly = r;
+ }
+ if (r.urgent && (!rush || newer(r.version, rush.version))) rush = r;
+ }
+ if (!reaches) return rush;
+ return rush && newer(rush.version, weekly.version) ? rush : weekly;
+}
+
+// readIndex is the ledger's index, null when there is no ledger, and
+// undefined when it can't be read: unknown, never an empty one.
+async function readIndex(env, cacheTtl) {
+ if (!env || !env.LEDGER) return null;
+ try {
+ const ix = await env.LEDGER.get("index", cacheTtl ? { type: "json", cacheTtl } : { type: "json" });
+ if (ix === null) return { list: [] };
+ return Array.isArray(ix && ix.list) ? ix : undefined;
+ } catch (e) {
+ console.log("ledger index", e);
+ return undefined;
+ }
+}
+
+// stable is the Stable release, condensed as latest's is: {rel, degraded},
+// or null when it isn't known. rel also says it is Stable's (channel) and
+// whether it went there as urgent.
+async function stable(ctx, env, now) {
+ const hit = await cached("stable");
+ if (hit && hit.rel && hit.rel.version) return hit;
+ const ix = await readIndex(env, 60);
+ if (!ix) return null;
+ const pick = stableOf(ix.list.map((r) => ({ version: r.v, published: r.p, urgent: !!r.u })), now);
+ if (!pick) return null;
+ const tag = "v" + pick.version;
+ let rel = await fromAPI(env, "releases/tags/" + tag), degraded = false;
+ if (!rel) {
+ // the API said no: the files from SHA256SUMS, the notes from the ledger
+ const [entry] = await fromLedger(env, null, null, pick.version, 1);
+ rel = await fromSums(tag, entry && entry.version === pick.version ? entry : { notes: "", published: pick.published });
+ degraded = true;
+ }
+ if (!rel) return null;
+ const got = { rel: { ...rel, channel: "stable", urgent: !!pick.urgent }, degraded };
+ remember(ctx, "stable", got, degraded ? BRIEF : TTL);
+ return got;
+}
+
+// fromLedger is the notes of the releases the ledger has older than below
+// (every one when null), after after (exclusive, "" for any) up to upto,
+// newest first, at most most: [{version, notes, url, published}].
+async function fromLedger(env, below, after, upto, most) {
+ const ix = await readIndex(env, 300);
+ if (!ix || !(most > 0)) return [];
+ const want = ix.list
+ .filter((r) => (!below || newer(below, r.v)) && (!after || newer(r.v, after)) && !newer(r.v, upto))
+ .sort((a, b) => (newer(a.v, b.v) ? -1 : newer(b.v, a.v) ? 1 : 0))
+ .slice(0, most);
+ const days = [...new Set(want.map((r) => dayOf(r.p)))];
+ const books = await Promise.all(days.map((d) => env.LEDGER.get("notes:" + d, { type: "json", cacheTtl: 300 }).catch(() => null)));
+ const by = Object.fromEntries(days.map((d, i) => [d, books[i] || {}]));
+ return want.map((r) => ({ version: r.v, notes: by[dayOf(r.p)][r.v] || "", url: `https://github.com/${REPO}/releases/tag/v${r.v}`, published: r.p }));
+}
+
+function dayOf(iso) {
+ return String(iso || "").slice(0, 10);
+}
+
+// keepLedger is the cron's run: GitHub's newest hundred releases into the
+// ledger (or the releases feed's ten, when the API says no), the pages
+// after them while the ledger has a hole or doesn't yet reach
+// LEDGER_REACH back, at most LEDGER_PAGES asks. A ledger that can't be
+// read is left alone, never written over as an empty one.
+export async function keepLedger(env, now) {
+ const ix = await readIndex(env);
+ if (!ix) return;
+ const kv = env.LEDGER;
+ const had = new Map(ix.list.map((r) => [r.v, r]));
+ const got = new Map(); // version: {version, notes, published}
+ let asks = 0, bottom = !!ix.bottom;
+ const page = async (n) => {
+ asks++;
+ let res;
+ try {
+ res = await api(`releases?per_page=100&page=${n}`, env);
+ } catch (e) {
+ console.log("ledger page", n, e);
+ return null;
+ }
+ if (!res.ok) {
+ console.log("ledger page", n, res.status);
+ return null;
+ }
+ const rels = (await res.json())
+ .filter((r) => !r.draft && !r.prerelease)
+ .map((r) => ({ version: r.tag_name.replace(/^v/, ""), notes: r.body || "", published: r.published_at }));
+ if (rels.length < 100) bottom = true;
+ for (const r of rels) got.set(r.version, r);
+ return rels;
+ };
+ const first = await page(1);
+ if (!first) for (const r of (await fromFeed()) || []) got.set(r.version, r);
+ // a hole (the cron stopped for a day): the pages after the first until
+ // one meets what the ledger had
+ let n = 1;
+ for (let last = first; last && last.length === 100 && had.size && !last.some((r) => had.has(r.version)) && asks < LEDGER_PAGES; ) last = await page(++n);
+ // reaching back: the page where the ledger's oldest is, then on
+ const oldest = () => {
+ let o = null;
+ for (const r of [...had.values(), ...[...got.values()].map((g) => ({ v: g.version, p: g.published }))]) if (!o || newer(o.v, r.v)) o = r;
+ return o;
+ };
+ let deep = Math.max(n + 1, Math.floor((had.size || got.size) / 100) + 1);
+ while (first && !bottom && asks < LEDGER_PAGES) {
+ const o = oldest();
+ if (o && Date.parse(o.p) <= now - LEDGER_REACH) break;
+ if (!(await page(deep++))) break;
+ }
+ // the index: what was had, what came, nothing older than LEDGER_DAYS
+ const keep = now - LEDGER_DAYS * DAY;
+ const merged = new Map(had);
+ for (const r of got.values()) {
+ // the feed's date is when the release was last edited: the API's, or
+ // the one the ledger has, is when it came out
+ const was = merged.get(r.version);
+ merged.set(r.version, { v: r.version, p: !first && was ? was.p : r.published, u: urgentIn(r.notes) });
+ }
+ const list = [...merged.values()]
+ .filter((r) => r.v && Date.parse(r.p) >= keep)
+ .sort((a, b) => (newer(a.v, b.v) ? -1 : newer(b.v, a.v) ? 1 : 0));
+ const next = { list, bottom };
+ if (JSON.stringify(next) !== JSON.stringify({ list: ix.list, bottom: !!ix.bottom })) await kv.put("index", JSON.stringify(next));
+ // each day's notes, written where they changed
+ const days = new Map();
+ for (const r of got.values()) {
+ // by the day the index has it out (the feed's date is its last edit)
+ const p = merged.get(r.version).p;
+ if (!p || Date.parse(p) < keep) continue;
+ const d = dayOf(p);
+ if (!days.has(d)) days.set(d, []);
+ days.get(d).push(r);
+ }
+ for (const [d, rels] of days) {
+ const key = "notes:" + d;
+ let book;
+ try {
+ book = (await kv.get(key, { type: "json" })) || {};
+ } catch (e) {
+ console.log("ledger notes", d, e);
+ continue; // unreadable: left as it is
+ }
+ let changed = false;
+ for (const r of rels) {
+ // the feed's notes (HTML made markdown, the Chinese marker guessed)
+ // never replace the API's
+ if (!first && book[r.version]) continue;
+ if (book[r.version] !== r.notes) {
+ book[r.version] = r.notes;
+ changed = true;
+ }
+ }
+ if (changed) await kv.put(key, JSON.stringify(book), { expirationTtl: (LEDGER_DAYS + 15) * 86400 });
+ }
+}
+
// fromFeed is the newest releases from GitHub's releases feed (Atom), which
// isn't the API and so isn't limited by it: [{version, notes, url,
// published}], newest first, or null. The feed has each release's notes as
diff --git a/site/wrangler.jsonc b/site/wrangler.jsonc
index ea961ae40..9ffaf5161 100644
--- a/site/wrangler.jsonc
+++ b/site/wrangler.jsonc
@@ -14,5 +14,9 @@
{ "pattern": "usemagpie.ai", "custom_domain": true },
{ "pattern": "www.usemagpie.ai", "custom_domain": true }
],
+ // the ledger of releases the Stable channel is chosen from (worker.js,
+ // "The Stable channel"), kept by the cron every quarter hour
+ "kv_namespaces": [{ "binding": "LEDGER", "id": "716b22ec7d4c496abb7b3dc085258fde" }],
+ "triggers": { "crons": ["*/15 * * * *"] },
"observability": { "enabled": true }
}