fix(sync-gbrain): read source_id from the pretty-printed sync state with jq

The sync-state file is written with JSON.stringify(state, null, 2)
(bin/gstack-gbrain-sync.ts), so the minified-JSON grep never matched and
SOURCE_ID was always empty, forcing CODE_PAGES to 0 even when an indexed
code corpus exists. Use a jq recursive lookup instead, which handles any
formatting and still degrades to empty when the file is missing.

Addresses review feedback from @time-attack.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
harjoth 2026-07-23 22:50:02 -07:00
parent ecd361c3ae
commit f3595995ac
2 changed files with 12 additions and 8 deletions

View File

@ -885,8 +885,9 @@ tmp-file + atomic rename. Concurrent runs are blocked by a lock file at
After the sync run, query gbrain for the cwd source's page_count:
```bash
SOURCE_ID=$(grep -o '"source_id":"[^"]*"' ~/.gstack/.gbrain-sync-state.json 2>/dev/null \
| head -1 | sed 's/.*"source_id":"//;s/".*//')
# NOTE: the sync-state file is pretty-printed (JSON.stringify(state, null, 2)
# in bin/gstack-gbrain-sync.ts), so a minified-JSON grep never matches. Use jq.
SOURCE_ID=$(jq -r 'first(.. | objects | .source_id? // empty)' ~/.gstack/.gbrain-sync-state.json 2>/dev/null)
PAGES=$(gbrain sources list --json 2>/dev/null \
| jq -r --arg id "$SOURCE_ID" '.sources[] | select(.id==$id) | .page_count' 2>/dev/null \
|| echo 0)
@ -949,8 +950,9 @@ gbrain delete "$SLUG" 2>/dev/null || true
# On a markdown-only brain (no code source, or one that can't register because
# its path overlaps the `default` markdown source) this is 0, so the
# code-search guidance must gate on it, not on CAPABILITY_OK.
SOURCE_ID=$(grep -o '"source_id":"[^"]*"' ~/.gstack/.gbrain-sync-state.json 2>/dev/null \
| head -1 | sed 's/.*"source_id":"//;s/".*//')
# NOTE: the sync-state file is pretty-printed (JSON.stringify(state, null, 2)
# in bin/gstack-gbrain-sync.ts), so a minified-JSON grep never matches. Use jq.
SOURCE_ID=$(jq -r 'first(.. | objects | .source_id? // empty)' ~/.gstack/.gbrain-sync-state.json 2>/dev/null)
CODE_PAGES=$(gbrain sources list --json 2>/dev/null \
| jq -r --arg id "$SOURCE_ID" '.sources[] | select(.id==$id) | .page_count' 2>/dev/null \
|| echo 0)

View File

@ -190,8 +190,9 @@ tmp-file + atomic rename. Concurrent runs are blocked by a lock file at
After the sync run, query gbrain for the cwd source's page_count:
```bash
SOURCE_ID=$(grep -o '"source_id":"[^"]*"' ~/.gstack/.gbrain-sync-state.json 2>/dev/null \
| head -1 | sed 's/.*"source_id":"//;s/".*//')
# NOTE: the sync-state file is pretty-printed (JSON.stringify(state, null, 2)
# in bin/gstack-gbrain-sync.ts), so a minified-JSON grep never matches. Use jq.
SOURCE_ID=$(jq -r 'first(.. | objects | .source_id? // empty)' ~/.gstack/.gbrain-sync-state.json 2>/dev/null)
PAGES=$(gbrain sources list --json 2>/dev/null \
| jq -r --arg id "$SOURCE_ID" '.sources[] | select(.id==$id) | .page_count' 2>/dev/null \
|| echo 0)
@ -254,8 +255,9 @@ gbrain delete "$SLUG" 2>/dev/null || true
# On a markdown-only brain (no code source, or one that can't register because
# its path overlaps the `default` markdown source) this is 0, so the
# code-search guidance must gate on it, not on CAPABILITY_OK.
SOURCE_ID=$(grep -o '"source_id":"[^"]*"' ~/.gstack/.gbrain-sync-state.json 2>/dev/null \
| head -1 | sed 's/.*"source_id":"//;s/".*//')
# NOTE: the sync-state file is pretty-printed (JSON.stringify(state, null, 2)
# in bin/gstack-gbrain-sync.ts), so a minified-JSON grep never matches. Use jq.
SOURCE_ID=$(jq -r 'first(.. | objects | .source_id? // empty)' ~/.gstack/.gbrain-sync-state.json 2>/dev/null)
CODE_PAGES=$(gbrain sources list --json 2>/dev/null \
| jq -r --arg id "$SOURCE_ID" '.sources[] | select(.id==$id) | .page_count' 2>/dev/null \
|| echo 0)