Skip to content

fix(mothership): report a browser-claimed workflow tool from its settled execution - #8481

Merged
waleedlatif1 merged 6 commits into
stagingfrom
fix/client-claimed-tool-liveness
Oct 1, 2026
Merged

waleedlatif1 merged 6 commits into
stagingfrom
fix/client-claimed-tool-liveness

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Problem. When the browser claims a Chat workflow tool (run_workflow and friends), a tab that closes, loses its network, or drops its pagehide beacon leaves the Chat turn waiting for the full client wait, even though the workflow itself finished.

Root cause. /api/workflows/[id]/execute takes the single-winner execution claim for the tool call and keeps running the workflow after the browser detaches. Only the browser's confirmation marked the tool call complete, though. With no confirmation, the call stayed running under the execution's claim until the worker swept it.

Fix. Once a claimed execution settles, the execute route reports its outcome. reportSettledClientWorkflowTool reads the trusted execution log, builds the same structural completion the browser would send, and records it through a new completeClientWorkflowToolCall. That write only applies while the call is still running under this execution's claim (completeClaimedAsyncToolCall). A confirmation is published only when this write wins, so a browser report or background detach that lands first is kept and the waiting turn never gets two deliveries. If the report fails, the route logs a warning and leaves the call to the existing client path.

Behaviour changes

  • A browser-claimed workflow tool now reaches the waiting Chat turn as soon as its execution settles, whether or not the browser is still there.
  • A browser report or a pagehide background detach that lands first still wins. The settled-execution report does nothing in that case.
  • A queued async run (async: true) is moved to the background by the route once it is queued, the same report the browser sends after the 202.
  • A bound execution that ended before it wrote any log is reported as failed, matching the browser's stream-error report. A paused run (non-terminal log) is still left to the client.
  • A call claimed by a different execution is never completed by a stray execution.
  • Workflows that were not claimed for a Copilot tool call run exactly as before.

Test plan

  • workflow-client-settlement.integration.ts against real Postgres: a completed, failed, or cancelled run reaches the waiting turn when the browser never reports; an earlier browser report is kept; a pagehide detach is kept; a stray execution cannot complete another execution's call; an execution with no log is reported failed while a paused one is left alone; a queued async run is moved to the background, and a browser report that landed first is kept
  • Related unit suites (app/api/workflows, lib/mothership/request/tools, lib/mothership/async-runs) pass
  • bun run type-check, biome, check:test-patterns, check:api-validation:strict
  • Staging: close the tab mid-run on a Chat run_workflow call and confirm the turn continues when the run settles

…led execution

When a browser claims a Chat workflow tool, the execute route runs the
workflow and keeps running it after the browser detaches, but only the
browser's confirmation completed the tool call. A tab that closed, lost its
network, or dropped its pagehide beacon left the Chat turn waiting for the
full client wait while the worker swept the call.

The execute route now records the bound execution's structural completion
itself once it settles, guarded on the call still running under that
execution's claim, so a browser report or background detach that lands first
is kept and nothing is delivered twice.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
docs Ready Ready Preview Sep 30, 2026 11:57pm UTC

Request Review

@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

@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/app/api/workflows/[id]/execute/route.ts
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds reporting of workflow tool outcomes from settled executions.

The PR appears safe to merge; no outstanding finding or new blocking issue remains.

Summary

The PR reports browser-claimed workflow tool outcomes from the settled execution, and moves queued async runs to the background without relying on a surviving browser. The latest revision extends the integration test to check Redis publication and first-report-wins behavior.

Diagram
sequenceDiagram
  participant Browser
  participant Route as Execute route
  participant DB as Execution log and tool call
  participant Chat as Chat waiter
  Browser->>Route: Claim and start workflow
  Route->>DB: Run and record execution
  alt Settled execution
    Route->>DB: Complete only under matching claim
  else Queued async run
    Route->>DB: Detach to background
  end
  DB-->>Chat: Publish winning confirmation
Loading

Reviews (4) · Last reviewed commit: "Merge remote-tracking branch 'origin/sta..."

Comment thread apps/sim/app/api/workflows/[id]/execute/route.ts
Comment thread apps/sim/lib/mothership/request/tools/workflow-client-settlement.ts Outdated
Comment thread apps/sim/lib/mothership/request/tools/workflow-client-settlement.ts Outdated
Comment thread apps/sim/lib/mothership/request/tools/workflow-client-settlement.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 6 files

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/app/api/workflows/[id]/execute/route.ts
@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 6 files

Confidence score: 5/5

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

Re-trigger cubic

@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 6 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 21b2972 into staging Oct 1, 2026
32 checks passed

This branch was successfully deployed

1 active deployment
Preview — 6e9ee1b3 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