feat(sessions): enhance session patching with sandbox state management

Merged
AH
5mo ago
+163-0
  • Added logic to handle archiving and unarchiving of sessions, ensuring proper sandbox state checks.
  • Introduced stopSandboxOnArchive function to manage sandbox teardown for archived sessions.
  • Updated patchSessionByIdHandler to include conditions for unarchiving sessions based on sandbox state.
  • Implemented CORS headers in response for error handling during sandbox operations.

Summary by cubic

Enhances session archive/unarchive with stricter sandbox checks and background teardown. Preserves snapshots until the sandbox stop succeeds to avoid bad restores.

  • New Features

    • Added stopSandboxOnArchive: runs via after(); re-reads the row; on success clears sandbox_state and nulls snapshot_url/snapshot_created_at; on stop failure logs, sets lifecycle_error, and only clears runtime state when there’s no snapshot_url.
    • Hardened patchSessionByIdHandler: on archive applies lifecycle patch and schedules background stop; on unarchive returns 409 if pausing with no snapshot_url (ignores "hibernated"/"archived"), and clears lifecycle state/error; adds CORS headers on errors.
    • Extracted isSandboxPausing and isUnarchiveConflict helpers.
  • Refactors

    • Replaced inline lifecycle updates with ARCHIVE_LIFECYCLE_PATCH and UNARCHIVE_LIFECYCLE_PATCH; streamlined related imports.

Summary by CodeRabbit

  • Bug Fixes

    • Rejects unsafe unarchive attempts while a session's sandbox runtime is pausing/paused.
    • Archiving now clears sandbox/lifecycle/snapshot metadata and defers sandbox shutdown to background.
    • Unarchiving clears lifecycle error/state so sessions restore cleanly; snapshot/pausing conditions are respected.
  • New Features

    • Introduces sandbox path utilities and updates file/grep/glob/edit/write/read tools to use them for consistent POSIX sandbox paths.
  • Chores

    • Switched event IDs to UUIDs and added a background helper to stop sandboxes on archive.

Review Change Stack