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
2 changes: 1 addition & 1 deletion apps/sim/app/workspace/[workspaceId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { redirect } from 'next/navigation'
import { ToastProvider } from '@/components/emcn'
import { getSession } from '@/lib/auth'
import { ImpersonationBanner } from '@/app/workspace/[workspaceId]/components/impersonation-banner'
import { NavTour } from '@/app/workspace/[workspaceId]/components/product-tour'
import { ImpersonationBanner } from '@/app/workspace/[workspaceId]/impersonation-banner'
import { GlobalCommandsProvider } from '@/app/workspace/[workspaceId]/providers/global-commands-provider'
import { ProviderModelsLoader } from '@/app/workspace/[workspaceId]/providers/provider-models-loader'
import { SettingsLoader } from '@/app/workspace/[workspaceId]/providers/settings-loader'
Expand Down
69 changes: 69 additions & 0 deletions apps/sim/lib/logs/execution/logging-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,75 @@ describe('LoggingSession completion retries', () => {
})
})

describe('completeWithError cancelled-status guard', () => {
beforeEach(() => {
vi.clearAllMocks()
dbMocks.updateWhere.mockResolvedValue(undefined)
dbMocks.execute.mockResolvedValue(undefined)
})

it('skips writing failed and marks session complete when DB status is already cancelled', async () => {
dbMocks.selectLimit.mockResolvedValue([{ status: 'cancelled' }])
const session = new LoggingSession('workflow-1', 'execution-1', 'api', 'req-1')

await session.safeCompleteWithError({ error: { message: 'block errored mid-cancel' } })

expect(completeWorkflowExecutionMock).not.toHaveBeenCalled()
expect(session.hasCompleted()).toBe(true)
})

it('writes failed when DB status is running (no cancel in flight)', async () => {
dbMocks.selectLimit.mockResolvedValue([{ status: 'running' }])
completeWorkflowExecutionMock.mockResolvedValue({})
const session = new LoggingSession('workflow-1', 'execution-1', 'api', 'req-1')

await session.safeCompleteWithError({ error: { message: 'genuine block failure' } })

expect(completeWorkflowExecutionMock).toHaveBeenCalledWith(
expect.objectContaining({ status: 'failed' })
)
expect(session.hasCompleted()).toBe(true)
})

it('writes failed when no execution log exists yet', async () => {
dbMocks.selectLimit.mockResolvedValue([])
completeWorkflowExecutionMock.mockResolvedValue({})
const session = new LoggingSession('workflow-1', 'execution-1', 'api', 'req-1')

await session.safeCompleteWithError({ error: { message: 'pre-log error' } })

expect(completeWorkflowExecutionMock).toHaveBeenCalledWith(
expect.objectContaining({ status: 'failed' })
)
})

it('deduplicates all subsequent completion attempts after guard early-return', async () => {
dbMocks.selectLimit.mockResolvedValue([{ status: 'cancelled' }])
completeWorkflowExecutionMock.mockResolvedValue({})
const session = new LoggingSession('workflow-1', 'execution-1', 'api', 'req-1')

await session.safeCompleteWithError({ error: { message: 'error 1' } })
await session.safeCompleteWithError({ error: { message: 'error 2' } })
await session.safeComplete({ finalOutput: { ok: true } })

expect(completeWorkflowExecutionMock).not.toHaveBeenCalled()
expect(session.hasCompleted()).toBe(true)
})

it('falls through to cost-only fallback when the DB check itself throws', async () => {
dbMocks.selectLimit.mockRejectedValueOnce(new Error('DB connection lost'))
completeWorkflowExecutionMock.mockResolvedValue({})
const session = new LoggingSession('workflow-1', 'execution-1', 'api', 'req-1')

await session.safeCompleteWithError({ error: { message: 'block failed' } })

expect(completeWorkflowExecutionMock).toHaveBeenCalledWith(
expect.objectContaining({ finalizationPath: 'force_failed' })
)
expect(session.hasCompleted()).toBe(true)
})
})

describe('LoggingSession.markExecutionAsFailed workflowId scoping', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
17 changes: 17 additions & 0 deletions apps/sim/lib/logs/execution/logging-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,23 @@ export class LoggingSession {
this.completing = true

try {
const currentLog = await db
.select({ status: workflowExecutionLogs.status })
.from(workflowExecutionLogs)
.where(
and(
eq(workflowExecutionLogs.workflowId, this.workflowId),
eq(workflowExecutionLogs.executionId, this.executionId)
)
)
.limit(1)
.then((rows) => rows[0])

if (currentLog?.status === 'cancelled') {
this.completed = true
return
}

const { endedAt, totalDurationMs, error, traceSpans, skipCost } = params

const endTime = endedAt ? new Date(endedAt) : new Date()
Expand Down
Loading