Skip to content

fix(scope): repo= is always a bare token - hash non-git dir keys (#92) - #101

Merged
abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:fix/repo-path-hash-key
Oct 25, 2026
Merged

abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:fix/repo-path-hash-key

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Non-git (and origin-less) workspace dirs used to stamp repo=<raw path>. logfmt quotes that field whenever the path contains a space or a backslash, which (a) breaks naive field extraction (grep -o 'repo=[^ ]*', awk) and (b) leaves the key unmatchable-by-hash. (Wording corrected per #109: a path-derived key is machine-local either way; hashing fixed quoting and spelling stability, not cross-machine portability.) This makes repo= always a bare [a-z0-9]+ token, per the #92 acceptance:

  • src/repokey.ts — when there's no origin to hash, the key becomes sha1(normalized resolved path). normalizeDirPath (exported, pure) unifies separators, collapses symlinks/junctions/8.3 short names via realpath, drops trailing separators, and folds case only on case-insensitive filesystems (win32/darwin) — on Linux Foo and foo are two dirs, so Linux folds nothing.
  • repoPath is untouched: still the raw resolved path, the human-readable secondary, the pre-hash compatibility matcher.
  • No data migration: repoPath has been written on every stamped row since the stamp existed (since 858d1da), so rows written before this change keep matching through it. Verified at the visibility predicate (memory.ts), not assumed.

Why the Windows angle matters (the part the issue's macOS example doesn't show)

On Windows the same bug is worse — backslash paths get quoted differently, the same dir is spelled many ways (short names, junctions, case, separators), and a path-hash that isn't normalized turns one project into several invisible scopes. normalizeDirPath exists to make the hash stable per directory, not just per spelling; the new tests pin stability across all four spelling variants.

Tests (test/repokey-path.test.ts, red-first)

  • dir with a space in the name → repo= matches /repo=[a-z0-9]+/, never repo=", repoPath stays raw — red before (keyed by raw path)
  • key stable across dir\., trailing-sep, symlink/junction alias, and case-flip (case check conditional on the fs being case-insensitive)
  • a forged pre-hash row (repo= raw path) stays visible under the hashed scope (non-breaking guarantee pinned, passes both sides by design)
  • normalizeDirPath pure-function contracts
  • Existing scope suites (repo-scope, repo-scope-session, repo-scope-serve-host) updated where they pinned "path-as-key" — those assertions became keyOf(dir); their security assertions (#90 launch-dir must not appear) were kept and sharpened (now also rejects the launch dir's hash).

Verified

Bun 1.3.10 Bun 1.4.4
Linux (Ubuntu 24, full bun run test) 379 pass / 0 fail 379 pass / 0 fail
Windows full suite main-parity fail-set (resolver trio, fixed by #89; load-sensitive races, pre-existing) same parity, verified against a main control run on the same machine in the same time window

tsc --noEmit + biome lint clean. Scope-targeted suites: 111 pass / 0 fail on Windows.

Refs #92.


Model-assisted work: Qwen3-flash-next (analysis and implementation assistance).

Non-git (and origin-less) dirs stamped repo= with the raw path, which
logfmt quotes whenever the path contains a space or backslash - naive
field extraction breaks and the key can't be hash-compared. The key is
now sha1 of the NORMALIZED resolved path: separators unified, symlinks/
junctions/8.3 aliases collapsed by realpath, case folded only on the
case-insensitive filesystems (Linux keeps case; Foo and foo are two
dirs there). repoPath stays the raw human-readable path, and rows
stamped before the hash keep matching through it (repoPath has been
written alongside repo since the stamp existed, so this is a
non-breaking key change, not a data migration).

Fixes abogartz#92.
# Conflicts:
#	test/repo-scope-session.test.ts

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review. Reproduced the bug, the fix, and every claim I could check off-branch (macOS, Bun 1.4.3).

What I verified myself

The bug is real on main, not theoretical. Ran base src/repokey.ts + appendMemory against a temp dir containing a space:

KEY:  /var/folders/…/T/osi-101 demo rkpwR9
LINE: ts=… type=learning scope=s content="…" repo="/var/folders/…/osi-101 demo rkpwR9" repoPath="/var/…/osi-101 demo rkpwR9"
bare-token match: false

Same script on your branch:

KEY:  211216eed5f2
LINE: ts=… repo=211216eed5f2 repoPath="/var/…/osi-101 demo a8vN6t"
bare-token match: true

Non-breaking is real at the predicate, not just in a test. src/memory.ts:534 is m.repo === cur.key || (!!m.repoPath && m.repoPath === cur.path), so the pre-hash rows keep matching through repoPath. I then audited every consumer of the field to make sure nothing else read repo= as a path: memory.ts (parse + stamp), portable.ts:144/195 (export/import remap), gates.ts:45 (type only), e2e/stamp.test.ts (origin-keyed). Nothing splits, prefixes, or stats it. Import behavior for a pre-hash row: it is now "foreign", so the stamp is dropped and re-applied by appendMemory — different bookkeeping, same effective scope.

Gates: bun run lint clean, tsc --noEmit clean, bun run test → 444 pass / 5 skip / 0 fail (Bun 1.4.3, macOS). CI lint/test/smoke green.

One note on the red-first claim

test/repokey-path.test.ts against base repokey.ts dies at import:

SyntaxError: Export named 'normalizeDirPath' not found in module 'src/repokey.ts'
0 pass / 1 fail

So the "red before" state is an import error, not a behavioral red — it teaches a bisector nothing. The behavioral red is real (I reproduced it directly, above); I'm only recording that this particular file can't demonstrate it.

Findings

  1. Case folding is decided by platform, not by volume (normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS choice — Foo and foo are genuinely different directories, but process.platform === "darwin" folds them to one key, and isVisibleHere matches on that key. Two unrelated projects would then silently see each other's rows. The fix is local and cheap: probe the volume once (stat the case-flipped spelling, compare the returned path/inode) or fold on win32 only. repoPath won't save you here — key equality alone grants visibility.
  2. An assertion that cannot fail: expect(n.endsWith("/") ? n.length > 1 : true).toBe(true) is true for every input except a literal /. Prefer expect(n).toBe(n.replace(/\/+$/, "")), which actually pins the trailing-separator contract.
  3. Body overclaim: bullet (b), "leaves the key unmatchable-by-hash across machines". A path-derived key is machine-local by construction — hashing the path does not make a non-git dir match on another machine, it only makes the spelling stable. Please reword, because the next reader may well assume repo= keys for non-git dirs are portable and build on that.

Nothing else: the normalizer is pure and throws-free on a vanished dir (falls back to the given spelling), the hash is the same sha1-12 shape as remote keys, and the security assertions in repo-scope-serve-host.test.ts are genuinely sharper than before (they now reject the launch dir and its hash).

Status

Approved with the three items above; 1 is worth fixing before merge if you want it, 2 and 3 are one-liners. The branch is BEHIND main (strict required checks), so it needs gh pr update-branch 101 before landing either way.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review. Reproduced the bug, the fix, and every claim I could check off-branch (macOS, Bun 1.4.3).

What I verified myself

The bug is real on main, not theoretical. Ran base src/repokey.ts + appendMemory against a temp dir containing a space:

KEY:  /var/folders/…/T/osi-101 demo rkpwR9
LINE: ts=… type=learning scope=s content="…" repo="/var/folders/…/osi-101 demo rkpwR9" repoPath="/var/…/osi-101 demo rkpwR9"
bare-token match: false

Same script on your branch:

KEY:  211216eed5f2
LINE: ts=… repo=211216eed5f2 repoPath="/var/…/osi-101 demo a8vN6t"
bare-token match: true

Non-breaking is real at the predicate, not just in a test. src/memory.ts:534 is m.repo === cur.key || (!!m.repoPath && m.repoPath === cur.path), so the pre-hash rows keep matching through repoPath. I then audited every consumer of the field to make sure nothing else read repo= as a path: memory.ts (parse + stamp), portable.ts:144/195 (export/import remap), gates.ts:45 (type only), e2e/stamp.test.ts (origin-keyed). Nothing splits, prefixes, or stats it. Import behavior for a pre-hash row: it is now "foreign", so the stamp is dropped and re-applied by appendMemory — different bookkeeping, same effective scope.

Gates: bun run lint clean, tsc --noEmit clean, bun run test → 444 pass / 5 skip / 0 fail (Bun 1.4.3, macOS). CI lint/test/smoke green.

One note on the red-first claim

test/repokey-path.test.ts against base repokey.ts dies at import:

SyntaxError: Export named 'normalizeDirPath' not found in module 'src/repokey.ts'
0 pass / 1 fail

So the "red before" state is an import error, not a behavioral red — it teaches a bisector nothing. The behavioral red is real (I reproduced it directly, above); I'm only recording that this particular file can't demonstrate it.

Findings

  1. Case folding is decided by platform, not by volume (normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS choice — Foo and foo are genuinely different directories, but process.platform === "darwin" folds them to one key, and isVisibleHere matches on that key. Two unrelated projects would then silently see each other's rows. The fix is local and cheap: probe the volume once (stat the case-flipped spelling, compare the returned path/inode) or fold on win32 only. repoPath won't save you here — key equality alone grants visibility.
  2. An assertion that cannot fail: expect(n.endsWith("/") ? n.length > 1 : true).toBe(true) is true for every input except a literal /. Prefer expect(n).toBe(n.replace(/\/+$/, "")), which actually pins the trailing-separator contract.
  3. Body overclaim: bullet (b), "leaves the key unmatchable-by-hash across machines". A path-derived key is machine-local by construction — hashing the path does not make a non-git dir match on another machine, it only makes the spelling stable. Please reword, because the next reader may well assume repo= keys for non-git dirs are portable and build on that.

Nothing else: the normalizer is pure and throws-free on a vanished dir (falls back to the given spelling), the hash is the same sha1-12 shape as remote keys, and the security assertions in repo-scope-serve-host.test.ts are genuinely sharper than before (they now reject the launch dir and its hash).

Status

Approved with the three items above; 1 is worth fixing before merge if you want it, 2 and 3 are one-liners. The branch is BEHIND main (strict required checks), so it needs gh pr update-branch 101 before landing either way.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review. Verified against origin/main locally (macOS, Bun 1.4.3) and against CI on e9a1cf6.

What I confirmed independently

  • The bug is live on main, not theoretical. Ran main's resolveRepo + appendMemory against a temp dir whose name contains a space:
    repo="/var/folders/…/osi-101 demo rkpwR9" repoPath="/var/folders/…/osi-101 demo rkpwR9", and /repo=[a-z0-9]+( |$)/m → false. On your branch the same run gives repo=211216eed5f2 with repoPath still raw and the bare-token match true. The acceptance claim holds end to end, not just in the unit.
  • Non-breaking claim is at the predicate, as you said: src/memory.ts:534 is m.repo === cur.key || (!!m.repoPath && m.repoPath === cur.path), so pre-hash rows keep matching, and your forged-legacy-row test pins it through relevantKnowledge rather than asserting it.
  • No consumer assumes repo= is a path. Grepped every .repo/repoPath reader in src, plus e2e/, bench/, README.md: the only consumers are the visibility predicate above and portable.ts's export/import remap. Worth recording what the remap does under your change (src/portable.ts:195): a pre-hash row imported into the same directory fails c.repo !== currentScope.key, loses its stamp, and gets re-stamped by appendMemory — same final scope, different bookkeeping than "keeps matching through repoPath". One sentence in the body would save the next reader.
  • Gates here: biome clean, tsc --noEmit clean, full suite 444 pass / 5 skip / 0 fail. lint/test/smoke green on the PR.
  • Red-first nuance, for the record: on the merge base, test/repokey-path.test.ts fails as a SyntaxError: Export named 'normalizeDirPath' not found — file-level red, not assertion-level. The assertion-level red is real (my main-control run above); noting only so nobody later mistakes that suite for a behavior-graded red-check.

Non-blocking findings

  1. Case folding keys off the platform, not the volume (src/repokey.ts, normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS setup — Foo and foo really are two directories, but process.platform === "darwin" folds them into one key, and isVisibleHere matches on key alone: two unrelated projects would then read each other's rows. realpathSync already returns the true spelling on both volume types, so a probe is available if you want the fold to be honest: compare the inode of r against r.replace(/i/i, "I")-style flip, or restrict the fold to win32. Low frequency; the failure mode is silent cross-project visibility, which is the class I'd rather not ship a precedent for.
  2. A vacuous assertion in the normalizeDirPath contract test: expect(n.endsWith("/") ? n.length > 1 : true).toBe(true) can only fail when n === "/". Say what it means: expect(n.endsWith("/") && n !== "/").toBe(false).
  3. Body wording, (b): "leaves the key unmatchable-by-hash across machines". Hashing a path does not make a non-git dir match across machines — the absolute paths differ, so those stores were never portable, and this change doesn't alter that. What you actually bought is a bare token plus stable spelling per directory. As written it plants a "path-hash works cross-machine" expectation in whoever picks this up next.
  4. Observation only: normalizeDirPath does synchronous realpathSync on the resolve path. It sits behind the byDir cache and two git spawns, so it is not a hot-path cost — no action.

Approving. Points 2 and 3 are one-line edits you can fold into the rebase; point 1 is yours to judge — if you don't want it here, it belongs in a ticket rather than in silence.

@abogartz

Copy link
Copy Markdown
Owner

Housekeeping on this thread: my review posted twice (two near-identical bodies, plus a COMMENTED copy) because the CLI posts silently and I re-ran it. Nothing is missing and nothing differs in substance — verdict: approved. Same duplicate exists on #102 and #103; their verdict there is request changes. Only the latest review per reviewer counts, so the state is correct.

@abogartz
abogartz merged commit e5cda31 into abogartz:main Oct 25, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.13.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants