Files
loki/scripts/test/agent-session-read.ts
CatoandClaude Opus 5.5 a1b009f26e feat(sessions): a token reads its sessions and what was said in them (#1661)
* feat(sessions): a token reads its sessions and what was said in them

An agent that starts Claude Code in a Loki session (tab-inject) could read
the screen (#1641) but not the conversation: the parsed transcript was only
readable over the cookie-only transcript stream, and since #1644 over
OrangeCat's signed rail.

- lib/session-reads: the list, one run, or one project with the last things
  said in its conversation, moved out of the signed route unchanged, so two
  doors share one copy
- GET /api/control/sessions: the same reads with a session or ck_ token
  (?project=, ?runId=, ?items=), no-store
- every row names its session and carries openUrl, the same session in
  Loki's Terminal on that run (lib/dispatch-card sessionOpenUrl)
- the list says it is cut at 25 and carries total, so a cut list is never
  read as the whole one
- LOOP-WALK's Session walk names the read; tests pin both doors to the one lib

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtCjm3CvrFm3kYaTgy

* fix(sessions): the conversation read is current, on the session's builder, and never ends a person's view

Traced end to end before merge (2026-11-12):

- a cached conversation was served forever: after the first read sent
  peek_stop the runner stopped streaming, so a later read returned the same
  copy without the agent's answer. The cache now counts only while someone
  is watching; otherwise the read waits for a frame posted after it asked.
- peek_start and peek_stop carried no channel, so the owner's laptop runner
  could take a read meant for the cloud session (transcript null). They now
  name the project's builder (projectPreferredChannel).
- the read counted its viewer under a key of its own, so its peek_stop could
  end a person's live view of the same session on a phone. It now shares the
  Terminal's viewer count (same user, tab and channel).
- a cloud link carried no source, so a phone that last chose 'Your computer'
  opened the laptop's session of the same name. sessionOpenUrl names the
  cloud builder, and each row's link uses the session's builder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtCjm3CvrFm3kYaTgy

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 12:32:25 +01:00

66 lines
3.5 KiB
TypeScript

/**
* An agent that starts Claude Code in a Loki session can see it and read it.
*
* Starting (tab-inject), typing (tab-inject-raw) and closing (close-tab) took
* a `ck_` token; listing open sessions and reading a capture took a browser
* session only, so the agent could ask for a screen and never read the
* answer. A plain shell stays a signed-in person's.
*/
import assert from "node:assert/strict";
import fs from "node:fs";
import { mayPeekTab } from "../../src/lib/peek-access";
import { shellTabFromHex } from "../../src/lib/shell-session";
import { terminalHref } from "../../src/lib/dispatch-card";
const read = (p: string) => fs.readFileSync(p, "utf8");
const shell = shellTabFromHex("0a1b2c3d");
assert.equal(mayPeekTab("loki", "token"), true, "an agent session is the token's to read");
assert.equal(mayPeekTab("loki", "session"), true);
assert.equal(mayPeekTab(shell, "token"), false, "a shell's screen is never a token's");
assert.equal(mayPeekTab(shell, "session"), true);
const list = read("src/app/api/control/open-tabs/route.ts");
assert.match(list, /getApiUserId\(\)/, "open sessions are listed for a token");
assert.doesNotMatch(list, /getSessionUserId/);
for (const route of [
"src/app/api/control/peek-tab/route.ts",
"src/app/api/control/peek-tab/[id]/route.ts",
]) {
const src = read(route);
assert.match(src, /getApiActor\(\)/, `${route}: session or token`);
assert.match(src, /mayPeekTab\(tab, actor\.via\)/, `${route}: shells stay out`);
assert.doesNotMatch(src, /getSessionUserId/);
}
// The asking route says where the answer is and where a person opens the session.
const ask = read("src/app/api/control/peek-tab/route.ts");
assert.match(ask, /resultUrl: `\/api\/control\/peek-tab\/\$\{peekId\}`/);
assert.match(ask, /openUrl: sessionOpenUrl\(tab, channel \?\? null\)/);
assert.equal(terminalHref("loki", "cloud"), "/terminal?project=loki");
// The conversation: a token reads its sessions through the same reads as
// OrangeCat's signed rail, and neither route keeps a copy of its own.
const control = read("src/app/api/control/sessions/route.ts");
assert.match(control, /getApiUserId\(\)/, "sessions and their conversation are read with a token");
assert.match(control, /readSessions\(userId, \{ \.\.\.query, poll: Boolean\(poll\) \}\)/);
const signed = read("src/app/api/orangecat/sessions/route.ts");
assert.match(signed, /readSessions\(user\.id, \{ project, runId, items, watch, poll \}\)/);
for (const src of [control, signed]) {
assert.doesNotMatch(src, /lastTranscript|addPeekViewer|loadRunTrack/, "one copy of the reads");
}
const reads = read("src/lib/session-reads.ts");
assert.match(reads, /export async function readSessions\(/);
assert.match(reads, /total: rows\.length,/, "a list cut at the cap says how many there were");
// The conversation read is current, on the session's builder, and shares the
// Terminal's viewer count (so it never ends a person's live view).
assert.match(reads, /projectPreferredChannel\(project\)/, "an unrecorded builder is the project's");
assert.match(reads, /const command = \{ tab, channel \};/, "peek commands name the builder");
assert.match(reads, /addPeekViewer\(userId, tab, channel\)/);
assert.match(reads, /removePeekViewer\(userId, tab, channel\)/);
assert.match(reads, /frame\.at >= joinedAt/, "a copy from before the read is not the answer");
// The live stream is still a browser's: it holds a viewer open on the runner.
assert.match(read("src/app/api/control/peek-stream/route.ts"), /getSessionUserId\(\)/);
console.log("agent-session-read ok");