267 lines
9.8 KiB
JavaScript
267 lines
9.8 KiB
JavaScript
/**
|
|
* @file Tests that a dashboard pane learns which session it holds: every pane
|
|
* is started with `AGENTHUB_RUN_ID` in its tmux env, and a hook carrying that
|
|
* `run_id` binds the hook's session to the run (`dashboard_runs.session_id`),
|
|
* following `/clear` to a new session; a `run_id` that is not a dashboard pane
|
|
* name, or names no run, changes nothing. A session a lane's pane holds is
|
|
* stamped with that lane (`sessions.lane_id`) — the workspace owns it — and a
|
|
* nested CLI's session, or one another lane already owns, is not. A new
|
|
* Claude pane is started with `--session-id`, so a hook without `run_id` is
|
|
* still stamped from the run that holds its session.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const { describe, it, before, after } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const http = require("node:http");
|
|
|
|
const ROOT = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "run-bind-")));
|
|
process.env.DASHBOARD_DB_PATH = path.join(ROOT, "t.db");
|
|
process.env.CLAUDE_HOME = path.join(ROOT, "claude");
|
|
process.env.DASHBOARD_DATA_DIR = path.join(ROOT, "data");
|
|
process.env.DASHBOARD_REMOTE_SYNC_MS = "0";
|
|
process.env.DASHBOARD_LIVENESS_PROBE = "0";
|
|
|
|
const { createApp } = require("../index");
|
|
const { db } = require("../db");
|
|
const dashboardRuns = require("../lib/dashboard-runs");
|
|
const tmux = require("../lib/tmux");
|
|
const pty = require("../lib/pty-run");
|
|
|
|
let server;
|
|
let base;
|
|
|
|
function postHook(body) {
|
|
return new Promise((resolve, reject) => {
|
|
const data = Buffer.from(JSON.stringify(body));
|
|
const req = http.request(
|
|
`${base}/api/hooks/event`,
|
|
{ method: "POST", headers: { "content-type": "application/json" } },
|
|
(res) => {
|
|
res.resume();
|
|
res.on("end", () => resolve(res.statusCode));
|
|
}
|
|
);
|
|
req.on("error", reject);
|
|
req.end(data);
|
|
});
|
|
}
|
|
|
|
before(async () => {
|
|
fs.mkdirSync(process.env.CLAUDE_HOME, { recursive: true });
|
|
server = http.createServer(createApp());
|
|
await new Promise((r) => server.listen(0, "127.0.0.1", r));
|
|
base = `http://127.0.0.1:${server.address().port}`;
|
|
});
|
|
|
|
after(() => {
|
|
server?.close();
|
|
fs.rmSync(ROOT, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("run ↔ session binding", () => {
|
|
it("starts every pane with AGENTHUB_RUN_ID naming that pane", () => {
|
|
let argv = null;
|
|
tmux.__setExecImpl((args) => {
|
|
if (args[0] === "has-session") throw Object.assign(new Error("gone"), { status: 1 });
|
|
if (args[0] === "new-session") argv = args;
|
|
return "";
|
|
});
|
|
pty.spawnRun({ laneId: 77, slot: 2, cwd: ROOT });
|
|
tmux.__reset();
|
|
const env = argv.filter((a, i) => argv[i - 1] === "-e");
|
|
assert.ok(env.includes("AGENTHUB_RUN_ID=agenthub-lane-77-2"), env.join(" "));
|
|
});
|
|
|
|
it("binds a hook's session to the run it names, and follows a new session", async () => {
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-5-1",
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
const hook = (session_id, run_id) =>
|
|
postHook({ hook_type: "SessionStart", data: { session_id, cwd: ROOT }, run_id });
|
|
assert.equal(await hook("sess-aaaa-1111", "agenthub-lane-5-1"), 200);
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-5-1").session_id, "sess-aaaa-1111");
|
|
// `/clear` in the pane: a new session that is the pane's now.
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "sess-bbbb-2222", cwd: ROOT, source: "clear" },
|
|
run_id: "agenthub-lane-5-1",
|
|
});
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-5-1").session_id, "sess-bbbb-2222");
|
|
});
|
|
|
|
it("keeps the pane's session when a nested CLI inside it starts its own", async () => {
|
|
// A `claude -p` the agent runs through Bash inherits AGENTHUB_RUN_ID.
|
|
for (const hook_type of ["SessionStart", "UserPromptSubmit", "Stop"]) {
|
|
await postHook({
|
|
hook_type,
|
|
data: { session_id: "sess-nest-9999", cwd: ROOT, source: "startup" },
|
|
run_id: "agenthub-lane-5-1",
|
|
});
|
|
}
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-5-1").session_id, "sess-bbbb-2222");
|
|
});
|
|
|
|
it("follows any new session on a non-claude pane, whose hooks carry no reliable source", async () => {
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-6-1",
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
const hook = (session_id) =>
|
|
postHook({
|
|
hook_type: "UserPromptSubmit",
|
|
tool: "codex",
|
|
data: { session_id, cwd: ROOT, prompt: "hi" },
|
|
run_id: "agenthub-lane-6-1",
|
|
});
|
|
await hook("codex-thread-aaaa");
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-6-1").session_id, "codex-thread-aaaa");
|
|
await hook("codex-thread-bbbb");
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-6-1").session_id, "codex-thread-bbbb");
|
|
});
|
|
|
|
it("ignores a run_id that is not a dashboard pane, or names no run", async () => {
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "sess-cccc-3333", cwd: ROOT },
|
|
run_id: "main",
|
|
});
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "sess-cccc-3333", cwd: ROOT },
|
|
run_id: "agenthub-lane-999",
|
|
});
|
|
assert.equal(dashboardRuns.getRun("main"), null);
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-999"), null);
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-5-1").session_id, "sess-bbbb-2222");
|
|
});
|
|
|
|
it("stamps the session a lane's pane holds with that lane, once", async () => {
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-8",
|
|
laneId: 8,
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
const laneOf = (id) => db.prepare("SELECT lane_id FROM sessions WHERE id = ?").get(id)?.lane_id;
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-aaaa-0001", cwd: ROOT, source: "startup" },
|
|
run_id: "agenthub-lane-8",
|
|
});
|
|
assert.equal(laneOf("own-aaaa-0001"), 8);
|
|
// A nested `claude -p` in that pane is not the pane's session.
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-nest-0002", cwd: ROOT, source: "startup" },
|
|
run_id: "agenthub-lane-8",
|
|
});
|
|
assert.equal(laneOf("own-nest-0002"), null);
|
|
// A session another lane owns keeps its owner when resumed elsewhere.
|
|
db.prepare("UPDATE sessions SET lane_id = 3 WHERE id = ?").run("own-nest-0002");
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-nest-0002", cwd: ROOT, source: "resume" },
|
|
run_id: "agenthub-lane-8",
|
|
});
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-8").session_id, "own-nest-0002");
|
|
assert.equal(laneOf("own-nest-0002"), 3);
|
|
});
|
|
|
|
it("stamps a resumed session the run was started with", async () => {
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-9",
|
|
laneId: 9,
|
|
sessionId: "own-resu-0003",
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-resu-0003", cwd: ROOT, source: "resume" },
|
|
run_id: "agenthub-lane-9",
|
|
});
|
|
assert.equal(
|
|
db.prepare("SELECT lane_id FROM sessions WHERE id = ?").get("own-resu-0003").lane_id,
|
|
9
|
|
);
|
|
});
|
|
|
|
it("starts a new claude session with --session-id and records that id on the run", () => {
|
|
let argv = null;
|
|
tmux.__setExecImpl((args) => {
|
|
if (args[0] === "has-session") throw Object.assign(new Error("gone"), { status: 1 });
|
|
if (args[0] === "new-session") argv = args;
|
|
return "";
|
|
});
|
|
pty.spawnRun({ laneId: 78, cwd: ROOT, initialPrompt: "hi" });
|
|
tmux.__reset();
|
|
const at = argv.indexOf("--session-id");
|
|
assert.ok(at > 0, argv.join(" "));
|
|
const id = argv[at + 1];
|
|
assert.match(id, /^[0-9a-f-]{36}$/);
|
|
assert.equal(dashboardRuns.getRun("agenthub-lane-78").session_id, id);
|
|
});
|
|
|
|
it("stamps the session a run was started holding, also from a hook with no run_id", async () => {
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-10",
|
|
laneId: 10,
|
|
sessionId: "own-norun-0004",
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
// An older hook handler that drops AGENTHUB_RUN_ID.
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-norun-0004", cwd: ROOT, source: "startup" },
|
|
});
|
|
await postHook({
|
|
hook_type: "SessionStart",
|
|
data: { session_id: "own-stray-0005", cwd: ROOT, source: "startup" },
|
|
});
|
|
const laneOf = (id) => db.prepare("SELECT lane_id FROM sessions WHERE id = ?").get(id).lane_id;
|
|
assert.equal(laneOf("own-norun-0004"), 10);
|
|
// A session no run holds stays unowned.
|
|
assert.equal(laneOf("own-stray-0005"), null);
|
|
});
|
|
it("lists a session its run holds before anything stamped it (hooks went to another dashboard)", async () => {
|
|
// Another dashboard on the same DB took the hooks and stamped nothing.
|
|
db.prepare(
|
|
"INSERT INTO sessions (id, name, status, cwd) VALUES ('held-0006', 'x', 'active', ?)"
|
|
).run(ROOT);
|
|
dashboardRuns.recordRun({
|
|
id: "agenthub-lane-11-2",
|
|
laneId: 11,
|
|
sessionId: "held-0006",
|
|
mode: "interactive",
|
|
cwd: ROOT,
|
|
status: "running",
|
|
startedAt: Date.now(),
|
|
});
|
|
const ids = async (lane) =>
|
|
(await (await fetch(`${base}/api/sessions?lane=${lane}`)).json()).sessions.map((x) => x.id);
|
|
assert.ok((await ids(11)).includes("held-0006"));
|
|
assert.ok(!(await ids(12)).includes("held-0006"));
|
|
// An owner already stamped wins over the run record.
|
|
db.prepare("UPDATE sessions SET lane_id = 12 WHERE id = 'held-0006'").run();
|
|
assert.ok(!(await ids(11)).includes("held-0006"));
|
|
});
|
|
});
|