Add a weekly digest and report which version of the agent each server runs

- Weekly digest through the notification channels: alerts now, a week of uptime, what happened, disks
  heading for full, expiring secrets and domains, updates waiting, planned maintenance. Off until switched
  on in Settings -> Notifications; Preview and Send it now there. Setting key 'digest'.
- Agents say their version in every report (Linux and Windows scripts are version 2); the server compares
  it with the scripts it serves and marks agents that are behind on the Servers, server and Updates pages.
  servers.agent_version, migration 0026.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-17 19:49:36 +02:00
1 parent 3da5c639b1
commit aa94d84f6c
28 files changed
+3179 -19

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
/**
* Which version of the agent each server runs, and which agents are behind the scripts this app serves.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
const dir = mkdtempSync(join(tmpdir(), "hm-agentver-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { parseAgentVersion, latestAgentVersions, isAgentOutdated } = await import("../src/services/agentVersions.js");
const { serversRouter } = await import("../src/routes/servers.js");
const { agentReportRouter } = await import("../src/routes/agentReport.js");
const { hashToken } = await import("../src/services/tokens.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
describe("reading a version out of a script", () => {
test("each script says what it is, in the form the server looks for", () => {
assert.equal(parseAgentVersion("#!/usr/bin/env bash\nset -e\n\nAGENT_VERSION=7\nENV_FILE=x\n", "linux"), 7);
assert.equal(parseAgentVersion("$script:AgentVersion = 12\r\n$script:Other = 1\r\n", "windows"), 12);
assert.equal(parseAgentVersion("# AGENT_VERSION=3 in a comment\n", "linux"), null, "only a line that is the assignment counts");
assert.equal(parseAgentVersion("no version here", "windows"), null);
});
test("the scripts this app really serves each carry a version", () => {
const latest = latestAgentVersions();
assert.ok(Number.isInteger(latest.linux) && latest.linux! >= 2, `linux ${latest.linux}`);
assert.ok(Number.isInteger(latest.windows) && latest.windows! >= 2, `windows ${latest.windows}`);
});
test("a folder with nothing in it gives no version, not an error", () => {
const empty = mkdtempSync(join(tmpdir(), "hm-noagent-"));
assert.deepEqual(latestAgentVersions(empty), { linux: null, windows: null });
mkdirSync(join(empty, "linux"));
writeFileSync(join(empty, "linux", "report-tasks.sh"), "AGENT_VERSION=5\n");
assert.deepEqual(latestAgentVersions(empty), { linux: 5, windows: null });
rmSync(empty, { recursive: true, force: true });
});
});
describe("which agents are behind", () => {
const latest = { linux: 3, windows: 2 };
const seen = "2026-10-17T00:00:00Z";
test("an agent that says nothing is version 1, and older than the latest is behind", () => {
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: seen, agentVersion: null }, latest), true);
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: seen, agentVersion: 2 }, latest), true);
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: seen, agentVersion: 3 }, latest), false);
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: seen, agentVersion: 4 }, latest), false, "ahead of this app is not behind");
assert.equal(isAgentOutdated({ osType: "windows", lastSeenAt: seen, agentVersion: 2 }, latest), false, "each system has its own latest");
});
test("a server with no agent has nothing to update, and an unknown latest flags nothing", () => {
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: null, agentVersion: null }, latest), false);
assert.equal(isAgentOutdated({ osType: "linux", lastSeenAt: seen, agentVersion: null }, { linux: null, windows: null }), false);
assert.equal(isAgentOutdated({ osType: "plan9", lastSeenAt: seen, agentVersion: null }, latest), false);
});
});
describe("the version in reports", () => {
let app: Server;
let base = "";
let id = 0;
before(async () => {
await db.insert(users).values({ oidcSub: "admin", name: "Ada", role: "admin" });
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t" }).returning();
id = s.id;
const a = express();
a.use("/api/agent/report", agentReportRouter);
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: "admin" } };
next();
});
a.use("/api/servers", serversRouter);
app = await new Promise<Server>((resolve) => {
const x = a.listen(0, "127.0.0.1", () => resolve(x));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const report = (extra: Record<string, unknown>) =>
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", tasks: [], ...extra }) });
test("an agent's version is stored; a report without one, or with nonsense, leaves it as it was", async () => {
assert.equal((await report({})).status, 202);
let [s] = await db.select().from(servers).where(eq(servers.id, id));
assert.equal(s.agentVersion, null, "an old agent says nothing");
assert.equal((await report({ agent_version: 2 })).status, 202);
[s] = await db.select().from(servers).where(eq(servers.id, id));
assert.equal(s.agentVersion, 2);
for (const odd of ["two", 0, -1, 1.5, 999999999, null, {}]) {
assert.equal((await report({ agent_version: odd })).status, 202, `${JSON.stringify(odd)} must not cost the report`);
}
[s] = await db.select().from(servers).where(eq(servers.id, id));
assert.equal(s.agentVersion, 2);
});
test("the lists say what the latest is, and the server's own row says what it runs", async () => {
const list = await (await fetch(`${base}/servers`)).json();
assert.ok(list.agentLatest.linux >= 2 && list.agentLatest.windows >= 2);
assert.equal(list.servers.find((x: { id: number }) => x.id === id).agentVersion, 2);
const detail = await (await fetch(`${base}/servers/${id}/detail`)).json();
assert.equal(detail.server.agentVersion, 2);
assert.ok(detail.agentLatest.linux >= 2);
const updates = await (await fetch(`${base}/servers/updates`)).json();
assert.equal(updates.servers.find((x: { id: number }) => x.id === id).agentVersion, 2);
assert.ok(updates.agentLatest.windows >= 2);
});
});