Dispatch cleanup hooks when run setup fails

Closed
DS
2mo ago
+2142-178

This pull request was posted by Codex Desktop using gpt-6-sol on behalf of David.

This follows the independent post-merge review of #6492; no separate issue is associated with this change.

Capability or toolset setup can leave workspace state behind when a later setup step fails before run lifecycle hooks start. Dispatch on_run_error for cleanup using the capabilities resolved so far. Also fix two workspace failure paths: shell realpath() must fail closed when readlink fails, and Prefect/Temporal must not retry a direct WorkspaceError.

New public surface

None.

User-visible behavior

If one capability creates a temporary workspace file and another capability's for_run() raises, the first capability's error hook now runs before the setup error reaches the caller. This applies to graph runs and realtime sessions, including failures while preparing the resolved capability context, selecting or entering a capability-owned model, acquiring a concurrency slot, recomputing graph metadata, or entering the run body through wrap_run/before_run. Error hooks cannot recover these setup failures because no AgentRunResult exists yet.

A nested run that fails during setup cannot save parent message history through StepPersistence; the parent retains its own recovery snapshot.

Realtime offer/client-secret creation does not run lifecycle hooks. Its capability resolution is isolated so a nested signaling failure cannot replace an outer run's cleanup instances.

Verification

Setup cleanup
Workspace retries
Symlink path validation

What changes for existing users

Shutdown exceptions, including SystemExit, propagate without recovery. Setup errors now invoke cleanup hooks; a cleanup hook's own exception replaces the setup error. Direct WorkspaceError instances no longer retry in Prefect or Temporal; existing subclass policies are preserved, including retries for potentially transient damaged shell output. Failed shell symlink resolution raises instead of permitting an unchecked path. Successful runs retain their behavior.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.