refactor(session-controller): report facet counts through retainInfo instead of SessionBinding
SessionBinding stays unchanged; SessionRetainInfo gains optional retainedFacets counts beside retainedBy, and owners derive their facet from retainInfo and the current binding. Adds the upgrade guide for Client plugins that render a Session they retain, and updates two web e2e expectations: a recently viewed Session now keeps its right Sidebar view state when the main view returns to it.
This commit is contained in:
1 parent
bad6605c93
commit
bf58ff25f4
25 files changed
+218
-196
No files matched your search
@@ -48,6 +48,11 @@ export interface SessionRetainInfo {
|
||||
readonly referenceCount: number
|
||||
/** Positive source counts only; a source without references is absent. */
|
||||
readonly retainedBy: Readonly<Partial<Record<SessionReferenceSource, number>>>
|
||||
/**
|
||||
* Positive counts of references retaining each facet; absent while no reference retains any facet.
|
||||
* Owners read their facet here, for example `(info.retainedFacets?.conversation ?? 0) > 0`.
|
||||
*/
|
||||
readonly retainedFacets?: Readonly<Partial<Record<SessionRetainFacet, number>>>
|
||||
}
|
||||
|
||||
/** The sessions-service face injected as `ctx.sessions`. */
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
/** 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 { SessionRetainInfo, SessionRetainOptions } from '../contract/sessions.ts'
|
||||
import type { SessionRetainFacet } from '../index.ts'
|
||||
|
||||
interface FacetCount {
|
||||
count: number
|
||||
readonly listeners: Set<() => void>
|
||||
readonly source: ObservableSnapshot<boolean>
|
||||
}
|
||||
type FacetCounts = NonNullable<SessionRetainInfo['retainedFacets']>
|
||||
|
||||
/**
|
||||
* The facet keys a retain request sets to `true`.
|
||||
@@ -19,67 +14,25 @@ export function heldFacets(facets: SessionRetainOptions['facets']): readonly Ses
|
||||
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
|
||||
}
|
||||
/**
|
||||
* Count one reference's facets in or out of a retain-info snapshot.
|
||||
* @param counts - the previous positive facet counts, absent when none.
|
||||
* @param facets - the facets of the retained or released reference.
|
||||
* @param delta - `1` when the reference is retained, `-1` when it is released.
|
||||
* @returns the frozen positive counts, or undefined when no facet remains retained.
|
||||
*/
|
||||
export function shiftFacetCounts(
|
||||
counts: FacetCounts | undefined,
|
||||
facets: readonly SessionRetainFacet[],
|
||||
delta: 1 | -1,
|
||||
): FacetCounts | undefined {
|
||||
if (facets.length === 0) return counts
|
||||
// The facet key type is empty in programs where no owner has declared a facet, so counts are built over strings.
|
||||
const next = new Map<string, number>(Object.entries(counts ?? {}))
|
||||
for (const facet of facets) next.set(facet, (next.get(facet) ?? 0) + delta)
|
||||
const positive = [...next].filter(([, count]) => count > 0)
|
||||
if (positive.length === 0) return undefined
|
||||
// Facet names are dictionary keys, including names also present on Object.prototype.
|
||||
const record: FacetCounts = Object.assign(Object.create(null) as object, Object.fromEntries(positive))
|
||||
return Object.freeze(record)
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import type {
|
||||
} from '../contract/sessions.ts'
|
||||
import type { SessionReferenceSource, SessionRetainFacet } from '../index.ts'
|
||||
import { createScope, scopeIdentityOf, scopeOf as scopeTagOf } from '../scope.ts'
|
||||
import { heldFacets, RetainedFacets } from './facets.ts'
|
||||
import { heldFacets, shiftFacetCounts } from './facets.ts'
|
||||
import { SessionManager } from './manager.ts'
|
||||
import type { SessionRemotes } from './remotes.ts'
|
||||
import type { SessionListPhase, SessionSearchResultItem, SessionProjectionSnapshot } from './manager.ts'
|
||||
@@ -110,14 +110,6 @@ 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
|
||||
@@ -174,6 +166,14 @@ function freezeRetainedBy(counts: Partial<Record<SessionReferenceSource, number>
|
||||
return Object.freeze(counts)
|
||||
}
|
||||
|
||||
function retentionOf(
|
||||
referenceCount: number,
|
||||
retainedBy: SessionRetainInfo['retainedBy'],
|
||||
retainedFacets: SessionRetainInfo['retainedFacets'],
|
||||
): SessionRetainInfo {
|
||||
return Object.freeze({ referenceCount, retainedBy, ...(retainedFacets === undefined ? {} : { retainedFacets }) })
|
||||
}
|
||||
|
||||
const EMPTY_RETAIN_INFO: SessionRetainInfo = Object.freeze({ referenceCount: 0, retainedBy: freezeRetainedBy({}) })
|
||||
|
||||
interface ScopeRecord {
|
||||
@@ -182,7 +182,6 @@ interface ScopeRecord {
|
||||
binding: SessionBinding
|
||||
session: Session
|
||||
retention: SessionRetainInfo
|
||||
readonly facets: RetainedFacets
|
||||
live: boolean
|
||||
}
|
||||
|
||||
@@ -296,7 +295,6 @@ 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) {
|
||||
@@ -577,20 +575,19 @@ export class ClientSessions implements ISessions {
|
||||
): 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)
|
||||
record.retention = retentionOf(
|
||||
previous.referenceCount + 1,
|
||||
freezeRetainedBy({ ...previous.retainedBy, [source]: (previous.retainedBy[source] ?? 0) + 1 }),
|
||||
shiftFacetCounts(previous.retainedFacets, facets, 1),
|
||||
)
|
||||
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
|
||||
record.retention = count === 0
|
||||
? EMPTY_RETAIN_INFO
|
||||
: Object.freeze({ referenceCount: count, retainedBy: freezeRetainedBy(retainedBy) })
|
||||
: retentionOf(count, freezeRetainedBy(retainedBy), shiftFacetCounts(record.retention.retainedFacets, facets, -1))
|
||||
if (count === 0) this.retireScope(id, record)
|
||||
else this.publishRetention(id)
|
||||
})
|
||||
@@ -624,7 +621,6 @@ 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)
|
||||
@@ -638,21 +634,13 @@ 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 facets = new RetainedFacets()
|
||||
const binding: SessionBinding = {
|
||||
sessionId: id,
|
||||
session,
|
||||
eventSource: session.eventSource,
|
||||
ctx,
|
||||
facet: facet => facets.source(facet),
|
||||
}
|
||||
const binding: SessionBinding = { sessionId: id, session, eventSource: session.eventSource, ctx }
|
||||
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