From fe0f7dc0a1de7807c21c163c80cf0f45313ad7d3 Mon Sep 17 00:00:00 2001 From: Evan Nevermore Date: Tue, 27 Oct 2026 21:13:14 -0400 Subject: [PATCH] test(gateway): give the viewer harness its own runtime root The harness booted the gateway without naming a runtime base, so runtimeBaseDir() fell back to the host's ~/.qwen and a harness page's rail listed this machine's own conversations: GET /rc/fs with no path defaults to $HOME, the folder browser renders that, and renderDirList then asks for that folder's conversations. Any rail assertion was therefore a statement about the host, and a full verifier run printed the user's session titles. The root is created once per process rather than once per boot because the variable is read at request time and the #152 pool boots up to four viewers inside one Node process, so a per-boot value would let one scenario's request resolve against another's directory. P127 guards it by planting a transcript inside the root and requiring the rail to list exactly that row, which fails in both directions: an empty-rail assertion alone would also pass on a host with nothing to list. Its refusal to write outside the root is a separate assertion, because an inherited QWEN_RUNTIME_DIR naming the real ~/.qwen satisfies the containment check and was found putting the plant into a live history. --- .../rc-gateway/scripts/lib/viewerHarness.mts | 44 ++++++++ .../verify-transcript-virtualization.mts | 102 +++++++++++++++++- 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/packages/rc-gateway/scripts/lib/viewerHarness.mts b/packages/rc-gateway/scripts/lib/viewerHarness.mts index 09d58efc222..7689a9ede69 100644 --- a/packages/rc-gateway/scripts/lib/viewerHarness.mts +++ b/packages/rc-gateway/scripts/lib/viewerHarness.mts @@ -231,9 +231,53 @@ export interface Viewer { export const SESSION_ID = '11111111-2222-3333-4444-555555555555'; +/** + * The runtime root a harness page reads conversations from (#178). + * + * The chats dir is resolved per request by `sessions/chatsPath.ts#runtimeBaseDir`, + * which falls back to the real `~/.qwen`. Left alone, a harness page's rail lists + * this machine's own conversations: `GET /rc/fs` with no path defaults to `$HOME`, + * the folder browser renders that, and `renderDirList` then asks for that folder's + * conversations. So any rail assertion is a statement about the host rather than + * about the page, and a full run prints the user's session titles. + * + * The root is created ONCE PER PROCESS rather than once per boot. The variable is + * read at request time, not captured at construction, and the pool in + * `verify-transcript-virtualization.mts` boots up to four viewers inside one Node + * process, so a per-boot value with a restore would let one scenario's request + * resolve against another scenario's directory. Nothing in `scripts/` seeds a + * chats dir (the scenarios needing particular rows intercept `/rc/sessions` with + * `page.route`), so one shared empty root is deterministic. + * + * An inherited `QWEN_RUNTIME_DIR` is respected rather than replaced: whoever set + * it already owns the isolation, and moving the ground under them would be the + * same fault pointed the other way. + */ +let harnessRoot: string | undefined; + +/** The runtime root the booted pages scan for conversations. */ +export function harnessRuntimeRoot(): string { + if (harnessRoot) return harnessRoot; + const inherited = process.env['QWEN_RUNTIME_DIR']; + if (inherited) { + harnessRoot = inherited; + return harnessRoot; + } + const dir = mkdtempSync(join(tmpdir(), 'rc-viewer-home-')); + process.env['QWEN_RUNTIME_DIR'] = dir; + process.once('exit', () => { + rmSync(dir, { recursive: true, force: true }); + }); + harnessRoot = dir; + return dir; +} + export async function bootViewer( opts: BootViewerOptions = {}, ): Promise { + // Before anything can be served, so no request ever resolves against the + // host's `~/.qwen` (#178). + harnessRuntimeRoot(); const stub = await startStubDaemon({ frames: opts.frames, holdOpenMs: opts.holdOpenMs, diff --git a/packages/rc-gateway/scripts/verify-transcript-virtualization.mts b/packages/rc-gateway/scripts/verify-transcript-virtualization.mts index 8e197a17ef8..539a78e8e33 100644 --- a/packages/rc-gateway/scripts/verify-transcript-virtualization.mts +++ b/packages/rc-gateway/scripts/verify-transcript-virtualization.mts @@ -30,11 +30,14 @@ * ONLY=V9,V10 npx tsx scripts/verify-transcript-virtualization.mts view */ import { execFile } from 'node:child_process'; -import { readFileSync } from 'node:fs'; +import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { homedir } from 'node:os'; +import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { promisify } from 'node:util'; import type { Page } from 'playwright'; -import { bootViewer, type Frame, type Viewer } from './lib/viewerHarness.mjs'; +import { bootViewer, harnessRuntimeRoot, type Frame, type Viewer } from './lib/viewerHarness.mjs'; +import { resolveChatsDir } from '../src/sessions/chatsPath.js'; const execFileAsync = promisify(execFile); @@ -12839,6 +12842,101 @@ PAGE_SCENARIOS.push({ ); }, }); + + // #178. The harness booted the gateway without naming a runtime root, so + // runtimeBaseDir() fell back to the host's ~/.qwen and the rail listed this + // machine's own conversations (16 rows on the box this was written on, real + // session titles in the run output). An empty-rail assertion is not a guard, + // because it also passes on a host that has nothing to list. So plant a + // transcript inside the harness root and require the rail to list exactly that + // row: were the root the host's again, the plant would land where the route no + // longer looks and the rows on screen would be the machine's own, which fails + // both halves at once. + PAGE_SCENARIOS.push({ + name: 'P127 the rail reads the harness chats dir and not the host directory', + run: async ({ page }) => { + // The chain that makes this observable without anything being driven: + // GET /rc/fs with no path defaults to $HOME, the folder browser renders + // that, and renderDirList asks for that folder's conversations. + await page.waitForFunction(() => curDirPath !== null); + const landed = await page.evaluate(() => curDirPath); + expect( + typeof landed === 'string' && landed.length > 0, + `the load path landed on no folder (${JSON.stringify(landed)}), so there is no folder to list conversations for and nothing below proves anything`, + ); + const dir = landed as string; + + // Load-bearing on its own, and it runs before any write. The plant has to + // go where the route will actually look, so if the harness ever stops + // isolating this refuses rather than leaving a throwaway conversation in a + // real person's history. + const root = harnessRuntimeRoot(); + const chatsDir = resolveChatsDir(dir); + expect( + chatsDir === root || chatsDir.startsWith(root + '/'), + `the chats dir the route scans for ${JSON.stringify(dir)} is ${chatsDir}, outside the harness root ${root}, so the harness is reading the host again`, + ); + // Its own assertion, because the one above cannot carry this. An inherited + // QWEN_RUNTIME_DIR that names the real ~/.qwen satisfies the containment + // check above, and mutation-tested that is exactly where the plant landed. + // So keeping a throwaway transcript out of a live history is a separate + // claim and it runs before any write. + const realHome = join(homedir(), '.qwen'); + expect( + chatsDir !== realHome && !chatsDir.startsWith(realHome + '/'), + `the chats dir ${chatsDir} sits inside the real user home ${realHome}, so planting a transcript here would write into a live history`, + ); + + const rows = () => + page.evaluate(() => + Array.from( + (document.getElementById('past-list') as HTMLElement).querySelectorAll('.token-row'), + ).map((row) => row.textContent ?? ''), + ); + + const CANARY = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + const TITLE = 'planted by P127'; + const planted = join(chatsDir, `${CANARY}.jsonl`); + // Wait for the chain's own signal that a list landed. Reading the box + // before that measures an empty box rather than an empty scan, which is + // how the count below was found passing while the host was being read. + await page.waitForFunction(() => pastConvosCwd !== null); + const before = await rows(); + try { + expect( + before.length === 0, + `a default load listed ${before.length} conversations for ${JSON.stringify(dir)} (${JSON.stringify(before.slice(0, 3))}), so the rail is reading somewhere other than the harness root`, + ); + + mkdirSync(chatsDir, { recursive: true }); + writeFileSync( + planted, + JSON.stringify({ + type: 'system', + subtype: 'custom_title', + systemPayload: { customTitle: TITLE, titleSource: 'manual' }, + }) + '\n', + ); + + // The page's own read, not a private path into the renderer. + await page.evaluate(([cwd]) => { + curDirPath = cwd; + return refreshPastConversations(cwd); + }, [dir]); + const after = await rows(); + expect( + after.length === 1 && after[0].includes(TITLE), + `a transcript planted in the harness root did not reach the rail on its own (${JSON.stringify(after)})`, + ); + } finally { + // The root is shared for the whole process, so the plant must not + // outlive the scenario or a later one lists a row it never asked for. + rmSync(planted, { force: true }); + } + + return `a default load listed nothing for ${JSON.stringify(dir)} rather than the host's conversations, then listed exactly the transcript planted in ${chatsDir.replace(root, '')}`; + }, + }); } /** Parses `rgb(r, g, b)` / `rgba(r, g, b, a)` into channels. */