fix(gbrain-local-status): classifier falsely reports broken-db inside repos with their own DATABASE_URL#1583
Open
jetsetterfl wants to merge 1 commit into
Conversation
…fier 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) <noreply@anthropic.com>
Contributor
|
Could you add a focused regression for the poisoned |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
/sync-gbrainhard-stops at the Step 1.5 pre-flight withgbrain_local_status: "broken-db"for any user whose gbrain engine ishealthy but who runs the skill from inside a repo that defines its own
DATABASE_URLin.env(common for web-app repos).Root cause:
gbrainis a Bun binary, and Bun autoloads.envfrom thecurrent directory. When
lib/gbrain-local-status.ts:freshClassifyprobeswith
gbrain sources list --json, it passesenv: env ?? process.env.Inside such a repo,
process.env.DATABASE_URLis the project's app DB, notgbrain's own DB from
~/.gbrain/config.json. gbrain connects to the wrongdatabase,
sources listfails with "Cannot connect to database", and theclassifier reports
broken-db.It is a pure false negative: the configured gbrain engine is healthy
(
gbrain doctorpasses, direct DB connection works), but the user is toldto debug their database when nothing is wrong with it.
Second-order amplifier: the 60s status cache key
(
{home, path_hash, gbrain_bin, version, config_mtime}) does not includecwd or the effective
DATABASE_URL, so one poisoned probe propagates thefalse
broken-dbto clean directories for up to a minute.Fix
Route the probe env through
buildGbrainEnvfromlib/gbrain-exec.ts—the exact helper the sync orchestrator (
gstack-gbrain-sync.ts) alreadyuses to seed
DATABASE_URLfrom~/.gbrain/config.json. The classifierinconsistently skipped it. This also makes the probe cwd-independent, so
the cache can no longer propagate a poisoned result. The existing
GSTACK_RESPECT_ENV_DATABASE_URL=1escape hatch is unaffected.Verification
.envsets adifferent
DATABASE_URL→broken-dbwhile the gbrain engine is healthy.ok, both cached and with the cache bypassed..envis unchanged:ok.Notes
--fullfirst-run empty-index bugfound during the same investigation).
garrytan/gbrain: because gbrainis a Bun binary, any
gbraininvocation from inside an app repo inheritsthat repo's
.envDATABASE_URL. Every downstream tool has todefensively re-seed it. A first-class fix (prefer
~/.gbrain/config.jsonover an inherited
DATABASE_URL, or aGBRAIN_DATABASE_URLthat alwayswins) would remove the need for these guards. Filed as a suggestion, not
blocking this PR.
Optional follow-up (not in this PR)
Add the effective
DATABASE_URLto the status-cache key asdefense-in-depth, so any future env leak can't be cached across
directories. Kept out to keep this PR focused.