mirror of
https://github.com/garrytan/gstack.git
synced 2026-06-27 20:20:03 +02:00
perf(gbrain): memoize gbrain resolution + use --fast doctor in detect
Cuts detect's wall time substantially by sharing fork-exec results between the helper that walks the JSON output and the localEngineStatus classifier from lib/gbrain-local-status.ts. Before: detect made 2x `command -v gbrain` calls (one in detect's detectGbrain, one in the classifier's resolveGbrainBin) and 2x `gbrain --version` calls. With memoization keyed on PATH, both collapse to one fork each (~400ms saved per skill preamble). Also adds `--fast` to the `gbrain doctor --json` call in detect so a broken-db config (Garry's repro) doesn't burn a full 5s timeout on the doctor's DB-connection check. The classifier still probes the DB directly via `gbrain sources list --json` for engine reachability — that's `gbrain_local_status`, separate from the coarse `gbrain_doctor_ok` summary flag. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -37,7 +37,11 @@ import { existsSync, readFileSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
|
||||
import { localEngineStatus } from "../lib/gbrain-local-status";
|
||||
import {
|
||||
localEngineStatus,
|
||||
resolveGbrainBin,
|
||||
readGbrainVersion,
|
||||
} from "../lib/gbrain-local-status";
|
||||
|
||||
const STATE_DIR = process.env.GSTACK_HOME || join(userHome(), ".gstack");
|
||||
const SCRIPT_DIR = __dirname;
|
||||
@@ -71,10 +75,12 @@ function tryReadJSON(path: string): unknown | null {
|
||||
}
|
||||
|
||||
// --- 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 out = tryExec("sh", ["-c", "command -v gbrain"], 2_000);
|
||||
if (!out) return { onPath: false, version: null };
|
||||
const verRaw = tryExec("gbrain", ["--version"], 2_000);
|
||||
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;
|
||||
@@ -93,9 +99,13 @@ function detectConfig(): { exists: boolean; engine: "pglite" | "postgres" | null
|
||||
}
|
||||
|
||||
// --- 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"], 5_000);
|
||||
const out = tryExec("gbrain", ["doctor", "--json", "--fast"], 3_000);
|
||||
if (!out) return false;
|
||||
try {
|
||||
const parsed = JSON.parse(out) as { status?: string };
|
||||
|
||||
Reference in New Issue
Block a user