fix(app): close missing session tabs (#33785)

This commit is contained in:
Brendan Allan 2026-06-25 13:28:32 +08:00 committed by GitHub
commit bdee94ca40
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 22 deletions

View file

@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test"
import type { SessionNotFoundError } from "@opencode-ai/sdk/v2/client"
import type { ConfigInvalidError, ProviderModelNotFoundError } from "./server-errors"
import { formatServerError, parseReadableConfigInvalidError } from "./server-errors"
import { formatServerError, isSessionNotFoundError, parseReadableConfigInvalidError } from "./server-errors"
function fill(text: string, vars?: Record<string, string | number>) {
if (!vars) return text
@ -142,3 +143,33 @@ describe("formatServerError", () => {
expect(formatServerError(wrapped, language.t)).toBe("Arquivo de config em config invalido: Missing host")
})
})
describe("isSessionNotFoundError", () => {
test("matches an SDK-wrapped error for the requested session", () => {
const body = {
_tag: "SessionNotFoundError",
sessionID: "ses_missing",
message: "Session not found",
} satisfies SessionNotFoundError
expect(isSessionNotFoundError(new Error(body.message, { cause: { body, status: 404 } }), body.sessionID)).toBe(true)
})
test("rejects errors for other sessions and other 404 responses", () => {
const body = {
_tag: "SessionNotFoundError",
sessionID: "ses_parent",
message: "Session not found",
} satisfies SessionNotFoundError
expect(isSessionNotFoundError(new Error(body.message, { cause: { body, status: 404 } }), "ses_tab")).toBe(false)
expect(
isSessionNotFoundError(
new Error("Provider not found", {
cause: { body: { _tag: "ProviderNotFoundError", providerID: "missing" }, status: 404 },
}),
"ses_tab",
),
).toBe(false)
})
})

View file

@ -42,6 +42,13 @@ function unwrapNamedError(error: unknown): unknown {
return error
}
export function isSessionNotFoundError(error: unknown, sessionID: string) {
const unwrapped = unwrapNamedError(error)
if (typeof unwrapped !== "object" || unwrapped === null) return false
const value = unwrapped as Record<string, unknown>
return value._tag === "SessionNotFoundError" && value.sessionID === sessionID
}
function isConfigInvalidErrorLike(error: unknown): error is ConfigInvalidError {
if (typeof error !== "object" || error === null) return false
const o = error as Record<string, unknown>