From 8d2e0b6c9b790b605454148da62f513fb0af1759 Mon Sep 17 00:00:00 2001 From: Horizon agent Date: Thu, 12 Nov 2026 17:19:35 +0100 Subject: [PATCH 1/2] Perf viewer clean-up: Kira calls the site's /api/perf/sweep every 5 minutes Only when PERF_SWEEP_TOKEN is set (the same value as the site's). The viewer's Share uploads expire after an hour unshared, or after PERF_SHARED_DAYS; the sweep frees their bytes in the bucket. Co-Authored-By: Claude Opus 5.5 --- src/bot.js | 3 ++- src/features/perfsweep.js | 23 +++++++++++++++++++++++ test/perfsweep.test.js | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/features/perfsweep.js create mode 100644 test/perfsweep.test.js diff --git a/src/bot.js b/src/bot.js index 74c4ef8..cd5798f 100644 --- a/src/bot.js +++ b/src/bot.js @@ -41,6 +41,7 @@ import * as apply from './features/apply.js'; import * as modcases from './features/modcases.js'; import * as batchmod from './features/batchmod.js'; import * as teamroles from './features/teamroles.js'; +import * as perfsweep from './features/perfsweep.js'; import { startServer } from './mcp.js'; import { adminRoutes } from './admin.js'; import * as launcherGithub from './launcher-github.js'; @@ -50,7 +51,7 @@ import { dataFile, freeze } from './store.js'; import { publicKey } from './secrets.js'; import { applyBrand } from './brand.js'; -const FEATURES = [modcases, moderation, batchmod, levels, rooms, community, forums, github, workboard, contributors, notify, site, smp, triage, agents, hub, updates, x, kiraAi, chatclear, sponsors, staff, support, apply, teamroles, teamvote, poll, age, elections]; +const FEATURES = [modcases, moderation, batchmod, levels, rooms, community, forums, github, workboard, contributors, notify, site, smp, triage, agents, hub, updates, x, kiraAi, chatclear, sponsors, staff, support, apply, teamroles, teamvote, poll, age, elections, perfsweep]; const DISCORD = process.env.KIRA_DISCORD !== 'off'; const handlers = Object.assign({}, ...FEATURES.map((f) => f.handlers)); const commands = FEATURES.flatMap((f) => f.commands); diff --git a/src/features/perfsweep.js b/src/features/perfsweep.js new file mode 100644 index 0000000..5876f86 --- /dev/null +++ b/src/features/perfsweep.js @@ -0,0 +1,23 @@ +// The perf viewer's clean-up (horizon-site /api/perf/sweep): every 5 minutes Kira asks the site to delete expired +// and abandoned capture uploads from its bucket. Only when PERF_SWEEP_TOKEN is set, the same value as the site's; +// without it Kira never calls. +import { LINKS } from '../config.js'; + +export const EVERY_MS = 5 * 60_000; + +// Returns how many captures the site deleted, or null when no token is set. +export async function sweep({ token = process.env.PERF_SWEEP_TOKEN, fetchImpl = fetch } = {}) { + if (!token) return null; + const response = await fetchImpl(`${LINKS.horizonSite}/api/perf/sweep`, { + method: 'POST', + headers: { Authorization: `Bearer ${token}` }, + signal: AbortSignal.timeout(30_000), + }); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + return (await response.json()).deleted; +} + +export function setup() { + if (!process.env.PERF_SWEEP_TOKEN) return; + setInterval(() => sweep().catch((e) => console.error('perf sweep', e.message)), EVERY_MS).unref?.(); +} diff --git a/test/perfsweep.test.js b/test/perfsweep.test.js new file mode 100644 index 0000000..ffcddd3 --- /dev/null +++ b/test/perfsweep.test.js @@ -0,0 +1,15 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { sweep } from '../src/features/perfsweep.js'; + +test('the sweep posts to the site with the token, and does nothing without one', async () => { + const calls = []; + const fetchImpl = async (url, options) => { calls.push({ url, options }); return { ok: true, json: async () => ({ deleted: 3 }) }; }; + assert.equal(await sweep({ token: '', fetchImpl }), null); + assert.equal(calls.length, 0); + assert.equal(await sweep({ token: 's3cret', fetchImpl }), 3); + assert.equal(calls[0].url, 'https://playhorizon.dev/api/perf/sweep'); + assert.equal(calls[0].options.method, 'POST'); + assert.equal(calls[0].options.headers.Authorization, 'Bearer s3cret'); + await assert.rejects(sweep({ token: 's3cret', fetchImpl: async () => ({ ok: false, status: 403 }) }), /HTTP 403/); +}); From 08c86613915707a8b7a8b9c8a20e2f6c59faf3b3 Mon Sep 17 00:00:00 2001 From: Horizon agent Date: Thu, 12 Nov 2026 17:24:39 +0100 Subject: [PATCH 2/2] elections test: the timer test runs its own vote at NOW It took the first open vote, but /elect and /vote in the tests above start on the real clock, so which of them was still open depended on the day: since 2026-11-12 it was an election with no ballots, which never lands in closed, and CI failed on main's tests. Co-Authored-By: Claude Opus 5.5 --- test/elections.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/elections.test.js b/test/elections.test.js index c1bb4d9..97543c4 100644 --- a/test/elections.test.js +++ b/test/elections.test.js @@ -293,7 +293,10 @@ test('council polls: money lasts 48 hours, a rule only when asked; veto is never }); test('the timer closes votes at 24 hours and takes the veto button away after 24 more', async () => { - const e = openE().find((x) => x.stage === 'vote'); + // Its own vote at NOW: /elect and /vote above start on the real clock, so which of theirs is still open depends on the day. + const { election: e } = await el.startVote(M1, { body: 'mods', question: 'Does the timer close this vote on time?' }, { now: NOW }); + for (const [x, v] of [[M1, 'yes'], [M3, 'yes'], [M4, 'no']]) await press(x, e.id, v); + assert.ok(core.state.elections.open[e.id], 'two Yes and one No of five settle nothing early'); await el.tick(e.endsAt + 1); assert.equal(core.state.elections.open[e.id], undefined); const left = Object.values(core.state.elections.closed).filter((c) => !c.vetoPoll);