Files
PaperClipAI/packages
DottaandClaude Opus 5.5 66a2cbab98 fix(runner): adopt the in-flight Codex turn after a restart lands just after turn.start (#20057)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A native Codex run survives a server restart by resuming its durable
provider session and adopting the turn that was in flight
> - The adoption rule (RC-59) only adopts a turn named by the latest
`turn.start` command's accepted result
> - Two gaps made it fail. A restart just after `turn.start` leaves that
command `execution_indeterminate` (the runner died after journaling it).
Separately, the rule demanded that Codex echo the requested turn id,
which it never does.
> - The restored session then rejected Codex's own in-flight turn as
unexpected, and the user's Retry failed on the first click
> - This pull request adopts exactly the turn the runner reconciled for
that ambiguous start, and accepts Codex's own turn ids
> - The benefit is that a restart landing right after a turn starts no
longer costs the user a second Retry

## Linked Issues or Issue Description

Found by the restart-chaos campaign: wave `w-20261108T134237-s9401`,
task t77 (Codex C1), Retry run `5358f1cb`. Related: #19852 (fast
follow-up turns), RC-59 adoption. No duplicate PR was found.

**What happened?**

The Retry run submitted its first turn at 14:05:25.171. A K2 SIGTERM
followed at .690 (graceful drain, then `resume_dead_runner`).
- The runner had journaled the `turn.start` and then died, so on restore
it settled the command as `failed / execution_indeterminate`.
- The runner's restored Codex session reconciled the ambiguous start to
the live replacement turn (`session.reconciled`,
`activeProviderTurnId`).
- Codex then sent `turn/started` for that turn. The restored driver had
no active or pending turn, because `controllerAcceptedTurnId()` was
null, and failed with `turn_binding_mismatch`.
- The run ended `native_event_replay_conflict`, and the user needed a
second Retry.

**Expected behavior**

The restored session adopts the runner-reconciled in-flight turn and
finishes it. It does not treat it as a replay conflict.

**Steps to reproduce**

1. Start a native Codex run.
2. Send SIGTERM to the server about 0.5 s after the first `turn.start`,
before runnerd confirms it.
3. Let the server restart and resume the run (`resume_dead_runner`).
4. Before this PR, the run fails with "Provider started an unexpected
turn."

**Paperclip version or commit**

`master` before this PR.

## What Changed

- `controllerAcceptedTurnStartId(commands, options)`
(runnerd-codex-transport.ts):
- **Codex and OpenCode results.** These providers assign their own turn
ids, so an accepted result never echoes the requested `turn_lab_` id,
and RC-59 adoption never matched a real Codex result. For them, the
accepted turn is now adopted only while it is also the runner's live
active turn in its authenticated `session.snapshot`. A turn the restored
runner reports lost has no live turn, so it keeps the established
restart-continuation path. ACPX still requires the echoed id.
- **Indeterminate start.** When the latest `turn.start` (after the
latest attach) failed with `execution_indeterminate`, it adopts the
runner's live active turn. Runnerd reports one only after it reconciles
an ambiguous start to a distinct replacement turn.
- The transport records the snapshot's active turn at `thread/read`,
which `recover()` reads before it evaluates adoption.
- The driver's existing RC-59 guards still apply: the turn must be in
provider history, be the only active turn, and not already be terminal.

## Verification

- Connected restart test, `runnerd-restart-continuation.test.ts` "adopts
the in-flight turn when a restart lands just after turn.start (t77)". It
uses the real runnerd and fake Codex:
  - the runner and provider die after journaling a started turn;
  - runnerd's own restore settles the command `execution_indeterminate`;
  - recovery adopts `provider-turn-1` with no second `turn/start`.
  - It fails without the fix: nothing is adopted.
- Unit cases for `controllerAcceptedTurnStartId`:
  - a Codex-assigned id is adopted only while it is the live turn;
  - an indeterminate start is adopted with the reconciled turn;
- it fails closed with no live turn, an ordinary failed start, a later
attachment, or an ACPX id mismatch.
- Regression guarded: "starts a new turn when the restored runner
reports a lost turn the checkpoint never bound" still passes.
- `runnerd-codex-transport`, `codex-app-server-driver.recovery`,
`runnerd-codex-transport-adoption`,
`runnerd-codex-transport-cold-restore`, `runnerd-restart-continuation`
and `runnerd-controller-outage`: 383/383. Runner TypeScript typecheck is
clean.
- Live: the chaos campaign will run the `runner.turn_start.*` crash
points (Codex A1/C1) and a Codex C1 K2 storm, and post the result here.

## Risks

- **Previous-release story.** No persisted shape changes. Commands
journaled by the previous release carry forward and are read with the
new rule. An indeterminate start that the runner did not reconcile to an
active turn still fails closed, as before.
- **Wider Codex adoption.** Codex adoption now actually fires. If
runnerd ever returned an accepted result that names a turn other than
the one this command started, the driver would adopt it. Runnerd binds
the result to the exact command id, so this is the same trust the rule
already placed in ACPX.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code with
tool use.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 09:26:58 -06:00
..