Resolve lost-session warning identity at the journal writer

This commit is contained in:
Brennan Benson
2026-11-09 02:02:45 -08:00
parent 492f473d4c
commit e897b41b69
22 changed files with 287 additions and 105 deletions
@@ -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: {
+2 -59
View File
@@ -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<string[]> {
// 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<string>
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
})
)
)
}
+1 -1
View File
@@ -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) {
@@ -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<string, { code: number; message: string }>
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')
})
})
@@ -76,7 +76,7 @@ export function launch(resume: () => boolean, spec = GROK): () => Promise<AcpStr
nativeId: PROVIDER_SESSION
}),
mayBeUnsaved: async () => false,
unannouncedLosses: async () => []
lossKeys: []
}
: null
})
@@ -59,8 +59,7 @@ function ompRecord(options: Record<string, string>, 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 })
@@ -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<boolean>
/** Lost conversations whose warning rows are missing; read after startup succeeds. */
unannouncedLosses: () => Promise<string[]>
/** 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
},
@@ -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
})
@@ -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<boolean>
/** The subset of these item IDs the chat has saved. */
savedItemIds(sessionId: string, itemIds: readonly string[]): Promise<ReadonlySet<string>>
}
/** Unreadable journals cannot prove no exchange. */
@@ -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,
@@ -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)
@@ -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,
@@ -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
]
}
}
@@ -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 }
]
})
})
@@ -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
@@ -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,
@@ -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<AgentJournalRenderItem>, 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>
): AgentJournalRenderItem | null {
@@ -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)
| ({
@@ -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,
@@ -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)
@@ -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(),
@@ -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(),