mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 03:45:08 +00:00
refactor: integrate direct item and lifecycle reads
This commit is contained in:
@@ -99,6 +99,18 @@ CREATE TABLE journal_items (
|
||||
queue_failed_at INTEGER NOT NULL DEFAULT 0,
|
||||
queue_failure_witnessed_through INTEGER NOT NULL DEFAULT 0,
|
||||
queue_failure_report_turn_item_id TEXT,
|
||||
placement_json TEXT,
|
||||
kind TEXT,
|
||||
message_role TEXT,
|
||||
turn_id TEXT,
|
||||
opener_item_id TEXT,
|
||||
context_clear INTEGER NOT NULL DEFAULT 0,
|
||||
goal_transition INTEGER NOT NULL DEFAULT 0,
|
||||
producer_agent_id TEXT,
|
||||
producer_parent_ref TEXT,
|
||||
producer_attempt INTEGER,
|
||||
prompt INTEGER NOT NULL DEFAULT 0,
|
||||
signed_out INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY(session_id, epoch, item_id)
|
||||
);
|
||||
CREATE TABLE journal_item_subagent_names (
|
||||
@@ -122,6 +134,12 @@ CREATE TABLE journal_item_tombstones (
|
||||
revision INTEGER NOT NULL,
|
||||
PRIMARY KEY(session_id, epoch, item_id)
|
||||
) WITHOUT ROWID;
|
||||
CREATE TABLE journal_removed_turns (
|
||||
session_id TEXT NOT NULL,
|
||||
epoch TEXT NOT NULL,
|
||||
turn_id TEXT NOT NULL,
|
||||
PRIMARY KEY(session_id, epoch, turn_id)
|
||||
) WITHOUT ROWID;
|
||||
CREATE TABLE journal_item_aliases (
|
||||
session_id TEXT NOT NULL,
|
||||
epoch TEXT NOT NULL,
|
||||
@@ -145,6 +163,8 @@ CREATE TABLE journal_submission_attempts (
|
||||
accepted_sequence INTEGER,
|
||||
fingerprint TEXT NOT NULL,
|
||||
dispatch_state TEXT NOT NULL,
|
||||
dispatch_fence INTEGER NOT NULL DEFAULT 0,
|
||||
signed_out INTEGER NOT NULL DEFAULT 0 CHECK(signed_out IN (0,1)),
|
||||
candidate INTEGER NOT NULL DEFAULT 0,
|
||||
admitted_personal INTEGER NOT NULL DEFAULT 0 CHECK(admitted_personal IN (0,1)),
|
||||
payload_json TEXT NOT NULL,
|
||||
@@ -217,6 +237,33 @@ CREATE INDEX journal_items_queue_failure_report ON journal_items(session_id, epo
|
||||
CREATE INDEX journal_items_stop_turn ON journal_items(session_id, epoch, turn_item_id)
|
||||
WHERE hidden=0 AND turn_item_id IS NOT NULL;
|
||||
CREATE INDEX journal_events_saved_at ON journal_events(saved_at,session_id,epoch,sequence);
|
||||
|
||||
CREATE INDEX journal_items_kind ON journal_items(session_id, epoch, kind, ordinal) WHERE hidden=0;
|
||||
CREATE INDEX journal_items_user_message ON journal_items(session_id, epoch, ordinal)
|
||||
WHERE hidden=0 AND kind='message' AND message_role='user';
|
||||
CREATE INDEX journal_items_named_turn ON journal_items(session_id, epoch, turn_id, sequence, sequence_index, ordinal)
|
||||
WHERE hidden=0 AND turn_id IS NOT NULL;
|
||||
CREATE INDEX journal_items_opener ON journal_items(session_id, epoch, opener_item_id, ordinal)
|
||||
WHERE hidden=0 AND opener_item_id IS NOT NULL;
|
||||
CREATE INDEX journal_items_context_clear ON journal_items(session_id, epoch, sequence)
|
||||
WHERE hidden=0 AND context_clear=1;
|
||||
CREATE INDEX journal_items_goal_transition ON journal_items(session_id, epoch, sequence, ordinal)
|
||||
WHERE hidden=0 AND goal_transition=1;
|
||||
CREATE INDEX journal_items_producer_parent ON journal_items(session_id, epoch, producer_parent_ref, ordinal, producer_agent_id)
|
||||
WHERE hidden=0 AND producer_parent_ref IS NOT NULL;
|
||||
CREATE INDEX journal_items_producer_attempt ON journal_items(session_id, epoch, producer_agent_id, producer_attempt)
|
||||
WHERE hidden=0 AND producer_agent_id IS NOT NULL;
|
||||
CREATE INDEX journal_items_prompt ON journal_items(session_id, epoch, sequence, ordinal)
|
||||
WHERE hidden=0 AND prompt=1;
|
||||
CREATE INDEX journal_items_signed_out ON journal_items(session_id, epoch, COALESCE(execution_owner,owner_fence))
|
||||
WHERE hidden=0 AND root=1 AND signed_out=1;
|
||||
CREATE INDEX journal_submission_accepted_position ON journal_submission_attempts(session_id, epoch, accepted_sequence, ordinal)
|
||||
WHERE current=1 AND accepted_sequence IS NOT NULL;
|
||||
CREATE INDEX journal_submission_signed_out ON journal_submission_attempts(session_id,epoch,dispatch_fence)
|
||||
WHERE current=1 AND signed_out=1;
|
||||
CREATE INDEX journal_submission_order ON journal_submission_attempts(session_id, epoch, submitted_at, ordinal, message_id)
|
||||
WHERE current=1;
|
||||
|
||||
CREATE TABLE journal_maintenance (
|
||||
id INTEGER PRIMARY KEY CHECK(id=1),
|
||||
compacted_at INTEGER NOT NULL
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { journalDomainItemReadColumns } from './journal-domain-item-read-columns'
|
||||
import { journalDomainItemPlacement } from './journal-domain-item-placement'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { readAgentJournalTurn } from '../../../shared/agent-session/lifecycle/agent-session-turn-record'
|
||||
@@ -57,8 +59,9 @@ export function writeJournalDomainCurrentItem(
|
||||
const metadata = journalDomainHistoryMetadata(next, payload)
|
||||
db.prepare(`INSERT INTO journal_items(session_id,epoch,item_id,ordinal,revision,sequence,sequence_index,owner_fence,execution_owner,payload_json,
|
||||
hidden,root,subagent_row,subagent_group_key,turn_marker,wire_bytes,turn_item_id,original_stop_note_json,
|
||||
queue_command_turn,queue_failed_outcome,queue_failed_at,queue_failure_witnessed_through,queue_failure_report_turn_item_id)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(session_id,epoch,item_id) DO UPDATE SET
|
||||
queue_command_turn,queue_failed_outcome,queue_failed_at,queue_failure_witnessed_through,queue_failure_report_turn_item_id,
|
||||
placement_json,kind,message_role,turn_id,opener_item_id,context_clear,goal_transition,producer_agent_id,producer_parent_ref,producer_attempt,prompt,signed_out)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(session_id,epoch,item_id) DO UPDATE SET
|
||||
ordinal=excluded.ordinal, revision=excluded.revision, sequence=excluded.sequence, sequence_index=excluded.sequence_index,
|
||||
owner_fence=excluded.owner_fence, execution_owner=excluded.execution_owner, payload_json=excluded.payload_json,
|
||||
hidden=excluded.hidden, root=excluded.root, subagent_row=excluded.subagent_row, subagent_group_key=excluded.subagent_group_key,
|
||||
@@ -66,7 +69,12 @@ export function writeJournalDomainCurrentItem(
|
||||
turn_item_id=excluded.turn_item_id, original_stop_note_json=excluded.original_stop_note_json,
|
||||
queue_command_turn=excluded.queue_command_turn, queue_failed_outcome=excluded.queue_failed_outcome,
|
||||
queue_failed_at=excluded.queue_failed_at, queue_failure_witnessed_through=excluded.queue_failure_witnessed_through,
|
||||
queue_failure_report_turn_item_id=excluded.queue_failure_report_turn_item_id`).run(
|
||||
queue_failure_report_turn_item_id=excluded.queue_failure_report_turn_item_id,
|
||||
placement_json=excluded.placement_json,kind=excluded.kind,message_role=excluded.message_role,
|
||||
turn_id=excluded.turn_id,opener_item_id=excluded.opener_item_id,context_clear=excluded.context_clear,
|
||||
goal_transition=excluded.goal_transition,producer_agent_id=excluded.producer_agent_id,
|
||||
producer_parent_ref=excluded.producer_parent_ref,producer_attempt=excluded.producer_attempt,
|
||||
prompt=excluded.prompt,signed_out=excluded.signed_out`).run(
|
||||
...prefix,
|
||||
next.itemId,
|
||||
input.ordinal,
|
||||
@@ -88,7 +96,9 @@ export function writeJournalDomainCurrentItem(
|
||||
failure.failedOutcome,
|
||||
failure.failedAt,
|
||||
failure.witnessedThrough,
|
||||
failure.reportTurnItemId
|
||||
failure.reportTurnItemId,
|
||||
journalDomainItemPlacement(next),
|
||||
...journalDomainItemReadColumns(next)
|
||||
)
|
||||
writeJournalDomainItemNames(
|
||||
db,
|
||||
|
||||
@@ -19,6 +19,7 @@ export function discardJournalDomainImportPage(
|
||||
['journal_item_subagent_names', 'epoch,subagent_id,item_id'],
|
||||
['journal_items', 'epoch,item_id'],
|
||||
['journal_item_tombstones', 'epoch,item_id'],
|
||||
['journal_removed_turns', 'epoch,turn_id'],
|
||||
['journal_item_aliases', 'epoch,provider_item_id'],
|
||||
['journal_submission_attempts', 'epoch,message_id,attempt_sequence,attempt_index'],
|
||||
['journal_send_receipts', 'epoch,message_id'],
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { journalDomainItemReadColumns } from './journal-domain-item-read-columns'
|
||||
import { journalDomainItemPlacement } from './journal-domain-item-placement'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import { readWholeAgentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure'
|
||||
import { agentJournalSubmissionKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { journalDomainHistoryMetadata } from './journal-domain-history-metadata'
|
||||
import { insertJournalDomainQueueRow, journalDomainQueueRow } from './journal-domain-queue-rows'
|
||||
@@ -46,8 +49,9 @@ export function insertJournalDomainImportPage(
|
||||
db.prepare(`INSERT INTO journal_items(session_id,epoch,item_id,ordinal,revision,
|
||||
sequence,sequence_index,owner_fence,execution_owner,payload_json,hidden,root,subagent_row,
|
||||
subagent_group_key,turn_marker,wire_bytes,turn_item_id,original_stop_note_json,
|
||||
queue_command_turn,queue_failed_outcome,queue_failed_at,queue_failure_witnessed_through,queue_failure_report_turn_item_id)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`)
|
||||
queue_command_turn,queue_failed_outcome,queue_failed_at,queue_failure_witnessed_through,queue_failure_report_turn_item_id,
|
||||
placement_json,kind,message_role,turn_id,opener_item_id,context_clear,goal_transition,producer_agent_id,producer_parent_ref,producer_attempt,prompt,signed_out)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`)
|
||||
for (const {
|
||||
ordinal,
|
||||
ownerFence,
|
||||
@@ -86,7 +90,9 @@ export function insertJournalDomainImportPage(
|
||||
Number(queueFailedOutcome),
|
||||
queueFailedAt,
|
||||
queueFailureWitnessedThrough,
|
||||
queueFailureReportTurnItemId
|
||||
queueFailureReportTurnItemId,
|
||||
journalDomainItemPlacement(item),
|
||||
...journalDomainItemReadColumns(item)
|
||||
)
|
||||
if (Number(result.changes) > 0) {
|
||||
writeJournalDomainItemNames(
|
||||
@@ -127,8 +133,8 @@ export function insertJournalDomainImportPage(
|
||||
const insert =
|
||||
db.prepare(`INSERT INTO journal_submission_attempts(session_id,epoch,message_id,
|
||||
attempt_sequence,attempt_index,current,item_id,ordinal,submitted_at,accepted_sequence,
|
||||
fingerprint,dispatch_state,candidate,admitted_personal,payload_json)
|
||||
VALUES(?,?,?,?,0,1,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`)
|
||||
fingerprint,dispatch_state,candidate,admitted_personal,dispatch_fence,signed_out,payload_json)
|
||||
VALUES(?,?,?,?,0,1,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING`)
|
||||
for (const { ordinal, candidate, admittedPersonal, submission } of page.rows) {
|
||||
insert.run(
|
||||
...prefix,
|
||||
@@ -142,6 +148,11 @@ export function insertJournalDomainImportPage(
|
||||
submission.dispatchState,
|
||||
Number(candidate),
|
||||
Number(admittedPersonal),
|
||||
submission.fence,
|
||||
Number(
|
||||
submission.dispatchState === 'rejected' &&
|
||||
readWholeAgentSessionFailureFact(submission.rejection)?.kind === 'notSignedIn'
|
||||
),
|
||||
JSON.stringify(submission)
|
||||
)
|
||||
}
|
||||
@@ -172,6 +183,14 @@ export function insertJournalDomainImportPage(
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'removed-turns': {
|
||||
const insert = db.prepare(`INSERT INTO journal_removed_turns(session_id,epoch,turn_id)
|
||||
VALUES(?,?,?) ON CONFLICT DO NOTHING`)
|
||||
for (const { turnId } of page.rows) {
|
||||
insert.run(...prefix, turnId)
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'queue-clear-members': {
|
||||
const insert =
|
||||
db.prepare(`INSERT INTO journal_queue_clear_members(session_id,epoch,message_id)
|
||||
|
||||
@@ -19,6 +19,7 @@ import { JournalSqlConversions } from './journal-sql-conversions'
|
||||
import { journalSqlReaderPath } from './journal-sql-worker-bundle.test-fixture'
|
||||
import { readJournalDomainQueuePauseContext } from './journal-domain-queue-pauses'
|
||||
import { readJournalDomainTurnScope } from './journal-domain-turn-scope'
|
||||
import { readJournalDomainItems } from './journal-sql-item-reads'
|
||||
|
||||
const cleanup: (() => void)[] = []
|
||||
afterEach(() => {
|
||||
@@ -192,6 +193,16 @@ it('imports a frozen saved chat as typed domain rows, ready last, and resumes af
|
||||
}
|
||||
)
|
||||
).toEqual({ kind: 'turn', turnItemId: 'failed' })
|
||||
expect(
|
||||
readJournalDomainItems(
|
||||
db,
|
||||
{ sessionId: 'session', epoch: 'epoch' },
|
||||
{
|
||||
kind: 'named-turn',
|
||||
turnId: 'failed'
|
||||
}
|
||||
)
|
||||
).toEqual({ kind: 'named-turn', turn: { scope: null, removed: true } })
|
||||
})
|
||||
|
||||
it('keeps a ready chat on its domain rows and never reads the changed or missing frozen source', () => {
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
import { journalInputPresentationMember } from './input-order/journal-input-obligations'
|
||||
import { journalInputOrder } from './input-order/journal-input-order'
|
||||
import { journalOutcomeSource } from './outcomes/journal-outcome-format'
|
||||
import { memoryJournalMap } from './journal-memory-collections'
|
||||
import { memoryJournalMap, memoryJournalSet } from './journal-memory-collections'
|
||||
import type { JournalReducerState } from './projection/journal-reducer'
|
||||
import { journalDomainStopNote } from './journal-domain-stop-notes'
|
||||
import type { JournalQueuePauseMarks } from './queued-messages/queued-message-pause'
|
||||
@@ -76,6 +76,7 @@ export type JournalDomainImportPage =
|
||||
| { kind: 'cards'; rows: QueuedMessageRow[] }
|
||||
| { kind: 'activity'; rows: { fence: number; lastActivityAt: number }[] }
|
||||
| { kind: 'queue-clear-members'; rows: { messageId: string }[] }
|
||||
| { kind: 'removed-turns'; rows: { turnId: string }[] }
|
||||
|
||||
export type JournalDomainImportHeader = {
|
||||
sessionId: string
|
||||
@@ -221,6 +222,10 @@ function* importPages(
|
||||
submission
|
||||
}))
|
||||
)
|
||||
yield* paged(
|
||||
'removed-turns',
|
||||
[...memoryJournalSet(state.removedTurnIds)].map((turnId) => ({ turnId }))
|
||||
)
|
||||
yield* paged('receipts', [...memoryJournalMap(state.receipts).values()])
|
||||
yield* paged('cards', [...cards.values()])
|
||||
yield* paged(
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
|
||||
/** Placement stays small when the writer replaces a growing text body. */
|
||||
export function journalDomainItemPlacement(item: AgentJournalRenderItem): string {
|
||||
const { body: _body, ...placement } = item
|
||||
return JSON.stringify(placement)
|
||||
}
|
||||
@@ -1,48 +1,13 @@
|
||||
import { isRootAgentJournalItem } from '../../../shared/agent-session/journal/agent-session-journal-producer'
|
||||
import { isStructuredAgentSessionCommandEntry } from '../../../shared/agent-session/structured-agent-session-command-entry'
|
||||
import { readWholeAgentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure'
|
||||
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { isAgentSessionContextClear } from '../../../shared/agent-session/agent-session-context-clear'
|
||||
import { currentAgentSessionThreadGoalSourceBySequence } from '../../../shared/agent-session/agent-session-thread-goal'
|
||||
import { readAgentJournalTurn } from '../../../shared/agent-session/lifecycle/agent-session-turn-record'
|
||||
import { claudeAgentRowFacts } from '../../claude/claude-subagent-journaled-identities'
|
||||
|
||||
/** journal_items columns the direct item reads seek by; frozen 001 declares them beside history's. */
|
||||
export const JOURNAL_DOMAIN_ITEM_READ_COLUMNS = [
|
||||
'kind TEXT',
|
||||
'message_role TEXT',
|
||||
'turn_id TEXT',
|
||||
'opener_item_id TEXT',
|
||||
'context_clear INTEGER NOT NULL DEFAULT 0',
|
||||
'goal_transition INTEGER NOT NULL DEFAULT 0',
|
||||
'producer_agent_id TEXT',
|
||||
'producer_parent_ref TEXT',
|
||||
'producer_attempt INTEGER'
|
||||
] as const
|
||||
|
||||
export const JOURNAL_DOMAIN_ITEM_READ_INDEXES = `
|
||||
CREATE INDEX journal_items_kind ON journal_items(session_id, epoch, kind, ordinal) WHERE hidden=0;
|
||||
CREATE INDEX journal_items_user_message ON journal_items(session_id, epoch, ordinal)
|
||||
WHERE hidden=0 AND kind='message' AND message_role='user';
|
||||
CREATE INDEX journal_items_named_turn ON journal_items(session_id, epoch, turn_id, sequence, sequence_index, ordinal)
|
||||
WHERE hidden=0 AND turn_id IS NOT NULL;
|
||||
CREATE INDEX journal_items_opener ON journal_items(session_id, epoch, opener_item_id, ordinal)
|
||||
WHERE hidden=0 AND opener_item_id IS NOT NULL;
|
||||
CREATE INDEX journal_items_context_clear ON journal_items(session_id, epoch, sequence)
|
||||
WHERE hidden=0 AND context_clear=1;
|
||||
CREATE INDEX journal_items_goal_transition ON journal_items(session_id, epoch, sequence, ordinal)
|
||||
WHERE hidden=0 AND goal_transition=1;
|
||||
CREATE INDEX journal_items_producer_parent ON journal_items(session_id, epoch, producer_parent_ref, ordinal, producer_agent_id)
|
||||
WHERE hidden=0 AND producer_parent_ref IS NOT NULL;
|
||||
CREATE INDEX journal_items_producer_attempt ON journal_items(session_id, epoch, producer_agent_id, producer_attempt)
|
||||
WHERE hidden=0 AND producer_agent_id IS NOT NULL;
|
||||
CREATE INDEX journal_submission_order ON journal_submission_attempts(session_id, epoch, submitted_at, ordinal, message_id)
|
||||
WHERE current=1;
|
||||
CREATE INDEX journal_item_tombstones_turn ON journal_item_tombstones(session_id, epoch, turn_id)
|
||||
WHERE turn_id IS NOT NULL;
|
||||
`
|
||||
|
||||
/** The removed item's turn ID, written by the tombstone writer from the row it deletes. */
|
||||
export const JOURNAL_DOMAIN_TOMBSTONE_READ_COLUMNS = ['turn_id TEXT'] as const
|
||||
|
||||
/** Bound in JOURNAL_DOMAIN_ITEM_READ_COLUMNS order by the one item/import upsert. */
|
||||
/** Bound in item-read column order by the one item/import upsert. */
|
||||
export type JournalDomainItemReadColumns = readonly [
|
||||
kind: string,
|
||||
messageRole: string | null,
|
||||
@@ -52,7 +17,9 @@ export type JournalDomainItemReadColumns = readonly [
|
||||
goalTransition: number,
|
||||
producerAgentId: string | null,
|
||||
producerParentRef: string | null,
|
||||
producerAttempt: number | null
|
||||
producerAttempt: number | null,
|
||||
prompt: number,
|
||||
signedOut: number
|
||||
]
|
||||
|
||||
/** Row-local facts only, written from each stored revision; no cross-row verdict. */
|
||||
@@ -71,6 +38,16 @@ export function journalDomainItemReadColumns(
|
||||
Number(currentAgentSessionThreadGoalSourceBySequence([item]) !== undefined),
|
||||
producer?.agentId ?? null,
|
||||
producer?.parentRef ?? null,
|
||||
producer?.attempt ?? null
|
||||
producer?.attempt ?? null,
|
||||
Number(
|
||||
body.kind === 'message' &&
|
||||
body.role === 'user' &&
|
||||
isRootAgentJournalItem(item) &&
|
||||
!isStructuredAgentSessionCommandEntry(body)
|
||||
),
|
||||
Number(
|
||||
body.kind === 'status' &&
|
||||
readWholeAgentSessionFailureFact(body.failure)?.kind === 'notSignedIn'
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
@@ -8,12 +8,7 @@ import type {
|
||||
import { migrateJournalDomainSchema } from './journal-domain-schema'
|
||||
import { journalDomainHistoryMetadata } from './journal-domain-history-metadata'
|
||||
import { writeJournalDomainItemNames } from './journal-domain-item-names'
|
||||
import {
|
||||
JOURNAL_DOMAIN_ITEM_READ_COLUMNS,
|
||||
JOURNAL_DOMAIN_ITEM_READ_INDEXES,
|
||||
JOURNAL_DOMAIN_TOMBSTONE_READ_COLUMNS,
|
||||
journalDomainItemReadColumns
|
||||
} from './journal-domain-item-read-columns'
|
||||
import { journalDomainItemReadColumns } from './journal-domain-item-read-columns'
|
||||
import { readJournalDomainItems, type JournalDomainItemRead } from './journal-sql-item-reads'
|
||||
|
||||
const target = { sessionId: 'chat', epoch: 'epoch' }
|
||||
@@ -28,14 +23,6 @@ function chat() {
|
||||
const db = new Database(':memory:')
|
||||
databases.push(db)
|
||||
migrateJournalDomainSchema(db)
|
||||
// TEMPORARY until the author folds the item-read fragment into frozen 001.
|
||||
for (const column of JOURNAL_DOMAIN_ITEM_READ_COLUMNS) {
|
||||
db.exec(`ALTER TABLE journal_items ADD COLUMN ${column}`)
|
||||
}
|
||||
for (const column of JOURNAL_DOMAIN_TOMBSTONE_READ_COLUMNS) {
|
||||
db.exec(`ALTER TABLE journal_item_tombstones ADD COLUMN ${column}`)
|
||||
}
|
||||
db.exec(JOURNAL_DOMAIN_ITEM_READ_INDEXES)
|
||||
db.prepare(
|
||||
`INSERT INTO journal_chat_state(session_id,epoch,last_sequence,highest_fence) VALUES(?,?,900,1)`
|
||||
).run(target.sessionId, target.epoch)
|
||||
@@ -51,8 +38,8 @@ function chat() {
|
||||
(session_id,epoch,item_id,ordinal,revision,sequence,sequence_index,owner_fence,payload_json,hidden,
|
||||
root,subagent_row,subagent_group_key,turn_marker,wire_bytes,
|
||||
kind,message_role,turn_id,opener_item_id,context_clear,goal_transition,
|
||||
producer_agent_id,producer_parent_ref,producer_attempt)
|
||||
VALUES(?,?,?,?,?,?,?,1,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
|
||||
producer_agent_id,producer_parent_ref,producer_attempt,prompt,signed_out)
|
||||
VALUES(?,?,?,?,?,?,?,1,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
item.itemId,
|
||||
@@ -83,12 +70,18 @@ function chat() {
|
||||
'INSERT INTO journal_item_aliases(session_id,epoch,provider_item_id,item_id) VALUES(?,?,?,?)'
|
||||
)
|
||||
.run(target.sessionId, target.epoch, providerItemId, itemId)
|
||||
const tombstone = (itemId: string, turnId: string | null = null) =>
|
||||
db
|
||||
.prepare(
|
||||
'INSERT INTO journal_item_tombstones(session_id,epoch,item_id,revision,turn_id) VALUES(?,?,?,9,?)'
|
||||
const tombstone = (itemId: string, turnId: string | null = null) => {
|
||||
db.prepare(
|
||||
'INSERT INTO journal_item_tombstones(session_id,epoch,item_id,revision) VALUES(?,?,?,9)'
|
||||
).run(target.sessionId, target.epoch, itemId)
|
||||
if (turnId !== null) {
|
||||
db.prepare('INSERT INTO journal_removed_turns(session_id,epoch,turn_id) VALUES(?,?,?)').run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
turnId
|
||||
)
|
||||
.run(target.sessionId, target.epoch, itemId, turnId)
|
||||
}
|
||||
}
|
||||
const send = (submission: AgentJournalSubmission, ordinal: number) =>
|
||||
db
|
||||
.prepare(`INSERT INTO journal_submission_attempts(session_id,epoch,message_id,attempt_sequence,attempt_index,
|
||||
|
||||
@@ -36,7 +36,6 @@ import {
|
||||
readJournalSqlAcceptedInputs,
|
||||
type JournalSqlAcceptedInputs
|
||||
} from './journal-sql-accepted-inputs'
|
||||
import type { JournalTextSnapshotPosition } from './writing/journal-text-snapshot-position'
|
||||
|
||||
type JournalDomainItemFields = {
|
||||
identity: AgentJournalItemIdentity
|
||||
@@ -50,7 +49,6 @@ export type JournalDomainItemWrite = JournalDomainItemFields &
|
||||
| {
|
||||
kind: 'text-snapshot'
|
||||
body: AgentJournalMessageItem
|
||||
position?: JournalTextSnapshotPosition
|
||||
}
|
||||
)
|
||||
|
||||
@@ -115,21 +113,19 @@ export function applyJournalDomainItem(
|
||||
itemId = agentJournalSubmissionKey(echo.clientMessageId)
|
||||
}
|
||||
}
|
||||
const position =
|
||||
const blindText =
|
||||
input.kind === 'text-snapshot' &&
|
||||
input.position?.itemId === itemId &&
|
||||
(input.body.role === 'assistant' || input.body.role === 'reasoning') &&
|
||||
input.body.blocks.every((block) => block.type === 'text')
|
||||
? input.position
|
||||
: undefined
|
||||
const stored = db
|
||||
.prepare(`SELECT revision, ordinal, owner_fence, execution_owner, hidden,
|
||||
CASE WHEN revision=? THEN NULL ELSE payload_json END AS payload_json FROM journal_items
|
||||
.prepare(`SELECT revision, ordinal, owner_fence, execution_owner, hidden, placement_json,
|
||||
CASE WHEN ? AND kind='message' AND message_role IN ('assistant','reasoning')
|
||||
THEN NULL ELSE payload_json END AS payload_json FROM journal_items
|
||||
WHERE session_id=? AND epoch=? AND item_id=?`)
|
||||
.get(position?.revision ?? -1, ...prefix, itemId)
|
||||
.get(Number(blindText), ...prefix, itemId)
|
||||
const previous: AgentJournalRenderItem | undefined = stored
|
||||
? stored.payload_json === null && position
|
||||
? { ...position, body: input.body }
|
||||
? stored.payload_json === null
|
||||
? { ...JSON.parse(String(stored.placement_json)), body: input.body }
|
||||
: JSON.parse(String(stored.payload_json))
|
||||
: undefined
|
||||
const item = (key: string): AgentJournalRenderItem | null => {
|
||||
@@ -145,9 +141,14 @@ export function applyJournalDomainItem(
|
||||
}
|
||||
let scope = input.options.turnScope ?? { kind: 'thread' as const }
|
||||
for (const key of input.scopeItemIds ?? []) {
|
||||
const related = item(key)
|
||||
const related = db
|
||||
.prepare(`SELECT json_extract(placement_json,'$.turnScope') AS scope_json
|
||||
FROM journal_items WHERE session_id=? AND epoch=? AND item_id=COALESCE(
|
||||
(SELECT item_id FROM journal_item_aliases WHERE session_id=? AND epoch=? AND provider_item_id=?),?)`)
|
||||
.get(...prefix, ...prefix, key, key)
|
||||
if (related) {
|
||||
scope = related.turnScope ?? { kind: 'thread' }
|
||||
scope =
|
||||
related.scope_json === null ? { kind: 'thread' } : JSON.parse(String(related.scope_json))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ const RETIRED_TABLES: readonly { name: string; key: string; payload?: readonly s
|
||||
{ name: 'journal_items', key: 'item_id', payload: ['payload_json', 'original_stop_note_json'] },
|
||||
{ name: 'journal_item_subagent_names', key: 'subagent_id,item_id' },
|
||||
{ name: 'journal_item_tombstones', key: 'item_id' },
|
||||
{ name: 'journal_removed_turns', key: 'turn_id' },
|
||||
{ name: 'journal_item_aliases', key: 'provider_item_id', payload: ['echo_json'] },
|
||||
{
|
||||
name: 'journal_submission_attempts',
|
||||
|
||||
@@ -244,14 +244,13 @@ function textMessages(steps: readonly JournalWriterStep[]): JournalSqlMessage[]
|
||||
if (step.kind !== 'resolved-item' || step.resolution.kind !== 'text-snapshot') {
|
||||
return null
|
||||
}
|
||||
const { identity, body, scopeItemIds, position } = step.resolution
|
||||
const { identity, body, scopeItemIds } = step.resolution
|
||||
messages.push({
|
||||
kind: 'text-snapshot',
|
||||
identity,
|
||||
body,
|
||||
options: step.options,
|
||||
scopeItemIds,
|
||||
position
|
||||
scopeItemIds
|
||||
})
|
||||
}
|
||||
return messages
|
||||
|
||||
@@ -150,7 +150,8 @@ export function readJournalDomainItems(
|
||||
options: {
|
||||
ordinaryMessage: body?.kind === 'message' && !body.command,
|
||||
runningTurn:
|
||||
readAgentJournalTurn(newestTurnOrClear(db, prefix)?.body)?.state === 'running'
|
||||
readAgentJournalTurn(newestJournalDomainTurnOrClear(db, prefix)?.body)?.state ===
|
||||
'running'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,8 +167,8 @@ export function readJournalDomainItems(
|
||||
scope: row ? { kind: 'turn', turnItemId: String(row.item_id) } : null,
|
||||
removed: Boolean(
|
||||
db
|
||||
.prepare(`SELECT 1 FROM journal_item_tombstones
|
||||
WHERE session_id=? AND epoch=? AND turn_id=? LIMIT 1`)
|
||||
.prepare(`SELECT 1 FROM journal_removed_turns
|
||||
WHERE session_id=? AND epoch=? AND turn_id=?`)
|
||||
.get(...prefix, operation.turnId)
|
||||
)
|
||||
}
|
||||
@@ -247,7 +248,10 @@ function visibleItem(
|
||||
}
|
||||
|
||||
/** The input order's newest turn: latest position, then the greatest item ID among ties. */
|
||||
function newestTurnOrClear(db: Database.Database, prefix: Prefix): AgentJournalRenderItem | null {
|
||||
export function newestJournalDomainTurnOrClear(
|
||||
db: Database.Database,
|
||||
prefix: Prefix
|
||||
): AgentJournalRenderItem | null {
|
||||
let newest: AgentJournalRenderItem | null = null
|
||||
for (const row of db
|
||||
.prepare(`SELECT payload_json FROM journal_items INDEXED BY journal_items_turn_marker
|
||||
|
||||
@@ -1,111 +1,105 @@
|
||||
import { join } from 'node:path'
|
||||
import { expect, it } from 'vitest'
|
||||
import { fixture } from '../agent-session-wire/publication/history/testing/agent-session-history-contract-test-fixture'
|
||||
import { afterEach, expect, it } from 'vitest'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure'
|
||||
import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { openTestJournalHostDatabase } from './store-lifecycle/journal-host-database-test-support'
|
||||
import { convertJournalSqlProjection } from './journal-sql-conversion'
|
||||
import { JournalSqlReaderPool } from './journal-sql-reader-pool'
|
||||
import { journalSqlReaderPath } from './journal-sql-worker-bundle.test-fixture'
|
||||
import { journalSqlReads } from './journal-sql-reads'
|
||||
import type { AgentSessionJournalReads } from './journal-reads'
|
||||
import { messageFixture, target } from './journal-sql-message.test-fixture'
|
||||
import { readJournalDomainLifecycle } from './journal-sql-lifecycle-reads'
|
||||
|
||||
const thread = { kind: 'thread' as const }
|
||||
const orca = (clientMessageId: string) => ({ provider: 'orca' as const, clientMessageId })
|
||||
const user = (text: string): AgentJournalMessageItem => ({
|
||||
kind: 'message',
|
||||
role: 'user',
|
||||
blocks: [{ type: 'text', text }]
|
||||
})
|
||||
const signedOut = {
|
||||
kind: 'status' as const,
|
||||
...agentSessionFailureWords(agentSessionFailureFact('notSignedIn'), { surface: 'row' })
|
||||
}
|
||||
|
||||
async function send(clientMessageId: string, text: string): Promise<void> {
|
||||
const { journal } = fixture
|
||||
await journal.appendSubmission({
|
||||
clientMessageId,
|
||||
payloadFingerprint: clientMessageId,
|
||||
body: user(text),
|
||||
fence: 1,
|
||||
handoverRecorded: true
|
||||
})
|
||||
await journal.resolveDispatch({
|
||||
clientMessageId,
|
||||
fence: 1,
|
||||
state: 'accepted',
|
||||
providerIdentity: null
|
||||
})
|
||||
}
|
||||
|
||||
it('answers lifecycle reads from one database snapshot exactly as the in-memory chat does', async () => {
|
||||
const { journal } = fixture
|
||||
await send('first', 'first prompt')
|
||||
const offered = await journal.reads.cursor()
|
||||
await send('second', 'second prompt')
|
||||
// Neither a command, a subagent's prompt nor a removed row is the chat's latest prompt.
|
||||
await journal.appendItem(
|
||||
orca('command'),
|
||||
{ ...user('/compact'), command: { name: 'compact' } },
|
||||
{ fence: 1, turnScope: thread }
|
||||
)
|
||||
await journal.appendItem(orca('child-prompt'), user('child prompt'), {
|
||||
fence: 1,
|
||||
turnScope: thread,
|
||||
agentId: 'child-agent',
|
||||
producerKind: 'agent'
|
||||
})
|
||||
await journal.appendItem(orca('removed'), user('removed prompt'), { fence: 1, turnScope: thread })
|
||||
await journal.appendTombstone(orca('removed'), { fence: 1 })
|
||||
await journal.appendItem(
|
||||
orca('turn-1'),
|
||||
{ kind: 'turn', turnId: 'turn-1', state: 'completed', startedAt: 10, completedAt: 20 },
|
||||
{ fence: 1, turnScope: thread }
|
||||
)
|
||||
// Sign-out at fence 2 is the root's; at fence 3 only a subagent reported it.
|
||||
await journal.appendItem(orca('root-signed-out'), signedOut, { fence: 2, turnScope: thread })
|
||||
await journal.appendItem(orca('child-signed-out'), signedOut, {
|
||||
fence: 3,
|
||||
turnScope: thread,
|
||||
agentId: 'child-agent',
|
||||
producerKind: 'agent'
|
||||
})
|
||||
|
||||
const answers = (reads: AgentSessionJournalReads) =>
|
||||
Promise.all([
|
||||
reads.latestPrompt(),
|
||||
reads.restartProgress(offered),
|
||||
reads.outgrownChild({ fence: 2, nextClientMessageId: 'second' }),
|
||||
reads.outgrownChild({ fence: 3, nextClientMessageId: 'turn-1' }),
|
||||
reads.outgrownChild({ fence: 1, nextClientMessageId: 'missing' }),
|
||||
reads.outgrownChild({ fence: 4 })
|
||||
])
|
||||
const memory = await answers(journal.reads)
|
||||
const [prompt, progress, rootFence, childFence, oldFence, unknownFence] = memory
|
||||
expect(prompt).toBe('second prompt')
|
||||
expect(progress.epoch).toBe(offered.epoch)
|
||||
expect(progress.acceptedInputs.map((input) => input.clientMessageId)).toEqual(['second'])
|
||||
expect(rootFence).toEqual({
|
||||
signedOut: true,
|
||||
newestTurnCompletedAt: 20,
|
||||
nextMessage: user('second prompt')
|
||||
})
|
||||
expect(childFence).toEqual({ signedOut: false, newestTurnCompletedAt: 20, nextMessage: null })
|
||||
expect(oldFence.signedOut).toBe(false)
|
||||
expect(unknownFence).toEqual({ signedOut: false, newestTurnCompletedAt: 20, nextMessage: null })
|
||||
|
||||
const database = openTestJournalHostDatabase(fixture.root)
|
||||
database.transaction((db) => convertJournalSqlProjection(db, 'session-1'))
|
||||
const pool = new JournalSqlReaderPool(
|
||||
join(fixture.root, 'agent-session-journal.db'),
|
||||
join(fixture.root, 'agent-session-journal.db'),
|
||||
journalSqlReaderPath()
|
||||
)
|
||||
try {
|
||||
expect(await answers(journalSqlReads(pool, 'session-1'))).toEqual(memory)
|
||||
} finally {
|
||||
await pool.close()
|
||||
const databases: Database.Database[] = []
|
||||
afterEach(() => {
|
||||
for (const db of databases.splice(0)) {
|
||||
db.close()
|
||||
}
|
||||
})
|
||||
|
||||
it('reads prompt, accepted handovers and root sign-out from the current chat snapshot', () => {
|
||||
const { db, seedItem, seedSend } = messageFixture()
|
||||
databases.push(db)
|
||||
seedSend()
|
||||
const signedOut = {
|
||||
kind: 'status' as const,
|
||||
...agentSessionFailureWords(agentSessionFailureFact('notSignedIn'), { surface: 'row' })
|
||||
}
|
||||
seedItem({
|
||||
itemId: 'command',
|
||||
revision: 1,
|
||||
sequence: 8,
|
||||
observedAt: 8,
|
||||
body: { kind: 'message', role: 'user', command: { name: 'compact' }, blocks: [] }
|
||||
})
|
||||
seedItem({
|
||||
itemId: 'child-prompt',
|
||||
revision: 1,
|
||||
sequence: 9,
|
||||
observedAt: 9,
|
||||
agentId: 'child',
|
||||
producerKind: 'agent',
|
||||
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'child' }] }
|
||||
})
|
||||
seedItem({
|
||||
itemId: 'removed',
|
||||
revision: 1,
|
||||
sequence: 10,
|
||||
observedAt: 10,
|
||||
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'removed' }] }
|
||||
})
|
||||
db.prepare('UPDATE journal_items SET hidden=1 WHERE item_id=?').run('removed')
|
||||
seedItem({
|
||||
itemId: 'turn',
|
||||
revision: 1,
|
||||
sequence: 11,
|
||||
observedAt: 11,
|
||||
body: { kind: 'turn', turnId: 'turn', state: 'completed', completedAt: 20 }
|
||||
})
|
||||
seedItem({ itemId: 'root-signout', revision: 1, sequence: 12, observedAt: 12, body: signedOut })
|
||||
db.prepare('UPDATE journal_items SET execution_owner=2 WHERE item_id=?').run('root-signout')
|
||||
seedItem({
|
||||
itemId: 'child-signout',
|
||||
revision: 1,
|
||||
sequence: 13,
|
||||
observedAt: 13,
|
||||
agentId: 'child',
|
||||
producerKind: 'agent',
|
||||
body: signedOut
|
||||
})
|
||||
db.prepare('UPDATE journal_items SET execution_owner=3 WHERE item_id=?').run('child-signout')
|
||||
|
||||
db.exec('BEGIN')
|
||||
const read = (operation: Parameters<typeof readJournalDomainLifecycle>[2]) =>
|
||||
readJournalDomainLifecycle(db, target, operation)
|
||||
expect(read({ kind: 'latest-prompt' })).toEqual({ kind: 'latest-prompt', prompt: 'original' })
|
||||
const progress = read({ kind: 'restart-progress', cursor: { epoch: 'epoch', sequence: 0 } })
|
||||
expect(progress).toMatchObject({
|
||||
kind: 'restart-progress',
|
||||
progress: {
|
||||
epoch: 'epoch',
|
||||
acceptedInputs: [{ clientMessageId: 'send', dispatchState: 'unknown', acceptedSequence: 1 }]
|
||||
}
|
||||
})
|
||||
expect(read({ kind: 'restart-progress', cursor: { epoch: 'epoch', sequence: 1 } })).toEqual({
|
||||
kind: 'restart-progress',
|
||||
progress: { epoch: 'epoch', acceptedInputs: [] }
|
||||
})
|
||||
expect(
|
||||
read({ kind: 'outgrown-child', input: { fence: 2, nextClientMessageId: 'send' } })
|
||||
).toEqual({
|
||||
kind: 'outgrown-child',
|
||||
child: {
|
||||
signedOut: true,
|
||||
newestTurnCompletedAt: 20,
|
||||
nextMessage: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'original' }] }
|
||||
}
|
||||
})
|
||||
expect(read({ kind: 'outgrown-child', input: { fence: 3 } })).toEqual({
|
||||
kind: 'outgrown-child',
|
||||
child: { signedOut: false, newestTurnCompletedAt: 20, nextMessage: null }
|
||||
})
|
||||
db.prepare(
|
||||
'UPDATE journal_submission_attempts SET dispatch_fence=4,signed_out=1 WHERE message_id=?'
|
||||
).run('send')
|
||||
expect(read({ kind: 'outgrown-child', input: { fence: 4 } })).toEqual({
|
||||
kind: 'outgrown-child',
|
||||
child: { signedOut: true, newestTurnCompletedAt: 20, nextMessage: null }
|
||||
})
|
||||
db.exec('COMMIT')
|
||||
})
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
|
||||
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { latestStructuredAgentSessionPrompt } from '../../../shared/agent-session/requests/structured-agent-session-latest-request'
|
||||
import {
|
||||
journalStatusReportsSignedOut,
|
||||
readJournalOutgrownChild,
|
||||
readJournalRestartProgress,
|
||||
type JournalLifecycleSnapshot,
|
||||
type JournalOutgrownChild,
|
||||
type JournalOutgrownChildRead,
|
||||
type JournalRestartProgress
|
||||
import { readAgentJournalTurn } from '../../../shared/agent-session/lifecycle/agent-session-turn-record'
|
||||
import { agentJournalSubmissionKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { journalDomainContextHead } from './journal-sql-continuation-context'
|
||||
import { newestJournalDomainTurnOrClear } from './journal-sql-item-reads'
|
||||
import type {
|
||||
JournalOutgrownChild,
|
||||
JournalOutgrownChildRead,
|
||||
JournalRestartProgress
|
||||
} from './journal-lifecycle-reads'
|
||||
import { readJournalSqlInput } from './journal-sql-input-reads'
|
||||
import type { JournalSqlItemReadContext } from './journal-sql-item-reads'
|
||||
import { sqlJournalStatusItems } from './journal-sql-status-items'
|
||||
|
||||
export type JournalSqlLifecycleRead =
|
||||
| { kind: 'latest-prompt' }
|
||||
@@ -23,49 +22,65 @@ export type JournalSqlLifecycleReadResult =
|
||||
| { kind: 'restart-progress'; progress: JournalRestartProgress }
|
||||
| { kind: 'outgrown-child'; child: JournalOutgrownChild }
|
||||
|
||||
// Execution ownership wins over creation provenance, as journalItemExecutionFence reads it.
|
||||
const ROOT_STATUSES_AT_FENCE = `SELECT item.entry_key FROM journal_proj_items item
|
||||
LEFT JOIN journal_proj_item_execution_owners owner ON owner.session_id=item.session_id
|
||||
AND owner.epoch=item.epoch AND owner.item_id=json_extract(item.entry_key, '$')
|
||||
LEFT JOIN journal_proj_item_fences created ON created.session_id=item.session_id
|
||||
AND created.epoch=item.epoch AND created.item_id=json_extract(item.entry_key, '$')
|
||||
WHERE item.session_id=? AND item.epoch=? AND item.kind='status' AND item.root=1
|
||||
AND item.hidden=0 AND coalesce(owner.fence, created.fence)=?`
|
||||
|
||||
/** Each answer comes from the one snapshot the point read opened; only named candidates decode. */
|
||||
export function readJournalSqlLifecycle(
|
||||
context: JournalSqlItemReadContext,
|
||||
/** Runs synchronously in the caller's existing reader snapshot. */
|
||||
export function readJournalDomainLifecycle(
|
||||
db: Database.Database,
|
||||
target: { sessionId: string; epoch: string },
|
||||
operation: JournalSqlLifecycleRead
|
||||
): JournalSqlLifecycleReadResult {
|
||||
const { scope } = context
|
||||
const prefix = [target.sessionId, target.epoch] as const
|
||||
if (operation.kind === 'latest-prompt') {
|
||||
const prompt = sqlJournalStatusItems(scope, context.item, { kind: 'prompt' })
|
||||
[Symbol.iterator]()
|
||||
.next().value?.item
|
||||
const row = db
|
||||
.prepare(`SELECT payload_json FROM journal_items
|
||||
WHERE session_id=? AND epoch=? AND hidden=0 AND prompt=1
|
||||
ORDER BY sequence DESC,ordinal DESC LIMIT 1`)
|
||||
.get(...prefix)
|
||||
return {
|
||||
kind: 'latest-prompt',
|
||||
prompt: prompt ? latestStructuredAgentSessionPrompt([prompt]) : ''
|
||||
prompt: row ? latestStructuredAgentSessionPrompt([JSON.parse(String(row.payload_json))]) : ''
|
||||
}
|
||||
}
|
||||
const snapshot: JournalLifecycleSnapshot = {
|
||||
epoch: context.epoch,
|
||||
input: (read) => readJournalSqlInput(scope, read),
|
||||
itemBody: (itemId) => (context.visible(itemId) ? context.item(itemId)?.body : null),
|
||||
rootStatusSignedOutAt: (fence) => {
|
||||
for (const row of scope.db.prepare(ROOT_STATUSES_AT_FENCE).iterate(...scope.prefix, fence)) {
|
||||
scope.examined(row)
|
||||
const item = context.item(String(JSON.parse(String(row.entry_key))))
|
||||
if (item && journalStatusReportsSignedOut(item.body, item)) {
|
||||
return true
|
||||
}
|
||||
if (operation.kind === 'restart-progress') {
|
||||
return {
|
||||
kind: 'restart-progress',
|
||||
progress: {
|
||||
epoch: journalDomainContextHead(db, target).epoch,
|
||||
acceptedInputs: db
|
||||
.prepare(`SELECT payload_json FROM journal_submission_attempts
|
||||
WHERE session_id=? AND epoch=? AND current=1 AND accepted_sequence>?
|
||||
ORDER BY accepted_sequence,ordinal`)
|
||||
.all(...prefix, operation.cursor.sequence)
|
||||
.map((row) => JSON.parse(String(row.payload_json)))
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return operation.kind === 'restart-progress'
|
||||
? {
|
||||
kind: 'restart-progress',
|
||||
progress: readJournalRestartProgress(snapshot, operation.cursor)
|
||||
}
|
||||
: { kind: 'outgrown-child', child: readJournalOutgrownChild(snapshot, operation.input) }
|
||||
const { fence, nextClientMessageId } = operation.input
|
||||
const signedOut = Boolean(
|
||||
db
|
||||
.prepare(`SELECT 1 FROM journal_submission_attempts
|
||||
WHERE session_id=? AND epoch=? AND current=1 AND signed_out=1 AND dispatch_fence=? LIMIT 1`)
|
||||
.get(...prefix, fence) ||
|
||||
db
|
||||
.prepare(`SELECT 1 FROM journal_items
|
||||
WHERE session_id=? AND epoch=? AND hidden=0 AND root=1 AND signed_out=1
|
||||
AND COALESCE(execution_owner,owner_fence)=? LIMIT 1`)
|
||||
.get(...prefix, fence)
|
||||
)
|
||||
const next =
|
||||
nextClientMessageId === undefined
|
||||
? undefined
|
||||
: db
|
||||
.prepare(`SELECT payload_json
|
||||
FROM journal_items WHERE session_id=? AND epoch=? AND item_id=? AND hidden=0`)
|
||||
.get(...prefix, agentJournalSubmissionKey(nextClientMessageId))
|
||||
const body = next ? (JSON.parse(String(next.payload_json)) as AgentJournalRenderItem).body : null
|
||||
return {
|
||||
kind: 'outgrown-child',
|
||||
child: {
|
||||
signedOut,
|
||||
newestTurnCompletedAt: readAgentJournalTurn(newestJournalDomainTurnOrClear(db, prefix)?.body)
|
||||
?.completedAt,
|
||||
nextMessage: body?.kind === 'message' ? body : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
} from './journal-domain-item'
|
||||
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import type { JournalStepOutcome } from './writing/journal-writer-step'
|
||||
import { journalTextSnapshotPosition } from './writing/journal-text-snapshot-position'
|
||||
import { journalSqlFailure, type JournalSqlFailure } from './journal-sql-failure'
|
||||
|
||||
export type JournalSqlMessage = JournalDomainItemWrite & { body: AgentJournalMessageItem }
|
||||
@@ -77,12 +76,7 @@ function applyMessageSteps(
|
||||
const commit = applyJournalDomainItem(db, target, step, savedAt)
|
||||
db.exec('RELEASE journal_message_step')
|
||||
result.cursor = commit.cursor
|
||||
const item = commit.items[0]
|
||||
result.outcomes.push(
|
||||
commit.outcome.kind === 'committed' && item
|
||||
? { ...commit.outcome, position: journalTextSnapshotPosition(item) }
|
||||
: commit.outcome
|
||||
)
|
||||
result.outcomes.push(commit.outcome)
|
||||
result.items.push(...commit.items)
|
||||
result.receipts.push(...commit.receipts)
|
||||
result.cards.push(...commit.cards)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { journalDomainItemReadColumns } from './journal-domain-item-read-columns'
|
||||
import { journalDomainItemPlacement } from './journal-domain-item-placement'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import type {
|
||||
AgentJournalRenderItem,
|
||||
@@ -59,7 +61,7 @@ export function messageFixture(db = new Database(':memory:')) {
|
||||
const metadata = journalDomainHistoryMetadata(item, payload)
|
||||
db.prepare(`INSERT INTO journal_items
|
||||
(session_id,epoch,item_id,ordinal,revision,sequence,sequence_index,owner_fence,payload_json,
|
||||
root,subagent_row,subagent_group_key,turn_marker,wire_bytes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
|
||||
root,subagent_row,subagent_group_key,turn_marker,wire_bytes,placement_json,kind,message_role,turn_id,opener_item_id,context_clear,goal_transition,producer_agent_id,producer_parent_ref,producer_attempt,prompt,signed_out) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
|
||||
target.sessionId,
|
||||
target.epoch,
|
||||
item.itemId,
|
||||
@@ -73,7 +75,9 @@ export function messageFixture(db = new Database(':memory:')) {
|
||||
metadata.subagentRow,
|
||||
metadata.subagentGroupKey,
|
||||
metadata.turnMarker,
|
||||
metadata.wireBytes
|
||||
metadata.wireBytes,
|
||||
journalDomainItemPlacement(item),
|
||||
...journalDomainItemReadColumns(item)
|
||||
)
|
||||
writeJournalDomainItemNames(
|
||||
db,
|
||||
|
||||
@@ -3,7 +3,9 @@ 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 { journalDomainItemPlacement } from './journal-domain-item-placement'
|
||||
import { applyJournalDomainItem } from './journal-domain-item'
|
||||
import { readJournalDomainItems } from './journal-sql-item-reads'
|
||||
import { writeJournalDomainCurrentItem } from './journal-domain-current-item'
|
||||
|
||||
const databases: Database.Database[] = []
|
||||
@@ -304,15 +306,7 @@ it('retains a stream placement while honoring a newer moved row and a settled tu
|
||||
if (!first?.committed) {
|
||||
throw new Error('stream start did not commit')
|
||||
}
|
||||
const position = first.value.outcomes[0]
|
||||
if (position?.kind !== 'committed' || !position.position) {
|
||||
throw new Error('stream start returned no placement')
|
||||
}
|
||||
const second = {
|
||||
...message('second'),
|
||||
kind: 'text-snapshot' as const,
|
||||
position: position.position
|
||||
}
|
||||
const second = { ...message('second'), kind: 'text-snapshot' as const }
|
||||
await commit(second)
|
||||
expect(item('orca:assistant')).toMatchObject({
|
||||
revision: 2,
|
||||
@@ -328,8 +322,8 @@ it('retains a stream placement while honoring a newer moved row and a settled tu
|
||||
observedAt: 50,
|
||||
turnScope: { kind: 'turn' as const, turnItemId: 'orca:turn' }
|
||||
}
|
||||
db.prepare(`UPDATE journal_items SET revision=3,sequence=8,sequence_index=2,payload_json=?
|
||||
WHERE item_id='orca:assistant'`).run(JSON.stringify(moved))
|
||||
db.prepare(`UPDATE journal_items SET revision=3,sequence=8,sequence_index=2,payload_json=?,placement_json=?
|
||||
WHERE item_id='orca:assistant'`).run(JSON.stringify(moved), journalDomainItemPlacement(moved))
|
||||
await commit(second)
|
||||
expect(item('orca:assistant')).toMatchObject({
|
||||
revision: 4,
|
||||
@@ -355,3 +349,32 @@ it('retains a stream placement while honoring a newer moved row and a settled tu
|
||||
})
|
||||
expect(item('orca:assistant')?.revision).toBe(4)
|
||||
})
|
||||
|
||||
it('retains removed turn identity when its item is reinserted', () => {
|
||||
const { db, item } = fixture()
|
||||
db.prepare(`INSERT INTO journal_item_tombstones(session_id,epoch,item_id,revision)
|
||||
VALUES(?,?,?,9)`).run(target.sessionId, target.epoch, 'orca:turn')
|
||||
db.prepare(`INSERT INTO journal_removed_turns(session_id,epoch,turn_id)
|
||||
VALUES(?,?,?)`).run(target.sessionId, target.epoch, 'removed-turn')
|
||||
db.exec('BEGIN')
|
||||
applyJournalDomainItem(
|
||||
db,
|
||||
target,
|
||||
{
|
||||
kind: 'item',
|
||||
identity: { provider: 'orca', clientMessageId: 'turn' },
|
||||
body: { kind: 'turn', turnId: 'removed-turn', state: 'completed' },
|
||||
options: { fence: 1, turnScope: { kind: 'thread' } }
|
||||
},
|
||||
100
|
||||
)
|
||||
db.exec('COMMIT')
|
||||
expect(item('orca:turn')).toMatchObject({ revision: 10 })
|
||||
expect(db.prepare('SELECT 1 FROM journal_item_tombstones').get()).toBeUndefined()
|
||||
expect(
|
||||
readJournalDomainItems(db, target, { kind: 'named-turn', turnId: 'removed-turn' })
|
||||
).toEqual({
|
||||
kind: 'named-turn',
|
||||
turn: { scope: { kind: 'turn', turnItemId: 'orca:turn' }, removed: true }
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readJournalDomainLifecycle } from './journal-sql-lifecycle-reads'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import type { JournalObserverRead, JournalObserverResult } from './journal-observer-read'
|
||||
import { readJournalSqlObserver } from './journal-sql-observer-read'
|
||||
@@ -45,7 +46,7 @@ import {
|
||||
type JournalSqlQueueReadResult
|
||||
} from './journal-sql-queued-messages'
|
||||
import {
|
||||
readJournalSqlItems,
|
||||
readJournalDomainItems,
|
||||
type JournalSqlItemRead,
|
||||
type JournalSqlItemReadResult
|
||||
} from './journal-sql-item-reads'
|
||||
@@ -138,12 +139,6 @@ function read(
|
||||
values.has('inputs.presentations', identity.clientMessageId)
|
||||
)
|
||||
}
|
||||
const items = {
|
||||
scope,
|
||||
item: (itemId: string) => values.get<AgentJournalRenderItem>('items', itemId),
|
||||
visible,
|
||||
epoch
|
||||
}
|
||||
switch (operation.kind) {
|
||||
case 'work':
|
||||
return {
|
||||
@@ -190,10 +185,19 @@ function read(
|
||||
case 'history-items':
|
||||
case 'claude-history-prompts':
|
||||
case 'canonical-item-ids':
|
||||
return readJournalDomainItems(
|
||||
scope.db,
|
||||
{ sessionId: scope.sessionId, epoch: scope.epoch },
|
||||
operation
|
||||
)
|
||||
case 'latest-prompt':
|
||||
case 'restart-progress':
|
||||
case 'outgrown-child':
|
||||
return readJournalSqlItems(items, operation)
|
||||
return readJournalDomainLifecycle(
|
||||
scope.db,
|
||||
{ sessionId: scope.sessionId, epoch: scope.epoch },
|
||||
operation
|
||||
)
|
||||
case 'metadata': {
|
||||
const sequence = scalars.get('lastSequence', 0)
|
||||
return {
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import type { AgentJournalRenderItem } from '../../../../shared/agent-session/journal/agent-session-journal-types'
|
||||
|
||||
/** The stream retains placement, never a second copy of its saved text. */
|
||||
export type JournalTextSnapshotPosition = Omit<AgentJournalRenderItem, 'body'>
|
||||
|
||||
export function journalTextSnapshotPosition(
|
||||
item: AgentJournalRenderItem
|
||||
): JournalTextSnapshotPosition | undefined {
|
||||
if (
|
||||
item.body.kind !== 'message' ||
|
||||
(item.body.role !== 'assistant' && item.body.role !== 'reasoning') ||
|
||||
item.body.blocks.some((block) => block.type !== 'text')
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
const { body: _body, ...position } = item
|
||||
return position
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import type { AgentJournalTurnScope } from '../../../../shared/agent-session/jou
|
||||
import { readAgentJournalTurn } from '../../../../shared/agent-session/lifecycle/agent-session-turn-record'
|
||||
import type { JournalResolvedItem } from './journal-item-appender'
|
||||
import type { JournalTimelineView } from './journal-timeline-view'
|
||||
import type { JournalTextSnapshotPosition } from './journal-text-snapshot-position'
|
||||
import { requiresTerminalSettlement } from '../item-lifecycle/journal-terminal-settlement'
|
||||
import { relightsProviderTimelineBackgroundTask } from '../../agent-session-timeline/provider-timeline-background-tasks'
|
||||
import {
|
||||
@@ -40,7 +39,6 @@ type TimelineItemResolution =
|
||||
kind: 'text-snapshot'
|
||||
identity: AgentJournalItemIdentity
|
||||
body: AgentJournalMessageItem
|
||||
position?: JournalTextSnapshotPosition
|
||||
}
|
||||
| {
|
||||
kind: 'guarded-item'
|
||||
|
||||
@@ -8,7 +8,6 @@ import type { JournalSqlFailure } from '../journal-sql-failure'
|
||||
import type { JournalLifecycleIdentityMutationInput } from './journal-lifecycle-mutation'
|
||||
import type { JournalTimelineItemResolution } from './journal-timeline-resolution'
|
||||
import type { ProviderTimelineSettlementResolution } from '../../agent-session-timeline/provider-timeline-writer-resolution'
|
||||
import type { JournalTextSnapshotPosition } from './journal-text-snapshot-position'
|
||||
|
||||
export type JournalHistoryResolution = {
|
||||
kind: 'history-catch-up'
|
||||
@@ -52,7 +51,6 @@ export type JournalStepOutcome =
|
||||
kind: 'committed'
|
||||
wrote: boolean
|
||||
stopped?: true
|
||||
position?: JournalTextSnapshotPosition
|
||||
}
|
||||
| {
|
||||
kind: 'dropped'
|
||||
|
||||
Reference in New Issue
Block a user