Repository navigation
fix(scope): repo= is always a bare token - hash non-git dir keys (#92) - #101
Conversation
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
left a comment
There was a problem hiding this comment.
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
- Case folding is decided by platform, not by volume (
normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS choice —Fooandfooare genuinely different directories, butprocess.platform === "darwin"folds them to one key, andisVisibleHerematches 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 onwin32only.repoPathwon't save you here — key equality alone grants visibility. - An assertion that cannot fail:
expect(n.endsWith("/") ? n.length > 1 : true).toBe(true)is true for every input except a literal/. Preferexpect(n).toBe(n.replace(/\/+$/, "")), which actually pins the trailing-separator contract. - 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
left a comment
There was a problem hiding this comment.
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
- Case folding is decided by platform, not by volume (
normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS choice —Fooandfooare genuinely different directories, butprocess.platform === "darwin"folds them to one key, andisVisibleHerematches 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 onwin32only.repoPathwon't save you here — key equality alone grants visibility. - An assertion that cannot fail:
expect(n.endsWith("/") ? n.length > 1 : true).toBe(true)is true for every input except a literal/. Preferexpect(n).toBe(n.replace(/\/+$/, "")), which actually pins the trailing-separator contract. - 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
left a comment
There was a problem hiding this comment.
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+appendMemoryagainst 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 givesrepo=211216eed5f2withrepoPathstill 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:534ism.repo === cur.key || (!!m.repoPath && m.repoPath === cur.path), so pre-hash rows keep matching, and your forged-legacy-row test pins it throughrelevantKnowledgerather than asserting it. - No consumer assumes
repo=is a path. Grepped every.repo/repoPathreader insrc, pluse2e/,bench/,README.md: the only consumers are the visibility predicate above andportable.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 failsc.repo !== currentScope.key, loses its stamp, and gets re-stamped byappendMemory— same final scope, different bookkeeping than "keeps matching through repoPath". One sentence in the body would save the next reader. - Gates here:
biomeclean,tsc --noEmitclean, full suite 444 pass / 5 skip / 0 fail.lint/test/smokegreen on the PR. - Red-first nuance, for the record: on the merge base,
test/repokey-path.test.tsfails as aSyntaxError: 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
- Case folding keys off the platform, not the volume (
src/repokey.ts,normalizeDirPath). On a case-sensitive APFS volume — a legitimate macOS setup —Fooandfooreally are two directories, butprocess.platform === "darwin"folds them into one key, andisVisibleHerematches on key alone: two unrelated projects would then read each other's rows.realpathSyncalready returns the true spelling on both volume types, so a probe is available if you want the fold to be honest: compare the inode ofragainstr.replace(/i/i, "I")-style flip, or restrict the fold towin32. Low frequency; the failure mode is silent cross-project visibility, which is the class I'd rather not ship a precedent for. - A vacuous assertion in the
normalizeDirPathcontract test:expect(n.endsWith("/") ? n.length > 1 : true).toBe(true)can only fail whenn === "/". Say what it means:expect(n.endsWith("/") && n !== "/").toBe(false). - 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.
- Observation only:
normalizeDirPathdoes synchronousrealpathSyncon the resolve path. It sits behind thebyDircache and twogitspawns, 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.
|
Housekeeping on this thread: my review posted twice (two near-identical bodies, plus a |
|
🎉 This PR is included in version 0.13.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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 makesrepo=always a bare[a-z0-9]+token, per the #92 acceptance:src/repokey.ts— when there's no origin to hash, the key becomessha1(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 LinuxFooandfooare two dirs, so Linux folds nothing.repoPathis untouched: still the raw resolved path, the human-readable secondary, the pre-hash compatibility matcher.repoPathhas been written on every stamped row since the stamp existed (since858d1da), 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.
normalizeDirPathexists 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)repo=matches/repo=[a-z0-9]+/, neverrepo=",repoPathstays raw — red before (keyed by raw path)dir\., trailing-sep, symlink/junction alias, and case-flip (case check conditional on the fs being case-insensitive)repo-scope,repo-scope-session,repo-scope-serve-host) updated where they pinned "path-as-key" — those assertions becamekeyOf(dir); their security assertions (#90launch-dir must not appear) were kept and sharpened (now also rejects the launch dir's hash).Verified
bun run test)tsc --noEmit+biome lintclean. Scope-targeted suites: 111 pass / 0 fail on Windows.Refs #92.
Model-assisted work: Qwen3-flash-next (analysis and implementation assistance).