Skip to content

Commit b88ca5c

Browse files
committed
fix(app): keep nested project sessions in the opened directory
Opening a folder that is a subdirectory of a git repository and starting a session there registered the session at the repository root, so everything scoped to the opened folder — most visibly `.opencode/skills/` — was never discovered. Both fallbacks that map a directory onto "its project" went through the server's project metadata, whose `worktree` is always the git repository root (`Project.resolve` returns `git rev-parse --show-toplevel`). A nested subdirectory resolves to the same project as the repository root but, unlike a linked worktree, is not listed in `sandboxes`, so it was silently rewritten to the root. `projectRoot()` then navigated to `/<repo-root>/session`, and the sidebar's new-session button used the project's worktree, which is what `POST /api/session` receives as `location.directory`. Only the worktree itself and the project's sandboxes now collapse onto the worktree; a nested directory stays its own root, and a new session starts in the directory the route is actually in. Refs anomalyco#51191
1 parent 7a8d46e commit b88ca5c

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎packages/app/src/pages/layout.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
effectiveWorkspaceOrder,
6666
errorMessage,
6767
latestRootSession,
68+
projectRootForDirectory,
6869
sortedRootSessions,
6970
} from "./layout/helpers"
7071
import {
@@ -1127,7 +1128,7 @@ export default function LegacyLayout(props: ParentProps) {
11271128
if (!id) return directory
11281129

11291130
const meta = serverSync().data.project.find((item) => item.id === id)
1130-
return meta?.worktree ?? directory
1131+
return projectRootForDirectory(directory, meta) ?? directory
11311132
}
11321133

11331134
function activeProjectRoot(directory: string) {
@@ -1932,6 +1933,14 @@ export default function LegacyLayout(props: ParentProps) {
19321933
})
19331934
const projectId = createMemo(() => project()?.id ?? "")
19341935
const worktree = createMemo(() => project()?.worktree ?? "")
1936+
// A session must start in the directory the route is in. A nested subdirectory of a repo
1937+
// shares the project's worktree, so falling back to it would root the session — and the
1938+
// skill, agent and instruction discovery scoped to it — at the repo root instead.
1939+
const newSessionDirectory = createMemo(() => {
1940+
const dir = currentDir()
1941+
if (dir && project() === currentProject()) return dir
1942+
return worktree()
1943+
})
19351944
const slug = createMemo(() => {
19361945
const dir = worktree()
19371946
if (!dir) return ""
@@ -2106,7 +2115,7 @@ export default function LegacyLayout(props: ParentProps) {
21062115
size="large"
21072116
class="w-full"
21082117
onClick={() => {
2109-
const dir = worktree()
2118+
const dir = newSessionDirectory()
21102119
if (!dir) return
21112120
navigateWithSidebarReset(`/${base64Encode(dir)}/session`)
21122121
}}

‎packages/app/src/pages/layout/helpers.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
homeProjectDirectories,
2020
homeSessionServerStatus,
2121
latestRootSession,
22+
projectRootForDirectory,
2223
sortedRootSessions,
2324
toggleHomeProjectSelection,
2425
} from "./helpers"
@@ -344,4 +345,14 @@ describe("layout workspace helpers", () => {
344345
expect(errorMessage(new Error("broken"), "fallback")).toBe("broken")
345346
expect(errorMessage("unknown", "fallback")).toBe("fallback")
346347
})
348+
349+
test("keeps a nested project directory out of the repo root", () => {
350+
const project = { worktree: "/home/me/Documents", sandboxes: ["/home/me/worktrees/feature"] }
351+
352+
expect(projectRootForDirectory("/home/me/Documents", project)).toBe("/home/me/Documents")
353+
expect(projectRootForDirectory("/home/me/Documents/", project)).toBe("/home/me/Documents")
354+
expect(projectRootForDirectory("/home/me/worktrees/feature", project)).toBe("/home/me/Documents")
355+
expect(projectRootForDirectory("/home/me/Documents/knowledgebase", project)).toBeUndefined()
356+
expect(projectRootForDirectory("/home/me/Documents", undefined)).toBeUndefined()
357+
})
347358
})

‎packages/app/src/pages/layout/helpers.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,18 @@ export const effectiveWorkspaceOrder = (local: string, dirs: string[], persisted
140140

141141
return [...result, ...live.values()]
142142
}
143+
144+
// A nested subdirectory of a repo resolves to the same project as the repo root, but it is
145+
// not interchangeable with it the way a linked worktree is. Only the worktree itself and the
146+
// project's sandboxes may be collapsed onto the worktree: collapsing a nested directory moves
147+
// the route, and every session started from it, up to the repo root.
148+
export function projectRootForDirectory<T extends { worktree: string; sandboxes?: readonly string[] }>(
149+
directory: string,
150+
project: T | undefined,
151+
) {
152+
if (!project) return undefined
153+
const key = pathKey(directory)
154+
if (pathKey(project.worktree) === key) return project.worktree
155+
if (project.sandboxes?.some((sandbox) => pathKey(sandbox) === key)) return project.worktree
156+
return undefined
157+
}

0 commit comments

Comments
 (0)