Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .claude/scripts/lib/project-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ const holdsMarker = (dir: string): boolean => {
}
};

/**
* Whether `path` is the vault root or inside it. Boundary-safe ("/vault" does
* not contain "/vaulting/…"), with either slash. On Windows the comparison
* ignores case, since the hook's vault root (from the environment or the cwd)
* and a tool's file path can spell the drive and folders differently, and the
* filesystem treats them as the same.
*/
export function isInsideVault(path: string, vaultRoot: string, platform: string = process.platform): boolean {
const fold = (s: string): string => {
const fwd = s.replaceAll("\\", "/").replace(/\/+$/, "");
return platform === "win32" ? fwd.toLowerCase() : fwd;
};
const p = fold(path);
const root = fold(vaultRoot);
return p === root || p.startsWith(root + "/");
}

export function resolveProjectDir(fallback: string, env: NodeJS.ProcessEnv = process.env): string {
const named = PROJECT_DIR_VARS.map((name) => env[name]).find(Boolean) ?? fallback;
return nearestVaultRoot(named) ?? named;
Expand Down
13 changes: 13 additions & 0 deletions .claude/scripts/lib/qmd-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { join, resolve as resolvePath } from "node:path";
import { buildQmdCommand, REFRESH_SWITCH, refreshDisabled, resolveQmdEntry } from "./qmd.ts";
import { envPath } from "./env-path.ts";
import { debug } from "./hook-io.ts";
import { isInsideVault } from "./project-dir.ts";
import { qmdArgsWithIndex } from "./session-start.ts";

/**
Expand Down Expand Up @@ -69,6 +70,18 @@ export function shouldRefreshForPath(filePath: unknown): boolean {
return !SKIP_SEGMENTS.some((seg) => normalized.includes(seg));
}

/**
* Whether a write to `filePath` should refresh this vault's index (#366): a
* note `shouldRefreshForPath` accepts, inside `vaultRoot` (forward slashes, no
* trailing slash). A note outside the vault can't change the index, so it
* starts no worker. The check is boundary-safe, like validate-write's skip
* ladder: "/vault" does not contain "/vaulting/…". In-vault paths that ladder
* skips (templates, root docs) still refresh: they are indexed all the same.
*/
export function refreshesVaultIndex(filePath: unknown, vaultRoot: string, platform: string = process.platform): boolean {
return shouldRefreshForPath(filePath) && isInsideVault(filePath as string, vaultRoot, platform);
}

/**
* Return true when a previous refresh ran recently enough that this one
* should be skipped. `sentinelMtimeMs` is the mtime of the debounce
Expand Down
44 changes: 44 additions & 0 deletions .claude/scripts/tests/refresh-in-vault.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Which writes refresh this vault's QMD index (#366). A `.md` written outside
* the vault can't change the index, so it starts no refresh worker. Inside the
* vault, paths the validation skips (templates, root docs) still refresh,
* because they are indexed all the same.
*/

import { test } from "node:test";
import assert from "node:assert/strict";
import { join, resolve } from "node:path";

import { isInsideVault } from "../lib/project-dir.ts";
import { refreshesVaultIndex } from "../lib/qmd-refresh.ts";

const VAULT = resolve("/vault");
const fwd = (p: string) => p.replaceAll("\\", "/");

test("a note inside the vault refreshes, skipped by validation or not", () => {
for (const rel of [join("work", "active", "Plan.md"), join("templates", "Work Note.md"), "README.md"]) {
assert.equal(refreshesVaultIndex(join(VAULT, rel), fwd(VAULT)), true, rel);
}
});

test("a note outside the vault refreshes nothing", () => {
assert.equal(refreshesVaultIndex(join(resolve("/elsewhere"), "README.md"), fwd(VAULT)), false);
assert.equal(refreshesVaultIndex(join(resolve("/vaulting"), "note.md"), fwd(VAULT)), false, "a sibling whose name starts like the vault");
});

test("inside the vault, the extension and skip rules still apply", () => {
assert.equal(refreshesVaultIndex(join(VAULT, "code.ts"), fwd(VAULT)), false);
assert.equal(refreshesVaultIndex(join(VAULT, ".git", "x.md"), fwd(VAULT)), false);
});

test("isInsideVault ignores case on Windows, where the drive and folders may be spelled differently", () => {
assert.equal(isInsideVault("C:/Dev/Vault/note.md", "c:/dev/vault", "win32"), true);
assert.equal(isInsideVault(String.raw`C:\Dev\Vault\note.md`, "c:/dev/vault", "win32"), true, "either slash");
assert.equal(isInsideVault("C:/Dev/Vaulting/note.md", "c:/dev/vault", "win32"), false, "still boundary-safe");
assert.equal(isInsideVault("/Vault/note.md", "/vault", "linux"), false, "case matters where the filesystem says so");
assert.equal(isInsideVault("/vault/", "/vault", "linux"), true, "a trailing slash on either side");
});

test("a note whose drive letter is cased differently still refreshes on Windows", () => {
assert.equal(refreshesVaultIndex("C:/Dev/Vault/work/Plan.md", "c:/dev/vault", "win32"), true);
});
27 changes: 27 additions & 0 deletions .claude/scripts/tests/validate-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,30 @@ describe("validate-write — apply_patch review follow-ups (#354)", () => {
assert.deepEqual(clusterPaths, ["work/active/Pelican Notes.md", "work/active/Pelican Plan.md", "work/active/Pelican Risks.md"]);
});
});

describe("validate-write — the refresh covers only notes inside the vault (#366)", () => {
const write = (filePath: string, env: Record<string, string>) =>
spawnHook(SCRIPT, { tool_input: { file_path: filePath } }, { CLAUDE_PROJECT_DIR: TMP_DIR, ...env });

test("a note written outside the vault requests no refresh", () => {
const outside = mkdtempSync(join(tmpdir(), "validate-write-refresh-outside-"));
try {
const note = join(outside, "README.md");
writeFileSync(note, "# Elsewhere\n");
const { code, stderr } = write(note, refreshEnv("outside"));
assert.equal(code, 0);
assert.equal(refreshed(stderr), false, "a write the index can't see starts no worker");
} finally {
rmTemp(outside);
}
});

test("an in-vault template, skipped by validation, still requests one (the control)", () => {
mkdirSync(join(TMP_DIR, "templates"), { recursive: true });
const note = join(TMP_DIR, "templates", "Refresh Control.md");
writeFileSync(note, "# Template\n");
const { code, stderr } = write(note, refreshEnv("template"));
assert.equal(code, 0);
assert.equal(refreshed(stderr), true);
});
});
17 changes: 9 additions & 8 deletions .claude/scripts/validate-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import {
newNoteClusterCandidate,
} from "./lib/active-hygiene.ts";
import {
refreshesVaultIndex,
sentinelPathFrom,
shouldRefreshForPath,
triggerDebouncedRefresh,
} from "./lib/qmd-refresh.ts";
import { resolveProjectDir } from "./lib/project-dir.ts";
import { isInsideVault, resolveProjectDir } from "./lib/project-dir.ts";
import { refreshTargets, writeTargets } from "./lib/write-targets.ts";

type HookInput = {
Expand Down Expand Up @@ -70,12 +70,14 @@ if (refreshed.length === 0) {
// Debounced QMD refresh (#107) — fire-and-forget, never blocks validation.
// Folded here from the retired qmd-refresh.ts entry so every .md write
// costs ONE process spawn, not two (~150-300ms each on Windows). Runs
// before the skip ladder so template/root/memory paths still refresh.
// before the skip ladder so in-vault template/root/memory paths still
// refresh; a note outside the vault can't change the index and refreshes
// nothing (#366).
const DEBOUNCE_MS = 30_000;
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
const SENTINEL_PATH = sentinelPathFrom(process.env, SCRIPT_DIR);
const WORKER_PATH = resolvePath(SCRIPT_DIR, "qmd-refresh-run.ts");
if (refreshed.some(shouldRefreshForPath)) {
if (refreshed.some((path) => refreshesVaultIndex(path, vaultRoot))) {
triggerDebouncedRefresh({
sentinelPath: SENTINEL_PATH,
workerPath: WORKER_PATH,
Expand Down Expand Up @@ -154,7 +156,7 @@ function checkFile(filePath: string): FileFindings {
// Vault-root skip AFTER the memory guard (which must inspect outside-vault
// paths): files outside the vault are not vault notes — no validation.
const filePathFwd = filePath.replaceAll("\\", "/");
if (filePathFwd !== vaultRoot && !filePathFwd.startsWith(vaultRoot + "/")) {
if (!isInsideVault(filePathFwd, vaultRoot)) {
debug(`validate: skipped (outside vault root) ${filePath}`);
return { blocks, policyResults };
}
Expand All @@ -173,9 +175,8 @@ function checkFile(filePath: string): FileFindings {
debug(`validate: could not read ${filePath}`);
return { blocks, policyResults };
}
const relPath = filePathFwd.startsWith(vaultRoot + "/")
? filePathFwd.slice(vaultRoot.length + 1)
: filePathFwd;
// Inside the vault by now (the skip above), whatever the case of its drive.
const relPath = filePathFwd.slice(vaultRoot.length + 1);
const warnings = validateContent(content, {
required: parseFrontmatterRequired(loadManifest()),
relPath,
Expand Down
Loading