testSearchAndFindFollowTheSessionsBranch
A session's tcm_search and tcm_find read its branch as it is now: a page it changed, and a definition whose doc and comments it changed (the same hash, another text version), are found as they read there, before and after each save, by the server that keeps running (its worker); main finds them as they read on main, and a save on main changes nothing the session finds. A codebase switched to the branch finds what the session does.
import { okIn, tempDir, withChildren } from "#codebase/tcmts/cli/test/e2e/harness"; import { session } from "#codebase/tcmts/cli/test/e2e/mcp"; import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; export const testSearchAndFindFollowTheSessionsBranch = () => withChildren(async (keep) => { const dir = tempDir("search on a branch"); const tcm = (...args: string[]) => okIn(dir, ...args); const write = (file: string, text: string) => { fs.mkdirSync(path.dirname(path.join(dir, file)), { recursive: true }); fs.writeFileSync(path.join(dir, file), text); }; tcm("init"); write( "scratch.ts", `// tcm: namespace lib\n/** Squares a number. */\nexport const square = (x: number): number =>\n // times itself\n x * x;\n`, ); tcm("add"); write("pages/notes.md", "<!-- tcm: namespace docs -->\n# Notes\n\nalpha text here\n"); tcm("add", "pages/notes.md"); const found = (out: string) => [...out.matchAll(/^(\w+\.\w+)(?: |$)/gm)].map((m) => m[1]); const a = session(dir, keep, "--branch", "agent/a"); await a.request("initialize", { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "test", version: "0" }, }); const search = async (text: string, under?: string) => found((await a.call("tcm_search", { text, ...(under ? { under } : {}) })).text); const find = async (query: string) => found((await a.call("tcm_find", { query })).text); // before: the session reads what main does (and its server has read it once) assert.deepEqual(await search("alpha", "docs.notes"), ["docs.notes"]); assert.deepEqual(await search("times itself"), ["lib.square"]); // find's words are in a name, its signature or its doc assert.deepEqual(await find("squares number"), ["lib.square"]); // the session changes the page, and the doc and comment (a text version: the hash stays) const hash = /#([a-z2-7]{8})/.exec(tcm("view", "lib.square"))![1]!; const saved = await a.call("tcm_patch", { patches: [ { name: "docs.notes", edits: [{ old: "alpha", new: "beta" }] }, { name: "lib.square", edits: [ { old: "Squares a number", new: "Gamma: the number times itself" }, { old: "// times itself", new: "// kappa" }, ], }, ], }); assert.equal(saved.isError, false, saved.text); assert.match( (await a.call("tcm_view", { names: ["lib.square"] })).text, new RegExp(`#${hash}`), ); // after: the session finds its text, and not main's assert.deepEqual(await search("beta", "docs.notes"), ["docs.notes"]); assert.deepEqual(await search("alpha", "docs.notes"), []); assert.deepEqual(await search("kappa"), ["lib.square"]); assert.deepEqual(await search("Gamma", "lib.square"), ["lib.square"]); assert.deepEqual(await search("times itself", "lib"), ["lib.square"]); assert.deepEqual(await find("gamma"), ["lib.square"]); assert.deepEqual(await find("number times"), ["lib.square"]); assert.deepEqual(await find("squares number"), []); // main reads as it did assert.deepEqual(found(tcm("search", "alpha", "--under", "docs.notes")), ["docs.notes"]); assert.deepEqual(found(tcm("search", "beta")), []); assert.deepEqual(found(tcm("search", "kappa")), []); assert.deepEqual(found(tcm("find", "squares", "number")), ["lib.square"]); assert.deepEqual(found(tcm("find", "gamma")), []); // a save on main changes nothing the session finds write( "scratch.ts", `// tcm: namespace lib\n/** Halves a number. */\nexport const half = (x: number): number => x / 2;\n`, ); tcm("add"); assert.deepEqual(await search("beta", "docs.notes"), ["docs.notes"]); assert.deepEqual(await search("kappa"), ["lib.square"]); assert.deepEqual(await find("halves"), []); assert.deepEqual(found(tcm("find", "halves")), ["lib.half"]); // the codebase switched to the branch finds what the session does, and back on main what main does tcm("switch", "agent/a"); assert.deepEqual(found(tcm("search", "beta", "--under", "docs.notes")), ["docs.notes"]); assert.deepEqual(found(tcm("search", "kappa")), ["lib.square"]); assert.deepEqual(found(tcm("find", "gamma")), ["lib.square"]); tcm("switch", "main"); assert.deepEqual(found(tcm("search", "kappa")), []); assert.deepEqual(found(tcm("find", "gamma")), []); a.close(); })