fix: prevent duplicate top-level workflow traces

This commit is contained in:
Yanli 盐粒
2026-03-25 19:07:35 +08:00
parent ead33f2914
commit c446dd6164
2 changed files with 27 additions and 1 deletions

View File

@ -91,6 +91,26 @@ describe('upsertTopLevelTracingNodeOnStart', () => {
expect(tracing).toEqual([existingTrace, startedNode])
})
it('should update an existing running top-level node when the same node restarts with a new execution id', () => {
const tracing: NodeTracing[] = [
createTrace({
id: 'trace-1',
node_id: 'node-1',
status: NodeRunningStatus.Running,
}),
]
const startedNode = createTrace({
id: 'trace-2',
node_id: 'node-1',
status: NodeRunningStatus.Running,
})
const updated = upsertTopLevelTracingNodeOnStart(tracing, startedNode)
expect(updated).toBe(true)
expect(tracing).toEqual([startedNode])
})
it('should ignore nested iteration node starts even when the node id matches a top-level trace', () => {
const existingTrace = createTrace({
id: 'top-level-trace',

View File

@ -1,4 +1,5 @@
import type { NodeTracing } from '@/types/workflow'
import { NodeRunningStatus } from '../types'
const isNestedTracingNode = (trace: Pick<NodeTracing, 'iteration_id' | 'loop_id'>) => {
return Boolean(trace.iteration_id || trace.loop_id)
@ -11,7 +12,12 @@ export const upsertTopLevelTracingNodeOnStart = (
if (isNestedTracingNode(startedNode))
return false
const currentIndex = tracing.findIndex(item => item.id === startedNode.id)
const currentIndex = tracing.findIndex((item) => {
if (item.id === startedNode.id)
return true
return item.node_id === startedNode.node_id && item.status === NodeRunningStatus.Running
})
if (currentIndex > -1)
// Started events are the authoritative snapshot for an execution; merging would retain stale client-side fields.
tracing[currentIndex] = startedNode