Files
PaperClipAI/doc
DottaandPaperclip c8dd2e400f fix(tasks): confirm Stop after preparation settles (#21906)
## Thinking Path

> - Paperclip lets people start and stop work by AI agents.
> - Stop must end the current turn and preserve the task for a later
message.
> - A person can press Stop while the server still prepares the turn.
> - The preparation owner can prove that no model work started.
> - That path saved the proof but left the Stop receipt at `requested`.
> - The page then reported an unverified Stop and a failed attempt.
> - This PR completes the receipt after proof and shows the confirmed
Stop without a failure notice.

## Linked Issues or Issue Description

**What happened?**
A person submits a task by mistake and presses Stop during preparation.
The run ends before the adapter starts. The server records that no
provider work started, but the Stop receipt stays at `requested`. The
page waits for a receipt that will never change. It then reports that it
cannot verify Stop. The task also shows a failed-attempt notice.

**Expected behavior**
Stop ends the current turn without a failure notice. A later message
starts one new turn through normal admission. Stop does not restart the
task by itself.

**Steps to reproduce**
Hold session preparation before adapter dispatch. Press Stop through the
run API. Release preparation and wait for the owner to finish. Read the
run and the task's interruption state. Send a new task comment.

Related: #20644 covers saved Retry after a committed native
cancellation. #21382 covers a provider failure after native Stop. This
PR covers Stop before adapter dispatch and its page presentation.

## What Changed

- Complete the Stop receipt in `settleCancelledLegacyPreparation`. Keep
its existing checks for process, session, events, leases and saved
files.
- Require no saved `budgetDispatch.handedOffAt` marker under the run
lock. This marker can exist before an adapter records its start. A stale
controller must keep the recovery hold after this handoff.
- Write and require the `preparation_settled` proof before clearing the
failed-attempt notice. Both page queries use one bounded SQL projection.
Unknown and foreign proof labels keep the notice.
- Clear an obsolete automatic-recovery marker only for a user's operator
Stop. Keep system cancellation delivery metadata so saved comments still
reach admission. Cover both cases with persisted recovery records and
the stale-comment journey.
- Give the bounded SQL result an explicit column name. Keep its
real-query tests for typed and bounded Stop evidence.
- Give the Retry test an active company membership and remove it during
cleanup. The test uses normal authorization.
- Record an expected cancellation as information in the run log.
- Add a regression through the real Stop, run-read and comment routes
with a real test database. Only the adapter boundary is simulated. Check
that preparation cannot confirm Stop early, Stop does not restart work,
and one later message starts one turn.
- Document preparation Stop settlement in the heartbeat module notes.

## Verification

- Head: `372ad381f4b60258e17b6b05eda133ed875e7de3`. This revision
includes master `20b0d4e5a5`. [Full CI attempt
2](https://github.com/paperclipai/paperclip/actions/runs/43910771322/attempts/2)
completed successfully: all 52 executed jobs passed, including Build,
Typecheck, all Runner lanes, all nine serialized suites, all twelve
server shards and all eight browser shards. Greptile and Paperclip
Review are both 5/5, and Superagent passed on this exact head. All three
inline threads are resolved. The final commit inventory has 57
successful checks, one neutral review and three expected skips, with no
pending or failing checks; Snyk also passed.
- All 339 focused tests pass at this head across 11 suites. The Stop,
saved-comment and fresh-continuation suites pass 132 tests. Eight other
Stop contract suites pass 207 tests.
- Red: the preparation regression receives `executionCancellation.state
= requested` after the owner finishes. The presentation regressions
report a failed attempt after a confirmed Stop.
- Red: automatic recovery resolves the hold first, then preparation
confirms Stop, but the run still shows a recovery notice. Green: the
confirmed operator Stop clears the obsolete marker and projects
`completed` / `Stopped`.
- Red: the previous repair also changed system cancellations. Both
stale-comment and Retry cases pass on the base and fail on the previous
PR head. A direct regression also catches a preparation Stop receipt
written for a system cancellation. Green: only a user's operator Stop
gets this receipt; system cancellations keep their recovery delivery
metadata.
- Red: the real-query tests read PostgreSQL's implicit expression-column
name after the expression changed. Green: the query has the explicit
`result_json` alias, and all typed and bounded evidence assertions pass.
- Red: a saved dispatch handoff marker was present before any adapter
event. Both new cases returned an acknowledged Stop instead of keeping
the hold. Green: the current locked row must have no marker, including a
marker with JSON null. The receipt stays requested and the hold stays
active. The tests use a stale owner snapshot.
- The HTTP Stop/read/comment test uses real routes and PostgreSQL. It
proves that pending preparation cannot confirm Stop, the owner confirms
it after cleanup, Stop does not restart work, and a later message starts
exactly one turn. Only the adapter boundary is simulated.
- All source typecheck layers and the changed `test-tree-0` layer pass
in low-memory mode. The first full typecheck attempt lost the compiler
to the workspace memory limit while tests ran in parallel. The affected
layers then passed alone. Other test layers passed on the previous head.
The full current-head CI Typecheck + Release Registry job passed. `git
diff --check` passes.
- `pnpm verify` returned exit 2 and requested broader CI. No full local
suite was started.
- Environment: Linux x64, Node 25.9.0, fresh embedded PostgreSQL. This
does not execute a live model or change staging. CI supplies Rust and
full-suite coverage.
- Previous-head CI found the SQL assertion and continuation regressions
above. Its cancelled workspace lane lost its runner before a test result
and was retried once. Previous-head results do not count as final-head
passes.

## Risks

- Only a requested, expected operator Stop with an acknowledged receipt
gets neutral presentation. Unknown termination and file recovery remain
visible.
- The preparation owner writes acknowledgement only after all existing
evidence checks pass. Terminal status alone is insufficient. A retained
local lease test checks both refusal before release and confirmation
after release.
- The receipt does not grant replay authority. Budget, approval,
dependency and cleanup gates still control later messages.
- No schema or migration changes are required.

## Model Used

OpenAI Codex, GPT-6, with reasoning and code tools. The exact served
model build and context size are not exposed in this session.

## 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-11-16 14:38:41 -06:00
..
…
…
…
…