Files
deepseek-harness/packages/session-query/session-query/src/observation.ts
T

396 lines
16 KiB
TypeScript

/** Shared live/prepared observations for Session page and lifecycle consumers. */
import type { Context } from '@deepseek-ai/cordis'
import { SessionLogOffset, SessionSeq } from '@deepseek-ai/dsh-session'
import type { Session, SessionEvent, SessionHeader, SessionId , SessionLogOffset as SessionLogOffsetType , SessionSeqCursor } from '@deepseek-ai/dsh-session'
import type SessionPersistence from '@deepseek-ai/dsh-session-persistence'
import type {
SessionPersistenceRevision,
SessionPersistenceSnapshot,
} from '@deepseek-ai/dsh-session-persistence'
import type { ProjectionSnapshot } from '@deepseek-ai/dsh-session-projection'
import type {} from '@deepseek-ai/dsh-session-projection-cache'
import { SESSION_QUERY_DEFAULT_PREPARED_SESSION_CACHE_SIZE, SessionQueryError } from './config.ts'
import { readColdSessionLog, type ColdSessionLog } from './cold-read.ts'
/** One exact immutable Session cut retained for the caller's read lifetime. */
export interface SessionObservation extends Disposable {
/** Whether the cut came from an attached Session or a retained preparation. */
readonly source: 'live' | 'prepared'
/** Immutable Session identity metadata. */
readonly header: SessionHeader
/** Exact fork-inherited event count paired with {@link header}. */
readonly inheritedEventCount: SessionLogOffsetType
/**
* Immutable contiguous events at {@link cursor}. A live observation
* materializes this array on first read, so a consumer that reads only the
* header, cursor, or projections never copies the log.
*/
readonly events: readonly SessionEvent[]
/** Last observed event seq, or -1 for an empty log. */
readonly cursor: SessionSeqCursor
/** Durable source revision for a cold prepared observation. */
readonly revision?: SessionPersistenceRevision
/** Exact projection baseline at {@link cursor}, when the registry is mounted. */
readonly projections?: ProjectionSnapshot
/**
* Retain the same immutable cut for another Host owner.
* @returns an independently disposable lease over this observation.
*/
retain(): SessionObservation
}
/** Projection work and cancellation requested for one exact observation. */
export interface SessionObservationOptions {
/** Optional cancellation while resolving a cold source. */
readonly signal?: AbortSignal
/** Whether to compute every projection or leave projection state untouched. */
readonly projectionMode?: 'all' | 'none'
}
/**
* One reusable cold observation: an unpublished restored Session plus the
* exact balanced log it represents, valid while the producing persistence
* instance still reports the same revision.
*/
interface PreparedEntry {
/** Stable service identity whose `stat` produced this revision; proxy references are not instance identities. */
readonly persistenceIdentity: symbol
/** Durable revision observed by `stat` immediately before the log read. */
readonly revision: SessionPersistenceRevision
/** Unpublished Session restored from the balanced log; never entered into the store. */
readonly session: Session
/** Immutable balanced log (stored events plus in-memory interrupted-turn closers). */
readonly events: readonly SessionEvent[]
/** Active observation leases; a pinned entry (`refs > 0`) is never evicted. */
refs: number
}
/**
* Builds point observations without a corpus listing preflight.
*
* Cold reads are cached per session id, keyed by the persistence instance and
* the `stat` revision observed before the log read: an unchanged revision
* reuses the restored Session without re-reading the log. The cache is bounded
* (least-recently-used unpinned entries are evicted past the capacity), and
* entries pinned by active leases survive eviction and replacement — a lease's
* cut stays valid for the lease lifetime even after a newer revision lands.
*/
export class SessionObservationReader {
private readonly cache = new Map<SessionId, PreparedEntry>()
/**
* @param ctx - context carrying Session and optional persistence/projection services.
* @param cacheCapacity - maximum unpinned cold observations retained for reuse.
*/
constructor(
private readonly ctx: Context,
private readonly cacheCapacity: number = SESSION_QUERY_DEFAULT_PREPARED_SESSION_CACHE_SIZE,
) {}
/**
* Read domain-owned state from a live or prepared Session without publishing it.
* The callback runs synchronously at a fresh cut and must not mutate or retain the Session.
* @param sessionId - logical Session identity.
* @param read - synchronous domain getter, typically an owned projection or policy resolver.
* @param signal - cancellation of preparation.
* @returns the getter's detached value.
*/
async inspect<T>(sessionId: SessionId, read: (session: Session) => T, signal?: AbortSignal): Promise<T> {
const { observation, session } = await this.acquire(sessionId, { projectionMode: 'none', ...signal === undefined ? {} : { signal } })
try {
signal?.throwIfAborted()
return read(session)
} finally {
observation[Symbol.dispose]()
}
}
/**
* Observe one live-preferred Session and retain a cold preparation until disposal.
* @param sessionId - logical Session identity.
* @param options - cancellation and all-or-none projection computation for this read.
* @returns one exact immutable observation.
* @throws {@link SessionQueryError} with code `SESSION_QUERY_CORRUPT_SESSION` when live or prepared projection computation fails.
*/
async read(
sessionId: SessionId,
options: SessionObservationOptions = {},
): Promise<SessionObservation> {
return (await this.acquire(sessionId, options)).observation
}
/** Pair a live or prepared Session with the observation lease that retains it. */
private async acquire(
sessionId: SessionId,
options: SessionObservationOptions,
): Promise<{ observation: SessionObservation; session: Session }> {
const { signal, projectionMode = 'all' } = options
for (;;) {
throwIfObservationAborted(signal)
const live = this.ctx.sessions.get(sessionId)
if (live !== undefined) return { observation: this.live(live, projectionMode), session: live }
const persistence = this.ctx.get('sessionPersistence')
if (persistence === undefined) throw notFound(sessionId)
const snapshot = await this.statSource(persistence, sessionId, signal)
const attachedDuringStat = this.ctx.sessions.get(sessionId)
if (attachedDuringStat !== undefined) {
return { observation: this.live(attachedDuringStat, projectionMode), session: attachedDuringStat }
}
let entry = this.cachedEntry(persistence.identity, sessionId, snapshot.revision)
if (entry === undefined) {
const loaded = await this.loadSource(persistence, sessionId, signal)
throwIfObservationAborted(signal)
const attached = this.ctx.sessions.get(sessionId)
if (attached !== undefined) return { observation: this.live(attached, projectionMode), session: attached }
// The handle marks persisted events as adoptable; synthetic closers
// are owned by this read, so the combined seed needs no copy.
const seed = loaded.events
let session: Session
try {
session = this.ctx.sessions.prepare(sessionId, {
seed,
meta: structuredClone(loaded.header),
inheritedEventCount: loaded.inheritedEventCount,
eventState: loaded.eventState,
})
} catch (error: unknown) {
// The store rejects an id with a live owner: that owner is the
// fresher source, so retry the live path. Any other rejection means
// the stored log failed restore validation.
if (this.ctx.sessions.get(sessionId) !== undefined) continue
throw new SessionQueryError(
`stored session "${sessionId}" is corrupt: ${errorMessage(error)}`,
'SESSION_QUERY_CORRUPT_SESSION',
{ cause: error },
)
}
entry = {
persistenceIdentity: persistence.identity,
revision: snapshot.revision,
session,
events: Object.freeze(seed),
refs: 0,
}
this.store(sessionId, entry)
}
let projections: ProjectionSnapshot | undefined
try {
projections = projectionMode === 'none' ? undefined : this.preparedProjections(entry)
} catch (error: unknown) {
throw new SessionQueryError(
`failed to project session "${sessionId}": ${errorMessage(error)}`,
'SESSION_QUERY_CORRUPT_SESSION',
{ cause: error },
)
}
return { observation: this.preparedLease(sessionId, entry, projections), session: entry.session }
}
}
/** Observe the stored snapshot, mapping absence and backend failures to the query taxonomy. */
private async statSource(
persistence: SessionPersistence,
sessionId: SessionId,
signal: AbortSignal | undefined,
): Promise<SessionPersistenceSnapshot> {
let snapshot: SessionPersistenceSnapshot | undefined
try {
snapshot = await persistence.stat(sessionId, signal === undefined ? undefined : { signal })
} catch (error: unknown) {
throwIfObservationAborted(signal)
throw mapPersistenceFailure(sessionId, error)
}
throwIfObservationAborted(signal)
if (snapshot === undefined) throw notFound(sessionId)
if (snapshot.header.id !== sessionId) {
throw new SessionQueryError(
`session persistence returned "${snapshot.header.id}" for "${sessionId}"`,
'SESSION_QUERY_SOURCE_CONFLICT',
)
}
return snapshot
}
/** Read the complete balanced cold log, mapping backend failures to the query taxonomy. */
private async loadSource(
persistence: SessionPersistence,
sessionId: SessionId,
signal: AbortSignal | undefined,
): Promise<ColdSessionLog> {
try {
return await readColdSessionLog(persistence, sessionId, signal)
} catch (error: unknown) {
throwIfObservationAborted(signal)
throw mapPersistenceFailure(sessionId, error)
}
}
/** Return a still-valid cached entry and mark it most recently used. */
private cachedEntry(
persistenceIdentity: symbol,
sessionId: SessionId,
revision: SessionPersistenceRevision,
): PreparedEntry | undefined {
const cached = this.cache.get(sessionId)
if (cached === undefined || cached.persistenceIdentity !== persistenceIdentity || cached.revision !== revision) {
return undefined
}
this.cache.delete(sessionId)
this.cache.set(sessionId, cached)
return cached
}
/** Insert or replace the entry for one id, then evict past the capacity. */
private store(sessionId: SessionId, entry: PreparedEntry): void {
// Replacing a stale revision only drops the map's reference; live leases
// keep the old entry alive through their own references.
this.cache.delete(sessionId)
this.cache.set(sessionId, entry)
this.evictPastCapacity(entry)
}
/**
* Evict oldest unpinned entries until the cache fits its capacity again.
* Runs on store and whenever a lease release unpins an entry, so leases
* that pinned every candidate cannot leave the cache over budget for good.
* @param keep - the entry being stored, about to be leased; never evicted.
*/
private evictPastCapacity(keep?: PreparedEntry): void {
if (this.cache.size <= this.cacheCapacity) return
for (const [id, candidate] of this.cache) {
if (candidate === keep || candidate.refs > 0) continue
this.cache.delete(id)
if (this.cache.size <= this.cacheCapacity) return
}
}
/** Build one disposable lease over a cached entry, pinning it until every lease releases. */
private preparedLease(
sessionId: SessionId,
entry: PreparedEntry,
projections: ProjectionSnapshot | undefined,
): SessionObservation {
entry.refs += 1
const lease = (): SessionObservation => {
let disposed = false
return {
source: 'prepared',
header: entry.session.header,
inheritedEventCount: entry.session.inheritedEventCount,
events: entry.events,
cursor: entry.events.at(-1)?.seq ?? -1,
revision: entry.revision,
...projections === undefined ? {} : { projections },
retain: () => {
if (disposed) throw new Error(`session observation "${sessionId}" is disposed`)
entry.refs += 1
return lease()
},
[Symbol.dispose]: () => {
if (disposed) return
disposed = true
entry.refs -= 1
if (entry.refs === 0) this.evictPastCapacity()
},
}
}
return lease()
}
private live(
session: Session,
projectionMode: NonNullable<SessionObservationOptions['projectionMode']>,
): SessionObservation {
// The cut is the log length now. The log only appends, so the prefix
// below `seq` is the same array whenever a consumer first reads `events`.
const seq = session.seq
let materialized: readonly SessionEvent[] | undefined
let projections: ProjectionSnapshot | undefined
try {
projections = projectionMode === 'none'
? undefined
: this.ctx.get('sessionProjections')?.snapshot(session)
} catch (error: unknown) {
throw new SessionQueryError(
`failed to project session "${session.id}": ${errorMessage(error)}`,
'SESSION_QUERY_CORRUPT_SESSION',
{ cause: error },
)
}
const lease = (): SessionObservation => {
let disposed = false
return {
source: 'live',
header: session.header,
inheritedEventCount: session.inheritedEventCount,
get events() {
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
materialized ??= session.snapshotEvents(SessionLogOffset(0), seq)
return materialized
},
cursor: seq === 0 ? -1 : SessionSeq(seq - 1),
...projections === undefined ? {} : { projections },
retain: () => {
if (disposed) throw new Error(`session observation "${session.id}" is disposed`)
return lease()
},
[Symbol.dispose]: () => { disposed = true },
}
}
return lease()
}
private preparedProjections(entry: PreparedEntry): ProjectionSnapshot | undefined {
const registry = this.ctx.get('sessionProjections')
if (registry === undefined) return undefined
const cache = this.ctx.get('sessionProjectionCache')
return cache === undefined
? registry.hydrate(entry.session, {}, entry.events, SessionLogOffset(0))
: cache.hydratePrepared(entry.session, entry.events)
}
}
function throwIfObservationAborted(signal: AbortSignal | undefined): void {
if (signal?.aborted !== true) return
throw new SessionQueryError(
'session observation was aborted',
'SESSION_QUERY_ABORTED',
{ cause: signal.reason },
)
}
function mapPersistenceFailure(sessionId: SessionId, error: unknown): SessionQueryError {
if (hasErrorName(error, 'SessionPersistenceNotFoundError')) return notFound(sessionId, error)
if (hasErrorName(error, 'SessionPersistenceCorruptionError')) {
return new SessionQueryError(
`stored session "${sessionId}" is corrupt: ${error.message}`,
'SESSION_QUERY_CORRUPT_SESSION',
{ cause: error },
)
}
return new SessionQueryError(
`failed to observe session "${sessionId}": ${errorMessage(error)}`,
'SESSION_QUERY_PERSISTENCE_FAILED',
{ cause: error },
)
}
function notFound(sessionId: SessionId, cause?: unknown): SessionQueryError {
return new SessionQueryError(
`session "${sessionId}" not found`,
'SESSION_QUERY_SESSION_NOT_FOUND',
cause === undefined ? undefined : { cause },
)
}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : 'unknown error'
}
function hasErrorName(error: unknown, name: string): error is Error {
return error instanceof Error && error.name === name
}