Skip to content

Fail runs whose provider turn never starts after a 5-minute deadline - #6736

Merged
ymichael merged 3 commits into
mainfrom
bb/deadline-for-turns-that-never-start-4363-thr_y893gtvk47
Nov 3, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/deadline-for-turns-that-never-start-4363-thr_y893gtvk47

Conversation

@ymichael

@ymichael ymichael commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A provider can accept a turn request and never start the turn (#4363: Claude Code, client/turn/requested with no turn/started after it). The daemon notices: its watchdog fires after 2 minutes, but it only posts a provider_turn_start_timeout warning and never clears or fails the pending start. Only turn/started, turn/completed or a non-retrying provider/error clear it. Nothing on the server ever ends the run, so the thread stays active ("Working…") forever. Follow-ups sent meanwhile are parked as turn-starting because the thread is active with no active turn, and that wait only clears once the thread is idle or has an active turn, so they never send.

What changed

  • Turn start deadline (server). A new periodic sweep, stopRunsWhoseTurnNeverStarted in apps/server/src/services/threads/thread-lifecycle.ts, is registered as the turn-start-deadline job in periodic-sweeps.ts. It finds threads that meet all of these conditions:

    • active and not deleted;
    • status_changed_at at least TURN_START_DEADLINE_MS (5 minutes) old;
    • no turn/completed in that window;
    • no active turn;
    • on a host whose daemon is connected.

    For each one it appends a system/error with code provider_turn_start_deadline and requests a stop with the provider-turn-idle interruption reason, which fails the run.

  • threads.status_changed_at (db). A new column, set at the only two places threads.status changes: thread creation and lifecycle transitions. Migration 0150_thread_status_changed_at was generated with drizzle-kit and backfills the column from updated_at. Renames, read state, section moves and other metadata updates don't move it, so they don't postpone the deadline.

  • No wire change. The interruption reason stays on the server; thread.stop still sends only environmentId, intent and threadId. HOST_DAEMON_PROTOCOL_VERSION and the plugin SDK are unchanged. No CLI, guide or doc surface is affected.

  • Left as is. The 2-minute watchdog text ("stopping the thread interrupts it") is unchanged. It's still true, and naming the deadline would put a value the server decides into daemon text.

Visible behaviour changes

  • A thread whose provider accepted a turn but didn't start it within 5 minutes now stops on its own and ends in error, instead of showing "Working…" indefinitely. The timeline shows the error "The provider did not start the turn within 5 minutes, so bb stopped it. Send a message to try again." and the stop marker "Stopped — provider turn stopped responding".
  • To try again, send a message from the composer; the app has no turn-level Retry button, and its placeholder already reads "Retry by sending a follow-up message". bb thread retry and the retry API resend the original input word for word, because the provider never accepted it.
  • provider-retry does not auto-retry this failure, by design for now. The turn.failed event carries no error category, so its policy declines it as not-retryable.
  • A follow-up queued while the turn was starting is kept rather than dropped. A new message or Retry releases it; the app hides "Send now" for this wait. While the thread sits in error, its row still reads "Waiting for turn to start"; that label predates this change and is left for a follow-up.
  • Threads on a host whose daemon is offline are not stopped by the deadline; host reconnect recovery owns those. The sweep also skips a host for one minute after its daemon reconnects (TURN_START_DEADLINE_RECONNECT_GRACE_MS). The hub reports the daemon as connected as soon as its socket opens, before the daemon replays events it buffered while away, so a turn that started during the disconnect could otherwise look like it never started.

How you verified

  • New tests: apps/server/test/threads/turn-start-deadline.test.ts, which drives the real runPeriodicSweeps with the clock moved forward.

    Test On main This PR
    Never-started turn → error after the deadline but not before; Retry resends the input fails (thread stays active) passes
    Same; a new message dispatches afterwards fails passes
    A rename and read-state update just before the deadline don't postpone it fails passes
    A turn that started is left alone at 10× the deadline guard passes
    A recent turn/completed is left alone guard passes
    A thread on an offline host is skipped guard passes
    A host that just reconnected is skipped for a minute, then the stuck run is stopped guard passes (fails without the grace check)

    The three guard tests can't fail on main, since main never stops a thread. I removed each check from the code in turn: removing the active-turn, turn/completed or daemon check failed exactly its own test, and switching to updated_at failed the metadata test.

  • Migration test: packages/db/test/migrate.test.ts "backfills each thread's status change time from its last update" fails without the backfill UPDATE. I added the new column to the helpers that rewind the schema for migration tests.

  • Scratch checks (not committed):

    • turn.failed is announced once with errorInfo: null and inputAccepted: false.
    • A follow-up queued as turn-starting is kept after the deadline, and "Send now" dispatches it.
  • Live QA on pnpm start:worktree: this PR and origin/main ran side by side, each with the same uncommitted fault patch. When a prompt contains a marker, Claude Code's bridge accepts the turn without delivering it, which is the Claude turn-start timeout leaves thread working and follow-up waiting #4363 shape.

    • On main the thread stayed "Working…" for 24 minutes, with its follow-up held.
    • On this PR (run with the earlier 10-minute deadline; it is now 5 minutes), threads failed 10 min 6–9 s after the turn was accepted, including one renamed and marked read at the 9-minute mark.
    • A new message typed in the browser recovered a failed thread and released its held follow-up. bb thread retry resent the same input.
    • A started turn waiting on an AskUserQuestion for 24 minutes was never stopped.
    • The migration on a copy of main's database backfilled all 7 threads from updated_at.
  • Commands:

    • pnpm exec turbo run typecheck lint --filter='...[origin/main]': 11 of 11 tasks pass.
    • pnpm exec turbo run test --filter=@bb/server --filter=@bb/db: db 653 passed; server 3859 passed, 1 skipped. The one failure, a 15 s timeout in plugin-native-server-loading.test.ts at load average 185, passes when run alone.
    • oxfmt is clean on the changed files.

Fixes #4363

🤖 Generated with Claude Code

AGENT GENERATED

ymichael added a commit that referenced this pull request Nov 2, 2026
## Human comments

## What was wrong

After a server restart, the GitHub PR Status plugin restores each
environment's PR from its database and serves it right away. It also
starts a background re-check right away, often before the host daemon
has reconnected. That re-check returned `unavailable` ("machine isn't
available"), and `store.load` replaced the environment's known PR link
with that result. The sidebar row badge disappeared and stayed gone
until something re-read that row, such as opening the thread or
refocusing the page. Observed on prod: the server restarted at 13:04:14
and the daemon reconnected at 13:04:20. Afterwards the sidebar showed
only #6849 (the thread that was opened). Six other inbox threads with
open PRs (#6760, #6755, #6736, #6728, #6727, #6734) showed no badge,
although their links and snapshots were still in the plugin database.

## What changed

- `plugins/github-pr-status/store.ts`: when a re-check of a known PR URL
comes back `unavailable`, the environment's link is no longer replaced,
so the last snapshot keeps being served. The snapshot's `fetchedAt` is
still bumped, so the regular poll retries it. Discovery reads (no known
URL) behave as before.
- Behaviour change: `bb pull-request show` for an environment whose
machine is offline now prints the last PR bb saw, or `unavailable` when
it has none. Actions (`ready`/`draft`/`merge`) still refuse with the
machine message. The skill doc and README are updated.

## How you verified

- New store test, "keeps serving a restored PR when its machine is
unreachable after a restart": it restores a persisted PR, makes the
first re-check return `unavailable`, and checks that the PR is still
served, nothing is published, and the next successful check publishes
the update. It fails without the fix (`expected { outcome: 'unavailable'
} to deeply equal { outcome: 'available' }`) and passes with it.
- `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-bb--github-pr-status`: 183/183 tests pass, typecheck
and lint clean.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/deadline-for-turns-that-never-start-4363-thr_y893gtvk47 branch from e5519e8 to d082344 Compare November 2, 2026 22:48
A provider can accept a turn request and never start the turn. The daemon's
2-minute watchdog only posts provider_turn_start_timeout, so the thread stayed
active forever and later messages queued behind "Waiting for turn to start"
(#4363).

A periodic sweep now finds active threads whose status has not changed for
TURN_START_DEADLINE_MS, with no turn/completed inside that window, no active
turn, and a connected host daemon. It appends a provider_turn_start_deadline
system/error and requests a stop with the provider-turn-idle interruption
reason, so the run fails into error and Retry or a new message sends again.

threads.status_changed_at records when the status last changed, so renames,
read state and section moves do not postpone the deadline. The migration
backfills it from updated_at.

Refs #4363

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/deadline-for-turns-that-never-start-4363-thr_y893gtvk47 branch from d082344 to db91783 Compare November 3, 2026 00:09
ymichael and others added 2 commits November 2, 2026 16:45
…deadline

The hub reports a daemon as connected as soon as its socket opens, before
the daemon replays events it buffered while disconnected. A run whose
turn started while the host was away could look like it never started
during that window, and the sweep would stop it.

The hub now records when each daemon session connected, and the sweep
skips hosts whose daemon connected less than
TURN_START_DEADLINE_RECONNECT_GRACE_MS (one minute) ago.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Fail runs whose provider turn never starts after a 10-minute deadline Fail runs whose provider turn never starts after a 5-minute deadline Nov 3, 2026
@ymichael
ymichael merged commit b809a49 into main Nov 3, 2026
33 checks passed
@ymichael
ymichael deleted the bb/deadline-for-turns-that-never-start-4363-thr_y893gtvk47 branch November 3, 2026 18:27
ymichael added a commit that referenced this pull request Nov 4, 2026
…iled (#7001)

## Human comments

## What was wrong

A steer sent while a turn is starting is parked in the queue as
`turn-starting`. If the provider never starts that turn, the run fails:
Codex's `turn/start` times out, or the 5-minute turn-start deadline from
#6736 fires. The thread then goes to `error`. The parked row correctly
stays queued. It goes out with the next turn, which starts either when
the user sends another message or when the provider recovers and starts
the turn that timed out. But `describeQueuedMessageWait` chose the row's
label from `waitingOn` alone, so on the errored thread it kept saying
"Waiting for turn to start" after that start had already failed.

## What changed

- `apps/app/src/lib/queued-message-wait.ts`: the label function now
takes the thread status. On an errored thread, a `turn-starting` row
reads "Turn didn't start · sends on the next turn". This covers both
ways the next turn can start, and it fits the collapsed queue header at
390 px without truncation. The label is unchanged in every other state.
- `QueuedMessagesList` and `LazyQueuedMessagesList` pass `threadStatus`
to the row wait line and to the collapsed header's wait line.
- `ThreadDetailPromptArea` passes `thread.status` and adds it to the
memo dependencies, so the label changes while the thread is open,
without a reload.

This is app-only: no wire, protocol, CLI or SDK changes.

## How you verified

**Live, `pnpm start:worktree` with real Codex.** I froze the thread's
`codex app-server` with `kill -STOP <pid>`, sent one message
(`turn/start` hangs), then a second (parked as `turn-starting`). After
30 s the `turn/start` request timed out (`JSON-RPC request timed out:
turn/start`) and the thread went to `error` with the second message
still queued. I ran the same steps on `origin/main` and on this branch,
both against the same data dir, in light mode.

- **Before (main):** the errored thread's queue row still says "Waiting
for turn to start".
- **After (this branch):** the page stayed open from the active state
through the error, with no reload. The row changed from "Waiting for
turn to start" to "Turn didn't start · sends on the next turn". After
`kill -CONT <pid>`, Codex started the timed-out turn with no new
message, and the parked steer joined it, was answered, and left the
queue empty. The first version of this branch said "sends with your next
message". Two things showed that was wrong: the text was cut off in the
390 px header, and on resume the steer went out about 4 s before my next
message.

| | Desktop (1440) | Phone (390) |
|---|---|---|
| Before | <img
src="https://raw.githubusercontent.com/get-bb/bb/0af99528e46de25d95e8b6ac0a7a961a4049ed83/before-desktop.jpg"
width="480"> | <img
src="https://raw.githubusercontent.com/get-bb/bb/0af99528e46de25d95e8b6ac0a7a961a4049ed83/before-phone.jpg"
width="200"> |
| After | <img
src="https://raw.githubusercontent.com/get-bb/bb/0af99528e46de25d95e8b6ac0a7a961a4049ed83/after-desktop.jpg"
width="480"> | <img
src="https://raw.githubusercontent.com/get-bb/bb/0af99528e46de25d95e8b6ac0a7a961a4049ed83/after-phone.jpg"
width="200"> |

**Tests:** `queued-message-wait.test.ts` has a new case for the errored
`turn-starting` label, and the list tests were updated for the new prop.
`vitest run` on `queued-message-wait.test.ts`,
`QueuedMessagesList.test.tsx` and `LazyQueuedMessagesList.test.tsx`
passes: 3 files, 87 tests. `pnpm exec turbo run typecheck
--filter=@bb/app` passes. CI runs the full suites.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Claude turn-start timeout leaves thread working and follow-up waiting

1 participant