Skip to content
Open
11 changes: 8 additions & 3 deletions docs/features/integrations/claude-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,17 @@ A file of your own ends the reading of `AGENTS.md` in the same way: a `CLAUDE.lo
the repository, or a `CLAUDE.md`, `.claude/CLAUDE.md` or `CLAUDE.local.md` in a folder
above it. The `CLAUDE.md` of Claude Code's own configuration folder (`~/.claude/CLAUDE.md`,
or the folder `CLAUDE_CONFIG_DIR` names) is not such a file, also for a checkout under
your home folder: the client reads it in every checkout, and `AGENTS.md` beside it.
your home folder: the client reads it in every checkout, and `AGENTS.md` beside it. The
client knows that file by the spelling of the home folder (`HOME`, `USERPROFILE` on
Windows) or of `CLAUDE_CONFIG_DIR`; in another letter case than the file system's, the file
counts as one of yours above the checkout (checked on Claude Code 2.1.335 on macOS). Setup
follows a symbolic link in that spelling.
Where one holds text, Claude Code reads the repository's `AGENTS.md` only through a
file that imports it. Setup picks the block's file from the repository alone, so it
names `AGENTS.md` on that machine too. Where a file of your own imports that `AGENTS.md`
(a line `@AGENTS.md`, the path from that file's folder, outside a fenced code block),
Claude Code reads it and setup adds nothing more. Where none does, setup adds the line `@AGENTS.md` to the
(a line `@` and its path: from that file's folder, from the home folder as `~/`, or
absolute; not inside a fenced code block), Claude Code reads it and setup adds nothing
more. Where none does, setup adds the line `@AGENTS.md` to the
`CLAUDE.md` at the root: it creates the file where there is none, and a `CLAUDE.md` that
held only the block becomes that one line. Your sessions then read the repository's
`AGENTS.md`, which they did not before. Every machine reads the two files, with or
Expand Down
2 changes: 1 addition & 1 deletion integrations/claude-code-plugin/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "loom",
"displayName": "ClickHouse Loom",
"version": "1.2.158",
"version": "1.2.162",
"description": "Shared long-term memory and code intelligence for Claude Code, self-hosted on ClickHouse. MCP tools to save and recall decisions, preferences and corrections across sessions and teammates; lifecycle hooks that capture each session with no model call; a code index that answers where-is, who-calls and blast-radius questions with line-accurate units instead of whole-file reads.",
"author": {
"name": "ClickHouse"
Expand Down
31 changes: 21 additions & 10 deletions integrations/claude-code-plugin/lib/agent-instructions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { existsSync, readFileSync, realpathSync, rmSync, writeFileSync } from "n
import { homedir } from "node:os";
import { dirname, join, relative, resolve, sep } from "node:path";

import { planeEnabled, pluginMode, promptPointersSetting } from "./loom-client.mjs";
import { planeEnabled, pluginMode, promptPointersSetting, realPath } from "./loom-client.mjs";
import { cachedUnindexed } from "./repo-scope.mjs";
import { readJsonFile, tmpStatePath, writeJsonFile } from "./tmp-state.mjs";

Expand Down Expand Up @@ -159,8 +159,9 @@ function agentsFile(root) {
return files.find(holdsBlock) || files[0];
}

/** `file` with its symbolic links resolved, as given where there is none. */
function real(file) {
/** `file` with its symbolic links resolved and the rest of its spelling as written, unlike
* `realPath`; as written where the file is missing. */
function asWritten(file) {
try {
return realpathSync(file);
} catch {
Expand All @@ -170,8 +171,10 @@ function real(file) {

/** Whether a line of `file` imports `target`: `@` and the path, from the
* file's folder, from the home folder (`~/`) or whole. A line inside a
* fenced code block is none: Claude Code evaluates no import there. */
function imports(file, target) {
* fenced code block is none: Claude Code evaluates no import there. Both
* sides are named by `spelling`. */
function imports(file, target, spelling) {
const want = spelling(target);
let fence = "";
return read(file)
.split(/\r?\n/)
Expand All @@ -182,7 +185,7 @@ function imports(file, target) {
if (mark && !fence) fence = mark;
else if (mark?.startsWith(fence)) fence = "";
const to = !mark && !fence && /^@(\S+)$/.exec(text)?.[1];
return Boolean(to) && real(resolve(dirname(file), to.replace(/^~(?=\/)/, homedir()))) === real(resolve(target));
return Boolean(to) && spelling(resolve(dirname(file), to.replace(/^~(?=\/)/, homedir()))) === want;
});
}

Expand All @@ -204,9 +207,16 @@ function agentsUnreadFor(root, agents) {
dir = dirname(dir);
files.push(...names.map((name) => join(dir, name)));
}
const user = real(join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "CLAUDE.md"));
const own = files.filter((file) => hasText(file) && real(file) !== user);
return own.some((file) => imports(file, agents)) ? undefined : own[0];
// The client knows its own CLAUDE.md by the spelling of the home folder (HOME, USERPROFILE on
// Windows) or of CLAUDE_CONFIG_DIR, as written: in another letter case than the file system's,
// the file is one above the checkout (checked on Claude Code 2.1.335 on macOS). Setup follows a
// symbolic link in that spelling.
const user = asWritten(join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "CLAUDE.md"));
const own = files.filter((file) => hasText(file) && asWritten(file) !== user);
// An import line in these files is compared as this machine's file system names its path: the
// client follows one under any letter case where the file system ignores it (checked on Claude
// Code 2.1.335, `~/` under another case of HOME).
return own.some((file) => imports(file, agents, realPath)) ? undefined : own[0];
}

/** The file the block belongs in, the same one on every machine: no file
Expand All @@ -228,7 +238,8 @@ export function instructionFile(root, client) {
if (repoAgents) return repoAgents;
return files.find((f) => existsSync(f)) || files[0];
}
const shared = [agents, join(root, ".claude", "AGENTS.md")].find((f) => imports(own, f));
// The repository's own import as written: a file system that folds letter case must not pick another file.
const shared = [agents, join(root, ".claude", "AGENTS.md")].find((f) => imports(own, f, asWritten));
return [...files, ...(shared ? [shared] : [])].find(holdsBlock) || shared || own;
}

Expand Down
4 changes: 2 additions & 2 deletions integrations/claude-code-plugin/lib/loom-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ export function git(args, cwd, { timeoutMs = 1500, raw = false, maxBuffer = 16 *
}
}

/** `p` as the file system names it: symlinks resolved and, on Windows,
* long names, drive-letter case and backslashes. Two spellings of one place
/** `p` as the file system names it: symlinks resolved, letter case on macOS
* and Windows and, on Windows, long names and backslashes. Two spellings of one place
* (macOS /tmp and /private/tmp, a Windows 8.3 temp path, git's C:/ form)
* come out equal. The part of `p` that does not exist yet is kept as given. */
export function realPath(p) {
Expand Down
92 changes: 73 additions & 19 deletions integrations/claude-code-plugin/tests/agent-instructions.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@ import { hostEnv } from "./host-env.mjs";
process.env.LOOM_HOOK_NO_MAIN = "1";
const { clientStatus } = await import("../bin/loom-client-status.mjs");

const scratch = () => fs.mkdtempSync(path.join(os.tmpdir(), "loom-instructions-"));
// A scratch folder spelled as setup names a checkout (`realPath`, through `gitToplevel`), which
// expands a Windows 8.3 name such as RUNNER~1 that the temporary folder can carry.
const realScratch = () => realPath(scratch());
// Spelled as setup names a checkout (`realPath`, through `gitToplevel`), which expands a
// Windows 8.3 name such as RUNNER~1 that the temporary folder can carry.
const scratch = () => realPath(fs.mkdtempSync(path.join(os.tmpdir(), "loom-instructions-")));
// Setup reads the folders above a checkout for a CLAUDE.md of the person's
// own, and the scratch checkouts are under the temporary folder: a file with
// text outside comments above it would change the layouts below, so it is
// named here. (The home folder is a scratch one: `temp-home.mjs`.)
for (const tmp of new Set([os.tmpdir(), fs.realpathSync(os.tmpdir())])) {
for (let dir = tmp, last = ""; dir !== last; last = dir, dir = path.dirname(dir)) {
for (const name of ["CLAUDE.md", path.join(".claude", "CLAUDE.md"), "CLAUDE.local.md"]) {
const file = path.join(dir, name);
const text = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
const held = text.split(/<!--[\s\S]*?-->/).some((part) => part.trim());
assert.equal(held, false, `${file} holds text: run these tests with a TMPDIR that has no such file above it`);
}
for (let dir = realPath(os.tmpdir()), last = ""; dir !== last; last = dir, dir = path.dirname(dir)) {
for (const name of ["CLAUDE.md", path.join(".claude", "CLAUDE.md"), "CLAUDE.local.md"]) {
const file = path.join(dir, name);
const text = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
const held = text.split(/<!--[\s\S]*?-->/).some((part) => part.trim());
assert.equal(held, false, `${file} holds text: run these tests with a TMPDIR that has no such file above it`);
}
}
const words = (block) =>
Expand Down Expand Up @@ -785,7 +782,7 @@ const ASKED = { serverUrl: "http://localhost:7777", token: "" };
/** A checkout a session's lookup found in no index (`indexed` false), or
* found its own repository indexed. */
function lookedUp(indexed, tmp = scratch()) {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
writeCache(root, { ts: Date.now(), indexed, own: indexed }, tmp, ASKED);
return { root, tmp };
Expand Down Expand Up @@ -834,7 +831,7 @@ function setup(cwd, args = [], env = {}, home = scratch()) {
}

test("loom-setup: writes at the checkout's root, is idempotent, updates and removes", () => {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
fs.mkdirSync(path.join(root, "src"));
const file = path.join(root, "CLAUDE.md");
Expand All @@ -861,7 +858,7 @@ test("loom-setup: writes at the checkout's root, is idempotent, updates and remo
});

test("loom-setup in a repository with AGENTS.md and no CLAUDE.md writes there, and undoes a CLAUDE.md that held only the block", () => {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
const [agents, claude] = ["AGENTS.md", "CLAUDE.md"].map((f) => path.join(root, f));
fs.writeFileSync(agents, RULES);
Expand Down Expand Up @@ -918,7 +915,7 @@ test("loom-setup in a repository with AGENTS.md and no CLAUDE.md writes there, a
});

test("a CLAUDE.md in Claude Code's own configuration folder above the checkout leaves AGENTS.md read: setup adds no CLAUDE.md", () => {
const home = realScratch();
const home = scratch();
const root = path.join(home, "work", "checkout");
const [agents, claude, mine] = [
path.join(root, "AGENTS.md"),
Expand All @@ -942,8 +939,56 @@ test("a CLAUDE.md in Claude Code's own configuration folder above the checkout l
assert.equal(fs.readFileSync(claude, "utf8"), "@AGENTS.md\n");
});

test("a home folder spelled another way: the client's own CLAUDE.md is known by that spelling, an import by any", () => {
const home = scratch();
// Other spellings of the same folder: letter case, where the file system ignores it (macOS, Windows; a
// lowercase drive letter there). A Linux file system usually tells letter case apart, leaving `home` alone.
const spellings = [...new Set([home, home.toUpperCase(), home.toLowerCase()])].filter((at) => fs.existsSync(at));
const root = path.join(home, "work", "checkout");
const [agents, claude, mine, above] = [
path.join(root, "AGENTS.md"),
path.join(root, "CLAUDE.md"),
path.join(home, ".claude", "CLAUDE.md"),
path.join(home, "work", "CLAUDE.md"),
];
fs.mkdirSync(root, { recursive: true });
execFileSync("git", ["init", "-q"], { cwd: root });
for (const at of spellings) {
// The configuration folder's CLAUDE.md, through HOME and through CLAUDE_CONFIG_DIR. Claude Code knows it as
// its own only by the spelling it is given: under another one it reads the file as one above the checkout
// and leaves AGENTS.md unread, so setup adds the import.
fs.mkdirSync(path.dirname(mine), { recursive: true });
fs.writeFileSync(mine, "Mine.\n");
for (const [env, homeFolder] of [
[{}, at],
[{ CLAUDE_CONFIG_DIR: path.join(at, ".claude") }, scratch()],
]) {
fs.writeFileSync(agents, RULES);
const wrote = setup(root, [], { LOOM_MODE: "code", ...env }, homeFolder);
if (at === home) {
assert.equal(wrote.stdout, `loom: wrote the Loom block to ${agents}\n`, `${at}: ${wrote.stderr}`);
assert.equal(fs.existsSync(claude), false);
} else {
const added = `loom: ${claude} now holds the line \`@AGENTS.md\`: `;
assert.equal(wrote.stdout.startsWith(added), true, `${at}: ${wrote.stdout}${wrote.stderr}`);
assert.equal(fs.readFileSync(claude, "utf8"), "@AGENTS.md\n");
fs.rmSync(claude);
}
}
// A CLAUDE.md above the checkout that imports AGENTS.md from the home folder: the client reads an import
// under any spelling, so AGENTS.md is read and setup adds no CLAUDE.md.
fs.rmSync(path.dirname(mine), { recursive: true });
fs.writeFileSync(above, "@~/work/checkout/AGENTS.md\n");
fs.writeFileSync(agents, RULES);
const read = setup(root, [], { LOOM_MODE: "code" }, at);
assert.equal(read.stdout, `loom: wrote the Loom block to ${agents}\n`, `${at}: ${read.stderr}`);
assert.equal(fs.existsSync(claude), false);
fs.rmSync(above);
}
});

test("loom-setup writes nothing where no code index answers, and still removes there", () => {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
const file = path.join(root, "CLAUDE.md");
const home = scratch();
Expand Down Expand Up @@ -1019,7 +1064,7 @@ test("loom-setup under auto writes the pointers block where the last lookup foun
});

test("loom-setup writes the block for the prompt's pointers as the last session saved them, else as the environment says", () => {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
const file = path.join(root, "CLAUDE.md");
const written = () => fs.readFileSync(file, "utf8");
Expand Down Expand Up @@ -1062,7 +1107,7 @@ test("the integration page shows both blocks setup writes", () => {
});

test("loom-setup under the Codex plugin writes AGENTS.md", () => {
const root = realScratch();
const root = scratch();
execFileSync("git", ["init", "-q"], { cwd: root });
const script = fileURLToPath(new URL("../../codex-plugin/bin/loom-setup.mjs", import.meta.url));
const home = scratch();
Expand Down Expand Up @@ -1124,3 +1169,12 @@ test("the status check warns about a checkout without the current block and pass
"AGENTS.md; `/loom:loom-setup` moves the block into AGENTS.md",
});
});

test("the repository's own import picks the block's file by its spelling, on every file system", () => {
// `@agents.md` reads AGENTS.md where the file system ignores letter case and nothing where it does not:
// the block stays in the CLAUDE.md, so every machine of the repository picks the same file.
const root = scratch();
fs.writeFileSync(path.join(root, "AGENTS.md"), "# Rules\n");
fs.writeFileSync(path.join(root, "CLAUDE.md"), "# Project\n\n@agents.md\n");
assert.equal(instructionFile(root, "claude-code"), path.join(root, "CLAUDE.md"));
});
2 changes: 1 addition & 1 deletion integrations/codex-plugin/.codex-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loom",
"version": "1.2.158",
"version": "1.2.162",
"description": "Shared long-term memory and code intelligence for Codex, self-hosted on ClickHouse. MCP tools to save and recall decisions, preferences and corrections across sessions and teammates; lifecycle hooks that capture each session with no model call; a code index that answers where-is, who-calls and blast-radius questions with line-accurate units instead of whole-file reads.",
"author": {
"name": "ClickHouse"
Expand Down
31 changes: 21 additions & 10 deletions integrations/codex-plugin/lib/agent-instructions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { existsSync, readFileSync, realpathSync, rmSync, writeFileSync } from "n
import { homedir } from "node:os";
import { dirname, join, relative, resolve, sep } from "node:path";

import { planeEnabled, pluginMode, promptPointersSetting } from "./loom-client.mjs";
import { planeEnabled, pluginMode, promptPointersSetting, realPath } from "./loom-client.mjs";
import { cachedUnindexed } from "./repo-scope.mjs";
import { readJsonFile, tmpStatePath, writeJsonFile } from "./tmp-state.mjs";

Expand Down Expand Up @@ -159,8 +159,9 @@ function agentsFile(root) {
return files.find(holdsBlock) || files[0];
}

/** `file` with its symbolic links resolved, as given where there is none. */
function real(file) {
/** `file` with its symbolic links resolved and the rest of its spelling as written, unlike
* `realPath`; as written where the file is missing. */
function asWritten(file) {
try {
return realpathSync(file);
} catch {
Expand All @@ -170,8 +171,10 @@ function real(file) {

/** Whether a line of `file` imports `target`: `@` and the path, from the
* file's folder, from the home folder (`~/`) or whole. A line inside a
* fenced code block is none: Claude Code evaluates no import there. */
function imports(file, target) {
* fenced code block is none: Claude Code evaluates no import there. Both
* sides are named by `spelling`. */
function imports(file, target, spelling) {
const want = spelling(target);
let fence = "";
return read(file)
.split(/\r?\n/)
Expand All @@ -182,7 +185,7 @@ function imports(file, target) {
if (mark && !fence) fence = mark;
else if (mark?.startsWith(fence)) fence = "";
const to = !mark && !fence && /^@(\S+)$/.exec(text)?.[1];
return Boolean(to) && real(resolve(dirname(file), to.replace(/^~(?=\/)/, homedir()))) === real(resolve(target));
return Boolean(to) && spelling(resolve(dirname(file), to.replace(/^~(?=\/)/, homedir()))) === want;
});
}

Expand All @@ -204,9 +207,16 @@ function agentsUnreadFor(root, agents) {
dir = dirname(dir);
files.push(...names.map((name) => join(dir, name)));
}
const user = real(join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "CLAUDE.md"));
const own = files.filter((file) => hasText(file) && real(file) !== user);
return own.some((file) => imports(file, agents)) ? undefined : own[0];
// The client knows its own CLAUDE.md by the spelling of the home folder (HOME, USERPROFILE on
// Windows) or of CLAUDE_CONFIG_DIR, as written: in another letter case than the file system's,
// the file is one above the checkout (checked on Claude Code 2.1.335 on macOS). Setup follows a
// symbolic link in that spelling.
const user = asWritten(join(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "CLAUDE.md"));
const own = files.filter((file) => hasText(file) && asWritten(file) !== user);
// An import line in these files is compared as this machine's file system names its path: the
// client follows one under any letter case where the file system ignores it (checked on Claude
// Code 2.1.335, `~/` under another case of HOME).
return own.some((file) => imports(file, agents, realPath)) ? undefined : own[0];
}

/** The file the block belongs in, the same one on every machine: no file
Expand All @@ -228,7 +238,8 @@ export function instructionFile(root, client) {
if (repoAgents) return repoAgents;
return files.find((f) => existsSync(f)) || files[0];
}
const shared = [agents, join(root, ".claude", "AGENTS.md")].find((f) => imports(own, f));
// The repository's own import as written: a file system that folds letter case must not pick another file.
const shared = [agents, join(root, ".claude", "AGENTS.md")].find((f) => imports(own, f, asWritten));
return [...files, ...(shared ? [shared] : [])].find(holdsBlock) || shared || own;
}

Expand Down
Loading
Loading