Skip to content

Commit f010076

Browse files
committed
Revert "fix: skip expired snapshots and fallback to fresh sandbox on failure (#407)"
This reverts commit 85fa5b8.
1 parent 85fa5b8 commit f010076

2 files changed

Lines changed: 15 additions & 61 deletions

File tree

lib/sandbox/createSandboxFromSnapshot.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export interface CreateSandboxFromSnapshotResult {
99
}
1010

1111
/**
12-
* Creates a new sandbox from the account's latest valid snapshot,
13-
* falling back to a fresh sandbox if the snapshot is expired or fails.
12+
* Creates a new sandbox from the account's latest snapshot (or fresh if none)
13+
* and records it in the database.
1414
*
1515
* @param accountId - The account ID to create a sandbox for
1616
* @returns The created Sandbox instance and whether it was created from a snapshot
@@ -19,34 +19,16 @@ export async function createSandboxFromSnapshot(
1919
accountId: string,
2020
): Promise<CreateSandboxFromSnapshotResult> {
2121
const snapshots = await selectAccountSnapshots(accountId);
22-
const snapshot = snapshots[0];
22+
const snapshotId = snapshots[0]?.snapshot_id;
2323

24-
const isExpired = snapshot?.expires_at && new Date(snapshot.expires_at) < new Date();
25-
const snapshotId = !isExpired ? snapshot?.snapshot_id : undefined;
26-
27-
let sandbox: Sandbox;
28-
let fromSnapshot = false;
29-
30-
if (snapshotId) {
31-
try {
32-
const result = await createSandbox({
33-
source: { type: "snapshot", snapshotId },
34-
});
35-
sandbox = result.sandbox;
36-
fromSnapshot = true;
37-
} catch {
38-
const result = await createSandbox({});
39-
sandbox = result.sandbox;
40-
}
41-
} else {
42-
const result = await createSandbox({});
43-
sandbox = result.sandbox;
44-
}
24+
const { sandbox, response } = await createSandbox(
25+
snapshotId ? { source: { type: "snapshot", snapshotId } } : {},
26+
);
4527

4628
await insertAccountSandbox({
4729
account_id: accountId,
48-
sandbox_id: sandbox.sandboxId,
30+
sandbox_id: response.sandboxId,
4931
});
5032

51-
return { sandbox, fromSnapshot };
33+
return { sandbox, fromSnapshot: !!snapshotId };
5234
}

lib/sandbox/processCreateSandbox.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@ type ProcessCreateSandboxInput = {
99
};
1010
type ProcessCreateSandboxResult = SandboxCreatedResponse & { runId?: string };
1111

12-
/**
13-
* Returns a valid (non-expired) snapshot ID for the account, or undefined.
14-
*
15-
* @param accountId - The account to look up
16-
* @returns The snapshot ID if it exists and has not expired
17-
*/
18-
async function getValidSnapshotId(accountId: string): Promise<string | undefined> {
19-
const accountSnapshots = await selectAccountSnapshots(accountId);
20-
const snapshot = accountSnapshots[0];
21-
if (!snapshot?.snapshot_id) return undefined;
22-
23-
if (snapshot.expires_at && new Date(snapshot.expires_at) < new Date()) {
24-
return undefined;
25-
}
26-
27-
return snapshot.snapshot_id;
28-
}
29-
3012
/**
3113
* Shared domain logic for creating a sandbox and optionally running a prompt.
3214
* Used by both POST /api/sandboxes handler and the prompt_sandbox MCP tool.
@@ -39,24 +21,14 @@ export async function processCreateSandbox(
3921
): Promise<ProcessCreateSandboxResult> {
4022
const { accountId, prompt } = input;
4123

42-
const snapshotId = await getValidSnapshotId(accountId);
43-
44-
let result;
24+
// Get account's most recent snapshot if available
25+
const accountSnapshots = await selectAccountSnapshots(accountId);
26+
const snapshotId = accountSnapshots[0]?.snapshot_id;
4527

46-
if (snapshotId) {
47-
try {
48-
const createResult = await createSandbox({
49-
source: { type: "snapshot", snapshotId },
50-
});
51-
result = createResult.response;
52-
} catch {
53-
const freshResult = await createSandbox({});
54-
result = freshResult.response;
55-
}
56-
} else {
57-
const freshResult = await createSandbox({});
58-
result = freshResult.response;
59-
}
28+
// Create sandbox (from snapshot if valid, otherwise fresh)
29+
const { response: result } = await createSandbox(
30+
snapshotId ? { source: { type: "snapshot", snapshotId } } : {},
31+
);
6032

6133
await insertAccountSandbox({
6234
account_id: accountId,

0 commit comments

Comments
 (0)