mirror of
https://github.com/garrytan/gstack.git
synced 2026-06-26 03:30:05 +02:00
chore: merge origin/main (v1.37.0.0 split-engine gbrain) + bump to v1.38.0.0
Main shipped v1.37.0.0 (split-engine gbrain) while this PR was in review. Merge resolved cleanly on bin/gstack-artifacts-init (allowlist patterns from both branches coexist). CHANGELOG entry retained for our changes, version header bumped from 1.36.0.0 to 1.38.0.0. Migration script renamed: gstack-upgrade/migrations/v1.36.0.0.sh -> v1.38.0.0.sh, with internal references and the test file updated to match. VERSION + package.json: 1.38.0.0. Regenerated SKILL.md across 12 hosts and refreshed golden fixtures. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -234,6 +234,11 @@ retros/*.md
|
||||
developer-profile.json
|
||||
builder-journey.md
|
||||
builder-profile.jsonl
|
||||
# Transcripts staged in remote-http MCP mode (per plan D11 split-engine).
|
||||
# gstack-memory-ingest persists per-run dirs here when local gbrain import
|
||||
# is skipped; brain admin pulls + indexes into the remote brain.
|
||||
transcripts/run-*/*.md
|
||||
transcripts/run-*/**/*.md
|
||||
# NOT synced (machine-local UX state):
|
||||
# projects/*/question-preferences.json (per-machine UX preferences)
|
||||
# projects/*/question-log.jsonl (audit/derivation log stays with preferences)
|
||||
@@ -255,7 +260,9 @@ cat > "$GSTACK_HOME/.brain-privacy-map.json" <<'EOF'
|
||||
{"pattern": "builder-journey.md", "class": "artifact"},
|
||||
{"pattern": "projects/*/timeline.jsonl", "class": "behavioral"},
|
||||
{"pattern": "developer-profile.json", "class": "behavioral"},
|
||||
{"pattern": "builder-profile.jsonl", "class": "behavioral"}
|
||||
{"pattern": "builder-profile.jsonl", "class": "behavioral"},
|
||||
{"pattern": "transcripts/run-*/*.md", "class": "behavioral"},
|
||||
{"pattern": "transcripts/run-*/**/*.md", "class": "behavioral"}
|
||||
]
|
||||
EOF
|
||||
|
||||
|
||||
+211
-176
@@ -1,188 +1,223 @@
|
||||
#!/usr/bin/env bash
|
||||
# gstack-gbrain-detect — emit current gbrain/gstack-brain state as JSON.
|
||||
#
|
||||
# Usage:
|
||||
# gstack-gbrain-detect
|
||||
#
|
||||
# Output (always valid JSON, even when every check is false):
|
||||
# {
|
||||
# "gbrain_on_path": true|false,
|
||||
# "gbrain_version": "0.18.2" | null,
|
||||
# "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_artifacts_remote": "https://..." | ""
|
||||
# }
|
||||
#
|
||||
# The /setup-gbrain skill reads this once at startup to decide which path
|
||||
# branches are live and which steps can be skipped. Never modifies state;
|
||||
# pure introspection. Exits 0 unless `jq` is missing.
|
||||
#
|
||||
# Env:
|
||||
# GSTACK_HOME — override ~/.gstack for gstack-brain-* state lookups.
|
||||
set -euo pipefail
|
||||
#!/usr/bin/env -S bun run
|
||||
/**
|
||||
* gstack-gbrain-detect — emit current gbrain/gstack-brain state as JSON.
|
||||
*
|
||||
* Rewritten from bash to TypeScript in v{X.Y.Z.0} to share the engine-status
|
||||
* classifier with bin/gstack-gbrain-sync.ts. Single source of truth via
|
||||
* lib/gbrain-local-status.ts. Filename and exec semantics unchanged: callers
|
||||
* just shell out to the file path; the bun shebang resolves at runtime.
|
||||
*
|
||||
* Output (always valid JSON, even when every check is false):
|
||||
* {
|
||||
* "gbrain_on_path": true|false,
|
||||
* "gbrain_version": "0.18.2" | null,
|
||||
* "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_artifacts_remote": "https://..." | "",
|
||||
* "gbrain_local_status": "ok"|"no-cli"|"missing-config"|"broken-config"|"broken-db"
|
||||
* }
|
||||
*
|
||||
* Backward compatibility (per plan codex #5): the 9 pre-existing fields stay
|
||||
* identical in name + type + value semantics. One new field added:
|
||||
* gbrain_local_status. Key order may differ from the bash version's `jq -n`
|
||||
* output — downstream parsers must not depend on key order (none currently do).
|
||||
*
|
||||
* Env:
|
||||
* GSTACK_HOME — override ~/.gstack for state lookups (used by tests).
|
||||
* HOME — effective user home (drives ~/.gbrain/config.json path).
|
||||
* GSTACK_DETECT_NO_CACHE=1 — bypass the 60s local-status cache.
|
||||
*/
|
||||
|
||||
STATE_DIR="${GSTACK_HOME:-$HOME/.gstack}"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONFIG_BIN="$SCRIPT_DIR/gstack-config"
|
||||
GBRAIN_CONFIG="$HOME/.gbrain/config.json"
|
||||
import { execFileSync } from "child_process";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
|
||||
die() { echo "gstack-gbrain-detect: $*" >&2; exit 2; }
|
||||
import {
|
||||
localEngineStatus,
|
||||
resolveGbrainBin,
|
||||
readGbrainVersion,
|
||||
} from "../lib/gbrain-local-status";
|
||||
|
||||
require_jq() {
|
||||
command -v jq >/dev/null 2>&1 || die "jq is required. Install with: brew install jq"
|
||||
const STATE_DIR = process.env.GSTACK_HOME || join(userHome(), ".gstack");
|
||||
const SCRIPT_DIR = __dirname;
|
||||
const CONFIG_BIN = join(SCRIPT_DIR, "gstack-config");
|
||||
const GBRAIN_CONFIG = join(userHome(), ".gbrain", "config.json");
|
||||
const CLAUDE_JSON = join(userHome(), ".claude.json");
|
||||
|
||||
function userHome(): string {
|
||||
return process.env.HOME || homedir();
|
||||
}
|
||||
require_jq
|
||||
|
||||
# --- gbrain binary presence + version ---
|
||||
gbrain_on_path=false
|
||||
gbrain_version=null
|
||||
if command -v gbrain >/dev/null 2>&1; then
|
||||
gbrain_on_path=true
|
||||
# Format versions as JSON strings; gbrain --version may print other chatter.
|
||||
v=$(gbrain --version 2>/dev/null | head -1 | tr -d '[:space:]' || true)
|
||||
if [ -n "$v" ]; then
|
||||
gbrain_version=$(jq -Rn --arg v "$v" '$v')
|
||||
fi
|
||||
fi
|
||||
function tryExec(cmd: string, args: string[], timeoutMs = 5_000): string | null {
|
||||
try {
|
||||
return execFileSync(cmd, args, {
|
||||
encoding: "utf-8",
|
||||
timeout: timeoutMs,
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
}).trim();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
# --- gbrain config file ---
|
||||
gbrain_config_exists=false
|
||||
gbrain_engine=null
|
||||
if [ -f "$GBRAIN_CONFIG" ]; then
|
||||
gbrain_config_exists=true
|
||||
# Engine is defensively parsed; an invalid config returns null, not a crash.
|
||||
engine_raw=$(jq -r '.engine // empty' "$GBRAIN_CONFIG" 2>/dev/null || true)
|
||||
case "$engine_raw" in
|
||||
pglite|postgres) gbrain_engine=$(jq -Rn --arg e "$engine_raw" '$e') ;;
|
||||
esac
|
||||
fi
|
||||
function tryReadJSON(path: string): unknown | null {
|
||||
if (!existsSync(path)) return null;
|
||||
try {
|
||||
return JSON.parse(readFileSync(path, "utf-8"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
# --- gbrain doctor health ---
|
||||
# Doctor is wrapped in `timeout 5s` to match the /health D6 pattern and avoid
|
||||
# the detect step hanging the skill when gbrain is broken or its DB is
|
||||
# unreachable. Any nonzero exit or non-"ok"/"warnings" status → false.
|
||||
gbrain_doctor_ok=false
|
||||
if [ "$gbrain_on_path" = "true" ]; then
|
||||
# Use `timeout` if available; some minimal macs use gtimeout from coreutils.
|
||||
timeout_bin=""
|
||||
if command -v timeout >/dev/null 2>&1; then timeout_bin="timeout 5s"
|
||||
elif command -v gtimeout >/dev/null 2>&1; then timeout_bin="gtimeout 5s"
|
||||
fi
|
||||
if doctor_json=$(eval "$timeout_bin gbrain doctor --json" 2>/dev/null); then
|
||||
status=$(echo "$doctor_json" | jq -r '.status // empty' 2>/dev/null || true)
|
||||
case "$status" in
|
||||
ok|warnings) gbrain_doctor_ok=true ;;
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
// --- gbrain binary presence + version ---
|
||||
// Uses the shared memoized resolvers from lib/gbrain-local-status.ts so
|
||||
// detect and the classifier share probe results within one process.
|
||||
function detectGbrain(): { onPath: boolean; version: string | null } {
|
||||
const bin = resolveGbrainBin();
|
||||
if (!bin) return { onPath: false, version: null };
|
||||
const verRaw = readGbrainVersion();
|
||||
if (!verRaw) return { onPath: true, version: null };
|
||||
// Match bash behavior: head -1 | tr -d '[:space:]'
|
||||
const version = verRaw.split("\n")[0].replace(/\s+/g, "") || null;
|
||||
return { onPath: true, version };
|
||||
}
|
||||
|
||||
# --- artifacts sync state (renamed from gbrain_sync_mode in v1.27.0.0) ---
|
||||
gstack_brain_sync_mode="off"
|
||||
if [ -x "$CONFIG_BIN" ]; then
|
||||
mode=$("$CONFIG_BIN" get artifacts_sync_mode 2>/dev/null || true)
|
||||
case "$mode" in
|
||||
off|artifacts-only|full) gstack_brain_sync_mode="$mode" ;;
|
||||
esac
|
||||
fi
|
||||
// --- gbrain config existence + engine kind ---
|
||||
function detectConfig(): { exists: boolean; engine: "pglite" | "postgres" | null } {
|
||||
if (!existsSync(GBRAIN_CONFIG)) return { exists: false, engine: null };
|
||||
const parsed = tryReadJSON(GBRAIN_CONFIG) as { engine?: string } | null;
|
||||
if (!parsed) return { exists: true, engine: null };
|
||||
if (parsed.engine === "pglite" || parsed.engine === "postgres") {
|
||||
return { exists: true, engine: parsed.engine };
|
||||
}
|
||||
return { exists: true, engine: null };
|
||||
}
|
||||
|
||||
gstack_brain_git=false
|
||||
if [ -d "$STATE_DIR/.git" ]; then
|
||||
gstack_brain_git=true
|
||||
fi
|
||||
// --- gbrain doctor health (any nonzero exit or non-"ok"/"warnings" status → false) ---
|
||||
//
|
||||
// Uses --fast to avoid hanging on a dead DB. Per the local-status classifier
|
||||
// (which probes DB directly via `gbrain sources list`), gbrain_doctor_ok is a
|
||||
// coarse health summary, not engine-reachability — that's gbrain_local_status.
|
||||
function detectDoctor(onPath: boolean): boolean {
|
||||
if (!onPath) return false;
|
||||
const out = tryExec("gbrain", ["doctor", "--json", "--fast"], 3_000);
|
||||
if (!out) return false;
|
||||
try {
|
||||
const parsed = JSON.parse(out) as { status?: string };
|
||||
return parsed.status === "ok" || parsed.status === "warnings";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
# --- 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 sync mode ---
|
||||
function detectSyncMode(): "off" | "artifacts-only" | "full" {
|
||||
if (!existsSync(CONFIG_BIN)) return "off";
|
||||
const out = tryExec(CONFIG_BIN, ["get", "artifacts_sync_mode"], 2_000);
|
||||
if (out === "off" || out === "artifacts-only" || out === "full") return out;
|
||||
return "off";
|
||||
}
|
||||
|
||||
# --- 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
|
||||
// --- gstack-brain git repo present? ---
|
||||
function detectBrainGit(): boolean {
|
||||
return existsSync(join(STATE_DIR, ".git"));
|
||||
}
|
||||
|
||||
# Emit single-object JSON.
|
||||
jq -n \
|
||||
--argjson on_path "$gbrain_on_path" \
|
||||
--argjson version "$gbrain_version" \
|
||||
--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_artifacts_remote: $artifacts_remote
|
||||
}'
|
||||
// --- MCP mode: local-stdio | remote-http | none ---
|
||||
//
|
||||
// Defense-in-depth fallback chain (same ordering as the bash version):
|
||||
// 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
|
||||
function detectMcpMode(): "local-stdio" | "remote-http" | "none" {
|
||||
const claudeOnPath = tryExec("sh", ["-c", "command -v claude"], 1_000) !== null;
|
||||
if (claudeOnPath) {
|
||||
// Tier 1: `claude mcp get gbrain --json`
|
||||
const get = tryExec("claude", ["mcp", "get", "gbrain", "--json"], 3_000);
|
||||
if (get) {
|
||||
try {
|
||||
const parsed = JSON.parse(get) as {
|
||||
type?: string;
|
||||
transport?: string;
|
||||
command?: string;
|
||||
url?: string;
|
||||
};
|
||||
const mtype = parsed.type || parsed.transport || "";
|
||||
if (mtype === "http" || mtype === "sse") return "remote-http";
|
||||
if (mtype === "stdio") return "local-stdio";
|
||||
if (parsed.url) return "remote-http";
|
||||
if (parsed.command) return "local-stdio";
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
// Tier 2: `claude mcp list` text-grep
|
||||
const list = tryExec("claude", ["mcp", "list"], 3_000);
|
||||
if (list) {
|
||||
const line = list.split("\n").find((l) => /^gbrain:/.test(l));
|
||||
if (line) {
|
||||
if (/\b(http|HTTP)\b/.test(line)) return "remote-http";
|
||||
return "local-stdio";
|
||||
}
|
||||
}
|
||||
}
|
||||
// Tier 3: read ~/.claude.json directly
|
||||
const cj = tryReadJSON(CLAUDE_JSON) as
|
||||
| { mcpServers?: { gbrain?: { type?: string; transport?: string; command?: string; url?: string } } }
|
||||
| null;
|
||||
const entry = cj?.mcpServers?.gbrain;
|
||||
if (entry) {
|
||||
const mtype = entry.type || entry.transport || "";
|
||||
if (mtype === "url" || mtype === "http" || mtype === "sse") return "remote-http";
|
||||
if (mtype === "stdio") return "local-stdio";
|
||||
if (entry.url) return "remote-http";
|
||||
if (entry.command) return "local-stdio";
|
||||
}
|
||||
return "none";
|
||||
}
|
||||
|
||||
// --- artifacts remote URL with brain-* fallback during the rename migration window ---
|
||||
function detectArtifactsRemote(): string {
|
||||
const newPath = join(userHome(), ".gstack-artifacts-remote.txt");
|
||||
const oldPath = join(userHome(), ".gstack-brain-remote.txt");
|
||||
for (const p of [newPath, oldPath]) {
|
||||
if (existsSync(p)) {
|
||||
try {
|
||||
return readFileSync(p, "utf-8").split("\n")[0].trim();
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function main(): void {
|
||||
const gbrain = detectGbrain();
|
||||
const config = detectConfig();
|
||||
const noCache = process.env.GSTACK_DETECT_NO_CACHE === "1";
|
||||
|
||||
// Order MATCHES the bash version's jq output for callers that visually grep
|
||||
// (key order doesn't affect JSON parsers, but minimizes review noise).
|
||||
const out = {
|
||||
gbrain_on_path: gbrain.onPath,
|
||||
gbrain_version: gbrain.version,
|
||||
gbrain_config_exists: config.exists,
|
||||
gbrain_engine: config.engine,
|
||||
gbrain_doctor_ok: detectDoctor(gbrain.onPath),
|
||||
gbrain_mcp_mode: detectMcpMode(),
|
||||
gstack_brain_sync_mode: detectSyncMode(),
|
||||
gstack_brain_git: detectBrainGit(),
|
||||
gstack_artifacts_remote: detectArtifactsRemote(),
|
||||
gbrain_local_status: localEngineStatus({ noCache }),
|
||||
};
|
||||
|
||||
process.stdout.write(JSON.stringify(out, null, 2) + "\n");
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
@@ -37,6 +37,7 @@ import { createHash } from "crypto";
|
||||
|
||||
import { detectEngineTier, withErrorContext, canonicalizeRemote } from "../lib/gstack-memory-helpers";
|
||||
import { ensureSourceRegistered, sourcePageCount } from "../lib/gbrain-sources";
|
||||
import { localEngineStatus, type LocalEngineStatus } from "../lib/gbrain-local-status";
|
||||
|
||||
// ── Types ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -290,6 +291,42 @@ function releaseLock(): void {
|
||||
|
||||
// ── Stage runners ──────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Build a SKIP result for the code/memory stage when the local engine is
|
||||
* not in 'ok' state (per plan D12). Surface the status verbatim so the
|
||||
* verdict block tells the user exactly what's wrong without re-probing.
|
||||
*
|
||||
* Reasons mapped to user-actionable summaries:
|
||||
* no-cli → "gbrain CLI not on PATH; install via /setup-gbrain"
|
||||
* missing-config → "no local engine; run /setup-gbrain to add local PGLite"
|
||||
* broken-config → "config file at ~/.gbrain/config.json is malformed; see /setup-gbrain Step 1.5"
|
||||
* broken-db → "config points at unreachable DB; see /setup-gbrain Step 1.5"
|
||||
*/
|
||||
function skipStageForLocalStatus(
|
||||
stage: "code" | "memory",
|
||||
status: LocalEngineStatus,
|
||||
t0: number,
|
||||
): StageResult {
|
||||
const reasons: Record<Exclude<LocalEngineStatus, "ok">, string> = {
|
||||
"no-cli": "gbrain CLI not on PATH; install via /setup-gbrain",
|
||||
"missing-config":
|
||||
"no local engine; run /setup-gbrain to add local PGLite for code search",
|
||||
"broken-config":
|
||||
"config at ~/.gbrain/config.json is malformed; see /setup-gbrain Step 1.5",
|
||||
"broken-db":
|
||||
"config points at unreachable DB; see /setup-gbrain Step 1.5",
|
||||
};
|
||||
const reason = reasons[status as Exclude<LocalEngineStatus, "ok">];
|
||||
return {
|
||||
name: stage,
|
||||
ran: false,
|
||||
ok: true, // SKIP (per D12) — not a stage failure, just an unsatisfied prerequisite
|
||||
duration_ms: Date.now() - t0,
|
||||
summary: `skipped — local engine ${status} — ${reason}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
async function runCodeImport(args: CliArgs): Promise<StageResult> {
|
||||
const t0 = Date.now();
|
||||
const root = repoRoot();
|
||||
@@ -302,6 +339,9 @@ async function runCodeImport(args: CliArgs): Promise<StageResult> {
|
||||
|
||||
const sourceId = deriveCodeSourceId(root);
|
||||
|
||||
// dry-run preview always shows the would-do steps, regardless of local
|
||||
// engine state. Useful for "what would /sync-gbrain do" without probing
|
||||
// the engine.
|
||||
if (args.mode === "dry-run") {
|
||||
return {
|
||||
name: "code",
|
||||
@@ -313,6 +353,17 @@ async function runCodeImport(args: CliArgs): Promise<StageResult> {
|
||||
};
|
||||
}
|
||||
|
||||
// Split-engine pre-flight (per plan D12): when local engine is not ok, SKIP
|
||||
// code stage cleanly. Brain-sync stage still runs because it doesn't depend
|
||||
// on local engine. The /sync-gbrain Step 1.5 pre-flight surfaces the user
|
||||
// remediation message; this skip just keeps the orchestrator from crashing
|
||||
// when the local DB is dead. Skipped on --dry-run (above) since dry-run
|
||||
// never actually probes anything.
|
||||
const localStatus = localEngineStatus({ noCache: false });
|
||||
if (localStatus !== "ok") {
|
||||
return skipStageForLocalStatus("code", localStatus, t0);
|
||||
}
|
||||
|
||||
// Step 0: Best-effort cleanup of pre-pathhash legacy source.
|
||||
// Earlier /sync-gbrain versions registered `gstack-code-<slug>` (no path
|
||||
// suffix). On a multi-worktree repo, those collapsed onto a single id
|
||||
@@ -431,6 +482,15 @@ function runMemoryIngest(args: CliArgs): StageResult {
|
||||
return { name: "memory", ran: false, ok: true, duration_ms: 0, summary: "would: gstack-memory-ingest --probe" };
|
||||
}
|
||||
|
||||
// Split-engine pre-flight (per plan D12). gstack-memory-ingest shells out
|
||||
// to `gbrain import` which targets the LOCAL engine. When that engine is
|
||||
// not ok, SKIP cleanly so brain-sync (the only stage that doesn't depend
|
||||
// on local engine) still runs.
|
||||
const localStatus = localEngineStatus({ noCache: false });
|
||||
if (localStatus !== "ok") {
|
||||
return skipStageForLocalStatus("memory", localStatus, t0);
|
||||
}
|
||||
|
||||
const ingestPath = join(import.meta.dir, "gstack-memory-ingest.ts");
|
||||
const ingestArgs = ["run", ingestPath];
|
||||
if (args.mode === "full") ingestArgs.push("--bulk");
|
||||
|
||||
+119
-5
@@ -1202,6 +1202,57 @@ function makeStagingDir(): string {
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persistent staging dir used in remote-http MCP mode (split-engine D11).
|
||||
*
|
||||
* Instead of staging to ~/.gstack/.staging-ingest-<pid>-<ts>/ and cleaning up
|
||||
* after `gbrain import`, remote-http users get a stable path that survives.
|
||||
* gstack-brain-sync's allowlist pushes ~/.gstack/transcripts/** to the
|
||||
* artifacts repo; the brain admin's pull job indexes them into the remote
|
||||
* brain. Local PGLite (if present) stays code-only.
|
||||
*
|
||||
* Path: ~/.gstack/transcripts/<run-id>/ (run-id pid+ts so concurrent passes
|
||||
* stay separate; brain-sync push doesn't care about subdir naming).
|
||||
*/
|
||||
function makePersistentTranscriptDir(): string {
|
||||
const dir = join(
|
||||
GSTACK_HOME,
|
||||
"transcripts",
|
||||
`run-${process.pid}-${Date.now()}`,
|
||||
);
|
||||
mkdirSync(dir, { recursive: true });
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether the gbrain MCP is remote-http (Path 4) — and therefore we
|
||||
* should NOT call `gbrain import` because we don't want the local PGLite
|
||||
* polluted with transcripts (per plan D11).
|
||||
*
|
||||
* Reads ~/.claude.json directly (same fallback chain as gstack-gbrain-detect
|
||||
* Tier 3). Cheap: one fs read, no fork-exec.
|
||||
*/
|
||||
function isRemoteHttpMcpMode(): boolean {
|
||||
const home = process.env.HOME || homedir();
|
||||
const claudeJsonPath = join(home, ".claude.json");
|
||||
if (!existsSync(claudeJsonPath)) return false;
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(claudeJsonPath, "utf-8")) as {
|
||||
mcpServers?: {
|
||||
gbrain?: { type?: string; transport?: string; url?: string };
|
||||
};
|
||||
};
|
||||
const entry = parsed.mcpServers?.gbrain;
|
||||
if (!entry) return false;
|
||||
const mtype = entry.type || entry.transport || "";
|
||||
if (mtype === "url" || mtype === "http" || mtype === "sse") return true;
|
||||
if (entry.url) return true;
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort recursive cleanup. Failures swallowed — at worst we leak a
|
||||
* staging dir to disk; the next run uses a new one and they age out via
|
||||
@@ -1387,12 +1438,24 @@ async function ingestPass(args: CliArgs): Promise<BulkResult> {
|
||||
};
|
||||
}
|
||||
|
||||
// Phase 2: stage to a per-run dir + invoke gbrain import.
|
||||
const stagingDir = makeStagingDir();
|
||||
// Phase 2: stage + (optionally) invoke gbrain import.
|
||||
//
|
||||
// Split-engine branch per plan D11: in remote-http MCP mode, we stage to a
|
||||
// PERSISTENT dir under ~/.gstack/transcripts/ and SKIP `gbrain import`
|
||||
// entirely. gstack-brain-sync push will pick the dir up via its allowlist
|
||||
// and the brain admin's pull job will index transcripts into the remote
|
||||
// brain. Local PGLite (if any) stays code-only.
|
||||
const remoteHttpMode = isRemoteHttpMcpMode();
|
||||
const stagingDir = remoteHttpMode
|
||||
? makePersistentTranscriptDir()
|
||||
: makeStagingDir();
|
||||
// Register staging dir with the signal forwarder so SIGTERM/SIGINT can
|
||||
// synchronously clean it up before process.exit (the async finally block
|
||||
// below does NOT run after a signal-handler exit).
|
||||
_activeStagingDir = stagingDir;
|
||||
// below does NOT run after a signal-handler exit). In remote-http mode we
|
||||
// skip registration — the dir is meant to persist.
|
||||
if (!remoteHttpMode) {
|
||||
_activeStagingDir = stagingDir;
|
||||
}
|
||||
try {
|
||||
const staging = writeStaged(prep.prepared, stagingDir);
|
||||
failed += staging.errors.length;
|
||||
@@ -1415,11 +1478,62 @@ async function ingestPass(args: CliArgs): Promise<BulkResult> {
|
||||
}
|
||||
|
||||
if (!args.quiet) {
|
||||
const action = remoteHttpMode
|
||||
? "persisting to artifacts pipeline (skipping local gbrain import — remote-http mode)"
|
||||
: "running gbrain import";
|
||||
console.error(
|
||||
`[memory-ingest] staged ${staging.written} pages → ${stagingDir}; running gbrain import...`,
|
||||
`[memory-ingest] staged ${staging.written} pages → ${stagingDir}; ${action}...`,
|
||||
);
|
||||
}
|
||||
|
||||
// Remote-http branch (split-engine D11): no local gbrain import. The
|
||||
// staged markdown lives under ~/.gstack/transcripts/<run-id>/ and the
|
||||
// next gstack-brain-sync push will move it to the artifacts repo. From
|
||||
// there the brain admin's pull job indexes into the remote brain.
|
||||
//
|
||||
// We treat ALL prepared pages as "written" since the import didn't run
|
||||
// and we have no per-page failures from gbrain to filter on. The
|
||||
// brain admin's pull pipeline is the authoritative gate; from this
|
||||
// machine's perspective, the act of staging IS the write.
|
||||
if (remoteHttpMode) {
|
||||
const nowIso = new Date().toISOString();
|
||||
for (const p of prep.prepared) {
|
||||
try {
|
||||
state.sessions[p.source_path] = {
|
||||
mtime_ns: Math.floor(statSync(p.source_path).mtimeMs * 1e6),
|
||||
sha256: fileSha256(p.source_path),
|
||||
ingested_at: nowIso,
|
||||
page_slug: p.page_slug,
|
||||
partial: p.partial,
|
||||
};
|
||||
written++;
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[state-record] ${p.source_path}: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
state.last_full_walk = nowIso;
|
||||
state.last_writer = "gstack-memory-ingest (remote-http mode)";
|
||||
saveState(state);
|
||||
if (!args.quiet) {
|
||||
console.error(
|
||||
`[memory-ingest] persisted ${written} pages to ${stagingDir} (brain admin will index on next pull)`,
|
||||
);
|
||||
}
|
||||
// Skip the gbrain-import error handling + cleanupStagingDir paths
|
||||
// below by short-circuiting the function.
|
||||
return {
|
||||
written,
|
||||
skipped_secret: prep.skippedSecret,
|
||||
skipped_dedup: prep.skippedDedup,
|
||||
skipped_unattributed: prep.skippedUnattributed,
|
||||
failed,
|
||||
duration_ms: Date.now() - t0,
|
||||
partial_pages: prep.partialPages,
|
||||
};
|
||||
}
|
||||
|
||||
// D6: single batch import. `--no-embed` matches the prior per-file
|
||||
// behavior (we never enabled embedding); embeddings happen on-demand
|
||||
// via gbrain's own pipelines. `--json` gives us structured counts.
|
||||
|
||||
Reference in New Issue
Block a user