Files
PaperClipAI/server/src/services
DottaandPaperclip c349b060db fix(runner): stop polling plugin task status during active per-turn runs (#23758)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - An environment plugin can run `paperclip_runner` tasks in a remote
computer or sandbox
> - After a task starts, the Paperclip Runner Protocol (PRP) WebSocket
checks if the Runner is alive and gives it a reconnect window across
network or service restarts
> - Warm plugin tasks stop polling the plugin `status` operation once
the task enters the `running` phase, as added in #17390
> - Per-turn plugin tasks, such as Google Antigravity runs, still polled
`status` every 500 ms for the full run and failed the run after a
15-second `status` outage
> - When the plugin host or Launcher restarted for more than 15 seconds
while the Runner and its PRP connection stayed healthy, every active
per-turn run failed as `native_provider_terminal_failed`
> - This pull request stops continuous `status` polling once a per-turn
plugin task is running, and checks `status` only on PRP reconnect, close
quiescence, or stop settlement
> - The benefit is that active per-turn runs survive temporary plugin
`status` outages and do not send recurring database and RPC status polls
during a turn

## Linked Issues or Issue Description

Refs #17029, #17390

**What happened?**

Active per-turn `paperclip_runner` tasks in a plugin environment
continued to call `environmentTask` with `{ kind: "status" }` every 500
ms after the task reached `phase: "running"` and authenticated over PRP.
When `status` calls failed for 15 seconds (for example during a plugin
worker or Launcher rollout) while the Runner and its PRP WebSocket
stayed alive, `createPluginRunnerTaskOwner` rejected
`registration.failure` with `Plugin Runner operation failed; reconcile
the saved task`. The session transport closed with
`notification_transport_failed` (`recoverable: false`), which failed the
run as `native_provider_terminal_failed` and stopped the healthy Runner.

**Expected behavior**

Once a per-turn plugin Runner task reaches `phase: "running"`, PRP owns
live failure detection, just as it does for warm plugin tasks. Temporary
`status` outages during an active turn must not fail a healthy PRP
session, and Core must not poll `status` every 500 ms throughout the
turn.

**Steps to reproduce**

1. Start a `per_turn` native Runner session through
`createPluginRunnerTaskOwner` (for example an ACPX `antigravity` or
`codex` per-turn session).
2. Wait for `registration.ready()` so the Runner task is in `phase:
"running"` and PRP ingress is connected.
3. Make the plugin `status` operation fail for more than 15 seconds
while PRP ingress stays healthy.
4. Before this fix, `registration.failure` rejects with `Plugin Runner
operation failed; reconcile the saved task` and terminates the active
turn.

**Paperclip version or commit**

Reproduced on `master` (`a6f7bcced3`).

**Deployment mode**

Self-hosted server and managed plugin sandbox environments
(`authenticated` and `local_trusted`).

## What Changed

- Stop the startup `readStatus()` loop in `createPluginRunnerTaskOwner`
as soon as a per-turn task reaches `phase: "running"`, matching warm
tasks so PRP owns live failure detection.
- Deduplicate `stop` dispatch (`sendStopOnce()`) and status probing
(`probeStatusOnce()` / `pollUntilStopped()`) across startup abort,
`child.kill()`, `checkpoint()`, reconnect probing, and close quiescence
so `stop` is sent once and status settlement follows one policy.
- Check `status` once without failing the run on PRP reconnect attempts,
including the first post-authentication redial (`connecting` after
authentication resets `attempt` to zero) and later `reconnecting`
attempts. A crashed sandbox Runner (`executionStopped: true`) resolves
`completion` immediately without waiting out the reconnect grace, while
a transient `status` error lets PRP reconnect continue.
- Poll `status` on `quiesce()` when `runner.suspend` commits the Runner
exit (guarding `outbound?.quiesce?.()` for partial test ingress
handles), so `transport.close()` observes natural process exit
immediately without waiting for `waitForProcess` or
`adoptedRunner.isAlive()` to time out.
- Update `doc/plugins/PLUGIN_RUNNER_EXECUTION.md` and add unit,
setup-probe, and real `paperclip-runnerd` two-turn regressions.

## Verification

- Red before the fix:
- `pnpm --dir server exec vitest run
src/services/native-runtime/plugin-runner-task.test.ts` failed in `does
not poll Launcher status or fail an active per-turn task during a
mid-turn status outage` with `Error: Plugin Runner operation failed;
reconcile the saved task`.
- `pnpm --dir server exec vitest run
src/services/native-runtime/plugin-runner-task-warm.integration.test.ts`
failed in `keeps a real per-turn plugin Runner session quiet during an
active turn and settles cleanly on close` with 27 mid-turn `status`
calls instead of 0.
- Green after the fix:
- `pnpm --dir server exec vitest run
src/services/native-runtime/plugin-runner-task.test.ts` (115 passed,
including real `connectRunnerPrpIngress` post-auth disconnect/crash and
startup abort deduplication)
- `pnpm exec vitest run
server/src/services/native-runtime/plugin-runner-probe.test.ts` (15
passed)
- `pnpm --dir server exec vitest run
src/services/native-runtime/plugin-runner-task-warm.integration.test.ts`
(2 passed, including real `paperclip-runnerd` two-turn per-turn
execution and session resume)
- `pnpm --dir server exec vitest run
src/__tests__/plugin-runner-task.integration.test.ts
src/__tests__/environment-task-authority.integration.test.ts
src/__tests__/antigravity-plugin-task-credential-writeback.test.ts
src/services/native-runtime/antigravity-run-start.test.ts` (39 passed)
  - `pnpm --dir server typecheck` (passed across all layers)

## Risks

- Low risk. No database schema, API route, or plugin wire protocol
changes.
- Startup before `phase: "running"` still enforces the 15-second
`readStatus()` deadline, and `checkpoint()` / `child.kill()` still
require `executionStopped: true` before `complete` releases task grants.
- If a sandbox Runner crashes mid-turn while PRP is connected, WebSocket
close triggers a post-authentication reconnect attempt (`connecting`,
then `reconnecting`), which checks `status` once per reconnect cycle and
resolves `completion` immediately when `executionStopped: true` is
reported.

## Model Used

- Google DeepMind Gemini 4 Carbon (Medium reasoning budget), via
Antigravity with tool use (code search, file edits, local test
execution, and GitHub CLI).

## 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 12:27:37 -06:00
..
…
…
…
…
…
…