Files
loki/scripts/test/terminal-deep-link.ts
CatoandClaude Opus 5 fadb7f4274 fix(terminal): a "Watch" link must open the thing it offered to show (#719)
Dispatch a prompt from Control and you get a "Queued — waiting for a builder"
banner with a Watch link. Clicking it opened a plain bash prompt showing a
week-old panic message. The agent was running the whole time; the page just
opened somewhere else.

WHY

Every watch link is `/terminal?project=<key>` with no source, and the terminal
resolved its source as `initialSource ?? mode.source` — where `mode.source` is
REMEMBERED IN LOCALSTORAGE. Anyone who had once picked Shell had every watch
link on Control silently redirected to a bash PTY, for that browser, forever.
Nothing errored. Nothing looked broken.

A shell cannot be the answer to a project link: it is a Loki-owned bash PTY
with no concept of project tabs — `stripTabs` is not even passed when the
source is "shell". So `?project=` plus a remembered "shell" is never what the
link meant.

THE RULE

A deep link that names a project resolves to an agent source. An EXPLICIT
`?source=shell` is still obeyed — that is the reader asking, rather than a
stale preference answering for them. Nothing else changes: opening /terminal
with no project still honours whatever you last chose.

The rule lives in src/lib/terminal-deep-link.ts so it can be asserted rather
than inferred from a component, and `fleetSurfaceHref` now takes an optional
source so callers that know where an agent runs can say so instead of leaving
it to be resolved.

PROVEN, NOT ASSUMED

- 8 assertions, both directions: the regression, the remembered-shell case with
  no project (still shell), explicit ?source=shell with a project (still
  shell), an agent source surviving a project link, a hosted plane with no
  shell at all, and shell-as-only-source returning something usable.
- Reverting the fix on a copy turns two of those red — the test catches the
  original bug rather than describing the new code.
- Driven in a real browser against a dev server, same URL, same browser, with
  `loki:terminal-mode` seeded to `shell`:
    before the fix -> the header reads "Shell"   (the dead end)
    after  the fix -> the header reads "Cloud"
  and the two negative cases re-checked live: no project -> "Shell",
  explicit ?source=shell -> "Shell".


Claude-Session: https://claude.ai/code/session_01NLZ6vs1jsWw2kKbpWTZHf2

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-14 22:48:39 -07:00

94 lines
3.4 KiB
TypeScript

// A "Watch" link has to land on the thing it offered to show.
//
// The defect this pins, measured 2026-09-13: dispatching a prompt to a project
// from Control produced a "Queued — waiting for a builder" banner with a Watch
// link. Clicking it opened /terminal?project=<key> with no source, so the
// source fell back to the one REMEMBERED IN LOCALSTORAGE — Shell — and the
// reader landed on an unrelated bash PTY showing a week-old panic message. The
// agent ran the whole time. Nothing errored; the page just opened elsewhere,
// and it did so for every watch link in that browser, permanently.
// Run: npx tsx scripts/test/terminal-deep-link.ts
import { resolveTerminalSource } from "@/lib/terminal-deep-link";
import type { TerminalSource } from "@/config/terminal-modes";
let pass = 0;
let fail = 0;
function ok(cond: boolean, label: string) {
if (cond) {
pass++;
} else {
fail++;
console.error(`✗ ${label}`);
}
}
const eq = (got: string, want: string, label: string) =>
ok(got === want, `${label} — got ${got}, want ${want}`);
const LOCAL: readonly TerminalSource[] = ["cloud", "machine", "shell"];
const HOSTED: readonly TerminalSource[] = ["cloud", "machine"];
// ── the regression ──────────────────────────────────────────────────────────
eq(
resolveTerminalSource({
remembered: "shell",
projectRequested: true,
available: LOCAL,
}),
"cloud",
"a project link never opens the shell just because shell was remembered",
);
// ── and the other direction: nothing else is disturbed ──────────────────────
eq(
resolveTerminalSource({ remembered: "shell", projectRequested: false, available: LOCAL }),
"shell",
"opening /terminal with NO project still honours the remembered shell",
);
eq(
resolveTerminalSource({
fromUrl: "shell",
remembered: "cloud",
projectRequested: true,
available: LOCAL,
}),
"shell",
"an EXPLICIT ?source=shell is obeyed even with a project — that is the reader asking",
);
eq(
resolveTerminalSource({ remembered: "machine", projectRequested: true, available: LOCAL }),
"machine",
"a remembered agent source survives a project link",
);
eq(
resolveTerminalSource({ remembered: "cloud", projectRequested: true, available: LOCAL }),
"cloud",
"cloud survives a project link",
);
eq(
resolveTerminalSource({
fromUrl: "machine",
remembered: "cloud",
projectRequested: true,
available: LOCAL,
}),
"machine",
"an explicit source always wins over the remembered one",
);
// ── deployments that cannot offer a shell at all ────────────────────────────
eq(
resolveTerminalSource({ remembered: "shell", projectRequested: true, available: HOSTED }),
"cloud",
"on a hosted plane with no shell, a project link still resolves to an agent source",
);
// ── degenerate input must not produce something unusable ────────────────────
eq(
resolveTerminalSource({ remembered: "shell", projectRequested: true, available: ["shell"] }),
"shell",
"if shell is the ONLY source, it is returned rather than nothing",
);
console.log(`terminal-deep-link: ${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);