Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/retry-failed-launch-cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@schlessera/brain-ui-sdk": patch
"@schlessera/brain-backend-claude": patch
"@schlessera/brain-backend-pi": patch
---

Retry unconfirmed failed-launch cleanup and release its reserved capacity only after worker and native-resource cleanup is confirmed.

Report stranded cleanup charges consistently when a legacy launch error provides no retryable close, without scheduling futile retries.
1 change: 1 addition & 0 deletions api-report/ui-sdk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@ export "./internal" (./src/internal.ts)
relayedProviderKeys
removeMetadataFile
removeOwnedArtifact
reportStrandedWorkerCleanup
requestPolicyPermission
requireWorkerHost
requireWorkerHostAsync
Expand Down
12 changes: 9 additions & 3 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3716,7 +3716,8 @@ end-of-turn cleanup that the awaited close above could not confirm.
- **The turn still fails.** The refusal is the turn's, as before; the
reconciler only takes ownership of the retryable close and the reservation.
- **Capped backoff without giving up (revised by #3815).** Both backends
retain a retry owner, including Pi initialization failures. After the
retain a retry owner, including Pi initialization and post-launch callback
failures. After the
foreground retry count, the owner continues at the capped delay. Activity
records the cause, charged allowances and later confirmation.
- **Release once, only on confirmation.** The reservation is released exactly
Expand All @@ -3729,8 +3730,13 @@ end-of-turn cleanup that the awaited close above could not confirm.
The owner is the reconciler
(`export function createCleanupReconciler`, `packages/ui-sdk/src/server/cleanup-reconciler.ts:75-148`), and the turn
hands its refusal over where it awaits the close
(`const close = async`, `packages/ui-backend-claude/src/worker-spawn.ts:273-284`). Pi supervision now uses the same owner; launch failures before a handle is
returned remain the launcher's responsibility.
(`const close = async`, `packages/ui-backend-claude/src/worker-spawn.ts:282-293`).
Pi supervision and post-launch callback failures use the same owner. A failed
SDK launch whose cleanup is unconfirmed exposes its original retryable close
to the backend, which transfers it and the reservation to this reconciler
(#3489). Required native stage and checkpoint cleanup must also confirm before
that failed launch's reservation is released. No separate launcher retry loop
or accounting owner is introduced.

### Turn completion includes worker teardown — 2026-10-25

Expand Down
11 changes: 10 additions & 1 deletion docs/hosting/agent-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ confirmed. An unconfirmed cleanup retains ownership and reservations for
reconciliation rather than reporting successful reclamation. For
end-of-turn cleanup, both backends keep retrying with capped backoff
and release the reservation only once a close is confirmed. Pi initialization
failures retain the same retry owner. The original turn's Activity events
failures and post-launch callback refusals retain the same retry owner. A Claude
launch whose supervisor cleanup is unconfirmed transfers its original retryable
close to that owner too, even though no worker handle reached the backend.
Required native output and checkpoint cleanup must confirm before the failed
launch's reservation is released. Retries do not keep the process alive; shutdown
cancels them and releases no uncertain allowance, including a close that confirms
after shutdown begins. If a legacy or malformed cleanup-unconfirmed error has no
retryable close, both backends keep its charge and emit one structured
`worker_cleanup_stranded` diagnostic with the backend, cause and retained
allowances; they do not schedule an operation they cannot retry. The original turn's Activity events
record the cleanup cause, retained allowances and eventual confirmation,
including confirmation after the turn has ended. After five failed retries a
host diagnostic reports slow reconciliation; retries continue without a restart.
Expand Down
20 changes: 17 additions & 3 deletions packages/ui-backend-claude/src/worker-spawn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Unconditional isolated Claude child; project MCP, tools and descendants inherit it. */
import { brainWorkerPath } from "@schlessera/brain-common/internal/worker-areas";
import { rewriteWorkerRtk, workerCleanupUnconfirmed } from "@schlessera/brain-ui-sdk/internal";
import { rewriteWorkerRtk, workerCleanupUnconfirmed, reportStrandedWorkerCleanup } from "@schlessera/brain-ui-sdk/internal";
import { EventEmitter } from "node:events";
import { deferredWorkerSpawn } from "./deferred-worker-spawn.js";
import type { SpawnOptions, SpawnedProcess } from "@anthropic-ai/claude-agent-sdk";
Expand Down Expand Up @@ -188,7 +188,16 @@ export function createWorkerSpawn(brainPath: string, config: ExecWrapperConfig,
command: wrapCommand([command, ...args], config.wrapper), env: childEnv, hostHome: env.HOME || homedir(), credentialEnv, ...(egress ? { egress } : {}), ...(outputLock ? { outputLock } : {}), ...(sandbox ? { sandbox } : {}),
...(limitsPlan ? { limits: limitsPlan } : {}) });
}
} catch (error) { record(error); if (!workerCleanupUnconfirmed(error)) state.release(); throw error; }
} catch (error) {
record(error);
if (!workerCleanupUnconfirmed(error)) state.release();
else if (error.close) (stateOptions.cleanup ?? processCleanupReconciler()).retain({
bytes: state.bytes, turnTempBytes: state.turnTempBytes, close: error.close,
release: () => state.release(), report: stateOptions.reportCleanup,
}, error);
else reportStrandedWorkerCleanup("claude", state, error);
throw error;
}
let startFailure: unknown;
reports.push(worker.limitReport);
const handle = worker.state!;
Expand Down Expand Up @@ -349,7 +358,12 @@ export function createWorkerSpawn(brainPath: string, config: ExecWrapperConfig,
try {
signal.throwIfAborted();
return await spawnNow({ ...options, signal }, signal, output);
} catch (error) { await output.cleanup(); throw error; }
} catch (error) {
// An unconfirmed launch owns this output until its retryable close
// confirms the worker's cleanup. Do not race that owner here.
if (!workerCleanupUnconfirmed(error)) await output.cleanup();
throw error;
}
}, [immediateSignal, options.signal]);
// Register preparation immediately: finish must own it even before a
// native child exists, including a cancelled late preparation result.
Expand Down
Loading
Loading