Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/opencode/src/cli/cmd/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
const match = sync.data.session
.toSorted((a, b) => b.time.updated - a.time.updated)
.find((x) => x.parentID === undefined)?.id
continued = true
if (match) {
continued = true
if (args.fork) {
void sdk.client.session.fork({ sessionID: match }).then((result) => {
if (result.data?.id) {
Expand All @@ -416,6 +416,10 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
} else {
route.navigate({ type: "session", sessionID: match })
}
} else {
// No sessions found for directory — navigate away from the initial "dummy" session
// route to prevent a crash when the Session component tries to fetch a non-existent session
route.navigate({ type: "home" })
}
})

Expand Down
35 changes: 24 additions & 11 deletions packages/opencode/src/cli/cmd/tui/validate-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ export async function validateSession(input: {
fetch?: typeof fetch
headers?: RequestInit["headers"]
}) {
if (!input.sessionID) return

let sessionID: SessionID
try {
sessionID = decodeSessionID(input.sessionID)
} catch (error) {
throw new Error(`Invalid session ID: ${error instanceof Error ? error.message : "unknown error"}`, { cause: error })
}

await createOpencodeClient({
const client = createOpencodeClient({
baseUrl: input.url,
directory: input.directory,
fetch: input.fetch,
headers: input.headers,
}).session.get({ sessionID }, { throwOnError: true })
})

if (input.sessionID) {
let sessionID: SessionID
try {
sessionID = decodeSessionID(input.sessionID)
} catch (error) {
throw new Error(`Invalid session ID: ${error instanceof Error ? error.message : "unknown error"}`, { cause: error })
}

await client.session.get({ sessionID }, { throwOnError: true })
}

if (input.directory && !input.sessionID) {
const response = await client.session.list(
{ directory: input.directory },
{ throwOnError: true },
)
const sessions = response.data ?? []
if (sessions.length === 0) {
throw new Error(`No sessions found for directory: ${input.directory}`)
}
}
}
92 changes: 92 additions & 0 deletions packages/opencode/test/cli/validate-session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { describe, expect, test } from "bun:test"

/**
* Test for issue #28214: `opencode attach <url> --dir <PATH>` segfaults
* when no session on the server has a working directory matching `<PATH>`.
*
* This test verifies the core validation logic that was added to
* validate-session.ts to prevent the crash.
*
* NOTE: Due to broken bun dependency cache (@opentui/solid → missing babel
* packages), this test may fail to load when run from within the package.
* Run standalone: `cp test/cli/validate-session.test.ts /tmp/ && bun test /tmp/validate-session.test.ts`
*/
describe("attach --dir directory validation", () => {
/**
* Simulates the directory validation logic added to validate-session.ts
* (lines 32-41)
*/
async function validateDirectory(
client: {
session: { list: (params: { directory: string }) => Promise<{ data: Array<unknown> }> }
},
directory: string,
): Promise<void> {
const response = await client.session.list({ directory })
const sessions = response.data ?? []
if (sessions.length === 0) {
throw new Error(`No sessions found for directory: ${directory}`)
}
}

test("throws clear error when directory has no matching sessions", async () => {
const mockClient = {
session: {
list: async () => ({ data: [] }),
},
}

await expect(
validateDirectory(mockClient, "/tmp/nonexistent-dir"),
).rejects.toThrow("No sessions found for directory: /tmp/nonexistent-dir")
})

test("passes when directory has matching sessions", async () => {
const mockClient = {
session: {
list: async () => ({
data: [{ id: "session-123", directory: "/tmp/existing-dir" }],
}),
},
}

await expect(validateDirectory(mockClient, "/tmp/existing-dir")).resolves.toBeUndefined()
})

test("rejects empty sessions array", async () => {
const mockClient = {
session: {
list: async () => ({ data: [] }),
},
}

await expect(validateDirectory(mockClient, "/some/path")).rejects.toThrow()
})

test("accepts non-empty sessions array", async () => {
const mockClient = {
session: {
list: async () => ({
data: [
{ id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890", directory: "/some/path" },
{ id: "b2c3d4e5-f6a7-8901-bcde-f12345678901", directory: "/some/path" },
],
}),
},
}

await expect(validateDirectory(mockClient, "/some/path")).resolves.toBeUndefined()
})

test("handles null data gracefully", async () => {
const mockClient = {
session: {
list: async () => ({ data: null as unknown as Array<unknown> }),
},
}

await expect(validateDirectory(mockClient, "/some/path")).rejects.toThrow(
"No sessions found for directory: /some/path",
)
})
})
Loading