Repository navigation
fix(native-chat): date a session by its own lifecycle, not its subagents' work #22520
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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c55e912
fix(native-chat): date a session by its own agent's rows, not its sub…
brennanb2025 bf00b3d
fix(native-chat): a reopened journal's verdict on stale work does not…
brennanb2025 85cdfca
fix(native-chat): date a session by the reducer's attribution of what…
brennanb2025 a439de3
Merge origin/main into brennanb2025/c1-parent-recency
brennanb2025 6f0af76
fix(native-chat): date a session's status by its own lifecycle edges
brennanb2025 4d38423
revert(native-chat): keep the journal clock over every row
brennanb2025 753f8e4
fix(native-chat): keep republishing an idle session its live child wo…
brennanb2025 fc01d31
fix(activity): order an agent's timeline by when each state was seen
brennanb2025 13da358
test(activity): pin the timeline across repeated asks, a clear, and a…
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
215 changes: 215 additions & 0 deletions
215
src/main/native-chat/agent-session-journal/journal-session-clock.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,215 @@ | ||
| 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 type { | ||
| AgentJournalItemBody, | ||
| AgentJournalProducerLinkage | ||
| } from '../../../shared/agent-session-journal-types' | ||
| import { agentJournalItemKey } from '../../../shared/agent-session-journal-item-key' | ||
| import type { NativeChatSubagentEntry } from '../../../shared/native-chat-types' | ||
| import { | ||
| claudeBackgroundTaskBody, | ||
| claudeBackgroundTaskIdentity | ||
| } from '../../claude/claude-background-task-row-journal' | ||
| import { | ||
| codexSubagentGroupBody, | ||
| codexSubagentGroupIdentity | ||
| } from '../../codex/codex-subagent-roster' | ||
| import { | ||
| applyJournalRow, | ||
| createJournalReducerState, | ||
| type JournalReducerState | ||
| } from './journal-reducer' | ||
| import type { JournalRow } from './journal-row-schema' | ||
| import { createTrackedJournalOpener } from './journal-store-test-open' | ||
|
|
||
| const EPOCH = 'epoch-1' | ||
| const GROUP_ID = 'thread-1:turn-1' | ||
| const ROSTER_ITEM = agentJournalItemKey(codexSubagentGroupIdentity(GROUP_ID)) | ||
| const CHILD = { agentId: 'task-1', producerKind: 'agent' } as const | ||
|
|
||
| function text(value: string): AgentJournalItemBody { | ||
| return { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: value }] } | ||
| } | ||
|
|
||
| function roster(state: NativeChatSubagentEntry['state']): AgentJournalItemBody { | ||
| return codexSubagentGroupBody(GROUP_ID, [{ id: 'a', label: 'review', state, startedAt: 1 }]) | ||
| } | ||
|
|
||
| /** Row `seq` lands at `ts = 1_000 + seq`, so every row is newer than the last. */ | ||
| function base(seq: number, linkage: AgentJournalProducerLinkage = {}) { | ||
| return { v: 1, epoch: EPOCH, seq, fence: 1, ts: 1_000 + seq, ...linkage } | ||
| } | ||
|
|
||
| function item(seq: number, itemId: string, body: AgentJournalItemBody, linkage = {}): JournalRow { | ||
| return { kind: 'item', itemId, revision: seq, body, ...base(seq, linkage) } | ||
| } | ||
|
|
||
| function fold(rows: JournalRow[]): JournalReducerState { | ||
| const state = createJournalReducerState('session-1', EPOCH) | ||
| for (const row of rows) { | ||
| applyJournalRow(state, row) | ||
| } | ||
| return state | ||
| } | ||
|
|
||
| describe("the session's clock counts only its own agent's rows", () => { | ||
| it.each([ | ||
| ['claude', 'claude:claude-session:child-1'], | ||
| ['codex', 'codex:thread-child:child-turn:0'] | ||
| ])("holds on a %s subagent's rows, and resumes on the session's next own row", (_, childItem) => { | ||
| const own = item(1, 'own-1', text('delegating')) | ||
| const child = [ | ||
| item(2, childItem, text('reading'), CHILD), | ||
| item(3, childItem, text('still reading'), CHILD) | ||
| ] | ||
| const held = fold([own, ...child]) | ||
| // Ordering still counts every row; only the clock is the session's. | ||
| expect(held.lastSequence).toBe(3) | ||
| expect(held.lastActivityAt).toBe(own.ts) | ||
|
|
||
| const resumed = fold([own, ...child, item(4, 'own-2', text('done'))]) | ||
| expect(resumed.lastActivityAt).toBe(1_004) | ||
| }) | ||
|
|
||
| it("holds on a subagent's lifecycle batch, and moves on the session's own", () => { | ||
| const batch = (seq: number, linkage = {}): JournalRow => ({ | ||
| kind: 'lifecycle-batch', | ||
| settlementId: `settle-${seq}`, | ||
| mutations: [{ kind: 'item', itemId: `i-${seq}`, revision: 1, body: text('settled') }], | ||
| ...base(seq, linkage) | ||
| }) | ||
| expect(fold([batch(1, CHILD)]).lastActivityAt).toBe(0) | ||
| expect(fold([batch(1)]).lastActivityAt).toBe(1_001) | ||
| }) | ||
|
|
||
| it('holds on every revision of a subagent roster, which the session writes about its children', () => { | ||
| const own = item(1, 'own-1', text('delegating')) | ||
| const state = fold([ | ||
| own, | ||
| item(2, ROSTER_ITEM, roster('working')), | ||
| item(3, ROSTER_ITEM, roster('completed')) | ||
| ]) | ||
| expect(state.lastSequence).toBe(3) | ||
| expect(state.lastActivityAt).toBe(own.ts) | ||
| }) | ||
|
|
||
| it("holds on a roster's removal, and moves on the removal of the session's own row", () => { | ||
| const own = item(1, 'own-1', text('delegating')) | ||
| const rosterRow = item(2, ROSTER_ITEM, roster('working')) | ||
| const tombstone = (seq: number, itemId: string): JournalRow => ({ | ||
| kind: 'tombstone', | ||
| itemId, | ||
| revision: seq, | ||
| ...base(seq) | ||
| }) | ||
| expect(fold([own, rosterRow, tombstone(3, ROSTER_ITEM)]).lastActivityAt).toBe(own.ts) | ||
| expect(fold([own, rosterRow, tombstone(3, 'own-1')]).lastActivityAt).toBe(1_003) | ||
| }) | ||
|
|
||
| it("holds on a subagent row's removal: a tombstone names no producer, the row it removes does", () => { | ||
| const own = item(1, 'own-1', text('delegating')) | ||
| const child = item(2, 'child-1', text('reading'), CHILD) | ||
| const removal: JournalRow = { kind: 'tombstone', itemId: 'child-1', revision: 3, ...base(3) } | ||
| expect(fold([own, child, removal]).lastActivityAt).toBe(own.ts) | ||
| }) | ||
|
|
||
| it('holds on a batch that only revises rosters, and moves on one that carries anything else', () => { | ||
| const batch = (mutations: Extract<JournalRow, { kind: 'lifecycle-batch' }>['mutations']) => | ||
| fold([{ kind: 'lifecycle-batch', settlementId: 'settle-1', mutations, ...base(1) }]) | ||
| const rosterMutation = { | ||
| kind: 'item' as const, | ||
| itemId: ROSTER_ITEM, | ||
| revision: 1, | ||
| body: roster('unverifiable') | ||
| } | ||
| expect(batch([rosterMutation]).lastActivityAt).toBe(0) | ||
| expect( | ||
| batch([rosterMutation, { kind: 'item', itemId: 'turn', revision: 1, body: text('ended') }]) | ||
| .lastActivityAt | ||
| ).toBe(1_001) | ||
| }) | ||
|
|
||
| it("moves on the session's own system rows that are not a roster", () => { | ||
| const status: AgentJournalItemBody = { | ||
| kind: 'message', | ||
| role: 'system', | ||
| blocks: [{ type: 'text', text: 'Compacted' }] | ||
| } | ||
| expect(fold([item(1, 'status-1', status)]).lastActivityAt).toBe(1_001) | ||
| }) | ||
| }) | ||
|
|
||
| describe('reopening a journal whose roster was left running', () => { | ||
| let root: string | ||
| let clock = 1_000 | ||
| const journals = createTrackedJournalOpener() | ||
| const open = () => | ||
| journals.open({ | ||
| identity: { | ||
| sessionId: 'session-1', | ||
| workspaceId: 'ws-1', | ||
| hostId: 'host-1', | ||
| agent: 'codex', | ||
| providerHandle: { kind: 'codex', threadId: 'thread-1' } | ||
| }, | ||
| journalDir: root, | ||
| now: () => (clock += 1_000), | ||
| mintEpoch: () => `epoch-${clock}` | ||
| }) | ||
|
|
||
| beforeEach(async () => { | ||
| root = await mkdtemp(join(tmpdir(), 'orca-journal-session-clock-')) | ||
| clock = 1_000 | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await journals.closeAll() | ||
| await rm(root, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| it('settles a live background task without dating the session to the restart', async () => { | ||
| const live = await open() | ||
| await live.appendItem( | ||
| { provider: 'orca', clientMessageId: 'prompt-1' }, | ||
| { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'watch the build' }] }, | ||
| { fence: 0 } | ||
| ) | ||
| await live.appendItem( | ||
| claudeBackgroundTaskIdentity('task-1'), | ||
| claudeBackgroundTaskBody({ | ||
| type: 'background-task', | ||
| taskId: 'task-1', | ||
| kind: 'command', | ||
| label: 'npm test --watch', | ||
| state: 'working' | ||
| }), | ||
| { fence: 0 } | ||
| ) | ||
| const ownClock = live.lastActivityAt() | ||
| await live.close() | ||
|
|
||
| const reopened = await open() | ||
| // A control: the reopen DID write the task's settling revision. | ||
| expect(reopened.snapshot().items.at(-1)?.revision).toBe(2) | ||
| expect(reopened.lastActivityAt()).toBe(ownClock) | ||
| }) | ||
|
|
||
| it('settles the roster without dating the session to the restart', async () => { | ||
| const live = await open() | ||
| await live.appendItem( | ||
| { provider: 'orca', clientMessageId: 'prompt-1' }, | ||
| { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'fan out' }] }, | ||
| { fence: 0 } | ||
| ) | ||
| await live.appendItem(codexSubagentGroupIdentity(GROUP_ID), roster('working'), { fence: 0 }) | ||
| const ownClock = live.lastActivityAt() | ||
| await live.close() | ||
|
|
||
| const reopened = await open() | ||
| // A control: the reopen DID write the roster's settling revision. | ||
| expect(reopened.snapshot().items.at(-1)?.revision).toBe(2) | ||
| expect(reopened.lastActivityAt()).toBe(ownClock) | ||
| }) | ||
| }) |
64 changes: 64 additions & 0 deletions
64
src/main/native-chat/agent-session-journal/journal-session-clock.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,64 @@ | ||
| // Which journal rows date the session. | ||
| // | ||
| // `lastActivityAt` becomes the status summary's `updatedAt`, which the status row uses as its | ||
| // completion stamp and acknowledgement clock. Subagents write into the same journal and keep | ||
| // going after the session's own agent settles, so counting their rows re-dates an idle | ||
| // session and marks it unread again. | ||
| // | ||
| // The clock reads the reducer's attribution of the items a row touches, never the raw row: | ||
| // a batch or a revision need not name the producer the reducer attributes the item to. | ||
|
|
||
| import { isRootAgentJournalItem } from '../../../shared/agent-session-journal-producer' | ||
| import type { AgentJournalRenderItem } from '../../../shared/agent-session-journal-types' | ||
| import { isSubagentGroupBlock } from '../../../shared/native-chat-types' | ||
| import type { JournalRow } from './journal-row-schema' | ||
|
|
||
| type JournalItemLookup = { | ||
| items: ReadonlyMap<string, AgentJournalRenderItem> | ||
| aliases: ReadonlyMap<string, string> | ||
| } | ||
|
|
||
| /** Read BEFORE the reducer applies `row`: whether it removes the session's own work. */ | ||
| export function journalRowRemovesSessionWork(lookup: JournalItemLookup, row: JournalRow): boolean { | ||
| if (row.kind === 'tombstone') { | ||
| return isSessionWork(currentItem(lookup, row.itemId)) | ||
| } | ||
| if (row.kind === 'lifecycle-batch') { | ||
| return row.mutations.some( | ||
| (mutation) => | ||
| mutation.kind === 'tombstone' && isSessionWork(currentItem(lookup, mutation.itemId)) | ||
| ) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| /** Read AFTER the reducer applied `row`: whether it wrote the session's own work. */ | ||
| export function journalRowWroteSessionWork(lookup: JournalItemLookup, row: JournalRow): boolean { | ||
| if (row.kind === 'item') { | ||
| return isSessionWork(currentItem(lookup, row.itemId)) | ||
| } | ||
| if (row.kind === 'lifecycle-batch') { | ||
| return row.mutations.some( | ||
| (mutation) => mutation.kind === 'item' && isSessionWork(currentItem(lookup, mutation.itemId)) | ||
| ) | ||
| } | ||
| return row.kind === 'submission' || row.kind === 'dispatch' | ||
| } | ||
|
|
||
| /** The session's own agent at work. Not a subagent's row, and not a subagent roster: the | ||
| * session's row, but revised on every child transition. A roster's first write sits beside | ||
| * the spawn call, which dates the session anyway. */ | ||
| function isSessionWork(item: AgentJournalRenderItem | undefined): boolean { | ||
| return ( | ||
| item !== undefined && | ||
| isRootAgentJournalItem(item) && | ||
| !(item.body.kind === 'message' && item.body.blocks.some(isSubagentGroupBlock)) | ||
| ) | ||
| } | ||
|
|
||
| function currentItem( | ||
| lookup: JournalItemLookup, | ||
| itemId: string | ||
| ): AgentJournalRenderItem | undefined { | ||
| return lookup.items.get(lookup.aliases.get(itemId) ?? itemId) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: stablyai/orca
Length of output: 45545
🏁 Script executed:
Repository: stablyai/orca
Length of output: 43113
🏁 Script executed:
Repository: stablyai/orca
Length of output: 42702
🏁 Script executed:
Repository: stablyai/orca
Length of output: 42722
🏁 Script executed:
Repository: stablyai/orca
Length of output: 45543
🏁 Script executed:
Repository: stablyai/orca
Length of output: 42439
🏁 Script executed:
Repository: stablyai/orca
Length of output: 41588
Preserve child ownership in lifecycle-batch revisions.
When a Codex settlement revises a child-linked item, the lifecycle batch omits producer linkage.
journalRenderItemthen replaces the item with a render item that has noagentId. The reducer andjournalRowWroteSessionWorkread that replacement as root-owned, so the child revision can advance the parent session’slastActivityAt.Carry producer ownership per mutation, or preserve the existing item’s ownership when applying settlement revisions. Add a settlement-to-reducer test. The current clock test supplies
agentIdmanually and does not exercise the translator path.