Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class StructuredAgentSessionClientDelivery {
private readonly turnCompletionFeed
readonly requestFailures
private readonly sendSettlement
private readonly journalObservers = new Map<string, Set<() => void>>()

constructor(
private readonly sessions: Map<string, StructuredAgentSessionHostSession>,
Expand Down Expand Up @@ -201,6 +202,19 @@ export class StructuredAgentSessionClientDelivery {
this.turnCompletionFeed.observe(sessionId, undefined, { historical: true })
}

/** Observe every journal publication, including row-free wakes, without opening a client stream. */
observeJournalPublications = (sessionId: string, observer: () => void): (() => void) => {
const observers = this.journalObservers.get(sessionId) ?? new Set<() => void>()
observers.add(observer)
this.journalObservers.set(sessionId, observers)
return () => {
observers.delete(observer)
if (observers.size === 0 && this.journalObservers.get(sessionId) === observers) {
this.journalObservers.delete(sessionId)
}
}
}

subscribeStatus = (subscriber: StructuredAgentSessionStatusSubscriber): (() => void) =>
this.statusFeed.subscribe(subscriber)
forgetStatus = (sessionId: string): void => this.statusFeed.forget(sessionId)
Expand Down Expand Up @@ -229,6 +243,7 @@ export class StructuredAgentSessionClientDelivery {
this.requestFailures.clear()
this.stopAtRestCommandUpdates()
this.sendSettlement.closeAll()
this.journalObservers.clear()
}

private publishJournal(sessionId: string, journal: AgentSessionJournal, activity = true): void {
Expand All @@ -237,6 +252,17 @@ export class StructuredAgentSessionClientDelivery {
// Observe completion after status projection updates, even without subscribers for background chats.
this.turnCompletionFeed.observe(sessionId, journal)
this.queue.onJournalActivity(sessionId, activity)
for (const observer of this.journalObservers.get(sessionId) ?? []) {
try {
observer()
} catch (error) {
this.deps().logger.warn('observing a journal publication failed', {
scope: 'journal-delivery',
sessionId,
error
})
}
}
}

private currentWork(sessionId: string, journal: AgentSessionJournal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ export class StructuredAgentSessionHost {

reconcileRestartLeases = (): Promise<void> => this.restore.reconcileRestartLeases()

restoreReadableSessions = (sessionIds?: readonly string[]): Promise<void> =>
this.restore.restoreReadableSessions(sessionIds)
restoreReadableSessions = (ids?: readonly string[]) => this.restore.restoreReadableSessions(ids)

revealSession = (sessionId: string): Promise<HostTypes.StructuredAgentSessionReveal> =>
reveal.revealStructuredAgentSession(this.deps, sessionId, this.lifetime.conversation)
Expand Down Expand Up @@ -326,6 +325,8 @@ export class StructuredAgentSessionHost {
readCurrentRequests: StructuredAgentSessionBackgroundTaskChannel['currentRequests'] = (...args) =>
this.backgroundTasks.currentRequests(...args)

observeJournalPublications = this.clientDelivery.observeJournalPublications

subscribe = (input: AgentSessionSubscribeInput) => this.backgroundTasks.subscribe(input)

settleLateDispatch = this.mutations.settleLateDispatch
Expand Down
Loading
Loading