gstack icon indicating copy to clipboard operation
gstack copied to clipboard

[feature] gstack-decision-search --semantic: let GSTACK_BRAIN_SOURCE_ID name the memory source

Open exGeni opened this issue 2 weeks ago • 3 comments

What happens

gstack-decision-search --query KW --semantic adds its "Related from memory" block only when it finds the curated-memory source by path. That means a gbrain source whose local_path ends in .gstack-brain-worktree. A source that holds the same artifacts repo under another directory is never found, and the block is absent. Please extend source resolution while keeping the scoped search and the silent fallback.

Checked at 4414f4cc9377a9df06c7847433fe567ba6398aa8 (main):

  • lib/gstack-decision-semantic.ts:46-50 keeps only rows whose local_path ends with BRAIN_WORKTREE_SUFFIX (:24). :85-89 returns null when there is none, "rather than searching UNSCOPED", and test/gstack-decision-semantic.test.ts:116-127 pins that. The module reads no environment variable or config key. That includes GSTACK_BRAIN_WORKTREE, so moving the worktree with the wireup's own override (bin/gstack-gbrain-source-wireup:50) also loses the block unless the new directory keeps the same name.
  • The two other readers of the artifacts source accept an explicit id. bin/gstack-gbrain-source-wireup:151-153 takes GSTACK_BRAIN_SOURCE_ID as the source id. bin/gstack-gbrain-sync.ts:274-275 uses it ahead of its path match (isArtifactsSourceRow, :248-262, which also honors GSTACK_BRAIN_WORKTREE and the state root).
  • The docs do not say --semantic needs the worktree layout. CLAUDE.md:570-571 and CHANGELOG.md:12236 say it appends related hits "from gbrain memory when it's up". The search-guidance block (sync-gbrain/SKILL.md.tmpl:497-498) answers "What did we decide last time?" with gbrain search "<terms>" --source gstack-artifacts-<user>, by id.

Our layout: the agent machine is also the brain host. The artifacts repo is registered as a host-side clone with the id gstack derives itself (derive_source_id, wireup :160-164). gstack prints this kind of registration for a separate brain host (bin/gstack-artifacts-init:504-523, docs/gbrain-sync.md:46-49); the same-host case is close to #3539 and #3727. Ids and paths below are renamed:

$ gstack-gbrain-source-wireup --probe
source_id=acme-gstack-artifacts
worktree=~/.gstack-brain-worktree
worktree_status=absent
gbrain=ok (0.60.675.0)
source_status=registered (different path)

$ gstack-decision-search --query "<a word in several checkpoints>" --semantic
- <one decision line from the file store>
$                                   # no "Related from memory" block

$ gbrain search "<the same word>" --source acme-gstack-artifacts
[0.52] projects/.../checkpoints/20261016-... -- ## Working on: ...

Steps to reproduce

Hermetic, in the style of test/gstack-decision-semantic.test.ts: a fake gbrain on PATH reports one federated source, and only its local_path changes.

#!/usr/bin/env bash
set -eu
GSTACK_ROOT="${GSTACK_ROOT:-$HOME/.claude/skills/gstack}"
T=$(mktemp -d); mkdir -p "$T/shim"
cat > "$T/shim/gbrain" <<'SH'
#!/usr/bin/env bash
if [ "$1" = "sources" ]; then
  echo "{\"sources\":[{\"id\":\"acme-gstack-artifacts\",\"local_path\":\"$SRC_PATH\",\"federated\":true}]}"
  exit 0
fi
if [ "$1" = "search" ]; then
  echo "[0.52] projects/acme/checkpoints/20261016-example -- Working on: example"
  exit 0
fi
exit 1
SH
chmod +x "$T/shim/gbrain"
cat > "$T/probe.ts" <<TS
const m = await import("$GSTACK_ROOT/lib/gstack-decision-semantic.ts");
console.log("  resolveMemorySourceId:", m.resolveMemorySourceId());
console.log("  semanticRecall:", JSON.stringify(m.semanticRecall("example")));
TS
run() { PATH="$T/shim:$PATH" bun --no-env-file --config=/dev/null "$T/probe.ts"; }
echo "A. source is a clone elsewhere on the host"
GSTACK_BRAIN_SOURCE_ID= SRC_PATH=/srv/brain/sources/acme-gstack-artifacts run
echo "B. same source at the wireup worktree path"
GSTACK_BRAIN_SOURCE_ID= SRC_PATH="$HOME/.gstack-brain-worktree" run
echo "C. as A, with GSTACK_BRAIN_SOURCE_ID set"
GSTACK_BRAIN_SOURCE_ID=acme-gstack-artifacts SRC_PATH=/srv/brain/sources/acme-gstack-artifacts run
rm -rf "$T"

Output on gstack 1.99.2.0, whose lib/gstack-decision-semantic.ts is byte-identical to 4414f4cc:

A. source is a clone elsewhere on the host
  resolveMemorySourceId: null
  semanticRecall: null
B. same source at the wireup worktree path
  resolveMemorySourceId: acme-gstack-artifacts
  semanticRecall: [{"score":0.52,"slug":"projects/acme/checkpoints/20261016-example","snippet":"Working on: example"}]
C. as A, with GSTACK_BRAIN_SOURCE_ID set
  resolveMemorySourceId: null
  semanticRecall: null

A is the gap; C shows the override is not read.

What would help

  1. Let GSTACK_BRAIN_SOURCE_ID name the source, with the same precedence gstack-gbrain-sync.ts:274-275 uses. When it is set, use it if a source with that id is registered, and otherwise return null. When it is unset, keep the worktree match. The search stays scoped, and the operator who sets the variable is responsible for naming the curated-memory source. The companion pull request (#4328) carries this change and two tests (named source wins over a worktree source; an unregistered id gives null). With it, case C returns the same single hit as B, and A and B are unchanged.
  2. Optional, so that no setting is needed, also accept the row whose id equals what derive_source_id yields from the artifacts repo's origin.
  3. The comment at lib/gstack-decision-semantic.ts:33-35 says "the caller then searches unscoped", which contradicts :85-89. The PR corrects it.

Alternatives

  • gbrain search "<terms>" --source <id> works today. It is the route the search-guidance block gives; only the --semantic flag misses it.
  • Re-registering through the wireup would move the source to ~/.gstack-brain-worktree. With --probe reporting registered (different path) and a different directory name, its drift branch runs gbrain sources remove <id> --yes and gbrain sources add <id> --path <worktree> --federated (bin/gstack-gbrain-source-wireup:330-337). Registering the clone under a directory named .gstack-brain-worktree would also pass the suffix check, but gbrain has no sources update, so it means the same remove and add. Another process on this host maintains that checkout, so re-registering it is a larger change than one lookup.

Environment

  • Host: Claude Code 2.1.309; OS: Linux
  • gstack 1.99.2.0 installed (bc858ffc); code and docs cited at 4414f4cc
  • gbrain 0.60.675.0
  • gstack doctor 1.99.2.0: 15 ok, 0 warn, 0 not configured, 0 fail
  • Searched open and closed issues and PRs for gstack-decision-search --semantic, gstack-decision-semantic, resolveMemorySourceId, semanticRecall, GSTACK_BRAIN_SOURCE_ID, gstack-brain-worktree, "artifacts source id", "wireup different path". No issue about how --semantic finds its source. Related: #2516, #3810, #3539, #3727.

exGeni avatar Nov 13 '26 01:11 exGeni

Thanks for the hermetic repro. On main, lib/gstack-decision-semantic.ts still finds the source only by the .gstack-brain-worktree path and never reads GSTACK_BRAIN_SOURCE_ID, while the wireup and sync already honor it. Matching their precedence is a small, plainly correct change, so we're taking it: a maintainer agent will rebuild it on current main (keeping the scoped search and the silent fallback) and close this issue with a link.

capy-ai[bot] avatar Nov 15 '26 01:11 capy-ai[bot]

Picking this up. A maintainer agent working through a batch of fixes (our tracking thread GSTA-804) will first reproduce this on current main. If it reproduces, the agent fixes it on main and we'll close this issue with a link when the change lands. If it no longer reproduces, we'll close it and say what changed. PR #4328 is the starting point.

capy-ai[bot] avatar Nov 22 '26 05:11 capy-ai[bot]

Fixed in #4685, now merged. gstack-decision-search --semantic now searches the source GSTACK_BRAIN_SOURCE_ID names. An unregistered name falls back to the file results, never an unscoped search. Thanks for the clear repro.

capy-ai[bot] avatar Nov 22 '26 11:11 capy-ai[bot]