Commit 9a31c7d
authored
improvement(processing): reduce redundant DB queries in execution preprocessing (#3320)
* improvement(processing): reduce redundant DB queries in execution preprocessing
* improvement(processing): add defensive ID check for prefetched workflow record
* improvement(processing): fix type safety in execution error logging
Replace `as any` cast in non-SSE error path with proper `buildTraceSpans()`
transformation, matching the SSE error path. Remove redundant `as any` cast
in preprocessing.ts where the types already align.
* improvement(processing): replace `as any` casts with proper types in logging
- logger.ts: cast JSONB cost column to `WorkflowExecutionLog['cost']` instead
of `any` in both `completeWorkflowExecution` and `getWorkflowExecution`
- logger.ts: replace `(orgUsageBefore as any)?.toString?.()` with `String()`
since COALESCE guarantees a non-null SQL aggregate value
- logging-session.ts: cast JSONB cost to `AccumulatedCost` (the local
interface) instead of `any` in `loadExistingCost`
* improvement(processing): use exported HighestPrioritySubscription type in usage.ts
Replace inline `Awaited<ReturnType<typeof getHighestPrioritySubscription>>`
with the already-exported `HighestPrioritySubscription` type alias.
* improvement(processing): replace remaining `as any` casts with proper types
- preprocessing.ts: use exported `HighestPrioritySubscription` type instead
of redeclaring via `Awaited<ReturnType<...>>`
- deploy/route.ts, status/route.ts: cast `hasWorkflowChanged` args to
`WorkflowState` instead of `any` (JSONB + object literal narrowing)
- state/route.ts: type block sanitization and save with `BlockState` and
`WorkflowState` instead of `any`
- search-suggestions.ts: remove 8 unnecessary `as any` casts on `'date'`
literal that already satisfies the `Suggestion['category']` union
* fix(processing): prevent double-billing race in LoggingSession completion
When executeWorkflowCore throws, its catch block fire-and-forgets
safeCompleteWithError, then re-throws. The caller's catch block also
fire-and-forgets safeCompleteWithError on the same LoggingSession. Both
check this.completed (still false) before either's async DB write resolves,
so both proceed to completeWorkflowExecution which uses additive SQL for
billing — doubling the charged cost on every failed execution.
Fix: add a synchronous `completing` flag set immediately before the async
work begins. This blocks concurrent callers at the guard check. On failure,
the flag is reset so the safe* fallback path (completeWithCostOnlyLog) can
still attempt recovery.
* fix(processing): unblock error responses and isolate run-count failures
Remove unnecessary `await waitForCompletion()` from non-SSE and SSE error
paths where no `markAsFailed()` follows — these were blocking error responses
on log persistence for no reason. Wrap `updateWorkflowRunCounts` in its own
try/catch so a run-count DB failure cannot prevent session completion, billing,
and trace span persistence.
* improvement(processing): remove dead setupExecutor method
The method body was just a debug log with an `any` parameter — logging
now works entirely through trace spans with no executor integration.
* remove logger.debug
* fix(processing): guard completionPromise as write-once (singleton promise)
Prevent concurrent safeComplete* calls from overwriting completionPromise
with a no-op. The guard now lives at the assignment site — if a completion
is already in-flight, return its promise instead of starting a new one.
This ensures waitForCompletion() always awaits the real work.
* improvement(processing): remove empty else/catch blocks left by debug log cleanup
* fix(processing): enforce waitForCompletion inside markAsFailed to prevent completion races
Move waitForCompletion() into markAsFailed() so every call site is
automatically safe against in-flight fire-and-forget completions.
Remove the now-redundant external waitForCompletion() calls in route.ts.
* fix(processing): reset completing flag on fallback failure, clean up empty catch
- completeWithCostOnlyLog now resets this.completing = false when
the fallback itself fails, preventing a permanently stuck session
- Use _disconnectError in MCP test-connection to signal intentional ignore
* fix(processing): restore disconnect error logging in MCP test-connection
Revert unrelated debug log removal — this file isn't part of the
processing improvements and the log aids connection leak detection.
* fix(processing): address audit findings across branch
- preprocessing.ts: use undefined (not null) for failed subscription
fetch so getUserUsageLimit does a fresh lookup instead of silently
falling back to free-tier limits
- deployed/route.ts: log warning on loadDeployedWorkflowState failure
instead of silently swallowing the error
- schedule-execution.ts: remove dead successLog parameter and all
call-site arguments left over from logger.debug cleanup
- mcp/middleware.ts: drop unused error binding in empty catch
- audit/log.ts, wand.ts: promote logger.debug to logger.warn in catch
blocks where these are the only failure signal
* revert: undo unnecessary subscription null→undefined change
getHighestPrioritySubscription never throws (it catches internally
and returns null), so the catch block in preprocessExecution is dead
code. The null vs undefined distinction doesn't matter and the
coercions added unnecessary complexity.
* improvement(processing): remove dead try/catch around getHighestPrioritySubscription
getHighestPrioritySubscription catches internally and returns null
on error, so the wrapping try/catch was unreachable dead code.
* improvement(processing): remove dead getSnapshotByHash method
No longer called after createSnapshotWithDeduplication was refactored
to use a single upsert instead of select-then-insert.
---------1 parent 9e817bc commit 9a31c7d
54 files changed
Lines changed: 335 additions & 614 deletions
File tree
- apps/sim
- app/api
- billing/update-cost
- chat/[identifier]
- otp
- creators
- form/[identifier]
- help
- knowledge/search
- logs/execution/[executionId]
- mcp/tools/execute
- schedules
- [id]
- execute
- templates
- [id]
- star
- users/me/api-keys/[id]
- wand
- webhooks
- [id]
- workflows/[id]
- chat/status
- deployed
- deploy
- execute
- state
- status
- background
- lib
- audit
- billing
- calculations
- core
- organizations
- execution
- guardrails
- logs
- execution
- snapshot
- mcp
- table/llm
- webhooks
- workflows
- executor
- persistence
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | 36 | | |
38 | 37 | | |
39 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
121 | | - | |
122 | 120 | | |
123 | 121 | | |
124 | 122 | | |
| |||
211 | 209 | | |
212 | 210 | | |
213 | 211 | | |
214 | | - | |
215 | | - | |
216 | 212 | | |
217 | 213 | | |
218 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | 45 | | |
48 | 46 | | |
49 | 47 | | |
| |||
294 | 292 | | |
295 | 293 | | |
296 | 294 | | |
297 | | - | |
298 | | - | |
299 | 295 | | |
300 | 296 | | |
301 | 297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | 98 | | |
104 | 99 | | |
105 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | 61 | | |
64 | 62 | | |
65 | 63 | | |
| |||
300 | 298 | | |
301 | 299 | | |
302 | 300 | | |
303 | | - | |
304 | | - | |
305 | 301 | | |
306 | 302 | | |
307 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
81 | | - | |
82 | 80 | | |
83 | 81 | | |
84 | 82 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
189 | | - | |
190 | | - | |
191 | 189 | | |
192 | 190 | | |
193 | 191 | | |
| |||
220 | 218 | | |
221 | 219 | | |
222 | 220 | | |
223 | | - | |
224 | 221 | | |
225 | 222 | | |
226 | 223 | | |
| |||
244 | 241 | | |
245 | 242 | | |
246 | 243 | | |
247 | | - | |
248 | 244 | | |
249 | 245 | | |
250 | 246 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | | - | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
| |||
140 | 137 | | |
141 | 138 | | |
142 | 139 | | |
143 | | - | |
144 | 140 | | |
145 | 141 | | |
146 | 142 | | |
147 | 143 | | |
148 | 144 | | |
149 | 145 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | 146 | | |
155 | 147 | | |
156 | 148 | | |
| |||
208 | 200 | | |
209 | 201 | | |
210 | 202 | | |
211 | | - | |
212 | 203 | | |
213 | 204 | | |
214 | 205 | | |
| |||
287 | 278 | | |
288 | 279 | | |
289 | 280 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | 281 | | |
294 | 282 | | |
295 | 283 | | |
| |||
380 | 368 | | |
381 | 369 | | |
382 | 370 | | |
383 | | - | |
384 | | - | |
385 | 371 | | |
386 | 372 | | |
387 | 373 | | |
| |||
431 | 417 | | |
432 | 418 | | |
433 | 419 | | |
434 | | - | |
435 | | - | |
436 | 420 | | |
437 | 421 | | |
438 | 422 | | |
| |||
489 | 473 | | |
490 | 474 | | |
491 | 475 | | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | 476 | | |
498 | 477 | | |
499 | 478 | | |
500 | 479 | | |
501 | | - | |
502 | 480 | | |
503 | 481 | | |
504 | 482 | | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | 483 | | |
510 | 484 | | |
511 | 485 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
| |||
125 | 121 | | |
126 | 122 | | |
127 | 123 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | 124 | | |
134 | 125 | | |
135 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
87 | 86 | | |
88 | 87 | | |
89 | 88 | | |
| |||
0 commit comments