Repository navigation
fix(native-chat): a Stop's note records whether it took, so a Stop that stopped nothing never marks a later turn as yours #24478
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8ffcfc6
fix(native-chat): a Stop's note records whether it took, and one rule…
brennanb2025 1879889
test(native-chat): each Stop answer, the shared rule, the turnless ga…
brennanb2025 54914c0
fix(native-chat): say when a turnless Stop's answer is declined
brennanb2025 031ca61
fix(native-chat): a Stop's answer names its event by id, and a host s…
brennanb2025 2fc82c0
ci(cross-version): run the Stop-answer downgrade journey
brennanb2025 b791285
test(native-chat): the Codex Stop that finds no turn opening says so …
brennanb2025 f3087fb
fix(native-chat): a later press of the same Stop never revises an owe…
brennanb2025 9d85e94
test(native-chat): the unexpected-exit fake journal answers both Stop…
brennanb2025 784b594
fix(native-chat): a Stop whose interrupt the provider took answers to…
brennanb2025 02ce253
Merge remote-tracking branch 'origin/main' into brennanb2025/stop-ans…
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
200 changes: 200 additions & 0 deletions
200
src/main/native-chat/agent-session-journal/journal-stop-answers.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,200 @@ | ||
| // Whether a Stop still counts, read from its notes' answers: until it has one, and then while any | ||
| // says it took. The turn-end rule reads it, so a turn a Stop that took nothing named ends as the | ||
| // news it is. | ||
|
|
||
| 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 { | ||
| AGENT_JOURNAL_THREAD_SCOPE, | ||
| type AgentJournalItemIdentity, | ||
| type AgentJournalStopNoteAnswer, | ||
| type AgentSessionJournalIdentity | ||
| } from '../../../shared/agent-session-journal-types' | ||
| import { readAgentJournalTurn } from '../../../shared/agent-session-turn-record' | ||
| import { createTrackedJournalOpener } from './journal-host-database-test-support' | ||
| import type { AgentSessionJournal } from './journal-store' | ||
|
|
||
| const IDENTITY: AgentSessionJournalIdentity = { | ||
| sessionId: 'session-answers', | ||
| workspaceId: 'ws-1', | ||
| hostId: 'host-1', | ||
| agent: 'codex', | ||
| providerHandle: { kind: 'codex', threadId: 'thread-1' } | ||
| } | ||
| const SCOPE = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } | ||
| const TURN: AgentJournalItemIdentity = { | ||
| provider: 'codex', | ||
| threadId: 'thread-1', | ||
| turnId: 'turn-1', | ||
| ordinal: 0 | ||
| } | ||
|
|
||
| let root: string | ||
| let journal: AgentSessionJournal | ||
| let minted = 0 | ||
| const journals = createTrackedJournalOpener() | ||
|
|
||
| beforeEach(async () => { | ||
| root = await mkdtemp(join(tmpdir(), 'orca-stop-answers-')) | ||
| minted = 0 | ||
| // One millisecond for every row: an answer must find its Stop by id, never by time. | ||
| journal = await journals.open({ identity: IDENTITY, stateDirectory: root, now: () => 1_000 }) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await journals.closeAll() | ||
| await rm(root, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| /** A Stop of `turn-1`, a person's unless `reason` says otherwise, as its writer writes it; its | ||
| * event's id. */ | ||
| async function stopped(reason: 'user-stop' | 'user-close' = 'user-stop'): Promise<string> { | ||
| const id = `stop-${++minted}` | ||
| await journal.appendStopEvent({ id, reason, turnId: 'turn-1' }, 1) | ||
| return id | ||
| } | ||
|
|
||
| /** A Stop's note at `key`, as `performCancel` writes it. */ | ||
| async function answered( | ||
| key: string, | ||
| stop: AgentJournalStopNoteAnswer | undefined, | ||
| identity: AgentJournalItemIdentity = { provider: 'orca', clientMessageId: `stop:${key}` } | ||
| ): Promise<void> { | ||
| await journal.appendItem( | ||
| identity, | ||
| { kind: 'status', text: 'Cancellation requested.', ...(stop ? { stop } : {}) }, | ||
| SCOPE | ||
| ) | ||
| } | ||
|
|
||
| function stillCounts(): boolean { | ||
| return journal.stopMarks.stillCounts(journal.stopMarks.latest()!.event) | ||
| } | ||
|
|
||
| describe('journalStopStillCounts', () => { | ||
| it('counts a Stop with no answer yet', async () => { | ||
| await stopped() | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| }) | ||
|
|
||
| it.each(['declined', 'interrupt-unconfirmed', 'no-effect'] as const)( | ||
| 'stops counting a Stop whose only answer is %s', | ||
| async (answer) => { | ||
| const eventId = await stopped() | ||
| await answered('turn-1', { answer, eventId }) | ||
|
|
||
| expect(stillCounts()).toBe(false) | ||
| } | ||
| ) | ||
|
|
||
| it('keeps counting while any answer says it took, a later one that it did not included', async () => { | ||
| const eventId = await stopped() | ||
| await answered('turn-1', { answer: 'took', eventId }) | ||
| await answered('op-2', { answer: 'no-effect', eventId }) | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| }) | ||
|
|
||
| it('counts an owed end as taking effect', async () => { | ||
| const eventId = await stopped() | ||
| await answered('turn-1', { answer: 'end-owed', eventId }) | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| }) | ||
|
|
||
| it("reads only answers to this Stop: an earlier Stop's in the same millisecond, one naming no event, and an older host's note are none", async () => { | ||
| const earlier = await stopped() | ||
| await answered('turn-1', { answer: 'no-effect', eventId: earlier }) | ||
| await stopped() | ||
| expect(journal.stopMarks.latest()?.event.at).toBe(1_000) | ||
| await answered('op-2', { answer: 'declined' }) | ||
| await answered('op-3', undefined) | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| }) | ||
|
|
||
| it("keeps counting a person's close written in the same millisecond as their declined Stop", async () => { | ||
| await answered('turn-1', { answer: 'declined', eventId: await stopped() }) | ||
| await stopped('user-close') | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| expect(journal.stopMarks.personStopDecides('turn-1')).toBe(true) | ||
| }) | ||
|
|
||
| it("reads an answer only from a Stop's note", async () => { | ||
| const eventId = await stopped() | ||
| await answered('row', { answer: 'declined', eventId }, TURN) | ||
|
|
||
| expect(stillCounts()).toBe(true) | ||
| }) | ||
|
|
||
| it('counts an event an early build wrote with no id: no answer names it', async () => { | ||
| await answered('op-1', { answer: 'declined' }) | ||
|
|
||
| expect(journal.stopMarks.stillCounts({})).toBe(true) | ||
| }) | ||
|
|
||
| it('links an answer a rewind keeps to the Stop it restates, by the id the restatement keeps', async () => { | ||
| const eventId = await stopped() | ||
| const note = { provider: 'orca' as const, clientMessageId: 'stop:turn-1' } | ||
|
|
||
| await journal.replaceEpochItems('legacy_import', 1, [ | ||
| { | ||
| identity: note, | ||
| body: { kind: 'status', text: 'Codex didn’t stop.', stop: { answer: 'declined', eventId } } | ||
| } | ||
| ]) | ||
|
|
||
| expect(journal.stopMarks.latest()?.event).toMatchObject({ id: eventId, reason: 'user-stop' }) | ||
| expect(stillCounts()).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('the turn-end rule', () => { | ||
| /** `turn-1` running, and a person's Stop of it; the event's id. */ | ||
| async function stoppedWhileRunning(): Promise<string> { | ||
| await journal.appendItem( | ||
| TURN, | ||
| { kind: 'turn', turnId: 'turn-1', state: 'running', startedAt: Date.now() - 1_000 }, | ||
| SCOPE | ||
| ) | ||
| return stopped() | ||
| } | ||
|
|
||
| function endTurn(): Promise<unknown> { | ||
| return journal.appendItem( | ||
| TURN, | ||
| { kind: 'turn', turnId: 'turn-1', state: 'interrupted', completedAt: Date.now() + 1_000 }, | ||
| SCOPE | ||
| ) | ||
| } | ||
|
|
||
| function outcome(): string | undefined { | ||
| return journal | ||
| .snapshot() | ||
| .items.map((item) => readAgentJournalTurn(item.body)) | ||
| .find((turn) => turn?.turnId === 'turn-1')?.outcome | ||
| } | ||
|
|
||
| it("makes the end of a turn a Stop that took named the person's cancellation", async () => { | ||
| await answered('turn-1', { answer: 'took', eventId: await stoppedWhileRunning() }) | ||
|
|
||
| await endTurn() | ||
|
|
||
| expect(outcome()).toBe('cancellation') | ||
| }) | ||
|
|
||
| it('leaves the end of a turn a declined Stop named as news, so a crash reads as one', async () => { | ||
| await answered('turn-1', { answer: 'declined', eventId: await stoppedWhileRunning() }) | ||
|
|
||
| // Still the person's Stop of the turn, which a host stop defers to; not their end. | ||
| expect(journal.stopMarks.personStopCovers('turn-1')).toBe(true) | ||
| expect(journal.stopMarks.personStopDecides('turn-1')).toBe(false) | ||
| await endTurn() | ||
|
|
||
| expect(outcome()).toBeUndefined() | ||
| }) | ||
| }) |
41 changes: 41 additions & 0 deletions
41
src/main/native-chat/agent-session-journal/journal-stop-answers.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,41 @@ | ||
| // Whether a Stop still counts, from the answers its notes record: the one rule every reader of a | ||
| // Stop's effect on a turn's end applies (the turn-end write, and through it the readers that must | ||
| // choose before the end is written, and the "Stopping…" state). | ||
|
|
||
| import { | ||
| agentJournalStopAnswerTook, | ||
| isStructuredAgentSessionStopNote, | ||
| readAgentJournalStopAnswer | ||
| } from '../../../shared/agent-session-stop-answer' | ||
| import type { JournalReducerState } from './journal-reducer' | ||
| import type { JournalStopEvent } from './journal-row-schema' | ||
|
|
||
| /** | ||
| * Whether the Stop that wrote `event` still counts as taking effect: it has no answer yet, or any | ||
| * answer it has says it took. A later answer that it did not take never cancels one that says it | ||
| * did. Answers name the event by its id; an event with none (an early dev build's), an older | ||
| * host's note, and an answer this build does not know have no answer. | ||
| */ | ||
| export function journalStopStillCounts( | ||
| state: Pick<JournalReducerState, 'items'>, | ||
| event: Pick<JournalStopEvent, 'id'> | ||
| ): boolean { | ||
| if (event.id === undefined) { | ||
| return true | ||
| } | ||
| let answered = false | ||
| for (const [itemId, item] of state.items) { | ||
| if (!isStructuredAgentSessionStopNote(itemId)) { | ||
| continue | ||
| } | ||
| const stop = readAgentJournalStopAnswer(item.body) | ||
| if (stop?.eventId !== event.id) { | ||
| continue | ||
| } | ||
| if (agentJournalStopAnswerTook(stop.answer)) { | ||
| return true | ||
| } | ||
| answered = true | ||
| } | ||
| return !answered | ||
| } | ||
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.
declinedorno-effect, this lookup examines every journal item and parses each key. It runs when a covered turn ends, adding work that grows with the conversation on the serialized write path. In long chats, that can make turn endings progressively slower.