From c03adfa62861d7fc21de4abdea4a27fd4754356c Mon Sep 17 00:00:00 2001 From: hy5424 <1031353743@qq.com> Date: Tue, 27 Oct 2026 22:19:32 +0800 Subject: [PATCH] fix: keep a session cwd that is a subdirectory of the repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pathUnderOf(base, target) answers "is target under base", so the effectiveBase guard has to be called as (canonicalTop, base). It was called as (base, canonicalTop), which asks the opposite question and always fell back to the repository toplevel — every workspace-relative path opened through the sidebar then resolved one level too high (deliverable cards reported cannot read ... ENOENT). Also adds the regression case the suite was missing: a workspace registered at a plain SUBDIRECTORY of the repository (base != top). --- src/active-worktree.ts | 2 +- tests/active-worktree.spec.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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