Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pm-stack/aggregator/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ function html(body: string, status = 200): Response {
return new Response(body, { status, headers: { "content-type": "text/html; charset=utf-8" } });
}

// Self-unregistering service worker, served at /sw.js on purpose. Until
// 2026-10-27 this origin (the laptop's tailnet-serve address) fronted the
// opencode web app, which registered a Workbox service worker that keeps
// serving the opencode shell from cache even though the origin now serves
// the dashboard. Browsers holding that registration pick this script up
// on their next update check: it installs, unregisters itself, and
// reloads open tabs into the dashboard. Harmless for fresh browsers --
// it removes itself immediately.
const SW_KILL_SWITCH = `self.addEventListener('install', () => { self.skipWaiting(); });
self.addEventListener('activate', async () => {
try { await self.registration.unregister(); } catch {}
const cs = await self.clients.matchAll({ type: 'window' });
for (const c of cs) { try { c.navigate(c.url); } catch {} }
Comment on lines +201 to +204

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Extend the activation event through cleanup and navigation

When this replacement worker activates for a browser displaying the stale opencode shell, the async listener's returned promise does not extend the activation event. The browser can therefore terminate the worker while unregister() or matchAll() is pending, leaving the open tabs unreloaded; the service-worker lifetime specification explicitly permits termination when there is no event to handle. Pass the entire cleanup operation to the activation event's waitUntil() and await the navigation promises as well, so the kill switch keeps the worker alive until its recovery work completes.

Useful? React with 👍 / 👎.

});
`;

function serviceWorkerKillSwitch(): Response {
return new Response(SW_KILL_SWITCH, {
headers: { "content-type": "text/javascript; charset=utf-8", "cache-control": "no-cache" },
});
}

async function boardContext() {
const board = await source.board();
const refs = allRefs(board);
Expand Down Expand Up @@ -570,6 +592,11 @@ async function handler(req: Request): Promise<Response> {
const section = await source.orchestration();
return json(section ?? { status: "absent", reason: "no orchestration run has published yet" });
}
if (path === "/sw.js") {
// Kill switch for the opencode service worker stranded on this
// origin (see SW_KILL_SWITCH above) -- never a real worker here.
return serviceWorkerKillSwitch();
}
if (path === "/api/health") {
// #1267: prime first — health's answer must not depend on
// request order. `sections` carries the structured diagnosis
Expand Down
10 changes: 10 additions & 0 deletions pm-stack/aggregator/test/boot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ describe("server boots from fixtures", () => {
expect(html).toContain("Cross-repo WIP board");
});

test("sw.js serves the self-unregistering kill switch", async () => {
const res = await fetch(`http://127.0.0.1:${port}/sw.js`);
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toContain("javascript");
expect(res.headers.get("cache-control")).toBe("no-cache");
const body = await res.text();
expect(body).toContain("skipWaiting");
expect(body).toContain("registration.unregister()");
});

test("roadmap page renders", async () => {
const res = await fetch(`http://127.0.0.1:${port}/roadmap`);
expect(res.status).toBe(200);
Expand Down
Loading