Skip to content

Stop /sync-gbrain from importing plans and design docs twice when GSTACK_BRAIN_SOURCE_ID names the artifacts source (#4443) - #4656

Closed
Barack-zero-to-one wants to merge 1 commit into
garrytan:mainfrom
Barack-zero-to-one:fix/issue-4443-federated-source-id
Closed

Barack-zero-to-one wants to merge 1 commit into
garrytan:mainfrom
Barack-zero-to-one:fix/issue-4443-federated-source-id

Conversation

@Barack-zero-to-one

Copy link
Copy Markdown
Contributor

/sync-gbrain no longer imports plans and design docs a second time on a machine that is also the brain host, once GSTACK_BRAIN_SOURCE_ID names the artifacts source.
Why: the memory stage could not find a federated artifacts source registered at a gbrain clone path, so the duplication #2922 fixed came back on that layout.
Nothing to do unless gbrain registered your artifacts source at its own clone path; then set GSTACK_BRAIN_SOURCE_ID to that source's id.

Root cause

federatedCuratedSourceId() in bin/gstack-gbrain-sync.ts found the federated source only through isArtifactsSourceRow(), which accepts a local_path equal to the artifacts worktree (GSTACK_BRAIN_WORKTREE, default ~/.gstack-brain-worktree) or the state root and nothing else. On a brain host, gbrain sources add --url or --path ~/<id> puts the clone somewhere else, so no source matched, selectMemorySources() passed no --sources filter, and ceo-plan and design-doc were imported again into the default source. The id override GSTACK_BRAIN_SOURCE_ID was already read by bin/gstack-gbrain-source-wireup and brainSyncIndexVerdict(), but not by this guard.

Fix

When GSTACK_BRAIN_SOURCE_ID is non empty after trimming, federatedCuratedSourceId() returns only a source with that exact id whose federated is true. An unknown or non federated id returns null with no fallback to the path match, so an explicit setting is never overridden by a guess, and the function writes one [sync] line to stderr naming the id and the consequence (the warning is skipped when gbrain sources list itself failed). When the variable is unset, empty or only spaces, the existing path match runs unchanged, still requiring federated === true. brainSyncIndexVerdict() now trims the variable the same way, so the memory stage and the brain sync page check pick the same source. docs/gbrain-sync.md gets one paragraph after the hookup step, and changelog.d/fix-issue-4443-federated-source-id.md carries the entry. sync-gbrain/SKILL.md.tmpl is unchanged: its sentence on skipped types stays true. Closes #4443.

Tests

A new block in test/gstack-gbrain-sync.test.ts covers six cases against a fake gbrain on PATH, a bun script with a gbrain.cmd wrapper on Windows as in test/helpers/sync-gbrain-readiness-fixture.ts, with an env holding only a temp HOME, GSTACK_HOME and PATH. A source at an external clone without the variable gives null and no filter; a source at the default worktree is found by path and the curated types are skipped; the variable naming a source at an external clone skips them too; a non federated source named by the variable gives null; an unknown id gives null without falling back to a valid federated source at the worktree, and the warning goes to stderr and not stdout; a value of spaces only falls back to the path match. Every case checks that the fake was called, so a null cannot come from a fake that failed to start. With the source change stashed, the external clone with id case and the unknown id case fail, which shows the block guards this fix.

On Windows 11 with bun 1.4.5, the six cases pass, bun run typecheck reports no error and bun run typecheck:test reports no new diagnostic. test/gstack-gbrain-sync.test.ts and test/regression-2922-memory-ingest-sources.test.ts together give 51 pass, 12 skip and 13 fail; the 13 failures are the same tests by name on untouched main, all using the existing #!/bin/sh shims that cannot run on Windows.

The full free suite (bun run test) was started on that Windows machine but was stopped partway because the machine ran low on memory, so it gives no result for this change. Before it stopped, every failure it reported came from files this PR does not touch, most of them suites built on bash or macOS tools, and those were not rerun on main. The full validation of this PR relies on the Linux CI in free-tests.yml.

Out of scope

bin/gstack-gbrain-source-wireup still tests GSTACK_BRAIN_SOURCE_ID without trimming it, so a value with surrounding spaces is used as is there. brainSyncIndexVerdict() still accepts a source named by the variable even when it is not federated, while the memory stage guard requires federated === true; the two functions answer different questions (page count versus skip), so changing that rule is left to a separate decision.

…ACK_BRAIN_SOURCE_ID names the artifacts source (garrytan#4443)

federatedCuratedSourceId() found the federated artifacts source only by path, so a source gbrain registered at its own clone path was missed and the memory stage imported ceo-plan and design-doc a second time. A non empty trimmed GSTACK_BRAIN_SOURCE_ID now names the source by id (federated required, no path fallback, a stderr warning when nothing matches), and brainSyncIndexVerdict() trims the variable the same way.
@trunk-io

trunk-io Bot commented Nov 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

@capy-ai

capy-ai Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thank you for the clear root cause and the six test cases. We're picking this up for #4443. A maintainer agent working through a batch of fixes (our tracking thread GSTA-804) will first reproduce the problem on current main. If it reproduces, the agent rebuilds the fix on main with you credited as co-author, because we don't merge outside branches directly, and we'll close this PR and #4443 with a link when it lands. If it no longer reproduces, we'll close this and say what changed.

@capy-ai

capy-ai Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thank you! We rebuilt this fix on current main in #4685, which is now merged, with you credited as co-author, so I'm closing this PR. Outside PRs get rebuilt rather than merged directly as part of our security process for code that runs.

@capy-ai capy-ai Bot closed this Nov 22, 2026
@Barack-zero-to-one
Barack-zero-to-one deleted the fix/issue-4443-federated-source-id branch November 22, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working S3 Severity 3: everything else

Projects

None yet

1 participant