mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
perf(native-chat): reuse history admission facts
This commit is contained in:
@@ -54,9 +54,8 @@ export function readJournalDomainForwardHistory(
|
||||
since?.ok && queuedImages && !useStoredClientForms
|
||||
? admitJournalDomainImageEvents(db, head, since.rows.slice(0, limit), reader, readSnapshot)
|
||||
: undefined
|
||||
const admitted =
|
||||
image?.limit ??
|
||||
(since?.ok
|
||||
const fitted =
|
||||
!image && since?.ok
|
||||
? fittingJournalDomainEventCount(
|
||||
db,
|
||||
head,
|
||||
@@ -64,7 +63,8 @@ export function readJournalDomainForwardHistory(
|
||||
reader.budgetBytes,
|
||||
forms
|
||||
)
|
||||
: limit)
|
||||
: undefined
|
||||
const admitted = image?.limit ?? fitted?.limit ?? limit
|
||||
const count =
|
||||
since?.ok && queuedImages && useStoredClientForms
|
||||
? halvedJournalDomainEventLimit(Math.min(limit, since.rows.length), admitted)
|
||||
@@ -90,7 +90,7 @@ export function readJournalDomainForwardHistory(
|
||||
count,
|
||||
image ? { ...reader, form: (item) => image.forms.get(item) ?? reader.form(item) } : reader,
|
||||
(ids) => {
|
||||
materialized ??= readSnapshot(ids, forms)
|
||||
materialized ??= readSnapshot(ids, forms, fitted?.states, undefined, fitted?.orderedIds)
|
||||
return {
|
||||
...materialized,
|
||||
items: materialized.items.filter((item) => ids.has(item.itemId)),
|
||||
@@ -251,7 +251,8 @@ export function readJournalDomainHistorySnapshot(
|
||||
present?: (send: AgentJournalSubmission) => AgentJournalSubmission,
|
||||
forms?: Map<string, HistoryItemForm>,
|
||||
states?: ReadonlyMap<string, unknown>,
|
||||
ordinals?: Map<string, number>
|
||||
ordinals?: Map<string, number>,
|
||||
orderedIds?: readonly string[]
|
||||
): AgentJournalSnapshot {
|
||||
const wanted = [...ids]
|
||||
return {
|
||||
@@ -261,12 +262,13 @@ export function readJournalDomainHistorySnapshot(
|
||||
...readJournalDomainClientItems(
|
||||
db,
|
||||
head,
|
||||
db
|
||||
.prepare(`SELECT item.item_id FROM json_each(?) wanted
|
||||
orderedIds?.filter((id) => ids.has(id)) ??
|
||||
db
|
||||
.prepare(`SELECT item.item_id FROM json_each(?) wanted
|
||||
CROSS JOIN journal_items item WHERE item.session_id=? AND item.epoch=? AND item.item_id=wanted.value AND item.hidden=0
|
||||
ORDER BY item.sequence,item.sequence_index,item.ordinal`)
|
||||
.all(JSON.stringify(wanted), head.sessionId, head.epoch)
|
||||
.map((row) => String(row.item_id)),
|
||||
.all(JSON.stringify(wanted), head.sessionId, head.epoch)
|
||||
.map((row) => String(row.item_id)),
|
||||
forms,
|
||||
states
|
||||
),
|
||||
|
||||
@@ -41,7 +41,11 @@ export function formJournalDomainHistoryGroups(
|
||||
const states = readJournalDomainStopStates(
|
||||
db,
|
||||
head,
|
||||
ids.filter((id) => !forms.has(id))
|
||||
groups.flatMap((group) =>
|
||||
group
|
||||
.filter((header) => header.bytes === null && !forms.has(header.itemId))
|
||||
.map((header) => header.itemId)
|
||||
)
|
||||
)
|
||||
const bodies = reader.presentSubmission
|
||||
? undefined
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@ export type JournalDomainHistorySnapshotReader = (
|
||||
ids: Set<string>,
|
||||
forms: Map<string, HistoryItemForm>,
|
||||
states?: ReadonlyMap<string, unknown>,
|
||||
ordinals?: Map<string, number>
|
||||
ordinals?: Map<string, number>,
|
||||
orderedIds?: readonly string[]
|
||||
) => AgentJournalSnapshot
|
||||
|
||||
const eventIds = (row: JournalOutcomeReadRow) => [
|
||||
|
||||
@@ -145,7 +145,7 @@ function readHistory(
|
||||
resolveHistoryLimit(request.limit),
|
||||
reader,
|
||||
latestTurnItem(db, head),
|
||||
(ids, forms, states, ordinals) =>
|
||||
(ids, forms, states, ordinals, orderedIds) =>
|
||||
readJournalDomainHistorySnapshot(
|
||||
db,
|
||||
head,
|
||||
@@ -154,7 +154,8 @@ function readHistory(
|
||||
reader.presentSubmission,
|
||||
forms,
|
||||
states,
|
||||
ordinals
|
||||
ordinals,
|
||||
orderedIds
|
||||
),
|
||||
reset,
|
||||
request.reader?.queuedImages,
|
||||
|
||||
@@ -220,7 +220,7 @@ export function fittingJournalDomainEventCount(
|
||||
rows: readonly JournalReadRow[],
|
||||
budgetBytes: number,
|
||||
forms: Map<string, HistoryItemForm>
|
||||
): number {
|
||||
): { limit: number; states: ReadonlyMap<string, unknown>; orderedIds: string[] } {
|
||||
const touched = rows.map((row) =>
|
||||
row.kind === 'outcome'
|
||||
? [...row.itemIds, ...row.submissionIds.map(agentJournalSubmissionKey)]
|
||||
@@ -228,7 +228,7 @@ export function fittingJournalDomainEventCount(
|
||||
)
|
||||
const sizes = new Map(
|
||||
db
|
||||
.prepare(`SELECT item.item_id, ${JOURNAL_DOMAIN_HISTORY_ITEM_BYTES} AS bytes,
|
||||
.prepare(`SELECT item.item_id, item.sequence, item.sequence_index, item.ordinal, ${JOURNAL_DOMAIN_HISTORY_ITEM_BYTES} AS bytes,
|
||||
CASE WHEN item.wire_version=${AGENT_SESSION_ITEM_WIRE_VERSION} THEN 0
|
||||
ELSE ${JOURNAL_DOMAIN_HISTORY_SUBMISSION_BYTES} END AS submission_bytes FROM json_each(?) wanted
|
||||
CROSS JOIN journal_items item
|
||||
@@ -276,5 +276,16 @@ export function fittingJournalDomainEventCount(
|
||||
total += bytes
|
||||
count += 1
|
||||
}
|
||||
return Math.max(count, 1)
|
||||
return {
|
||||
limit: Math.max(count, 1),
|
||||
states,
|
||||
orderedIds: [...sizes]
|
||||
.sort(
|
||||
([, a], [, b]) =>
|
||||
Number(a.sequence) - Number(b.sequence) ||
|
||||
Number(a.sequence_index) - Number(b.sequence_index) ||
|
||||
Number(a.ordinal) - Number(b.ordinal)
|
||||
)
|
||||
.map(([id]) => id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,12 +36,14 @@ export function readJournalDomainClientForms(
|
||||
return forms
|
||||
}
|
||||
const wantedJson = JSON.stringify(wanted)
|
||||
states ??= readJournalDomainStopStates(db, target, wanted, wantedJson)
|
||||
for (const row of db
|
||||
.prepare(`SELECT ${journalDomainClientFormColumns()} FROM json_each(?) wanted
|
||||
CROSS JOIN journal_items item
|
||||
WHERE item.session_id=? AND item.epoch=? AND item.item_id=wanted.value AND item.hidden=0`)
|
||||
.iterate(wantedJson, target.sessionId, target.epoch)) {
|
||||
if (row.wire_version !== AGENT_SESSION_ITEM_WIRE_VERSION && states === undefined) {
|
||||
states = readJournalDomainStopStates(db, target, wanted, wantedJson)
|
||||
}
|
||||
forms.set(String(row.item_id), readJournalDomainClientForm(row, states))
|
||||
}
|
||||
return forms
|
||||
@@ -49,7 +51,7 @@ export function readJournalDomainClientForms(
|
||||
|
||||
export function readJournalDomainClientForm(
|
||||
row: SqliteRow,
|
||||
states: ReadonlyMap<string, unknown>
|
||||
states?: ReadonlyMap<string, unknown>
|
||||
): HistoryItemForm {
|
||||
const item: AgentJournalRenderItem = JSON.parse(String(row.client_json))
|
||||
return row.wire_version === AGENT_SESSION_ITEM_WIRE_VERSION
|
||||
@@ -59,7 +61,7 @@ export function readJournalDomainClientForm(
|
||||
item,
|
||||
row.wire_version,
|
||||
null,
|
||||
journalDomainDisplayTurnState(item, states)
|
||||
states && journalDomainDisplayTurnState(item, states)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user