Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,30 @@ import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel'
// This test mimics that verbatim content pass-through (the shared fixture's
// own GET mock does not carry tool_calls) and asserts the summary survives a
// refresh.
// Release note: upstream's TRACE interleaves thinking rows, which this
// branch's trace does not carry. The row this fix changes is the same one:
// the restored Add node call now reads as an unconfirmed canvas write rather
// than a plain tool label.
const THINKING_ROUND_1 =
'The user wants a loader node, so I will search for one.'
const THINKING_ROUND_2 = 'Found a match; adding it to the graph.'
const THINKING_FINAL = 'Both steps succeeded, so I can answer.'
const TRACE = [
THINKING_ROUND_1,
'Search nodes',
enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node')
THINKING_ROUND_2,
enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node'),
THINKING_FINAL
]

test.describe.configure({ timeout: 120_000 })
test.use({ connectWebSocketToServer: false })

test(
'keeps a completed turn work summary after reload and chat switching',
{ tag: ['@cloud', '@ui'] },
'keeps a completed turn work summary, thinking included, after reload and chat switching',
{
tag: ['@cloud', '@ui'],
annotation: {
type: 'source',
description: 'https://linear.app/comfyorg/issue/PM-717'
}
},
async ({ page, promptHistory, workflowSelection }) => {
await new AgentPanel(page).open()
await page
Expand Down Expand Up @@ -98,6 +107,11 @@ test(
status: 'complete',
content: {
text: 'Found it.',
thinking: [
{ text: THINKING_ROUND_1, tool_call_id: 'call-1' },
{ text: THINKING_ROUND_2, tool_call_id: 'call-2' },
{ text: THINKING_FINAL }
],
tool_calls: [
{
id: 'call-1',
Expand Down
9 changes: 9 additions & 0 deletions src/workbench/extensions/agent/schemas/agentApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
zAgentRunMode as zGeneratedAgentRunMode,
zAgentThreadListResponse as zGeneratedAgentThreadListResponse,
zAgentToolCallData as zGeneratedAgentToolCallData,
zAgentThinkingEntry,
zAgentTurnAccepted as zGeneratedAgentTurnAccepted,
zToolCallSummary,
zWorkflowListResponse
Expand Down Expand Up @@ -143,6 +144,14 @@ export const zPersistedToolCallSummary = zToolCallSummary.extend({
})
export type PersistedToolCallSummary = z.infer<typeof zPersistedToolCallSummary>

/**
* One persisted `content.thinking` entry: a model round's thinking text,
* anchored to that round's first tool call when it made one.
*/
export const zPersistedThinkingEntry = zAgentThinkingEntry

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: this derivation and the explicit-null test come from #22109, which is still open on main. Could #22109 merge before this one, or together with it? Otherwise this release branch uses the generated schema while main keeps the hand-declared z.object, and the next backport of #21600-area code will conflict here.

.pick({ text: true })
.extend({ tool_call_id: z.string().nullish() })

/**
* The message-parse boundary's `content.tool_calls` field is deliberately
* `z.array(z.unknown())`, not the stricter `zPersistedToolCallSummary`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,85 @@ describe('normalizeAgentTranscript', () => {
])
})

it('skips malformed persisted thinking and keeps an entry whose call is missing after the calls', () => {
const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1')
message.content = {
text: 'Done',
thinking: [
{ text: 42 },
{ text: ' ' },
{ text: 'Looking it up.', tool_call_id: 'call-1' },
{ text: 'The audit row is gone.', tool_call_id: 'call-missing' }
],
tool_calls: [
{
id: 'audit-row-uuid-1',
tool_call_id: 'call-1',
tool_name: 'search_nodes',
status: 'success'
}
]
}

const transcript = normalizeAgentTranscript([message])

expect(transcript.messages[0].parts).toEqual([
{ type: 'thinking', text: 'Looking it up.', state: 'done' },
{
type: 'tool',
callId: 'call-1',
name: 'search_nodes',
state: 'done',
ok: true
},
{ type: 'thinking', text: 'The audit row is gone.', state: 'done' },
{ type: 'text', text: 'Done', state: 'done' }
])
})

it('restores persisted thinking whose tool_call_id is an explicit null', () => {
const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1')
message.content = {
text: 'Done',
thinking: [{ text: 'Final round.', tool_call_id: null }]
}

const transcript = normalizeAgentTranscript([message])

expect(transcript.messages[0].parts).toEqual([
{ type: 'thinking', text: 'Final round.', state: 'done' },
{ type: 'text', text: 'Done', state: 'done' }
])
})

it('keeps persisted thinking in order when an earlier entry names a call the transcript lacks', () => {
const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1')
message.content = {
text: 'Done',
thinking: [
{ text: 'Round one.', tool_call_id: 'call-missing' },
{ text: 'Round two.', tool_call_id: 'call-2' },
{ text: 'Final round.' }
],
tool_calls: [
{
id: 'audit-row-uuid-2',
tool_call_id: 'call-2',
tool_name: 'add_node',
status: 'success'
}
]
}

const transcript = normalizeAgentTranscript([message])

expect(
transcript.messages[0].parts.map((part) =>
part.type === 'thinking' ? part.text : part.type
)
).toEqual(['Round one.', 'Round two.', 'tool', 'Final round.', 'text'])
})

it('restores a persisted skill name', () => {
const message = row(1, 'assistant', 'turn-a', 'Done', 'row-1')
message.content = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { AgentMessages, TurnId } from '../../schemas/agentApiSchema'
import {
toTurnId,
zPersistedThinkingEntry,
zPersistedToolCallSummary
} from '../../schemas/agentApiSchema'
import type { WorkflowReference } from '../../types/workflowReference'
import type { SkillReference } from '../../types/skillReference'
import { parseSkillReferenceText } from '../../utils/skillReferenceText'
import { parseWorkflowReferences } from '../../utils/workflowReferenceText'
import type { AssistantMessage, ToolPart } from './agentMessageParts'
import type {
AssistantMessage,
ThinkingPart,
ToolPart
} from './agentMessageParts'
import {
createAssistantMessage,
isOpenAskPart,
Expand Down Expand Up @@ -331,6 +336,31 @@ function parseToolCalls(
return parsedByCallId.size > 0 ? [...parsedByCallId.values()] : undefined
}

function insertThinking(
parts: AssistantMessage['parts'],
content: Record<string, unknown> | undefined
): AssistantMessage['parts'] {
const raw = content?.thinking
if (!Array.isArray(raw)) return parts
const next = [...parts]
let unplaced: ThinkingPart[] = []
for (const entry of raw as unknown[]) {
const parsed = zPersistedThinkingEntry.safeParse(entry)
if (!parsed.success || !parsed.data.text.trim()) continue
unplaced.push({ type: 'thinking', text: parsed.data.text, state: 'done' })
const anchor = parsed.data.tool_call_id
const index = anchor
? next.findIndex(
(existing) => existing.type === 'tool' && existing.callId === anchor
)
: -1
if (index === -1) continue
next.splice(index, 0, ...unplaced)
unplaced = []
}
return [...next, ...unplaced]
}

/** A persisted tool-call `status` of `pending`/`running` is still in flight;
* anything else (`success`/`error`/`ok`, or an unrecognized string) is
* terminal — settled for good, independent of `isLive` clamping. This
Expand Down Expand Up @@ -408,6 +438,7 @@ function appendAssistantContent(
}
message.parts = appended.length > 0 ? [...parts, ...appended] : parts
}
message.parts = insertThinking(message.parts, row.content)
if (text)
message.parts = [...message.parts, { type: 'text', text, state: 'done' }]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,37 @@ describe('useAgentConversationStore', () => {
}
)

it.for([
{
name: 'persisted thinking replaces the streamed thinking',
persisted: [
{ type: 'thinking', text: 'planning the whole turn', state: 'done' },
{ type: 'text', text: 'done', state: 'done' }
] satisfies AssistantMessage['parts'],
expected: ['planning the whole turn', 'done']
},
{
name: 'a row without thinking keeps the streamed thinking',
persisted: [
{ type: 'text', text: 'done', state: 'done' }
] satisfies AssistantMessage['parts'],
expected: ['planning', 'done']
}
])('settles a recovered turn so $name', ({ persisted, expected }) => {
const store = useAgentConversationStore()
store.setThreadId('th')
store.startTurn(T1)
store.ingest(thinking('t1', 'planning'))

store.settleTurn({ threadId: 'th', messageId: T1 }, persisted)

expect(
store.messages[0].parts.map((part) =>
part.type === 'thinking' || part.type === 'text' ? part.text : part.type
)
).toEqual(expected)
})

it('splits persisted text around a local tab link when there is no tool', () => {
const store = useAgentConversationStore()
store.setThreadId('th')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,14 @@ function finishWithPersistedParts(
}
return
}
const persistedHasThinking = persistedParts.some(
(part) => part.type === 'thinking'
)
message.parts = interleaveLocalParts(
persistedParts,
anchorLocalParts(message.parts)
anchorLocalParts(message.parts).filter(
({ part }) => !persistedHasThinking || part.type !== 'thinking'
)
)
}

Expand Down
Loading