Repository navigation
feat(acp): compact Grok, OpenCode and OMP chats #26502
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
6c3617b
feat(acp): compact Grok, OpenCode and OMP chats
brennanb2025 537a7b4
test(acp): expect Grok compaction capability
brennanb2025 cb05561
fix(chat): keep skipped compaction outcomes visible across history pages
brennanb2025 754b21c
Merge remote-tracking branch 'origin/main' into brennanb2025/acp-comp…
brennanb2025 059ccbd
fix(acp): a signed-out /compact reports not signed in, as a send does
brennanb2025 bfa8c84
Merge remote-tracking branch 'origin/main' into brennanb2025/acp-comp…
brennanb2025 052f365
refactor(acp): keep the timeline translator under its line limit afte…
brennanb2025 f87dd3b
Merge origin/main (cbad6412be5) into brennanb2025/acp-compaction
brennanb2025 996ea50
Merge remote-tracking branch 'origin/main' into brennanb2025/acp-comp…
brennanb2025 9b57992
Merge origin/main (39a01f9c0be) into brennanb2025/acp-compaction
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
172 changes: 172 additions & 0 deletions
172
config/scripts/native-chat-compaction-pagination.test.mjs
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,172 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { expect, it } from 'vitest' | ||
| import { readAgentSessionHistory } from '../../src/main/native-chat/agent-session-wire/agent-session-history-page.ts' | ||
| import { projectStructuredAgentSessionMessages } from '../../src/renderer/src/components/native-chat/structured-agent-session-message-projection.ts' | ||
| import { nativeChatTurnMembership } from '../../src/shared/native-chat-turn-membership.ts' | ||
| import { nativeChatRowsInDrawOrder } from '../../src/shared/native-chat-turn-grouping.ts' | ||
| import { selectNativeChatTurnStatuses } from '../../src/shared/native-chat-turn-status.ts' | ||
| import { selectStructuredAgentSettledTurns } from '../../src/shared/structured-agent-session-turn-timing.ts' | ||
| import { buildNativeChatTranscriptSlots } from '../../src/renderer/src/components/native-chat/native-chat-transcript-slots.ts' | ||
|
|
||
| async function snapshotWithLaterHistory() { | ||
| const captured = JSON.parse( | ||
| await readFile( | ||
| new URL('../../src/main/acp/fixtures/omp-v17-compact-skip.render.jsonl', import.meta.url), | ||
| 'utf8' | ||
| ) | ||
| ) | ||
| const items = [...captured.items] | ||
| // Later turns are constructed test history; the skipped outcome is captured. | ||
| for (let n = 0; n < 66; n++) { | ||
| const sequence = 24 + n * 3 | ||
| const startedAt = 1791468228000 + n * 1000 | ||
| const userItemId = `later-user-${n}` | ||
| const turnItemId = `later-turn-${n}` | ||
| items.push( | ||
| { | ||
| itemId: userItemId, | ||
| revision: 1, | ||
| sequence, | ||
| observedAt: startedAt, | ||
| turnScope: { kind: 'thread' }, | ||
| body: { | ||
| kind: 'message', | ||
| role: 'user', | ||
| blocks: [{ type: 'text', text: `later request ${n}` }] | ||
| } | ||
| }, | ||
| { | ||
| itemId: turnItemId, | ||
| revision: 2, | ||
| sequence: sequence + 1, | ||
| observedAt: startedAt, | ||
| turnScope: { kind: 'thread' }, | ||
| body: { | ||
| kind: 'turn', | ||
| turnId: turnItemId, | ||
| state: 'completed', | ||
| outcome: 'success', | ||
| userItemId, | ||
| requestedAt: startedAt, | ||
| startedAt, | ||
| completedAt: startedAt + 7 | ||
| } | ||
| }, | ||
| { | ||
| itemId: `later-answer-${n}`, | ||
| revision: 1, | ||
| sequence: sequence + 2, | ||
| observedAt: startedAt + 7, | ||
| turnScope: { kind: 'turn', turnItemId }, | ||
| body: { | ||
| kind: 'message', | ||
| role: 'assistant', | ||
| blocks: [{ type: 'text', text: `later answer ${n}` }] | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| return { | ||
| sessionId: 'session-1', | ||
| cursor: { epoch: 'epoch-1', sequence: 221 }, | ||
| items, | ||
| submissions: captured.submissions | ||
| } | ||
| } | ||
|
|
||
| function slotsFor(page) { | ||
| const messages = projectStructuredAgentSessionMessages(page.items, [], page.submissions) | ||
| const membership = nativeChatTurnMembership(messages, page) | ||
| const turnStatuses = selectNativeChatTurnStatuses( | ||
| {}, | ||
| { | ||
| activeTurnKey: membership.liveTurnKey ?? '', | ||
| isWorking: false, | ||
| thinking: false, | ||
| settledByTurn: selectStructuredAgentSettledTurns(page.items, page.submissions) | ||
| } | ||
| ) | ||
| return buildNativeChatTranscriptSlots({ | ||
| messages: nativeChatRowsInDrawOrder(messages, membership.drawOrder), | ||
| turnKeys: nativeChatRowsInDrawOrder(membership.turnKeys, membership.drawOrder), | ||
| liveTurnKey: membership.liveTurnKey, | ||
| receipts: new Map(), | ||
| turnStatuses, | ||
| turnDiffs: new Map(), | ||
| expandedTurnKeys: new Set(), | ||
| isWorking: false, | ||
| lifecycleWorking: false | ||
| }) | ||
| } | ||
|
|
||
| it('keeps the captured skip visible when the actual 200-item history tail omits only its opener', async () => { | ||
| const snapshot = await snapshotWithLaterHistory() | ||
| const result = readAgentSessionHistory( | ||
| { snapshot: () => snapshot }, | ||
| { sessionId: 'session-1', direction: 'tail', limit: 200 }, | ||
| snapshot | ||
| ) | ||
| expect(result.ok).toBe(true) | ||
| const page = result.page | ||
| expect(page.items).toHaveLength(200) | ||
| expect(page.items[0].itemId).toBe('orca:command-turn%3Acompact-1') | ||
| expect(page.items[1].body.text).toBe('Nothing to compact (session too small)') | ||
| expect(page.submissions).toEqual([]) | ||
| const slot = slotsFor(page).find( | ||
| (slot) => | ||
| slot.kind === 'message' && | ||
| slot.message.blocks.some( | ||
| (block) => block.type === 'text' && block.text === 'Nothing to compact (session too small)' | ||
| ) | ||
| ) | ||
| expect(slot).toMatchObject({ drawsMessage: true, folded: false }) | ||
| }) | ||
|
|
||
| it('restores the captured skip after the real older-page read loads its opener', async () => { | ||
| const snapshot = await snapshotWithLaterHistory() | ||
| const tail = readAgentSessionHistory( | ||
| { snapshot: () => snapshot }, | ||
| { sessionId: 'session-1', direction: 'tail', limit: 200 }, | ||
| snapshot | ||
| ).page | ||
| const older = readAgentSessionHistory( | ||
| { snapshot: () => snapshot }, | ||
| { sessionId: 'session-1', direction: 'before', cursor: tail.window.nextCursor, limit: 200 }, | ||
| snapshot | ||
| ).page | ||
| expect(older.items.map((item) => item.itemId)).toEqual(['orca:compact-1']) | ||
| const slots = slotsFor({ | ||
| ...tail, | ||
| items: [...older.items, ...tail.items], | ||
| submissions: older.submissions | ||
| }) | ||
| const slot = slots.find( | ||
| (slot) => | ||
| slot.kind === 'message' && | ||
| slot.message.blocks.some( | ||
| (block) => block.type === 'text' && block.text === 'Nothing to compact (session too small)' | ||
| ) | ||
| ) | ||
| expect(slot).toMatchObject({ drawsMessage: true, folded: false }) | ||
| }) | ||
|
|
||
| it('continues folding an ordinary warning at the same real history boundary', async () => { | ||
| const snapshot = await snapshotWithLaterHistory() | ||
| snapshot.items = snapshot.items.map((item) => | ||
| item.body.kind === 'status' ? { ...item, itemId: 'orca:ordinary-warning' } : item | ||
| ) | ||
| const result = readAgentSessionHistory( | ||
| { snapshot: () => snapshot }, | ||
| { sessionId: 'session-1', direction: 'tail', limit: 200 }, | ||
| snapshot | ||
| ) | ||
| expect(result.ok).toBe(true) | ||
| const slot = slotsFor(result.page).find( | ||
| (slot) => | ||
| slot.kind === 'message' && | ||
| slot.message.blocks.some( | ||
| (block) => block.type === 'text' && block.text === 'Nothing to compact (session too small)' | ||
| ) | ||
| ) | ||
| expect(slot).toMatchObject({ drawsMessage: false, folded: true }) | ||
| }) |
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,33 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { expect, it } from 'vitest' | ||
| import { | ||
| applyJournalRow, | ||
| createJournalReducerState, | ||
| renderJournalState | ||
| } from '../native-chat/agent-session-journal/journal-reducer' | ||
| import { parseJournalRow } from '../native-chat/agent-session-journal/journal-row-schema' | ||
|
|
||
| async function capturedCompaction() { | ||
| const text = await readFile( | ||
| new URL('./fixtures/omp-v17-compact-skip.journal.jsonl', import.meta.url), | ||
| 'utf8' | ||
| ) | ||
| const state = createJournalReducerState('session-1', 'epoch-1') | ||
| for (const line of text.trim().split('\n')) { | ||
| const parsed = parseJournalRow(line) | ||
| expect(parsed.ok).toBe(true) | ||
| if (!parsed.ok) { | ||
| throw new Error('Unreadable captured journal row') | ||
| } | ||
| applyJournalRow(state, parsed.row) | ||
| } | ||
| return renderJournalState(state) | ||
| } | ||
|
|
||
| it('replays the captured OMP skip into the renderer fixture without changing its outcome', async () => { | ||
| const journal = await capturedCompaction() | ||
| const expected: unknown = JSON.parse( | ||
| await readFile(new URL('./fixtures/omp-v17-compact-skip.render.jsonl', import.meta.url), 'utf8') | ||
| ) | ||
| expect({ items: journal.items, submissions: journal.submissions }).toEqual(expected) | ||
| }) |
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,134 @@ | ||
| // A `/compact` Orca sends as a prompt, run as the command turn the host opened. What the agent | ||
| // writes meanwhile is the command's output, read for how it went and never drawn; its answer ends | ||
| // the turn with one result row, as Claude's and Codex's compactions end. | ||
|
|
||
| import { agentSessionFailureFact, providerDiagnostic } from '../../shared/agent-session-failure' | ||
| import { agentSessionFailureWords } from '../../shared/agent-session-failure-words' | ||
| import { AGENT_SESSION_COMPACTION_SKIPPED_PRESENTATION } from '../../shared/agent-session-compaction' | ||
| import type { ProviderTimelineEvent } from '../native-chat/agent-session-timeline/provider-timeline-event' | ||
| import { structuredCompactionOutcome } from '../native-chat/agent-session-wire/structured-conversation-command-outcome' | ||
| import type { AcpDialect } from './acp-dialects/acp-dialect' | ||
| import type { ContentBlock, SessionUpdate } from './generated/acp-protocol.generated' | ||
|
|
||
| /** The prompt every compacting ACP agent takes as its own compaction command. */ | ||
| export const ACP_COMPACT_PROMPT: readonly ContentBlock[] = [{ type: 'text', text: '/compact' }] | ||
|
|
||
| /** What a running compaction has said so far. */ | ||
| export type AcpCompaction = { reply: string; failureDetail?: string } | ||
|
|
||
| /** The parts of a dialect's frame a running compaction reads. */ | ||
| export type AcpCompactionFrame = { | ||
| failureDetail?: string | ||
| end?: { durationMs?: number; failureDetail?: string } | ||
| } | ||
|
|
||
| /** Reads the running compaction's reply text; true for an update that is its output. */ | ||
| export function readAcpCompactionUpdate(compaction: AcpCompaction, update: SessionUpdate): boolean { | ||
| if (update.sessionUpdate === 'agent_message_chunk') { | ||
| if (update.content.type === 'text') { | ||
| compaction.reply += update.content.text | ||
| } | ||
| return true | ||
| } | ||
| return update.sessionUpdate === 'agent_thought_chunk' | ||
| } | ||
|
|
||
| /** Folds a frame of the running compaction into it: its words and end are read for the result its | ||
| * answer writes. True for a frame that is the compaction's own, which draws nothing. */ | ||
| export function absorbAcpCompactionFrame( | ||
| prompt: { compaction?: AcpCompaction; durationMs?: number }, | ||
| extension: AcpCompactionFrame | undefined, | ||
| update: SessionUpdate | undefined | ||
| ): boolean { | ||
| const { compaction } = prompt | ||
| if (!compaction) { | ||
| return false | ||
| } | ||
| if (extension?.failureDetail) { | ||
| compaction.failureDetail = extension.failureDetail | ||
| } | ||
| const end = extension?.end | ||
| if (end) { | ||
| prompt.durationMs = end.durationMs | ||
| if (end.failureDetail) { | ||
| compaction.failureDetail = end.failureDetail | ||
| } | ||
| return true | ||
| } | ||
| return update !== undefined && readAcpCompactionUpdate(compaction, update) | ||
| } | ||
|
|
||
| /** The compaction's result row and its turn's end, from the agent's answer. */ | ||
| export function acpCompactionEnd(input: { | ||
| compaction: AcpCompaction | ||
| turn: string | ||
| thread: string | ||
| stopReason: string | ||
| at: number | ||
| durationMs?: number | ||
| /** Words the answer itself carried, for one that failed. */ | ||
| failureDetail?: string | ||
| /** The answer said the agent is not signed in. */ | ||
| notSignedIn?: boolean | ||
| dialect: AcpDialect | ||
| agentName?: string | ||
| }): ProviderTimelineEvent[] { | ||
| const { compaction, turn, stopReason } = input | ||
| // The host's command turn is the assembler's open turn, which no provider key names. | ||
| const join = { thread: input.thread } | ||
| const end = (outcome: 'success' | 'failure' | 'cancellation'): ProviderTimelineEvent => ({ | ||
| type: 'turn.end', | ||
| at: input.at, | ||
| state: outcome === 'cancellation' ? 'interrupted' : 'completed', | ||
| outcome, | ||
| ...(input.durationMs === undefined ? {} : { durationMs: input.durationMs }) | ||
| }) | ||
| const reply = | ||
| stopReason === 'end_turn' ? input.dialect.compactionReply?.(compaction.reply) : undefined | ||
| if (reply?.outcome === 'skipped') { | ||
| // Nothing to compact is no failure: the turn succeeds and says why nothing changed. | ||
| const text = providerDiagnostic(reply.detail, 'person')?.text ?? 'Nothing to compact' | ||
| return [ | ||
| { | ||
| type: 'item.close', | ||
| item: `compaction:${turn}`, | ||
| body: { | ||
| kind: 'status', | ||
| tone: 'warning', | ||
| text, | ||
| presentation: AGENT_SESSION_COMPACTION_SKIPPED_PRESENTATION | ||
| }, | ||
| join | ||
| }, | ||
| end('success') | ||
| ] | ||
| } | ||
| const words = reply?.detail ?? input.failureDetail ?? compaction.failureDetail | ||
| const detail = words === undefined ? undefined : providerDiagnostic(words, 'person') | ||
| const verdict = structuredCompactionOutcome({ | ||
| // An agent that ends `/compact` normally compacted, unless its reply says otherwise. | ||
| compacted: stopReason === 'end_turn' && reply === undefined, | ||
| interruptRequested: stopReason === 'cancelled', | ||
| failed: detail ? { detail } : {} | ||
| }) | ||
| if (verdict.outcome === 'cancellation') { | ||
| return [end('cancellation')] | ||
| } | ||
| // The same fact a signed-out send records, so its guidance shows and the child is replaced. | ||
| const failure = | ||
| verdict.failure && input.notSignedIn | ||
| ? agentSessionFailureFact('notSignedIn', detail ? { detail } : {}) | ||
| : verdict.failure | ||
| const body = | ||
| verdict.outcome === 'success' || !failure | ||
| ? { kind: 'status' as const, text: 'Context compacted', presentation: 'compaction' as const } | ||
| : { | ||
| kind: 'status' as const, | ||
| ...agentSessionFailureWords(failure, { | ||
| surface: 'row', | ||
| ...(input.agentName === undefined ? {} : { agentName: input.agentName }) | ||
| }), | ||
| tone: 'error' as const | ||
| } | ||
| return [{ type: 'item.close', item: `compaction:${turn}`, body, join }, end(verdict.outcome)] | ||
| } | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: stablyai/orca
Length of output: 5519
🏁 Script executed:
Repository: stablyai/orca
Length of output: 41958
🏁 Script executed:
Repository: stablyai/orca
Length of output: 17865
Make extension failures override the default
end_turnsuccess.When an ACP extension supplies
failureDetailand the prompt ends withend_turnwithout a parsed reply,acpCompactionEndpasses bothcompacted: trueandfailed: { detail }.structuredCompactionOutcomecheckscompactedfirst, so the turn reports successful compaction instead of the failure.Suggested fix
📝 Committable suggestion