Check pi liveness per tmux pane and add "Seen" marks to the control board (#1503)

First step-3 refinement from Jason's daily use. Liveness now lists the
panes of the agent's tmux session and counts it alive only if a pane runs
pi, so killed pi sessions whose tmux session still exists show offline
instead of waiting. A "Seen" button on waiting and error rows stores the
row's lastActivity in <dataRoot>/board/seen.json (clicks only, never
rewritten by a scan, fail closed if corrupt) and drops the row from
"Waiting on you" until the agent writes anything newer; "Unsee" reverses
it. New POST /api/seen route: JSON only, 4 KB limit, 400 on bad input.

Tests: control-board 63/63 (30 new), registry 69/69. Review APPROVED;
receipt docs/plans/reviews/2026-09-12_control-board-step3-seen-marks.md.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
2026-09-12 08:24:49 -05:00
co-authored by Claude Fable 5.1
parent ebedd1281e
commit 88d21defde
12 changed files with 777 additions and 21 deletions
+231
View File
@@ -9,6 +9,7 @@ import {
readFileSync,
readdirSync,
existsSync,
statSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve, basename } from "node:path";
@@ -23,6 +24,10 @@ import {
discoverRepoAgents,
discoverFleetAgents,
scan,
panesRunPi,
tmuxIsAlive,
loadSeen,
markSeen,
} from "../src/scan.mjs";
const pkgRoot = resolve(import.meta.dirname, "..");
@@ -464,3 +469,229 @@ test("CLI: unknown --liveness value exits 2", () => {
assert.equal(r.status, 2);
assert.match(r.stderr, /^refused:/);
});
// ---------------------------------------------------------------------------
// 9. panesRunPi / tmuxIsAlive: liveness means a pane actually runs pi
// ---------------------------------------------------------------------------
test("panesRunPi: true when any trimmed line equals 'pi'", () => {
assert.equal(panesRunPi("bash\npi\n"), true);
assert.equal(panesRunPi("pi"), true);
assert.equal(panesRunPi(" pi \n"), true);
});
test("panesRunPi: false for bash-only, claude, empty, or node-pi-style lines", () => {
assert.equal(panesRunPi("bash"), false);
assert.equal(panesRunPi("claude"), false);
assert.equal(panesRunPi(""), false);
assert.equal(panesRunPi("node pi"), false);
});
function fakeExec(result) {
const calls = [];
const exec = (cmd, args, opts) => {
calls.push({ cmd, args, opts });
return result;
};
exec.calls = calls;
return exec;
}
test("tmuxIsAlive: a pane running pi is alive", () => {
const exec = fakeExec({ status: 0, stdout: "bash\npi\n" });
assert.equal(tmuxIsAlive({ socket: null, session: "a" }, { exec }), true);
});
test("tmuxIsAlive: session exists but pi has exited is not alive", () => {
const exec = fakeExec({ status: 0, stdout: "bash\n" });
assert.equal(tmuxIsAlive({ socket: null, session: "a" }, { exec }), false);
});
test("tmuxIsAlive: no such tmux session is not alive", () => {
const exec = fakeExec({ status: 1, stdout: "" });
assert.equal(tmuxIsAlive({ socket: null, session: "a" }, { exec }), false);
});
test("tmuxIsAlive: tmux could not be run at all is unknown (null), never assumed alive", () => {
const exec = fakeExec({ error: new Error("ENOENT") });
assert.equal(tmuxIsAlive({ socket: null, session: "a" }, { exec }), null);
});
test("tmuxIsAlive: passes -L <socket> only when a socket is given", () => {
const withSocket = fakeExec({ status: 0, stdout: "pi\n" });
tmuxIsAlive({ socket: "mosaic-fleet", session: "name" }, { exec: withSocket });
assert.equal(withSocket.calls[0].cmd, "tmux");
assert.deepEqual(withSocket.calls[0].args, ["-L", "mosaic-fleet", "list-panes", "-s", "-t", "=name", "-F", "#{pane_current_command}"]);
const noSocket = fakeExec({ status: 0, stdout: "pi\n" });
tmuxIsAlive({ socket: null, session: "name" }, { exec: noSocket });
assert.deepEqual(noSocket.calls[0].args, ["list-panes", "-s", "-t", "=name", "-F", "#{pane_current_command}"]);
});
// ---------------------------------------------------------------------------
// 10. Seen marks: loadSeen / markSeen
// ---------------------------------------------------------------------------
test("loadSeen: missing file returns {}", () => {
const root = makeRoot();
assert.deepEqual(loadSeen(root), {});
});
test("loadSeen: invalid JSON throws ConfigError", () => {
const root = makeRoot();
writeFile(join(root, "seen.json"), "{ not json");
assert.throws(() => loadSeen(root), ConfigError);
});
test("loadSeen: a JSON array throws ConfigError", () => {
const root = makeRoot();
writeFile(join(root, "seen.json"), "[]");
assert.throws(() => loadSeen(root), ConfigError);
});
test("loadSeen: a non-string value throws ConfigError", () => {
const root = makeRoot();
writeFile(join(root, "seen.json"), JSON.stringify({ "p/a": 123 }));
assert.throws(() => loadSeen(root), ConfigError);
});
test("markSeen: seen true adds the key and writes seen.json mode 0600, no leftover tmp files", () => {
const root = makeRoot();
const boardDir = join(root, "board");
const marks = markSeen(boardDir, { project: "p", agent: "a", lastActivity: "2026-09-01T00:00:00Z", seen: true });
assert.deepEqual(marks, { "p/a": "2026-09-01T00:00:00Z" });
const path = join(boardDir, "seen.json");
assert.ok(existsSync(path));
assert.equal(statSync(path).mode & 0o777, 0o600);
const names = readdirSync(boardDir);
assert.ok(!names.some((n) => n.includes(".tmp-")), `leftover tmp file among: ${names.join(", ")}`);
});
test("markSeen: seen false deletes the key", () => {
const root = makeRoot();
const boardDir = join(root, "board");
markSeen(boardDir, { project: "p", agent: "a", lastActivity: "t1", seen: true });
const marks = markSeen(boardDir, { project: "p", agent: "a", lastActivity: "t1", seen: false });
assert.deepEqual(marks, {});
});
test("markSeen: missing, empty, or non-string fields throw ConfigError", () => {
const root = makeRoot();
const boardDir = join(root, "board");
assert.throws(() => markSeen(boardDir, { project: "p", agent: "a", lastActivity: "" }), ConfigError);
assert.throws(() => markSeen(boardDir, { project: "p", agent: "a" }), ConfigError);
assert.throws(() => markSeen(boardDir, { project: "p", agent: 5, lastActivity: "t" }), ConfigError);
assert.throws(() => markSeen(boardDir, { agent: "a", lastActivity: "t" }), ConfigError);
});
test("markSeen: project containing '/' throws ConfigError", () => {
const root = makeRoot();
const boardDir = join(root, "board");
assert.throws(() => markSeen(boardDir, { project: "p/x", agent: "a", lastActivity: "t" }), ConfigError);
});
test("markSeen: non-boolean seen throws ConfigError", () => {
const root = makeRoot();
const boardDir = join(root, "board");
assert.throws(() => markSeen(boardDir, { project: "p", agent: "a", lastActivity: "t", seen: "true" }), ConfigError);
});
// ---------------------------------------------------------------------------
// 11. scanAgent: seen marks apply only to waiting/error, and only while the
// mark's timestamp still matches the agent's newest message
// ---------------------------------------------------------------------------
test("scanAgent: a seen mark matching the waiting session's lastTimestamp clears waitingOnYou", () => {
const root = makeRoot();
const sessionsDir = join(root, "sessions");
mkdirSync(sessionsDir, { recursive: true });
writeSessionFile(sessionsDir, "s.jsonl", [
sessionLine({ id: "s1", timestamp: "2026-09-01T00:00:00Z", cwd: "/w" }),
messageLine({ timestamp: "2026-09-01T00:00:01Z", role: "assistant", stopReason: "stop", texts: ["done"] }),
]);
const seen = { "p/a": "2026-09-01T00:00:01Z" };
const rec = scanAgent({ agent: "a", project: "p", sessionsDir, tmux: {} }, { isAlive: () => true, now: () => new Date("2026-09-01T00:01:00Z"), seen });
assert.equal(rec.state, "waiting");
assert.equal(rec.seen, true);
assert.equal(rec.waitingOnYou, false);
});
test("scanAgent: a stale mark (agent wrote something newer) is not seen and waitingOnYou is true", () => {
const root = makeRoot();
const sessionsDir = join(root, "sessions");
mkdirSync(sessionsDir, { recursive: true });
writeSessionFile(sessionsDir, "s.jsonl", [
sessionLine({ id: "s1", timestamp: "2026-09-01T00:00:00Z", cwd: "/w" }),
messageLine({ timestamp: "2026-09-01T00:00:01Z", role: "assistant", stopReason: "stop", texts: ["done"] }),
]);
const seen = { "p/a": "2026-09-01T00:00:00Z" }; // stale: older than lastTimestamp
const rec = scanAgent({ agent: "a", project: "p", sessionsDir, tmux: {} }, { isAlive: () => true, now: () => new Date("2026-09-01T00:01:00Z"), seen });
assert.equal(rec.state, "waiting");
assert.equal(rec.seen, false);
assert.equal(rec.waitingOnYou, true);
});
test("scanAgent: a working session with a matching mark is not seen (marks only apply to waiting/error)", () => {
const root = makeRoot();
const sessionsDir = join(root, "sessions");
mkdirSync(sessionsDir, { recursive: true });
writeSessionFile(sessionsDir, "s.jsonl", [
sessionLine({ id: "s1", timestamp: "2026-09-01T00:00:00Z", cwd: "/w" }),
messageLine({ timestamp: "2026-09-01T00:00:01Z", role: "assistant", stopReason: "toolUse", texts: ["thinking"] }),
]);
const seen = { "p/a": "2026-09-01T00:00:01Z" };
const rec = scanAgent({ agent: "a", project: "p", sessionsDir, tmux: {} }, { isAlive: () => true, now: () => new Date("2026-09-01T00:01:00Z"), seen });
assert.equal(rec.state, "working");
assert.equal(rec.seen, false);
assert.equal(rec.waitingOnYou, false);
});
test("scanAgent: an error-state session with a matching mark is seen", () => {
const root = makeRoot();
const sessionsDir = join(root, "sessions");
mkdirSync(sessionsDir, { recursive: true });
writeSessionFile(sessionsDir, "s.jsonl", [
sessionLine({ id: "s1", timestamp: "2026-09-01T00:00:00Z", cwd: "/w" }),
messageLine({ timestamp: "2026-09-01T00:00:01Z", role: "assistant", stopReason: "aborted" }),
]);
const seen = { "p/a": "2026-09-01T00:00:01Z" };
const rec = scanAgent({ agent: "a", project: "p", sessionsDir, tmux: {} }, { isAlive: () => true, now: () => new Date("2026-09-01T00:01:00Z"), seen });
assert.equal(rec.state, "error");
assert.equal(rec.seen, true);
assert.equal(rec.waitingOnYou, false);
});
// ---------------------------------------------------------------------------
// 12. scan: reads seen.json, index reflects it, and scanning never rewrites it
// ---------------------------------------------------------------------------
test("scan: index.seen and waitingOnYou reflect seen.json, which scan never rewrites or deletes", () => {
const root = makeRoot();
const boardDir = join(root, "board");
const sessionsDir = join(root, "sessions");
mkdirSync(sessionsDir, { recursive: true });
const lastActivity = "2026-09-01T00:00:01Z";
writeSessionFile(sessionsDir, "s.jsonl", [
sessionLine({ id: "s1", timestamp: "2026-09-01T00:00:00Z", cwd: "/w" }),
messageLine({ timestamp: lastActivity, role: "assistant", stopReason: "stop", texts: ["done"] }),
]);
mkdirSync(boardDir, { recursive: true });
const seenPath = join(boardDir, "seen.json");
writeFile(seenPath, JSON.stringify({ "p/agent1": lastActivity }));
const before = readFileSync(seenPath);
const index = scan([{ agent: "agent1", project: "p", sessionsDir, tmux: {} }], { boardDir, isAlive: () => true, now: () => new Date("2026-09-01T00:01:00Z") });
assert.deepEqual(index.seen, ["p/agent1"]);
assert.deepEqual(index.waitingOnYou, []);
const after = readFileSync(seenPath);
assert.deepEqual(before, after, "scan must never rewrite seen.json");
});
test("scan: a corrupt seen.json makes scan throw ConfigError (fail closed)", () => {
const root = makeRoot();
const boardDir = join(root, "board");
mkdirSync(boardDir, { recursive: true });
writeFile(join(boardDir, "seen.json"), "{ bad json");
assert.throws(() => scan([], { boardDir, isAlive: () => true }), ConfigError);
});