Skip to content
Closed
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
1 change: 1 addition & 0 deletions changelog.d/semantic-source-id.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 14 additions & 8 deletions lib/gstack-decision-semantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* Surface reality (verified against gbrain 0.42.x, not guessed):
* - `gbrain search "<q>"` 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-<user>` id. Scoping search to it keeps code/doc corpora out.
*/

Expand All @@ -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 });
Expand All @@ -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),
);
Expand Down Expand Up @@ -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 });
Expand Down
1 change: 1 addition & 0 deletions test/gstack-decision-bins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions test/gstack-decision-semantic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading