diff --git a/CHANGELOG.md b/CHANGELOG.md index bba6ee6..6fea983 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ Each package and app has its own section and its own tags (`contracts@1.0.0`, `accounts@1.0.0`, …). +## agent@1.1.1 - 2026-11-18 + +- **Stats keep going while a job runs.** The agent sent stats and polled in one loop, so during + a job (an install or update can take 30 minutes) the Hub heard nothing and alerted, by email, + that the server was down. Stats now go every minute on their own timer, job or not (Hub 2.3.1 + also treats a server running a job as busy, for agents not updated yet). +- The agent reports its real version: 1.1.0 still said 1.0.1 (a test now keeps it equal to + package.json). + ## accounts@1.0.0, contracts@1.5.0, agent@1.1.0 - 2026-11-18 Phase 3 (docs/ACCOUNTS-1.0-PLAN.md). Released before the VM test and a run on real Postgres (both still to do). diff --git a/apps/agent/package.json b/apps/agent/package.json index 39cf93f..3bdaaf3 100644 --- a/apps/agent/package.json +++ b/apps/agent/package.json @@ -1,6 +1,6 @@ { "name": "@crosscode/agent", - "version": "1.1.0", + "version": "1.1.1", "private": true, "description": "The server agent: runs the crosscode script for signed jobs from Cross Code Hub, reports stats (docs/AGENT-1.0-PLAN.md)", "license": "UNLICENSED", diff --git a/apps/agent/src/loop.ts b/apps/agent/src/loop.ts index 17d19aa..d4b1fc1 100644 --- a/apps/agent/src/loop.ts +++ b/apps/agent/src/loop.ts @@ -214,17 +214,33 @@ export class Agent { } } - /** Runs until stopped: polls and stats on their own timers, never at the same time as a job. */ + /** + * Runs until stopped. Stats go on their own timer, every `statsSeconds`, also while a job runs: + * an install or update can take 30 minutes, and before 1.1.1 the agent went quiet for all of it, + * so the Hub reported it as down (and emailed) though it was busy. Polls wait for the job. + */ async start(): Promise { log(`[agent] ${AGENT_VERSION} (${this.deps.config.mode}) for ${this.deps.config.serverId}, Hub ${this.deps.config.hubUrl}`); - let nextStats = 0; - while (!this.stopped) { - if (Date.now() >= nextStats) { + let sending = false; + const statsTick = async () => { + if (sending || this.stopped) return; + sending = true; + try { await this.statsOnce(); - nextStats = Date.now() + this.deps.config.statsSeconds * 1000; + } finally { + sending = false; } - const delay = await this.pollOnce(); - await new Promise((r) => setTimeout(r, Math.max(5, Math.min(delay, this.deps.config.statsSeconds)) * 1000)); + }; + void statsTick(); + const timer = setInterval(() => void statsTick(), this.deps.config.statsSeconds * 1000); + try { + while (!this.stopped) { + const delay = await this.pollOnce(); + if (this.stopped) break; + await new Promise((r) => setTimeout(r, Math.max(5, delay) * 1000)); + } + } finally { + clearInterval(timer); } } diff --git a/apps/agent/src/stats.ts b/apps/agent/src/stats.ts index 3f0ed97..36b8d53 100644 --- a/apps/agent/src/stats.ts +++ b/apps/agent/src/stats.ts @@ -12,7 +12,7 @@ import { paths } from "./paths"; * the tools that are there; anything missing is null rather than an error. */ -export const AGENT_VERSION = "1.0.1"; +export const AGENT_VERSION = "1.1.1"; function run(cmd: string, args: string[], timeoutMs = 10_000): Promise { return new Promise((resolve) => { diff --git a/apps/agent/tests/command.test.ts b/apps/agent/tests/command.test.ts index aa4b091..40f6779 100644 --- a/apps/agent/tests/command.test.ts +++ b/apps/agent/tests/command.test.ts @@ -49,3 +49,12 @@ describe("crosscode arguments for each job", () => { expect(crosscodeArgs(parsed({ type: "update-agent", args: { version: "agent@1.0.1" } }))).toBeNull(); }); }); + +describe("the agent's version", () => { + it("is the one in package.json (it's what the Hub shows and compares)", () => { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const pkg = require("../package.json") as { version: string }; + // eslint-disable-next-line @typescript-eslint/no-require-imports + expect((require("../src/stats") as { AGENT_VERSION: string }).AGENT_VERSION).toBe(pkg.version); + }); +}); diff --git a/apps/agent/tests/loop.test.ts b/apps/agent/tests/loop.test.ts index 0cd986d..b596595 100644 --- a/apps/agent/tests/loop.test.ts +++ b/apps/agent/tests/loop.test.ts @@ -42,6 +42,32 @@ function make(jobs: unknown[], run: jest.Mock, Parameters> const reports = (posts: { path: string; body: unknown }[]) => posts.filter((p) => p.path === "/api/agent/v1/report").map((p) => p.body as agent.AgentReport); describe("the agent loop", () => { + it("keeps sending stats while a long job runs, so the Hub doesn't think it's down", async () => { + jest.useFakeTimers(); + try { + let finish!: () => void; + const longRun = jest.fn, Parameters>(() => new Promise((resolve) => { finish = () => resolve(ok()); })); + const h = fakeHub([job()]); + const a = new Agent({ config, keys: { ed25519Pem: serverKeys.ed25519.privateKeyPem, x25519Pem: serverKeys.x25519.privateKeyPem }, hub: h.hub, seen: new SeenJobs(path.join(state, `seen-${Math.random()}.json`)), run: longRun, now: () => NOW, stats: async () => ({}) as agent.AgentStats }); + const running = a.start(); + await jest.advanceTimersByTimeAsync(10); + expect(longRun).toHaveBeenCalledTimes(1); + // 20 minutes of a job: a stats post every minute all the way through. + await jest.advanceTimersByTimeAsync(20 * 60 * 1000); + const stats = h.posts.filter((p) => p.path === "/api/agent/v1/stats").length; + expect(stats).toBeGreaterThanOrEqual(20); + expect(reports(h.posts)).toHaveLength(0); + finish(); + await jest.advanceTimersByTimeAsync(10); + expect(reports(h.posts)).toHaveLength(1); + a.stop(); + await jest.advanceTimersByTimeAsync(60 * 1000); + await running; + } finally { + jest.useRealTimers(); + } + }); + it("runs a valid job once and reports it", async () => { const j = job(); const { a, run, posts } = make([j, j]); diff --git a/package-lock.json b/package-lock.json index a4b9a96..b7a3c9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -108,7 +108,7 @@ }, "apps/agent": { "name": "@crosscode/agent", - "version": "1.1.0", + "version": "1.1.1", "license": "UNLICENSED", "dependencies": { "@crosscode/contracts": "*",