From fa162a28962e41bdc328237d2743d780619b3461 Mon Sep 17 00:00:00 2001 From: jetsetterfl Date: Mon, 18 May 2026 08:49:00 -0400 Subject: [PATCH] fix(gbrain-local-status): seed DATABASE_URL from config so the classifier isn't poisoned by a project .env freshClassify probed `gbrain sources list` with raw process.env. Because gbrain is a Bun binary, Bun autoloads a project's .env from cwd, so running the preflight inside any repo that defines its own DATABASE_URL (common for web-app repos) made the probe connect to the wrong database. `sources list` then failed and the classifier reported `broken-db`, hard-blocking /sync-gbrain even though the configured gbrain engine was healthy. Route the probe env through buildGbrainEnv (the same helper the sync orchestrator already uses) so DATABASE_URL is always seeded from ~/.gbrain/config.json. This also makes the probe result cwd-independent, so the 60s status cache can no longer propagate a poisoned negative to clean directories. The existing GSTACK_RESPECT_ENV_DATABASE_URL=1 escape hatch still works. Co-Authored-By: Claude Opus 4.7 (1M context) --- lib/gbrain-local-status.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/gbrain-local-status.ts b/lib/gbrain-local-status.ts index f546a93bc7..ca26211af4 100644 --- a/lib/gbrain-local-status.ts +++ b/lib/gbrain-local-status.ts @@ -35,6 +35,7 @@ import { } from "fs"; import { homedir } from "os"; import { dirname, join } from "path"; +import { buildGbrainEnv } from "./gbrain-exec"; export type LocalEngineStatus = | "ok" @@ -220,12 +221,20 @@ function freshClassify(env?: NodeJS.ProcessEnv): LocalEngineStatus { if (!existsSync(gbrainConfigPath())) return "missing-config"; // 3. Probe gbrain sources list. + // + // Seed DATABASE_URL from ~/.gbrain/config.json (via buildGbrainEnv, the + // same helper the sync orchestrator uses in lib/gbrain-exec.ts). Without + // this, Bun autoloads a project's .env when the probe runs inside a repo + // that defines its own DATABASE_URL (e.g. an app DB on a different port), + // gbrain connects to the wrong DB, and the classifier falsely reports + // broken-db. This also makes the result cwd-independent, so the 60s cache + // can no longer propagate a poisoned negative to clean directories. try { execFileSync("gbrain", ["sources", "list", "--json"], { encoding: "utf-8", timeout: PROBE_TIMEOUT_MS, stdio: ["ignore", "pipe", "pipe"], - env: env ?? process.env, + env: buildGbrainEnv({ baseEnv: env ?? process.env }), }); return "ok"; } catch (err) {