Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/core/auto-approval/WorkspaceRootCanonicalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { WorkspaceRootStrategy } from "./strategies/WorkspaceRootStrategy"
import { WorkspaceRootStrategyFactory } from "./strategies/WorkspaceRootStrategyFactory"

/** Translate only a verified workspace-root spelling, never a file symlink. */
export class WorkspaceRootCanonicalizer {
private constructor(
private readonly root: string | undefined,
private readonly canonicalRoot: string | undefined,
private readonly strategy: WorkspaceRootStrategy,
) {}

static forStrategy(
cwd: string | undefined,
canonicalCwd: string | undefined,
strategy: WorkspaceRootStrategy,
): WorkspaceRootCanonicalizer {
const root = strategy.normalizeAbsoluteRoot(cwd)
const canonicalRoot = strategy.normalizeAbsoluteRoot(canonicalCwd)
return new WorkspaceRootCanonicalizer(root, canonicalRoot, strategy)
}

static forPlatform(
cwd: string | undefined,
canonicalCwd: string | undefined,
isWindows: boolean,
): WorkspaceRootCanonicalizer {
const strategy = WorkspaceRootStrategyFactory.create(isWindows)
return WorkspaceRootCanonicalizer.forStrategy(cwd, canonicalCwd, strategy)
}

canonicalize(value: string): string {
if (this.root === undefined || this.canonicalRoot === undefined) return value

return this.replaceWorkspaceRootPrefix(value, this.root, this.canonicalRoot)
}

private replaceWorkspaceRootPrefix(value: string, root: string, canonicalRoot: string): string {
const comparableValue = this.strategy.comparisonKey(value)
const comparableRoot = this.strategy.comparisonKey(root)
return comparableValue === comparableRoot || comparableValue.startsWith(`${comparableRoot}/`)
? `${canonicalRoot}${value.slice(root.length)}`
: value
}
}
166 changes: 166 additions & 0 deletions src/core/auto-approval/__tests__/WorkspaceRootCanonicalizer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { PosixWorkspaceRootStrategy } from "../strategies/PosixWorkspaceRootStrategy"
import { WindowsWorkspaceRootStrategy } from "../strategies/WindowsWorkspaceRootStrategy"
import type { WorkspaceRootStrategy } from "../strategies/WorkspaceRootStrategy"
import { WorkspaceRootCanonicalizer } from "../WorkspaceRootCanonicalizer"

it("normalizes roots once in the factory before any path comparisons", () => {
const normalizeAbsoluteRoot = vi
.fn<WorkspaceRootStrategy["normalizeAbsoluteRoot"]>()
.mockReturnValueOnce("/aliases/project")
.mockReturnValueOnce("/storage/project")
const comparisonKey = vi.fn((value: string) => value)
const strategy: WorkspaceRootStrategy = {
normalizeAbsoluteRoot,
comparisonKey,
toMatcherAbsolutePath: vi.fn((value: string) => value),
}

const canonicalizer = WorkspaceRootCanonicalizer.forStrategy("/aliases/project/", "/storage/project/", strategy)

expect(normalizeAbsoluteRoot).toHaveBeenCalledTimes(2)
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(1, "/aliases/project/")
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(2, "/storage/project/")
expect(comparisonKey).not.toHaveBeenCalled()

expect(canonicalizer.canonicalize("/aliases/project/docs/file.md")).toBe("/storage/project/docs/file.md")
expect(canonicalizer.canonicalize("/aliases/project/notes.md")).toBe("/storage/project/notes.md")
expect(normalizeAbsoluteRoot).toHaveBeenCalledTimes(2)
})

describe.each([
{
platform: "POSIX",
isWindows: false,
cwd: "/aliases/project",
canonicalCwd: "/storage/project",
root: "/aliases/project",
canonicalRoot: "/storage/project",
},
{
platform: "Windows",
isWindows: true,
cwd: "C:\\Aliases\\Project",
canonicalCwd: "D:\\Storage\\Project",
root: "/C/Aliases/Project",
canonicalRoot: "/D/Storage/Project",
},
])("workspace-root canonicalization on $platform", ({ isWindows, cwd, canonicalCwd, root, canonicalRoot }) => {
const canonicalizer = WorkspaceRootCanonicalizer.forPlatform(cwd, canonicalCwd, isWindows)

it("translates an exact root and preserves descendant spelling and globs", () => {
expect(canonicalizer.canonicalize(root)).toBe(canonicalRoot)
expect(canonicalizer.canonicalize(`${root}/Docs/NOTES.md`)).toBe(`${canonicalRoot}/Docs/NOTES.md`)
expect(canonicalizer.canonicalize(`${root}/docs/**`)).toBe(`${canonicalRoot}/docs/**`)
})

it("does not translate similarly prefixed siblings or lexical parents", () => {
for (const value of [`${root}-other/docs/file.md`, `${root}2/file.md`, "/elsewhere/docs/file.md"]) {
expect(canonicalizer.canonicalize(value)).toBe(value)
}
})

it("leaves canonical-root paths and relative values unchanged", () => {
for (const value of [canonicalRoot, `${canonicalRoot}/docs/file.md`, "docs/file.md", "../shared/file.md"]) {
expect(canonicalizer.canonicalize(value)).toBe(value)
}
})

it.each([undefined, "", "relative/project", "C:relative"])(
"does not infer equivalence from an unknown or relative root: %s",
(invalidRoot) => {
const value = `${root}/docs/file.md`
expect(
WorkspaceRootCanonicalizer.forPlatform(invalidRoot, canonicalCwd, isWindows).canonicalize(value),
).toBe(value)
expect(WorkspaceRootCanonicalizer.forPlatform(cwd, invalidRoot, isWindows).canonicalize(value)).toBe(value)
},
)
})

describe("POSIX workspace-root strategy", () => {
it("compares case-sensitively and keeps backslashes as filename characters", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"/aliases/My\\Project",
"/storage/My\\Project",
new PosixWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/aliases/My\\Project/docs/file.md")).toBe(
"/storage/My\\Project/docs/file.md",
)
expect(canonicalizer.canonicalize("/aliases/my\\project/docs/file.md")).toBe(
"/aliases/my\\project/docs/file.md",
)
expect(canonicalizer.canonicalize("/aliases/My/Project/docs/file.md")).toBe("/aliases/My/Project/docs/file.md")
})

it("normalizes root dot segments and trailing separators", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"/aliases/temporary/../project/",
"/storage/./project/",
new PosixWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/aliases/project/docs/file.md")).toBe("/storage/project/docs/file.md")
})

it("does not interpret Windows drive paths as absolute roots", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"C:/aliases/project",
"D:/storage/project",
new PosixWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/C/aliases/project/docs/file.md")).toBe("/C/aliases/project/docs/file.md")
})
})

describe("Windows workspace-root strategy", () => {
it("compares case-insensitively across drives without changing descendant case", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"C:\\Aliases\\Project",
"D:\\Storage\\Project",
new WindowsWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/c/ALIASES/project/Docs/NOTES.md")).toBe("/D/Storage/Project/Docs/NOTES.md")
expect(canonicalizer.canonicalize("/E/Aliases/Project/Docs/NOTES.md")).toBe("/E/Aliases/Project/Docs/NOTES.md")
})

it("normalizes mixed root separators, dot segments, and trailing separators", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"C:\\Aliases/temporary/../Project\\",
"D:/Storage/./Project/",
new WindowsWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/C/Aliases/Project/docs/file.md")).toBe("/D/Storage/Project/docs/file.md")
})

it("supports drive roots and drive-less absolute roots in matcher notation", () => {
const strategy = new WindowsWorkspaceRootStrategy()
expect(WorkspaceRootCanonicalizer.forStrategy("C:", "D:/", strategy).canonicalize("/C/docs/file.md")).toBe(
"/D/docs/file.md",
)
expect(
WorkspaceRootCanonicalizer.forStrategy("\\Aliases\\Project", "/Storage/Project", strategy).canonicalize(
"/Aliases/Project/docs/file.md",
),
).toBe("/Storage/Project/docs/file.md")
})

it("normalizes network roots into the shared UNC matcher namespace", () => {
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
"\\\\server\\share\\project",
"\\\\server\\storage\\project",
new WindowsWorkspaceRootStrategy(),
)
expect(canonicalizer.canonicalize("/UNC/SERVER/SHARE/project/docs/file.md")).toBe(
"/UNC/server/storage/project/docs/file.md",
)
})
})

it.each([false, true])("preserves filesystem-root equivalence (Windows=%s)", (isWindows) => {
expect(WorkspaceRootCanonicalizer.forPlatform("/", "/storage", isWindows).canonicalize("/docs/file.md")).toBe(
"/storage/docs/file.md",
)
expect(
WorkspaceRootCanonicalizer.forPlatform("/aliases", "/", isWindows).canonicalize("/aliases/docs/file.md"),
).toBe("/docs/file.md")
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { isFileMatchedByPatterns } from "../filePatterns"
import type { WorkspaceRootStrategy } from "../strategies/WorkspaceRootStrategy"
import { WorkspaceRootStrategyFactory } from "../strategies/WorkspaceRootStrategyFactory"

describe("WorkspaceRootStrategyFactory", () => {
afterEach(() => vi.restoreAllMocks())

it("selects POSIX behavior without interpreting drives, network prefixes, or backslashes", () => {
const strategy = WorkspaceRootStrategyFactory.create(false)

expect(strategy.toMatcherAbsolutePath("/tmp/notes.md", "/C")).toBe("/tmp/notes.md")
expect(strategy.toMatcherAbsolutePath("//server/share/notes.md", "/C")).toBe("//server/share/notes.md")
expect(strategy.toMatcherAbsolutePath("/tmp/my\\file.md", "/C")).toBe("/tmp/my\\file.md")
expect(strategy.comparisonKey("/tmp/NOTES.md")).toBe("/tmp/NOTES.md")
})

it("selects Windows behavior for drives, rooted paths, and the distinct UNC namespace", () => {
const strategy = WorkspaceRootStrategyFactory.create(true)

expect(strategy.toMatcherAbsolutePath("D:/tmp/notes.md", "/C")).toBe("/D/tmp/notes.md")
expect(strategy.toMatcherAbsolutePath("/tmp/notes.md", "/C")).toBe("/C/tmp/notes.md")
expect(strategy.toMatcherAbsolutePath("//server/share/notes.md", "/C")).toBe("/UNC/server/share/notes.md")
expect(strategy.toMatcherAbsolutePath("//C/share/notes.md", "/C")).not.toBe("/C/share/notes.md")
expect(strategy.comparisonKey("/D/Tmp/NOTES.md")).toBe("/d/tmp/notes.md")
})

it.each([false, true])("returns independent instances (Windows=%s)", (isWindows) => {
expect(WorkspaceRootStrategyFactory.create(isWindows)).not.toBe(WorkspaceRootStrategyFactory.create(isWindows))
})

it("uses one injected strategy for candidates, every pattern, and both workspace roots", () => {
const toMatcherAbsolutePath = vi.fn((value: string) => value)
const normalizeAbsoluteRoot = vi
.fn<WorkspaceRootStrategy["normalizeAbsoluteRoot"]>()
.mockReturnValueOnce("/aliases/project")
.mockReturnValueOnce("/storage/project")
const comparisonKey = vi.fn((value: string) => value)
const strategy: WorkspaceRootStrategy = { toMatcherAbsolutePath, normalizeAbsoluteRoot, comparisonKey }
const create = vi.spyOn(WorkspaceRootStrategyFactory, "create").mockReturnValue(strategy)

expect(
isFileMatchedByPatterns({
filePath: "/storage/project/docs/notes.md",
cwd: "/aliases/project",
canonicalCwd: "/storage/project",
patterns: ["docs/**", "!/aliases/project/docs/private.md", "/aliases/project/docs/notes.md"],
isWindows: false,
}),
).toBe(true)

expect(create).toHaveBeenCalledOnce()
expect(create).toHaveBeenCalledWith(false)
expect(toMatcherAbsolutePath).toHaveBeenCalledWith("/storage/project/docs/notes.md", "")
expect(toMatcherAbsolutePath).toHaveBeenCalledWith("/aliases/project", "")
expect(toMatcherAbsolutePath).toHaveBeenCalledWith("/aliases/project/docs/private.md", "")
expect(toMatcherAbsolutePath).toHaveBeenCalledWith("/aliases/project/docs/notes.md", "")
expect(normalizeAbsoluteRoot).toHaveBeenCalledTimes(2)
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(1, "/aliases/project")
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(2, "/storage/project")
expect(comparisonKey).toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion src/core/auto-approval/__tests__/filePatterns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("toMatcherPattern", () => {
// name, so `C:/tmp/notes.md` names a file inside it, relative to the workspace.
describe("on Windows", () => {
// The colon is dropped, leaving the drive as an ordinary leading path
// segment; see `toPosixAbsolutePath` for why it cannot stay.
// segment: the Windows strategy avoids drive-rooted paths rejected by `ignore`.
it("keeps the drive of an absolute pattern as its first path segment", () => {
expect(toWindowsPattern("D:/tmp/notes.md")).toBe("/D/tmp/notes.md")
})
Expand Down
87 changes: 87 additions & 0 deletions src/core/auto-approval/__tests__/windowsPaths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,63 @@ describe("matching on Windows", () => {
const matches = (filePath: string, patterns: string[], cwd: string | undefined = WINDOWS_CWD) =>
matcher().isFileMatchedByPatterns({ filePath, cwd, patterns, homeDir: WINDOWS_HOME })

it.each(["\\\\server\\share\\project\\docs\\notes.md", "//server/share/project/docs/notes.md"])(
"matches an absolute UNC candidate against a relative grant: %s",
(filePath) => {
expect(matches(filePath, ["docs/**"], "\\\\server\\share\\project")).toBe(true)
},
)

it("matches explicit UNC patterns from a local-drive workspace and without a workspace", () => {
const filePath = "\\\\server\\share\\project\\docs\\notes.md"
const patterns = ["//server/share/project/docs/**"]
expect(matches(filePath, patterns)).toBe(true)
expect(matcher().isFileMatchedByPatterns({ filePath, patterns, homeDir: WINDOWS_HOME })).toBe(true)
})

it.each([
{ cwd: "\\\\alias\\share\\project", canonicalCwd: "\\\\server\\storage\\project" },
{ cwd: "C:\\Aliases\\Project", canonicalCwd: "\\\\server\\storage\\project" },
{ cwd: "\\\\alias\\share\\project", canonicalCwd: "D:\\Storage\\Project" },
])("matches equivalent local and UNC roots ($cwd -> $canonicalCwd)", ({ cwd, canonicalCwd }) => {
const filePath = `${canonicalCwd}\\DOCS\\NOTES.md`
const rootMatch = (patterns: string[]) =>
matcher().isFileMatchedByPatterns({ filePath, cwd, canonicalCwd, patterns, homeDir: WINDOWS_HOME })
expect(rootMatch(["docs/**"])).toBe(true)
expect(rootMatch([`${cwd}/docs/**`])).toBe(true)
expect(rootMatch([`${canonicalCwd}/docs/**`])).toBe(true)
expect(rootMatch(["docs/**", `!${cwd}/docs/notes.md`])).toBe(false)
expect(rootMatch(["docs/**", `!${canonicalCwd}/docs/notes.md`])).toBe(false)
expect(rootMatch(["docs/**", `!${cwd}/docs/notes.md`, `${canonicalCwd}/docs/notes.md`])).toBe(true)
})

it("honors UNC exclusions for both relative and absolute candidates", () => {
const cwd = "\\\\server\\share\\project"
const patterns = ["docs/**", "!//server/share/project/docs/private.md"]
expect(matches("docs/private.md", patterns, cwd)).toBe(false)
expect(matches(`${cwd}\\docs\\private.md`, patterns, cwd)).toBe(false)
expect(matches(`${cwd}\\docs\\notes.md`, patterns, cwd)).toBe(true)
expect(matches("docs/private.md", [...patterns, "//server/share/project/docs/private.md"], cwd)).toBe(true)
})

it("keeps different UNC servers, shares, and similarly prefixed siblings separate", () => {
const cwd = "\\\\server\\share\\project"
for (const filePath of [
"\\\\other-server\\share\\project\\docs\\notes.md",
"\\\\server\\other-share\\project\\docs\\notes.md",
"\\\\server\\share\\project-other\\docs\\notes.md",
]) {
expect(matches(filePath, ["docs/**"], cwd)).toBe(false)
}
})

it("does not confuse a UNC server named C with the local C drive", () => {
expect(matches("C:\\share\\project\\docs\\notes.md", ["docs/**"], "\\\\C\\share\\project")).toBe(false)
expect(matches("\\\\C\\share\\project\\docs\\notes.md", ["docs/**"], "C:\\share\\project")).toBe(false)
expect(matches("C:\\share\\project\\docs\\notes.md", ["//C/share/project/docs/**"])).toBe(false)
expect(matches("\\\\C\\share\\project\\docs\\notes.md", ["C:/share/project/docs/**"])).toBe(false)
})

it("matches a workspace-relative path against a bare pattern", () => {
expect(matches("notes.md", ["notes.md"])).toBe(true)
expect(matches("docs\\notes.md", ["notes.md"])).toBe(true)
Expand Down Expand Up @@ -99,4 +156,34 @@ describe("matching on Windows", () => {
expect(matches("C:/path/to/repo/etc/passwd", ["passwd"])).toBe(true)
expect(matches("C:/etc/passwd", ["passwd"])).toBe(false)
})

it("matches equivalent workspace roots across drives with Windows case and ordered exclusions", () => {
const rootMatch = (patterns: string[]) =>
matcher().isFileMatchedByPatterns({
filePath: "d:\\storage\\project\\DOCS\\NOTES.md",
cwd: "C:\\Aliases\\Project",
canonicalCwd: "D:\\Storage\\Project",
patterns,
})
expect(rootMatch(["docs/**"])).toBe(true)
expect(rootMatch(["c:/aliases/PROJECT/docs/**"])).toBe(true)
expect(rootMatch(["d:/STORAGE/project/docs/**"])).toBe(true)
expect(rootMatch(["docs/**", "!c:/aliases/project/docs/notes.md"])).toBe(false)
expect(rootMatch(["docs/**", "!D:/Storage/Project/docs/notes.md"])).toBe(false)
})

it("keeps Windows siblings and lexical-parent outside patterns separate from canonical roots", () => {
const rootMatch = (filePath: string, patterns: string[]) =>
matcher().isFileMatchedByPatterns({
filePath,
cwd: "C:\\Aliases\\Project",
canonicalCwd: "D:\\Storage\\Project",
patterns,
})
expect(rootMatch("C:/Aliases/shared/file.md", ["../shared/file.md"])).toBe(true)
expect(rootMatch("D:/Storage/shared/file.md", ["../shared/file.md"])).toBe(false)
expect(rootMatch("D:/Storage/shared/file.md", ["D:/Storage/shared/file.md"])).toBe(true)
expect(rootMatch("D:/Storage/Project-other/docs/file.md", ["docs/**"])).toBe(false)
expect(rootMatch("D:/Storage/Project-other/docs/file.md", ["C:/Aliases/Project-other/docs/**"])).toBe(false)
})
})
Loading
Loading