fix(sessions): route new sessions to the correct project when server omits directory (#1637, #2270) (#2272)

* fix(sessions): route new sessions to the correct project when server omits directory

createSession() passed the server response's `directory` field to
setCurrentSession as `directoryHint`. When the server response omitted
the field (a common path), `directoryHint` was `null` and
setCurrentSession fell back to `opencodeClient.getDirectory()`, which
could still hold a stale directory from a different project.

That fallback caused:
- #1637: clicking `+` on one project while browsing another created
  a session grouped under the wrong project.
- #2270: in a parent Git repo with multiple child projects (some
  without their own Git), sessions from a child with its own Git were
  grouped under a sibling child without Git.

Capture the effective directory passed to the SDK and reuse it as the
fallback for the server response. This guarantees `setCurrentSession`
and `registerSessionDirectory` always see the directory the user
explicitly selected for the new session.

Add regression tests covering both reported topologies and the
no-override / no-server-directory preservation path.

* fix(sessions): prefer registered project paths

---------

Co-authored-by: bashrusakh <bashrusakh@users.noreply.github.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
Leonid
2026-07-17 13:14:22 +03:00
committed by GitHub
co-authored by bashrusakh Bohdan Triapitsyn
parent 0202e88eb4
commit f5b4a267c0
3 changed files with 312 additions and 4 deletions
+8 -2
View File
@@ -436,13 +436,19 @@ export async function createSession(
metadata?: Record<string, unknown>,
): Promise<Session | null> {
try {
// Capture the effective directory used for session creation so we can fall
// back to it when the server response omits the `directory` field.
// Without this, setCurrentSession would fall through to a stale
// opencodeClient.getDirectory() value and group the session under the
// wrong project (closes #1637, #2270).
const effectiveDirectory = directoryOverride ?? dir()
const session = await opencodeClient.createSession({
title,
parentID: parentID ?? undefined,
metadata,
}, directoryOverride ?? dir())
}, effectiveDirectory)
const sessionDirectory = (session as { directory?: string | null }).directory ?? null
const sessionDirectory = (session as { directory?: string | null }).directory ?? effectiveDirectory ?? null
// Pre-populate routing index so SSE events arriving before session.created
// can be routed to the correct child store
if (sessionDirectory) {