mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
Merge commit 'eee23ce2c6c' into brennanb2025/native-chat-sqlite-host
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import type { AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { isQueuedAgentJournalSubmission } from '../../../shared/agent-session/requests/agent-session-queued-submission'
|
||||
import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end'
|
||||
import { compareQueuedRows } from './input-order/journal-queued-row-index'
|
||||
import {
|
||||
deriveQueuePauses,
|
||||
journalUserStopInForce,
|
||||
type DerivedQueuePause
|
||||
} from './queued-messages/queued-message-pause'
|
||||
import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
import {
|
||||
readJournalDomainQueueRow,
|
||||
SELECT_JOURNAL_DOMAIN_QUEUE_ROW
|
||||
} from './journal-domain-queue-rows'
|
||||
import { readJournalDomainQueuePauseContext } from './journal-domain-queue-pauses'
|
||||
|
||||
type Target = { sessionId: string; epoch: string }
|
||||
|
||||
function liveCards(db: Database.Database, target: Target): QueuedMessageRow[] {
|
||||
return db
|
||||
.prepare(`${SELECT_JOURNAL_DOMAIN_QUEUE_ROW} WHERE session_id=? AND epoch=?
|
||||
AND state IN ('waiting','returned') ORDER BY position, message_id`)
|
||||
.all(target.sessionId, target.epoch)
|
||||
.map((row) => readJournalDomainQueueRow(target.sessionId, row).card)
|
||||
}
|
||||
|
||||
/** Dispatched cards whose current attempt is still owed delivery, from the finite recovery index. */
|
||||
function owedDispatchedCards(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
owed: (submission: AgentJournalSubmission) => boolean
|
||||
): QueuedMessageRow[] {
|
||||
return db
|
||||
.prepare(`SELECT card.message_id, card.revision, card.state, card.position, card.payload_json,
|
||||
attempt.message_id AS attempt_id, attempt.payload_json AS attempt_json
|
||||
FROM journal_submission_attempts AS attempt INDEXED BY journal_submission_recovery
|
||||
JOIN journal_queue_cards AS card ON card.session_id=attempt.session_id
|
||||
AND card.epoch=attempt.epoch AND card.message_id=attempt.admission_family_id
|
||||
WHERE attempt.session_id=? AND attempt.epoch=? AND attempt.current=1
|
||||
AND (attempt.dispatch_state='pending' OR (attempt.dispatch_state='unknown' AND attempt.recovered=0))
|
||||
AND card.state='dispatched'`)
|
||||
.all(target.sessionId, target.epoch)
|
||||
.flatMap((row) => {
|
||||
const { card } = readJournalDomainQueueRow(target.sessionId, row)
|
||||
const submission: AgentJournalSubmission = JSON.parse(String(row.attempt_json))
|
||||
return card.consumedAs === String(row.attempt_id) && owed(submission) ? [card] : []
|
||||
})
|
||||
}
|
||||
|
||||
/** Membership outlives automatic eligibility until the card's handoff completes. */
|
||||
export function readJournalDomainQueueList(
|
||||
db: Database.Database,
|
||||
target: Target
|
||||
): QueuedMessageRow[] {
|
||||
return [
|
||||
...liveCards(db, target),
|
||||
...owedDispatchedCards(db, target, isQueuedAgentJournalSubmission)
|
||||
].sort(compareQueuedRows)
|
||||
}
|
||||
|
||||
export function readJournalDomainQueueHead(
|
||||
db: Database.Database,
|
||||
target: Target
|
||||
): { lastSequence: number; clearedThrough: number } {
|
||||
const head = db
|
||||
.prepare(`SELECT last_sequence, queue_clear_sequence, queue_clear_operation_id
|
||||
FROM journal_chat_state WHERE session_id=? AND epoch=?`)
|
||||
.get(target.sessionId, target.epoch)
|
||||
if (!head) {
|
||||
throw new Error('journal queue read targets a retired chat epoch')
|
||||
}
|
||||
return {
|
||||
lastSequence: Number(head.last_sequence),
|
||||
clearedThrough:
|
||||
head.queue_clear_operation_id === null ? 0 : Number(head.queue_clear_sequence ?? 0)
|
||||
}
|
||||
}
|
||||
|
||||
/** A recovered doubt outlived its writer: nothing brings that card back to waiting. */
|
||||
export function journalDomainQueueAwaitsReopen(db: Database.Database, target: Target): boolean {
|
||||
const waiting = db
|
||||
.prepare(`SELECT 1 FROM journal_queue_cards INDEXED BY journal_queue_order
|
||||
WHERE session_id=? AND epoch=? AND state IN ('waiting','returned') AND state='waiting' LIMIT 1`)
|
||||
.get(target.sessionId, target.epoch)
|
||||
return waiting !== undefined || owedDispatchedCards(db, target, () => true).length > 0
|
||||
}
|
||||
|
||||
/** Clear and reopen membership are listed only for cards a pause can hold. */
|
||||
export function readJournalDomainQueuePauses(
|
||||
db: Database.Database,
|
||||
target: Target,
|
||||
reopenFloor: AgentJournalCursor | null
|
||||
): DerivedQueuePause[] {
|
||||
const cards = liveCards(db, target).filter(
|
||||
(card) => card.state === 'waiting' && card.holdReason === null
|
||||
)
|
||||
const context = readJournalDomainQueuePauseContext(
|
||||
db,
|
||||
target,
|
||||
reopenFloor,
|
||||
cards.map((card) => card.messageId)
|
||||
)
|
||||
const { cleared } = context.marks
|
||||
return deriveQueuePauses({
|
||||
...context,
|
||||
marks: {
|
||||
...context.marks,
|
||||
...(cleared ? { cleared: { ...cleared, messageIds: [...context.clearMembers] } } : {})
|
||||
},
|
||||
cards
|
||||
})
|
||||
}
|
||||
|
||||
export function readJournalDomainUserStopInForce(
|
||||
db: Database.Database,
|
||||
target: Target
|
||||
): JournalLatestStop | null {
|
||||
const context = readJournalDomainQueuePauseContext(db, target, null, [])
|
||||
return journalUserStopInForce(
|
||||
context.marks,
|
||||
context.latestPersonAcceptedTurnSequence ?? context.latestAcceptedTurnSequence
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import { orchestrationMailNotice } from '../../../shared/agent-session/agent-session-message-source'
|
||||
import {
|
||||
orchestrationMailNotice,
|
||||
type AgentMessageSource
|
||||
} from '../../../shared/agent-session/agent-session-message-source'
|
||||
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import { journalMailSenderKey } from './journal-writer-host-facts'
|
||||
import type { JournalWriterOperation } from './journal-writer-command'
|
||||
import type {
|
||||
@@ -19,14 +23,24 @@ import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
|
||||
type Submission = Extract<JournalWriterOperation, { kind: 'submission' }>
|
||||
|
||||
/** Current ordered cards and exact edit holds are selected on the same writer snapshot. */
|
||||
function nextJournalDomainSubmissionCard(
|
||||
export type JournalDomainQueueSelection = {
|
||||
reopenFloor: AgentJournalCursor | null
|
||||
/** Wall-clock milliseconds that decide which persisted edit holds are live. */
|
||||
now: number
|
||||
steer: boolean
|
||||
/** Manual sends pass edit holds; automatic delivery waits behind them. */
|
||||
automatic: boolean
|
||||
/** Asked only for the first eligible mail card; past unavailable mail, later mail is skipped. */
|
||||
mailUnavailable: (from: AgentMessageSource) => boolean
|
||||
}
|
||||
|
||||
/** Current ordered cards and exact edit holds are selected on one snapshot, a bounded page at a time. */
|
||||
export function nextJournalDomainQueueCard(
|
||||
db: Database.Database,
|
||||
target: JournalDomainSubmissionTarget,
|
||||
facts: JournalDomainSubmissionFacts,
|
||||
steer: boolean,
|
||||
savedAt: number
|
||||
selection: JournalDomainQueueSelection
|
||||
): QueuedMessageRow | null {
|
||||
const { steer, automatic } = selection
|
||||
let after: { position: number; messageId: string } | undefined
|
||||
let pastUnavailableMail = false
|
||||
for (;;) {
|
||||
@@ -41,7 +55,7 @@ function nextJournalDomainSubmissionCard(
|
||||
${after ? 'AND (card.position,card.message_id)>(?,?)' : ''}
|
||||
ORDER BY card.position,card.message_id LIMIT 64`)
|
||||
.all(
|
||||
savedAt,
|
||||
selection.now,
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
...(after ? [after.position, after.messageId] : [])
|
||||
@@ -52,11 +66,11 @@ function nextJournalDomainSubmissionCard(
|
||||
const context = readJournalDomainQueuePauseContext(
|
||||
db,
|
||||
target,
|
||||
facts.reopenFloor ?? null,
|
||||
selection.reopenFloor,
|
||||
rows.map((row) => String(row.message_id))
|
||||
)
|
||||
for (const row of rows) {
|
||||
if (row.edit_held) {
|
||||
if (automatic && row.edit_held) {
|
||||
return null
|
||||
}
|
||||
const { card } = readJournalDomainQueueRow(target.sessionId, row)
|
||||
@@ -75,16 +89,9 @@ function nextJournalDomainSubmissionCard(
|
||||
if (pastUnavailableMail && mail) {
|
||||
continue
|
||||
}
|
||||
if (mail && card.body.from) {
|
||||
const key = journalMailSenderKey(card.body.from)
|
||||
const authority = facts.mail?.find(([sender]) => sender === key)
|
||||
if (!authority) {
|
||||
throw new Error('journal automatic mail selection requires supplied sender authority')
|
||||
}
|
||||
if (authority[1]) {
|
||||
pastUnavailableMail = true
|
||||
continue
|
||||
}
|
||||
if (mail && card.body.from && selection.mailUnavailable(card.body.from)) {
|
||||
pastUnavailableMail = true
|
||||
continue
|
||||
}
|
||||
return card
|
||||
}
|
||||
@@ -117,7 +124,21 @@ export function selectJournalDomainSubmissionCard(
|
||||
fingerprint: input.payloadFingerprint
|
||||
},
|
||||
current?.card ?? null,
|
||||
() => nextJournalDomainSubmissionCard(db, target, facts, consume.steer === true, savedAt)
|
||||
() =>
|
||||
nextJournalDomainQueueCard(db, target, {
|
||||
reopenFloor: facts.reopenFloor ?? null,
|
||||
now: savedAt,
|
||||
steer: consume.steer === true,
|
||||
automatic: true,
|
||||
mailUnavailable: (from) => {
|
||||
const key = journalMailSenderKey(from)
|
||||
const authority = facts.mail?.find(([sender]) => sender === key)
|
||||
if (!authority) {
|
||||
throw new Error('journal automatic mail selection requires supplied sender authority')
|
||||
}
|
||||
return authority[1]
|
||||
}
|
||||
})
|
||||
)
|
||||
const owed = consume.owed ? (facts.owed ?? 'unavailable') : 'owed'
|
||||
if (owed !== 'owed') {
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { codexProviderHandle } from '../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
|
||||
import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
import { ensureAgentSessionAttachmentClaimTables } from '../agent-session-attachments/agent-session-attachment-claims'
|
||||
import { migrateJournalDomainSchema } from './journal-domain-schema'
|
||||
import { applyJournalDomainSubmission } from './journal-domain-submission-admission'
|
||||
import { applyJournalDomainEditLease } from './journal-domain-edit-leases'
|
||||
import { insertJournalDomainQueueRow } from './journal-domain-queue-rows'
|
||||
import { journalMailSenderKey } from './journal-writer-host-facts'
|
||||
import { JournalSqlScope } from './journal-sql-scope'
|
||||
import {
|
||||
readJournalSqlQueue,
|
||||
type JournalSqlQueueFacts,
|
||||
type JournalSqlQueueRead
|
||||
} from './journal-sql-queued-messages'
|
||||
import type { QueuedMessageRow } from './queued-messages/queued-message-table'
|
||||
|
||||
type QueueRead = JournalSqlQueueRead extends infer R
|
||||
? R extends unknown
|
||||
? Omit<R, 'facts'>
|
||||
: never
|
||||
: never
|
||||
const target = { sessionId: 'chat', epoch: 'epoch' }
|
||||
const cleanups: (() => void)[] = []
|
||||
const body = (text: string): AgentJournalMessageItem => ({
|
||||
kind: 'message',
|
||||
role: 'user',
|
||||
blocks: [{ type: 'text', text }]
|
||||
})
|
||||
const from = {
|
||||
kind: 'agent' as const,
|
||||
senders: [],
|
||||
orchestration: {
|
||||
message: 'mail-notice' as const,
|
||||
mailbox: 'owner',
|
||||
dispatchId: null,
|
||||
messages: []
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const cleanup of cleanups.splice(0)) {
|
||||
cleanup()
|
||||
}
|
||||
})
|
||||
|
||||
function fixture() {
|
||||
const db = new Database(':memory:')
|
||||
const stateDirectory = mkdtempSync(join(tmpdir(), 'orca-queue-reader-'))
|
||||
cleanups.push(() => {
|
||||
db.close()
|
||||
rmSync(stateDirectory, { recursive: true, force: true })
|
||||
})
|
||||
migrateJournalDomainSchema(db)
|
||||
ensureAgentSessionAttachmentClaimTables(db)
|
||||
db.prepare(`INSERT INTO journal_chat_state(workspace_id,session_id,epoch,last_sequence,highest_fence)
|
||||
VALUES('workspace','chat','epoch',10,1)`).run()
|
||||
const transaction = <T>(write: () => T): T => {
|
||||
db.exec('BEGIN IMMEDIATE')
|
||||
try {
|
||||
const value = write()
|
||||
db.exec('COMMIT')
|
||||
return value
|
||||
} catch (error) {
|
||||
db.exec('ROLLBACK')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
const seed = (id: string, position: number, fields: Partial<QueuedMessageRow> = {}) => {
|
||||
const message = fields.body ?? body(id)
|
||||
insertJournalDomainQueueRow(db, target, {
|
||||
messageId: id,
|
||||
body: message,
|
||||
position,
|
||||
fingerprint: agentSessionSendBodyFingerprint('chat', message),
|
||||
state: 'waiting',
|
||||
holdReason: null,
|
||||
returnedReason: null,
|
||||
returnedRejection: null,
|
||||
consumedAs: null,
|
||||
queuedAt: { epoch: 'epoch', sequence: 1 },
|
||||
...fields
|
||||
})
|
||||
}
|
||||
const consume = (messageId: string, clientMessageId: string) =>
|
||||
transaction(() =>
|
||||
applyJournalDomainSubmission(
|
||||
db,
|
||||
stateDirectory,
|
||||
target,
|
||||
{
|
||||
kind: 'submission',
|
||||
input: {
|
||||
clientMessageId,
|
||||
body: body(messageId),
|
||||
payloadFingerprint: agentSessionSendBodyFingerprint('chat', body(messageId)),
|
||||
fence: 1,
|
||||
handoverRecorded: true,
|
||||
origin: 'client',
|
||||
source: { kind: 'user' }
|
||||
},
|
||||
consume: { messageId, expect: 'waiting' }
|
||||
},
|
||||
{
|
||||
identity: {
|
||||
sessionId: 'chat',
|
||||
workspaceId: 'workspace',
|
||||
hostId: 'host',
|
||||
agent: 'codex',
|
||||
providerHandle: codexProviderHandle('thread')
|
||||
}
|
||||
},
|
||||
1_800_000_000_000
|
||||
)
|
||||
)
|
||||
const hold = (messageId: string) =>
|
||||
transaction(() =>
|
||||
applyJournalDomainEditLease(
|
||||
db,
|
||||
target,
|
||||
'phone-handle',
|
||||
{
|
||||
kind: 'acquire',
|
||||
callerKey: 'phone',
|
||||
editId: 'edit',
|
||||
messageId,
|
||||
fingerprint: agentSessionSendBodyFingerprint('chat', body(messageId))
|
||||
},
|
||||
Date.now()
|
||||
)
|
||||
)
|
||||
const read = (operation: QueueRead, facts: Partial<JournalSqlQueueFacts> = {}) =>
|
||||
readJournalSqlQueue(new JournalSqlScope(db, 'chat', 'epoch'), {
|
||||
...operation,
|
||||
facts: { reopenFloor: null, ...facts }
|
||||
})
|
||||
return { db, seed, consume, hold, read }
|
||||
}
|
||||
|
||||
const ids = (result: ReturnType<typeof readJournalSqlQueue>) =>
|
||||
result.kind === 'list' ? result.cards.map((card) => card.messageId) : result
|
||||
|
||||
it('lists current and still-owed handoff cards, and selects past persisted holds and mail', () => {
|
||||
const f = fixture()
|
||||
f.seed('withdrawn', 0, { state: 'withdrawn' })
|
||||
f.seed('sent', 1)
|
||||
f.seed('mail', 2, { body: { ...body('mail'), from } })
|
||||
f.seed('next', 3)
|
||||
f.consume('sent', 'send-1')
|
||||
|
||||
expect(ids(f.read({ kind: 'list' }))).toEqual(['sent', 'mail', 'next'])
|
||||
expect(f.read({ kind: 'get', messageId: 'withdrawn' })).toMatchObject({
|
||||
card: { messageId: 'withdrawn', state: 'withdrawn' }
|
||||
})
|
||||
expect(f.read({ kind: 'revision' })).toEqual({ kind: 'revision', revision: 'epoch:11' })
|
||||
|
||||
const next = (automatic: boolean, unavailable?: boolean) =>
|
||||
f.read(
|
||||
{ kind: 'next-input-card', automatic, steer: false },
|
||||
unavailable === undefined ? {} : { mail: [[journalMailSenderKey(from), unavailable]] }
|
||||
)
|
||||
expect(next(true)).toEqual({ kind: 'mail-needed', from })
|
||||
expect(next(true, false)).toMatchObject({ card: { messageId: 'mail' } })
|
||||
expect(next(true, true)).toMatchObject({ card: { messageId: 'next' } })
|
||||
|
||||
f.hold('mail')
|
||||
expect(next(true, false)).toEqual({ kind: 'next-input-card', card: null })
|
||||
expect(next(false, false)).toMatchObject({ card: { messageId: 'mail' } })
|
||||
})
|
||||
|
||||
it('owes a reopen mark only for waiting cards or dispatched cards still awaiting delivery', () => {
|
||||
const f = fixture()
|
||||
f.seed('only', 1)
|
||||
expect(f.read({ kind: 'reopen-mark' })).toEqual({ kind: 'reopen-mark', awaits: true, since: 11 })
|
||||
f.consume('only', 'send-1')
|
||||
expect(f.read({ kind: 'reopen-mark' })).toEqual({ kind: 'reopen-mark', awaits: true, since: 12 })
|
||||
f.db.prepare(`UPDATE journal_submission_attempts SET dispatch_state='unknown', recovered=1`).run()
|
||||
expect(f.read({ kind: 'reopen-mark' })).toEqual({ kind: 'reopen-mark', awaits: false, since: 12 })
|
||||
expect(ids(f.read({ kind: 'list' }))).toEqual([])
|
||||
})
|
||||
|
||||
it('derives Stop, clear and reopen pauses from head facts and exact clear membership', () => {
|
||||
const f = fixture()
|
||||
f.seed('cleared', 1)
|
||||
f.seed('kept', 2)
|
||||
f.db.exec(`UPDATE journal_chat_state SET queue_clear_sequence=8, queue_clear_operation_id='clear',
|
||||
queue_latest_stop_json='{"sequence":9,"event":{"reason":"user-stop","at":5}}';
|
||||
INSERT INTO journal_queue_clear_members VALUES('chat','epoch','cleared')`)
|
||||
const floor = { reopenFloor: { epoch: 'epoch', sequence: 7 } }
|
||||
|
||||
expect(f.read({ kind: 'cleared-through' })).toEqual({ kind: 'cleared-through', sequence: 8 })
|
||||
expect(f.read({ kind: 'user-stop-in-force' })).toEqual({
|
||||
kind: 'user-stop-in-force',
|
||||
stop: { sequence: 9, event: { reason: 'user-stop', at: 5 } }
|
||||
})
|
||||
expect(f.read({ kind: 'pauses' }, floor)).toEqual({
|
||||
kind: 'pauses',
|
||||
pauses: [
|
||||
{ reason: 'stopped', since: { epoch: 'epoch', sequence: 9 } },
|
||||
{ reason: 'cleared', since: { epoch: 'epoch', sequence: 8 }, messageIds: ['cleared'] },
|
||||
{
|
||||
reason: 'restarted',
|
||||
since: { epoch: 'epoch', sequence: 7 },
|
||||
messageIds: ['cleared', 'kept']
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { AgentMessageSource } from '../../../shared/agent-session/agent-session-message-source'
|
||||
import { orchestrationMailNotice } from '../../../shared/agent-session/agent-session-message-source'
|
||||
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import type { AgentSessionExecutionOptions } from '../../../shared/agent-session/options/agent-session-execution-options'
|
||||
@@ -9,27 +8,31 @@ import type {
|
||||
AgentSessionQueuedMessageUpdateResult
|
||||
} from '../../../shared/agent-session/requests/agent-session-queued-message-wire'
|
||||
import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end'
|
||||
import { JournalQueuedMessages } from './queued-messages/journal-queued-messages'
|
||||
import type { DerivedQueuePause } from './queued-messages/queued-message-pause'
|
||||
import type {
|
||||
QueuedMessageHoldReason,
|
||||
QueuedMessageRow
|
||||
} from './queued-messages/queued-message-table'
|
||||
import type { QueuedMessageUpdate } from './queued-messages/queued-message-update'
|
||||
import type { JournalTransactionalDatabase } from './database/journal-host-database'
|
||||
import { JournalSqlReducer } from './journal-sql-reducer'
|
||||
import type { QueuedMessageEditLease } from './queued-messages/queued-message-edit-leases'
|
||||
import { journalDomainQueueRow } from './journal-domain-queue-rows'
|
||||
import {
|
||||
journalDomainQueueAwaitsReopen,
|
||||
readJournalDomainQueueHead,
|
||||
readJournalDomainQueueList,
|
||||
readJournalDomainQueuePauses,
|
||||
readJournalDomainUserStopInForce
|
||||
} from './journal-domain-queue-reads'
|
||||
import { nextJournalDomainQueueCard } from './journal-domain-submission-consume'
|
||||
import type { JournalSqlScope } from './journal-sql-scope'
|
||||
import { journalMailSenderKey } from './journal-writer-host-facts'
|
||||
import { JournalRowWriter, type JournalOperationReceipt } from './writing/journal-row-writer'
|
||||
import {
|
||||
queuedMessageEditLeaseHolds,
|
||||
type QueuedMessageEditLease
|
||||
} from './queued-messages/queued-message-edit-leases'
|
||||
import type { JournalOperationReceipt } from './writing/journal-row-writer'
|
||||
import { SqlQueuedMessageEditLeases } from './journal-sql-edit-leases'
|
||||
|
||||
/** Host-memory facts a queue read needs, sent as data like the writer's facts. */
|
||||
export type JournalSqlQueueFacts = {
|
||||
reopenFloor: AgentJournalCursor | null
|
||||
/** Writer-session transport only; reads select persisted holds. */
|
||||
editLeases?: readonly QueuedMessageEditLease[]
|
||||
mail?: readonly (readonly [string, boolean])[]
|
||||
}
|
||||
@@ -64,78 +67,49 @@ class MailNeeded extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
const refuse = (): never => {
|
||||
throw new Error('a journal queue read cannot write')
|
||||
}
|
||||
|
||||
/**
|
||||
* The writer's own queue procedures over a reducer shell of the caller's read snapshot. The shell
|
||||
* is never flushed and its row writer refuses, so a read can only answer.
|
||||
*/
|
||||
/** Named queue reads on the current cards, typed head facts and persisted edit holds of one snapshot. */
|
||||
export function readJournalSqlQueue(
|
||||
read: JournalSqlScope,
|
||||
operation: JournalSqlQueueRead
|
||||
): JournalSqlQueueReadResult {
|
||||
// Card selection records the host's reopen floor as a fact; a read keeps it to itself.
|
||||
const scope = read.asSnapshot()
|
||||
const reducer = new JournalSqlReducer(scope, scope.epoch)
|
||||
const { db } = read
|
||||
const target = { sessionId: read.sessionId, epoch: read.epoch }
|
||||
const { facts } = operation
|
||||
const held = queuedMessageEditLeaseHolds(facts.editLeases ?? [])
|
||||
const database: JournalTransactionalDatabase = {
|
||||
db: scope.db,
|
||||
readOnly: true,
|
||||
stateDirectory: '',
|
||||
transaction: refuse
|
||||
}
|
||||
const queue = new JournalQueuedMessages({
|
||||
sessionId: scope.sessionId,
|
||||
serialize: refuse,
|
||||
database: () => database,
|
||||
readOnly: () => true,
|
||||
state: () => reducer.state,
|
||||
reopenFloor: () => facts.reopenFloor,
|
||||
writer: new JournalRowWriter({
|
||||
sessionId: scope.sessionId,
|
||||
now: Date.now,
|
||||
serialize: refuse,
|
||||
database: () => database,
|
||||
readOnly: () => true,
|
||||
highestFence: () => 0,
|
||||
nextSequence: () => 0,
|
||||
commit: refuse
|
||||
}),
|
||||
committed: () => {},
|
||||
claimAttachments: refuse,
|
||||
editHeld: (messageId) => held(queue.get(messageId))
|
||||
})
|
||||
queue.mailUnavailable = (card) => {
|
||||
const from = card.body.from
|
||||
if (!from || !orchestrationMailNotice(from)) {
|
||||
return false
|
||||
}
|
||||
const known = facts.mail?.find(([sender]) => sender === journalMailSenderKey(from))
|
||||
if (!known) {
|
||||
throw new MailNeeded(from)
|
||||
}
|
||||
return known[1]
|
||||
}
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: untracking returns the decoded value the storage proxy wraps, of the same type.
|
||||
const plain = <T>(value: T): T => reducer.context.untracked('', value) as T
|
||||
const card = (value: QueuedMessageRow | null) => (value ? detachCard(plain(value)) : null)
|
||||
switch (operation.kind) {
|
||||
case 'list':
|
||||
return { kind: 'list', cards: queue.list().map((value) => detachCard(plain(value))) }
|
||||
return { kind: 'list', cards: readJournalDomainQueueList(db, target) }
|
||||
case 'get':
|
||||
return { kind: 'get', card: card(queue.get(operation.messageId)) }
|
||||
case 'revision':
|
||||
return { kind: 'revision', revision: queue.revision() }
|
||||
return {
|
||||
kind: 'get',
|
||||
card: journalDomainQueueRow(db, target, operation.messageId)?.card ?? null
|
||||
}
|
||||
case 'revision': {
|
||||
// Every queue command appends an event, so the head sequence moves whenever a card does.
|
||||
const { lastSequence } = readJournalDomainQueueHead(db, target)
|
||||
return { kind: 'revision', revision: `${target.epoch}:${lastSequence}` }
|
||||
}
|
||||
case 'cleared-through':
|
||||
return { kind: 'cleared-through', sequence: queue.clearedThrough() }
|
||||
return {
|
||||
kind: 'cleared-through',
|
||||
sequence: readJournalDomainQueueHead(db, target).clearedThrough
|
||||
}
|
||||
case 'next-input-card':
|
||||
try {
|
||||
return {
|
||||
kind: 'next-input-card',
|
||||
card: card(queue.nextInputCard(operation.automatic, operation.steer))
|
||||
card: nextJournalDomainQueueCard(db, target, {
|
||||
reopenFloor: facts.reopenFloor,
|
||||
now: Date.now(),
|
||||
steer: operation.steer,
|
||||
automatic: operation.automatic,
|
||||
mailUnavailable: (from) => {
|
||||
const known = facts.mail?.find(([sender]) => sender === journalMailSenderKey(from))
|
||||
if (!known) {
|
||||
throw new MailNeeded(from)
|
||||
}
|
||||
return known[1]
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof MailNeeded) {
|
||||
@@ -146,19 +120,15 @@ export function readJournalSqlQueue(
|
||||
case 'pauses':
|
||||
return {
|
||||
kind: 'pauses',
|
||||
pauses: queue.pauses().map((pause) => ({
|
||||
reason: pause.reason,
|
||||
since: pause.since && { ...pause.since },
|
||||
...(pause.messageIds ? { messageIds: [...pause.messageIds] } : {})
|
||||
}))
|
||||
pauses: readJournalDomainQueuePauses(db, target, facts.reopenFloor)
|
||||
}
|
||||
case 'user-stop-in-force':
|
||||
return { kind: 'user-stop-in-force', stop: detachJournalSqlStop(queue.userStopInForce()) }
|
||||
return { kind: 'user-stop-in-force', stop: readJournalDomainUserStopInForce(db, target) }
|
||||
case 'reopen-mark':
|
||||
return {
|
||||
kind: 'reopen-mark',
|
||||
awaits: queue.awaitReopenMark(),
|
||||
since: reducer.state.lastSequence + 1
|
||||
awaits: journalDomainQueueAwaitsReopen(db, target),
|
||||
since: readJournalDomainQueueHead(db, target).lastSequence + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,11 +144,6 @@ export function detachJournalSqlStop(stop: JournalLatestStop | null): JournalLat
|
||||
)
|
||||
}
|
||||
|
||||
function detachCard(card: QueuedMessageRow): QueuedMessageRow {
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the storage row is already typed; JSON detaches its lazy value proxies.
|
||||
return JSON.parse(JSON.stringify(card)) as QueuedMessageRow
|
||||
}
|
||||
|
||||
export type JournalSqlQueuePort = {
|
||||
read: <K extends JournalSqlQueueRead['kind']>(
|
||||
operation: Extract<JournalSqlQueueRead, { kind: K }>
|
||||
@@ -254,6 +219,17 @@ export class SqlJournalQueuedMessages {
|
||||
return (await this.read({ kind: 'cleared-through' }, 'cleared-through')).sequence
|
||||
}
|
||||
|
||||
/** The one mail fact an automatic consume selection can ask, captured before its writer command. */
|
||||
async automaticMailFacts(steer = false): Promise<(readonly [string, boolean])[]> {
|
||||
const result = await this.port.read({
|
||||
kind: 'next-input-card',
|
||||
automatic: true,
|
||||
steer,
|
||||
facts: this.facts()
|
||||
})
|
||||
return result.kind === 'mail-needed' ? this.answerMail([result.from]) : []
|
||||
}
|
||||
|
||||
async nextInputCard(automatic = true, steer = false): Promise<QueuedMessageRow | null> {
|
||||
const mail: (readonly [string, boolean])[] = []
|
||||
for (;;) {
|
||||
|
||||
Reference in New Issue
Block a user