mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(app,client): wire session transcript retention and evict background transcripts (#57025)
This commit is contained in:
committed by
GitHub
Unverified
parent
4386838922
commit
b4525d79bf
@@ -5,11 +5,13 @@ 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 type { ServerScope } from "@/runtime/server/scope"
|
||||
import { createPermissionAutoApprover } from "@/session/requests/auto-approve"
|
||||
import { createServerNotificationState } from "@/shell/notifications/notification"
|
||||
import { createNotificationCoordinator } from "@/shell/notifications/coordinator"
|
||||
import { useCurrentRoute } from "@/shell/state/layout"
|
||||
import { useTabs } from "@/shell/tabs/tabs"
|
||||
import { Persist, persisted } from "@/runtime/persistence/storage"
|
||||
import { createDesktopData } from "./data"
|
||||
import { ModelState } from "./persistence"
|
||||
@@ -168,6 +170,8 @@ function createServerController(
|
||||
) {
|
||||
const language = useLanguage()
|
||||
const settings = useSettings()
|
||||
const tabs = useTabs()
|
||||
const currentRoute = useCurrentRoute()
|
||||
const connKey = ServerConnection.key(conn)
|
||||
const sdk = createServerSdkContext(conn, scope)
|
||||
|
||||
@@ -194,6 +198,22 @@ function createServerController(
|
||||
remove: (sessionID) => sdk.api.session.remove({ sessionID }),
|
||||
})
|
||||
|
||||
createSessionRetention({
|
||||
session: data.session,
|
||||
current: () => {
|
||||
const route = currentRoute()
|
||||
|
||||
return route.type === "session" && route.server === connKey ? route.sessionId : undefined
|
||||
},
|
||||
keep: () =>
|
||||
tabs.store.flatMap((tab) =>
|
||||
tab.type === "session" && tab.server === connKey
|
||||
? [tab.sessionId, ...(tab.routeSessionId ? [tab.routeSessionId] : [])]
|
||||
: [],
|
||||
),
|
||||
limit: 3,
|
||||
})
|
||||
|
||||
const sync = createServerSyncContext(sdk, data)
|
||||
createPermissionAutoApprover({ sdk, data })
|
||||
const notification = createServerNotificationState({ sdk, data, key: connKey, coordinator: notificationCoordinator })
|
||||
|
||||
@@ -466,6 +466,7 @@ export function createData(config: CreateDataInput) {
|
||||
// Streaming events target one assistant message and, within it, the latest part of a kind.
|
||||
// A missing target means the row was never loaded or was evicted; the event is dropped.
|
||||
editAssistant(sessionID: string, messageID: string, fn: (assistant: SessionMessageAssistant) => void) {
|
||||
if (!messageIndex.get(sessionID)?.has(messageID)) return
|
||||
message.update(sessionID, (draft, index) => {
|
||||
const position = index.get(messageID)
|
||||
const item = position === undefined ? undefined : draft[position]
|
||||
@@ -876,6 +877,7 @@ export function createData(config: CreateDataInput) {
|
||||
})
|
||||
return
|
||||
case "session.shell.ended":
|
||||
if (!store.session.message[event.data.sessionID]) return
|
||||
message.update(event.data.sessionID, (draft) => {
|
||||
const match = message.shell(draft, event.data.shell.id)
|
||||
if (!match) return
|
||||
@@ -1142,6 +1144,7 @@ export function createData(config: CreateDataInput) {
|
||||
})
|
||||
return
|
||||
case "session.compaction.delta":
|
||||
if (!store.session.message[event.data.sessionID]) return
|
||||
message.update(event.data.sessionID, (draft) => {
|
||||
const current = message.compaction(draft)
|
||||
if (current?.status === "running") current.summary += event.data.text
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./data"
|
||||
export * from "./connection"
|
||||
export * from "./pty"
|
||||
export * from "./retention"
|
||||
export * from "./usage"
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { createEffect, untrack, type Accessor } from "solid-js"
|
||||
import type { Data } from "./data"
|
||||
|
||||
export function createSessionRetention(input: {
|
||||
session: Pick<Data["session"], "list" | "root" | "evict"> & {
|
||||
message?: Pick<Data["session"]["message"], "list">
|
||||
}
|
||||
current: Accessor<string | undefined>
|
||||
keep: Accessor<readonly string[]>
|
||||
limit: number
|
||||
}) {
|
||||
let previous: string | undefined
|
||||
let recent: string[] = []
|
||||
let evicted = new Map<string, string>()
|
||||
|
||||
createEffect(() => {
|
||||
const viewed = input.current()
|
||||
const current = viewed === undefined ? undefined : input.session.root(viewed)
|
||||
const keep = new Set(input.keep().map((id) => input.session.root(id)))
|
||||
// Resolve again when metadata arrives, but only navigation advances recency.
|
||||
recent = [
|
||||
...new Set([
|
||||
...(viewed !== previous && current !== undefined ? [current] : []),
|
||||
...recent.map((id) => input.session.root(id)),
|
||||
]),
|
||||
]
|
||||
previous = viewed
|
||||
const retained = new Set([...keep, ...recent.filter((id) => !keep.has(id)).slice(0, input.limit)])
|
||||
if (current !== undefined) retained.add(current)
|
||||
recent = recent.filter((id) => retained.has(id))
|
||||
|
||||
const count = (id: string) => untrack(() => input.session.message?.list(id).length ?? 0)
|
||||
const excluded = new Map(
|
||||
input.session.list().flatMap((session) => {
|
||||
const root = input.session.root(session.id)
|
||||
return retained.has(root) ? [] : [[session.id, root] as const]
|
||||
}),
|
||||
)
|
||||
// New descendants and non-retained sessions repopulated by background turns need eviction even when their root was already evicted.
|
||||
const roots = new Set(
|
||||
[...excluded].filter(([id, root]) => evicted.get(id) !== `${root}:${count(id)}`).map(([, root]) => root),
|
||||
)
|
||||
untrack(() => roots.forEach((root) => input.session.evict(root)))
|
||||
evicted = new Map([...excluded].map(([id, root]) => [id, `${root}:${count(id)}`]))
|
||||
})
|
||||
}
|
||||
@@ -1,41 +1 @@
|
||||
import { createEffect, untrack, type Accessor } from "solid-js"
|
||||
import type { Data } from "@opencode/client/solid"
|
||||
|
||||
export function createSessionRetention(input: {
|
||||
session: Pick<Data["session"], "list" | "root" | "evict">
|
||||
current: Accessor<string | undefined>
|
||||
keep: Accessor<readonly string[]>
|
||||
limit: number
|
||||
}) {
|
||||
let previous: string | undefined
|
||||
let recent: string[] = []
|
||||
let evicted = new Map<string, string>()
|
||||
|
||||
createEffect(() => {
|
||||
const viewed = input.current()
|
||||
const current = viewed === undefined ? undefined : input.session.root(viewed)
|
||||
const keep = new Set(input.keep().map((id) => input.session.root(id)))
|
||||
// Resolve again when metadata arrives, but only navigation advances recency.
|
||||
recent = [
|
||||
...new Set([
|
||||
...(viewed !== previous && current !== undefined ? [current] : []),
|
||||
...recent.map((id) => input.session.root(id)),
|
||||
]),
|
||||
]
|
||||
previous = viewed
|
||||
const retained = new Set([...keep, ...recent.filter((id) => !keep.has(id)).slice(0, input.limit)])
|
||||
if (current !== undefined) retained.add(current)
|
||||
recent = recent.filter((id) => retained.has(id))
|
||||
|
||||
const excluded = new Map(
|
||||
input.session.list().flatMap((session) => {
|
||||
const root = input.session.root(session.id)
|
||||
return retained.has(root) ? [] : [[session.id, root] as const]
|
||||
}),
|
||||
)
|
||||
// New descendants need eviction even when their root was already evicted.
|
||||
const roots = new Set([...excluded].filter(([id, root]) => evicted.get(id) !== root).map(([, root]) => root))
|
||||
evicted = excluded
|
||||
untrack(() => roots.forEach((root) => input.session.evict(root)))
|
||||
})
|
||||
}
|
||||
export { createSessionRetention } from "@opencode/client/solid"
|
||||
|
||||
@@ -203,3 +203,33 @@ test("uses the caller's recent-family limit", async () => {
|
||||
scope.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
test("re-evicts an already excluded family when background turn events repopulate its transcript", async () => {
|
||||
const scope = setup({ current: "current" })
|
||||
try {
|
||||
scope.remember("current")
|
||||
scope.remember("background")
|
||||
expect(scope.evictions).toEqual(["background"])
|
||||
|
||||
scope.events.emit("session.step.started", {
|
||||
id: "evt_step_1",
|
||||
created: 1,
|
||||
type: "session.step.started",
|
||||
durable: { aggregateID: "background", seq: 1, version: 1 },
|
||||
data: {
|
||||
sessionID: "background",
|
||||
assistantMessageID: "msg_bg_assistant",
|
||||
agent: "build",
|
||||
model: { providerID: "openai", id: "gpt-5" },
|
||||
started: 1,
|
||||
},
|
||||
})
|
||||
expect(scope.cached("background")).toBe(true)
|
||||
|
||||
scope.remember("background", undefined, 10)
|
||||
expect(scope.cached("background")).toBe(false)
|
||||
expect(scope.evictions).toEqual(["background", "background"])
|
||||
} finally {
|
||||
scope.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user