Skip to content

fix(logs): record a run's cost before it reads finished, and hold sync execute responses until the log is final - #8473

Merged
waleedlatif1 merged 6 commits into
stagingfrom
fix/log-ledger-before-terminal
Sep 30, 2026
Merged

waleedlatif1 merged 6 commits into
stagingfrom
fix/log-ledger-before-terminal

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • completeWorkflowExecution committed a run's terminal status and cost_total, and only then read the payer's usage and wrote its usage_log ledger. In that window a finished run had no ledger rows, and buildCostLedger reports that as cost.items: null, which the logs contract defines as "this run has no ledger" (GET /api/v2/logs/{runId} can return an unfinished log right after a sync execute completes #8354)
  • The ledger is now written before the terminal commit, together with the pre-increment usage reads the threshold email needs. The email is sent after the commit, as before
  • cost_total keeps its current final value: when the ledger write sets the exact reconciled sum, the terminal commit leaves it alone; otherwise it applies the same GREATEST as before
  • The billing safety net collapses to a single ledger write. recordExecutionUsage never throws, so a failed threshold read no longer needs a second attempt
  • The internal /api/workflows/{id}/execute non-SSE sync path now waits for the background log finalizer before responding, so a caller that reads the log right after the response sees it final. fix(v2): await run log finalization before sync execute responds #8369 does the same for v2 sync execute; together they close GET /api/v2/logs/{runId} can return an unfinished log right after a sync execute completes #8354

Type of Change

  • Bug fix

Testing

  • New real-Postgres completion-ledger-order.integration.ts: across 20 real completions, a concurrent reader never sees a finished run without its ledger, and the ledger and cost_total both match the charge. On the original code it fails in all 20 runs (a local diagnostic saw the gap last up to 15 ms)
  • New route test: the sync response is not sent until the log finalizer settles. It fails without the change
  • Unit: lib/logs, lib/billing, execute routes (internal + v2), lib/workflows/executor, background — 1718 passed
  • Integration: usage-threshold-email, usage-log, usage-reservation, pause-persistence, start-execution, secret-provenance, execution-archive-provenance, reporting-usage-cache, usage-analytics-queries, organization-activity, service-store — 99 passed
  • bun run type-check, bun run lint, block-registry check, bun run check:audits, docs-manifest:check

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 30, 2026 6:02pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Changes when and how execution costs are recorded and finalized.

The PR appears safe to merge; no outstanding or new actionable finding was established.

Summary

The PR records usage before committing a run’s terminal status and holds the internal synchronous execute response until post-execution finalization settles.

  • Adds a PostgreSQL integration test for ledger-before-terminal ordering.
  • Adds a route test for the synchronous response wait.
  • The changes since the previous review narrow the integration test’s lock-wait check to this execution and surface completion that settles before blocking.
Diagram
sequenceDiagram
  participant Caller
  participant Route
  participant Finalizer
  participant Ledger
  participant Log
  Caller->>Route: Sync execute
  Route->>Finalizer: Run workflow
  Finalizer->>Ledger: Record usage and reconcile cost
  Ledger-->>Finalizer: Ledger write settled
  Finalizer->>Log: Commit terminal status
  Log-->>Finalizer: Final log settled
  Route->>Finalizer: Wait for post-execution
  Finalizer-->>Route: Settled
  Route-->>Caller: Response
Loading

Reviews (6) · Last reviewed commit: "fix(logs): scope the ledger-lock waiter ..."

Comment thread apps/sim/app/api/workflows/[id]/execute/route.ts
Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
Comment thread apps/sim/lib/logs/execution/completion-ledger-order.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit c30d58d into staging Sep 30, 2026
23 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/log-ledger-before-terminal branch September 30, 2026 18:09

This branch was previously deployed

1 inactive deployment
Preview — 4a3b2c0f Deployed Sep 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant