refactor: share the containment gate with hook-before-edit

hook-before-edit.mjs kept its own string-based isInsideProject; it now
uses the shared isScanTargetInsideProject so all three hook passes
apply one containment semantic, symlink canonicalization included.

Because the before-edit hook gates proposed Writes whose target does
not exist yet, canonicalPath now resolves the nearest existing
ancestor and re-appends the remainder instead of falling back to the
raw resolved path — a new file under a symlinked root compares equal
to its canonical project.

Written with AI assistance (Claude Code).

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Paul Bakaus
2026-07-31 18:30:50 -07:00
co-authored by Claude Code
parent ae03e9e09c
commit febce52e8d
3 changed files with 41 additions and 22 deletions
+13
View File
@@ -195,6 +195,19 @@ describe('isScanTargetInsideProject()', () => {
assert.equal(isScanTargetInsideProject(file, link), true);
assert.equal(isScanTargetInsideProject(path.join(link, 'src', 'Card.tsx'), real), true);
});
it('classifies not-yet-written files by their nearest existing ancestor', () => {
// The before-edit hook gates proposed Writes, so the target often does
// not exist. Canonicalization must climb to an existing ancestor rather
// than bail, or a new file under a symlinked root would read as outside.
const real = path.join(root, 'real');
const link = path.join(root, 'link');
fs.mkdirSync(real, { recursive: true });
fs.symlinkSync(real, link);
assert.equal(isScanTargetInsideProject(path.join(link, 'src', 'New.tsx'), real), true);
assert.equal(isScanTargetInsideProject(path.join(real, 'deep', 'New.tsx'), link), true);
assert.equal(isScanTargetInsideProject(path.join(root, 'elsewhere', 'New.tsx'), real), false);
});
});
describe('readConfig()', () => {