perf(api): time-slice Session list work
This commit is contained in:
1 parent
ecc01b54a4
commit
4402fa47e4
14 files changed
+317
-144
No files matched your search
@@ -79,8 +79,13 @@ declare module '@deepseek-ai/cordis' {
|
||||
export interface Config {
|
||||
/** Override platform desktop-opener detection. */
|
||||
readonly nativeOpen?: boolean
|
||||
/** Positive integral milliseconds of list work before yielding between complete rows. */
|
||||
readonly listWorkSliceMs?: number
|
||||
}
|
||||
|
||||
/** Deployment policy after schema defaults have been applied. */
|
||||
type ResolvedConfig = Config & { readonly listWorkSliceMs: number }
|
||||
|
||||
/** Host integrations replaceable by direct unit tests. */
|
||||
export interface SessionControllerInternals {
|
||||
/** Native default-application handoff. */
|
||||
@@ -111,8 +116,9 @@ export class SessionController extends TypertRemoteService {
|
||||
'workspaceRegistry',
|
||||
]
|
||||
|
||||
static Config: z<Config> = z.object({
|
||||
static Config: z<Config, ResolvedConfig> = z.object({
|
||||
nativeOpen: z.boolean(),
|
||||
listWorkSliceMs: z.natural().min(1).default(16),
|
||||
})
|
||||
|
||||
private readonly agents: ApiSessionAgentController
|
||||
@@ -129,11 +135,12 @@ export class SessionController extends TypertRemoteService {
|
||||
|
||||
/**
|
||||
* @param ctx - Host context containing the Session capability assembly.
|
||||
* @param config - native-opener deployment policy.
|
||||
* @param config - native-opener and list-scheduling deployment policy.
|
||||
* @param internals - host integrations replaceable by direct unit tests.
|
||||
*/
|
||||
constructor(ctx: Context, config: Config, internals: SessionControllerInternals = {}) {
|
||||
super(ctx, 'sessionController', { namespace: 'session' })
|
||||
const resolved = SessionController.Config(config)
|
||||
installModelSelectionProjection(ctx)
|
||||
this.agents = new ApiSessionAgentController(ctx)
|
||||
this.commands = new SessionCommandController(ctx, this.agents, process.cwd())
|
||||
@@ -149,7 +156,7 @@ export class SessionController extends TypertRemoteService {
|
||||
await Promise.allSettled([...this.promotions])
|
||||
}, 'session-controller.promotions')
|
||||
this.history = new SessionHistoryController(ctx, (observation) => { this.promote(observation) })
|
||||
this.listState = new ApiSessionList(ctx)
|
||||
this.listState = new ApiSessionList(ctx, resolved.listWorkSliceMs)
|
||||
this.fileApplications = internals.fileApplications ?? nativeFileApplications
|
||||
this.openFileApplication = internals.openFileApplication ?? openNativeFileApplication
|
||||
this.openPath = internals.openPath ?? openNativeAssociatedPath
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/** Cold-safe Session list and search projection. */
|
||||
|
||||
import { performance } from 'node:perf_hooks'
|
||||
import { scheduler } from 'node:timers/promises'
|
||||
import type { Context } from '@deepseek-ai/cordis'
|
||||
import type {} from '@deepseek-ai/dsh-agent-preset-registry'
|
||||
@@ -75,8 +76,11 @@ export function truncateUnicodeCodePoints(value: string, maximum: number): strin
|
||||
|
||||
/** Owns list projection registration, bounded cold summaries, and authorized search. */
|
||||
export class ApiSessionList {
|
||||
/** @param ctx - Host context carrying Session, query, persistence, and projection services. */
|
||||
constructor(private readonly ctx: Context) {
|
||||
/**
|
||||
* @param ctx - Host context carrying Session, query, persistence, and projection services.
|
||||
* @param workSliceMs - Resolved positive integral list-work budget in milliseconds.
|
||||
*/
|
||||
constructor(private readonly ctx: Context, private readonly workSliceMs: number) {
|
||||
ctx.sessionProjections.register<'sessionListMetadata', SessionListMetadata>({
|
||||
key: 'sessionListMetadata',
|
||||
stateSchema: sessionListMetadataSchema,
|
||||
@@ -130,22 +134,31 @@ export class ApiSessionList {
|
||||
signal?.throwIfAborted()
|
||||
const items: SessionSummary[] = []
|
||||
const cold: SessionHeader[] = []
|
||||
let yieldDeadline = performance.now() + this.workSliceMs
|
||||
for (const record of records) {
|
||||
signal?.throwIfAborted()
|
||||
const live = this.ctx.sessions.get(record.header.id)
|
||||
if (live !== undefined) {
|
||||
items.push(this.summaryFor(live))
|
||||
} else if (record.header.cwd !== undefined) {
|
||||
cold.push(record.header)
|
||||
}
|
||||
if (performance.now() >= yieldDeadline) {
|
||||
await scheduler.yield()
|
||||
signal?.throwIfAborted()
|
||||
continue
|
||||
yieldDeadline = performance.now() + this.workSliceMs
|
||||
}
|
||||
if (record.header.cwd === undefined) continue
|
||||
cold.push(record.header)
|
||||
}
|
||||
for (const header of cold) {
|
||||
items.push(this.summarizeCold(header))
|
||||
await scheduler.yield()
|
||||
signal?.throwIfAborted()
|
||||
items.push(this.summarizeCold(header))
|
||||
if (performance.now() >= yieldDeadline) {
|
||||
await scheduler.yield()
|
||||
signal?.throwIfAborted()
|
||||
yieldDeadline = performance.now() + this.workSliceMs
|
||||
}
|
||||
}
|
||||
signal?.throwIfAborted()
|
||||
items.sort((left, right) => right.updatedAt - left.updatedAt)
|
||||
return items
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user