Skip to content

fix(gbrain): stop a path case difference from destroying the source index - #2386

Closed
ShahriarLak wants to merge 1 commit into
garrytan:mainfrom
ShahriarLak:fix/gbrain-source-path-case
Closed

ShahriarLak wants to merge 1 commit into
garrytan:mainfrom
ShahriarLak:fix/gbrain-source-path-case

Conversation

@ShahriarLak

Copy link
Copy Markdown
Contributor

The bug

On Windows, /sync-gbrain fails on every run, in every mode:

ERR   code   source registration failed: gbrain sources remove <id> failed:
⚠️  This will permanently delete: 129 pages, 627 chunks, 627 embeddings
To proceed, pass --confirm-destructive (or use soft-delete: gbrain sources archive <id>)

The flag demand is the symptom, not the cause. In ensureSourceRegistered, gbrain held the source at C:/Users/me/projects/quill while the caller derived C:/Users/me/Projects/Quill. Those were compared with !==, so the same directory spelled differently was classified as drift, and every run took the remove-then-add path.

Passing --confirm-destructive would be worse than the bug. It really deletes the source's pages, chunks and embeddings and re-walks from scratch, over a mismatch that was never real. Anyone hitting this and following the error message's advice loses their index.

The fix

lib/gbrain-sources.ts compares paths case-folded with separators normalised. Windows and macOS both default to case-insensitive filesystems, so this is correct there; on Linux a case-distinct pair is possible in principle but gstack never creates one, and re-using an existing index is a much cheaper failure than deleting it.

Two related fixes in the same area:

bin/gstack-gbrain-source-wireup carries the identical defect, plus a second trigger. Its multi-machine guard parses sources list --json with jq, so on a machine without jq existing_path comes back empty, the guard can never fire, and every mismatch falls through to remove-then-add. It now falls back to sed, compares normalised, and on a genuine mismatch warns and continues instead of deleting. Its own comment already states that gbrain stores pages by content and local_path is informational metadata, which is the argument for never deleting on a path change.

bin/gstack-gbrain-sync.ts spawned gstack-brain-sync with shell: true on Windows. The existing comment (#1731) notes Windows can't spawn a shebang script without a shell, but shell: true means cmd.exe, which can't run an extensionless bash-shebang script either. It reported '...gstack-brain-sync' is not recognized as an internal or external command and exited 1 on every run. It now passes the script to bash.

Verification

Windows 11, gbrain 0.42.52.0, gstack 1.60.1.0.

Before: 1 ok, 2 error, 0 skipped (the two errors above), on every run.

After:

gstack-gbrain-sync (incremental):
  OK    code         synced gstack-code-<repo> (page_count=129) (18.8s)
  OK    memory       gbrain import: 0 imported, 0 unchanged, 0 failed (2.4s)
  OK    brain-sync   curated artifacts pushed (1.7s)

  3 ok, 0 error, 0 skipped

No destructive operation attempted, index intact at 129 pages.

Note on the dry-run

Worth flagging separately: --dry-run previews this as an idempotent gbrain sources add ... and gives no hint that a real run would attempt a sources remove first. That made the dry-run actively misleading while diagnosing this. Not addressed here, since it's a reporting change rather than a correctness one.

…ndex

On Windows, /sync-gbrain fails in every mode with:

  source registration failed: gbrain sources remove <id> failed:
  This will permanently delete: 129 pages, 627 chunks, 627 embeddings
  To proceed, pass --confirm-destructive

The destructive-flag demand is the symptom. The cause is in
ensureSourceRegistered: gbrain held the source at
C:/Users/me/projects/quill while the caller derived
C:/Users/me/Projects/Quill, and the two were compared with !==. The same
directory, spelled differently, was read as drift, so every run took the
remove-then-add path.

Passing --confirm-destructive is the wrong fix. It really deletes the
source's pages, chunks and embeddings and re-walks from scratch, over a
mismatch that was never real. This compares case-folded with separators
normalised instead, so the registration is left alone.

Two related fixes in the same area:

- gstack-gbrain-source-wireup had the same defect. Its multi-machine guard
  parses `sources list --json` with jq, so where jq isn't installed
  existing_path is empty, the guard can't fire, and every mismatch falls
  through to remove-then-add. It now falls back to sed, compares normalised,
  and on a genuine mismatch warns and continues rather than deleting. Its own
  comment already says gbrain stores pages by content and local_path is
  informational, which is the argument for never deleting on a path change.

- The brain-sync stage spawned gstack-brain-sync with shell:true on Windows.
  That means cmd.exe, which cannot run an extensionless bash-shebang script:
  "is not recognized as an internal or external command", exit 1, every run.
  It now passes the script to bash.

Verified on Windows 11 (gbrain 0.42.52, gstack 1.60.1.0): /sync-gbrain went
from failing every run to `3 ok, 0 error, 0 skipped`, with the source synced
at 129 pages and no destructive operation attempted.
@trunk-io

trunk-io Bot commented Jul 29, 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

@ShahriarLak

Copy link
Copy Markdown
Contributor Author

Closing — upstream fixed this independently and better.

samePath() in lib/gbrain-sources.ts now realpath-normalises both sides:

const real = (p: string): string => {
  try { return realpathSync(p) } catch { return p }
}
return real(registered) === real(requested)

On Windows realpathSync returns the canonical on-disk casing, so
projects/quill and Projects/Quill compare equal and the drift branch never
fires. That covers the case difference this PR targeted, and handles symlink
aliases too, which my case-folding comparison did not. It is also why the branch
now shows as conflicting — it edits the same function.

Verified on the original repro (Windows 11, gstack 1.61.0.0): a full
/sync-gbrain --full completed with the source synced at 142 pages and no
destructive sources remove.

Thanks for sorting it. Nothing needed here.

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