mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
Coalesce pending publications across provider dispatches
This commit is contained in:
+25
-8
@@ -22,11 +22,30 @@ export type Admitted = StructuredAgentSessionSinkOperation & { superseded?: bool
|
||||
/** Capture a complete synchronous source unit; ready-batch caps never divide its dependencies. */
|
||||
export class StructuredAgentSessionDispatch {
|
||||
private pending: Admitted[] = []
|
||||
private readonly waitingPublications = new Map<string, Admitted>()
|
||||
constructor(
|
||||
private readonly flush: () => void,
|
||||
private readonly canPublish: () => boolean
|
||||
) {}
|
||||
|
||||
coalesce(operation: Admitted): void {
|
||||
const key = operation.publicationKey
|
||||
if (key !== undefined) {
|
||||
const prior = this.waitingPublications.get(key)
|
||||
if (prior) {
|
||||
prior.superseded = true
|
||||
}
|
||||
this.waitingPublications.set(key, operation)
|
||||
}
|
||||
}
|
||||
|
||||
forget(operation: Admitted): void {
|
||||
const key = operation.publicationKey
|
||||
if (key !== undefined && this.waitingPublications.get(key) === operation) {
|
||||
this.waitingPublications.delete(key)
|
||||
}
|
||||
}
|
||||
|
||||
push(operation: Admitted): void {
|
||||
if (this.pending.length === 0) {
|
||||
const pending = this.pending
|
||||
@@ -36,13 +55,6 @@ export class StructuredAgentSessionDispatch {
|
||||
}
|
||||
})
|
||||
}
|
||||
const key = operation.publicationKey
|
||||
if (key !== undefined) {
|
||||
const prior = this.pending.findLast((queued) => queued.publicationKey === key)
|
||||
if (prior) {
|
||||
prior.superseded = true
|
||||
}
|
||||
}
|
||||
this.pending.push(operation)
|
||||
}
|
||||
|
||||
@@ -89,7 +101,12 @@ export class StructuredAgentSessionDispatch {
|
||||
sequence: dispatch.at(-1)!.sequence,
|
||||
bytes: dispatch.reduce((total, operation) => total + operation.bytes, 0),
|
||||
operations: dispatch.reduce((total, operation) => total + (operation.operations ?? 1), 0),
|
||||
run
|
||||
run: () =>
|
||||
run().finally(() => {
|
||||
for (const operation of dispatch) {
|
||||
this.forget(operation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -207,6 +207,7 @@ export class StructuredAgentSessionSinkQueue {
|
||||
if (replaceAt >= 0) {
|
||||
this.buffered.splice(replaceAt, 1)
|
||||
}
|
||||
this.dispatch.coalesce(accepted)
|
||||
this.queuedBytes = queuedBytes
|
||||
this.queuedOperations = queuedOperations
|
||||
if (this.target) {
|
||||
@@ -255,6 +256,7 @@ export class StructuredAgentSessionSinkQueue {
|
||||
}
|
||||
|
||||
private release(operation: Admitted): void {
|
||||
this.dispatch.forget(operation)
|
||||
this.queuedBytes = Math.max(0, this.queuedBytes - operation.bytes)
|
||||
this.queuedOperations = Math.max(0, this.queuedOperations - (operation.operations ?? 1))
|
||||
}
|
||||
|
||||
+6
-4
@@ -252,19 +252,21 @@ export function createDeferredStructuredAgentSessionEventSink(deps: {
|
||||
identity,
|
||||
body,
|
||||
options
|
||||
) =>
|
||||
queue.submit({
|
||||
bytes: estimateStructuredAgentSessionItemBytes(identity, body),
|
||||
) => {
|
||||
const reservedBytes = estimateStructuredAgentSessionItemBytes(identity, body)
|
||||
return queue.submit({
|
||||
bytes: reservedBytes,
|
||||
steps: (bound) => [
|
||||
{
|
||||
kind: 'item',
|
||||
identity,
|
||||
body,
|
||||
reservedBytes: estimateStructuredAgentSessionItemBytes(identity, body),
|
||||
reservedBytes,
|
||||
options: structuredAgentSessionJournalAppendOptions(bound.fence, options)
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
const appendTombstone = (
|
||||
identity: AgentJournalItemIdentity
|
||||
|
||||
+39
@@ -1,4 +1,5 @@
|
||||
import { expect, it } from 'vitest'
|
||||
import type { AgentSessionTurnActivity } from '../../../../../shared/agent-session/agent-session-wire'
|
||||
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import type { AgentJournalRenderItem } from '../../../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
@@ -13,6 +14,44 @@ import {
|
||||
|
||||
const options = { turnScope: AGENT_JOURNAL_THREAD_SCOPE }
|
||||
|
||||
it('delivers the latest queued activity without superseded activities while every durable dispatch lands', async () => {
|
||||
const { journal, deferred, sink } = await rig()
|
||||
const received: AgentSessionTurnActivity[] = []
|
||||
deferred.bind({
|
||||
journal,
|
||||
fence: 1,
|
||||
publish: (activity) => {
|
||||
if (activity) {
|
||||
received.push(activity)
|
||||
}
|
||||
}
|
||||
})
|
||||
const entered = Promise.withResolvers<void>()
|
||||
const release = Promise.withResolvers<void>()
|
||||
const held = journal.readInOrder(async () => {
|
||||
entered.resolve()
|
||||
await release.promise
|
||||
})
|
||||
await entered.promise
|
||||
try {
|
||||
for (const text of ['first', 'second', 'latest']) {
|
||||
sink.appendItem(identity(text), tool(text, 'completed'), options)
|
||||
sink.setActivity?.({ turnId: 'turn-1', text })
|
||||
await Promise.resolve()
|
||||
}
|
||||
} finally {
|
||||
release.resolve()
|
||||
}
|
||||
await held
|
||||
await expect(deferred.drained()).resolves.toEqual({ ok: true })
|
||||
expect(received).toEqual([{ turnId: 'turn-1', text: 'latest' }])
|
||||
expect((await readTestJournalItems(journal)).map((item) => item.body)).toEqual([
|
||||
tool('first', 'completed'),
|
||||
tool('second', 'completed'),
|
||||
tool('latest', 'completed')
|
||||
])
|
||||
})
|
||||
|
||||
it.each(['step threshold', 'byte threshold'] as const)(
|
||||
'publishes a terminal turn and trailing output together across the %s despite two publish calls',
|
||||
async (threshold) => {
|
||||
|
||||
Reference in New Issue
Block a user