-
Notifications
You must be signed in to change notification settings - Fork 10
feat(sessions): enhance session patching with sandbox state management #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c107840
feat(sessions): enhance session patching with sandbox state management
ahmednahima0-beep 5c682b0
test(sessions): enhance session route tests with sandbox management m…
ahmednahima0-beep a496aaf
refactor(tests): simplify mock implementation for session route tests
ahmednahima0-beep cc48342
fix(sessions): improve error handling in stopSandboxOnArchive function
ahmednahima0-beep 2c110cd
fix(sessions): enhance error handling in stopSandboxOnArchive function
ahmednahima0-beep b16d185
fix(sessions): refine unarchive condition for sandbox state management
ahmednahima0-beep 0cdec95
fix(sessions): further refine unarchive condition for sandbox state m…
ahmednahima0-beep 04790ee
fix(sessions): add session status check before stopping sandbox on ar…
ahmednahima0-beep cd8782b
fix(sessions): optimize session state update in stopSandboxOnArchive
ahmednahima0-beep de9729b
Merge branch 'test' into feat/patch-session-archive-side-effects
ahmednahima0-beep 07ea801
Merge branch 'test' into feat/patch-session-archive-side-effects
ahmednahima0-beep 8b1e349
Enhance session patching and sandbox state management
ahmednahima0-beep 4c2a791
Merge branch 'feat/patch-session-archive-side-effects' of https://git…
ahmednahima0-beep 5a2ce3c
Refactor file path resolution in agent tools
ahmednahima0-beep bfda13a
Merge branch 'test' into feat/patch-session-archive-side-effects
ahmednahima0-beep 7fefdaa
Refactor credit deduction and path handling
ahmednahima0-beep 4cc3a16
Merge branch 'feat/patch-session-archive-side-effects' of https://git…
ahmednahima0-beep 925c011
Refactor sandbox path handling in agent tools
ahmednahima0-beep a141f48
Enhance path comparison in isPathWithinSandboxDirectory for Windows c…
ahmednahima0-beep 8ca5866
refactor(agent/tools): revert sandbox path helpers to native path module
ahmednahima0-beep eec2fff
chore(pr-578): remove all scope creep
ahmednahima0-beep 40e7a2e
refactor(sessions): extract isSandboxPausing into lib/sandbox
ahmednahima0-beep 1b021b0
fix(sessions): preserve snapshot as fallback until stop succeeds; cle…
ahmednahima0-beep 83af7dd
refactor(sessions): extract isUnarchiveConflict predicate into lib
ahmednahima0-beep ecf407a
refactor(sessions): simplify lifecycle state updates in patchSessionB…
ahmednahima0-beep d6a924e
refactor(sessions): format imports for lifecycle state patches in pat…
ahmednahima0-beep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { hasRuntimeSandboxState } from "@/lib/sandbox/hasRuntimeSandboxState"; | ||
|
|
||
| /** | ||
| * Returns true when a sandbox is actively being paused — i.e. it has | ||
| * live runtime state but has not yet reached a terminal lifecycle state | ||
| * (`hibernated` or `archived`). | ||
| * | ||
| * Used by `PATCH /api/sessions/{sessionId}` to guard unarchive requests: | ||
| * if the sandbox is still pausing the request returns 409 so the caller | ||
| * can retry once the sandbox has settled. | ||
| */ | ||
| export function isSandboxPausing(row: { | ||
| sandbox_state: unknown; | ||
| lifecycle_state: string | null; | ||
| }): boolean { | ||
| return ( | ||
| hasRuntimeSandboxState(row.sandbox_state) && | ||
| row.lifecycle_state !== "hibernated" && | ||
| row.lifecycle_state !== "archived" | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { isSandboxPausing } from "@/lib/sandbox/isSandboxPausing"; | ||
|
|
||
| /** | ||
| * Returns true when an unarchive request should be rejected with 409: | ||
| * the sandbox has no snapshot to restore from and is still actively pausing. | ||
| * | ||
| * Without a snapshot, the sandbox cannot be unarchived until the pause | ||
| * completes; callers should retry after the sandbox settles. | ||
| */ | ||
| export function isUnarchiveConflict(row: { | ||
| sandbox_state: unknown; | ||
| lifecycle_state: string | null; | ||
| snapshot_url: string | null; | ||
| }): boolean { | ||
| return !row.snapshot_url && isSandboxPausing(row); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Lifecycle fields applied synchronously when a session is archived via | ||
| * `PATCH /api/sessions/{sessionId}`. Sandbox stop and snapshot clearing | ||
| * run afterward via `stopSandboxOnArchive`. | ||
| */ | ||
| export const ARCHIVE_LIFECYCLE_PATCH = { | ||
| lifecycle_state: "archived", | ||
| lifecycle_error: null, | ||
| lifecycle_run_id: null, | ||
| sandbox_expires_at: null, | ||
| hibernate_after: null, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Lifecycle fields applied synchronously when a session is unarchived via | ||
| * `PATCH /api/sessions/{sessionId}`. | ||
| */ | ||
| export const UNARCHIVE_LIFECYCLE_PATCH = { | ||
| lifecycle_state: null, | ||
| lifecycle_error: null, | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { connectSandbox, type SandboxState } from "@/lib/sandbox/factory"; | ||
| import { clearSandboxState } from "@/lib/sandbox/clearSandboxState"; | ||
| import { hasRuntimeSandboxState } from "@/lib/sandbox/hasRuntimeSandboxState"; | ||
| import { selectSessions } from "@/lib/supabase/sessions/selectSessions"; | ||
| import { updateSession } from "@/lib/supabase/sessions/updateSession"; | ||
| import type { Tables } from "@/types/database.types"; | ||
| import type { Json } from "@/types/database.types"; | ||
|
|
||
| /** | ||
| * Fire-and-forget sandbox teardown for newly-archived sessions. | ||
| * | ||
| * Stops the running sandbox then clears its runtime state so the | ||
| * auto-hibernate workflow ignores the row. When the stop fails, persists | ||
| * a `lifecycle_error` and — if the row has no snapshot to fall back to — | ||
| * clears the runtime sandbox state so future unarchive attempts are not | ||
| * blocked forever by the 409 guard. | ||
| * | ||
| * Must be scheduled via `after()` so the HTTP response is not blocked. | ||
| * No-ops immediately when the session has no runtime sandbox. | ||
| * | ||
| * @param session - The session row as it existed before archiving. | ||
| */ | ||
| export async function stopSandboxOnArchive(session: Tables<"sessions">): Promise<void> { | ||
| if (!hasRuntimeSandboxState(session.sandbox_state)) return; | ||
|
|
||
| let stopError: unknown; | ||
|
|
||
| try { | ||
| const sandbox = await connectSandbox(session.sandbox_state as unknown as SandboxState); | ||
| await sandbox.stop(); | ||
| } catch (error) { | ||
| stopError = error; | ||
| console.error(`[stopSandboxOnArchive] stop failed for session ${session.id}:`, error); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| try { | ||
| const rows = await selectSessions({ id: session.id }); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| const current = rows?.[0] ?? null; | ||
|
|
||
| if (!current || current.status !== "archived") return; | ||
|
|
||
| if (stopError !== undefined) { | ||
| const message = stopError instanceof Error ? stopError.message : String(stopError); | ||
| const shouldClearState = | ||
| !current.snapshot_url && hasRuntimeSandboxState(current.sandbox_state); | ||
|
|
||
| await updateSession(session.id, { | ||
| lifecycle_error: `Archive finalization failed: ${message}`, | ||
| lifecycle_state: "archived", | ||
| lifecycle_run_id: null, | ||
| sandbox_expires_at: null, | ||
| hibernate_after: null, | ||
| ...(shouldClearState && { | ||
| sandbox_state: clearSandboxState(current.sandbox_state) as unknown as Json, | ||
| }), | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| await updateSession(session.id, { | ||
| snapshot_url: null, | ||
| snapshot_created_at: null, | ||
| sandbox_state: clearSandboxState(session.sandbox_state) as unknown as Json, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error(`[stopSandboxOnArchive] state update failed for session ${session.id}:`, error); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OCP