Skip to content
Merged
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
71 changes: 52 additions & 19 deletions lib/hosts/v10-upgrade.integration.test.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Real pre-cutover daemons (link protocol 10 and 11) upgrading to this checkout through this checkout's hub.
//
// Real pre-cutover daemons (link protocol 10 and 11) and a real current-protocol one (12, v0.7.1) updating to this
// checkout through this checkout's hub.
// Nothing of the old side is imitated. The daemon is the published source, extracted from git at a pinned commit into
// a disposable npm-layout install and started as `node bin/omp-web-host.js`; its welcome check, its `checkUpdates` /
// `updateSoftware` handlers and its updater engine (candidate protocol check, install, swap, restart, health) all run
// unchanged. Only delivery is disposable: a fetch preload answers the npm registry and the raw-GitHub link-protocol.ts
// (the worktree's real file, parsed by the OLD updater's own regex), and a fake `npm` "installs" a copy of this
// checkout as the release. The hub is the real hub behind the real /api/hosts/link route. No user service is touched.
//
// Pinned sources (both reachable from main; the test skips, naming the commit, when a shallow clone lacks it):
// Pinned sources (all reachable from main; the test skips, naming the commit, when a shallow clone lacks it):
// v10 546a945df0d1716a7a135d6e3221ba885b00171e package 0.6.0, link 10. The last commit before 8ec9208d (runner
// channels); published as prerelease 20261113T113829Z-546a945df0. v10 first appeared at a53494c4. Its updater
// takes target "ompweb" or "omp" and has no preflight.
// v11 3a56e79b217a3949f7180069e6769bc3fa031167 package 0.7.0, link 11: the commit tag v0.7.0 points at.
// v12 068ec4d414c7d2098f53c987ff956a5a2e939c77 package 0.7.1, link 12: the commit tag v0.7.1 points at. Same protocol as
// this checkout, so the hub welcomes it normally (online, not upgrade-only) and starts its update through the
// same POST /api/hosts/<id>/updates a GUI click uses; only the old daemon's own updater does the install.
//
// Run: bun test --timeout 240000 lib/hosts/v10-upgrade.integration.test.mjs
// Needs: full git history (`git cat-file -e <sha>`), `tar` and `git` on PATH, node >= 24 on PATH, `npm ci` done in this
Expand All @@ -37,6 +40,7 @@ import { nodeBinary, repo, repoVersion, spawnNodeOutsideService } from "./update
const LEGACY = {
v10: { name: "v10", link: 10, version: "0.6.0", commit: "546a945df0d1716a7a135d6e3221ba885b00171e" },
v11: { name: "v11", link: 11, version: "0.7.0", commit: "3a56e79b217a3949f7180069e6769bc3fa031167" },
v12: { name: "v12", link: 12, version: "0.7.1", commit: "068ec4d414c7d2098f53c987ff956a5a2e939c77" },
};
/** The application link version this release speaks, and the only target compatibility an old updater may be given. */
const APPLICATION = 12;
Expand All @@ -46,6 +50,7 @@ const previousAgentDir = process.env.PI_CODING_AGENT_DIR;
process.env.PI_CODING_AGENT_DIR = join(base, "main-agent");

const linkRoute = await import("../../app/api/hosts/link/route.ts");
const updatesRoute = await import("../../app/api/hosts/[id]/updates/route.ts");
const hub = await import("./hub.ts");
const { createHost } = await import("./registry.ts");
const { closeNotificationStore } = await import("../notification-store.ts");
Expand Down Expand Up @@ -82,6 +87,10 @@ const server = Bun.serve({
GET: (req) => linkRoute.GET(req, { upgrade: (data) => server.upgrade(req, { data }) }),
POST: (req) => linkRoute.POST(req),
},
"/api/hosts/:id/updates": {
GET: (req) => updatesRoute.GET(req, { params: Promise.resolve(req.params) }),
POST: (req) => updatesRoute.POST(req, { params: Promise.resolve(req.params) }),
},
},
websocket: {
...hub.hostLinkSocket,
Expand All @@ -92,7 +101,7 @@ const server = Bun.serve({
});

/** Pids of every process whose command line names `dir`: daemons, workers and stand-ins all run files from inside it. */
function processesUnder(dir) {
function processesUnder(dir, needle = "") {
let lines;
if (process.platform === "linux") {
lines = readdirSync("/proc").filter((name) => /^\d+$/.test(name)).flatMap((name) => {
Expand All @@ -106,7 +115,7 @@ function processesUnder(dir) {
return match ? [[Number(match[1]), match[2]]] : [];
});
}
return lines.filter(([pid, command]) => pid !== process.pid && command.includes(dir)).map(([pid]) => pid);
return lines.filter(([pid, command]) => pid !== process.pid && command.includes(dir) && command.includes(needle)).map(([pid]) => pid);
}
async function stopAll(dir) {
for (const signal of ["SIGTERM", "SIGKILL"]) {
Expand Down Expand Up @@ -190,6 +199,12 @@ async function startLegacy(legacy, layout) {
mkdirSync(join(work, "bin"), { recursive: true });
mkdirSync(modules, { recursive: true });
mkdirSync(agent, { recursive: true });
// A session file no update may touch: the daemon only ever reads the sessions directory.
const sessionFile = join(agent, "sessions", "--project--", "s.jsonl");
mkdirSync(dirname(sessionFile), { recursive: true });
writeFileSync(sessionFile, '{"type":"session"}\n');
const sessions = () => [readdirSync(join(agent, "sessions"), { recursive: true }).sort(), readFileSync(sessionFile, "utf8"), statSync(sessionFile).mtimeMs];
const sessionsBefore = sessions();
// The daemon's dependencies, resolved from the node_modules above the install as for any hoisted global install.
for (const name of readdirSync(join(repo, "node_modules"))) {
if (!name.startsWith(".")) symlinkSync(realpathSync(join(repo, "node_modules", name)), join(modules, name), "dir");
Expand Down Expand Up @@ -234,7 +249,7 @@ fs.cpSync(${JSON.stringify(release())}, path.join(args[args.indexOf("--prefix")
child.stderr.on("data", (data) => { log += data; });
const lines = (name) => existsSync(join(work, name)) ? readFileSync(join(work, name), "utf8").split("\n").filter(Boolean) : [];
return {
legacy, layout, work, install, child, hostId: host.id, stateRoot: join(agent, "omp-web", "host-update"),
legacy, layout, work, install, child, hostId: host.id, stateRoot: join(agent, "omp-web", "host-update"), sessions, sessionsBefore,
log: () => log,
npmCalls: () => lines("npm.log").map((line) => JSON.parse(line)),
fetched: () => lines("fetch.log"),
Expand All @@ -260,13 +275,20 @@ async function checkUpdates(run) {
}
}

/** What a GUI click sends to start a host's update: the real route, over HTTP. */
const startUpdate = (hostId, version) => fetch(`http://127.0.0.1:${server.port}/api/hosts/${hostId}/updates`, {
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ target: "ompweb", version }),
});

async function upgrade(run) {
const { legacy, layout, hostId } = run;
const row = () => hub.listHosts().find((host) => host.id === hostId);
const sent = () => down.get(hostId) ?? [];

// The real old daemon is welcomed with the version it said (it throws on any other) and linked for upgrading only.
await waitFor(() => row()?.status === "needs-upgrade" && row().upgradeOnly === true && /linked to /.test(run.log()), `the ${legacy.name} daemon to be welcomed upgrade-only`, run);
// The real old daemon is welcomed with the version it said (it throws on any other). A daemon on this release's own
// protocol is simply online; an older one is linked for upgrading only.
const compatible = legacy.link === APPLICATION;
await waitFor(() => row()?.status === (compatible ? "online" : "needs-upgrade") && row().upgradeOnly === (compatible ? undefined : true) && /linked to /.test(run.log()), `the ${legacy.name} daemon to be welcomed ${compatible ? "online" : "upgrade-only"}`, run);
assert.equal(row().hostVersion, legacy.link);
assert.equal(row().ompwebVersion, legacy.version);
assert.deepEqual([...new Set(sent().filter((message) => message.type === "welcome").map((message) => message.v))], [legacy.link]);
Expand All @@ -288,7 +310,8 @@ async function upgrade(run) {
const stale = await checkUpdates(run);
assert.equal(stale.ompweb.canUpdate, false);
assert.match(stale.ompweb.blockedReason, /installation is shared/);
await assert.rejects(hub.updateHostSoftware(hostId, "ompweb", repoVersion));
const refused = await startUpdate(hostId, repoVersion);
assert.deepEqual([refused.status, (await refused.json()).code], [409, "host_update_blocked"]);
assert.deepEqual(run.npmCalls(), []);
// `npm i -g` of the web package: the shared installation now holds the candidate while the old daemon keeps running.
for (const name of readdirSync(run.install)) rmSync(join(run.install, name), { recursive: true, force: true });
Expand All @@ -304,8 +327,11 @@ async function upgrade(run) {
assert.equal(info.ompweb.protocolVersion, APPLICATION);
assert.equal(info.ompweb.canUpdate, true, info.ompweb.blockedReason);

const accepted = await hub.updateHostSoftware(hostId, "ompweb", repoVersion);
assert.deepEqual({ target: accepted.operation.target, version: accepted.operation.version }, { target: "ompweb", version: repoVersion });
// The request a GUI click sends: nothing of the hub is called directly.
const response = await startUpdate(hostId, repoVersion);
assert.equal(response.status, 202);
const accepted = await response.json();
assert.deepEqual({ accepted: accepted.accepted, target: accepted.operation.target, version: accepted.operation.version }, { accepted: true, target: "ompweb", version: repoVersion });
await waitFor(() => existsSync(join(run.stateRoot, "status.json")) && run.status().operation.state !== "running", "the old updater to finish", run);
const { operation, daemonPid } = run.status();
assert.equal(operation.state, "succeeded", operation.error);
Expand All @@ -318,6 +344,8 @@ async function upgrade(run) {
assert.notEqual(health.pid, daemonPid);
assert.ok(alive(health.pid), "the new daemon keeps running");
assert.equal(read(join(run.install, "package.json")).version, repoVersion);
assert.deepEqual(processesUnder(run.work, "omp-web-host.js"), [health.pid], "the old daemon is gone and exactly one new one runs");
assert.deepEqual(run.sessions(), run.sessionsBefore, "the update never touches the sessions directory");

const backups = readdirSync(dirname(run.install)).filter((name) => name.startsWith(".ompweb-backup-"));
if (layout === "separate") {
Expand All @@ -338,15 +366,18 @@ async function upgrade(run) {
const candidateSource = new RegExp(`^https://raw\\.githubusercontent\\.com/[^/]+/[^/]+/v${repoVersion.replaceAll(".", "\\.")}/lib/hosts/link-protocol\\.ts$`);
assert.ok(run.fetched().some((url) => candidateSource.test(url)), "the old updater read the target release's link protocol");

// Everything the legacy connection ever received: transport and the two update requests, each with the one
// compatibility an application-12 release may be reached through. The v12 welcome ends it.
// Everything the legacy connection ever received. A pre-12 daemon sees only transport and the two update requests;
// the v12 welcome ends its connection. A v12 daemon's connection is a full link that ends with the second v12 welcome.
// In both, each update request carries the one compatibility an application-12 release may be reached through.
const frames = sent();
const upgraded = frames.findIndex((message) => message.type === "welcome" && message.v === APPLICATION);
assert.ok(upgraded > 0, "the upgraded daemon is welcomed at the application version");
const legacyFrames = frames.slice(0, upgraded);
assert.deepEqual([...new Set(legacyFrames.map((message) => message.type))].filter((type) => !["welcome", "ack", "ping", "pong", "req"].includes(type)), []);
const requests = legacyFrames.filter((message) => message.type === "req");
assert.deepEqual([...new Set(requests.map((message) => message.op))].sort(), ["checkUpdates", "updateSoftware"]);
const welcomes = frames.flatMap((message, index) => message.type === "welcome" && message.v === APPLICATION ? [index] : []);
assert.equal(welcomes.length, compatible ? 2 : 1, "the upgraded daemon is welcomed at the application version");
const legacyFrames = frames.slice(0, welcomes.at(-1));
const requests = legacyFrames.filter((message) => message.type === "req" && ["checkUpdates", "updateSoftware"].includes(message.op));
if (!compatible) {
assert.deepEqual([...new Set(legacyFrames.map((message) => message.type))].filter((type) => !["welcome", "ack", "ping", "pong", "req"].includes(type)), []);
assert.deepEqual([...new Set(legacyFrames.filter((message) => message.type === "req").map((message) => message.op))].sort(), ["checkUpdates", "updateSoftware"]);
}
for (const request of requests) assert.deepEqual(request.args.compatibility, { min: APPLICATION, max: APPLICATION });
assert.deepEqual(requests.filter((message) => message.op === "updateSoftware").map((message) => [message.args.target, message.args.version]), [["ompweb", repoVersion]]);
}
Expand All @@ -365,3 +396,5 @@ test("v10 daemon (546a945d) is welcomed as v10 and upgraded by its own updater t
test("v10 daemon (546a945d) on an installation the web package already replaced is restarted, not reinstalled", { timeout: 240_000 }, (t) => scenario(t, LEGACY.v10, "shared"));
test("v11 daemon (v0.7.0) is welcomed as v11 and upgraded by its own updater to a separate v12 install", { timeout: 240_000 }, (t) => scenario(t, LEGACY.v11, "separate"));
test("v11 daemon (v0.7.0) on an installation the web package already replaced is restarted, not reinstalled", { timeout: 240_000 }, (t) => scenario(t, LEGACY.v11, "shared"));
test("v12 daemon (v0.7.1) is welcomed online and updated through the compatible path by its own updater to a separate install", { timeout: 240_000 }, (t) => scenario(t, LEGACY.v12, "separate"));
test("v12 daemon (v0.7.1) on an installation the web package already replaced is restarted, not reinstalled", { timeout: 240_000 }, (t) => scenario(t, LEGACY.v12, "shared"));
Loading