From f3595995ac5a56a4e12cd41e82294aab178c0818 Mon Sep 17 00:00:00 2001 From: harjoth Date: Thu, 23 Jul 2026 22:50:02 -0700 Subject: [PATCH] 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 --- sync-gbrain/SKILL.md | 10 ++++++---- sync-gbrain/SKILL.md.tmpl | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/sync-gbrain/SKILL.md b/sync-gbrain/SKILL.md index 18fed9fdf..7fb6ffc17 100644 --- a/sync-gbrain/SKILL.md +++ b/sync-gbrain/SKILL.md @@ -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) diff --git a/sync-gbrain/SKILL.md.tmpl b/sync-gbrain/SKILL.md.tmpl index 4db602cbe..c27b5413a 100644 --- a/sync-gbrain/SKILL.md.tmpl +++ b/sync-gbrain/SKILL.md.tmpl @@ -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)