Skip to content

fix(backends): retain retry owners for refused launch cleanup - #3964

Merged
schlessera merged 4 commits into
mainfrom
fix/retry-cleanup-owner
Nov 11, 2026
Merged

schlessera merged 4 commits into
mainfrom
fix/retry-cleanup-owner

Conversation

@schlessera

@schlessera schlessera commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Closes #3821
Closes #3489
Refs #3813

What changed

A failed Claude launch now hands its original retryable cleanup and retained runtime-state/turn-temp reservation to the shared SDK reconciler. Pi does the same when a post-launch callback refuses. Required native output and checkpoint deletion must confirm before the failed launch releases its reservation; rejected deletion promises can be retried without reopening publication or admitting new consumers. The original launch error remains the outcome.

Why this shape

This implements #3489's selected owner and reuses #3815's reconciler unchanged: indefinite retries, capped backoff, unreferenced timers, existing Activity notices, and shutdown that releases nothing. The SDK close retains the exact failed-launch broker instead of reconstructing cleanup authority. The native deletion memo changes are necessary so a retry can reach the real deletion operation after a refusal. The launcher/error path remains internal; supported API signatures, CLI/MCP/HTTP shapes and provider history are unchanged. The review follow-up records one shared diagnostic helper name in the existing internal export inventory. A patch changeset covers the three affected packages.

Collision review found draft #3464 also touching packages/ui-sdk/src/server/worker-launcher.ts; this diff is confined to the failed-launch cleanup branch. Both issues and this PR target milestone 0.45.0.

Proof

Current base: 067d1904bafccf0503a1e3effd6bd260ebf34769. Current head: 4c707a691d38c322f841c6eb648a978f32f9200f.

The full-suite proof for the review follow-up is recorded in Review follow-up below. The original implementation receipts in this table were collected on 4082835e75cd76a27e5b0b94a13c9bdb2cf15536, starting from 60d7967f16665846fe4655b251fa19543847fbd5, with a later preflight/typecheck refresh at bcc5ac40008eb05e54297a532a9c135b48c58222. They retain those actual identities; they are not relabelled as proof of the follow-up.

Command / scope Actual result
bun test --timeout 30000 ./tests Initial inherited environment: exit 1, 2,237 pass / 2 sandbox-note failures (#2769). Full rerun with only ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY, OPENROUTER_API_KEY and TYPESAFE_API_KEY removed from the command environment: exit 0, 2,239 pass / 0 fail, 198 files.
bun run test packages/ui-sdk packages/ui-backend-claude packages/ui-backend-pi Exit 0, 2,854 pass / 0 fail / 1 expected capability skip, all 301 test files. Both provider-history guards and actual worker lifecycle cases included.
bun run test packages/ui-server/tests/no-console.test.ts packages/ui-server/tests/maintenance-temp-boundary-diagnostics.test.ts Exit 0, 6 pass / 0 fail.
bun run lint Exit 0 after restored mutations; also passed in check:pr.
NODE_OPTIONS=--max-old-space-size=16384 bun run typecheck Exit 0 after restored mutations and again on the final rebased head.
bun run api-report --write; bun run api-report Both exit 0; no supported API report diff.
bun scripts/check-changeset-packages.ts; bunx --no-install changeset status Both exit 0; inspected full pending plan, correct manifest package names and intended patch bumps.
bun run check:pr --base origin/main Exit 0 on the tested pre-rebase head: all gates passed, 993 tests across 41 files. Final rebased run also exited 0 with 993 pass / 0 fail across 41 files; receipt below.
bun scripts/prove-worker-identity.ts Exit 1 at the existing resumed prepared-consumer assertion, after the selected failed-launch boundary and full broker qualification passed. Original-checkpoint control and candidate diagnostic composition each exit 0; see limitations below.

Failing-first tests before implementation: Claude exited 1 on FAILED_LAUNCH_EXPOSES_OWNED_CLOSE; Pi exited 1 on PI_POST_LAUNCH_REFUSAL_HAS_RETRY_OWNER and the required-output reservation assertion. The restored focused run passed all 46 tests; the later complete package run includes the final test versions.

Acceptance criteria

Issue / criterion Verdict and evidence
#3821 / 1: throwing workerStarted, refused turn-temp, original error, eventual release PASS: deterministic post-launch test preserves error identity, remains charged through seven refusals, then releases on confirmation. Existing actual-process throwing-callback lifecycle cases remain.
#3821 / 2: required output refusal retains ownership/charge PASS: native-output variant holds the reservation until its eighth cleanup confirms; SDK native-stage test proves deletion can actually retry.
#3821 / 3: unrelated worker usable; shutdown cancels without release PASS: another Pi turn succeeds during reconciliation; shutdown cancels its scheduled task and leaves the isolated ledger charged.
#3821 / 4: deterministic doubles and handoff mutation PASS: mutation receipt below.
#3489 / 1: actual failed-launch owner reaches reconciler PASS: test drives real launcher to deferred backend to reconciler using a scripted supervisor; error exposes the original close and ordinary errors remain distinct.
#3489 / 2: coalesced/idempotent close; required resources confirmed PASS: concurrent/duplicate close reaches the broker once, successful close stays memoized, refused native stage/checkpoint cleanup retries with admission closed. Native qualification covers actual identity cleanup.
#3489 / 3: late confirmation releases both reservations exactly once; ordinary behavior PASS: eight refusals, ninth close confirms; both counters return to zero with one release. Ordinary confirmed launch failure releases directly with no owner.
#3489 / 4: unchanged policy/reporting/shutdown PASS: shared reconciler unchanged; tests cover 30-second cap, continuing diagnostic, retrying/resolved notices, pending cancellation and late confirmation after shutdown retaining charges.
#3489 / 5: deterministic real handoff and mutation PASS: launcher/backend/reconciler tests include refusal, concurrent close and late shutdown confirmation; accounting mutation below.
#3489 / 6: actual-process/native qualification with exact scope PASS for chosen boundary: disposable-container real cross-UID broker acquires state, population refuses, first cleanup acknowledgement is withheld after real termination, retry performs real broker cleanup, payload never runs, and no worker remains. Broader proof limitation is recorded below.
#3489 / 7: contract inventory and changeset PASS: internal coordination only; the API report inventories the new internal diagnostic helper. Supported signatures are unchanged. Patch changeset names the SDK and both backend manifests.

Mutation receipts

Each mutation was baseline pass (exit 0), expected assertion failure (exit 1), then restored pass (exit 0). The failure output was inspected, including the actual reservation count for the Claude handoff.

Removed behavior Named failing assertion Before / mutant / restored
Claude reconciler handoff FAILED_LAUNCH_CONFIRMED_RETRY_RELEASES_RESERVATION 0 / 1 / 0
Pi post-launch reconciler handoff PI_POST_LAUNCH_REFUSAL_HAS_RETRY_OWNER 0 / 1 / 0
Native stage retry after rejected deletion NATIVE_STAGE_REFUSED_DELETE_CAN_RETRY 0 / 1 / 0
Checkpoint retry after rejected deletion CHECKPOINT_DELETE_RETRIES_AFTER_REFUSAL 0 / 1 / 0

Proportionate containment

  • Trigger: the launch's cleanup cannot confirm, or Pi's required post-launch output/turn-temp cleanup refuses. Ordinary launch errors with confirmed cleanup still release normally; existing end-of-turn ownership is unchanged.
  • Scope: only that launch's uncertain resources and reservation stay held. Other workers can use remaining capacity.
  • Restricted operation: keep the failed launch stopped and its charge held. Continuing that launch or reclaiming its unconfirmed bytes cannot enforce the ownership/accounting boundary.
  • Recovery: the existing process reconciler retries the original close indefinitely with capped backoff; no human judgment or restart is needed.
  • Diagnostics: existing worker_cleanup Activity notices carry retryable cleanup allowances and cause through the backend reporter, then report resolution. A missing-close handoff instead emits the structured stranded-charge diagnostic described below, without falsely claiming that a retry is scheduled. Existing continuing-reconciliation/shutdown diagnostics remain; provider history receives none of this data.
  • Isolation evidence: the Pi post-launch test completes a second worker's turn while the first cleanup is still refusing.
bun run check:pr --base origin/main
{
  "version": 1,
  "kind": "brain-check-pr-receipt",
  "head": "3d12b32096617f52383361b2c1d5dbaf788b18db",
  "base": "origin/main",
  "mode": "focused",
  "startedAt": "2026-11-11T02:33:12.275Z",
  "finishedAt": "2026-11-11T02:34:33.322Z",
  "checks": [
    {
      "name": "all lint and leakage gates",
      "categories": [
        "changeset"
      ],
      "outcome": "passed",
      "elapsedMs": 14051.830699999999
    },
    {
      "name": "environment documentation",
      "categories": [
        "changeset"
      ],
      "outcome": "passed",
      "elapsedMs": 118.78857900000003
    },
    {
      "name": "pending changeset package names",
      "categories": [
        "changeset"
      ],
      "outcome": "passed",
      "elapsedMs": 76.63833700000032
    },
    {
      "name": "contribution changeset",
      "categories": [
        "changeset"
      ],
      "outcome": "passed",
      "elapsedMs": 23.98508799999763
    },
    {
      "name": "affected fast invariants",
      "categories": [
        "verify",
        "unit"
      ],
      "outcome": "passed",
      "elapsedMs": 66666.353645
    }
  ]
}

Review follow-up

Independent review of bcc5ac400 identified F1 and F2 proof gaps and N1's inconsistent missing-close behavior. Current main was merged first (merge commit 3e8ec581b); this follow-up is 4c707a691d38c322f841c6eb648a978f32f9200f.

  • F1 — addressed. The actual Claude launcher/backend/reconciler path now has strict prepared-output coverage with writable areas, plus a strict startup-checkpoint-only variant. Production native preparation, reference counting and cleanup run over owned ordinary-directory manager doubles. The supervisor refuses twice, then the stage deletion refuses, then final checkpoint deletion refuses. Assertions observe stage/checkpoint directory existence, output-staging accounting and retained runtime-state/turn-temp reservations. Stage accounting clears only when stage deletion confirms; the launch remains charged until its final checkpoint reference confirms. This qualifies the ownership handoff and resource accounting, not Btrfs or kernel namespace enforcement; the earlier real-broker qualification retains its stated scope.
  • F2 — addressed. The prepared-output variant observes that the stage still exists and its charge stays held after the outer strict catch. The deletion double intentionally permits premature deletion: it cannot hide an unconditional cleanup by enforcing the guard on the product's behalf.
  • N1 — addressed. Both backends keep an unconfirmed charge whose error supplies no retryable close. They emit one structured worker_cleanup_stranded stderr diagnostic with backend, missing_retryable_close, runtime-state/turn-temp bytes and cause, and schedule no retry. Both backend tests assert the original error, retained charge, zero scheduled tasks and exactly one diagnostic. A shared internal helper keeps the diagnostic shape consistent. The internal export inventory and patch changeset are updated; no supported API or wire shape is added.
Mutation Intended failure Baseline / mutated / restored exit
F1: remove both final native-cleanup awaits from the failed launch's retryable close STRICT_FAILED_LAUNCH_RESOURCE_REFUSAL_RETAINS_RESERVATION, expected 1 retained reservation, got 0; fails for both prepared-output and startup-only variants 0 / 1 / 0
F2: make strict outer-catch output cleanup unconditional STRICT_FAILED_LAUNCH_OUTPUT_NOT_DELETED_BY_OUTER_CATCH, expected an existing stage, got deleted 0 / 1 / 0
N1 Claude: remove the stranded-charge diagnostic CLAUDE_STRANDED_CHARGE_HAS_ONE_DIAGNOSTIC, expected one line, got zero 0 / 1 / 0
N1 Pi: restore the futile reconciler handoff NO_FUTILE_PI_CLEANUP_RETRY, expected zero scheduled retries, got one 0 / 1 / 0

All failure outputs were inspected at the named assertions, and source was restored after every mutation.

Full local proof after restoring all mutations (every command's real exit code was read):

Command / scope Follow-up result
bun test --timeout 30000 ./tests Exit 0; 2,239 pass / 0 fail across 198 files. The same five provider credential variables listed above were removed only from this command's environment for known #2769.
bun run test packages/ui-sdk packages/ui-backend-claude packages/ui-backend-pi Exit 0; 2,858 pass / 0 fail / 1 expected delegated-cgroup capability skip across all 301 files. Includes both backends' provider-history suites.
bun run test packages/ui-server/tests/no-console.test.ts packages/ui-server/tests/maintenance-temp-boundary-diagnostics.test.ts Exit 0; 6 pass / 0 fail across 2 files.
NODE_OPTIONS=--max-old-space-size=16384 bun run typecheck Exit 0, including final committed head.
bun run lint Exit 0; all gates clean.
bun run api-report Exit 0; internal diagnostic helper inventory committed, supported signatures unchanged.
bun run check:pr --base origin/main Exit 0; all gates passed, 993 pass / 0 fail across 41 files. Final head receipt above.

All four mutations above have independently recorded baseline, mutated and restored exit codes. No candidate confirmation or merge was run.

Checklist

  • Focused local preflight/behavioral receipts and complete selected hosted proof passed; actual head/base/job checkout evidence is in Proof. Local proof is recorded; hosted candidate confirmation is deliberately left to the coordinator.
  • Acceptance criteria from the issue are all met, or the gap is named below.
  • The PR and completed/advanced issues have verified release milestones under docs/process/github.md.
  • A changeset is included, or this changes nothing a consumer can see.
  • No personal data anywhere in the diff — the leakage gate covers the whole tree.
  • The integration contract index or relevant component is updated in this same commit, or nothing in the machine surface moved.
  • Docs under docs/ reflect the change, or nothing documented moved.
  • A fail-closed guard added or changed answers the proportionate-containment checklist in this body.
  • Provider history stays append-only: the provider-history guards pass.

Anything left open

The full native command initially exited 1 in the existing prepared-consumer cleanup-failure mode: the resumed turn failed during startup before its second request. The task's real broker cleanup qualification passed in that run. One original-checkpoint control and one candidate diagnostic run with extra fixture logging passed the full native identity composition, including both prepared-consumer modes. These passes do not attribute or repair the intermittent failure. Evidence and recurrence capture are recorded in #2744.

The root suite's initial ambient-environment run reproduced the two sandbox-note failures tracked in #2769; the environment-controlled full result is listed above. The package suite's one expected skip is the existing delegated-cgroup fork/OOM test: no systemd user manager can delegate a cgroup subtree here. No cgroup behavior is changed or newly qualified by this PR.

No candidate confirmation or merge was requested by this implementation worker. The coordinator owns those steps and the final hosted proof review.

@schlessera schlessera added this to the 0.45.0 milestone Nov 11, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. area: backends AgentBackend implementations and the seam they share. labels Nov 11, 2026
@schlessera
schlessera marked this pull request as draft November 11, 2026 02:35
@schlessera
schlessera marked this pull request as ready for review November 11, 2026 02:35
@schlessera
schlessera merged commit aa95dab into main Nov 11, 2026
39 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: backends AgentBackend implementations and the seam they share. area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

1 participant