Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/claude/claude-structured-item-translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import type { NativeChatBlock } from '../../shared/native-chat-types'
import {
boundInlineText,
DEFAULT_JOURNAL_PAYLOAD_LIMITS
type JournalPayloadLimits
} from '../native-chat/agent-session-journal/journal-payload-bounds'

export type ClaudeMessageEnvelope = {
Expand Down Expand Up @@ -171,18 +171,19 @@ export function claudeThinkingText(envelope: ClaudeMessageEnvelope): string | nu
return parts.length > 0 ? parts.join('\n') : null
}

export function claudeToolBody(input: {
tool: ClaudeToolUse
result?: ClaudeToolResult
}): AgentJournalItemBody {
export function claudeToolBody(
input: {
tool: ClaudeToolUse
result?: ClaudeToolResult
},
limits: JournalPayloadLimits
): AgentJournalItemBody {
return {
kind: 'tool-call',
name: input.tool.name,
input: input.tool.input,
state: input.result ? (input.result.failed ? 'failed' : 'completed') : 'running',
...(input.result
? { output: boundInlineText(input.result.output, DEFAULT_JOURNAL_PAYLOAD_LIMITS).bounded }
: {})
...(input.result ? { output: boundInlineText(input.result.output, limits).bounded } : {})
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/claude/claude-structured-journal-translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import {
boundInlineText,
DEFAULT_JOURNAL_PAYLOAD_LIMITS
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
} from '../native-chat/agent-session-journal/journal-payload-bounds'
import type { ClaudePendingPrompt } from './claude-structured-prompt-replies'
import { createClaudeJournalTranslator } from './claude-structured-journal-translation'
Expand Down Expand Up @@ -567,7 +567,7 @@ describe('Claude structured journal translation', () => {

expect(state.items.at(-1)?.body).toEqual({
kind: 'status',
text: boundInlineText(thinking, DEFAULT_JOURNAL_PAYLOAD_LIMITS).text
text: boundInlineText(thinking, UNRETAINED_JOURNAL_PAYLOAD_LIMITS).text
})
})

Expand Down
19 changes: 11 additions & 8 deletions src/main/claude/claude-structured-journal-translation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AgentJournalItemIdentity } from '../../shared/agent-session-journal-types'
import { agentJournalItemKey } from '../../shared/agent-session-journal-item-key'
import type { AgentSessionDeltaCoalescerDeps } from '../native-chat/agent-session-wire/agent-session-delta-coalescer'
import type { StructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import {
boundInlineText,
DEFAULT_JOURNAL_PAYLOAD_LIMITS
} from '../native-chat/agent-session-journal/journal-payload-bounds'
structuredAgentSessionPayloadLimits,
type StructuredAgentSessionEventSink
} from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import { boundInlineText } from '../native-chat/agent-session-journal/journal-payload-bounds'
import type { ClaudeStructuredSessionEvent } from './claude-structured-session-state'
import {
claudeMessageBody,
Expand Down Expand Up @@ -166,7 +166,7 @@ export function createClaudeJournalTranslator(
tools.set(tool.id, tool)
deps.sink.appendItem(
claudeToolIdentity(envelope.sessionId, tool.id),
claudeToolBody({ tool })
claudeToolBody({ tool }, structuredAgentSessionPayloadLimits(deps.sink))
)
changed = true
}
Expand All @@ -178,7 +178,7 @@ export function createClaudeJournalTranslator(
}
deps.sink.appendItem(
claudeToolIdentity(envelope.sessionId, result.toolUseId),
claudeToolBody({ tool, result })
claudeToolBody({ tool, result }, structuredAgentSessionPayloadLimits(deps.sink))
)
// A spawn call's result is the parent turn's evidence its child finished.
subagents.observeToolResult(result.toolUseId, result.failed)
Expand All @@ -190,7 +190,7 @@ export function createClaudeJournalTranslator(
if (thinking) {
deps.sink.appendItem(claudeThinkingIdentity(envelope.sessionId, envelope.uuid), {
kind: 'status',
text: boundInlineText(thinking, DEFAULT_JOURNAL_PAYLOAD_LIMITS).text
text: boundInlineText(thinking, structuredAgentSessionPayloadLimits(deps.sink)).text
})
changed = true
}
Expand Down Expand Up @@ -234,7 +234,10 @@ export function createClaudeJournalTranslator(
promptKey: event.prompt.promptKey
})
identities.push(identity)
deps.sink.appendItem(identity, claudeApprovalItem(event.prompt))
deps.sink.appendItem(
identity,
claudeApprovalItem(event.prompt, structuredAgentSessionPayloadLimits(deps.sink))
)
deps.bindPromptItemId?.(agentJournalItemKey(identity), event.prompt.promptKey)
}
promptItems.set(event.prompt.promptKey, identities)
Expand Down
9 changes: 6 additions & 3 deletions src/main/claude/claude-structured-prompt-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '../../shared/agent-session-journal-types'
import {
boundInlineText,
DEFAULT_JOURNAL_PAYLOAD_LIMITS
type JournalPayloadLimits
} from '../native-chat/agent-session-journal/journal-payload-bounds'
import { claudeRecord, claudeText } from './claude-structured-item-translation'
import {
Expand Down Expand Up @@ -43,12 +43,15 @@ export function claudePromptIdentity(input: {
}
}

export function claudeApprovalItem(prompt: ClaudePendingPrompt): AgentJournalApprovalItem {
export function claudeApprovalItem(
prompt: ClaudePendingPrompt,
limits: JournalPayloadLimits
): AgentJournalApprovalItem {
const serialized = JSON.stringify(prompt.input)
return {
kind: 'approval',
title: `Allow ${prompt.toolName}?`,
detail: serialized ? boundInlineText(serialized, DEFAULT_JOURNAL_PAYLOAD_LIMITS).text : null,
detail: serialized ? boundInlineText(serialized, limits).text : null,
options: CLAUDE_APPROVAL_DECISIONS.map((decision) => ({
id: decision,
label: APPROVAL_LABELS[decision]
Expand Down
15 changes: 7 additions & 8 deletions src/main/claude/claude-structured-provider-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { StructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import {
boundInlineText,
DEFAULT_JOURNAL_PAYLOAD_LIMITS
} from '../native-chat/agent-session-journal/journal-payload-bounds'
structuredAgentSessionPayloadLimits,
type StructuredAgentSessionEventSink
} from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import { boundInlineText } from '../native-chat/agent-session-journal/journal-payload-bounds'
import { CLAUDE_STREAM_JSON_FRAME_KINDS } from '../native-chat/agent-session-wire/claude-stream-json-frame-schema'
import {
readableProviderFrameText,
Expand Down Expand Up @@ -112,13 +112,12 @@ export function createClaudeProviderFrameFallback(
return {
append: (kind, payload, displayText) => {
sequence += 1
const translated = unhandledProviderFrameJournalItem('claude', kind, payload)
const limits = structuredAgentSessionPayloadLimits(sink)
const translated = unhandledProviderFrameJournalItem('claude', kind, payload, limits)
if (!translated) {
return
}
const bounded = displayText
? boundInlineText(displayText, DEFAULT_JOURNAL_PAYLOAD_LIMITS).text
: null
const bounded = displayText ? boundInlineText(displayText, limits).text : null
sink.appendItem(
{
provider: 'orca',
Expand Down
4 changes: 2 additions & 2 deletions src/main/codex/codex-image-item-translation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AgentJournalMessageItem } from '../../shared/agent-session-journal-types'
import type { NativeChatBlock } from '../../shared/native-chat-types'
import { buildImageDataUri } from '../../shared/image-data-uri'
import { DEFAULT_JOURNAL_PAYLOAD_LIMITS } from '../native-chat/agent-session-journal/journal-payload-bounds'
import { DEFAULT_JOURNAL_INLINE_HEAD_BYTES } from '../native-chat/agent-session-journal/journal-payload-bounds'
import { readString } from './codex-item-field-readers'
import type { CodexThreadItem } from './codex-thread-item-identity'

// Leave room for operation text and the journal envelope beside inline image bytes.
const MAX_IMAGE_REFERENCE_BYTES = DEFAULT_JOURNAL_PAYLOAD_LIMITS.inlineHeadBytes / 2
const MAX_IMAGE_REFERENCE_BYTES = DEFAULT_JOURNAL_INLINE_HEAD_BYTES / 2

function imagePath(item: CodexThreadItem, key: string): string | null {
const value = readString(item, key)
Expand Down
72 changes: 51 additions & 21 deletions src/main/codex/codex-notice-item-translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,44 @@ import { describe, expect, it } from 'vitest'
import { codexItemBody, codexStreamingJournalItem } from './codex-structured-item-translation'
import { AgentJournalItemBodySchema } from '../../shared/agent-session-journal-schemas'
import { projectStructuredItemsToNativeChat } from '../../shared/structured-agent-session-projection'
import { UNRETAINED_JOURNAL_PAYLOAD_LIMITS } from '../native-chat/agent-session-journal/journal-payload-bounds'

describe('plan document translation', () => {
it('marks both complete documents and streaming snapshots', () => {
const item = { id: 'plan-1', type: 'plan', text: '# Plan\n\nReadable prose.' }
expect(codexItemBody(item)).toEqual({
expect(codexItemBody(item, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)).toEqual({
kind: 'status',
text: item.text,
presentation: 'plan-document'
})
expect(codexStreamingJournalItem(item, '# Plan\n\nPartial').body).toEqual({
expect(
codexStreamingJournalItem(item, '# Plan\n\nPartial', UNRETAINED_JOURNAL_PAYLOAD_LIMITS).body
).toEqual({
kind: 'status',
text: '# Plan\n\nPartial',
presentation: 'plan-document'
})
expect(codexItemBody({ id: 'plan-1', type: 'plan' })).toBeNull()
expect(
codexItemBody({ id: 'plan-1', type: 'plan' }, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)
).toBeNull()
})
it('preserves the full existing reasoning body byte for byte', () => {
expect(
codexItemBody({ id: 'r', type: 'reasoning', summary: ['Thinking through the problem.'] })
codexItemBody(
{ id: 'r', type: 'reasoning', summary: ['Thinking through the problem.'] },
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
).toEqual({
kind: 'status',
text: 'Thinking through the problem.'
})
expect(codexStreamingJournalItem({ id: 'r', type: 'reasoning' }, 'Thinking…')).toEqual({
expect(
codexStreamingJournalItem(
{ id: 'r', type: 'reasoning' },
'Thinking…',
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
).toEqual({
body: { kind: 'status', text: 'Thinking…' },
handled: true
})
Expand All @@ -36,7 +50,9 @@ describe('image item translation', () => {
it.each(['/remote/work/image.png', 'C:\\work\\image.png'])(
'preserves the execution-host path %s and old-reader operation text',
(path) => {
expect(codexItemBody({ id: 'view', type: 'imageView', path })).toEqual({
expect(
codexItemBody({ id: 'view', type: 'imageView', path }, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)
).toEqual({
kind: 'message',
role: 'assistant',
blocks: [
Expand All @@ -47,13 +63,16 @@ describe('image item translation', () => {
}
)
it('prefers saved paths over unbounded inline image data', () => {
const body = codexItemBody({
id: 'gen',
type: 'imageGeneration',
status: 'completed',
savedPath: '/remote/image.png',
result: 'A'.repeat(100_000)
})
const body = codexItemBody(
{
id: 'gen',
type: 'imageGeneration',
status: 'completed',
savedPath: '/remote/image.png',
result: 'A'.repeat(100_000)
},
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
expect(body).toEqual({
kind: 'message',
role: 'assistant',
Expand All @@ -72,7 +91,10 @@ describe('image item translation', () => {
'maps bounded image data onto a meaningful existing image-ref: %s',
(result) => {
expect(
codexItemBody({ id: 'gen', type: 'imageGeneration', status: 'completed', result })
codexItemBody(
{ id: 'gen', type: 'imageGeneration', status: 'completed', result },
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
).toMatchObject({
kind: 'message',
role: 'assistant',
Expand All @@ -86,12 +108,15 @@ describe('image item translation', () => {
it.each(['A'.repeat(20_000), 'not valid image bytes', 'data:text/html;base64,AAAA', ''])(
'keeps unavailable results bounded and readable',
(result) => {
const body = codexItemBody({
id: 'gen',
type: 'imageGeneration',
status: 'completed',
result
})
const body = codexItemBody(
{
id: 'gen',
type: 'imageGeneration',
status: 'completed',
result
},
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
expect(body).toEqual({
kind: 'message',
role: 'assistant',
Expand All @@ -105,7 +130,12 @@ describe('image item translation', () => {
[{ status: 'completed', failure: { type: 'usageLimitExceeded' } }, 'Image generation failed'],
[{ status: 'future-state' }, 'Image generation: preview unavailable']
])('does not invent completed output for %j', (fields, text) => {
expect(codexItemBody({ id: 'gen', type: 'imageGeneration', ...fields })).toEqual({
expect(
codexItemBody(
{ id: 'gen', type: 'imageGeneration', ...fields },
UNRETAINED_JOURNAL_PAYLOAD_LIMITS
)
).toEqual({
kind: 'message',
role: 'assistant',
blocks: [{ type: 'text', text }]
Expand Down
6 changes: 5 additions & 1 deletion src/main/codex/codex-prompt-registry-bounds.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JOURNAL_OVERFLOW_NOT_RETAINED } from '../native-chat/agent-session-journal/journal-overflow-store'
import {
boundPayload,
digestPayload
Expand All @@ -19,8 +20,11 @@ export function codexJournalPromptIdPart(value: string): string {
return value
}
const suffix = `#${digestPayload(value).slice(0, 32)}`
// A prompt id, not user content: the clipped tail is replaced by the digest
// suffix that keeps it unique, so there is nothing to retain.
const bounded = boundPayload(value, {
inlineHeadBytes: CODEX_JOURNAL_PROMPT_ID_COMPONENT_MAX_BYTES - suffix.length
inlineHeadBytes: CODEX_JOURNAL_PROMPT_ID_COMPONENT_MAX_BYTES - suffix.length,
overflow: JOURNAL_OVERFLOW_NOT_RETAINED
})
return `${bounded.head}${suffix}`
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/codex/codex-structured-item-streams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { agentJournalItemKey } from '../../shared/agent-session-journal-item-key'
import { UNRETAINED_JOURNAL_PAYLOAD_LIMITS } from '../native-chat/agent-session-journal/journal-payload-bounds'
import { createAgentSessionDeltaCoalescer } from '../native-chat/agent-session-wire/agent-session-delta-coalescer'
import { CodexItemStreamRetention } from './codex-item-stream-retention'
import {
Expand Down Expand Up @@ -99,7 +100,16 @@ export function createCodexStructuredItemStreams(
}

const append = (state: CodexItemStreamState, text: string): boolean => {
const translated = codexStreamingJournalItem(state.item, text)
// An in-flight checkpoint retains nothing. Its text is a PREFIX of the text
// the completed item — or, on an interrupt, the settlement row — carries,
// and that one does retain. Retaining here instead would rewrite the whole
// prefix under a fresh digest at every checkpoint, so one long answer costs
// the sum of its prefixes, and the row may then be coalesced away or refused.
const translated = codexStreamingJournalItem(
state.item,
text,
UNRETAINED_JOURNAL_PAYLOAD_LIMITS

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Retain the forced capacity-eviction snapshot.

trimStates calls persist(..., true) before forgetState discards the buffered stream text. This call now uses UNRETAINED_JOURNAL_PAYLOAD_LIMITS, so an oversized stream retains only its clipped head when capacity evicts it. If the provider then interrupts before a completed item is available, the overflow tail has no remaining source for settlement.

Use retaining payload limits for this forced final snapshot, or emit it through the terminal lifecycle path. Keep ordinary coalesced checkpoints unretained.

)
if (!translated.body) {
return true
}
Expand Down Expand Up @@ -237,7 +247,9 @@ export function createCodexStructuredItemStreams(
return { handled: true, admission: { accepted: false, reason: 'failed' } }
}
state.item = { ...state.item, changes: paramsRecord.changes }
const translated = codexJournalItem(state.item)
// Pending patches replace one another before they flush, so this body is
// provisional in the same way a checkpoint is; the completed item retains.
const translated = codexJournalItem(state.item, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)
if (translated.body) {
const nextPending: CodexPendingItemPatch = {
identity: state.identity,
Expand Down
16 changes: 13 additions & 3 deletions src/main/codex/codex-structured-item-translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import {
describeToolInput
} from '../../shared/native-chat-tool-summary'
import {
codexItemBody,
codexItemBody as boundCodexItemBody,
codexItemIdentity,
codexJournalItem,
codexMessageBlocks,
codexJournalItem as boundCodexJournalItem,
codexMessageBlocks as boundCodexMessageBlocks,
CodexTurnOrdinals,
MAX_CODEX_TURN_ORDINAL_BYTES,
MAX_CODEX_TURN_ORDINAL_ENTRIES,
isCodexMessageItemType,
readCodexThreadItem,
type CodexThreadItem
} from './codex-structured-item-translation'
import { UNRETAINED_JOURNAL_PAYLOAD_LIMITS } from '../native-chat/agent-session-journal/journal-payload-bounds'

// These cases assert the clip and the shape of a translated row. Retention of
// what the clip drops has its own suite, so they all bound the same way.
const codexItemBody = (item: CodexThreadItem) =>
boundCodexItemBody(item, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)
const codexJournalItem = (item: CodexThreadItem) =>
boundCodexJournalItem(item, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)
const codexMessageBlocks = (item: CodexThreadItem) =>
boundCodexMessageBlocks(item, UNRETAINED_JOURNAL_PAYLOAD_LIMITS)

/** The tool-call input a Codex item lands on, which is what the row label and
* the collapsed run header are both derived from. */
Expand Down
Loading