Skip to content

fix(task-execution): Stop a late worker from replying or saving after it lost the Lease - #2901

Merged
dcramer merged 5 commits into
mainfrom
fix/turn-terminal-guard
Nov 7, 2026
Merged

dcramer merged 5 commits into
mainfrom
fix/turn-terminal-guard

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Item 3 of #2890.

What happens today

A worker can stall past its Lease. In JUNIOR-G1, a synchronous webFetch conversion blocked one instance's event loop for 201s. Its workers missed every check-in, recovery gave the Turn to another worker, and the first worker then came back with an answer.

On main, the worker that comes back still acts as the owner:

  • One stall: the new worker replies, then the old worker posts a second, out-of-date reply.
  • Two stalls: recovery fails the Turn and posts the error. The old worker then sets the Turn back to running, posts 2 more replies, and saves it as completed.

Fix

  • The agent checks in (durability.checkIn) right before it delivers a reply and right before it saves a step. A failed check-in means another worker holds the Lease, so the run stops with TurnInputCommitLostError and writes nothing.
  • The Slack turn and resume paths rethrow that error. They do not save a failure or post an error reply. The worker returns lost_lease.
  • checkIn is passed through every path that already passes shouldYield: Slack, resumed Turns, dispatch, child agents, and mailbox Turns.
  • A finished Turn (completed, failed, or abandoned) can no longer go back to running or paused. This stays as a backstop.

Proof

Two integration tests in durable-queue.test.ts hold a live worker inside a model request until its Lease expires, let recovery run the Turn on another worker, and then release the held worker. Only Slack and the model are fake. Both tests fail on main with the behavior above, and pass with this change.

The tests run the agent, so the durable-queue.test.ts test-architecture baseline goes from 8 to 10. Evals can't inject this fault.

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 7, 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 7, 2026 9:01pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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

Review screenshots in Frameshift

Automation Page · Desktop
Automation Page · Desktop
Changed
Code · Desktop
Code · Desktop
Changed
Conversation Attachment · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Show 4 more changed screenshots
Conversation Details Watched Linked Work · Mobile
Conversation Details Watched Linked Work · Mobile
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Stop Active · Mobile
Conversation Stop Active · Mobile
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@dcramer

dcramer commented Nov 7, 2026

Copy link
Copy Markdown
Member

theres no such thing as a terminal turn? you're using jargon that doesnt make sense to me

@sentry-junior sentry-junior Bot changed the title fix(turns): Keep terminal Turns terminal on late progress writes fix(turns): Stop late workers from restarting finished Turns Nov 7, 2026
@sentry-junior

sentry-junior Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Fair, it was my word, not a repo concept. I meant a Turn that already ended as completed, failed, or abandoned. I renamed the code to say that: TurnAlreadyFinishedError and isFinishedTurnState (d630574). I also rewrote the title and description in plain words.

…saves

A worker can stall past its Lease, for example when a synchronous
webFetch conversion blocks the event loop (JUNIOR-G1). Recovery then
gives the Turn to another worker. When the first worker came back, it
posted its out-of-date reply and saved over the new owner's work.

The agent now checks in right before it delivers a reply and right
before it saves a step. When the check-in fails, the run stops with
TurnInputCommitLostError. The Slack turn and resume paths rethrow it
without writing a failure or posting an error, and the worker returns
lost_lease.

Two integration tests hold a live worker inside a model request until
recovery takes its Turn. Both fail on main.

Co-Authored-By: David Cramer <[email protected]>
@sentry-junior sentry-junior Bot changed the title fix(turns): Stop late workers from restarting finished Turns fix(task-execution): Stop a late worker from replying or saving after it lost the Lease Nov 7, 2026

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

Comment thread packages/junior/src/chat/agent/index.ts Resolved
Comment thread packages/junior/src/chat/agent/index.ts Resolved
A lost Lease now stops the run before the timeout and pause paths can
save the Turn. The agent also checks in before it returns a completed
result, so a child or dispatch run with no reply cannot save completed
over another worker's Turn.
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: high PR risk score: high labels Nov 7, 2026

@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 496b65e. Configure here.

Comment thread packages/junior/src/chat/agent/index.ts Resolved
A hard timeout after an accepted reply returns a completed result from
the error path. Check the Lease there too, so a late worker cannot save
completed over a Turn that recovery already finished.
@dcramer
dcramer merged commit b45f4bf into main Nov 7, 2026
44 checks passed
@dcramer
dcramer deleted the fix/turn-terminal-guard branch November 7, 2026 21:16
dcramer pushed a commit that referenced this pull request Nov 8, 2026
…#2927)

When the last attempt of a web or event Turn failed, the mailbox worker
marked the Turn failed in the Conversation, but its Turn record stayed
`running`. A later wake with no new input then picked up that Turn again
as the open mailbox Turn and ran it a second time. This is the root
cause behind JUNIOR-9M. #2562 only closed the open Turn when new input
arrived.

Now the last failed attempt also fails the Turn record, in the same
step. `failTurnRecord` already ignores Turns that are finished or have a
newer version, so it can't overwrite another worker's result.

**Test:** a new case in `conversation-stale-turn.test.ts` runs the agent
with a fake model, so a real step is saved before Slack rejects the
reply. Then it wakes the Conversation again with no input. On `main`,
the Turn record stays `running` and the Turn gets picked up again. With
this change, the record is `failed` and no work is resolved. Evals can't
inject this failure, so the file gets a test-architecture baseline
entry, the same as #2901 did for `durable-queue.test.ts`.

Part of #2890 (Turn and execution core).

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0B595QDZLL%3A1794073557.784359 -->

--

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

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

---------

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

This branch was successfully deployed

1 active deployment
Preview – junior-docs — fdc2f772 Deployed Nov 7, 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