Skip to content
Merged
4 changes: 3 additions & 1 deletion packages/api-client/src/jobTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
* Jobs API provides a memory-optimized alternative to history API.
*/

import { zJobEntry } from '@comfyorg/ingest-types/zod'
import { zJobEntry, zJobExecutionMetaEntry } from '@comfyorg/ingest-types/zod'

Check warning on line 9 in packages/api-client/src/jobTypes.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'job-queue/domain' to zone 'ingest-api/domain' is not allowed. packages/api-client/src/jobTypes.ts -> packages/ingest-types/src/zod.gen.ts Route the import through an allowed zone or restructure the dependency.
import { z } from 'zod'

import { resultItemType } from './resultItemTypeSchema'
import { zTaskOutput } from './executionTypes'

export { zJobExecutionMetaEntry }

const zJobStatus = z.enum([
'pending',
'in_progress',
Expand Down
146 changes: 146 additions & 0 deletions src/composables/useReconnectQueueRefresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,152 @@ describe('useReconnectQueueRefresh', () => {
])
})

it('sends an expanded node output to the node it is displayed on', async () => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: {
'15.0.0.3': { images: [image] },
'9': { images: [image] }
},
execution_meta: {
'15.0.0.3': {
node_id: '15.0.0.3',
display_node: '15',
parent_node: '15',
real_node_id: '5'
},
'9': { node_id: '9', display_node: '9' },
'4': null
}
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
['executed', expect.objectContaining({ node: '9', display_node: '9' })],
[
'executed',
expect.objectContaining({ node: '15.0.0.3', display_node: '15' })
]
])
})

it.for([
['no metadata', undefined],
['malformed metadata', 'not-an-object'],
[
'metadata without a display node',
{ '15.0.0.3': { node_id: '15.0.0.3' } }
]
] as const)(
'falls back to the output key when the job detail has %s',
async ([, executionMeta]) => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: { '15.0.0.3': { images: [image] } },
execution_meta: executionMeta
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
[
'executed',
expect.objectContaining({
node: '15.0.0.3',
display_node: '15.0.0.3'
})
]
])
}
)

it('replays a display node output before the expanded output, as live', async () => {
const own = { images: [{ ...image, filename: 'own.png' }] }
const child = { images: [{ ...image, filename: 'child.png' }] }
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: { '17:15.0.0.3': child, '17:15': own },
execution_meta: {
'17:15': { node_id: '17:15', display_node: '17:15' },
'17:15.0.0.3': {
node_id: '17:15.0.0.3',
display_node: '17:15',
parent_node: '17:15'
}
}
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
[
'executed',
expect.objectContaining({
node: '17:15',
display_node: '17:15',
output: own
})
],
[
'executed',
expect.objectContaining({
node: '17:15.0.0.3',
display_node: '17:15',
output: child
})
]
])
})

it('keeps a cached output on its own node when its metadata names the node that first ran it', async () => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: { '7': { images: [image] } },
execution_meta: { '7': { node_id: '4', display_node: '4' } }
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
['executed', expect.objectContaining({ node: '7', display_node: '7' })]
])
})

it('falls back to the output key when the display node is not a node id', async () => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: { '7': { images: [image] } },
execution_meta: { '7': { node_id: '7', display_node: ':' } }
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
['executed', expect.objectContaining({ node: '7', display_node: '7' })]
])
})

it('ignores execution_meta that comes back as a list', async () => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue({
...makeJob('job-1', 'completed'),
outputs: { '0': { images: [image] } },
execution_meta: [{ display_node: '9' }]
})

await useReconnectQueueRefresh()()

expect(replayedExecutedFrames()).toEqual([
['executed', expect.objectContaining({ node: '0', display_node: '0' })]
])
})

it('lets the terminal event clear the run after its output event', async () => {
followRun('job-1')
vi.mocked(api.getJobDetail).mockResolvedValue(finishedJob)
Expand Down
49 changes: 44 additions & 5 deletions src/composables/useReconnectQueueRefresh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Semaphore, delay, withTimeout } from 'es-toolkit'
import { tryNormalizeNodeExecutionId } from '@comfyorg/graph-model/nodeIdentification'

Check warning on line 1 in src/composables/useReconnectQueueRefresh.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'job-queue/application' to zone 'graph-model/domain' is not allowed. src/composables/useReconnectQueueRefresh.ts -> packages/graph-model/src/nodeIdentification.ts Route the import through an allowed zone or restructure the dependency.
import { Semaphore, delay, partition, withTimeout } from 'es-toolkit'

import { t } from '@/i18n'
import { zJobExecutionMetaEntry } from '@comfyorg/api-client/jobTypes'
import type { ExecutionError, JobDetail } from '@comfyorg/api-client/jobTypes'
import { api } from '@/scripts/api'
import { useExecutionStore } from '@/stores/executionStore'
Expand All @@ -19,6 +21,7 @@
state: 'ended'
status: EndedStatus
outputs?: TaskOutput
displayNodes?: Map<string, string>
executionError?: ExecutionError
timestamp: number
}
Expand All @@ -41,6 +44,13 @@
}
}

function ownDisplayNode(node: string, metaEntry: unknown) {
const entry = zJobExecutionMetaEntry.safeParse(metaEntry)
if (!entry.success || !entry.data.display_node) return null
const { node_id: metaNode = node, display_node: displayNode } = entry.data
return metaNode === node ? tryNormalizeNodeExecutionId(displayNode) : null
}

/**
* Returns the handler for the `reconnected` api event.
*/
Expand Down Expand Up @@ -83,6 +93,19 @@
return historyTask.outputs
}

function displayNodesOf(detail: JobDetail | undefined) {
const meta = detail?.execution_meta
if (meta == null || typeof meta !== 'object' || Array.isArray(meta)) {
return undefined
}
const displayNodes = new Map<string, string>()
for (const [node, entry] of Object.entries(meta)) {
const displayNode = ownDisplayNode(node, entry)
if (displayNode) displayNodes.set(node, displayNode)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
}
return displayNodes
}

function completedOutcome(
detail: JobDetail | undefined,
historyTask: TaskItemImpl | undefined
Expand All @@ -93,6 +116,7 @@
state: 'ended',
status: 'completed',
outputs,
displayNodes: displayNodesOf(detail),
timestamp: outcomeTimestamp(detail, historyTask)
}
}
Expand Down Expand Up @@ -137,11 +161,24 @@
return readOutcome(jobId, historyTask)
}

function replayOutputs(jobId: JobId, outputs: TaskOutput) {
for (const [node, output] of Object.entries(outputs)) {
function replayOutputs(
jobId: JobId,
outputs: TaskOutput,
displayNodes: Map<string, string> | undefined
) {
const frames = Object.entries(outputs).map(([node, output]) => ({
node,
output,
displayNode: displayNodes?.get(node) ?? node
}))
const [own, expanded] = partition(
frames,
({ node, displayNode }) => node === displayNode
)
for (const { node, output, displayNode } of [...own, ...expanded]) {
api.dispatchCustomEvent('executed', {
node,
display_node: node,
display_node: displayNode,
output,
prompt_id: jobId
})
Expand Down Expand Up @@ -215,7 +252,9 @@

function replayOutcome(jobId: JobId, outcome: JobOutcome) {
if (outcome.state !== 'ended' || !isFollowed(jobId)) return
if (outcome.outputs) replayOutputs(jobId, outcome.outputs)
if (outcome.outputs) {
replayOutputs(jobId, outcome.outputs, outcome.displayNodes)
}
dispatchTerminal(jobId, outcome)
}

Expand Down
Loading