Repository navigation
Native chat: record which agent run wrote each journal item (1/5, rebuild of #26867) #28092
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d438377
feat(native-chat): record which agent run wrote each journal item
brennanb2025 d6925c4
test(native-chat): fewer provenance tests: fold reopen and unstated-o…
brennanb2025 f0a45c2
test(native-chat): an unstamped roster entry from an older build read…
brennanb2025 2e19393
Merge remote-tracking branch 'origin/main' into brennanb2025/settle-s…
brennanb2025 ada76fe
test(native-chat): build the older-build roster row inline, after mai…
brennanb2025 75f2c36
Merge remote-tracking branch 'origin/main' into brennanb2025/settle-s…
brennanb2025 d4471e8
refactor(native-chat): provenance is per item only; drop per-entry st…
brennanb2025 efa2676
fix(native-chat): an item's provenance is set when it is created; rev…
brennanb2025 28edc8d
test(native-chat): keep the provenance test below the production line…
brennanb2025 387d313
Merge remote-tracking branch 'origin/main' into brennanb2025/settle-s…
brennanb2025 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
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
128 changes: 128 additions & 0 deletions
128
src/main/native-chat/agent-session-journal/journal-item-provenance.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,128 @@ | ||
| // Execution provenance (`JournalItemRow.ownerFence`): the generation whose execution produced an | ||
| // item, kept apart from each row's writer fence. Creation states it; every revision keeps it. | ||
|
|
||
| import { mkdtemp, rm } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
| import { agentJournalItemKey } from '../../../shared/agent-session-journal-item-key' | ||
| import { | ||
| AGENT_JOURNAL_THREAD_SCOPE, | ||
| type AgentJournalItemBody, | ||
| type AgentJournalItemIdentity, | ||
| type AgentSessionJournalIdentity | ||
| } from '../../../shared/agent-session-journal-types' | ||
| import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding' | ||
| import { retainedRowReplacement } from '../agent-session-wire/structured-rewind-retained-host-rows' | ||
| import type { JournalHostDatabase } from './journal-host-database' | ||
| import { | ||
| createTrackedJournalOpener, | ||
| liveTestJournalRows, | ||
| openTestJournalHostDatabase | ||
| } from './journal-host-database-test-support' | ||
| import type { AgentSessionJournal } from './journal-store' | ||
|
|
||
| const IDENTITY: AgentSessionJournalIdentity = { | ||
| sessionId: 'session-1', | ||
| workspaceId: 'ws-1', | ||
| hostId: 'host-1', | ||
| agent: 'codex', | ||
| providerHandle: codexProviderHandle('thread-1') | ||
| } | ||
| const SCOPE = AGENT_JOURNAL_THREAD_SCOPE | ||
| const TURN: AgentJournalItemIdentity = { | ||
| provider: 'codex', | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| ordinal: 1 | ||
| } | ||
| const TURN_ID = agentJournalItemKey(TURN) | ||
| const RUNNING: AgentJournalItemBody = { kind: 'turn', turnId: 'turn-1', state: 'running' } | ||
| const SETTLED: AgentJournalItemBody = { kind: 'turn', turnId: 'turn-1', state: 'interrupted' } | ||
|
|
||
| let root: string | ||
| let database: JournalHostDatabase | ||
| const journals = createTrackedJournalOpener() | ||
|
|
||
| function item(ordinal: number): AgentJournalItemIdentity { | ||
| return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal } | ||
| } | ||
|
|
||
| type StoredItemWrite = { kind?: string; ownerFence?: number; mutations?: StoredItemWrite[] } | ||
|
|
||
| /** Each stored item write's stated provenance, in write order, a lifecycle batch's included. */ | ||
| function storedOwnerFences(): (number | undefined)[] { | ||
| return liveTestJournalRows(database.db, IDENTITY.sessionId).flatMap((row) => { | ||
| const parsed: StoredItemWrite = JSON.parse(row.rowJson) | ||
| const writes = parsed.kind === 'lifecycle-batch' ? (parsed.mutations ?? []) : [parsed] | ||
| return writes.flatMap((write) => (write.kind === 'item' ? [write.ownerFence] : [])) | ||
| }) | ||
| } | ||
|
|
||
| async function open(): Promise<AgentSessionJournal> { | ||
| return journals.open({ identity: IDENTITY, stateDirectory: root }) | ||
| } | ||
|
|
||
| beforeEach(async () => { | ||
| root = await mkdtemp(join(tmpdir(), 'orca-journal-provenance-')) | ||
| database = openTestJournalHostDatabase(root) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await journals.closeAll() | ||
| await rm(root, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| describe('execution provenance', () => { | ||
| it("records the creating observation's owner, every revision keeps it, and a reopen reads it", async () => { | ||
| const journal = await open() | ||
| await journal.appendItem(TURN, RUNNING, { fence: 4, ownerFence: 3, turnScope: SCOPE }) | ||
| // A newer run's provider revising it (a start's context-usage report on the newest saved turn). | ||
| await journal.appendItem(TURN, RUNNING, { fence: 5, ownerFence: 5, turnScope: SCOPE }) | ||
| // Host bookkeeping under a later writer fence states no owner of its own. | ||
| await journal.appendItem(TURN, SETTLED, { fence: 5, turnScope: SCOPE }) | ||
| // A first write with no stated owner is its writer generation's. | ||
| await journal.appendItem( | ||
| item(2), | ||
| { kind: 'status', text: 'note' }, | ||
| { fence: 6, turnScope: SCOPE } | ||
| ) | ||
|
|
||
| expect(journal.itemFence(TURN_ID)).toBe(3) | ||
| expect(journal.itemFence(agentJournalItemKey(item(2)))).toBe(6) | ||
| // Every row states it: none leaves the reader to guess from the writer fence. | ||
| expect(storedOwnerFences()).toEqual([3, 3, 3, 6]) | ||
| await journals.closeAll() | ||
| expect((await open()).itemFence(TURN_ID)).toBe(3) | ||
| }) | ||
|
|
||
| it('a lifecycle batch states it on each item: the batch owner, else the kept value', async () => { | ||
| const journal = await open() | ||
| await journal.appendItem(TURN, RUNNING, { fence: 2, ownerFence: 2, turnScope: SCOPE }) | ||
| // A provider batch at the owner's fence creates one item. | ||
| await journal.appendLifecycleBatch({ | ||
| settlementId: 'provider', | ||
| fence: 3, | ||
| ownerFence: 3, | ||
| mutations: [{ kind: 'item', identity: item(2), body: RUNNING, turnScope: SCOPE }] | ||
| }) | ||
| // A settlement at a later fence revises the first: it keeps that item's owner. | ||
| await journal.appendLifecycleBatch({ | ||
| settlementId: 'settlement', | ||
| fence: 4, | ||
| recovered: true, | ||
| mutations: [{ kind: 'item', identity: TURN, body: SETTLED, turnScope: SCOPE }] | ||
| }) | ||
|
|
||
| expect(journal.itemFence(TURN_ID)).toBe(2) | ||
| expect(journal.itemFence(agentJournalItemKey(item(2)))).toBe(3) | ||
| expect(storedOwnerFences()).toEqual([2, 3, 2]) | ||
| }) | ||
|
|
||
| it("a rewind's retained row carries the old fold's provenance; an older record's is never current", () => { | ||
| const row = { itemId: TURN_ID, body: RUNNING, observedAt: 1, ownerFence: 2 } | ||
| expect(retainedRowReplacement(row).ownerFence).toBe(2) | ||
| const { ownerFence: _stated, ...older } = row | ||
| expect(retainedRowReplacement(older).ownerFence).toBe(0) | ||
| }) | ||
| }) |
16 changes: 16 additions & 0 deletions
16
src/main/native-chat/agent-session-journal/journal-item-provenance.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,16 @@ | ||
| // Execution provenance: the generation whose execution produced an item (`JournalItemRow.ownerFence`), | ||
| // kept apart from each row's writer fence. The write that creates an item states it; every revision | ||
| // keeps it. | ||
|
|
||
| import type { JournalReducerState } from './journal-reducer' | ||
|
|
||
| /** An existing item keeps its owner whoever revises it: a newer run's start revises the newest saved | ||
| * turn. Creation states the writer's claim (an observation, carried history), else its fence. */ | ||
| export function journalItemOwnerFence( | ||
| state: Pick<JournalReducerState, 'itemFences' | 'items'>, | ||
| resolvedItemId: string, | ||
| write: { fence: number; ownerFence?: number } | ||
| ): number { | ||
| const kept = state.items.has(resolvedItemId) ? state.itemFences.get(resolvedItemId) : undefined | ||
| return kept ?? write.ownerFence ?? write.fence | ||
| } |
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
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
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
Oops, something went wrong.
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.