feat(client): retain recent sessions and skip conversation folding while unrendered
This commit is contained in:
1 parent
b1e7c58953
commit
4f9ee56b8a
55 files changed
+1478
-108
No files matched your search
@@ -14,7 +14,7 @@ import type { SessionSearchResultItem } from '../sessions/manager.ts'
|
||||
import type { SessionBinding, SessionListState } from '../sessions/service.ts'
|
||||
import type { SessionFace } from './session.ts'
|
||||
import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
|
||||
import type { SessionReferenceSource } from '../index.ts'
|
||||
import type { SessionReferenceSource, SessionRetainFacet } from '../index.ts'
|
||||
|
||||
export type { AgentContext } from '../scope.ts'
|
||||
|
||||
@@ -32,9 +32,14 @@ export interface SessionReference extends Disposable {
|
||||
release(): void
|
||||
}
|
||||
|
||||
/** Consumer identity and optional cancellation of one acquisition waiter. */
|
||||
/** Consumer identity, retained facets, and optional cancellation of one acquisition waiter. */
|
||||
export interface SessionRetainOptions {
|
||||
readonly source: SessionReferenceSource
|
||||
/**
|
||||
* Facets this reference keeps alive beyond the history stream every reference retains.
|
||||
* A facet is retained only when its key is `true`; the set is fixed for the reference's lifetime.
|
||||
*/
|
||||
readonly facets?: Readonly<Partial<Record<SessionRetainFacet, boolean>>> | undefined
|
||||
readonly signal?: AbortSignal | undefined
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,15 @@ export interface SessionReferenceSourceMap {
|
||||
/** Declaration-merge-extensible labels carried by independent Client references. */
|
||||
export type SessionReferenceSource = Extract<keyof SessionReferenceSourceMap, string>
|
||||
|
||||
/**
|
||||
* Owner-declared parts of a retained Session beyond its history stream, each counted independently.
|
||||
* Owners extend it by augmenting this package's canonical `/client` module specifier; the Controller assigns no meaning to a key.
|
||||
*/
|
||||
export interface SessionRetainFacetMap {}
|
||||
|
||||
/** Declaration-merge-extensible facet names a reference may retain. */
|
||||
export type SessionRetainFacet = Extract<keyof SessionRetainFacetMap, string>
|
||||
|
||||
declare module '@deepseek-ai/cordis' {
|
||||
interface Context {
|
||||
/** Client Session object layer and Agent scope owner. */
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/** Per-generation reference counts of owner-declared retained facets. */
|
||||
import { notifySubscribers, type ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
|
||||
import type { SessionRetainOptions } from '../contract/sessions.ts'
|
||||
import type { SessionRetainFacet } from '../index.ts'
|
||||
|
||||
interface FacetCount {
|
||||
count: number
|
||||
readonly listeners: Set<() => void>
|
||||
readonly source: ObservableSnapshot<boolean>
|
||||
}
|
||||
|
||||
/**
|
||||
* The facet keys a retain request sets to `true`.
|
||||
* @param facets - the request's facet record.
|
||||
* @returns the facets one reference retains for its lifetime.
|
||||
*/
|
||||
export function heldFacets(facets: SessionRetainOptions['facets']): readonly SessionRetainFacet[] {
|
||||
if (facets === undefined) return []
|
||||
return (Object.keys(facets) as SessionRetainFacet[]).filter(facet => facets[facet] === true)
|
||||
}
|
||||
|
||||
/** Facet counts of one live Client generation; observers are notified only when a facet starts or stops being retained. */
|
||||
export class RetainedFacets {
|
||||
private readonly counts = new Map<SessionRetainFacet, FacetCount>()
|
||||
|
||||
/**
|
||||
* Observe one facet of this generation.
|
||||
* @param facet - owner-declared facet name.
|
||||
* @returns the identity-stable source reading whether any live reference retains `facet`.
|
||||
*/
|
||||
source(facet: SessionRetainFacet): ObservableSnapshot<boolean> {
|
||||
return this.entry(facet).source
|
||||
}
|
||||
|
||||
/**
|
||||
* Count one new reference for each of its facets.
|
||||
* @param facets - facets one new reference retains.
|
||||
*/
|
||||
retain(facets: readonly SessionRetainFacet[]): void {
|
||||
for (const facet of facets) this.shift(facet, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Uncount one released reference from each of its facets.
|
||||
* @param facets - facets of one released reference.
|
||||
*/
|
||||
release(facets: readonly SessionRetainFacet[]): void {
|
||||
for (const facet of facets) this.shift(facet, -1)
|
||||
}
|
||||
|
||||
/** End every facet when the generation retires. */
|
||||
releaseAll(): void {
|
||||
for (const entry of this.counts.values()) {
|
||||
if (entry.count === 0) continue
|
||||
entry.count = 0
|
||||
notifySubscribers(entry.listeners, '[session-controller] retained facet')
|
||||
}
|
||||
}
|
||||
|
||||
private shift(facet: SessionRetainFacet, delta: 1 | -1): void {
|
||||
const entry = this.entry(facet)
|
||||
const wasHeld = entry.count > 0
|
||||
entry.count += delta
|
||||
if (wasHeld !== entry.count > 0) notifySubscribers(entry.listeners, '[session-controller] retained facet')
|
||||
}
|
||||
|
||||
private entry(facet: SessionRetainFacet): FacetCount {
|
||||
const existing = this.counts.get(facet)
|
||||
if (existing !== undefined) return existing
|
||||
const listeners = new Set<() => void>()
|
||||
const created: FacetCount = {
|
||||
count: 0,
|
||||
listeners,
|
||||
source: {
|
||||
getSnapshot: () => created.count > 0,
|
||||
subscribe: (listener) => {
|
||||
listeners.add(listener)
|
||||
return () => { listeners.delete(listener) }
|
||||
},
|
||||
},
|
||||
}
|
||||
this.counts.set(facet, created)
|
||||
return created
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,9 @@ import type { SessionFace } from '../contract/session.ts'
|
||||
import type {
|
||||
AgentContext, ISessions, SessionReference, SessionRetainInfo, SessionRetainOptions, SessionTarget,
|
||||
} from '../contract/sessions.ts'
|
||||
import type { SessionReferenceSource } from '../index.ts'
|
||||
import type { SessionReferenceSource, SessionRetainFacet } from '../index.ts'
|
||||
import { createScope, scopeIdentityOf, scopeOf as scopeTagOf } from '../scope.ts'
|
||||
import { heldFacets, RetainedFacets } from './facets.ts'
|
||||
import { SessionManager } from './manager.ts'
|
||||
import type { SessionRemotes } from './remotes.ts'
|
||||
import type { SessionListPhase, SessionSearchResultItem, SessionProjectionSnapshot } from './manager.ts'
|
||||
@@ -109,6 +110,14 @@ export interface SessionBinding {
|
||||
/** Contiguous event window reserved for Conversation assembly. */
|
||||
readonly eventSource: SessionEventSource
|
||||
readonly ctx: AgentContext
|
||||
/**
|
||||
* Observe whether any live reference retains one facet of this generation.
|
||||
* @param facet - owner-declared facet name.
|
||||
* @returns an identity-stable source per binding and facet; it reads false once the generation ends.
|
||||
* Observers run synchronously inside `retain()` and `release()`, before `retainInfo` and list
|
||||
* observers hear the same change, and must not release the Session's last reference from that callback.
|
||||
*/
|
||||
facet(facet: SessionRetainFacet): ObservableSnapshot<boolean>
|
||||
}
|
||||
|
||||
// Scope primitives live in ../scope.ts (the client mirror of host
|
||||
@@ -173,6 +182,7 @@ interface ScopeRecord {
|
||||
binding: SessionBinding
|
||||
session: Session
|
||||
retention: SessionRetainInfo
|
||||
readonly facets: RetainedFacets
|
||||
live: boolean
|
||||
}
|
||||
|
||||
@@ -286,6 +296,7 @@ export class ClientSessions implements ISessions {
|
||||
for (const [, record] of scopes) {
|
||||
record.live = false
|
||||
record.session.unbindScope()
|
||||
record.facets.releaseAll()
|
||||
}
|
||||
const managerDisposal = this.manager.dispose()
|
||||
for (const [id, record] of scopes) {
|
||||
@@ -299,11 +310,11 @@ export class ClientSessions implements ISessions {
|
||||
}
|
||||
|
||||
retain(target: SessionTarget, options: SessionRetainOptions): SessionReference {
|
||||
const { source, signal } = options
|
||||
const { source, signal, facets } = options
|
||||
signal?.throwIfAborted()
|
||||
if (this.closed) throw new Error('Session Controller is disposed')
|
||||
const id = this.manager.resolveTarget(target)
|
||||
const reference = this.retainScope(id, source)
|
||||
const reference = this.retainScope(id, source, heldFacets(facets))
|
||||
try {
|
||||
reference.attachOpening(this.manager.get(id).open(), signal)
|
||||
return reference
|
||||
@@ -517,7 +528,7 @@ export class ClientSessions implements ISessions {
|
||||
*/
|
||||
retainAgentScope(id: SessionId): SessionReference {
|
||||
if (this.closed) throw new Error('Session Controller is disposed')
|
||||
return this.retainScope(id, 'gateway')
|
||||
return this.retainScope(id, 'gateway', [])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -559,15 +570,21 @@ export class ClientSessions implements ISessions {
|
||||
return this.scopes.get(id)?.binding
|
||||
}
|
||||
|
||||
private retainScope(id: SessionId, source: SessionReferenceSource): ClientSessionReference {
|
||||
private retainScope(
|
||||
id: SessionId,
|
||||
source: SessionReferenceSource,
|
||||
facets: readonly SessionRetainFacet[],
|
||||
): ClientSessionReference {
|
||||
const record = this.scopes.get(id) ?? this.materializeScope(id)
|
||||
const previous = record.retention
|
||||
record.retention = Object.freeze({
|
||||
referenceCount: previous.referenceCount + 1,
|
||||
retainedBy: freezeRetainedBy({ ...previous.retainedBy, [source]: (previous.retainedBy[source] ?? 0) + 1 }),
|
||||
})
|
||||
record.facets.retain(facets)
|
||||
const reference = new ClientSessionReference(id, record, () => {
|
||||
if (!record.live) return
|
||||
record.facets.release(facets)
|
||||
const count = record.retention.referenceCount - 1
|
||||
const { [source]: sourceCount = 0, ...otherSources } = record.retention.retainedBy
|
||||
const retainedBy = sourceCount > 1 ? { ...otherSources, [source]: sourceCount - 1 } : otherSources
|
||||
@@ -607,6 +624,7 @@ export class ClientSessions implements ISessions {
|
||||
record.live = false
|
||||
if (this.scopes.get(id) === record) this.scopes.delete(id)
|
||||
record.session.unbindScope()
|
||||
record.facets.releaseAll()
|
||||
const sessionDisposal = this.manager.drop(id, record.session)
|
||||
this.projectList()
|
||||
this.publishRetention(id)
|
||||
@@ -620,13 +638,21 @@ export class ClientSessions implements ISessions {
|
||||
// The Session owns its scoped dispatch point (host Agent.loopCtx mirror);
|
||||
// mint and bind are one step so a live scope record implies a bound actx.
|
||||
session.bindScope(ctx)
|
||||
const binding: SessionBinding = { sessionId: id, session, eventSource: session.eventSource, ctx }
|
||||
const facets = new RetainedFacets()
|
||||
const binding: SessionBinding = {
|
||||
sessionId: id,
|
||||
session,
|
||||
eventSource: session.eventSource,
|
||||
ctx,
|
||||
facet: facet => facets.source(facet),
|
||||
}
|
||||
const record: ScopeRecord = {
|
||||
fiber,
|
||||
ctx,
|
||||
binding,
|
||||
session,
|
||||
retention: EMPTY_RETAIN_INFO,
|
||||
facets,
|
||||
live: true,
|
||||
}
|
||||
this.scopes.set(id, record)
|
||||
|
||||
Reference in new issue
Block a user