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 @@ -28,7 +28,7 @@ const TRACE = [
THINKING_ROUND_1,
'Search nodes',
THINKING_ROUND_2,
enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node'),
'Add node',
THINKING_FINAL
]

Expand All @@ -39,10 +39,16 @@ test(
'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'
}
annotation: [
{
type: 'source',
description: 'https://linear.app/comfyorg/issue/PM-717'
},
{
type: 'source',
description: 'https://linear.app/comfyorg/issue/FRONT-3803'
}
]
},
async ({ page, promptHistory, workflowSelection }) => {
await new AgentPanel(page).open()
Expand Down Expand Up @@ -148,10 +154,16 @@ test(
await expect(reopenedPanel).toBeVisible({ timeout: 30_000 })

const summary = reopenedPanel.getByRole('button', {
name: enMessages.agent.workedNotConfirmedOnCanvas,
name: enMessages.agent.worked,
exact: true
})
await expect(summary).toBeVisible({ timeout: 10_000 })
await expect(
reopenedPanel.getByText(enMessages.agent.workedNotConfirmedOnCanvas)
).toHaveCount(0)
await expect(
reopenedPanel.getByTestId('agent-canvas-unconfirmed')
).toHaveCount(0)
await expect(summary).toHaveAttribute('aria-expanded', 'false')
await expect(reopenedPanel.getByText('Search nodes')).toBeHidden()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,7 @@ describe('normalizeAgentTranscript', () => {
callId: 'call-2',
name: 'add_node',
state: 'done',
ok: true,
canvasConfirmed: false
ok: true
},
{ type: 'text', text: 'Done', state: 'done' }
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
isOpenAskPart,
toAskOrNoticePart
} from './agentMessageParts'
import { toolCanMutateCanvas } from './agentToolClassification'

type AttachmentKind = 'image' | 'video' | 'audio'

Expand Down Expand Up @@ -311,10 +310,7 @@ function parseToolCallEntry(
callId: toolCallId || id,
name: toolName,
state,
...toolPartOptionalFields(ok, skill, durationMs),
...(ok === true && toolCanMutateCanvas(toolName)
? { canvasConfirmed: false }
: {})
...toolPartOptionalFields(ok, skill, durationMs)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,146 @@ describe('useAgentConversationStore', () => {
expect(reconciledParts(store)).toEqual(storedParts())
})

const verdictWorkflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e'
const verdictTurn = { threadId: 'th', messageId: SNAPSHOT_ROW_ID }
function settleBySocket(store: ConversationStore) {
store.ingest(done(SNAPSHOT_ROW_ID))
store.reconcileSettledTurn(verdictTurn, storedParts())
}
function settleByRecoveryRead(store: ConversationStore) {
store.settleTurn(verdictTurn, storedParts())
}
function liveCanvasEdit(gate: 'awaiting' | 'unavailable') {
const store = useAgentConversationStore()
store.setCanvasSyncGate(() => gate)
store.setThreadId('th')
store.hydrate(liveSnapshot())
store.ingest(
toolCall(SNAPSHOT_ROW_ID, 'add_node', 'success', {
workflow_id: verdictWorkflowId,
op_ids: ['op-add']
})
)
return store
}
function settledTool(store: ConversationStore) {
return reconciledParts(store)?.find((part) => part.type === 'tool')
}

it.for([
{ through: 'socket', settle: settleBySocket },
{ through: 'recovery read', settle: settleByRecoveryRead }
])(
'keeps an observed canvas edit confirmed when the $through settles the turn',
({ settle }) => {
const store = liveCanvasEdit('awaiting')
store.notifyCanvasCaughtUp(
liveCanvasEvidence(verdictWorkflowId, ['op-add'])
)

settle(store)

expect(settledTool(store)).toMatchObject({
callId: 'call-add_node',
canvasConfirmed: true
})
}
)

it.for([
{ gate: 'unavailable', through: 'socket', settle: settleBySocket },
{
gate: 'unavailable',
through: 'recovery read',
settle: settleByRecoveryRead
},
{
gate: 'awaiting',
through: 'recovery read',
settle: settleByRecoveryRead
}
] as const)(
'keeps an unobserved canvas edit unconfirmed ($gate) when the $through settles the turn',
({ gate, settle }) => {
const store = liveCanvasEdit(gate)

settle(store)

expect(settledTool(store)).toMatchObject({
callId: 'call-add_node',
canvasConfirmed: false
})
}
)

it('does not move a live canvas verdict onto a different stored tool', () => {
const store = useAgentConversationStore()
store.setCanvasSyncGate(() => 'unavailable')
store.setThreadId('th')
store.hydrate(liveSnapshot())
store.ingest(toolCall(SNAPSHOT_ROW_ID, 'add_node', 'success'))

store.settleTurn({ threadId: 'th', messageId: SNAPSHOT_ROW_ID }, [
{
type: 'tool',
callId: 'row-fallback-id',
name: 'search_nodes',
state: 'done',
ok: true
}
])

expect(
reconciledParts(store)?.find((part) => part.type === 'tool')
).not.toHaveProperty('canvasConfirmed')
})

it('does not move a live canvas verdict onto a stored tool when the row omits a call', () => {
const workflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e'
function liveAddNode(n: number): AgentChatEvent {
return chat({
type: 'agent_tool_call',
data: {
tool_call_id: `live-${n}`,
tool_name: 'add_node',
status: 'success',
workflow_id: workflowId,
op_ids: [`op-${n}`],
message_id: SNAPSHOT_ROW_ID,
thread_id: 'th'
}
})
}
const store = useAgentConversationStore()
store.setCanvasSyncGate(() => 'awaiting')
store.setThreadId('th')
store.hydrate(liveSnapshot())
store.ingest(liveAddNode(1))
store.ingest(liveAddNode(2))
store.ingest(liveAddNode(3))
store.notifyCanvasCaughtUp(
liveCanvasEvidence(workflowId, ['op-1', 'op-3'])
)
function stored(callId: string): AssistantMessage['parts'][number] {
return {
type: 'tool',
callId,
name: 'add_node',
state: 'done',
ok: true
}
}

store.settleTurn({ threadId: 'th', messageId: SNAPSHOT_ROW_ID }, [
stored('row-1'),
stored('row-3')
])

expect(
reconciledParts(store)?.filter((part) => part.type === 'tool')
).toEqual([stored('row-1'), stored('row-3')])
})

it('waits for a held tool call to clear before announcing the turn', () => {
const workflowId = 'f2b4c6d8-1a3e-4b5c-9d7e-0f1a2b3c4d5e'
const store = useAgentConversationStore()
Expand Down Expand Up @@ -2330,7 +2470,11 @@ describe('useAgentConversationStore', () => {

store.retireAsk('turn-7:call-1', { status: 'retired' })

expect(reconciledParts(store)).toEqual(storedParts())
expect(reconciledParts(store)).toEqual(
storedParts().map((part) =>
part.type === 'tool' ? { ...part, canvasConfirmed: false } : part
)
)
})

it('keeps a re-registered held turn for its own read when an earlier read lands late', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import type {
import { createAgentEventTransport } from '@/workbench/extensions/agent/services/agent/agentEventTransport'
import type {
AskUserResolution,
AssistantMessage
AssistantMessage,
ToolPart
} from '@/workbench/extensions/agent/services/agent/agentMessageParts'
import {
askIdOf,
Expand Down Expand Up @@ -276,41 +277,60 @@ function storedCallIdOf(
}
}

function preserveCanvasConfirmation(
function preserveCanvasVerdicts(
liveParts: AssistantMessage['parts'],
persistedParts: AssistantMessage['parts'],
storedCallId: (callId: string) => string
persistedParts: AssistantMessage['parts']
): AssistantMessage['parts'] {
const confirmedCallIds = new Set(
liveParts.flatMap((part) =>
part.type === 'tool' && part.canvasConfirmed === true
? [storedCallId(part.callId)]
: []
)
)
return persistedParts.map((part) =>
part.type === 'tool' &&
part.ok === true &&
confirmedCallIds.has(part.callId)
? { ...part, canvasConfirmed: true }
: part
const offset = liveToolOffset(liveParts, persistedParts)
const storedCallId = storedCallIdOf(liveParts, persistedParts, offset)
const liveTools = toolPartsOf(liveParts)
const persistedCallIds = new Set(toolCallIdsOf(persistedParts))
const liveCounts = toolNameCounts(liveTools)
const alignedCounts = toolNameCounts(
toolPartsOf(persistedParts).slice(offset, offset + liveTools.length)
)
const liveByStoredCallId = new Map<string, ToolPart>()
for (const part of liveTools) {
if (
!persistedCallIds.has(part.callId) &&
liveCounts.get(part.name) !== alignedCounts.get(part.name)
)
continue
liveByStoredCallId.set(storedCallId(part.callId), part)
}
return persistedParts.map((part) => {
if (part.type !== 'tool' || part.ok !== true) return part
const live = liveByStoredCallId.get(part.callId)
if (live?.name !== part.name) return part
const canvasConfirmed = liveCanvasVerdict(live)
return canvasConfirmed === undefined ? part : { ...part, canvasConfirmed }
})
}

function toolPartsOf(parts: AssistantMessage['parts']): ToolPart[] {
return parts.filter((part): part is ToolPart => part.type === 'tool')
}

function toolNameCounts(tools: ToolPart[]): Map<string, number> {
const counts = new Map<string, number>()
for (const tool of tools)
counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1)
return counts
}

function liveCanvasVerdict(part: ToolPart): boolean | undefined {
if (part.canvasConfirmed !== undefined) return part.canvasConfirmed
return part.ok === true && part.state === 'streaming' ? false : undefined
}

function finishSnapshotTurn(
message: AssistantMessage,
persistedParts: AssistantMessage['parts']
): void {
const liveParts = message.parts
const offset = liveToolOffset(liveParts, persistedParts)
finishWithPersistedParts(
message,
preserveCanvasConfirmation(
liveParts,
persistedParts,
storedCallIdOf(liveParts, persistedParts, offset)
),
offset
persistedParts,
liveToolOffset(message.parts, persistedParts)
)
}

Expand Down Expand Up @@ -338,7 +358,7 @@ function finishWithPersistedParts(
(part) => part.type === 'thinking'
)
message.parts = interleaveLocalParts(
persistedParts,
preserveCanvasVerdicts(message.parts, persistedParts),
anchorLocalParts(message.parts)
.filter(({ part }) => !persistedHasThinking || part.type !== 'thinking')
.map((anchor) => ({
Expand Down
Loading