diff --git a/src/active-worktree.ts b/src/active-worktree.ts index 94e65cc8..208ac203 100644 --- a/src/active-worktree.ts +++ b/src/active-worktree.ts @@ -519,7 +519,7 @@ async function resolveActiveWorktreeRoot(ctx: Context, sessionId: string, base: return base } const canonicalTop = requireAbsolute(top) - const effectiveBase = pathUnderOf(base, canonicalTop) ? base : canonicalTop + const effectiveBase = pathUnderOf(canonicalTop, base) ? base : canonicalTop const active = await activeRootFromEvents(await sessionEventsOf(ctx, sessionId), effectiveBase) const root = active ?? effectiveBase cachePut(activeWorktreeRootCache, sessionId, { root, base, directoryEpoch, eventSeq, gitResolved: true, expires: Date.now() + ACTIVE_ROOT_TTL_MS }, ACTIVE_ROOT_CACHE_MAX) diff --git a/tests/active-worktree.spec.ts b/tests/active-worktree.spec.ts index 97ad289b..bdf843dd 100644 --- a/tests/active-worktree.spec.ts +++ b/tests/active-worktree.spec.ts @@ -162,6 +162,23 @@ describe('active worktree root', () => { } }) + it('keeps a session cwd that is a SUBDIRECTORY of the repository', async () => { + // A workspace registered at `/workspace` (a plain subdirectory, not a + // linked worktree) must stay the root: replacing it with the repository + // toplevel makes every relative path resolve one level too high. + const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-')) + const repo = join(root, 'repo') + const subdir = join(repo, 'workspace') + try { + initRepo(repo, 'a.txt') + mkdirSync(subdir) + const ctx = sessionCtx(subdir, []) + await expect(activeWorktreeRootOf(ctx, 's-subdir', subdir)).resolves.toBe(realpathSync.native(subdir)) + } finally { + removeTree(root) + } + }) + 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