mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
Expose finite lifecycle evidence through the journal reader
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import type {
|
||||
AgentJournalRenderItem,
|
||||
AgentJournalSubmission
|
||||
} from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end'
|
||||
import type { JournalStopSettleFact } from './journal-writer-host-facts'
|
||||
import type { JournalSqlScope } from './journal-sql-scope'
|
||||
import { readJournalDomainGeneration } from './journal-domain-generation-reads'
|
||||
|
||||
export type JournalGenerationRead = {
|
||||
startKey?: string
|
||||
stopSettle?: JournalStopSettleFact
|
||||
exitScope?: { completedAt: number; ownerFence: number }
|
||||
/** Prior unfinished identities may now be settled and outside the eligible lifecycle index. */
|
||||
itemIds?: readonly string[]
|
||||
}
|
||||
|
||||
export type JournalGenerationResult = {
|
||||
cursor: AgentJournalCursor
|
||||
items: AgentJournalRenderItem[]
|
||||
submissions: AgentJournalSubmission[]
|
||||
itemFences: [string, number | undefined][]
|
||||
stop: JournalLatestStop | null
|
||||
}
|
||||
|
||||
/** Materialize only lifecycle obligations and explicitly named prior observations in one snapshot. */
|
||||
export function readJournalGeneration(
|
||||
scope: JournalSqlScope,
|
||||
input: JournalGenerationRead
|
||||
): JournalGenerationResult {
|
||||
const generation = readJournalDomainGeneration(
|
||||
scope.db,
|
||||
{ sessionId: scope.sessionId, epoch: scope.epoch },
|
||||
input.startKey,
|
||||
input.stopSettle,
|
||||
input.exitScope
|
||||
)
|
||||
const items = new Map(generation.snapshot().items.map((item) => [item.itemId, item]))
|
||||
for (const itemId of input.itemIds ?? []) {
|
||||
const row = scope.db
|
||||
.prepare(
|
||||
`SELECT payload_json FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0 AND item_id=?`
|
||||
)
|
||||
.get(...scope.prefix, itemId)
|
||||
if (row) {
|
||||
const item: AgentJournalRenderItem = JSON.parse(String(row.payload_json))
|
||||
items.set(item.itemId, item)
|
||||
}
|
||||
}
|
||||
return {
|
||||
cursor: generation.cursor(),
|
||||
items: [...items.values()],
|
||||
submissions: generation.submissions(),
|
||||
itemFences: [...items.keys()].map((itemId) => [itemId, generation.itemFence(itemId)]),
|
||||
stop: generation.stopMarks.latest()
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,9 @@
|
||||
import {
|
||||
readJournalGeneration,
|
||||
type JournalGenerationRead,
|
||||
type JournalGenerationResult
|
||||
} from './journal-generation-read'
|
||||
import { readJournalDomainAcceptedSendUnopened } from './journal-domain-accepted-unopened'
|
||||
import { readJournalDomainEntity } from './journal-domain-entity-read'
|
||||
import { readJournalDomainLifecycle } from './journal-sql-lifecycle-reads'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
@@ -29,11 +35,7 @@ import type {
|
||||
JournalStatusIndicators
|
||||
} from './journal-status-indicators'
|
||||
import { readJournalSqlStatusIndicators } from './journal-sql-status-indicators'
|
||||
import {
|
||||
latestAcceptedSendUnopened,
|
||||
type JournalLatestStop
|
||||
} from './context-epochs/journal-stop-turn-end'
|
||||
import { JournalSqlReducer } from './journal-sql-reducer'
|
||||
import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end'
|
||||
import {
|
||||
readJournalSqlQueue,
|
||||
type JournalSqlQueueRead,
|
||||
@@ -48,6 +50,7 @@ import {
|
||||
export type JournalSqlPointRead = {
|
||||
sessionId: string
|
||||
operation:
|
||||
| { kind: 'generation'; input: JournalGenerationRead }
|
||||
| { kind: 'observer'; input: JournalObserverRead }
|
||||
| { kind: 'work'; input: JournalSqlWorkRead }
|
||||
| { kind: 'status-indicators'; input: JournalStatusIndicatorRead }
|
||||
@@ -74,6 +77,7 @@ export type JournalSqlPointRead = {
|
||||
}
|
||||
|
||||
export type JournalSqlPointReadResult =
|
||||
| { kind: 'generation'; generation: JournalGenerationResult }
|
||||
| { kind: 'observer'; observation: JournalObserverResult }
|
||||
| { kind: 'work'; answer: JournalSqlWorkResult }
|
||||
| { kind: 'status-indicators'; indicators: JournalStatusIndicators }
|
||||
@@ -122,6 +126,8 @@ function read(
|
||||
operation: JournalSqlPointRead['operation']
|
||||
): JournalSqlPointReadResult {
|
||||
switch (operation.kind) {
|
||||
case 'generation':
|
||||
return { kind: 'generation', generation: readJournalGeneration(scope, operation.input) }
|
||||
case 'work':
|
||||
return {
|
||||
kind: 'work',
|
||||
@@ -197,7 +203,7 @@ function read(
|
||||
case 'accepted-send-unopened':
|
||||
return {
|
||||
kind: 'accepted-send-unopened',
|
||||
unopened: latestAcceptedSendUnopened(new JournalSqlReducer(scope, scope.epoch).state)
|
||||
unopened: readJournalDomainAcceptedSendUnopened(scope)
|
||||
}
|
||||
case 'queue':
|
||||
return { kind: 'queue', result: readJournalSqlQueue(scope, operation.operation) }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { JournalGenerationRead, JournalGenerationResult } from './journal-generation-read'
|
||||
import type { JournalObserverRead, JournalObserverResult } from './journal-observer-read'
|
||||
import type { JournalSqlWorkRead, JournalSqlWorkResult } from './journal-work-read'
|
||||
import type { AgentSessionJournalReads } from './journal-reads'
|
||||
@@ -12,6 +13,7 @@ import type {
|
||||
} from './journal-input-reads'
|
||||
|
||||
export type JournalSqlReads = AgentSessionJournalReads & {
|
||||
generation(input?: JournalGenerationRead): Promise<JournalGenerationResult>
|
||||
observer(input: JournalObserverRead): Promise<JournalObserverResult>
|
||||
work(input: JournalSqlWorkRead): Promise<JournalSqlWorkResult>
|
||||
/** The saved latest Stop, without the host's settlement overlay. */
|
||||
@@ -61,6 +63,7 @@ export function journalSqlReads(
|
||||
const provenance = async (itemId: string) =>
|
||||
(await read({ kind: 'item-provenance', itemId })).provenance
|
||||
return {
|
||||
generation: async (input = {}) => (await read({ kind: 'generation', input })).generation,
|
||||
work: async (input) => (await read({ kind: 'work', input })).answer,
|
||||
observer: async (input) => (await read({ kind: 'observer', input })).observation,
|
||||
latestStop: async () => (await read({ kind: 'latest-stop' })).stop,
|
||||
|
||||
Reference in New Issue
Block a user