mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-23 21:24:45 +01:00
fix(native): end a run whose finalizer a newer run superseded, so it stops holding its task (#21106)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - A native run saves its accepted result, then a finalizer exports the workspace and applies the result to the task. > - When a newer run already committed the task's decision, the finalizer retires the older result as `native_finalization_superseded`. > - That retirement set the finalizer terminal, but it did not end the run. The run stayed `running` and held the task and every run queued behind it, with no way out except a person. > - A stuck run must never hold a task and its queue forever. > - This pull request ends the run when its finalizer is retired as superseded, and it ends runs that an older release left in that state. > - The benefit is that the task continues with its queued runs by itself. ## Linked Issues or Issue Description Refs #21046 **What happened?** On a hosted staging server, a native run accepted its result at 11:31:08Z. The workspace export then failed (`remote_workspace_archive_fs_eacces`). The task's last decision came from another run that was created 42 seconds later but ran earlier. The finalizer judged this run superseded: it set the finalizer to `terminal_failure` with `native_finalization_superseded`. The run stayed `running` with "Recovery needed". Over one hour later it still held the task, and five queued runs waited behind it. A server upgrade at 12:02Z did not change it. **Expected behavior** A run whose finalizer is retired ends. The task keeps the newer run's decision, the retired result is never applied, and the queued runs proceed. **Steps to reproduce** 1. Accept a native result for a run. 2. Commit a status decision on the same task from a run that was created later. 3. Let the older run's finalization fail once, so the finalizer records the failure. 4. See that the finalizer is `terminal_failure` / `native_finalization_superseded` and the run is still `running`. **Root cause** `recordRetryableFailure` (`server/src/services/native-runtime/native-run-finalizer.ts`) projects a terminal run status only when `exhausted && !supersededByNewerRun`. A superseded retirement therefore never ended the run. No other path ends a run whose finalizer is already `terminal_failure`: the reconciler skips `terminal_failure` rows. **Paperclip version or commit** Staging Core `3eca7a6112`. The behavior is the same on master `482063aeca`. ## What Changed - `native-run-finalizer.ts`: a superseded retirement now ends the run with the same status mapping as an exhausted finalization (workspace failure: `failed`; otherwise the provider's terminal state). It does not change the task, does not apply the result, and does not keep the workspace export lease. The run's error says in plain words that a newer run already decided the task. - `native-run-finalizer.ts`: add `endSupersededNativeFinalizationRuns`. It ends a native run that is still `running` while its finalizer is `terminal_failure` / `native_finalization_superseded`. This covers rows that an older release wrote. It locks in the finalizer's order (coordinator, task, run), and re-checks the row under the lock. - `native-run-projection-work.ts`: the registered native run projection worker (it loads at startup, pages through all candidates, and wakes on finalization changes) also selects such runs and ends them with `endSupersededNativeFinalizationRuns`. - `doc/execution-semantics.md`: document the rule. - No schema change. Nothing changes on the path that starts a run or a turn. ## Verification - `server/src/__tests__/native-finalization-recovery.test.ts`, red on master and green on this branch: - "retires an older failed finalizer when a newer run already committed the issue": the retired run now ends (`succeeded`, `finishedAt` set). Master: the run stays `running`. - "ends a run an earlier release left running after retiring its finalizer as superseded": the shape of the staging row (workspace export failed, superseded finalizer, run `running`). The repair ends it as `failed` with the plain message. The task, its decision and the finalizer stay unchanged, and no status decision is written for the run. A second pass changes nothing. - `server/src/__tests__/native-run-projection-work.test.ts`, "ends, at startup, a run an earlier release left running after retiring its finalizer as superseded": the registered worker ends the staging-shaped row at startup and settles its claimed wake. Red without the worker change. - Related suites pass: `native-finalization-recovery`, `native-run-projection-work`, `native-finalization-reconciler-timing`, `native-reconciliation-timing`, `native-workspace-finalizer`, `native-workspace-finalizer-failure`, `native-run-finalizer-telemetry`. - `tsc --noEmit` for `server`. ## Risks - Low risk. The change only ends runs whose finalizer is already terminal and superseded. It never applies a result and never changes the task. - A superseded run whose workspace export failed now releases its sandbox lease through the normal settlement instead of waiting forever. Its result was not applied, because a newer run decided the task. - Not changed here: the superseded check compares run creation times. A run created earlier but executed later is still judged superseded. This PR makes that verdict end the run; it does not change the verdict. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use (code search, shell, test execution, read-only API inspection) in Claude Code. ## 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>
This commit is contained in:
1 parent
0a2f618cae
commit
81d2e4d8d9
5 files changed
+197
-24
No files matched your search
@@ -28,6 +28,7 @@ import {
|
||||
reconcileRetainedNativeSessionCleanups,
|
||||
} from "../services/native-runtime/native-finalization-reconciler.js";
|
||||
import { PaperclipControlPlanePort } from "../services/native-runtime/paperclip-control-plane-port.js";
|
||||
import { endSupersededNativeFinalizationRuns } from "../services/native-runtime/native-run-finalizer.js";
|
||||
import { assertRetainedNativeSourceArchiveSettled, reconcileRetainedNativeSessionCleanup } from "../services/native-runtime/native-session-executor.js";
|
||||
|
||||
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_NATIVE_FINALIZATION_TEST_DATABASE_URL");
|
||||
@@ -331,6 +332,46 @@ describe("P6-16/P6-25/P6-28 native finalization recovery", () => {
|
||||
.where(eq(issueRecoveryActions.sourceIssueId, staleIssueId))).resolves.toEqual([
|
||||
expect.objectContaining({ status: "resolved", outcome: "false_positive" }),
|
||||
]);
|
||||
// The retired run ends, so it no longer holds the task and its queue.
|
||||
const [ended] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, staleRunId));
|
||||
// Provider work succeeded; the result is kept for reference only.
|
||||
expect(ended).toMatchObject({ status: "succeeded", nativePhase: "terminal_failure", finishedAt: expect.any(Date) });
|
||||
});
|
||||
|
||||
it("ends a run an earlier release left running after retiring its finalizer as superseded", async () => {
|
||||
// The row an earlier release wrote after a failed workspace export: a
|
||||
// terminal superseded coordinator, and the run still running.
|
||||
await db.update(heartbeatRuns).set({ status: "running", finishedAt: null, error: null, errorCode: null })
|
||||
.where(eq(heartbeatRuns.id, staleRunId));
|
||||
// Written here, so the case does not depend on the previous test's outcome.
|
||||
await db.update(nativeRunFinalizations).set({ phase: "terminal_failure", leaseOwner: null, leaseExpiresAt: null,
|
||||
failureCode: "native_finalization_superseded", nextAttemptAt: null, failureDetail: { workspaceFinalizeAttempt: 1,
|
||||
originalFailureCode: "native_workspace_sync_out_failed", recoveryOwner: { kind: "none", reason: "newer_native_decision" } } })
|
||||
.where(eq(nativeRunFinalizations.runId, staleRunId));
|
||||
const [issueBefore] = await db.select().from(issues).where(eq(issues.id, staleIssueId));
|
||||
const [coordinatorBefore] = await db.select().from(nativeRunFinalizations)
|
||||
.where(eq(nativeRunFinalizations.runId, staleRunId));
|
||||
expect(coordinatorBefore).toMatchObject({ phase: "terminal_failure", failureCode: "native_finalization_superseded" });
|
||||
|
||||
await expect(endSupersededNativeFinalizationRuns(db, [staleRunId])).resolves.toEqual([staleRunId]);
|
||||
|
||||
const [ended] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, staleRunId));
|
||||
expect(ended).toMatchObject({ status: "failed", errorCode: "native_finalization_superseded", finishedAt: expect.any(Date) });
|
||||
expect(ended.error).toBe("A newer run already decided this task, so this run's result was kept for reference and not applied.");
|
||||
// The durable verdict stands: no issue change, no re-applied result.
|
||||
await expect(db.select().from(issues).where(eq(issues.id, staleIssueId))).resolves.toEqual([
|
||||
expect.objectContaining({ status: issueBefore.status, statusVersion: issueBefore.statusVersion,
|
||||
lastStatusDecisionId: issueBefore.lastStatusDecisionId }),
|
||||
]);
|
||||
await expect(db.select().from(statusDecisions).where(eq(statusDecisions.runId, staleRunId))).resolves.toHaveLength(0);
|
||||
await expect(db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, staleRunId)))
|
||||
.resolves.toEqual([expect.objectContaining({ phase: "terminal_failure", failureCode: "native_finalization_superseded",
|
||||
resultId: coordinatorBefore.resultId })]);
|
||||
// A second pass finds nothing left to end.
|
||||
await expect(endSupersededNativeFinalizationRuns(db, [staleRunId])).resolves.toEqual([]);
|
||||
await expect(db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, staleRunId))).resolves.toEqual([
|
||||
expect.objectContaining({ status: "failed" }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -365,6 +365,37 @@ describe("committed native run projection lifecycle", () => {
|
||||
expect(await work.load({ companyId, runId: binding.runId })).toEqual([]);
|
||||
});
|
||||
|
||||
// Staging, 2026-11-12: an earlier release retired a finalizer as superseded
|
||||
// after its workspace export failed, but left the run running. It held the
|
||||
// task and five queued runs for over an hour, through an upgrade.
|
||||
it("ends, at startup, a run an earlier release left running after retiring its finalizer as superseded", async () => {
|
||||
const binding = await seed();
|
||||
await complete(binding, "control_plane");
|
||||
const before = await state(binding.runId);
|
||||
await db.update(nativeRunFinalizations).set({ phase: "terminal_failure", leaseOwner: null, leaseExpiresAt: null,
|
||||
failureCode: "native_finalization_superseded", failureDetail: { workspaceFinalizeAttempt: 1,
|
||||
originalFailureCode: "native_workspace_sync_out_failed", recoveryOwner: { kind: "none", reason: "newer_native_decision" } } })
|
||||
.where(eq(nativeRunFinalizations.runId, binding.runId));
|
||||
await db.update(heartbeatRuns).set({ nativePhase: "terminal_failure" }).where(eq(heartbeatRuns.id, binding.runId));
|
||||
const wake = await claimedWake(binding);
|
||||
const [issueBefore] = await db.select().from(issues).where(eq(issues.id, binding.issueId));
|
||||
|
||||
await start();
|
||||
await flush();
|
||||
|
||||
const ended = await read(binding.runId);
|
||||
expect(ended).toMatchObject({ status: "failed", errorCode: "native_finalization_superseded", finishedAt: expect.any(Date),
|
||||
error: "A newer run already decided this task, so this run's result was kept for reference and not applied." });
|
||||
expect(await readWake(wake.id)).toMatchObject({ status: "failed", finishedAt: ended.finishedAt });
|
||||
// The durable verdict stands: the task and the retired finalizer are unchanged.
|
||||
expect(await state(binding.runId)).toMatchObject({ phase: "terminal_failure",
|
||||
failureCode: "native_finalization_superseded", resultId: before.resultId });
|
||||
const [issueAfter] = await db.select().from(issues).where(eq(issues.id, binding.issueId));
|
||||
expect({ status: issueAfter.status, statusVersion: issueAfter.statusVersion, lastStatusDecisionId: issueAfter.lastStatusDecisionId })
|
||||
.toEqual({ status: issueBefore.status, statusVersion: issueBefore.statusVersion, lastStatusDecisionId: issueBefore.lastStatusDecisionId });
|
||||
expect(await nativeRunProjectionWork(db).load({ companyId, runId: binding.runId })).toEqual([]);
|
||||
});
|
||||
|
||||
it.each(["unbound", "newer-run", "queued", "cancelled", "foreign-company", "foreign-agent", "live-owner"] as const)(
|
||||
"does not settle a %s wake claim", async fault => {
|
||||
const binding = await accepted(), wake = await claimedWake(binding);
|
||||
|
||||
Reference in new issue
Block a user