mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 03:45:08 +00:00
refactor: reuse the domain item command for ordinary rows
This commit is contained in:
+21
-12
@@ -9,6 +9,7 @@ import type {
|
||||
} from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import type {
|
||||
AgentJournalMessageItem,
|
||||
AgentJournalItemBody,
|
||||
AgentJournalRenderItem,
|
||||
AgentJournalSubmission,
|
||||
AgentJournalAcceptanceReceipt
|
||||
@@ -37,15 +38,19 @@ import {
|
||||
type JournalSqlAcceptedInputs
|
||||
} from './journal-sql-accepted-inputs'
|
||||
|
||||
export type JournalSqlMessage = {
|
||||
kind: 'item' | 'text-snapshot'
|
||||
type JournalDomainItemFields = {
|
||||
identity: AgentJournalItemIdentity
|
||||
body: AgentJournalMessageItem
|
||||
options: JournalItemAppendOptions
|
||||
scopeItemIds?: readonly string[]
|
||||
}
|
||||
|
||||
export type JournalSqlMessageCommit = {
|
||||
export type JournalDomainItemWrite = JournalDomainItemFields &
|
||||
(
|
||||
| { kind: 'item'; body: AgentJournalItemBody }
|
||||
| { kind: 'text-snapshot'; body: AgentJournalMessageItem }
|
||||
)
|
||||
|
||||
export type JournalDomainItemCommit = {
|
||||
cursor: AgentJournalCursor
|
||||
outcome:
|
||||
| { kind: 'committed'; wrote: boolean }
|
||||
@@ -59,12 +64,12 @@ export type JournalSqlMessageCommit = {
|
||||
}
|
||||
|
||||
/** Runs only on the owning writer connection, inside its command SAVEPOINT. */
|
||||
export function applyJournalSqlMessage(
|
||||
export function applyJournalDomainItem(
|
||||
db: Database.Database,
|
||||
target: { sessionId: string; epoch: string },
|
||||
input: JournalSqlMessage,
|
||||
input: JournalDomainItemWrite,
|
||||
savedAt: number
|
||||
): JournalSqlMessageCommit {
|
||||
): JournalDomainItemCommit {
|
||||
if (!db.isTransaction) {
|
||||
throw new Error('journal message requires its writer transaction')
|
||||
}
|
||||
@@ -79,7 +84,7 @@ export function applyJournalSqlMessage(
|
||||
}
|
||||
assertJournalFence(input.options.fence, Number(head.highest_fence))
|
||||
const cursor = { epoch, sequence: Number(head.last_sequence) }
|
||||
const empty: JournalSqlMessageCommit = {
|
||||
const empty: JournalDomainItemCommit = {
|
||||
cursor,
|
||||
outcome: { kind: 'committed', wrote: false },
|
||||
items: [],
|
||||
@@ -95,7 +100,12 @@ export function applyJournalSqlMessage(
|
||||
let itemId = alias ? String(alias.item_id) : rawId
|
||||
let echo: AgentJournalSubmission | undefined
|
||||
const declaration = Boolean(alias?.echo_declared) || Object.hasOwn(input.options, 'echoOf')
|
||||
if (!alias && !declaration && isProviderUserMessageEcho(rawId, input.body)) {
|
||||
if (
|
||||
!alias &&
|
||||
!declaration &&
|
||||
input.body.kind === 'message' &&
|
||||
isProviderUserMessageEcho(rawId, input.body)
|
||||
) {
|
||||
const fingerprint = agentSessionSendBodyFingerprint(sessionId, input.body)
|
||||
const candidates = db
|
||||
.prepare(`SELECT attempt.payload_json FROM journal_submission_attempts AS attempt
|
||||
@@ -175,8 +185,7 @@ export function applyJournalSqlMessage(
|
||||
}
|
||||
const rendered = journalRenderItem(itemId, revision, row.body, row, scope)
|
||||
const next = previous ? journalItemRevisionValue(previous, rendered) : rendered
|
||||
const spent = spendJournalSqlCommandCards(db, prefix, savedAt)
|
||||
const result: JournalSqlMessageCommit = {
|
||||
const result: JournalDomainItemCommit = {
|
||||
cursor: { epoch, sequence },
|
||||
outcome: { kind: 'committed', wrote: true },
|
||||
items: [next],
|
||||
@@ -230,7 +239,7 @@ export function applyJournalSqlMessage(
|
||||
result.acceptedInputs = readJournalSqlAcceptedInputs(db, prefix)
|
||||
}
|
||||
}
|
||||
Object.assign(result, spent)
|
||||
Object.assign(result, spendJournalSqlCommandCards(db, prefix, savedAt))
|
||||
result.inputChanged ||= result.cards.length > 0
|
||||
if (isJournalProviderActivity(row.recovered, rawId, row.body)) {
|
||||
db.prepare(`INSERT INTO journal_provider_activity(session_id,epoch,fence,last_activity_at) VALUES(?,?,?,?)
|
||||
@@ -1,10 +1,13 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { JournalSqlBatch, JournalSqlBatchResult } from './journal-sql-batch'
|
||||
import {
|
||||
applyJournalSqlMessage,
|
||||
type JournalSqlMessage,
|
||||
type JournalSqlMessageCommit
|
||||
} from './journal-sql-message'
|
||||
applyJournalDomainItem,
|
||||
type JournalDomainItemWrite,
|
||||
type JournalDomainItemCommit
|
||||
} from './journal-domain-item'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
|
||||
export type JournalSqlMessage = JournalDomainItemWrite & { body: AgentJournalMessageItem }
|
||||
|
||||
type MessageCommand = {
|
||||
target: { sessionId: string; epoch: string }
|
||||
@@ -17,8 +20,8 @@ export type JournalSqlMessageJob = {
|
||||
bytes: number
|
||||
}
|
||||
|
||||
export type JournalSqlMessagesCommit = Omit<JournalSqlMessageCommit, 'outcome'> & {
|
||||
outcomes: JournalSqlMessageCommit['outcome'][]
|
||||
export type JournalSqlMessagesCommit = Omit<JournalDomainItemCommit, 'outcome'> & {
|
||||
outcomes: JournalDomainItemCommit['outcome'][]
|
||||
failure?: { message: string }
|
||||
}
|
||||
|
||||
@@ -68,7 +71,7 @@ function applyMessageSteps(
|
||||
for (const step of steps) {
|
||||
db.exec('SAVEPOINT journal_message_step')
|
||||
try {
|
||||
const commit = applyJournalSqlMessage(db, target, step, savedAt)
|
||||
const commit = applyJournalDomainItem(db, target, step, savedAt)
|
||||
db.exec('RELEASE journal_message_step')
|
||||
result.cursor = commit.cursor
|
||||
result.outcomes.push(commit.outcome)
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
prepareJournalSqlMessages,
|
||||
type JournalSqlMessagesCommit
|
||||
} from './journal-sql-message-commit'
|
||||
import type { JournalSqlMessage } from './journal-sql-message'
|
||||
import type { JournalSqlMessage } from './journal-sql-message-commit'
|
||||
|
||||
export const target = { sessionId: 'chat', epoch: 'epoch' }
|
||||
export function message(
|
||||
|
||||
@@ -3,6 +3,7 @@ import type Database from '../../sqlite/sync-database'
|
||||
import { message, messageFixture, target } from './journal-sql-message.test-fixture'
|
||||
import { commitJournalSqlMessages, prepareJournalSqlMessages } from './journal-sql-message-commit'
|
||||
import type { AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { applyJournalDomainItem } from './journal-domain-item'
|
||||
|
||||
const databases: Database.Database[] = []
|
||||
const fixture = () => {
|
||||
@@ -176,21 +177,31 @@ it('accepts an optimistic echo once, preserving its local body and moved placeme
|
||||
})
|
||||
|
||||
it('refuses a snapshot after the saved turn ends and commits only a successful step prefix', async () => {
|
||||
const { db, commit, item, seedItem } = fixture()
|
||||
seedItem({
|
||||
itemId: 'orca:turn',
|
||||
revision: 3,
|
||||
sequence: 3,
|
||||
observedAt: 30,
|
||||
body: { kind: 'turn', turnId: 'turn', state: 'completed', startedAt: 1, completedAt: 2 },
|
||||
turnScope: { kind: 'thread' }
|
||||
})
|
||||
const { db, batch, commit, item } = fixture()
|
||||
const turns = await batch.run(['running', 'completed'] as const, (state) =>
|
||||
applyJournalDomainItem(
|
||||
db,
|
||||
target,
|
||||
{
|
||||
kind: 'item',
|
||||
identity: { provider: 'orca', clientMessageId: 'turn' },
|
||||
body: { kind: 'turn', turnId: 'turn', state, startedAt: 1 },
|
||||
options: { fence: 1, turnScope: { kind: 'thread' } }
|
||||
},
|
||||
30
|
||||
)
|
||||
)
|
||||
expect(turns.outcomes).toMatchObject([
|
||||
{ committed: true, value: { items: [{ revision: 1, body: { state: 'running' } }] } },
|
||||
{ committed: true, value: { items: [{ revision: 2, body: { state: 'completed' } }] } }
|
||||
])
|
||||
expect(item('orca:turn')).toMatchObject({ sequence: 11, revision: 2, observedAt: 30 })
|
||||
const late = message('late')
|
||||
late.options.turnScope = { kind: 'turn', turnItemId: 'orca:turn' }
|
||||
expect((await commit(late)).outcomes[0]).toMatchObject({
|
||||
committed: true,
|
||||
value: {
|
||||
cursor: { epoch: 'epoch', sequence: 10 },
|
||||
cursor: { epoch: 'epoch', sequence: 12 },
|
||||
items: [],
|
||||
outcomes: [{ kind: 'dropped', reason: 'turn-settled' }]
|
||||
}
|
||||
@@ -209,14 +220,14 @@ it('refuses a snapshot after the saved turn ends and commits only a successful s
|
||||
).toMatchObject({
|
||||
committed: true,
|
||||
value: {
|
||||
cursor: { epoch: 'epoch', sequence: 11 },
|
||||
cursor: { epoch: 'epoch', sequence: 13 },
|
||||
items: [{ itemId: 'orca:prefix' }],
|
||||
failure: { message: 'refused' }
|
||||
}
|
||||
})
|
||||
expect(item('orca:refused')).toBeUndefined()
|
||||
expect(item('orca:suffix')).toBeUndefined()
|
||||
expect(db.prepare('SELECT COUNT(*) AS count FROM journal_events').get()!.count).toBe(1)
|
||||
expect(db.prepare('SELECT COUNT(*) AS count FROM journal_events').get()!.count).toBe(3)
|
||||
})
|
||||
|
||||
it('stores one growing text body and a metadata-only replay tail', async () => {
|
||||
|
||||
Reference in New Issue
Block a user