diff --git a/changelog.d/semantic-source-id.md b/changelog.d/semantic-source-id.md new file mode 100644 index 00000000000..97a5a9fd9cb --- /dev/null +++ b/changelog.d/semantic-source-id.md @@ -0,0 +1 @@ +- **`gstack-decision-search --semantic` honors `GSTACK_BRAIN_SOURCE_ID`.** The related-from-memory block found the artifacts source only when its gbrain path ended in `.gstack-brain-worktree`, so a source registered elsewhere, such as a clone on the brain host, never got the block. When `GSTACK_BRAIN_SOURCE_ID` is set, the semantic lookup now uses that source if it is registered and degrades to the file results if it is not, the precedence `gstack-gbrain-sync` already uses. Unset, nothing changes, and the search never runs unscoped. diff --git a/lib/gstack-decision-semantic.ts b/lib/gstack-decision-semantic.ts index 242fdfc7095..bb7dee9ffd6 100644 --- a/lib/gstack-decision-semantic.ts +++ b/lib/gstack-decision-semantic.ts @@ -12,8 +12,9 @@ * Surface reality (verified against gbrain 0.42.x, not guessed): * - `gbrain search ""` prints TEXT lines `[score] slug -- snippet`, NOT JSON * (so we parse the text surface; execGbrainJson would always null here). - * - The curated-memory source is the one whose local_path is the gstack brain - * worktree (`~/.gstack-brain-worktree`), id `default` by convention — NOT a + * - The curated-memory source is the one GSTACK_BRAIN_SOURCE_ID names, when set; + * otherwise the one whose local_path is the gstack brain worktree + * (`~/.gstack-brain-worktree`), id `default` by convention — NOT a * `gstack-brain-` id. Scoping search to it keeps code/doc corpora out. */ @@ -30,9 +31,11 @@ export interface SemanticHit { } /** - * Resolve the curated-memory source id (the gstack brain worktree). Returns null - * when gbrain is down/unparseable OR no worktree-backed source is registered — the - * caller then searches unscoped (best-effort) rather than failing. + * Resolve the curated-memory source id. When GSTACK_BRAIN_SOURCE_ID is nonempty + * (the override gstack-gbrain-source-wireup and gstack-gbrain-sync honor), return + * it only if that source is registered, else null. When it is empty or unset, + * resolve the worktree-backed source. Also null when gbrain is down/unparseable; + * the caller then degrades to the file results and never searches unscoped. */ export function resolveMemorySourceId(env?: NodeJS.ProcessEnv): string | null { const r = spawnGbrain(["sources", "list", "--json"], { baseEnv: env, timeout: TIMEOUT_MS }); @@ -43,6 +46,8 @@ export function resolveMemorySourceId(env?: NodeJS.ProcessEnv): string | null { } catch { return null; } + const explicit = (env ?? process.env).GSTACK_BRAIN_SOURCE_ID; + if (explicit) return rows.some((s) => s.id === explicit) ? explicit : null; const atWorktree = rows.filter( (s) => typeof s.local_path === "string" && s.local_path.endsWith(BRAIN_WORKTREE_SUFFIX), ); @@ -82,9 +87,10 @@ export function semanticRecall( limit = 3, ): SemanticHit[] | null { if (!query.trim()) return null; - // Require the curated-memory source. If it's absent (gbrain down OR no worktree-backed - // source), degrade to null rather than searching UNSCOPED — an unscoped search pulls - // code/doc corpora that would be mislabeled as "related decisions" (Codex finding). + // Require the curated-memory source. If it's absent (gbrain unavailable, the explicit + // id unregistered, or, with no explicit id, no worktree-backed source), degrade to + // null rather than searching UNSCOPED — an unscoped search pulls code/doc corpora + // that would be mislabeled as "related decisions" (Codex finding). const sourceId = resolveMemorySourceId(env); if (!sourceId) return null; const r = spawnGbrain(["search", query, "--source", sourceId], { baseEnv: env, timeout: TIMEOUT_MS }); diff --git a/test/gstack-decision-bins.test.ts b/test/gstack-decision-bins.test.ts index c91224078c7..b5469214b62 100644 --- a/test/gstack-decision-bins.test.ts +++ b/test/gstack-decision-bins.test.ts @@ -244,6 +244,7 @@ describe("gstack-decision-search --semantic (optional gbrain enhancement)", () = } function searchWithPath(args: string, pathPrefix?: string): string { const env = { ...process.env, GSTACK_HOME: tmpDir } as NodeJS.ProcessEnv; + delete env.GSTACK_BRAIN_SOURCE_ID; // a developer's override would bypass the shim's worktree source if (pathPrefix) env.PATH = `${pathPrefix}:${process.env.PATH}`; try { return execSync(`${SEARCH} ${args}`, { cwd: ROOT, env, encoding: "utf-8", timeout: 20000 }).trim(); diff --git a/test/gstack-decision-semantic.test.ts b/test/gstack-decision-semantic.test.ts index 71de35cb68e..89682942dd5 100644 --- a/test/gstack-decision-semantic.test.ts +++ b/test/gstack-decision-semantic.test.ts @@ -126,6 +126,42 @@ exit 1 expect(semanticRecall("anything", env())).toBeNull(); }); + test("GSTACK_BRAIN_SOURCE_ID names a source outside the worktree and wins over it", () => { + writeShim( + `#!/usr/bin/env bash +if [ "$1" = "sources" ]; then + echo '{"sources":[{"id":"default","local_path":"/u/.gstack-brain-worktree"},{"id":"team-artifacts","local_path":"/srv/brain/team-artifacts"}]}' + exit 0 +fi +if [ "$1" = "search" ]; then + if printf '%s ' "$@" | grep -q -- "--source team-artifacts"; then + echo "[0.80] decisions/bar -- We kept the host-side clone" + else + echo "[0.80] WRONG-SOURCE -- not the named source" + fi + exit 0 +fi +exit 1 +`, + ); + const named = { ...env(), GSTACK_BRAIN_SOURCE_ID: "team-artifacts" }; + expect(resolveMemorySourceId(named)).toBe("team-artifacts"); // not the worktree's "default" + expect(semanticRecall("clone", named)![0].slug).toBe("decisions/bar"); + }); + + test("an unregistered GSTACK_BRAIN_SOURCE_ID degrades to null (no fallback, no unscoped search)", () => { + writeShim( + `#!/usr/bin/env bash +if [ "$1" = "sources" ]; then echo '{"sources":[{"id":"default","local_path":"/u/.gstack-brain-worktree"}]}'; exit 0; fi +if [ "$1" = "search" ]; then echo "[0.50] code/x -- should not be searched"; exit 0; fi +exit 1 +`, + ); + const named = { ...env(), GSTACK_BRAIN_SOURCE_ID: "missing-source" }; + expect(resolveMemorySourceId(named)).toBeNull(); + expect(semanticRecall("anything", named)).toBeNull(); + }); + test("degrades to null when gbrain search exits non-zero", () => { writeShim( `#!/usr/bin/env bash