Skip to content

fix(memory): a loopback gbrain serve --http is a local brain, not Path 4 (#3539) - #3552

Closed
wmorin wants to merge 1 commit into
garrytan:mainfrom
wmorin:fix/loopback-gbrain-is-local
Closed

wmorin wants to merge 1 commit into
garrytan:mainfrom
wmorin:fix/loopback-gbrain-is-local

Conversation

@wmorin

@wmorin wmorin commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Why (in your own words)

gbrain's recommended setup for local agents on a Postgres brain is one resident gbrain serve --http on loopback, registered by gbrain bootstrap harness as an http MCP entry (http://127.0.0.1:3131/mcp). gstack treated every http entry as a Path 4 remote brain. So /sync-gbrain staged transcripts under ~/.gstack/transcripts/run-* for a brain admin's pull that never happens, and skipped gbrain import. The brain is on the same machine, so nothing was imported. Pages staged that way also stayed stuck after switching back to stdio: --probe counted them Unchanged and --incremental wrote 0.

This keeps #2352's one-delivery-path-per-transport design. It only changes which machine an http entry is taken to point at. An http entry counts as local when its URL is http(s) on a loopback host and this machine's gbrain config has an engine and no remote_mcp marker. Pages left staged / remote-http are imported on the next local run.

The fix targets Postgres brains, the setup in the issue. A PGLite brain held open by a live gbrain serve is still skipped by /sync-gbrain: gbrain reports it engine-locked and the memory stage only runs on ok or timeout. This PR does not address that case.

Live evidence

The issue's sandbox repro, using a PGLite brain under GBRAIN_HOME, the http://127.0.0.1:3131/mcp entry that gbrain bootstrap harness writes, gbrain 0.60.472.0, bun 1.4.4 and Claude Code 2.1.311. The script runs issue steps 1-3, plus a "stuck" variant that runs step 3 with origin/main and then with the branch in the same sandbox. Output is trimmed to the relevant lines; $SB is the sandbox. No server listens on 3131, because the rule reads config only.

Before (origin/main 9e3308f):

$ gstack-gbrain-detect
  "gbrain_engine": "pglite",
  "gbrain_mcp_mode": "remote-http",
  "gbrain_local_status": "ok",
$ gstack-memory-ingest --incremental --sources transcript
[memory-ingest] staged 1 pages → $SB/.gstack/transcripts/run-78973-…; persisting to artifacts pipeline (skipping local gbrain import — remote-http mode)...
[memory-ingest] persisted 1 pages to $SB/.gstack/transcripts/run-78973-… (brain admin will index on next pull)
{"status":"staged","source_id":"remote-http"}
$ gbrain sources list
  default               federated          0 pages  never synced

After (this branch), fresh sandbox, same http entry:

$ gstack-gbrain-detect
  "gbrain_engine": "pglite",
  "gbrain_mcp_mode": "local-stdio",
  "gbrain_local_status": "ok",
$ gstack-memory-ingest --incremental --sources transcript
[memory-ingest] staged 1 pages → $SB/.gstack/.staging-ingest-78829-…-src-gstack-transcripts-repo-0ad97a; running gbrain import...
[memory-ingest] gbrain import: 1 imported, 0 unchanged, 0 failed
{"status":"ingested","source_id":"gstack-transcripts-repo-0ad97a"}
$ gbrain sources list
  default               federated          0 pages  never synced
  gstack-transcripts-repo-0ad97a  isolated           1 pages  never synced

Stuck state: the "before" run left {"status":"staged","source_id":"remote-http"}. The branch then runs in the same sandbox with the same http entry. (On origin/main this probe prints New: 0 / Unchanged: 1.)

$ gstack-memory-ingest --probe --sources transcript
New (never ingested):  1
Unchanged:             0
$ gstack-memory-ingest --incremental --sources transcript
[memory-ingest] gbrain import: 1 imported, 0 unchanged, 0 failed
{"status":"ingested","source_id":"gstack-transcripts-repo-0ad97a"}

The new tests were run on an origin/main export with bin/ unchanged, then on this branch. lib/gbrain-mcp-locality.ts had to be copied in with the tests, because the new unit file imports it. That means the unit cases for the helper itself pass on main too; the failures below are the bin/ and lib/gbrain-local-status.ts behavior.

origin/main  test/gbrain-mcp-locality.test.ts test/gstack-gbrain-detect-mcp-mode.test.ts  145 pass, 21 fail
origin/main  test/gbrain-local-status.test.ts                                              54 pass,  1 fail
origin/main  test/gstack-memory-ingest.test.ts -t 3539                                      2 pass,  3 fail
branch       test/gbrain-mcp-locality.test.ts test/gstack-gbrain-detect-mcp-mode.test.ts  166 pass,  0 fail
branch       test/gbrain-local-status.test.ts                                              55 pass,  0 fail
branch       test/gstack-memory-ingest.test.ts -t 3539                                      5 pass,  0 fail

The cases that pass on main include the "stays remote" ones: a non-loopback URL, a loopback URL with remote_mcp, the claude mcp list lines that must never read as local, and a remote exact gbrain: line listed before a loopback gbrain-a: line. They pin the behavior that must not change.

Scope

  • Changed:
    • lib/gbrain-mcp-locality.ts (new) holds the rule. An http/url/sse entry (or a bare url) is local when the URL is http:/https: on a loopback host (localhost, 127.0.0.0/8, ::1) and gbrainConfigDir()/config.json (honors GBRAIN_HOME) has engine: "pglite" or a non-blank database_url, with no remote_mcp marker. A marker counts when it is JS-truthy, as in gbrain's isThinClient. A URL with userinfo (user@, user:pw@) or containing ${ is never local: Claude Code expands ${VAR} in url before it connects, so http://${X}@127.0.0.1/mcp can dial any host. Every other entry is classified as before. The rule reads files only and makes no network calls.
    • bin/gstack-memory-ingest.ts: isRemoteHttpMcpMode() uses the rule, so a loopback serve takes the gbrain import path. It is memoized because the change check below now calls it once per state entry. Entries with status: "staged" and source_id: "remote-http" count as not landed when the mode is local, so they are imported again. While the brain is remote they stay Unchanged, as before. brainIsRemote() itself is unchanged.
    • bin/gstack-gbrain-detect: all three tiers use the rule. The loopback case reports the existing local-stdio value, so sync-gbrain's trust-policy gate and setup-gbrain's Path 4 shortcut need no change. Tier 2 is the live path on current Claude Code: 2.1.311 rejects claude mcp get --json (unknown option '--json'), so tier 1 never answers. Tier 2 now anchors on the claude mcp list line format (gbrain…: <url> (HTTP) or (SSE)) and classifies the whole URL token. It never cuts the token at ), which would read http://127.0.0.1)@evil.com/mcp (host evil.com) as loopback. A line with no usable URL keeps the old remote-http answer. This also fixes a pre-existing false local: a remote (SSE) line has no http word and used to fall through to local-stdio. With several gbrain servers, tier 2 now prefers the exact gbrain: line and falls back to the first gbrain-* / gbrain_* line, the same order as tier 3 (and memory-ingest, which reads mcpServers.gbrain). Before, it took the first gbrain* line in list order, so a loopback gbrain-a listed before a remote gbrain read as local.
    • lib/gbrain-local-status.ts: hasRemoteOnlyGbrainMcp() uses the same classifier instead of its own transport-only copy. detect no longer prints gbrain_mcp_mode: local-stdio next to gbrain_local_status: thin-client for the same loopback entry; a locked or broken local engine keeps its precise status.
    • bin/gstack-skill-start: the jq hot path applies the rule, so ARTIFACTS_SYNC no longer prints remote-mode for a loopback serve. The jq copy is a conservative subset of the TS rule, not an exact mirror. The config half has the same semantics. The URL half accepts only canonical loopback spellings (localhost, 127.a.b.c with valid decimal octets, [::1], a valid port), lowercased with ascii_downcase (the regex "i" flag would case-fold ſ to s and accept httpſ://). Like TS, it rejects userinfo and any URL containing ${. Other spellings TS accepts (127.1, 0x7f.1, a trailing dot) stay remote in jq, which is the old answer.
    • Tests: test/gbrain-mcp-locality.test.ts (new) has the rule cases, claude mcp list line cases, and a table-driven parity run of the shipped jq programs against the TS rule: config must match exactly, and jq may never say local where TS says remote. There are also loopback, truncation, (SSE) and multi-server ordering cases in test/gstack-gbrain-detect-mcp-mode.test.ts, loopback cases in test/gbrain-local-status.test.ts, and stuck-entry cases in test/gstack-memory-ingest.test.ts.
    • VERSION and CHANGELOG are unchanged, for the maintainer's release step.
  • Verified live by: the sandbox repro above, before and after. Also bun run typecheck (clean), bun run typecheck:test ("819 known diagnostics, none new"), bun run skill:check (clean), and shellcheck -S warning bin/gstack-skill-start (the same 4 warnings as origin/main). The four touched test files together: 314 pass, 5 skip, 1 fail. The failure (#2394 … parity: probe post-attribution count…) fails the same way on origin/main on this machine.
  • Did NOT test:
    • A running gbrain serve --http, Postgres or PGLite. No server listens in the repro. With a live serve, a PGLite brain is held by that one process. gbrain reports engine-locked, and /sync-gbrain skips the memory stage as before (see Why).
    • The full bun run test suite, Windows, paid evals, and /sync-gbrain end to end in Claude Code.

Not changed here (follow-ups):

  • An explicit override (gstack-config set gbrain_mcp_mode local|remote-http) for setups the rule does not cover, e.g. an SSH tunnel to a remote brain on a loopback port.
  • Path 4 docs (USING_GBRAIN_WITH_GSTACK.md, setup-gbrain): add a line saying a shared loopback serve over this machine's engine is a local setup, not Path 4.
  • gstack-config endpoint-hash still hashes a loopback URL instead of returning local. sync-gbrain's trust gate is skipped now (the mode is not remote-http), but /setup-gbrain step 9.5 and write-back gating still key on the sha8.
  • brainIsRemote() still treats any non-PGLite database_url as remote, including a Postgres on localhost.
  • Old ~/.gstack/transcripts/run-* staging copies are not pruned once their pages are imported locally. If ~/.gstack is later wired as a federated source, the same transcript could exist in two sources.

Liveness proof (required for external contributors)

gstack-pr-liveness

Checklist

Fixes #3539

…h 4 (garrytan#3539)

gbrain's recommended setup for local agents on a Postgres brain is one
resident `gbrain serve --http` on loopback, registered as an http MCP
entry. gstack classified any http entry as a remote brain, so memory
ingest staged transcripts for a brain admin's pull that never comes.

- lib/gbrain-mcp-locality.ts: one rule. An http/url/sse entry is local
  when its URL is http(s) on a loopback host (localhost, 127.0.0.0/8,
  ::1) and the local gbrain config (GBRAIN_HOME honored) has an engine
  (pglite or a database_url) and no remote_mcp marker (JS-truthy, as in
  gbrain's isThinClient). A URL with userinfo or a `${` is never local
  (Claude Code expands ${VAR} in the url before connecting). Everything
  else is unchanged.
- gstack-memory-ingest: isRemoteHttpMcpMode uses it (memoized), so the
  loopback case takes the gbrain import path and brainIsRemote no
  longer forces remote for it.
- gstack-memory-ingest: entries left staged/remote-http are treated as
  not landed when the brain is local, so --probe counts them New and
  --incremental imports them.
- gstack-gbrain-detect: all three tiers use the rule; the loopback case
  reports the existing local-stdio value. Tier 2 anchors on the
  `claude mcp list` line format and classifies the whole URL token
  (never cut at ")"), for (HTTP) and (SSE) lines; a line without a
  usable URL stays remote-http. A remote (SSE) line no longer falls
  through to local-stdio. With several gbrain servers, tier 2 prefers
  the exact `gbrain:` line, then the first gbrain[-_]* line, the same
  order as tier 3.
- gbrain-local-status: hasRemoteOnlyGbrainMcp uses the same classifier,
  so detect never reports local-stdio next to thin-client.
- gstack-skill-start: the jq hot path carries a conservative subset of
  the rule (canonical loopback spellings only, ASCII lowercasing; same
  config semantics).
  A table-driven test runs the shipped jq programs against the TS rule.
@trunk-io

trunk-io Bot commented Oct 28, 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

@wmorin
wmorin marked this pull request as ready for review October 28, 2026 16:53
@capy-ai

capy-ai Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Thank you for this fix and the thorough write-up. A crew lane is picking it up to land on current main with Co-authored-by credit to you.

capy-ai Bot added a commit that referenced this pull request Nov 3, 2026
…in, not Path 4 (#3539) (#3727)

* test(memory): a loopback gbrain serve --http is a local brain (#3539)

Tests from #3552. On main they fail: 21 locality/detect cases, 1 local-status case, 3 memory-ingest cases.

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(memory): a loopback gbrain serve --http is a local brain, not Path 4 (#3539)

gbrain's recommended setup for local agents on a Postgres brain is one
resident `gbrain serve --http` on loopback, registered as an http MCP
entry. gstack classified every http entry as a remote brain, so memory
ingest staged transcripts for a brain admin's pull that never comes.

- lib/gbrain-mcp-locality.ts: one rule. An http/url/sse entry is local
  when its URL is http(s) on a loopback host and the local gbrain config
  has an engine and no remote_mcp marker. A URL with userinfo or ${ is
  never local.
- gstack-memory-ingest: the loopback case takes the gbrain import path,
  and entries left staged/remote-http import on the next local run.
- gstack-gbrain-detect: all three tiers use the rule; tier 2 classifies
  the whole URL token of the claude mcp list line.
- gbrain-local-status: hasRemoteOnlyGbrainMcp uses the same classifier.
- gstack-skill-start: the jq hot path carries a conservative subset.
- USING_GBRAIN_WITH_GSTACK.md: a loopback serve over this machine's
  engine is not Path 4.

Ported from #3552 onto current main.

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* changelog: #3539 loopback gbrain serve fragment

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(memory): keep userinfo fixtures clear of the pregate secrets scan (#3539)

The pregate secrets stage read four userinfo URLs in the locality test as basic-auth credentials (creds.basic_auth_url). Use the PASSWORD placeholder token and split the port-lookalike URLs so the same strings reach the rule.

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* changelog: note the 90-day window for stuck staged pages (#3539)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* changelog: state the measured failing-first counts (#3539)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(memory): bound the jq parity spawn with a timeout (#3539)

test/spawnsync-timeout-tripwire.test.ts requires every sync spawn in the test trees to carry a timeout; the jq parity helper had none (remote pregate, tree 037200b).

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(setup-gbrain): the loopback Path 4 fixture over a local PGLite now reads local-stdio (#3539)

The owned Path 4 fixture registers its fake brain at 127.0.0.1 and then runs gbrain init --pglite in the same HOME. Under the #3539 rule a loopback HTTP entry over this machine's own engine is a local brain, so detect reports local-stdio there. free-suite (7) on PR #3727 (run 41231145675) failed on the old remote-http expectation.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(memory): count a loopback serve as local only over a Postgres brain (#3539)

Path 4's split-engine setup pairs a remote brain with a local PGLite for code search; through a tunnel on a loopback port, the PGLite config made the remote brain read as local, so transcripts would import into the code-search PGLite and the trust-policy step would be skipped. The loopback rule now needs a Postgres config (a database_url, engine not pglite), which is the setup gbrain recommends the resident serve for. The setup-gbrain Path 4 fixture expects remote-http again, as on main. Decided by GStack Minder (Captain), 2026-10-31.

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: Willy Morin <3635875+wmorin@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Thanks @wmorin. Your diagnosis drove the fix. We rewrote it on current main in #3727, merged as b257eff (v1.93.29.0), and credited you with Co-authored-by. One change from your version: the loopback rule applies only when this machine's gbrain config is a Postgres brain (database_url). A loopback URL next to a local PGLite still reads as Path 4's split-engine (remote) setup. Closing this PR as superseded by #3727.

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.

memory-ingest: a loopback gbrain serve --http is treated as a remote brain, so transcripts are staged and never imported

1 participant