Files
loki/scripts/test/run-tab.ts
T
CatoandClaude Opus 5.5 14cd5ae93c fix(terminal): a parallel run's tab reads as its project, and its rail finds its run (#911)
Since #900 an extra concurrent run gets the tab alias <project>~<runId8>.
The terminal context matched tabs to projects by raw name, so the alias
matched nothing: the strip, page header and composer showed
"skif~d0a14b69", and the Loki rail said "No run on skif~d0a14b69 yet"
beside an agent nine minutes into its work (2026-09-25).

- terminal context resolves tabs via baseProjectKey (the run-tab SSOT)
- getLatestRunForProjectKey understands a lane tab: base project AND the
  run its suffix names (runLaneOfTab), so parallel lanes don't swap runs;
  a malformed suffix is rejected, never a LIKE wildcard
- the strip labels a lane "Skif · parallel", with the alias in the tooltip

Tests: run-tab (lane parse, malformed suffixes, context uses the SSOT).

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 05:12:11 -07:00

113 lines
4.4 KiB
TypeScript

/**
* Regression net for derived run-tabs (src/lib/run-tab.ts) — the identity
* primitive of same-project parallel dispatch (phase 2 of worktree-per-agent).
*
* Everything in the dispatch loop is tab-keyed; parallel runs work by minting
* a unique alias per extra run. These checks lock the alias algebra both the
* server (dispatch route, ingestion, close-sweep) and the desktop runner
* (force-isolation guard) depend on staying in agreement.
*
* Run: npx tsx scripts/test/run-tab.ts
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
deriveRunTab,
baseProjectKey,
isDerivedRunTab,
runLaneOfTab,
RUN_TAB_SEPARATOR,
} from "@/lib/run-tab";
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
check("derive → base round-trips to the project key", () => {
const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45-6789-abcd-ef0123456789");
assert(baseProjectKey(tab) === "kivvi", `round-trip broke: ${tab}`);
});
check("alias embeds a compact runId suffix (dashes stripped, 8 chars)", () => {
const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45-6789-abcd-ef0123456789");
assert(tab === `kivvi${RUN_TAB_SEPARATOR}0b9c1d2e`, `unexpected alias: ${tab}`);
});
check("two runs of one project get distinct aliases (no coordination needed)", () => {
const a = deriveRunTab("kivvi", "aaaaaaaa-1111-2222-3333-444444444444");
const b = deriveRunTab("kivvi", "bbbbbbbb-1111-2222-3333-444444444444");
assert(a !== b, "aliases collided");
assert(baseProjectKey(a) === baseProjectKey(b), "aliases lost their base");
});
check("isDerivedRunTab: true for aliases, false for plain tabs", () => {
assert(isDerivedRunTab(deriveRunTab("revampit", "abc123")), "alias not detected");
assert(!isDerivedRunTab("revampit"), "plain tab misdetected");
assert(!isDerivedRunTab("revamp-it"), "hyphenated plain tab misdetected");
assert(!isDerivedRunTab("Tab With Spaces"), "spaced plain tab misdetected");
});
check("base of a plain tab is itself (safe to apply everywhere)", () => {
assert(baseProjectKey("orangecat") === "orangecat", "identity broken");
assert(baseProjectKey("Tab With Spaces") === "Tab With Spaces", "identity broken for spaces");
});
check("base takes the FIRST separator (suffix content can never smuggle a fake base)", () => {
const weird = `proj${RUN_TAB_SEPARATOR}abc${RUN_TAB_SEPARATOR}def`;
assert(baseProjectKey(weird) === "proj", "multi-separator base wrong");
});
check("short/odd runIds still produce a usable alias", () => {
const t1 = deriveRunTab("p", "ab");
assert(t1 === `p${RUN_TAB_SEPARATOR}ab` && isDerivedRunTab(t1), "short runId broke alias");
const t2 = deriveRunTab("p", "---");
assert(
t2 === `p${RUN_TAB_SEPARATOR}run` && baseProjectKey(t2) === "p",
"dash-only runId broke alias",
);
});
check("alias is filesystem-safe for the session file convention (<tab>.md)", () => {
const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45");
assert(!/[/\\\0]/.test(tab), "alias contains path-hostile characters");
});
check("a lane tab names its project AND its run, so a lookup can find both", () => {
// 2026-09-25: the Terminal rail looked up "skif~d0a14b69" as a project key,
// found nothing, and said "No run… yet" beside an agent nine minutes in.
const runId = "d0a14b69-1234-4abc-9def-001122334455";
const lane = runLaneOfTab(deriveRunTab("Skif", runId));
assert(lane?.project === "Skif", `lane lost its project: ${JSON.stringify(lane)}`);
assert(
runId.replace(/-/g, "").startsWith(lane?.runPrefix ?? "✗"),
`lane prefix does not match its run: ${JSON.stringify(lane)}`,
);
assert(runLaneOfTab("Skif") === null, "a plain tab is not a lane");
});
check("a malformed suffix never becomes a wildcard in a query", () => {
for (const bad of ["p~", "p~%", "p~_x", "p~run", "p~' OR 1=1", "p~abc"]) {
assert(runLaneOfTab(bad) === null, `"${bad}" was accepted as a run prefix`);
}
});
check("the terminal resolves a lane tab to its project, not to nothing", () => {
const route = readFileSync(
join(__dirname, "../../src/app/api/terminal/context/route.ts"),
"utf8",
);
assert(
/byName\.get\(baseProjectKey\(tab\)/.test(route),
"terminal context must match tabs by baseProjectKey — the raw alias matched no project",
);
});
console.log(`\n${passed}/${passed} passed`);