feat: extend gstack-gbrain-detect with mcp_mode + artifacts_remote

Adds two new fields to detect's JSON output:

- gbrain_mcp_mode: local-stdio | remote-http | none
  Resolved via 3-tier fallback (codex Finding D3): claude mcp get --json
  → claude mcp list text-grep → ~/.claude.json jq read. If Anthropic moves
  the file format, the first two tiers absorb it.

- gstack_artifacts_remote: HTTPS URL from ~/.gstack-artifacts-remote.txt
  Falls back to ~/.gstack-brain-remote.txt during the v1.27.0.0 migration
  window so detect doesn't return empty between upgrade and migration.

Existing detect tests still pass (15/15). New 19 tests cover every fallback
tier independently, plus a schema regression for /sync-gbrain compat.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-05-06 09:20:10 -07:00
parent a4cadb42d8
commit b06184e097
2 changed files with 353 additions and 2 deletions
+78 -2
View File
@@ -11,8 +11,10 @@
# "gbrain_config_exists": true|false,
# "gbrain_engine": "pglite"|"postgres" | null,
# "gbrain_doctor_ok": true|false,
# "gbrain_mcp_mode": "local-stdio"|"remote-http"|"none",
# "gstack_brain_sync_mode": "off"|"artifacts-only"|"full",
# "gstack_brain_git": true|false
# "gstack_brain_git": true|false,
# "gstack_artifacts_remote": "https://..." | ""
# }
#
# The /setup-gbrain skill reads this once at startup to decide which path
@@ -92,6 +94,76 @@ if [ -d "$STATE_DIR/.git" ]; then
gstack_brain_git=true
fi
# --- gbrain_mcp_mode: local-stdio | remote-http | none ---
# Defense-in-depth fallback chain (intentional ordering, do not reorder):
# 1. `claude mcp get gbrain --json` — public CLI surface, structured output
# 2. `claude mcp list` text-grep — older claude versions without --json
# 3. `~/.claude.json` jq read — last resort if `claude` isn't on PATH
# Fallback chain logged because if Anthropic moves the file or renames keys,
# the third tier breaks silently; the first two tiers should catch it.
gbrain_mcp_mode="none"
if command -v claude >/dev/null 2>&1; then
# Tier 1: claude mcp get --json
if mcp_get_json=$(claude mcp get gbrain --json 2>/dev/null); then
if echo "$mcp_get_json" | jq -e '.' >/dev/null 2>&1; then
mtype=$(echo "$mcp_get_json" | jq -r '.type // .transport // empty' 2>/dev/null)
mcommand=$(echo "$mcp_get_json" | jq -r '.command // empty' 2>/dev/null)
murl=$(echo "$mcp_get_json" | jq -r '.url // empty' 2>/dev/null)
case "$mtype" in
http|sse) gbrain_mcp_mode="remote-http" ;;
stdio) gbrain_mcp_mode="local-stdio" ;;
*)
# Newer claude versions may emit just url + command; infer.
if [ -n "$murl" ]; then gbrain_mcp_mode="remote-http"
elif [ -n "$mcommand" ]; then gbrain_mcp_mode="local-stdio"
fi
;;
esac
fi
fi
# Tier 2: claude mcp list text-grep (only if Tier 1 didn't resolve)
if [ "$gbrain_mcp_mode" = "none" ]; then
if mcp_list=$(claude mcp list 2>/dev/null); then
gbrain_line=$(echo "$mcp_list" | grep -E '^gbrain:' || true)
if [ -n "$gbrain_line" ]; then
if echo "$gbrain_line" | grep -q 'http\|HTTP'; then
gbrain_mcp_mode="remote-http"
else
gbrain_mcp_mode="local-stdio"
fi
fi
fi
fi
fi
# Tier 3: ~/.claude.json jq read (only if claude binary or earlier tiers failed)
if [ "$gbrain_mcp_mode" = "none" ]; then
if [ -f "$HOME/.claude.json" ]; then
# Look for a gbrain MCP server entry. Type field disambiguates http vs stdio.
mtype=$(jq -r '.mcpServers.gbrain.type // .mcpServers.gbrain.transport // empty' "$HOME/.claude.json" 2>/dev/null)
murl=$(jq -r '.mcpServers.gbrain.url // empty' "$HOME/.claude.json" 2>/dev/null)
mcommand=$(jq -r '.mcpServers.gbrain.command // empty' "$HOME/.claude.json" 2>/dev/null)
case "$mtype" in
url|http|sse) gbrain_mcp_mode="remote-http" ;;
stdio) gbrain_mcp_mode="local-stdio" ;;
*)
if [ -n "$murl" ]; then gbrain_mcp_mode="remote-http"
elif [ -n "$mcommand" ]; then gbrain_mcp_mode="local-stdio"
fi
;;
esac
fi
fi
# --- artifacts remote URL (post-rename) with brain-* fallback during the
# migration window (gstack-upgrade migration runs the rename). ---
gstack_artifacts_remote=""
if [ -f "$HOME/.gstack-artifacts-remote.txt" ]; then
gstack_artifacts_remote=$(head -1 "$HOME/.gstack-artifacts-remote.txt" 2>/dev/null | tr -d '[:space:]' || true)
elif [ -f "$HOME/.gstack-brain-remote.txt" ]; then
# Pre-migration fallback. Migration v1.27.0.0 will mv this to the new path.
gstack_artifacts_remote=$(head -1 "$HOME/.gstack-brain-remote.txt" 2>/dev/null | tr -d '[:space:]' || true)
fi
# Emit single-object JSON.
jq -n \
--argjson on_path "$gbrain_on_path" \
@@ -99,14 +171,18 @@ jq -n \
--argjson config_exists "$gbrain_config_exists" \
--argjson engine "$gbrain_engine" \
--argjson doctor_ok "$gbrain_doctor_ok" \
--arg mcp_mode "$gbrain_mcp_mode" \
--arg sync_mode "$gstack_brain_sync_mode" \
--argjson brain_git "$gstack_brain_git" \
--arg artifacts_remote "$gstack_artifacts_remote" \
'{
gbrain_on_path: $on_path,
gbrain_version: $version,
gbrain_config_exists: $config_exists,
gbrain_engine: $engine,
gbrain_doctor_ok: $doctor_ok,
gbrain_mcp_mode: $mcp_mode,
gstack_brain_sync_mode: $sync_mode,
gstack_brain_git: $brain_git
gstack_brain_git: $brain_git,
gstack_artifacts_remote: $artifacts_remote
}'