Skip to content

fix(preamble): probe gbrain engine liveness before recommending it#2321

Open
syong wants to merge 1 commit into
garrytan:mainfrom
syong:fix/gbrain-liveness-probe
Open

fix(preamble): probe gbrain engine liveness before recommending it#2321
syong wants to merge 1 commit into
garrytan:mainfrom
syong:fix/gbrain-liveness-probe

Conversation

@syong

@syong syong commented Jul 22, 2026

Copy link
Copy Markdown

Problem

The gbrain block in the skill preamble decides whether to tell the agent "prefer gbrain search over Grep". Today it checks three things:

  1. ~/.gbrain/config.json exists
  2. gbrain --version responds
  3. the worktree has a .gbrain-source pin

All three can pass while the engine is completely unreachable — none of them opens a connection. They prove gbrain is installed, not that it works.

So when the engine goes down (disk full, container stopped, pooler unreachable, autopilot wedged), every skill preamble keeps printing:

GBrain configured. Prefer gbrain search/gbrain query over Grep for semantic questions…

The agent then reaches for a tool whose every query fails, instead of falling back to Grep. Nothing errors, nothing is surfaced — the guidance actively asserts health it never verified, so the outage is silent and can persist indefinitely.

I hit exactly this: the local Postgres engine was crash-looping on a full Docker disk for well over a day. The preamble recommended gbrain the entire time.

Fix

Add a bounded liveness probe and branch on the result:

_GBRAIN_ALIVE=0
if command -v timeout >/dev/null 2>&1; then
  timeout 5 gbrain sources list >/dev/null 2>&1 && _GBRAIN_ALIVE=1
else
  gbrain sources list >/dev/null 2>&1 && _GBRAIN_ALIVE=1
fi

When it fails, the preamble now says so and redirects to Grep:

GBrain DOWN — this worktree is pinned, but a probe query failed, so
the engine is unreachable. Do NOT rely on `gbrain search`/`code-def`
this session; use Grep instead. Diagnose with `gbrain doctor --fast`.

Design notes:

  • Bounded. timeout 5 means a hung engine can't stall the preamble of every skill. Falls back to an unbounded call only where timeout isn't available (rare on macOS/Linux; the alternative is skipping the check entirely).
  • Engine-agnostic. Points at gbrain doctor --fast rather than assuming any particular backend (PGLite / Postgres / Supabase).
  • Zero cost when gbrain isn't in play. The probe runs only inside the existing "pin is present" branch, so non-gbrain users and unpinned worktrees are untouched.
  • Cheap. One sources list per skill invocation.

Changes

  • scripts/resolvers/preamble/generate-brain-sync-block.ts — the actual change
  • 49 generated SKILL.md files — regenerated via bun run gen:skill-docs

Insertions only; no existing lines modified.

Verification

Tested against a genuinely stopped engine, from a pinned worktree:

engine state preamble output
up normal "prefer gbrain search" guidance
stopped GBrain DOWN + use Grep + diagnose hint`
restarted normal guidance again

Also confirmed the unpinned-worktree branch still prints its existing "isn't pinned yet" message and never runs the probe.

The gbrain block in every skill preamble checked three things: the config
file exists, `gbrain --version` responds, and the worktree has a
`.gbrain-source` pin. All three can pass while the engine is unreachable —
none of them opens a connection.

When the engine is down (disk full, container stopped, pooler unreachable,
autopilot wedged), the preamble still printed "Prefer `gbrain search` over
Grep", so the agent kept reaching for a tool whose every query fails
instead of falling back to Grep. The failure is silent: there is no error
surfaced anywhere, and the guidance actively asserts health it never
verified.

Add a bounded probe (`timeout 5 gbrain sources list`) and branch on it.
When it fails, say so and tell the agent to use Grep, pointing at
`gbrain doctor --fast` to diagnose. The timeout keeps a hung engine from
stalling the preamble of every skill; the probe only runs when a pin is
already present, so the non-gbrain path stays free.

Verified against a real stopped engine: healthy -> normal guidance,
stopped -> DOWN guidance, restarted -> normal guidance.
@trunk-io

trunk-io Bot commented Jul 22, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@syong

syong commented Jul 22, 2026

Copy link
Copy Markdown
Author

/trunk merge

@trunk-io

trunk-io Bot commented Jul 22, 2026

Copy link
Copy Markdown

An error occurred while submitting your PR to the queue: Only users that are a part of this repo's Trunk organization or have write permissions to the repo can submit a PR to the queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant