Compare commits

...
Author SHA1 Message Date
joshuaandClaude Opus 5.5 7fd699a2fd Release the crosscode script fix: failed installs show their PM2 logs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 09:03:48 +02:00
joshuaandClaude Opus 5.5 6988b5ce7c Scripts: a failed install shows its PM2 apps' last log lines
Before removing what a failed install made, print each PM2 app's last
30 log lines, so the Hub's job log says why the backend didn't start
instead of only "/health didn't report 2.3.1 within 90s".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 08:44:38 +02:00
joshua 06222fd94a Merge branch 'fix/agent-stats-during-jobs' 2026-11-18 19:03:20 +02:00
joshuaandClaude Opus 5.5 43189c599a Release agent@1.1.1: stats during jobs, its real version
Stats go every minute on their own timer, also while a job runs (before,
a 30-minute install made the Hub alert that the server was down). The
version constant said 1.0.1 in 1.1.0; a test keeps it equal to
package.json now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 19:03:19 +02:00
9 changed files with 109 additions and 11 deletions
+16
View File
@@ -2,6 +2,22 @@
Each package and app has its own section and its own tags (`contracts@1.0.0`, `accounts@1.0.0`, …).
## crosscode script - 2026-11-19 (no tag: servers run `main`)
- **Scripts: a failed install says why its apps didn't start.** Before removing what a failed
install made, `crosscode install` / `crosscode add` print each PM2 app's last 30 log lines. The
logs used to go with the apps, so the Hub only showed "/health didn't report 2.3.1 within 90s
(last answer: none)" and the reason was lost.
## 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).
+1 -1
View File
@@ -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",
+23 -7
View File
@@ -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<void> {
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);
}
}
+1 -1
View File
@@ -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<string | null> {
return new Promise((resolve) => {
+9
View File
@@ -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);
});
});
+26
View File
@@ -42,6 +42,32 @@ function make(jobs: unknown[], run: jest.Mock<ReturnType<Run>, Parameters<Run>>
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<ReturnType<Run>, Parameters<Run>>(() => new Promise<RunResult>((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]);
+1 -1
View File
@@ -108,7 +108,7 @@
},
"apps/agent": {
"name": "@crosscode/agent",
"version": "1.1.0",
"version": "1.1.1",
"license": "UNLICENSED",
"dependencies": {
"@crosscode/contracts": "*",
+15 -1
View File
@@ -58,12 +58,26 @@ random_secret() { openssl rand -base64 64 | tr -d '/+=\n' | head -c "${1:-48}";
INSTALL_UNDO=""
undo_add() { [ -z "$INSTALL_UNDO" ] || printf '%s\n' "$*" >> "$INSTALL_UNDO"; }
# pm2_last_logs <user> <name…>: each PM2 app's last 30 log lines (output and errors), indented.
pm2_last_logs() {
local user="$1" name
shift
for name in "$@"; do
info "PM2: last log lines of $name"
run_as "$user" / pm2 logs "$name" --lines 30 --nostream --raw </dev/null 2>&1 | tail -n 60 | sed 's/^/ /' || true
done
}
install_undo() {
local kind a b
step "Removing what this install made"
tac "$INSTALL_UNDO" | while read -r kind a b; do
case "$kind" in
pm2) # shellcheck disable=SC2086 # b is a space-separated list of PM2 names
pm2)
# Their logs go with them, so show why they didn't start first (the Hub gets the log).
# shellcheck disable=SC2086 # b is a space-separated list of PM2 names
pm2_last_logs "$a" $b
# shellcheck disable=SC2086
run_as "$a" / pm2 delete $b >/dev/null 2>&1 || true
run_as "$a" / pm2 save --force >/dev/null 2>&1 || true
info "PM2: $b removed" ;;
+17
View File
@@ -259,3 +259,20 @@ EOF
[ "$(host_from_url "$(env_value "$TMP/.env" FRONTEND_URL)")" = events.hopefamily.co.za ]
[ "$(host_from_url http://localhost:3000/x)" = localhost ]
}
# ── install undo ────────────────────────────────────────────────────────────────────────────
@test "a failed install shows each PM2 app's last log lines before removing it" {
# shellcheck source=../lib/install.sh
source "$BATS_TEST_DIRNAME/../lib/install.sh"
run_as() { shift 2; case "$2" in logs) printf 'Error: %s crashed\n' "$3" ;; esac; echo "$*" >> "$TMP/pm2"; }
INSTALL_UNDO="$TMP/undo"
undo_add pm2 cc-hope "hope-events-backend hope-events-frontend"
run install_undo
[ "$status" -eq 0 ]
[[ "$output" == *"last log lines of hope-events-backend"* ]]
[[ "$output" == *" Error: hope-events-backend crashed"* ]]
[[ "$output" == *" Error: hope-events-frontend crashed"* ]]
# The logs are read before the apps are deleted.
[ "$(grep -n 'logs hope-events-backend' "$TMP/pm2" | cut -d: -f1)" -lt "$(grep -n '^pm2 delete' "$TMP/pm2" | cut -d: -f1)" ]
}