Skip to content

fix(task-execution): Resume a turn once after its worker stops checking in - #2298

Merged
dcramer merged 4 commits into
mainfrom
fix/resume-turn-after-lost-worker
Oct 28, 2026
Merged

dcramer merged 4 commits into
mainfrom
fix/resume-turn-after-lost-worker

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

When a running turn's lease expired, recovery failed the turn and posted "I ran into an internal error" (JUNIOR-G1). The worker was often still alive. A synchronous webFetch HTML-to-markdown conversion blocked one instance's event loop for 201s. Every turn on that instance missed its check-ins, so each one got the error. The original worker then came back with a finished answer and had to discard it.

A blocked event loop and a dead process leave the same residue, so recovery can't tell them apart. With this change, recovery parks the turn as a retry pause at its last committed boundary and resumes it right away. It no longer fails the turn.

lostWorkerResumeCount on the turn cursor limits this to one resume per turn. Without the limit, a turn that stalls every worker it runs on would move from instance to instance. A second loss fails the turn the same way as before.

Follow-ups, not in this PR: move the webFetch conversion off the main thread so one page can't block the process, and warn on event-loop delay.

via David Cramer.

--

View Junior Session [Sentry]

…ng in

An expired lease made recovery fail the running turn and post an internal
error. A blocked event loop leaves the same residue as a dead process, so a
slow tool on one instance failed every turn on it. Resume the turn once from
its last committed boundary instead. A second loss still fails the turn, so
one turn that stalls each worker cannot spread.

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

vercel Bot commented Oct 28, 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 Oct 28, 2026 8:20pm 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.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

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 8fc0c2b. Configure here.

… run

The test architecture check caps agent runs in durable-queue tests. The
resume case asserts the resumed reply and a free conversation; the later
mention is already covered by the happy path.
A resumed lost-worker turn stays paused until it writes a running
checkpoint, so a second stall before that skipped the one-resume cap.
Fail a paused retry that still carries the park marker on a later wake.

Also check the slice limit before parking so a turn with no slice left
gets the visible fallback instead of failing silently.

Co-Authored-By: David Cramer <[email protected]>
Rename park/marker wording to pause and resume message, and drop event-loop
and boundary jargon from comments, tests, and the README.

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

@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.

we probably are going to need to come back to this as its getting a little complex at this point

@dcramer
dcramer merged commit c587ed5 into main Oct 28, 2026
63 of 64 checks passed
@dcramer
dcramer deleted the fix/resume-turn-after-lost-worker branch October 28, 2026 20:36
dcramer pushed a commit that referenced this pull request Nov 2, 2026
## What failed

JUNIOR-9M: `getActiveMailboxTurnId` throws `Conversation … has multiple
active mailbox Turns` (for example `local:web:67232d03…` and
`slack:C0B595QDZLL:1793638594.088319`). Every resume attempt of the
Conversation throws, retries run out
(`conversation.work.retry.exhausted`), and the Conversation stays stuck
until a person posts again.

## Root cause

Generic conversation work can stop a Turn without closing its Turn
cursor. Examples: the heartbeat clears an expired lease after a worker
is lost (deploy rollout, freeze), lost-lease recovery runs out of
retries, or a final-attempt Run failure in `conversation-turn.ts` that
marks the Turn failed in the Conversation but leaves the checkpoint
`running`/`paused`. The next web or event message starts a new Turn next
to it. New input only superseded auth-paused Turns, so the Conversation
now has two open mailbox Turns. When the new Turn yields, its resume
fails.

## Fix

When new mailbox input starts a Turn, abandon every other open web or
event Turn, not only auth-paused ones. The OAuth prompt cleanup still
runs only when an auth-paused Turn is superseded. Slack Turns are not
mailbox Turns and stay as before.

## Why a Turn that waits to resume is not superseded

New mailbox input starts a Turn only when no yielded Turn waits:

- Every path that leaves a Turn to resume sets execution `paused`: yield
(`requestAnotherSlice`), expired-lease recovery
(`clearExpiredConversationLease`), a counted retry
(`recordConversationRetry`), and lost-lease recovery in the worker.
- `selectAttemptMessages` returns no web or event input while execution
is `paused`. A steered web message is not an attempt interrupt while
paused. Deferred input appended while paused keeps `paused`.
- The lost-worker `retry` pause from #2298 runs only in
`runNextPausedTurn` (Slack and dispatch workers). Mailbox Turns do not
use it.
- A Slack mention can interrupt a paused Conversation. It does not reach
this path: `resolveMailboxTurnWork` returns no work for Slack input, and
the Slack worker resumes the paused Turn and steers the mention into it.

So a different open mailbox Turn at this point is stale.
`conversation-turn-work.test.ts` › "resumes a paused dashboard Turn
before a deferred event" protects the selection rule: it fails when the
`paused` check in `selectAttemptMessages` is removed.

## Review evidence

An end-to-end run through `processConversationQueueMessage`, the real
conversation-turn worker and `recoverConversationWork` reproduces
JUNIOR-9M: a web Turn yields, five workers take the lease and are lost,
and the heartbeat sets the Conversation to `failed` with the cursor
still `paused`. On `main`, the next message yields and every resume
fails with "multiple active mailbox Turns". On this branch, the stale
Turn is `abandoned` and the next message yields, resumes and replies.

That test is not in this PR: `scripts/check-test-architecture.mjs` does
not allow tests in `packages/junior/tests` to run the agent or compose
the worker. Its home is
`evals/integration/conversation/lifecycle.eval.ts`, with a fixture step
that loses workers. That needs new model recordings.

Other checks:

- A late save from a lost worker of the abandoned Turn does not reopen
it after the new Turn commits history: the save fails with
`AgentHistoryBranchError` and the cursor stays `abandoned`.
- No terminal lifecycle event is written for the superseded Turn. Do not
add one here: the new Turn already wrote `turn_started`, and the
dashboard reads the latest lifecycle event to show that Junior is
responding.

## Remaining risk

- A stale Turn is closed only when new input arrives. A Conversation
that is already stuck still needs a new message, but that message now
unsticks it.
- The abandoned Turn sends no reply. The person who sent it is not told
that it was dropped. This is the "nothing finishes the Turn" gap in
#2567.
- Between the abandon and the new Turn's first history commit, a late
save from a lost worker could still reopen the old cursor. Turn saves do
not check the Conversation lease (#2567, item 8).
- JUNIOR-NP (`multiple active agent invocations` for child
Conversations) is the same class of bug. #2558 fixed it: the worker ends
every other active agent invocation of the child before it starts a new
one.

<!-- junior-request-attribution:start -->
via @dcramer.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0AHB7N2JCR%3A1793655819.784179 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0AHB7N2JCR%3A1793655819.784179)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0AHB7N2JCR%3A1793655819.784179/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Nov 4, 2026
David's answer on the reopen card (review item 0d751ba0) adopts items 1,
3 and 6. Items 2, 4, 5, 7 and 8 keep their text.

- Plain errors (P4, P10, P11, P31): one error limit of 5; pause reasons
  continue and sign_in; task 8.0 fixes the #2298 root cause first.
- One result shape (P18): replyTo is a list of location, parent and
  destination targets; no outcomes or stored adapter.
- Inbox in SQL (P7, P23, P32): the request record is the inbox entry;
  send() and the release lock the conversation row. The new core keeps
  no state in Redis.

G.1f lists the model checks these make stale. The model is unchanged.

Co-Authored-By: David Cramer <[email protected]>
BYK added a commit that referenced this pull request Nov 7, 2026
A child agent run can fail with `Agent invocation lost its worker before
it could pause`, and the parent then has to start the task over.

**Cause.** On 2026-11-07, a worker took a second child invocation 703s
into its request (trace `cd517dd5433d4a39be16732422c2f5c0`). The soft
yield was not due yet. The child Turn deadline is `turnTimeoutMs` from
the Turn start, and nothing caps it at the host request deadline. So the
run kept going until Vercel killed the function at `maxDuration` (800s).
The last span is 799s after the first one. The lease expired, and on the
next wake `projectTerminalTurn` found the Turn still `running` and
failed it. Root Slack Turns and automations resume once from their last
save in this case (#2298). Child invocations did not.

The deploy at about 16:00 UTC was not the trigger. The killed request
ran on the old release until 16:08:51. Sentry shows about 26 child
traces in 14 days that were cut off at 795–800s with no `queue.process`
span. They happen throughout the day, not at deploy times.

**Fix.**
- Cap the child Turn deadline at the worker's host request deadline
(`getTurnRequestDeadline()`), as Slack Turns do (#1381). The Turn then
times out and parks before the host kills the request.
- When the worker dies anyway, the main worker path resumes the Turn
once from its last save with `pauseLostWorkerTurn`, the same helper the
root path uses. A second loss before the resumed run saves still fails
the invocation and reports the failure to the parent. Stop and supersede
paths do not change.
- Move the lost-worker helpers from `paused-turn.ts` to `checkpoint.ts`
so both paths can use them.
- `killWorker` in the shared harness can now save the run's input before
it kills the worker. That gives the integration tests a real `running`
Turn.

**Remaining risk.** A resumed run can repeat tool work done after the
last save. Root Turns have the same limit. Web, event, and dispatch
Turns also run without a deadline cap. This PR does not change them.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3Aa59eb1d03b0a1ee74e1524f22301a86e -->

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3Aa59eb1d03b0a1ee74e1524f22301a86e/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

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

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant