Repository navigation
perf(native-chat): flush send transactions without flushing every streamed revision #28966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
296eae1
perf(native-chat): sync durable actions without flushing streamed rev…
brennanb2025 a689491
test(native-chat): provide the pending dispatch turn scope
brennanb2025 ab282cb
refactor(native-chat): reuse the journal transaction for durable writes
brennanb2025 4325baa
fix(native-chat): save user intent before provider effects
brennanb2025 097673e
perf(native-chat): skip the per-commit fsync only for streamed assist…
brennanb2025 347a143
fix(native-chat): keep image revisions fully durable
272082a
Merge origin/main into brennanb2025/nc-perf-sync
c527fef
fix(native-chat): preserve marked assistant revisions at FULL
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/native-chat/agent-session-journal/journal-streamed-text-revision.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { mkdtemp, rm } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { afterEach, beforeEach, expect, it, vi } from 'vitest' | ||
| import type { AgentJournalItemBody } from '../../../shared/agent-session-journal-types' | ||
| import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../shared/agent-session-journal-types' | ||
| import { codexImageItemBody } from '../../codex/codex-image-item-translation' | ||
| import { journalPragmaNumber } from './journal-database' | ||
| import { | ||
| closeTestJournalHostDatabases, | ||
| openTestJournalHostDatabase | ||
| } from './journal-host-database-test-support' | ||
| import { openAgentSessionJournal } from './journal-store-factory' | ||
|
|
||
| let root: string | ||
|
|
||
| beforeEach(async () => { | ||
| root = await mkdtemp(join(tmpdir(), 'orca-streamed-revision-')) | ||
| }) | ||
| afterEach(async () => { | ||
| vi.restoreAllMocks() | ||
| closeTestJournalHostDatabases() | ||
| await rm(root, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| const text = (role: 'assistant' | 'user', value: string): AgentJournalItemBody => ({ | ||
| kind: 'message', | ||
| role, | ||
| blocks: [{ type: 'text', text: value }] | ||
| }) | ||
|
|
||
| it('commits streamed text at NORMAL, Codex image transitions at FULL, and restores FULL', async () => { | ||
| const database = openTestJournalHostDatabase(root) | ||
| const levels: number[] = [] | ||
| database.transaction((db) => levels.push(journalPragmaNumber(db, 'synchronous')), true) | ||
| levels.push(journalPragmaNumber(database.db, 'synchronous')) | ||
| database.transaction((db) => levels.push(journalPragmaNumber(db, 'synchronous'))) | ||
| expect(() => | ||
| database.transaction(() => { | ||
| throw new Error('rolled back') | ||
| }, true) | ||
| ).toThrow('rolled back') | ||
| levels.push(journalPragmaNumber(database.db, 'synchronous')) | ||
| expect(levels).toEqual([1, 2, 2, 2]) | ||
| const journal = await openAgentSessionJournal({ | ||
| database, | ||
| identity: { | ||
| sessionId: 'chat', | ||
| workspaceId: 'workspace', | ||
| hostId: 'host', | ||
| agent: 'codex', | ||
| providerHandle: null | ||
| } | ||
| }) | ||
| levels.length = 0 | ||
| const exec = database.db.exec.bind(database.db) | ||
| vi.spyOn(database.db, 'exec').mockImplementation((sql) => { | ||
| if (sql === 'COMMIT') { | ||
| levels.push(journalPragmaNumber(database.db, 'synchronous')) | ||
| } | ||
| exec(sql) | ||
| }) | ||
| const image = { id: 'image', type: 'imageGeneration' } | ||
| for (const body of [ | ||
| text('assistant', 'Generating image'), | ||
| codexImageItemBody({ ...image, status: 'inProgress' }), | ||
| codexImageItemBody({ ...image, status: 'completed', result: 'AAAA' }), | ||
| text('assistant', 'Image preview unavailable') | ||
| ]) { | ||
| await journal.appendItem( | ||
| { provider: 'codex', threadId: 'thread', turnId: 'turn', ordinal: 1 }, | ||
| body, | ||
| { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } | ||
| ) | ||
| } | ||
| expect(levels).toEqual([2, 1, 2, 2]) | ||
| expect(journalPragmaNumber(database.db, 'synchronous')).toBe(2) | ||
| await journal.close() | ||
| }) |
42 changes: 42 additions & 0 deletions
42
src/main/native-chat/agent-session-journal/journal-streamed-text-revision.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { isDeepStrictEqual } from 'node:util' | ||
| import { agentJournalLinkageFields } from '../../../shared/agent-session-journal-producer' | ||
| import type { JournalReducerState } from './journal-reducer' | ||
| import type { JournalRow } from './journal-row-schema' | ||
|
|
||
| /** | ||
| * A later revision of an assistant message that changes only its content. An OS crash that loses | ||
| * it loses streamed text and nothing else: no send, approval, stop, ownership or turn fact rides | ||
| * it, and a turn's settlement is written FULL after it, which makes it durable. Everything else | ||
| * keeps the per-commit fsync. | ||
| */ | ||
| export function isJournalStreamedTextRevision( | ||
| state: JournalReducerState, | ||
| row: JournalRow | ||
| ): boolean { | ||
| if (row.kind !== 'item' || row.recovered) { | ||
| return false | ||
| } | ||
| const { body } = row | ||
| const previous = state.items.get(row.itemId) | ||
| if ( | ||
| body.kind !== 'message' || | ||
| body.role !== 'assistant' || | ||
| !body.blocks.every((block) => block.type === 'text') || | ||
| body.sentAs !== undefined || | ||
| body.command !== undefined || | ||
| body.from !== undefined || | ||
| previous?.body.kind !== 'message' || | ||
| previous.body.role !== 'assistant' || | ||
| !previous.body.blocks.every((block) => block.type === 'text') | ||
| ) { | ||
| return false | ||
| } | ||
| const ownerFence = state.itemFences.get(row.itemId) | ||
| return ( | ||
| (row.ownerFence ?? ownerFence) === ownerFence && | ||
| (row.executionOwner === undefined || | ||
| row.executionOwner === state.itemExecutionOwners.get(row.itemId)) && | ||
|
brennanb2025 marked this conversation as resolved.
Resolved
|
||
| (row.turnScope === undefined || isDeepStrictEqual(row.turnScope, previous.turnScope)) && | ||
| isDeepStrictEqual(agentJournalLinkageFields(row), agentJournalLinkageFields(previous)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.