Files
PaperClipAI/server
DottaandPaperclip 686215ecdb fix(native-runtime): wait for prior lease release and read same-lease plugin checkpoint (#23768)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The native runtime coordinates per-turn and plugin-task runner state
across consecutive runs in
`server/src/services/native-runtime/native-session-executor.ts`.
> - A committed per-turn plugin run can finish while its async
environment lease cleanup is still running, so a follow-up run hits
`retirement_refused=prior_lease_active`, quarantines the scoped runner
root, and fails.
> - A same-run plugin-task retry that reuses the same environment lease
without restart recovery also skips `read_state` when local runner
authority is absent, which triggers `authority_indeterminate` and
quarantines the root.
> - This pull request adds a bounded wait for prior lease teardown
before refusing `prior_lease_active`, skips quarantine when the prior
lease stays active, and reads the plugin checkpoint on same-lease
retries when local authority is absent.
> - The benefit is that follow-up runs retire finished per-turn owners
cleanly instead of poisoning the scoped runner root, and same-lease
plugin retries verify their checkpoint instead of failing with
`authority_indeterminate`.

## Linked Issues or Issue Description

**What happened?**
When a per-turn plugin run commits its terminal result and its async
environment lease teardown is still in flight, a follow-up run fails
with `runner_state_identity_mismatch:
prior_owner_terminal_state_indeterminate;
retirement_refused=prior_lease_active` and quarantines the scoped runner
state root. On a same-run plugin-task retry that reuses the same
environment lease without restart recovery, `migrateRunnerdStateRoot`
skips reading the plugin runner checkpoint when local runner authority
is absent, which causes `runner_state_identity_mismatch:
authority_indeterminate` and quarantines the scoped root.

**Expected behavior**
Follow-up runs wait briefly for an in-flight prior lease release to
finish and then retire the finished per-turn owner cleanly. If the prior
lease remains active after the bounded wait, the run still refuses with
`retirement_refused=prior_lease_active` without retiring into
`continuity-breaks` and without quarantining the scoped root. Same-run
plugin-task retries on the same lease read and verify the plugin
checkpoint via `read_state` when local runner authority is absent and
unclaimed bootstrap retry does not apply.

**Steps to reproduce**
1. Commit a per-turn plugin run with a bound stop receipt while its
`environment_leases` row is still `status: "active"` and `releasedAt:
null`.
2. Start a follow-up run on a replacement lease before async lease
teardown marks the prior lease released.
3. Observe `migrateRunnerdStateRoot` refuse with
`retirement_refused=prior_lease_active` and move the scoped state root
into `quarantine/`.
4. Start a same-run plugin-task retry reusing
`identity.environmentLeaseId` without `restartRecovery` and without a
local `runner/runner-state.json`.
5. Observe `migrateRunnerdStateRoot` throw
`runner_state_identity_mismatch: authority_indeterminate` and quarantine
the root.

**Paperclip version or commit**
`master` (`f4baaa4f48`)

## What Changed

- Added `priorLeaseReleaseWait` (`5_000 ms` timeout, `100 ms` poll
interval), `isPriorRemoteSandboxLeaseEnded`, and
`waitForPriorRemoteSandboxLeasesToEnd` in
`server/src/services/native-runtime/native-session-executor.ts`.
- Updated `migrateRunnerdStateRoot` to wait for in-flight prior remote
sandbox lease release when `retireFinishedPerTurnRunnerdOwner` returns
`prior_lease_active`, and to retry retirement once if the prior lease
ends during the wait.
- Updated `migrateRunnerdStateRoot` to skip quarantining `scoped` when
retirement still refuses with `prior_lease_active`, matching the
existing non-quarantine behavior for `"active"` and `"unavailable"`.
- Updated `migrateRunnerdStateRoot` to call `read_state` and
`savePluginRunnerCheckpoint` on same-run, same-lease plugin-task retries
without `restartRecovery` when `admitUnclaimedNativeBootstrapRetry` does
not apply and `runnerdAuthorityLifecycle` is `"absent"`.
- Kept this minimal bugfix inside
`server/src/services/native-runtime/native-session-executor.ts` (which
already exceeds 1,000 lines) because it modifies existing state
migration and lease-retirement checks directly in their owning module
without introducing a new subsystem.
- Added red-then-green regression tests in
`server/src/services/native-runtime/native-session-executor.test.ts`
covering in-flight prior lease release, persistent unreleased prior
lease (no retirement and no quarantine), and same-lease plugin
checkpoint verification and mismatch rejection.

## Verification

- Verified all 4 new regression tests failed before the fix
(`retirement_refused=prior_lease_active`, `scoped` moved to quarantine,
and `authority_indeterminate`).
- Ran focused tests after the fix:
- `pnpm --dir server exec vitest run
src/services/native-runtime/native-session-executor.test.ts -t "handles
unconfirmed plugin checkpoint status on replacement lease|retires a
finished per-turn plugin owner|waits briefly for a stopped per-turn
prior lease|admits only an authorized same-run plugin checkpoint|reads
same-lease plugin checkpoint instead of quarantining|reattaches a
surviving plugin task"` (27 passed, including existing
`stopped-lease-unreleased`).
- Ran server typecheck:
  - `pnpm --dir server typecheck` (passed).

## Risks

- Low risk. The bounded wait applies only when
`retireFinishedPerTurnRunnerdOwner` refuses with `prior_lease_active`,
exits early when the prior lease holds the same `providerLeaseId` as the
current run or no prior leases exist, and re-runs the full retirement
proof before retiring. Same-lease `read_state` runs only after unclaimed
bootstrap retry checks and only when local runner authority is
`"absent"`, preserving fail-closed identity verification via
`savePluginRunnerCheckpoint`.

## Model Used

- Google DeepMind Gemini 4 Neon (Medium reasoning budget) with tool use
and code execution.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-12-04 14:08:51 -06:00
..