perf(ui): index and batch session updates

Batch global session, status, ordering, and activity timing mutations at the existing directory event boundary so large subagent bursts publish each owner once.

Maintain active session roots, children, and directory buckets in the global store, reuse them in Sidebar projections, and avoid rebuilding live aggregates and structural data for unrelated renders while preserving authoritative ordering reconciliation.
This commit is contained in:
c_w_xiaohei
2026-08-26 00:42:37 +08:00
parent df924aa532
commit 701173e399
16 changed files with 1153 additions and 376 deletions
@@ -4,6 +4,7 @@ import type { Event, Session } from "@opencode-ai/sdk/v2/client"
let currentSessions: Session[] = []
const upsertedSessions: Session[] = []
const removedSessionIds: string[] = []
let mutationCalls = 0
let runtimeKey = "runtime-a"
let runtimeWillChange: (() => void) | null = null
@@ -15,6 +16,7 @@ mock.module("@/stores/useGlobalSessionsStore", () => ({
getState: () => ({
activeSessions: currentSessions,
archivedSessions: [] as Session[],
entityById: new Map(currentSessions.map((session) => [session.id, session])),
upsertSession: (session: Session) => {
upsertedSessions.push(session)
},
@@ -24,6 +26,15 @@ mock.module("@/stores/useGlobalSessionsStore", () => ({
removeSessions: (ids: string[]) => {
removedSessionIds.push(...ids)
},
applySessionMutations: (mutations: Array<
{ type: "upsert"; session: Session } | { type: "remove"; sessionId: string }
>) => {
mutationCalls += 1
for (const mutation of mutations) {
if (mutation.type === "upsert") upsertedSessions.push(mutation.session)
else removedSessionIds.push(mutation.sessionId)
}
},
}),
},
}))
@@ -34,7 +45,7 @@ mock.module("@/lib/runtime-switch", () => ({
return () => undefined
},
}))
import { applySessionEventToGlobalSessions } from "../session-event-router"
import { applySessionEventsToGlobalSessions, applySessionEventToGlobalSessions } from "../session-event-router"
const buildSession = (title: string, time: Session["time"]): Session => ({
id: "ses_1",
@@ -66,6 +77,7 @@ describe("applySessionEventToGlobalSessions", () => {
currentSessions = []
upsertedSessions.length = 0
removedSessionIds.length = 0
mutationCalls = 0
})
test("skips stale global session.updated echoes after a newer rename", () => {
@@ -116,4 +128,22 @@ describe("applySessionEventToGlobalSessions", () => {
expect(upsertedSessions).toEqual([])
})
test("commits an ordered event batch once", () => {
const events = Array.from({ length: 1_000 }, (_, index) => ({
type: "session.created",
properties: {
info: {
id: `ses_${index}`,
title: `Session ${index}`,
time: { created: index, updated: index },
},
},
} as Event))
applySessionEventsToGlobalSessions(events)
expect(mutationCalls).toBe(1)
expect(upsertedSessions).toHaveLength(1_000)
})
})