diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts index 1c0f19aed37..7ea3ca50708 100644 --- a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts @@ -65,6 +65,12 @@ CREATE INDEX journal_items_user_message ON journal_items(session_id, epoch, ordi 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_provider_turn ON journal_items(session_id,epoch,provider_turn_id,sequence,sequence_index,ordinal) + WHERE provider_turn_id IS NOT NULL; +CREATE INDEX journal_items_codex_turn ON journal_items(session_id,epoch,codex_thread_id,codex_turn_id,sequence,sequence_index,ordinal) + WHERE hidden=0 AND codex_turn_id IS NOT NULL; +CREATE INDEX journal_submission_codex_turn ON journal_submission_attempts(session_id,epoch,codex_thread_id,codex_turn_id,item_id) + WHERE current=1 AND dispatch_state='accepted'; 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) diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-outline.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-outline.ts new file mode 100644 index 00000000000..0cd04c60a5f --- /dev/null +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-outline.ts @@ -0,0 +1,33 @@ +export const JOURNAL_DOMAIN_SCHEMA_001_OUTLINE = ` +CREATE TABLE journal_outline_members ( + session_id TEXT NOT NULL, + epoch TEXT NOT NULL, + item_id TEXT NOT NULL, + source_revision INTEGER NOT NULL, + sequence INTEGER NOT NULL, + sequence_index INTEGER NOT NULL, + ordinal INTEGER NOT NULL, + structure_json TEXT NOT NULL, + turn_key TEXT, + reply TEXT, + entry_json TEXT, + short_json TEXT, + empty_json TEXT, + entry_bytes INTEGER NOT NULL, + short_bytes INTEGER NOT NULL, + empty_bytes INTEGER NOT NULL, + PRIMARY KEY(session_id,epoch,item_id) +) WITHOUT ROWID; +CREATE INDEX journal_outline_entries ON journal_outline_members(session_id,epoch,sequence,sequence_index,ordinal) + WHERE entry_json IS NOT NULL; +CREATE INDEX journal_outline_reply_candidates ON journal_outline_members(session_id,epoch,turn_key,sequence DESC,sequence_index DESC,ordinal DESC) + WHERE reply IS NOT NULL; +CREATE TABLE journal_outline_replies ( + session_id TEXT NOT NULL, + epoch TEXT NOT NULL, + turn_key TEXT NOT NULL, + reply TEXT NOT NULL, + reply_bytes INTEGER NOT NULL, + PRIMARY KEY(session_id,epoch,turn_key) +) WITHOUT ROWID; +` diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts index 22d5f340e43..de968cac712 100644 --- a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts @@ -1,3 +1,4 @@ +import { JOURNAL_DOMAIN_SCHEMA_001_OUTLINE } from './journal-domain-schema-001-outline' import { JOURNAL_DOMAIN_SCHEMA_001_INDEXES } from './journal-domain-schema-001-indexes' export const JOURNAL_DOMAIN_SCHEMA_001 = ` @@ -120,6 +121,9 @@ CREATE TABLE journal_items ( kind TEXT, message_role TEXT, turn_id TEXT, + provider_turn_id TEXT, + codex_thread_id TEXT, + codex_turn_id TEXT, opener_item_id TEXT, context_clear INTEGER NOT NULL DEFAULT 0, goal_transition INTEGER NOT NULL DEFAULT 0, @@ -202,6 +206,12 @@ CREATE TABLE journal_submission_attempts ( covers_stopped_history INTEGER NOT NULL DEFAULT 0, fingerprint TEXT NOT NULL, dispatch_state TEXT NOT NULL, + provider_item_id TEXT GENERATED ALWAYS AS ( + CASE WHEN json_valid(payload_json) THEN json_extract(payload_json,'$.providerItemId') END + ) VIRTUAL, + codex_thread_id TEXT, + codex_turn_id TEXT, + queued INTEGER NOT NULL DEFAULT 0, dispatch_fence INTEGER NOT NULL DEFAULT 0, signed_out INTEGER NOT NULL DEFAULT 0 CHECK(signed_out IN (0,1)), recovered INTEGER NOT NULL DEFAULT 0 CHECK(recovered IN (0,1)), @@ -270,36 +280,6 @@ CREATE TABLE journal_maintenance ( id INTEGER PRIMARY KEY CHECK(id=1), compacted_at INTEGER NOT NULL ); -CREATE TABLE journal_outline_members ( - session_id TEXT NOT NULL, - epoch TEXT NOT NULL, - item_id TEXT NOT NULL, - source_revision INTEGER NOT NULL, - sequence INTEGER NOT NULL, - sequence_index INTEGER NOT NULL, - ordinal INTEGER NOT NULL, - structure_json TEXT NOT NULL, - turn_key TEXT, - reply TEXT, - entry_json TEXT, - short_json TEXT, - empty_json TEXT, - entry_bytes INTEGER NOT NULL, - short_bytes INTEGER NOT NULL, - empty_bytes INTEGER NOT NULL, - PRIMARY KEY(session_id,epoch,item_id) -) WITHOUT ROWID; -CREATE INDEX journal_outline_entries ON journal_outline_members(session_id,epoch,sequence,sequence_index,ordinal) - WHERE entry_json IS NOT NULL; -CREATE INDEX journal_outline_reply_candidates ON journal_outline_members(session_id,epoch,turn_key,sequence DESC,sequence_index DESC,ordinal DESC) - WHERE reply IS NOT NULL; -CREATE TABLE journal_outline_replies ( - session_id TEXT NOT NULL, - epoch TEXT NOT NULL, - turn_key TEXT NOT NULL, - reply TEXT NOT NULL, - reply_bytes INTEGER NOT NULL, - PRIMARY KEY(session_id,epoch,turn_key) -) WITHOUT ROWID; +${JOURNAL_DOMAIN_SCHEMA_001_OUTLINE} ${JOURNAL_DOMAIN_SCHEMA_001_INDEXES} ` diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts b/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts index b91b8476e27..83d8ba88d76 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts @@ -14,6 +14,7 @@ import { } from './journal-item-status-metadata' import { lostLiveWorkJournalBody } from './item-lifecycle/journal-subagent-liveness' import { isUnverifiedEndAgentJournalToolCall } from '../../../shared/agent-journal-tool-call-lifecycle' +import { parseAgentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' export function journalDomainItemLifecycleColumns( item: AgentJournalRenderItem @@ -108,13 +109,18 @@ export const JOURNAL_DOMAIN_ITEM_COLUMNS = [ 'turn_completed_at', 'command_entry', 'assistant_prose', - 'turn_scope_kind' + 'turn_scope_kind', + 'provider_turn_id', + 'codex_thread_id', + 'codex_turn_id' ] as const /** One payload projection supplies import, current writes and derived Stop revisions. */ export function journalDomainItemColumns(item: AgentJournalRenderItem, payload: string) { const metadata = journalDomainHistoryMetadata(item, payload) const [, commandEntry, , assistantProse, turnScopeKind] = journalItemStatusMetadata(item) + const turn = readAgentJournalTurn(item.body) + const identity = parseAgentJournalItemKey(item.itemId) return { metadata, values: [ @@ -131,7 +137,10 @@ export function journalDomainItemColumns(item: AgentJournalRenderItem, payload: ...journalDomainItemLifecycleColumns(item), commandEntry, assistantProse, - turnScopeKind + turnScopeKind, + turn?.providerTurnId ?? turn?.turnId ?? null, + identity?.provider === 'codex' ? identity.threadId : null, + identity?.provider === 'codex' ? identity.turnId : null ] } } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-rewind-cut.ts b/src/main/native-chat/agent-session-journal/journal-domain-rewind-cut.ts index b5bb3e83b7c..e7056c57847 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-rewind-cut.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-rewind-cut.ts @@ -1,5 +1,17 @@ import { parseAgentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' import type { JournalSqlScope } from './journal-sql-scope' +import { + JOURNAL_REWIND_CONTEXT, + JOURNAL_REWIND_JOINS, + JOURNAL_REWIND_NATIVE_TURN, + JOURNAL_REWIND_THREAD, + rewindMember, + rewindPosition, + rewindContextBindings, + rewindRetainedTurns, + rewindKept, + type JournalRewindMembership +} from './journal-domain-rewind-membership' export type JournalRewindSelection = { itemId: string; provider: string; nativeId: string } export type JournalRewindCut = { @@ -10,113 +22,121 @@ export type JournalRewindCut = { | { beforeMessage: { sessionId: string; uuid: string } } ) -/** Provider membership and placement suffice; transcript bodies never leave the database. */ +/** Provider indexes find the boundary; only its fixed-size plan crosses the reader channel. */ export function planJournalDomainRewindCut(scope: JournalSqlScope, target: JournalRewindSelection) { const { db, prefix } = scope const floor = Number( db .prepare(`SELECT MAX(sequence) AS sequence FROM journal_items - INDEXED BY journal_items_context_clear - WHERE session_id=? AND epoch=? AND hidden=0 AND context_clear=1`) + INDEXED BY journal_items_context_clear + WHERE session_id=? AND epoch=? AND hidden=0 AND context_clear=1`) .get(...prefix)!.sequence ?? 0 ) - const rows = db - .prepare(`SELECT item.item_id,item.sequence,item.sequence_index,item.ordinal, - item.turn_scope_kind,item.turn_item_id, - CASE WHEN item.turn_id IS NOT NULL - THEN COALESCE(json_extract(item.payload_json,'$.body.providerTurnId'),item.turn_id) END AS own_turn, - CASE WHEN owner.turn_id IS NOT NULL - THEN COALESCE(json_extract(owner.payload_json,'$.body.providerTurnId'),owner.turn_id) END AS scoped_turn, - CASE WHEN send.dispatch_state='accepted' - THEN json_extract(send.payload_json,'$.providerItemId') END AS provider_item_id, - CASE WHEN send.dispatch_state='rejected' OR - (send.dispatch_state='pending' AND json_extract(send.payload_json,'$.handoverRecorded')=1 - AND json_extract(send.payload_json,'$.handedOverAt') IS NULL) THEN 1 ELSE 0 END AS omitted - FROM journal_items item INDEXED BY journal_items_visible_position - LEFT JOIN journal_items owner ON owner.session_id=item.session_id AND owner.epoch=item.epoch - AND owner.item_id=item.turn_item_id - LEFT JOIN journal_submission_attempts send ON send.session_id=item.session_id AND send.epoch=item.epoch - AND send.item_id=item.item_id AND send.current=1 - WHERE item.session_id=? AND item.epoch=? AND item.hidden=0 - ORDER BY item.sequence,item.sequence_index,item.ordinal`) - .all(...prefix) - .map((row) => { - scope.examined(row) - const itemId = String(row.item_id) - const providerItemId = row.provider_item_id === null ? itemId : String(row.provider_item_id) - const identity = parseAgentJournalItemKey(providerItemId) - return { - itemId, - sequence: Number(row.sequence), - identity, - providerItemId, - omitted: Boolean(row.omitted), - scopedTurn: row.turn_scope_kind === 'turn' ? String(row.turn_item_id) : undefined, - turn: - identity?.provider === 'codex' && identity.threadId === target.nativeId - ? identity.turnId - : row.turn_scope_kind === 'turn' - ? row.scoped_turn === null - ? undefined - : String(row.scoped_turn) - : row.own_turn === null - ? undefined - : String(row.own_turn) - } - }) - const active = rows.filter((row) => row.sequence > floor) - const selected = active.find((row) => row.itemId === target.itemId) - const key = selected?.identity - if (!selected || !key || (key.provider !== 'claude' && key.provider !== 'codex')) { + const selected = db + .prepare(`SELECT item.sequence,item.sequence_index,item.ordinal, + CASE WHEN send.dispatch_state='accepted' THEN COALESCE(send.provider_item_id,item.item_id) + ELSE item.item_id END AS provider_item_id + FROM journal_items item ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=? AND item.epoch=? AND item.item_id=? AND item.hidden=0 AND item.sequence>?`) + .get(...prefix, target.itemId, floor) + if (!selected) { return null } - const claude = key.provider === 'claude' && target.provider === 'claude' + scope.examined(selected) + const providerItemId = String(selected.provider_item_id) + const key = parseAgentJournalItemKey(providerItemId) + const claude = key?.provider === 'claude' && target.provider === 'claude' if ( !claude && - (key.provider !== 'codex' || target.provider !== 'codex' || key.threadId !== target.nativeId) + (key?.provider !== 'codex' || target.provider !== 'codex' || key.threadId !== target.nativeId) ) { return null } - const boundary = active.findIndex((row) => + const membership: JournalRewindMembership = { + floor, + boundary: rewindPosition(selected), + nativeId: target.nativeId, claude - ? row.itemId === target.itemId - : row.turn === (key.provider === 'codex' ? key.turnId : undefined) - ) - const membership = (row: (typeof rows)[number]) => (claude ? row.scopedTurn : row.turn) - const keptTurns = new Set(active.slice(0, boundary).flatMap((row) => membership(row) ?? [])) - const kept = new Set( - active.flatMap((row, index) => { - const turn = membership(row) - return !row.omitted && (index < boundary || (turn !== undefined && keptTurns.has(turn))) - ? [row.itemId] - : [] - }) - ) - let cut: JournalRewindCut - if (claude && key.provider === 'claude') { - cut = { + } + if (claude && key?.provider === 'claude') { + const cut: JournalRewindCut = { floor, - providerItemId: selected.providerItemId, + providerItemId, beforeMessage: { sessionId: key.sessionId, uuid: key.uuid } } - } else if (key.provider === 'codex') { - const seen = new Set() - let keptTurnId: string | null = null - for (const row of rows) { - const identity = row.identity - if ( - (row.sequence <= floor || kept.has(row.itemId)) && - identity?.provider === 'codex' && - identity.threadId === key.threadId && - !seen.has(identity.turnId) - ) { - seen.add(identity.turnId) - keptTurnId = identity.turnId - } - } - cut = { floor, providerItemId: selected.providerItemId, beforeTurnId: key.turnId, keptTurnId } - } else { + return { cut, membership } + } + if (key?.provider !== 'codex') { return null } - return { cut, kept, rows } + const boundary = db + .prepare(`${JOURNAL_REWIND_CONTEXT}, candidates AS ( + SELECT item_id FROM journal_items INDEXED BY journal_items_codex_turn + WHERE session_id=? AND epoch=? AND hidden=0 AND codex_thread_id=? AND codex_turn_id=? + UNION SELECT item_id FROM journal_submission_attempts INDEXED BY journal_submission_codex_turn + WHERE session_id=? AND epoch=? AND current=1 AND dispatch_state='accepted' AND codex_thread_id=? AND codex_turn_id=? + UNION SELECT item_id FROM journal_items INDEXED BY journal_items_provider_turn + WHERE session_id=? AND epoch=? AND hidden=0 AND provider_turn_id=? + UNION SELECT item.item_id FROM journal_items owner INDEXED BY journal_items_provider_turn + CROSS JOIN journal_items item INDEXED BY journal_items_stop_turn + WHERE owner.session_id=? AND owner.epoch=? AND owner.provider_turn_id=? + AND item.session_id=owner.session_id AND item.epoch=owner.epoch AND item.hidden=0 + AND item.turn_item_id=owner.item_id AND item.turn_item_id IS NOT NULL) + SELECT item.sequence,item.sequence_index,item.ordinal FROM rewind_context context + CROSS JOIN candidates CROSS JOIN journal_items item ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.item_id=candidates.item_id + AND item.hidden=0 AND item.sequence>context.floor AND (${rewindMember(false)})=? + ORDER BY item.sequence,item.sequence_index,item.ordinal LIMIT 1`) + .get( + ...rewindContextBindings(scope, membership), + ...prefix, + key.threadId, + key.turnId, + ...prefix, + key.threadId, + key.turnId, + ...prefix, + key.turnId, + ...prefix, + key.turnId, + key.turnId + ) + if (!boundary) { + return null + } + scope.examined(boundary) + membership.boundary = rewindPosition(boundary) + const last = db + .prepare(`${rewindRetainedTurns(false)}, candidates AS ( + SELECT item_id FROM journal_items INDEXED BY journal_items_codex_turn + WHERE session_id=? AND epoch=? AND hidden=0 AND codex_thread_id=? AND codex_turn_id IS NOT NULL + UNION SELECT item_id FROM journal_submission_attempts INDEXED BY journal_submission_codex_turn + WHERE session_id=? AND epoch=? AND current=1 AND dispatch_state='accepted' AND codex_thread_id=?), + first_occurrences AS ( + SELECT ${JOURNAL_REWIND_NATIVE_TURN} AS turn_id,item.sequence,item.sequence_index,item.ordinal, + ROW_NUMBER() OVER (PARTITION BY ${JOURNAL_REWIND_NATIVE_TURN} + ORDER BY item.sequence,item.sequence_index,item.ordinal) AS occurrence + FROM rewind_context context CROSS JOIN candidates CROSS JOIN journal_items item ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.item_id=candidates.item_id + AND item.hidden=0 AND (${JOURNAL_REWIND_THREAD})=context.native_id + AND (item.sequence<=context.floor OR (${rewindKept(false)}))) + SELECT turn_id FROM first_occurrences WHERE occurrence=1 + ORDER BY sequence DESC,sequence_index DESC,ordinal DESC LIMIT 1`) + .get( + ...rewindContextBindings(scope, membership), + ...prefix, + key.threadId, + ...prefix, + key.threadId + ) + if (last) { + scope.examined(last) + } + const cut: JournalRewindCut = { + floor, + providerItemId, + beforeTurnId: key.turnId, + keptTurnId: last ? String(last.turn_id) : null + } + return { cut, membership } } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-rewind-membership.ts b/src/main/native-chat/agent-session-journal/journal-domain-rewind-membership.ts new file mode 100644 index 00000000000..689e66d7711 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/journal-domain-rewind-membership.ts @@ -0,0 +1,68 @@ +import type { SqliteRow } from '../../sqlite/sqlite-statement' +import type { JournalSqlScope } from './journal-sql-scope' + +export type JournalRewindBoundary = { sequence: number; sequenceIndex: number; ordinal: number } +export type JournalRewindMembership = { + floor: number + boundary: JournalRewindBoundary + nativeId: string + claude: boolean +} + +export const JOURNAL_REWIND_CONTEXT = `WITH rewind_context(session_id,epoch,floor,sequence,sequence_index,ordinal,native_id) + AS (VALUES(?,?,?,?,?,?,?))` + +export function rewindContextBindings(scope: JournalSqlScope, plan: JournalRewindMembership) { + return [ + ...scope.prefix, + plan.floor, + plan.boundary.sequence, + plan.boundary.sequenceIndex, + plan.boundary.ordinal, + plan.nativeId + ] as const +} + +export const JOURNAL_REWIND_JOINS = `LEFT JOIN journal_items owner + ON owner.session_id=item.session_id AND owner.epoch=item.epoch AND owner.item_id=item.turn_item_id + LEFT JOIN journal_submission_attempts send + ON send.session_id=item.session_id AND send.epoch=item.epoch AND send.item_id=item.item_id AND send.current=1` + +export const JOURNAL_REWIND_POSITION = '(item.sequence,item.sequence_index,item.ordinal)' +export const JOURNAL_REWIND_BOUNDARY = '(context.sequence,context.sequence_index,context.ordinal)' +const adopted = "send.dispatch_state='accepted' AND send.provider_item_id IS NOT NULL" +export const JOURNAL_REWIND_THREAD = `CASE WHEN ${adopted} THEN send.codex_thread_id ELSE item.codex_thread_id END` +export const JOURNAL_REWIND_NATIVE_TURN = `CASE WHEN ${adopted} THEN send.codex_turn_id ELSE item.codex_turn_id END` + +export function rewindMember(claude: boolean): string { + return claude + ? "CASE WHEN item.turn_scope_kind='turn' THEN item.turn_item_id END" + : `CASE WHEN (${JOURNAL_REWIND_THREAD})=context.native_id THEN (${JOURNAL_REWIND_NATIVE_TURN}) + WHEN item.turn_scope_kind='turn' THEN owner.provider_turn_id ELSE item.provider_turn_id END` +} + +/** Only prefix membership is needed to keep later output; originals never participate. */ +export function rewindRetainedTurns(claude: boolean): string { + return `${JOURNAL_REWIND_CONTEXT}, rewind_kept_turns AS MATERIALIZED ( + SELECT DISTINCT ${rewindMember(claude)} AS member FROM rewind_context context + CROSS JOIN journal_items item INDEXED BY journal_items_visible_position + ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.hidden=0 + AND item.sequence>context.floor AND ${JOURNAL_REWIND_POSITION}<${JOURNAL_REWIND_BOUNDARY} + AND (${rewindMember(claude)}) IS NOT NULL)` +} + +export function rewindKept(claude: boolean, table = 'rewind_kept_turns'): string { + return `item.hidden=0 AND item.sequence>context.floor + AND (send.dispatch_state IS NULL OR (send.dispatch_state<>'rejected' AND send.queued=0)) + AND (${JOURNAL_REWIND_POSITION}<${JOURNAL_REWIND_BOUNDARY} + OR EXISTS(SELECT 1 FROM ${table} WHERE member=(${rewindMember(claude)})))` +} + +export function rewindPosition(row: SqliteRow): JournalRewindBoundary { + return { + sequence: Number(row.sequence), + sequenceIndex: Number(row.sequence_index), + ordinal: Number(row.ordinal) + } +} diff --git a/src/main/native-chat/agent-session-journal/journal-domain-rewind-removal.ts b/src/main/native-chat/agent-session-journal/journal-domain-rewind-removal.ts new file mode 100644 index 00000000000..e8c38a715e1 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/journal-domain-rewind-removal.ts @@ -0,0 +1,122 @@ +import type { AgentSessionForkedFrom } from '../../../shared/agent-session/lifecycle/agent-session-record' +import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types' +import type { JournalSqlScope } from './journal-sql-scope' +import { removeJournalDomainItem } from './journal-domain-item-removal' +import { retainedFailures } from './journal-domain-retained-failures' +import { + JOURNAL_REWIND_CONTEXT, + JOURNAL_REWIND_JOINS, + JOURNAL_REWIND_POSITION, + rewindContextBindings, + rewindRetainedTurns, + rewindKept, + type JournalRewindMembership +} from './journal-domain-rewind-membership' + +/** Materialize each selection once; uniform rows share the delete while turn-owned effects stay explicit. */ +export function removeJournalDomainRewindSuffix( + scope: JournalSqlScope, + plan: JournalRewindMembership, + floor: number, + sequence: number, + failureBoundary: number, + childBoundary: AgentSessionForkedFrom['childBoundary'] | undefined +) { + const { db, prefix } = scope + const bindings = rewindContextBindings(scope, plan) + db.exec(`CREATE TEMP TABLE IF NOT EXISTS journal_rewind_members(member TEXT PRIMARY KEY) WITHOUT ROWID; + CREATE TEMP TABLE IF NOT EXISTS journal_rewind_removals( + item_id TEXT PRIMARY KEY,revision INTEGER,sequence INTEGER,sequence_index INTEGER,ordinal INTEGER, + turn_id TEXT,special INTEGER) WITHOUT ROWID; + DELETE FROM journal_rewind_members; DELETE FROM journal_rewind_removals`) + db.prepare( + `${rewindRetainedTurns(plan.claude)} INSERT INTO journal_rewind_members SELECT member FROM rewind_kept_turns` + ).run(...bindings) + const kept = rewindKept(plan.claude, 'temp.journal_rewind_members') + if (childBoundary?.kind === 'item') { + const anchor = db + .prepare(`SELECT sequence,sequence_index,ordinal FROM journal_items + WHERE session_id=? AND epoch=? AND item_id=? AND hidden=0`) + .get(...prefix, childBoundary.itemId) + const survivor = + anchor && + db + .prepare(`${JOURNAL_REWIND_CONTEXT} + SELECT item.item_id FROM rewind_context context CROSS JOIN journal_items item INDEXED BY journal_items_visible_position + ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.hidden=0 + AND ${JOURNAL_REWIND_POSITION}<=(?,?,?) AND (item.sequence<=? OR (${kept})) + ORDER BY item.sequence DESC,item.sequence_index DESC,item.ordinal DESC LIMIT 1`) + .get( + ...bindings, + Number(anchor.sequence), + Number(anchor.sequence_index), + Number(anchor.ordinal), + floor + ) + childBoundary = survivor + ? { kind: 'item', itemId: String(survivor.item_id) } + : { kind: 'start' } + } + const failed = db + .prepare(`${JOURNAL_REWIND_CONTEXT} + SELECT item.item_id FROM rewind_context context CROSS JOIN journal_items item INDEXED BY journal_items_turn_marker + ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.hidden=0 + AND item.turn_marker=1 AND item.queue_failed_at>0 AND (${kept})`) + .all(...bindings) + const failures = retainedFailures( + db, + scope, + failed.map((row) => String(row.item_id)), + failureBoundary + ) + db.prepare(`${JOURNAL_REWIND_CONTEXT} INSERT INTO journal_rewind_removals + SELECT item.item_id,item.revision,item.sequence,item.sequence_index,item.ordinal, + CASE WHEN item.kind='turn' THEN item.turn_id END, + item.turn_marker=1 OR item.turn_id IS NOT NULL OR item.queue_failure_report_turn_item_id IS NOT NULL + FROM rewind_context context CROSS JOIN journal_items item ${JOURNAL_REWIND_JOINS} + WHERE item.session_id=context.session_id AND item.epoch=context.epoch AND item.sequence>? + AND NOT (${kept}) AND NOT (substr(item.item_id,1,5)='orca:' AND COALESCE(send.queued,0)=1)`).run( + ...bindings, + floor + ) + const removed = db + .prepare(`SELECT item_id,turn_id,special FROM journal_rewind_removals + ORDER BY sequence,sequence_index,ordinal`) + .all() + const removedItemIds = removed.map((row) => String(row.item_id)) + const removedTurnIds = removed.flatMap((row) => + row.turn_id === null ? [] : [String(row.turn_id)] + ) + db.prepare(`INSERT INTO journal_item_tombstones(session_id,epoch,item_id,revision) + SELECT ?,?,cut.item_id,MAX(cut.revision,COALESCE(old.revision,0))+1 + FROM journal_rewind_removals cut LEFT JOIN journal_item_tombstones old + ON old.session_id=? AND old.epoch=? AND old.item_id=cut.item_id WHERE cut.special=0 + ON CONFLICT(session_id,epoch,item_id) DO UPDATE SET revision=excluded.revision`).run( + ...prefix, + ...prefix + ) + db.prepare(`DELETE FROM journal_item_subagent_names WHERE session_id=? AND epoch=? + AND item_id IN (SELECT item_id FROM journal_rewind_removals WHERE special=0)`).run(...prefix) + db.prepare(`DELETE FROM journal_items WHERE session_id=? AND epoch=? + AND item_id IN (SELECT item_id FROM journal_rewind_removals WHERE special=0)`).run(...prefix) + const items: AgentJournalRenderItem[] = [] + const removedSet = new Set(removedItemIds) + for (const row of removed) { + if (row.special) { + items.push( + ...removeJournalDomainItem( + db, + scope, + String(row.item_id), + sequence, + undefined, + false + ).items.filter((item) => !removedSet.has(item.itemId)) + ) + } + } + db.exec('DELETE FROM journal_rewind_members; DELETE FROM journal_rewind_removals') + return { childBoundary, failures, removedItemIds, removedTurnIds, items } +} diff --git a/src/main/native-chat/agent-session-journal/journal-domain-submission-columns.ts b/src/main/native-chat/agent-session-journal/journal-domain-submission-columns.ts index 9a0e1568f61..e32f772f9a5 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-submission-columns.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-submission-columns.ts @@ -1,5 +1,7 @@ import type { AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types' import { journalDomainOutlineSubmission } from './journal-domain-outline-source' +import { parseAgentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' +import { isQueuedAgentJournalSubmission } from '../../../shared/agent-session/requests/agent-session-queued-submission' import { readWholeAgentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' import { classifyDispatchRejection } from '../../../shared/agent-session/requests/structured-agent-session-dispatch-rejection' import { @@ -20,7 +22,10 @@ export const JOURNAL_DOMAIN_SUBMISSION_COLUMNS = [ 'steer_turn_item_id', 'stopped_history', 'covers_stopped_history', - 'outline_json' + 'outline_json', + 'codex_thread_id', + 'codex_turn_id', + 'queued' ] as const /** All current-attempt selectors describe the same saved dispatch and its source coverage. */ @@ -28,6 +33,9 @@ export function journalDomainSubmissionColumns( submission: AgentJournalSubmission, carries: readonly string[] ) { + const identity = submission.providerItemId + ? parseAgentJournalItemKey(submission.providerItemId) + : null return [ submission.submittedAt, submission.acceptedSequence ?? null, @@ -47,6 +55,9 @@ export function journalDomainSubmissionColumns( submission.answeredInTurn?.via === 'steer' ? submission.answeredInTurn.turnItemId : null, Number(stoppedHistoryCandidate(submission)), Number(carries.length > 0 && !refusedBeforeWrite(submission)), - journalDomainOutlineSubmission(submission) + journalDomainOutlineSubmission(submission), + identity?.provider === 'codex' ? identity.threadId : null, + identity?.provider === 'codex' ? identity.turnId : null, + Number(isQueuedAgentJournalSubmission(submission)) ] } diff --git a/src/main/native-chat/agent-session-journal/journal-sql-rewind-command.ts b/src/main/native-chat/agent-session-journal/journal-sql-rewind-command.ts index 73a258ea37b..b97cebcb688 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-rewind-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-rewind-command.ts @@ -15,12 +15,9 @@ import { readJournalDomainContextHead, journalDomainPublicCursor } from './journal-domain-context-replacement' -import { - removeJournalDomainItem, - queuedJournalDomainSubmission, - journalDomainEventItem -} from './journal-domain-item-removal' -import { retainedFailures, restoreRetainedFailures } from './journal-domain-retained-failures' +import { journalDomainEventItem } from './journal-domain-item-removal' +import { restoreRetainedFailures } from './journal-domain-retained-failures' +import { removeJournalDomainRewindSuffix } from './journal-domain-rewind-removal' import { invalidateJournalDomainQueue } from './journal-domain-queue-revision' import { queueClearRestatement, writeJournalDomainQueueClear } from './journal-domain-queue-clear' import { appendJournalSqlEvent } from './journal-sql-event-tail' @@ -111,37 +108,21 @@ export function applyJournalSqlRewindCommand( throw new Error('agent_session_rewind:stale-context') } const floor = local ? plan.cut.floor : 0 - if (childBoundary?.kind === 'item') { - const anchorId = childBoundary.itemId - const anchor = plan.rows.findIndex((row) => row.itemId === anchorId) - const survivor = - anchor === -1 - ? undefined - : plan.rows - .slice(0, anchor + 1) - .findLast((row) => (local && row.sequence <= floor) || plan.kept.has(row.itemId)) - childBoundary = survivor ? { kind: 'item', itemId: survivor.itemId } : { kind: 'start' } - } const restatement = head.cleared ? queueClearRestatement(db, target) : null - const failures = retainedFailures(db, target, [...plan.kept], head.failureBoundary) let sequence = head.lastSequence + 1 invalidateJournalDomainQueue(db, target, sequence) - const removed = db - .prepare(`SELECT item_id FROM journal_items WHERE session_id=? AND epoch=? AND sequence>? - ORDER BY sequence,sequence_index,ordinal`) - .all(...prefix, floor) - for (const row of removed) { - const itemId = String(row.item_id) - if (plan.kept.has(itemId) || queuedJournalDomainSubmission(db, target, itemId)) { - continue - } - const removal = removeJournalDomainItem(db, target, itemId, sequence, undefined, false) - commit.removedItemIds.push(removal.itemId) - commit.items.push(...removal.items) - if (removal.removed?.body.kind === 'turn') { - commit.removedTurnIds.push(removal.removed.body.turnId) - } - } + const removed = removeJournalDomainRewindSuffix( + new JournalSqlScope(db, target.sessionId, target.epoch), + plan.membership, + floor, + sequence, + head.failureBoundary, + childBoundary + ) + childBoundary = removed.childBoundary + commit.removedItemIds = removed.removedItemIds + commit.removedTurnIds = removed.removedTurnIds + commit.items = removed.items if (!local) { const providerHandle = agentSessionJournalProviderHandle({ agent: record.provider, @@ -189,7 +170,7 @@ export function applyJournalSqlRewindCommand( fence, observedAt: savedAt }) - restoreRetainedFailures(db, target, failures) + restoreRetainedFailures(db, target, removed.failures) if (restatement) { sequence++ commit.clear = writeJournalDomainQueueClear(db, target, sequence, restatement) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-rewind.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-rewind.test.ts index f78b1c5710d..9c79f76309d 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-rewind.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-rewind.test.ts @@ -72,7 +72,14 @@ it('keeps late child output and queued input in place, while removing the target const late = f.item('codex:child:child-turn:1') const queued = f.item('orca:queued') const { complete } = f.prepare('codex:thread:next:0') + f.db + .prepare('UPDATE journal_items SET payload_json=? WHERE item_id=?') + .run('unreadable original', 'codex:thread:old:0') const result = await complete() + expect( + f.db.prepare('SELECT payload_json FROM journal_items WHERE item_id=?').get('codex:thread:old:0') + ?.payload_json + ).toBe('unreadable original') expect(f.item('codex:child:child-turn:1')).toEqual(late) expect(f.item('orca:queued')).toEqual(queued) expect(f.item('codex:thread:next:0')).toBeUndefined() @@ -85,6 +92,60 @@ it('keeps late child output and queued input in place, while removing the target expect(f.readRecord().rewind).toMatchObject({ phase: 'completed', epoch: result.cursor.epoch }) expect(result.cursor.epoch).not.toBe('epoch') expect(result.requestEvents).toEqual([]) + await f.write(native('next', 0), rewindMessage('replacement', 'user')) + expect(f.item('codex:thread:next:0')?.revision).toBe(3) +}) + +it('cuts accepted provider aliases at their scoped opener while keeping late output from the preceding turn', async () => { + const f = sqlRewindFixture(cleanup) + await f.write(host('kept-turn'), { + kind: 'turn', + turnId: 'host-old', + providerTurnId: 'old:/ turn', + state: 'running' + }) + await f.write(native('old:/ turn', 0), rewindMessage('kept', 'user')) + await f.write(host('hidden-owner'), { + kind: 'turn', + turnId: 'host-next', + providerTurnId: 'next:/ turn', + state: 'running' + }) + f.db.prepare('UPDATE journal_items SET hidden=1 WHERE item_id=?').run('orca:hidden-owner') + await f.write(host('scoped-opener'), rewindMessage('target opener', 'user'), { + turnScope: { kind: 'turn', turnItemId: 'orca:hidden-owner' } + }) + await f.write(host('between'), rewindMessage('after the actual boundary')) + await f.run({ + kind: 'submission', + input: { + clientMessageId: 'target', + body: rewindMessage('target', 'user'), + payloadFingerprint: 'target', + fence: 1, + origin: 'client', + source: { kind: 'user' } + } + }) + await f.run({ + kind: 'dispatch', + input: { + clientMessageId: 'target', + state: 'accepted', + fence: 1, + providerIdentity: native('next:/ turn', 0) + } + }) + await f.write(native('late', 1, 'child'), rewindMessage('kept late output'), { + turnScope: { kind: 'turn', turnItemId: 'orca:kept-turn' } + }) + const { complete } = f.prepare('orca:target') + await complete() + expect(f.item('orca:scoped-opener')).toBeUndefined() + expect(f.item('orca:between')).toBeUndefined() + expect(f.item('orca:target')).toBeUndefined() + expect(f.item('codex:thread:old%3A%2F%20turn:0')?.body).toEqual(rewindMessage('kept', 'user')) + expect(f.item('codex:child:late:1')?.body).toEqual(rewindMessage('kept late output')) }) it('rolls the cut and usage-limit cancellation back with its receipt and completes both on retry', async () => {