From e897b41b69eaa671690f068904651df2fc07477d Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:02:45 -0800 Subject: [PATCH] Resolve lost-session warning identity at the journal writer --- .../acp/acp-session-reopen-failure.test.ts | 4 +- src/main/acp/acp-session-reopen-failure.ts | 61 +------ src/main/acp/acp-structured-handshake.ts | 2 +- ...acp-structured-host-restore-failed.test.ts | 34 +++- .../acp/acp-structured-host.test-support.ts | 2 +- .../acp-structured-launch-resolution.test.ts | 18 +- .../acp/acp-structured-launch-resolution.ts | 14 +- .../acp-structured-managed-runtime.test.ts | 2 +- src/main/acp/acp-structured-resume-history.ts | 2 - ...-structured-session-adapter-resume.test.ts | 6 +- .../journal-domain-schema-001-indexes.ts | 2 + .../database/journal-domain-schema-001.ts | 1 + .../journal-domain-item-read-columns.ts | 7 +- .../journal-domain-loss-warnings.test.ts | 169 ++++++++++++++++++ .../journal-domain-timeline-reads.ts | 7 + .../writing/journal-timeline-resolution.ts | 22 ++- .../writing/journal-timeline-view.ts | 19 ++ .../provider-timeline-event.ts | 9 +- .../provider-timeline-item-decisions.ts | 3 +- .../structured-agent-runtime-registrations.ts | 4 +- ...red-agent-launch-support-probe-omp.test.ts | 2 +- ...uctured-agent-launch-support-probe.test.ts | 2 +- 22 files changed, 287 insertions(+), 105 deletions(-) create mode 100644 src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts diff --git a/src/main/acp/acp-session-reopen-failure.test.ts b/src/main/acp/acp-session-reopen-failure.test.ts index 2c441d5d7a1..5989dc528e3 100644 --- a/src/main/acp/acp-session-reopen-failure.test.ts +++ b/src/main/acp/acp-session-reopen-failure.test.ts @@ -22,7 +22,7 @@ it('propagates a temporary load envelope before takeover or a dead-child asserti key: 'saved-key', sessionId: 'saved-id', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, script: (agent) => @@ -53,7 +53,7 @@ it.each([ key: 'saved-key', sessionId: 'saved-id', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, deps: { diff --git a/src/main/acp/acp-session-reopen-failure.ts b/src/main/acp/acp-session-reopen-failure.ts index 75bda3c797d..ee9a75d8ebe 100644 --- a/src/main/acp/acp-session-reopen-failure.ts +++ b/src/main/acp/acp-session-reopen-failure.ts @@ -1,16 +1,10 @@ import { agentSessionFailureFact } from '../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../shared/agent-session/failures/agent-session-failure-words' -import type { - AgentSessionProviderHandleChain, - AgentSessionProviderHandleLink -} from '../../shared/agent-session/lifecycle/agent-session-provider-handle' -import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key' -import { createLegacyProviderTimelineIdentityScheme } from '../native-chat/agent-session-timeline/provider-timeline-identity' +import type { AgentSessionProviderHandleLink } from '../../shared/agent-session/lifecycle/agent-session-provider-handle' import type { ProviderTimelineEvent } from '../native-chat/agent-session-timeline/provider-timeline-event' import { AcpAuthRequiredError, AcpRpcError } from './acp-errors' import type { AcpStructuredLaunch } from './acp-structured-launch-resolution' import { providerRequestTimeoutOf } from '../provider-process/provider-request-timeout' -import type { AcpSavedSessionReads } from './acp-structured-resume-history' import { temporaryAcpStartupAnswer } from './acp-startup-outcome' @@ -56,6 +50,7 @@ export function acpSessionNotRestoredRow( { type: 'item.update', item: acpSessionNotRestoredItem(lostKey), + lossWarning: true, body: { kind: 'status', tone: 'warning', @@ -68,55 +63,3 @@ export function acpSessionNotRestoredRow( } ] } - -/** Retry missing loss-warning rows on successful starts; unreadable journals suppress new warnings. */ -export async function acpUnannouncedLosses( - reads: AcpSavedSessionReads, - sessionId: string, - chain: AgentSessionProviderHandleChain -): Promise { - // Any later session in the chain may have written a lost link's warning, keyed by its own id. - const warnings = chain.flatMap((link, index) => - link.replaces?.reason === 'restore-failed' - ? [ - { - key: link.replaces.key, - itemIds: warningItemIds(sessionId, link.replaces.key, chain.slice(index)) - } - ] - : [] - ) - if (warnings.length === 0) { - return [] - } - let saved: ReadonlySet - try { - saved = await reads.savedItemIds( - sessionId, - warnings.flatMap((warning) => warning.itemIds) - ) - } catch { - return [] - } - // Warnings survive even if the replacement session that wrote them is superseded. - return warnings - .filter((warning) => !warning.itemIds.some((itemId) => saved.has(itemId))) - .map((warning) => warning.key) -} - -function warningItemIds( - sessionId: string, - lostKey: string, - writers: AgentSessionProviderHandleChain -): string[] { - return writers.map(({ handle }) => - agentJournalItemKey( - createLegacyProviderTimelineIdentityScheme({ agent: handle.agent, sessionId }).item({ - namespace: handle.nativeId, - family: 'item', - key: { source: 'provider', value: acpSessionNotRestoredItem(lostKey) }, - thread: handle.nativeId - }) - ) - ) -} diff --git a/src/main/acp/acp-structured-handshake.ts b/src/main/acp/acp-structured-handshake.ts index aa7d6f7bfdd..f2337d31a7f 100644 --- a/src/main/acp/acp-structured-handshake.ts +++ b/src/main/acp/acp-structured-handshake.ts @@ -203,7 +203,7 @@ export async function initializeAcpStructuredSession( } // Include warning rows missed by earlier failed starts. const lost = [ - ...((await launch.resume?.unannouncedLosses()) ?? []), + ...(launch.resume?.lossKeys ?? []), ...(takeover?.replaces ? [takeover.replaces.key] : []) ] for (const key of lost) { diff --git a/src/main/acp/acp-structured-host-restore-failed.test.ts b/src/main/acp/acp-structured-host-restore-failed.test.ts index b1622cd3033..a006f3834d0 100644 --- a/src/main/acp/acp-structured-host-restore-failed.test.ts +++ b/src/main/acp/acp-structured-host-restore-failed.test.ts @@ -27,13 +27,15 @@ afterEach(async () => { const FRESH = 'grok-fresh-session' const FRESH_2 = 'grok-second-fresh-session' +const FRESH_3 = 'grok-third-fresh-session' +const FRESH_4 = 'grok-fourth-fresh-session' const keyOf = (nativeId: string) => agentSessionProviderHandleKey({ transport: ACP_HANDLE_TRANSPORT, agent: 'grok', nativeId }) type Lost = Map function sessionsLost(loads: string[], lost: Lost) { - const opened = [PROVIDER_SESSION, FRESH, FRESH_2] + const opened = [PROVIDER_SESSION, FRESH, FRESH_2, FRESH_3, FRESH_4] return (agent: AcpScriptedAgent) => { agent.on('session/new', (frame) => { agent.reply(frame, { @@ -83,9 +85,7 @@ async function openRestoreRig(code: number, message: string) { store, savedSession: { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(journalDatabase.readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(journalDatabase.readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(journalDatabase.readers, sessionId).hasProviderTurn(nativeId) }, resolveWorkspacePath: async () => '/workspace', resolveEnvironment: async () => ({ PATH: '/usr/bin' }), @@ -224,7 +224,8 @@ describe('a replacement after its earlier attach failed to open the journal', () } it('is written when Grok never saved the fresh session and a newer one supersedes it', async () => { - const { host, store, fence, loads, lost, warnings, ready } = await failAttachBeforeReplacement() + const { host, store, fence, loads, lost, warnings, ready, exchange } = + await failAttachBeforeReplacement() expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true }) await ready() await host.close(SESSION, 'user-close') @@ -241,11 +242,32 @@ describe('a replacement after its earlier attach failed to open the journal', () replaces: { key: keyOf(PROVIDER_SESSION), reason: 'restore-failed' } }) expect(await warnings()).toHaveLength(1) + const firstWarning = (await warnings())[0] await host.close(SESSION, 'user-close') expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true }) await ready() expect(loads).toEqual([PROVIDER_SESSION, FRESH, FRESH_2]) - expect(await warnings()).toHaveLength(1) + expect(await warnings()).toEqual([firstWarning]) + await host.close(SESSION, 'user-close') + + for (const missing of [FRESH_2, FRESH_3]) { + lost.set(missing, { code: -32002, message: 'Resource not found' }) + expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true }) + await ready() + expect(await warnings()).toEqual([firstWarning]) + if (missing === FRESH_2) { + await host.close(SESSION, 'user-close') + } + } + await exchange('remember this', 'saved', FRESH_4) + await host.close(SESSION, 'user-close') + lost.set(FRESH_4, { code: -32603, message: 'session file is corrupt' }) + expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true }) + await ready() + const afterAnotherLoss = await warnings() + expect(afterAnotherLoss).toHaveLength(2) + expect(afterAnotherLoss[0]).toEqual(firstWarning) + expect(afterAnotherLoss[1]?.itemId).not.toBe(firstWarning?.itemId) await host.close(SESSION, 'user-close') }) }) diff --git a/src/main/acp/acp-structured-host.test-support.ts b/src/main/acp/acp-structured-host.test-support.ts index 6415d278f82..58ce5a7bc91 100644 --- a/src/main/acp/acp-structured-host.test-support.ts +++ b/src/main/acp/acp-structured-host.test-support.ts @@ -76,7 +76,7 @@ export function launch(resume: () => boolean, spec = GROK): () => Promise false, - unannouncedLosses: async () => [] + lossKeys: [] } : null }) diff --git a/src/main/acp/acp-structured-launch-resolution.test.ts b/src/main/acp/acp-structured-launch-resolution.test.ts index 395bbe65a98..1ab9229e429 100644 --- a/src/main/acp/acp-structured-launch-resolution.test.ts +++ b/src/main/acp/acp-structured-launch-resolution.test.ts @@ -59,8 +59,7 @@ function ompRecord(options: Record, resumes: boolean): AgentSess } const NOTHING_SAVED: AcpSavedSessionReads = { - hasProviderTurn: async () => false, - savedItemIds: async () => new Set() + hasProviderTurn: async () => false } function resolver(record: AgentSessionRecord, savedSession = NOTHING_SAVED, spec = GROK) { @@ -82,7 +81,7 @@ function resolver(record: AgentSessionRecord, savedSession = NOTHING_SAVED, spec } describe('ACP launch resolution', () => { - it('resumes a chat with a saved exchange and warns of an unannounced loss, unless unreadable', async () => { + it('resumes a saved exchange and carries loss candidates independently of journal readability', async () => { const root = mkdtempSync(join(tmpdir(), 'orca-acp-saved-read-')) const journals = createTrackedJournalOpener() try { @@ -108,15 +107,13 @@ describe('ACP launch resolution', () => { const { readers } = await openTestAgentSessionDatabase(root) const savedSession: AcpSavedSessionReads = { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(readers, sessionId).hasProviderTurn(nativeId) } const readDecisions = async (saved: AcpSavedSessionReads) => { const launch = await resolver(record, saved).resolve({ identity }) return { unsaved: await launch.resume?.mayBeUnsaved(), - losses: await launch.resume?.unannouncedLosses() + losses: launch.resume?.lossKeys } } // Nothing saved on acp-1 yet, so the created session may hold an unsaved exchange. @@ -137,12 +134,11 @@ describe('ACP launch resolution', () => { ) // The turn on acp-1 is found, so the exchange is saved; the loss has no warning yet. expect(await readDecisions(savedSession)).toEqual({ unsaved: false, losses: [lostKey] }) - // An unreadable chat proves no exchange and suppresses new warnings. + // An unreadable chat cannot prove no exchange; its writer still owns warning admission. const unreadable: AcpSavedSessionReads = { - hasProviderTurn: () => Promise.reject(new Error('journal unreadable')), - savedItemIds: () => Promise.reject(new Error('journal unreadable')) + hasProviderTurn: () => Promise.reject(new Error('journal unreadable')) } - expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [] }) + expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [lostKey] }) } finally { await journals.closeAll() rmSync(root, { recursive: true, force: true }) diff --git a/src/main/acp/acp-structured-launch-resolution.ts b/src/main/acp/acp-structured-launch-resolution.ts index df02eb17a83..f1af1e421da 100644 --- a/src/main/acp/acp-structured-launch-resolution.ts +++ b/src/main/acp/acp-structured-launch-resolution.ts @@ -31,7 +31,6 @@ import { import type { AcpLaunchSpec } from './acp-launch-specs' import { acpLaunchSpecForAccount, resolveAcpLaunchCommand } from './acp-launch-command' import { nothingExchangedOn, type AcpSavedSessionReads } from './acp-structured-resume-history' -import { acpUnannouncedLosses } from './acp-session-reopen-failure' import { loadAcpVisualsSkill, type AcpLaunchVisualsDeps } from './acp-structured-launch-visuals' export type AcpStructuredLaunch = StructuredAgentCommand & { @@ -50,8 +49,8 @@ export type AcpStructuredLaunch = StructuredAgentCommand & { key: string /** A chat-created session with no exchanged turn may never have been saved. */ mayBeUnsaved: () => Promise - /** Lost conversations whose warning rows are missing; read after startup succeeds. */ - unannouncedLosses: () => Promise + /** The writer decides which lost conversations still need warning rows. */ + lossKeys: readonly string[] } | null } @@ -205,12 +204,9 @@ export function acpStructuredLaunchPart( identity.sessionId, head.handle.nativeId )), - unannouncedLosses: () => - acpUnannouncedLosses( - deps.savedSession, - identity.sessionId, - record.providerHandleChain - ) + lossKeys: record.providerHandleChain.flatMap((link) => + link.replaces?.reason === 'restore-failed' ? [link.replaces.key] : [] + ) } : null }, diff --git a/src/main/acp/acp-structured-managed-runtime.test.ts b/src/main/acp/acp-structured-managed-runtime.test.ts index 574eb9e9644..4c5ccfd7aac 100644 --- a/src/main/acp/acp-structured-managed-runtime.test.ts +++ b/src/main/acp/acp-structured-managed-runtime.test.ts @@ -45,7 +45,7 @@ it('prefers the pin even when older, else a complete installed or external copy' }), pinLaunchDirectory: vi.fn() }, - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, resolveEnvironment: async () => ({ PATH: '', HOME: homePath }), dataDirectory }) diff --git a/src/main/acp/acp-structured-resume-history.ts b/src/main/acp/acp-structured-resume-history.ts index d235858ebdd..1097798aaf1 100644 --- a/src/main/acp/acp-structured-resume-history.ts +++ b/src/main/acp/acp-structured-resume-history.ts @@ -2,8 +2,6 @@ export type AcpSavedSessionReads = { /** Whether any saved turn belongs to the provider session with this native id. */ hasProviderTurn(sessionId: string, nativeId: string): Promise - /** The subset of these item IDs the chat has saved. */ - savedItemIds(sessionId: string, itemIds: readonly string[]): Promise> } /** Unreadable journals cannot prove no exchange. */ diff --git a/src/main/acp/acp-structured-session-adapter-resume.test.ts b/src/main/acp/acp-structured-session-adapter-resume.test.ts index 0e4a4822813..d26a7ceb001 100644 --- a/src/main/acp/acp-structured-session-adapter-resume.test.ts +++ b/src/main/acp/acp-structured-session-adapter-resume.test.ts @@ -37,7 +37,7 @@ const resume = { sessionId: PROVIDER_SESSION, key: 'acp:grok:saved', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } } } @@ -248,7 +248,7 @@ describe('reattaching a Grok chat the journal holds', () => { sessionId: 'saved-1', key: 'acp-key-saved-1', mayBeUnsaved: async () => mayBeUnsaved, - unannouncedLosses: async () => [] + lossKeys: [] } }, initialize: RESUMES, @@ -280,7 +280,7 @@ describe('reattaching a Grok chat the journal holds', () => { sessionId: 'saved-1', key: 'acp-key-saved-1', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, initialize: RESUMES, 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 8b890896ea1..8364f324aa6 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 @@ -72,6 +72,8 @@ CREATE INDEX journal_items_provider_turn ON journal_items(session_id,epoch,provi 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_items_visible_provider_key ON journal_items(session_id,epoch,legacy_provider_item_key) + WHERE hidden=0 AND legacy_provider_item_key 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) 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 de968cac712..7d3a839c51c 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 @@ -124,6 +124,7 @@ CREATE TABLE journal_items ( provider_turn_id TEXT, codex_thread_id TEXT, codex_turn_id TEXT, + legacy_provider_item_key TEXT, opener_item_id TEXT, context_clear INTEGER NOT NULL DEFAULT 0, goal_transition INTEGER NOT NULL DEFAULT 0, 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 83d8ba88d76..2d7730df6fb 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 @@ -15,6 +15,7 @@ import { 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' +import { spelledProviderTimelineItemKey } from '../../../shared/provider-timeline-item-key' export function journalDomainItemLifecycleColumns( item: AgentJournalRenderItem @@ -112,7 +113,8 @@ export const JOURNAL_DOMAIN_ITEM_COLUMNS = [ 'turn_scope_kind', 'provider_turn_id', 'codex_thread_id', - 'codex_turn_id' + 'codex_turn_id', + 'legacy_provider_item_key' ] as const /** One payload projection supplies import, current writes and derived Stop revisions. */ @@ -140,7 +142,8 @@ export function journalDomainItemColumns(item: AgentJournalRenderItem, payload: turnScopeKind, turn?.providerTurnId ?? turn?.turnId ?? null, identity?.provider === 'codex' ? identity.threadId : null, - identity?.provider === 'codex' ? identity.turnId : null + identity?.provider === 'codex' ? identity.turnId : null, + identity?.provider === 'legacy' ? spelledProviderTimelineItemKey(identity.recordId) : null ] } } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts new file mode 100644 index 00000000000..f468dfbce94 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts @@ -0,0 +1,169 @@ +import { afterEach, expect, it } from 'vitest' +import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' +import { createLegacyProviderTimelineIdentityScheme } from '../agent-session-timeline/provider-timeline-identity' +import { acpSessionNotRestoredItem } from '../../acp/acp-session-reopen-failure' +import { + domainLifecycleFixture, + lifecycleOptions as options +} from './journal-domain-lifecycle.test-fixture' +import type { JournalWriterStep } from './writing/journal-writer-step' +import type { JournalDomainImportItem } from './journal-domain-import' +import { insertJournalDomainImportPage } from './journal-domain-import-rows' +import { runJournalTransaction } from './database/journal-database' + +const cleanups: (() => void)[] = [] +afterEach(() => { + for (const cleanup of cleanups.splice(0)) { + cleanup() + } +}) + +function warningIdentity(namespace: string, lostKey: string, agent = 'grok') { + return createLegacyProviderTimelineIdentityScheme({ agent, sessionId: 'chat' }).item({ + namespace, + family: 'item', + key: { source: 'provider', value: acpSessionNotRestoredItem(lostKey) }, + thread: namespace + }) +} + +const warning = (namespace: string, lostKey: string): JournalWriterStep => ({ + kind: 'resolved-item', + options, + reservedBytes: 4096, + resolution: { + kind: 'acp-loss-warning', + identity: warningIdentity(namespace, lostKey), + body: { kind: 'status', tone: 'warning', text: 'The earlier session could not be reopened.' } + } +}) + +it('uses surviving source-order writes for duplicate warnings and retries a rolled-back warning', async () => { + const f = domainLifecycleFixture(cleanups) + f.db.exec(`CREATE TRIGGER refuse_loss BEFORE INSERT ON journal_items + WHEN NEW.legacy_provider_item_key='session-not-restored%3Asecond' + BEGIN SELECT RAISE(ABORT,'loss refused'); END`) + const first = await f.run({ + kind: 'steps', + steps: [warning('a', 'first'), warning('b', 'first'), warning('b', 'second')] + }) + expect(first.value).toMatchObject({ + outcomes: [{ kind: 'committed', wrote: true }, { kind: 'dropped' }], + failure: { message: 'loss refused' }, + cursor: { epoch: 'epoch', sequence: 1 } + }) + const originalId = agentJournalItemKey(warningIdentity('a', 'first')) + const original = f.item(originalId) + expect(first.publication?.items.map((item) => item.itemId)).toEqual([originalId]) + f.db.exec('DROP TRIGGER refuse_loss') + f.reopen() + const retry = await f.run({ + kind: 'steps', + steps: [warning('c', 'first'), warning('c', 'second'), warning('d', 'second')] + }) + expect(retry.value).toMatchObject({ + outcomes: [{ kind: 'dropped' }, { kind: 'committed', wrote: true }, { kind: 'dropped' }], + cursor: { epoch: 'epoch', sequence: 2 } + }) + expect(f.item(originalId)).toEqual(original) + expect(retry.publication?.items).toHaveLength(1) +}) + +it('preserves imported semantic duplicates across writers and body kinds until their visible rows are removed', async () => { + const f = domainLifecycleFixture(cleanups) + f.db.exec('DELETE FROM journal_chat_state') + const firstIdentity = warningIdentity('old/writer', 'escaped:/% ☃', 'omp') + const secondIdentity = warningIdentity('another:writer', 'escaped:/% ☃', 'grok') + const longKey = 'long-key'.repeat(100) + const imported = [firstIdentity, secondIdentity, warningIdentity('old', longKey)].map( + (identity, index): JournalDomainImportItem => ({ + ordinal: index + 1, + ownerFence: 1, + executionOwner: null, + hidden: false, + item: { + itemId: agentJournalItemKey(identity), + revision: 3, + sequence: index + 1, + observedAt: 12, + body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'saved' }] }, + turnScope: { kind: 'thread' } + }, + turnItemId: null, + originalStopNoteJson: null, + queueCommandTurn: false, + queueFailedOutcome: false, + queueFailedAt: 0, + queueFailureWitnessedThrough: 0, + queueFailureReportTurnItemId: null + }) + ) + runJournalTransaction(f.db, () => + insertJournalDomainImportPage( + f.db, + { sessionId: 'chat', epoch: 'epoch' }, + { + kind: 'items', + rows: imported + } + ) + ) + f.db + .exec(`INSERT INTO journal_chat_state(session_id,workspace_id,epoch,last_sequence,highest_fence) + VALUES('chat','workspace','epoch',3,1)`) + const held = imported.map(({ item }) => item) + expect( + ( + await f.run({ + kind: 'steps', + steps: [warning('new', 'escaped:/% ☃'), warning('new', longKey)] + }) + ).value + ).toMatchObject({ outcomes: [{ kind: 'dropped' }, { kind: 'dropped' }] }) + expect(held.map((item) => f.item(item.itemId))).toEqual(held) + await f.run({ kind: 'tombstone', identity: firstIdentity, fence: 1 }) + expect( + (await f.run({ kind: 'steps', steps: [warning('new', 'escaped:/% ☃')] })).value + ).toMatchObject({ outcomes: [{ kind: 'dropped' }] }) + await f.run({ kind: 'tombstone', identity: secondIdentity, fence: 1 }) + expect( + (await f.run({ kind: 'steps', steps: [warning('new', 'escaped:/% ☃')] })).value + ).toMatchObject({ outcomes: [{ kind: 'committed', wrote: true }] }) +}) + +it('does not suppress a missing warning for hidden rows, another physical epoch or an alias alone', async () => { + const f = domainLifecycleFixture(cleanups) + const hidden = warningIdentity('old', 'hidden') + const retired = warningIdentity('old', 'retired') + for (const identity of [hidden, retired]) { + await f.run({ kind: 'item', identity, body: { kind: 'status', text: 'saved' }, options }) + } + f.db.prepare('UPDATE journal_items SET hidden=1 WHERE item_id=?').run(agentJournalItemKey(hidden)) + f.db + .prepare("UPDATE journal_items SET epoch='retired' WHERE item_id=?") + .run(agentJournalItemKey(retired)) + await f.run({ + kind: 'item', + identity: { provider: 'orca', clientMessageId: 'alias-target' }, + body: { kind: 'status', text: 'saved' }, + options + }) + f.db + .prepare(`INSERT INTO journal_item_aliases(session_id,epoch,provider_item_id,item_id,echo_declared) + VALUES('chat','epoch',?,'orca:alias-target',0)`) + .run(agentJournalItemKey(warningIdentity('new', 'alias'))) + expect( + ( + await f.run({ + kind: 'steps', + steps: [warning('new', 'hidden'), warning('new', 'retired'), warning('new', 'alias')] + }) + ).value + ).toMatchObject({ + outcomes: [ + { kind: 'committed', wrote: true }, + { kind: 'committed', wrote: true }, + { kind: 'committed', wrote: true } + ] + }) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts index 1427796197e..6f439998ea8 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts @@ -72,6 +72,13 @@ export function journalDomainTimelineReads( return { item, itemBody: (key) => item(key)?.body ?? null, + hasVisibleProviderItemKey: (key) => + Boolean( + db + .prepare(`SELECT 1 FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0 + AND legacy_provider_item_key IS NOT NULL AND legacy_provider_item_key=? LIMIT 1`) + .get(...prefix, key) + ), newestTurn: () => decode( db diff --git a/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts b/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts index b1bf91d7c6e..1fb43ee987c 100644 --- a/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts +++ b/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts @@ -1,5 +1,9 @@ import { isDeepStrictEqual } from 'node:util' -import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { + agentJournalItemKey, + parseAgentJournalItemKey +} from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { spelledProviderTimelineItemKey } from '../../../../shared/provider-timeline-item-key' import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import { AGENT_JOURNAL_THREAD_SCOPE, @@ -51,6 +55,7 @@ type TimelineItemResolution = change: 'open' | 'update' | 'close' } | { kind: 'literal'; identity: AgentJournalItemIdentity; body: AgentJournalItemBody } + | { kind: 'acp-loss-warning'; identity: AgentJournalItemIdentity; body: AgentJournalItemBody } | { kind: 'request-open' request: ProviderTimelineRequest @@ -140,6 +145,21 @@ function resolveTimelineItem( switch (resolution.kind) { case 'literal': return resolved({ identity: resolution.identity, body: resolution.body }) + case 'acp-loss-warning': { + const identity = parseAgentJournalItemKey(agentJournalItemKey(resolution.identity)) + const key = + identity?.provider === 'legacy' ? spelledProviderTimelineItemKey(identity.recordId) : null + if (key === null) { + throw new Error('journal_acp_loss_warning_requires_provider_item_key') + } + return view.hasVisibleProviderItemKey(key) + ? nothing + : resolveJournalGuardedItem( + view, + { ...resolution, kind: 'guarded-item', change: 'update' }, + placement + ) + } case 'request-open': return resolveProviderTimelineRequestOpen(view, { ...resolution, diff --git a/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts b/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts index 80d3065972f..39954244658 100644 --- a/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts +++ b/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts @@ -8,11 +8,14 @@ import { readJournalInputItem } from '../input-order/journal-input-obligations' import { journalItemLifecycleState } from '../journal-item-status-metadata' import { lostLiveWorkJournalBody } from '../item-lifecycle/journal-subagent-liveness' import { personStopDecidesTurn } from '../context-epochs/journal-stop-turn-end' +import { parseAgentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { spelledProviderTimelineItemKey } from '../../../../shared/provider-timeline-item-key' /** Named selections a timeline resolution reads at its write position; never the whole chat. */ export type JournalTimelineView = { item(itemId: string): AgentJournalRenderItem | null itemBody(itemId: string): AgentJournalItemBody | null + hasVisibleProviderItemKey(key: string): boolean /** The most recently written turn row. */ newestTurn(): AgentJournalRenderItem | null newestWithKeyPrefix(prefix: string): AgentJournalRenderItem | null @@ -35,6 +38,8 @@ export function journalTimelineView(state: JournalReducerState): JournalTimeline return { item, itemBody: (itemId) => item(itemId)?.body ?? null, + hasVisibleProviderItemKey: (key) => + hasProviderItemKey(state.queries.itemsWithKeyPrefix(state, 'legacy:'), key), newestTurn: () => state.queries.newestTurnItem(state), newestWithKeyPrefix: (prefix) => state.queries.newestItemWithKeyPrefix(state, prefix), withKeyPrefix: (prefix) => state.queries.itemsWithKeyPrefix(state, prefix), @@ -53,6 +58,7 @@ export function journalTimelineItemsView( return { item, itemBody: (itemId) => item(itemId)?.body ?? null, + hasVisibleProviderItemKey: (key) => hasProviderItemKey(items(), key), newestTurn: () => newestJournalTimelineTurn(items()), withKeyPrefix: function* (prefix) { for (const item of items()) { @@ -76,6 +82,19 @@ export function journalTimelineItemsView( } } +function hasProviderItemKey(items: Iterable, key: string): boolean { + for (const item of items) { + const identity = parseAgentJournalItemKey(item.itemId) + if ( + identity?.provider === 'legacy' && + spelledProviderTimelineItemKey(identity.recordId) === key + ) { + return true + } + } + return false +} + export function newestJournalTimelineTurn( items: Iterable ): AgentJournalRenderItem | null { diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts index f3879c4ebb8..6e4fc268f33 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts @@ -66,7 +66,14 @@ export type ProviderTimelineEvent = /** Background-task messages outlive their initiating call/turn and become unverifiable on session end. */ | ({ type: 'item.open'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) /** Full snapshot; settled tools retain terminal bodies. */ - | ({ type: 'item.update'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) + | ({ + type: 'item.update' + item: string + body: ProviderTimelineItemBody + /** ACP startup retries a lost-session warning by visible semantic identity. */ + lossWarning?: true + } & Produced & + Joined) /** Terminal snapshot replaces text streamed under the same item ID. */ | ({ type: 'item.close'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) | ({ diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts index 8490f8f0981..d0710b25acb 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts @@ -41,7 +41,8 @@ export function decideItem( lifecycle: change !== 'update', options: { ...event.producer, turnScope: scope }, resolution: { - kind: 'guarded-item', + kind: + event.type === 'item.update' && event.lossWarning ? 'acp-loss-warning' : 'guarded-item', identity: row.identity, body: event.body, change, diff --git a/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts b/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts index a0292e6a62e..b025f17cc60 100644 --- a/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts +++ b/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts @@ -145,9 +145,7 @@ function createAcpAdapter( const { readers } = context.journalDatabase const savedSession: AcpSavedSessionReads = { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(readers, sessionId).hasProviderTurn(nativeId) } const definition = acpStructuredAgentDefinition(spec) const sources = structuredAgentLaunchSources(context) diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts index 9131d4432cd..133a715f24c 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts @@ -42,7 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('omp')!, { - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, store: { getRecord: () => ({ ...agentSessionRecordFixture(), diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts index 8442e80d45c..5cffebb1156 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts @@ -42,7 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('opencode')!, { - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, store: { getRecord: () => ({ ...agentSessionRecordFixture(),