mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(app): evict transcripts of closed sessions (#58434)
This commit is contained in:
committed by
GitHub
Unverified
parent
dddec8f3c1
commit
d7a08170e7
@@ -5,7 +5,9 @@ import { pathKey } from "@/workspaces/path-key"
|
||||
import { useServerHealth } from "@/runtime/server/health"
|
||||
import { createServerSdkContext } from "./client"
|
||||
import { createServerSyncContext } from "./sync"
|
||||
import { createData } from "@opencode/client/solid"
|
||||
import { createData, createSessionRetention } from "@opencode/client/solid"
|
||||
import { useTabs } from "@/shell/tabs/tabs"
|
||||
import { useCurrentRoute } from "@/shell/state/layout"
|
||||
import type { ServerScope } from "@/runtime/server/scope"
|
||||
import { createPermissionAutoApprover } from "@/session/requests/auto-approve"
|
||||
import { createServerNotificationState } from "@/shell/notifications/notification"
|
||||
@@ -214,6 +216,28 @@ function createServerController(
|
||||
remove: (sessionID) => sdk.api.session.remove({ sessionID }),
|
||||
})
|
||||
|
||||
// Transcripts otherwise stay loaded for every session this window ever opened. Open tabs and the three most
|
||||
// recently viewed sessions keep theirs; any other session reloads its transcript when it is opened again.
|
||||
const tabs = useTabs()
|
||||
const route = useCurrentRoute()
|
||||
createSessionRetention({
|
||||
session: source.session,
|
||||
current: () => {
|
||||
const value = route()
|
||||
|
||||
return value.type === "session" && value.server === connKey ? value.sessionId : undefined
|
||||
},
|
||||
keep: () =>
|
||||
tabs.ready()
|
||||
? tabs.store.flatMap((tab) =>
|
||||
tab.type === "session" && tab.server === connKey
|
||||
? [tab.sessionId, tab.routeSessionId ?? tab.sessionId]
|
||||
: [],
|
||||
)
|
||||
: source.session.list().map((session) => session.id),
|
||||
limit: 3,
|
||||
})
|
||||
|
||||
const sync = createServerSyncContext(sdk, data)
|
||||
createPermissionAutoApprover({ sdk, data })
|
||||
const notification = createServerNotificationState({ sdk, data, key: connKey, coordinator: notificationCoordinator })
|
||||
|
||||
@@ -324,6 +324,7 @@ function SessionScreenContent(props: {
|
||||
)
|
||||
},
|
||||
() => conversationVisible() && messagesReady(),
|
||||
(sessionID) => server.ctx.data.session.message.list(sessionID),
|
||||
)
|
||||
|
||||
const mobileTabs = () => (
|
||||
|
||||
@@ -16,6 +16,7 @@ export function createTimelineCache(
|
||||
session: TimelineSessionSource & { identity: { workspaceKey: Accessor<string> } },
|
||||
render: (source: TimelineSessionSource, active: Accessor<boolean>) => JSX.Element,
|
||||
visible: Accessor<boolean>,
|
||||
transcript: (sessionID: string) => readonly unknown[],
|
||||
) {
|
||||
const owner = getOwner()
|
||||
let workspace = untrack(session.identity.workspaceKey)
|
||||
@@ -31,6 +32,20 @@ export function createTimelineCache(
|
||||
const select = <T>(read: Accessor<T>) =>
|
||||
createMemo<T>((previous) => (active() ? read() : previous), untrack(read))
|
||||
|
||||
// Session retention evicts the transcripts of closed sessions. A detached view would still hold the old
|
||||
// transcript and its DOM, so it goes too; reopening the session renders a new view.
|
||||
createComputed(
|
||||
on(
|
||||
() => !!id && transcript(id).length === 0,
|
||||
(evicted, previous) => {
|
||||
if (!evicted || previous !== false || untrack(active)) return
|
||||
queueMicrotask(() => {
|
||||
if (!untrack(active) && cache.peek(key)?.dispose === dispose) cache.delete(key)
|
||||
})
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
return {
|
||||
value: render(
|
||||
{
|
||||
|
||||
@@ -71,6 +71,7 @@ function setup() {
|
||||
return element
|
||||
},
|
||||
() => state.visible,
|
||||
(id) => state.messages[id] ?? [],
|
||||
)
|
||||
|
||||
return { state, setState, cache, views, disposed, dispose }
|
||||
@@ -105,6 +106,29 @@ test("reuses a session view and refreshes its own history when selected again",
|
||||
expect(input.disposed.sort()).toEqual(["ses_a", "ses_b"])
|
||||
})
|
||||
|
||||
test("disposes a detached view once its transcript is evicted, but never the selected one", async () => {
|
||||
const input = setup()
|
||||
|
||||
try {
|
||||
const first = input.cache()
|
||||
input.setState("messages", "ses_a", [])
|
||||
await Promise.resolve()
|
||||
expect(input.disposed).toEqual([])
|
||||
|
||||
input.setState("messages", "ses_a", [{ id: "msg_a", type: "user", text: "Reloaded", time: { created: 1 } }])
|
||||
input.setState("id", "ses_b")
|
||||
input.cache()
|
||||
input.setState("messages", "ses_a", [])
|
||||
await Promise.resolve()
|
||||
expect(input.disposed).toEqual(["ses_a"])
|
||||
|
||||
input.setState("id", "ses_a")
|
||||
expect(input.cache()).not.toBe(first)
|
||||
} finally {
|
||||
input.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
test("suspends a detached mobile view and reuses it when the conversation returns", () => {
|
||||
const input = setup()
|
||||
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from "./data"
|
||||
export * from "./connection"
|
||||
export * from "./pty"
|
||||
export * from "./usage"
|
||||
export * from "./session-retention"
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { createEffect, untrack, type Accessor } from "solid-js"
|
||||
import type { Data } from "@opencode/client/solid"
|
||||
import type { Data } from "./data"
|
||||
|
||||
export function createSessionRetention(input: {
|
||||
session: Pick<Data["session"], "list" | "root" | "evict">
|
||||
@@ -13,7 +13,7 @@ import { useLocation } from "./location"
|
||||
import { useStorage } from "./storage"
|
||||
import { useTuiPaths } from "./runtime"
|
||||
import { newSessionLocation } from "../config/new-session-location"
|
||||
import { createSessionRetention } from "./session-retention"
|
||||
import { createSessionRetention } from "@opencode/client/solid"
|
||||
import { createProgramStatus, type PendingSession } from "./program-status"
|
||||
import { anchorKey, type AnchorTarget } from "../routes/session/anchors"
|
||||
import {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import type { OpenCodeEvent } from "@opencode/client"
|
||||
import { createData } from "@opencode/client/solid"
|
||||
import { createData, createSessionRetention } from "@opencode/client/solid"
|
||||
import { createGlobalEmitter } from "@solid-primitives/event-bus"
|
||||
import { createRoot, createSignal } from "solid-js"
|
||||
import { createSessionRetention } from "../../src/context/session-retention"
|
||||
import { createApi, createFetch, directory, json } from "../fixture/tui-client"
|
||||
|
||||
function setup(options: { keep?: string[]; current?: string; limit?: number } = {}) {
|
||||
|
||||
Reference in New Issue
Block a user