diff --git a/src/active-worktree.ts b/src/active-worktree.ts index 08e45bf0..c1120106 100644 --- a/src/active-worktree.ts +++ b/src/active-worktree.ts @@ -52,7 +52,14 @@ const activeWorktreeRootCache = new Map() const linkedWorktreeCache = new Map() -/** Normalized identity of a path (case- and separator-insensitive). */ +/** + * Normalized IDENTITY of a path (case- and separator-insensitive), for + * comparisons and cache keys only: it lowercases, so it must never be handed + * back to the filesystem. On Linux (case-sensitive) `…/iHpOqm` → `…/ihpoqm` + * is a different, usually non-existent path, and probing it silently drops + * the candidate; macOS and Windows mask that because their filesystems fold + * case. Keep the real path next to its key. + */ function pathKeyOf(value: string): string { return value.replace(/[\\/]+/g, '/').replace(/\/+$/, '').toLowerCase() } @@ -87,12 +94,21 @@ function normalizeActivePath(value: string): string { return trimmed.replace(/[\\/]+/g, sep) } -function addActivePath(map: Map, value: unknown, weight: number): void { +/** One candidate path mentioned by a tool call, with its accumulated score. */ +interface ActivePathEntry { + /** The real path (original case), the only form safe to probe. */ + path: string + score: number +} + +function addActivePath(map: Map, value: unknown, weight: number): void { if (typeof value !== 'string') return const normalized = normalizeActivePath(value) if (normalized === '' || !isAbsolute(normalized)) return const key = pathKeyOf(normalized) - map.set(key, (map.get(key) ?? 0) + weight) + const entry = map.get(key) + if (entry === undefined) map.set(key, { path: normalized, score: weight }) + else entry.score += weight } /** @@ -101,7 +117,7 @@ function addActivePath(map: Map, value: unknown, weight: number) * @param map - accumulator keyed by normalized path identity. * @param structured - whether the current value sits in a cwd/workdir/path field. */ -function collectActivePaths(value: unknown, map: Map, structured: boolean): void { +function collectActivePaths(value: unknown, map: Map, structured: boolean): void { if (typeof value === 'string') { if (isAbsolute(value)) { addActivePath(map, value, structured ? 8 : 1) @@ -175,22 +191,22 @@ async function activeRootFromEvents(events: readonly SidebarSessionEvent[], base for (let index = events.length - 1; index >= start; index -= 1) { const event = events[index] if (event === undefined || event.type !== 'tool/call') continue - const weights = new Map() + const weights = new Map() collectActivePaths(event.data, weights, false) if (weights.size === 0) continue - const ordered = [...weights.entries()].sort((left, right) => right[1] - left[1]) + const ordered = [...weights.values()].sort((left, right) => right.score - left.score) const scores = new Map() - for (const [value, weight] of ordered) { + for (const candidate of ordered) { if (probes >= ACTIVE_ROOT_MAX_PROBES) break probes += 1 - const top = await topOfActivePath(value) + const top = await topOfActivePath(candidate.path) if (top === undefined) continue const topKey = pathKeyOf(top) if (topKey === baseKey) continue if (!(await isLinkedWorktree(top))) continue const entry = scores.get(topKey) - if (entry === undefined) scores.set(topKey, { top, score: weight }) - else entry.score += weight + if (entry === undefined) scores.set(topKey, { top, score: candidate.score }) + else entry.score += candidate.score } if (scores.size > 0) { let best: string | undefined diff --git a/tests/active-worktree.spec.ts b/tests/active-worktree.spec.ts index 7bc78078..1c9f604f 100644 --- a/tests/active-worktree.spec.ts +++ b/tests/active-worktree.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { spawnSync } from 'node:child_process' import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' @@ -6,6 +6,22 @@ import { join } from 'node:path' import { activeWorktreeRootOf } from '../src/active-worktree.ts' import type { Context } from '../src/context-types.ts' +/** Every cwd handed to the plugin's own git probe, in call order. */ +const probed = vi.hoisted(() => [] as string[]) + +// Records (and delegates to) the real spawn so the case-folding regression is +// visible on case-insensitive hosts too. +vi.mock('../src/git.ts', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + runGitRaw: (cwd: string, args: string[], timeoutMs?: number) => { + probed.push(cwd) + return actual.runGitRaw(cwd, args, timeoutMs) + }, + } +}) + const IDENTITY = { GIT_AUTHOR_NAME: 'dsh-better-sidebar-test', GIT_AUTHOR_EMAIL: 'test@dsh.invalid', @@ -78,4 +94,27 @@ describe('active worktree root', () => { rmSync(root, { recursive: true, force: true }) } }) + + it('probes the REAL path, never its case-folded identity (Linux is case-sensitive)', async () => { + // `addActivePath` keys candidates by a lowercased identity; probing that + // KEY instead of the path silently drops every candidate whose path has an + // uppercase letter. macOS and Windows fold case and never noticed it — + // `Repo-A` / `WorkTree-B` below make the difference visible on every host. + const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-')) + const repoA = join(root, 'Repo-A') + const repoB = join(root, 'Repo-B') + const worktreeB = join(root, 'WorkTree-B') + try { + initRepo(repoA, 'a.txt') + initRepo(repoB, 'b.txt') + git(repoB, ['worktree', 'add', '-q', '-b', 'agent', worktreeB]) + probed.length = 0 + const ctx = sessionCtx(repoA, [{ cwd: worktreeB, path: join(worktreeB, 'b.txt') }]) + await expect(activeWorktreeRootOf(ctx, 's-case', repoA)).resolves.toBe(realpathSync.native(worktreeB)) + expect(probed).toContain(worktreeB) + expect(probed).not.toContain(worktreeB.toLowerCase()) + } finally { + rmSync(root, { recursive: true, force: true }) + } + }) })