fix(workspace): a new session shows in its workspace

This commit is contained in:
2026-10-02 16:18:00 +07:00
parent f35bf2043f
commit da1b17778c
6 changed files with 118 additions and 8 deletions
+66 -1
View File
@@ -5,7 +5,9 @@
* 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.
* 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>
*/
@@ -198,4 +200,67 @@ describe("run ↔ session binding", () => {
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"));
});
});