Skip to content

fix(agent-invocations): Tell the parent when a child's work stops after its tries run out - #2627

Merged
dcramer merged 8 commits into
mainfrom
fix/agent-invocation-stopped-work
Nov 4, 2026
Merged

dcramer merged 8 commits into
mainfrom
fix/agent-invocation-stopped-work

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

A child worker that is killed (function timeout, OOM, deploy) never runs its own failure path. Only the heartbeat sees the expired lease. After CONVERSATION_WORK_MAX_RETRIES expired leases, recoverConversationWork marks the child's work failed and only logs conversation.work.retry.exhausted. The agent invocation stays running or paused, so the parent never gets a result. The open invocation also makes later results from that child replaced until new work reaches the child (#2558).

Fix

  • recoverConversationWork takes an onWorkStopped callback. It calls it when the tries run out, and again on each later heartbeat while the work stays failed. The conversation leaves the recovery scan only after the callback succeeds. task-execution stays unaware of agent invocations.
  • runHeartbeat passes endAgentInvocationsOfStoppedWork. It ends each running or paused invocation of that child the same way endOtherActiveAgentInvocation does: a running Turn fails with "lost its worker", and a paused Turn is abandoned with a failed result. Stopped work drops its mailbox, so a pending invocation whose input was already appended also fails ("This task did not start"). A pending invocation whose append has not run yet is left alone, because that append starts the work again. All results use reportNow, so none is replaced.
  • The result phase of the same heartbeat sends the results to the parent.

Injected faults

Until now, tests of a dead worker wrote by hand the state a dead worker leaves (store calls, startConversationWork with no release, hand-built checkpoints). That tests the heartbeat against a guessed state, not against what the worker leaves.

  • createConversationWorkHarness().killWorker(message) runs the production worker and kills it when the agent runner starts. The run never returns, and the worker keeps its lease. With fake setInterval, the dead worker never checks in.
  • runHeartbeatAfterLeasesExpire(queue) moves the clock past the lease TTL and runs the production runHeartbeat. A test that calls only recoverConversationWork misses this bug, because the result is sent in a later heartbeat phase.
  • policies/testing.md gets an "Injected faults" section: inject at the production edge, use a named harness fault, mark it with Injected fault:, and write failure state by hand only when no harness fault can cause it.

agent-invocation-worker-crash.test.ts kills every try of a child and runs the heartbeat after each one. On the last try the parent sends the child a follow-up task. The test checks that the parent gets a failed result for both. It fails on main and passes with the fix.

Not moved yet

The "worker dies mid-run" cases in durable-queue.test.ts and the lost-resume case in agent-dispatch-work.test.ts kill the worker after a tool result is saved. killWorker cannot reach that point yet, so those tests still write their state by hand. They can move onto the harness when it gets a kill point after a save. The paused case of #2558's leftover-invocation test needs the same kill point.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 3, 2026 11:55pm UTC

Request Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

6 screenshot changes — 6 changed · 0 added · 0 removed

Review screenshots in Frameshift

Conversation Attachment · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment · Mobile
Conversation Attachment · Mobile
Changed
Conversation Attachment Modal · Desktop
Conversation Attachment Modal · Desktop
Changed
Conversation Share Details · Desktop
Conversation Share Details · Desktop
Changed
Conversation Subagents · Desktop
Conversation Subagents · Desktop
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@dcramer dcramer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

im a little worried we have over engineered this and we're including yet again a bunch more jargon, another branch of execution, etc

Run the cleanup before the heartbeat stops the work, so the existing
expired-lease retry covers a failed cleanup. This removes the failed-status
branch, which never ran because stopped work leaves the active index.

End every unfinished invocation of the child with one message, and reuse
one status-filtered invocation query instead of adding a second one.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Run beforeWorkStops inside clearExpiredConversationLease, after it confirms
the lease expired. A late worker check-in now waits for the lock and then
finds no lease, so it cannot keep running after its invocations end. This
also removes the retry-count check that the heartbeat copied.
@blacksmith-sh

This comment has been minimized.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

…rt again

A pending invocation whose input has not reached the child's mailbox is
appended after the stop, and that append starts new work for the child.
Failing it told the parent the task failed instead of running it.

Co-Authored-By: David Cramer <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f11dcbc. Configure here.

…mailbox

The enqueue writes the child's mailbox before it marks the invocation
appended. A stop between those writes dropped input that the mailbox
already recorded, so the later append was a duplicate and the invocation
stayed open. The cleanup now reads the recorded message ids under the
stop's lock and ends every invocation whose input reached the mailbox.
@dcramer
dcramer merged commit 6286698 into main Nov 4, 2026
46 checks passed
@dcramer
dcramer deleted the fix/agent-invocation-stopped-work branch November 4, 2026 00:05

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 1a8a84ec Deployed Nov 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant