Files
archived-orca/tests/e2e/cross-version-wire/agent-session-interrupted-tool-downgrade.unit.test.ts
Neil 7f38c5d004 refactor: group shared agent and skill contract owners (#42620)
* refactor: group shared agent and skill contract owners

* chore: regenerate RPC catalog import ordering
2026-11-15 02:00:23 -08:00

117 lines
4.9 KiB
TypeScript

import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { expect, test } from 'vitest'
import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/provider-handles/agent-session-provider-handle-encoding'
import {
endedRunningAgentJournalToolCall,
interruptedAgentJournalToolCall
} from '../../../src/shared/agent-session/journal/agent-journal-tool-call-lifecycle'
import type { AgentJournalItemIdentity } from '../../../src/shared/agent-session/journal/agent-session-journal-identity'
import { createTrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support'
import {
importReleaseCheckoutModule,
materializeReleaseCheckout,
releaseModuleExport
} from './release-checkout'
import { prepareReleaseFixtures } from './release-fixture-setup'
// Pin a release predating endedAs to prove cut-short tool calls still read as failed.
const BASELINE_REF = 'v1.4.218'
const releaseToolModules = prepareReleaseFixtures([BASELINE_REF], async (ref) => {
const checkout = await materializeReleaseCheckout(ref)
return Promise.all(
[
'src/shared/agent-session-journal-schemas.ts',
'src/shared/structured-agent-session-projection.ts',
'src/shared/native-chat-tool-run-outcome.ts'
].map((path) => importReleaseCheckoutModule(checkout, path))
)
})
const SESSION = 'session-interrupted-tool'
function call(ordinal: number): AgentJournalItemIdentity {
return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal }
}
type OldRenderItem = { itemId: string; body: { kind: string; [field: string]: unknown } }
type OldMessage = { blocks: unknown[] } | null
type OldRunOutcome = (
blocks: readonly unknown[],
options: { activeTurnIsWorking?: boolean }
) => { failedCallCount: number; succeeded: boolean }
// Loads a real old build, including cold extraction and transforms.
test('an older client reads a call a stop cut short as the failure they always showed', async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-interrupted-tool-downgrade-'))
const journals = createTrackedJournalOpener()
try {
// This build: a call a stop cut short, with the partial output it left.
const journal = await journals.open({
identity: {
sessionId: SESSION,
workspaceId: 'ws-1',
hostId: 'host-1',
agent: 'codex',
providerHandle: codexProviderHandle('thread-1')
},
stateDirectory: directory,
now: () => 1_000
})
const cutShort = interruptedAgentJournalToolCall({
kind: 'tool-call',
name: 'shell',
input: { command: 'sleep 20' },
state: 'running',
output: { head: 'partial', byteLength: 7, digest: 'd', truncated: false }
})
await journal.appendItem(call(0), cutShort, { fence: 1, turnScope: { kind: 'thread' } })
// And one an end nothing proved closed, kept so a later proof can still correct it.
const unverified = endedRunningAgentJournalToolCall(
{ kind: 'tool-call', name: 'shell', input: { command: 'sleep 30' }, state: 'running' },
'unverifiable'
)
await journal.appendItem(call(1), unverified, { fence: 1, turnScope: { kind: 'thread' } })
const current = await readTestJournalItems(journal)
expect(current.map((item) => item.body)).toMatchObject([
{ state: 'failed', endedAs: 'interrupted' },
{ state: 'failed', endedAs: 'unverifiable' }
])
const [schemas, projection, outcome] = releaseToolModules(BASELINE_REF)
const renderItemSchema = releaseModuleExport<{ parse: (value: unknown) => OldRenderItem }>(
schemas,
'AgentJournalRenderItemSchema'
)
const project = releaseModuleExport<(item: unknown) => OldMessage>(
projection,
'projectStructuredItemToNativeChat'
)
const runOutcome = releaseModuleExport<OldRunOutcome>(outcome, 'nativeChatToolRunOutcome')
const settled = { activeTurnIsWorking: false }
const blocksOf = (items: OldRenderItem[]) =>
items.flatMap((item) => project(item)?.blocks ?? [])
// The older client reads this host's render items, as sent on the wire, as failed calls.
const read = current.map((item) => renderItemSchema.parse(JSON.parse(JSON.stringify(item))))
expect(runOutcome(blocksOf(read), settled)).toEqual({
failedCallCount: 2,
succeeded: false
})
// Why the fact rides beside `failed` rather than replacing it: that build calls an unknown
// state a success.
const asNewState = renderItemSchema.parse({
...current[0],
body: { ...cutShort, state: 'interrupted', endedAs: undefined }
})
expect(runOutcome(project(asNewState)?.blocks ?? [], settled).succeeded).toBe(true)
} finally {
await journals.closeAll()
rmSync(directory, { recursive: true, force: true })
}
})