mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 11:24:37 +00:00
Show changed files directly in chat turns (#38256)
* Show changed files directly in chat turns * Use the fixture frame for the diff interaction * Keep changed filenames readable and preserve file actions * Keep unconfirmed file changes in tool activity * Capture changed files in completed transcript fixtures * Preserve patch data with authoritative history status * Keep mobile diff interaction coverage beside its card * Assert the decoded patch line contract * Exercise full returned history in catch-up coverage * test(chat): target file summary when reopening patches
This commit is contained in:
@@ -96,45 +96,54 @@ const diffSummaries = new WeakMap<
|
||||
>()
|
||||
|
||||
// Only the journal's path-only Diff envelope has counts that can be read without tool normalization.
|
||||
export function buildDiffSummaries(blocks: NativeChatBlock[]): Map<
|
||||
export function buildEditSummaries(blocks: NativeChatBlock[]): Map<
|
||||
NativeChatToolCallBlock,
|
||||
{
|
||||
files: NativeChatEditFileSummary[]
|
||||
key: string
|
||||
result: NativeChatToolResultBlock
|
||||
result: NativeChatToolResultBlock | undefined
|
||||
}
|
||||
> {
|
||||
const summaries = new Map<
|
||||
NativeChatToolCallBlock,
|
||||
{ files: NativeChatEditFileSummary[]; key: string; result: NativeChatToolResultBlock }
|
||||
{
|
||||
files: NativeChatEditFileSummary[]
|
||||
key: string
|
||||
result: NativeChatToolResultBlock | undefined
|
||||
}
|
||||
>()
|
||||
for (const [index, pair] of pairToolBlocks(blocks).entries()) {
|
||||
const { call, result } = pair
|
||||
if (
|
||||
!call ||
|
||||
call.name !== 'Diff' ||
|
||||
call.state === 'running' ||
|
||||
call.state === 'failed' ||
|
||||
result?.isError ||
|
||||
result?.editPatch ||
|
||||
!result?.output
|
||||
) {
|
||||
if (!call || !isEditToolName(call.name)) {
|
||||
continue
|
||||
}
|
||||
const input = call.input
|
||||
if (
|
||||
!input ||
|
||||
typeof input !== 'object' ||
|
||||
!('path' in input) ||
|
||||
typeof input.path !== 'string' ||
|
||||
Object.keys(input).some((key) => key !== 'path')
|
||||
) {
|
||||
const journalDiff =
|
||||
call.name === 'Diff' &&
|
||||
call.state !== 'running' &&
|
||||
call.state !== 'failed' &&
|
||||
result &&
|
||||
!result.isError &&
|
||||
!result.editPatch &&
|
||||
result.output &&
|
||||
input &&
|
||||
typeof input === 'object' &&
|
||||
'path' in input &&
|
||||
typeof input.path === 'string' &&
|
||||
Object.keys(input).every((key) => key === 'path')
|
||||
? { output: result.output, path: input.path }
|
||||
: undefined
|
||||
if (!journalDiff) {
|
||||
const files = normalizedToolEditFiles(call, result)
|
||||
if (files?.length) {
|
||||
summaries.set(call, { files, key: `${call.name}:${index}`, result })
|
||||
}
|
||||
continue
|
||||
}
|
||||
const cached = diffSummaries.get(call)
|
||||
let files = cached?.result === result ? cached.files : undefined
|
||||
let files = cached && cached.result === result ? cached.files : undefined
|
||||
if (files === undefined) {
|
||||
files = editFilesFromPatchText(result.output, input.path, true)
|
||||
files = editFilesFromPatchText(journalDiff.output, journalDiff.path, true)
|
||||
diffSummaries.set(call, { result, files })
|
||||
}
|
||||
if (files?.length) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { NativeChatMessage } from '../native-chat-types'
|
||||
import { nativeChatTurnDiffs, sameNativeChatTurnDiff } from './native-chat-turn-diffs'
|
||||
|
||||
// Count source normalization separately from rendered patches: unopened summaries still consume input.
|
||||
describe('landed edit summary work', () => {
|
||||
it.each([100, 1000])('normalizes %i Claude edits once and only new input on append', (count) => {
|
||||
let contentReads = 0
|
||||
const edit = (index: number): NativeChatMessage => ({
|
||||
id: `edit-${index}`,
|
||||
role: 'assistant',
|
||||
source: 'transcript',
|
||||
timestamp: index,
|
||||
blocks: [
|
||||
{
|
||||
type: 'tool-call',
|
||||
name: 'Edit',
|
||||
state: 'completed',
|
||||
input: {
|
||||
file_path: `/repo/file-${index}.ts`,
|
||||
old_string: 'old-line\n'.repeat(40),
|
||||
get new_string() {
|
||||
contentReads += 1
|
||||
return 'new-line\n'.repeat(40)
|
||||
}
|
||||
}
|
||||
},
|
||||
{ type: 'tool-result', output: 'The file was updated successfully.' }
|
||||
]
|
||||
})
|
||||
const messages = Array.from({ length: count }, (_, index) => edit(index))
|
||||
const keys = messages.map(() => 'u')
|
||||
const first = nativeChatTurnDiffs(messages, keys).get('u')!
|
||||
expect(first.files).toHaveLength(count)
|
||||
const coldReads = contentReads
|
||||
expect(coldReads).toBeGreaterThanOrEqual(count)
|
||||
expect(coldReads).toBeLessThanOrEqual(count * 4)
|
||||
const same = nativeChatTurnDiffs([...messages], [...keys]).get('u')!
|
||||
expect(contentReads).toBe(coldReads)
|
||||
expect(sameNativeChatTurnDiff(first, same)).toBe(true)
|
||||
const appended = nativeChatTurnDiffs([...messages, edit(count)], [...keys, 'u']).get('u')!
|
||||
expect(appended.files).toHaveLength(count + 1)
|
||||
expect(contentReads - coldReads).toBe(coldReads / count)
|
||||
expect(sameNativeChatTurnDiff(first, appended)).toBe(false)
|
||||
const refreshed = [...messages]
|
||||
const last = refreshed[count - 1]!
|
||||
refreshed[count - 1] = {
|
||||
...last,
|
||||
blocks: [last.blocks[0]!, { type: 'tool-result', output: 'Updated successfully.' }]
|
||||
}
|
||||
const beforeResult = contentReads
|
||||
nativeChatTurnDiffs(refreshed, keys)
|
||||
expect(contentReads - beforeResult).toBe(coldReads / count)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,162 @@
|
||||
import type { NativeChatEditFile } from './native-chat-edit-model'
|
||||
import type {
|
||||
NativeChatMessage,
|
||||
NativeChatToolCallBlock,
|
||||
NativeChatToolResultBlock
|
||||
} from '../native-chat-types'
|
||||
import { buildEditSummaries, normalizedToolEditFiles } from './native-chat-edit-cards'
|
||||
|
||||
export type NativeChatDiffTarget = {
|
||||
messageId: string
|
||||
editKey: string
|
||||
fileIndex: number
|
||||
/** The subagent sections the row sits in, outermost first, which a reveal opens. */
|
||||
subagentSections?: readonly string[]
|
||||
}
|
||||
|
||||
export type NativeChatDiffReveal = NativeChatDiffTarget & { requestId: number }
|
||||
|
||||
export type NativeChatTurnDiffEdit = {
|
||||
call: NativeChatToolCallBlock
|
||||
result: NativeChatToolResultBlock | undefined
|
||||
fileIndex: number
|
||||
target: NativeChatDiffTarget
|
||||
order: number
|
||||
}
|
||||
|
||||
export type NativeChatTurnDiffFile = {
|
||||
path: string
|
||||
added: number
|
||||
removed: number
|
||||
truncated: boolean
|
||||
target: NativeChatDiffTarget
|
||||
edits: NativeChatTurnDiffEdit[]
|
||||
}
|
||||
|
||||
export type NativeChatTurnDiff = {
|
||||
files: NativeChatTurnDiffFile[]
|
||||
added: number
|
||||
removed: number
|
||||
truncated: boolean
|
||||
}
|
||||
|
||||
/** Resolve a patch only when its inline card is opened. */
|
||||
export function nativeChatTurnDiffEditFile(
|
||||
edit: NativeChatTurnDiffEdit
|
||||
): NativeChatEditFile | undefined {
|
||||
return normalizedToolEditFiles(edit.call, edit.result)?.[edit.fileIndex]
|
||||
}
|
||||
|
||||
export function nativeChatTurnDiffs(
|
||||
messages: readonly NativeChatMessage[],
|
||||
turnKeys: readonly (string | undefined)[],
|
||||
subagentSectionsOf?: ReadonlyMap<string, readonly string[]>,
|
||||
/** A turn only partly loaded, whose totals would read as the whole turn's. */
|
||||
partialTurnKey?: string
|
||||
): Map<string, NativeChatTurnDiff> {
|
||||
const turns = new Map<string, Map<string, NativeChatTurnDiffFile>>()
|
||||
let editOrder = 0
|
||||
for (const [index, message] of messages.entries()) {
|
||||
const turnKey = turnKeys[index]
|
||||
if (!turnKey) {
|
||||
continue
|
||||
}
|
||||
const sections = subagentSectionsOf?.get(message.id)
|
||||
for (const [call, edit] of buildEditSummaries(message.blocks)) {
|
||||
let files = turns.get(turnKey)
|
||||
if (!files) {
|
||||
files = new Map()
|
||||
turns.set(turnKey, files)
|
||||
}
|
||||
for (const [fileIndex, file] of edit.files.entries()) {
|
||||
const previous = files.get(file.path)
|
||||
const renamed =
|
||||
file.oldPath && file.oldPath !== file.path ? files.get(file.oldPath) : undefined
|
||||
if (renamed) {
|
||||
files.delete(renamed.path)
|
||||
}
|
||||
const target: NativeChatDiffTarget = {
|
||||
messageId: message.id,
|
||||
editKey: edit.key,
|
||||
fileIndex,
|
||||
...(sections === undefined ? {} : { subagentSections: sections })
|
||||
}
|
||||
const entry = previous ??
|
||||
renamed ?? {
|
||||
path: file.path,
|
||||
added: 0,
|
||||
removed: 0,
|
||||
truncated: false,
|
||||
target,
|
||||
edits: []
|
||||
}
|
||||
if (renamed && previous) {
|
||||
entry.added += renamed.added
|
||||
entry.removed += renamed.removed
|
||||
entry.truncated ||= renamed.truncated
|
||||
entry.edits.push(...renamed.edits)
|
||||
entry.edits.sort((left, right) => left.order - right.order)
|
||||
}
|
||||
entry.path = file.path
|
||||
entry.added += file.added
|
||||
entry.removed += file.removed
|
||||
entry.truncated ||= file.truncated
|
||||
entry.target = target
|
||||
entry.edits.push({ call, result: edit.result, fileIndex, target, order: editOrder++ })
|
||||
files.set(file.path, entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Map(
|
||||
Array.from(turns, ([key, byPath]) => {
|
||||
const files = Array.from(byPath.values())
|
||||
return [
|
||||
key,
|
||||
{
|
||||
files,
|
||||
added: files.reduce((sum, file) => sum + file.added, 0),
|
||||
removed: files.reduce((sum, file) => sum + file.removed, 0),
|
||||
truncated: key === partialTurnKey || files.some((file) => file.truncated)
|
||||
}
|
||||
]
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export const NATIVE_CHAT_TURN_DIFF_FILE_LIMIT = 20
|
||||
|
||||
/** Compare recorded inputs so unrelated streaming turns do not repaint existing outcome cards. */
|
||||
export function sameNativeChatTurnDiff(
|
||||
left: NativeChatTurnDiff | undefined,
|
||||
right: NativeChatTurnDiff | undefined
|
||||
): boolean {
|
||||
return (
|
||||
left === right ||
|
||||
(!!left &&
|
||||
!!right &&
|
||||
left.added === right.added &&
|
||||
left.removed === right.removed &&
|
||||
left.truncated === right.truncated &&
|
||||
left.files.length === right.files.length &&
|
||||
left.files.every((file, index) => {
|
||||
const other = right.files[index]!
|
||||
return (
|
||||
file.path === other.path &&
|
||||
file.added === other.added &&
|
||||
file.removed === other.removed &&
|
||||
file.truncated === other.truncated &&
|
||||
file.edits.length === other.edits.length &&
|
||||
file.edits.every((edit, editIndex) => {
|
||||
const candidate = other.edits[editIndex]!
|
||||
return (
|
||||
edit.call === candidate.call &&
|
||||
edit.result === candidate.result &&
|
||||
edit.fileIndex === candidate.fileIndex &&
|
||||
edit.target.messageId === candidate.target.messageId &&
|
||||
edit.target.editKey === candidate.target.editKey
|
||||
)
|
||||
})
|
||||
)
|
||||
}))
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user