diff --git a/src/index.ts b/src/index.ts index 260e87df..ab024ebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -260,13 +260,18 @@ function buildApi( const clientCwd = typeof record?.cwd === 'string' && record.cwd !== '' ? record.cwd : undefined return { sessionId, cwd: await sessionCwdOf(ctx, sessionId, clientCwd) } } - /** Resolve the optional Git-panel checkout selector against the authoritative + /** Resolve the optional Git-panel checkout selector against the selected * session repository. Unlike `cwd`, `worktree` is never trusted directly. */ const gitCwdOf = async (payload: unknown): Promise<{ sessionId: string; cwd: string }> => { const base = await cwdOf(payload) const record = payload as { worktree?: unknown } | null const requested = typeof record?.worktree === 'string' && record.worktree !== '' ? record.worktree : undefined - return { sessionId: base.sessionId, cwd: await git.resolveWorktree(base.cwd, requested) } + if (requested === undefined) return base + // A container workspace is not itself a repository. Validate the checkout + // against the selected child (or the default discovered repository), just + // as the inventory does, before accepting the requested worktree path. + const root = await git.repoRoot(base.cwd, selectedRepoOf(payload)) + return { sessionId: base.sessionId, cwd: await git.resolveWorktree(root, requested) } } // Subagent live previews: one batch request instead of N per-child // `subagents.history` calls. The route degrades to a 503 when the host diff --git a/tests/smoke.spec.ts b/tests/smoke.spec.ts index 247974b7..2895ba4e 100644 --- a/tests/smoke.spec.ts +++ b/tests/smoke.spec.ts @@ -5,7 +5,7 @@ */ import { describe, expect, it } from 'vitest' import { spawnSync } from 'node:child_process' -import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' +import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join, resolve as resolvePath } from 'node:path' import { SettingsConflictError, type SettingsNamespace } from '@deepseek-ai/dsh-settings' @@ -365,6 +365,49 @@ describe('session cwd resolution over the API route', () => { return out } + it('loads branches and history when switching child repositories and their linked checkouts', async () => { + const scratch = realpathSync.native(mkdtempSync(join(tmpdir(), 'sidebar-multi-repo-'))) + const cwd = join(scratch, 'workspace') + mkdirSync(cwd) + const run = (dir: string, args: string[]): void => { + const result = spawnSync('git', ['-C', dir, '-c', 'user.name=Test', '-c', 'user.email=test@dsh.invalid', ...args], { encoding: 'utf8' }) + if (result.status !== 0) throw new Error(result.stderr) + } + const a = join(cwd, 'a') + const b = join(cwd, 'b') + const linked = join(scratch, 'b-linked') + const unrelated = join(scratch, 'unrelated') + try { + for (const [path, branch] of [[a, 'a-main'], [b, 'b-main'], [unrelated, 'other']]) { + run(scratch, ['init', '-q', path!]) + run(path!, ['checkout', '-q', '-b', branch!]) + run(path!, ['commit', '-q', '--allow-empty', '-m', branch!]) + } + run(b, ['worktree', 'add', '-q', '-b', 'b-linked', linked]) + const route = mount({ sessions: { get: () => ({ header: { cwd } }) } }) + const base = { sessionId: 'multi', cwd } + expect(await invoke(route, 'git.log', base)).toMatchObject({ ok: true, value: [{ subject: 'a-main' }] }) + // Match the UI: inventory first, then both repoRoot and worktree on + // derived requests. Switching back used to fail just like switching away. + for (const [repoRoot, worktree, branch, subject] of [ + [b, b, 'b-main', 'b-main'], + [a, a, 'a-main', 'a-main'], + [b, linked, 'b-linked', 'b-main'], + ]) { + expect(await invoke(route, 'git.worktrees', { ...base, repoRoot })).toMatchObject({ ok: true }) + const payload = { ...base, repoRoot, worktree } + expect(await invoke(route, 'git.branch', payload)).toMatchObject({ ok: true, value: { current: branch } }) + expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 0 })).toMatchObject({ ok: true, value: [{ subject }] }) + expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 1 })).toMatchObject({ ok: true, value: [] }) + } + for (const worktree of [a, unrelated]) { + expect(await invoke(route, 'git.log', { ...base, repoRoot: b, worktree })).toMatchObject({ ok: false }) + } + } finally { + rmSync(scratch, { recursive: true, force: true }) + } + }) + it('uses the client summary cwd while the session is detached', async () => { const route = mount() const result = await invoke(route, 'session.cwd', { sessionId: 's-detached', cwd: '/tmp/summary-cwd' })