Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: treat an empty state-path override as unset, so the refresh debounce and the hook stores hold (#395)

Brenno Ferrarif8e0f7fab198
diff --git a/.claude/scripts/classify-message.ts b/.claude/scripts/classify-message.ts
index d08419d..a2c89ba 100644
--- a/.claude/scripts/classify-message.ts
+++ b/.claude/scripts/classify-message.ts
@@ -15,13 +15,13 @@ import { fileURLToPath } from "node:url";
import { debug, readStdinJson, writeHookOutput } from "./lib/hook-io.ts";
import { classify } from "./lib/matcher.ts";
import { claimUnseen } from "./lib/hint-state.ts";
+import { envPath } from "./lib/env-path.ts";
import { HANDOFF_DIR, takeHandoff } from "./lib/stop-handoff.ts";
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
// CLASSIFY_HINT_STATE routes the state file into a tmp path for tests
// (mirrors the QMD_REFRESH_SENTINEL pattern). Production never sets it.
-const STATE_PATH =
- process.env["CLASSIFY_HINT_STATE"] ?? join(SCRIPT_DIR, ".hint-state.json");
+const STATE_PATH = envPath(process.env, "CLASSIFY_HINT_STATE", join(SCRIPT_DIR, ".hint-state.json"));
type HookInput = {
readonly prompt?: unknown;
diff --git a/.claude/scripts/lib/env-path.ts b/.claude/scripts/lib/env-path.ts
new file mode 100644
index 0000000..78eea86
--- /dev/null
+++ b/.claude/scripts/lib/env-path.ts
@@ -0,0 +1,13 @@
+/**
+ * A path the environment may override, for the hooks' state files (#360).
+ *
+ * Tests point these variables at temp files so parallel test workers don't
+ * share state; production never sets them. An empty or blank value counts as
+ * unset: `VAR=` with no value is common in shell and CI templates, and as the
+ * path "" the file could be neither read nor written, so a debounce or a
+ * dedupe built on it silently stops working.
+ */
+export function envPath(env: Readonly<Record<string, string | undefined>>, name: string, fallback: string): string {
+ const set = env[name];
+ return set?.trim() ? set : fallback;
+}
diff --git a/.claude/scripts/lib/qmd-refresh.ts b/.claude/scripts/lib/qmd-refresh.ts
index b518856..588d60f 100644
--- a/.claude/scripts/lib/qmd-refresh.ts
+++ b/.claude/scripts/lib/qmd-refresh.ts
@@ -16,8 +16,9 @@
import { spawn } from "node:child_process";
import { rmSync, statSync, writeFileSync } from "node:fs";
-import { resolve as resolvePath } from "node:path";
+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 { qmdArgsWithIndex } from "./session-start.ts";
@@ -34,6 +35,15 @@ const SKIP_SEGMENTS: readonly string[] = [
"/node_modules/",
];
+/**
+ * Where the debounce sentinel lives: QMD_REFRESH_SENTINEL when set (tests point
+ * it at a temp file so parallel workers don't race on the shared one), else
+ * beside the hook scripts. Empty counts as unset (lib/env-path.ts, #360).
+ */
+export function sentinelPathFrom(env: Readonly<Record<string, string | undefined>>, scriptDir: string): string {
+ return envPath(env, "QMD_REFRESH_SENTINEL", join(scriptDir, ".qmd-refresh-sentinel"));
+}
+
/**
* Return true if a Write/Edit to `filePath` should trigger a QMD refresh.
* Accepts `.md` files that aren't under an excluded segment. Rejects non-
diff --git a/.claude/scripts/lib/stop-handoff.ts b/.claude/scripts/lib/stop-handoff.ts
index 50d0f25..937a470 100644
--- a/.claude/scripts/lib/stop-handoff.ts
+++ b/.claude/scripts/lib/stop-handoff.ts
@@ -29,14 +29,14 @@ import { mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { writeFileAtomic } from "./atomic-write.ts";
+import { envPath } from "./env-path.ts";
/**
* Where a Stop report waits for the next prompt: one place for the writer
* (stop-checklist) and the reader (classify-message), so they cannot drift
* apart. STOP_HANDOFF_DIR routes it to a tmp path for tests.
*/
-export const HANDOFF_DIR =
- process.env["STOP_HANDOFF_DIR"] ?? join(dirname(fileURLToPath(import.meta.url)), "..", ".stop-handoff");
+export const HANDOFF_DIR = envPath(process.env, "STOP_HANDOFF_DIR", join(dirname(fileURLToPath(import.meta.url)), "..", ".stop-handoff"));
/** Reports nobody picked up are removed after this long. */
export const HANDOFF_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
diff --git a/.claude/scripts/pre-compact.ts b/.claude/scripts/pre-compact.ts
index 4558db0..f7d2fd4 100644
--- a/.claude/scripts/pre-compact.ts
+++ b/.claude/scripts/pre-compact.ts
@@ -20,7 +20,7 @@ import { dirname, join, resolve as resolvePath } from "node:path";
import { fileURLToPath } from "node:url";
import { debug, readStdinJson } from "./lib/hook-io.ts";
import { isMainModule } from "./lib/main-guard.ts";
-import { triggerDebouncedRefresh } from "./lib/qmd-refresh.ts";
+import { sentinelPathFrom, triggerDebouncedRefresh } from "./lib/qmd-refresh.ts";
import { resolveProjectDir } from "./lib/project-dir.ts";
type HookInput = {
@@ -73,9 +73,7 @@ if (isMainModule(import.meta.url)) {
{
const scriptDir = dirname(fileURLToPath(import.meta.url));
triggerDebouncedRefresh({
- sentinelPath:
- process.env["QMD_REFRESH_SENTINEL"] ??
- join(scriptDir, ".qmd-refresh-sentinel"),
+ sentinelPath: sentinelPathFrom(process.env, scriptDir),
workerPath: resolvePath(scriptDir, "qmd-refresh-run.ts"),
debounceMs: 30_000,
logPrefix: "pre-compact",
diff --git a/.claude/scripts/stop-checklist.ts b/.claude/scripts/stop-checklist.ts
index a9ceb11..82884a5 100644
--- a/.claude/scripts/stop-checklist.ts
+++ b/.claude/scripts/stop-checklist.ts
@@ -60,7 +60,8 @@ import {
writeSystemMessage,
} from "./lib/hook-io.ts";
import { readOmMod } from "./lib/om-mod.ts";
-import { triggerDebouncedRefresh } from "./lib/qmd-refresh.ts";
+import { envPath } from "./lib/env-path.ts";
+import { sentinelPathFrom, triggerDebouncedRefresh } from "./lib/qmd-refresh.ts";
import { HANDOFF_DIR, pruneHandoffs, writeHandoff } from "./lib/stop-handoff.ts";
import { AGENT_PREFACE, FEEDBACK_PREFACE, FEEDBACK_TRAILER, MOD_PREFACE, stopSummary } from "./lib/stop-report.ts";
import {
@@ -80,16 +81,12 @@ const DEBOUNCE_MS = 30_000;
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
// See qmd-refresh.ts for the rationale behind the env override — it
// keeps parallel test workers from racing on the shared repo sentinel.
-const SENTINEL_PATH =
- process.env["QMD_REFRESH_SENTINEL"] ??
- join(SCRIPT_DIR, ".qmd-refresh-sentinel");
+const SENTINEL_PATH = sentinelPathFrom(process.env, SCRIPT_DIR);
const WORKER_PATH = resolvePath(SCRIPT_DIR, "qmd-refresh-run.ts");
// The last report each session was shown (lib/hint-state.ts, the same
// self-pruning, fail-open store as the classifier's hint dedupe, #107).
// STOP_CHECKLIST_STATE routes it to a tmp path for tests.
-const STATE_PATH =
- process.env["STOP_CHECKLIST_STATE"] ??
- join(SCRIPT_DIR, ".checklist-state.json");
+const STATE_PATH = envPath(process.env, "STOP_CHECKLIST_STATE", join(SCRIPT_DIR, ".checklist-state.json"));
type HookInput = {
readonly hook_event_name?: unknown;
diff --git a/.claude/scripts/tests/sentinel-path.test.ts b/.claude/scripts/tests/sentinel-path.test.ts
new file mode 100644
index 0000000..40f7e04
--- /dev/null
+++ b/.claude/scripts/tests/sentinel-path.test.ts
@@ -0,0 +1,58 @@
+/**
+ * Paths the environment may override for the hooks' state files (#360). An
+ * empty or blank value counts as unset: passed through as the path "", the
+ * QMD refresh sentinel could be neither read nor written, so every write,
+ * Stop and PreCompact spawned a refresh worker with no debounce, and nothing
+ * showed it. The Stop checklist's dedupe store, the classifier's hint store
+ * and the Stop handoff folder were read the same way.
+ */
+
+import { test, describe } from "node:test";
+import assert from "node:assert/strict";
+import { readFileSync, readdirSync, statSync } from "node:fs";
+import { dirname, join, relative } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { envPath } from "../lib/env-path.ts";
+import { sentinelPathFrom } from "../lib/qmd-refresh.ts";
+
+const SCRIPTS = dirname(dirname(fileURLToPath(import.meta.url)));
+
+describe("envPath", () => {
+ const fallback = join("scripts", "dir", ".state.json");
+
+ test("unset is the fallback", () => {
+ assert.equal(envPath({}, "SOME_STATE", fallback), fallback);
+ });
+
+ test("empty or blank is unset too, not the path \"\"", () => {
+ assert.equal(envPath({ SOME_STATE: "" }, "SOME_STATE", fallback), fallback);
+ assert.equal(envPath({ SOME_STATE: " " }, "SOME_STATE", fallback), fallback);
+ });
+
+ test("a set value is honoured as given", () => {
+ const mine = join("elsewhere", "my.state");
+ assert.equal(envPath({ SOME_STATE: mine }, "SOME_STATE", fallback), mine);
+ });
+});
+
+describe("sentinelPathFrom", () => {
+ test("an empty QMD_REFRESH_SENTINEL is the default path beside the scripts", () => {
+ const dir = join("scripts", "dir");
+ assert.equal(sentinelPathFrom({ QMD_REFRESH_SENTINEL: "" }, dir), join(dir, ".qmd-refresh-sentinel"));
+ });
+});
+
+// Every one of these is read through envPath, never with a bare `??` again.
+test("no script reads a state-path variable directly", () => {
+ const names = ["QMD_REFRESH_SENTINEL", "STOP_CHECKLIST_STATE", "CLASSIFY_HINT_STATE", "STOP_HANDOFF_DIR"];
+ const direct = new RegExp(`process\\.env(?:\\[["'](?:${names.join("|")})["']\\]|\\.(?:${names.join("|")})\\b)`);
+ const walk = (dir: string): string[] =>
+ readdirSync(dir).flatMap((name) => {
+ if (name === "node_modules" || name === "tests") return [];
+ const path = join(dir, name);
+ return statSync(path).isDirectory() ? walk(path) : /\.(ts|mjs)$/.test(name) ? [path] : [];
+ });
+ const offenders = walk(SCRIPTS).filter((file) => direct.test(readFileSync(file, "utf8")));
+ assert.deepEqual(offenders.map((f) => relative(SCRIPTS, f)), []);
+});
diff --git a/.claude/scripts/validate-write.ts b/.claude/scripts/validate-write.ts
index 6c59d6b..a94c894 100644
--- a/.claude/scripts/validate-write.ts
+++ b/.claude/scripts/validate-write.ts
@@ -34,6 +34,7 @@ import {
newNoteClusterCandidate,
} from "./lib/active-hygiene.ts";
import {
+ sentinelPathFrom,
shouldRefreshForPath,
triggerDebouncedRefresh,
} from "./lib/qmd-refresh.ts";
@@ -72,9 +73,7 @@ if (refreshed.length === 0) {
// before the skip ladder so template/root/memory paths still refresh.
const DEBOUNCE_MS = 30_000;
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
-const SENTINEL_PATH =
- process.env["QMD_REFRESH_SENTINEL"] ??
- join(SCRIPT_DIR, ".qmd-refresh-sentinel");
+const SENTINEL_PATH = sentinelPathFrom(process.env, SCRIPT_DIR);
const WORKER_PATH = resolvePath(SCRIPT_DIR, "qmd-refresh-run.ts");
if (refreshed.some(shouldRefreshForPath)) {
triggerDebouncedRefresh({