Skip to content

An open-source, faster Devin alternative

TypeScript

remove hot patch, change for overlay changes

Brian Reardon350234a3d102
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 9251c073..69902d2b 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -1262,7 +1262,6 @@ func (s *Server) createCheckpointPatch(c echo.Context) error {
var req struct {
Script string `json:"script"`
Description string `json:"description"`
- Strategy string `json:"strategy"`
}
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
@@ -1270,61 +1269,20 @@ func (s *Server) createCheckpointPatch(c echo.Context) error {
if req.Script == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "script is required"})
}
- if req.Strategy == "" {
- req.Strategy = "hot"
- }
- if req.Strategy != "hot" && req.Strategy != "on_wake" {
- return c.JSON(http.StatusBadRequest, map[string]string{"error": "strategy must be 'hot' or 'on_wake'"})
- }
-
- // Create the patch record
+ // Create the patch record (patches apply on next wake/boot)
patch := &db.CheckpointPatch{
ID: uuid.New(),
CheckpointID: checkpointID,
Script: req.Script,
Description: req.Description,
- Strategy: req.Strategy,
+ Strategy: "on_wake",
}
if err := s.store.CreateCheckpointPatch(ctx, patch); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to create patch: " + err.Error()})
}
- // Fan out to running sandboxes if strategy is "hot"
- var results struct {
- Total int `json:"total"`
- RunningPatched int `json:"runningPatched"`
- RunningFailed int `json:"runningFailed"`
- HibernatedQueued int `json:"hibernatedQueued"`
- }
-
- sandboxes, err := s.store.ListSandboxesByCheckpoint(ctx, checkpointID)
- if err != nil {
- log.Printf("api: patch %s: failed to list sandboxes: %v", patch.ID, err)
- } else {
- results.Total = len(sandboxes)
-
- for _, sess := range sandboxes {
- if sess.Status == "hibernated" {
- results.HibernatedQueued++
- continue
- }
-
- if sess.Status == "running" && req.Strategy == "hot" {
- // Execute patch on running sandbox
- if err := s.execPatchOnSandbox(ctx, sess.SandboxID, sess.WorkerID, patch); err != nil {
- log.Printf("api: patch %s: sandbox %s failed: %v", patch.ID, sess.SandboxID, err)
- results.RunningFailed++
- } else {
- results.RunningPatched++
- _ = s.store.UpdateSandboxPatchSequence(ctx, sess.SandboxID, patch.Sequence)
- }
- }
- }
- }
-
return c.JSON(http.StatusCreated, map[string]interface{}{
- "patch": patch,
- "results": results,
+ "patch": patch,
})
}
diff --git a/sdks/python/opencomputer/sandbox.py b/sdks/python/opencomputer/sandbox.py
index 1b13694f..992b1b2b 100644
--- a/sdks/python/opencomputer/sandbox.py
+++ b/sdks/python/opencomputer/sandbox.py
@@ -292,6 +292,75 @@ class Sandbox:
_data_client=data_client,
)
+ @staticmethod
+ async def create_checkpoint_patch(
+ checkpoint_id: str,
+ script: str,
+ description: str = "",
+ api_key: str | None = None,
+ api_url: str | None = None,
+ ) -> dict:
+ """Create a patch for a checkpoint (applied on next wake/boot).
+
+ Args:
+ checkpoint_id: UUID of the checkpoint to patch.
+ script: Bash script to execute on each forked sandbox.
+ description: Human-readable description of the patch.
+ api_key: API key (or OPENCOMPUTER_API_KEY env var).
+ api_url: API URL (or OPENCOMPUTER_API_URL env var).
+
+ Returns:
+ Dict with "patch" info (id, sequence, script, etc.).
+ """
+ url = api_url or os.environ.get("OPENCOMPUTER_API_URL", "https://app.opencomputer.dev")
+ url = url.rstrip("/")
+ key = api_key or os.environ.get("OPENCOMPUTER_API_KEY", "")
+
+ api_base = url if url.endswith("/api") else f"{url}/api"
+
+ headers = {}
+ if key:
+ headers["X-API-Key"] = key
+
+ async with httpx.AsyncClient(base_url=api_base, headers=headers, timeout=300.0) as client:
+ resp = await client.post(
+ f"/sandboxes/checkpoints/{checkpoint_id}/patches",
+ json={"script": script, "description": description},
+ )
+ resp.raise_for_status()
+ return resp.json()
+
+ @staticmethod
+ async def list_checkpoint_patches(
+ checkpoint_id: str,
+ api_key: str | None = None,
+ api_url: str | None = None,
+ ) -> list[dict]:
+ """List all patches for a checkpoint, ordered by sequence.
+
+ Args:
+ checkpoint_id: UUID of the checkpoint.
+ api_key: API key (or OPENCOMPUTER_API_KEY env var).
+ api_url: API URL (or OPENCOMPUTER_API_URL env var).
+
+ Returns:
+ List of patch dicts with id, sequence, script, strategy, etc.
+ """
+ url = api_url or os.environ.get("OPENCOMPUTER_API_URL", "https://app.opencomputer.dev")
+ url = url.rstrip("/")
+ key = api_key or os.environ.get("OPENCOMPUTER_API_KEY", "")
+
+ api_base = url if url.endswith("/api") else f"{url}/api"
+
+ headers = {}
+ if key:
+ headers["X-API-Key"] = key
+
+ async with httpx.AsyncClient(base_url=api_base, headers=headers, timeout=30.0) as client:
+ resp = await client.get(f"/sandboxes/checkpoints/{checkpoint_id}/patches")
+ resp.raise_for_status()
+ return resp.json()
+
async def delete_checkpoint(self, checkpoint_id: str) -> None:
"""Delete a checkpoint.
diff --git a/sdks/typescript/examples/test-checkpoint-patches.ts b/sdks/typescript/examples/test-checkpoint-patches.ts
index ffdef252..c7c3af62 100644
--- a/sdks/typescript/examples/test-checkpoint-patches.ts
+++ b/sdks/typescript/examples/test-checkpoint-patches.ts
@@ -2,14 +2,11 @@
* Checkpoint Patch System Test
*
* Tests:
- * 1. Create a sandbox, checkpoint it, fork 2 sandboxes from it
- * 2. Create a "hot" patch on the checkpoint (should exec on running sandboxes)
- * 3. Verify the patch was applied to both running forks
- * 4. Create a third fork — verify existing patches are applied on boot
- * 5. Create an "on_wake" patch
- * 6. Hibernate a fork, wake it, verify on_wake patch was applied
- * 7. List patches and verify ordering
- * 8. Verify patch failure handling (bad script)
+ * 1. Create a sandbox, checkpoint it, create a patch
+ * 2. Fork a sandbox — verify patch is applied on boot
+ * 3. Create a second patch, hibernate fork, wake it — verify both patches applied
+ * 4. List patches and verify ordering
+ * 5. Verify bad patch script fails gracefully
*
* Usage:
* npx tsx examples/test-checkpoint-patches.ts
@@ -71,9 +68,8 @@ async function main() {
green(`Created source sandbox: ${source.sandboxId}`);
// Install a marker to verify patch effects
- await source.commands.run("echo 'original' > /workspace/patch-marker.txt");
- await source.commands.run("echo 'python3.10' > /workspace/python-version.txt");
- const initial = await source.commands.run("cat /workspace/python-version.txt");
+ await source.commands.run("echo 'python3.10' > /root/python-version.txt");
+ const initial = await source.commands.run("cat /root/python-version.txt");
check("Initial state set", initial.stdout.trim() === "python3.10");
// Create checkpoint
@@ -86,102 +82,50 @@ async function main() {
if (!ready) throw new Error("Checkpoint not ready, cannot proceed");
console.log();
- // ── Test 1: Fork 2 sandboxes from checkpoint ──────────────────
- bold("--- Test 1: Fork sandboxes from checkpoint ---\n");
-
- const fork1 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });
- sandboxes.push(fork1);
- green(`Fork 1 created: ${fork1.sandboxId}`);
-
- const fork2 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });
- sandboxes.push(fork2);
- green(`Fork 2 created: ${fork2.sandboxId}`);
-
- // Wait for forks to boot
- await sleep(5000);
-
- // Verify forks have the original state
- const f1State = await fork1.commands.run("cat /workspace/python-version.txt");
- check("Fork 1 has original state", f1State.stdout.trim() === "python3.10");
- const f2State = await fork2.commands.run("cat /workspace/python-version.txt");
- check("Fork 2 has original state", f2State.stdout.trim() === "python3.10");
- console.log();
-
- // ── Test 2: Create a "hot" patch ─────────────────────────────
- bold("--- Test 2: Create hot patch (should apply to running forks) ---\n");
+ // ── Test 1: Create a patch ──────────────────────────────────
+ bold("--- Test 1: Create a patch ---\n");
const patchResult = await Sandbox.createCheckpointPatch(cp.id, {
- script: "echo 'python3.11' > /workspace/python-version.txt && echo 'patched' > /workspace/patch-applied.txt",
+ script: "echo 'python3.11' > /root/python-version.txt && echo 'patched' > /root/patch-applied.txt",
description: "Upgrade Python version marker",
- strategy: "hot",
});
check("Patch created", patchResult.patch.id !== undefined);
check("Patch sequence is 1", patchResult.patch.sequence === 1);
- check("Patch strategy is hot", patchResult.patch.strategy === "hot");
dim(`Patch ID: ${patchResult.patch.id}`);
- dim(`Results: total=${patchResult.results.total}, patched=${patchResult.results.runningPatched}, failed=${patchResult.results.runningFailed}, queued=${patchResult.results.hibernatedQueued}`);
-
- // The source sandbox is not forked from the checkpoint, so only fork1 and fork2 should be patched
- check("Running sandboxes were patched", patchResult.results.runningPatched >= 2, `got ${patchResult.results.runningPatched}`);
- check("No failures", patchResult.results.runningFailed === 0, `got ${patchResult.results.runningFailed}`);
console.log();
- // ── Test 3: Verify patch was applied ─────────────────────────
- bold("--- Test 3: Verify patch applied to running forks ---\n");
-
- // Small delay for exec to complete
- await sleep(2000);
-
- const f1Patched = await fork1.commands.run("cat /workspace/python-version.txt");
- check("Fork 1 has patched state", f1Patched.stdout.trim() === "python3.11", `got: ${f1Patched.stdout.trim()}`);
-
- const f1Marker = await fork1.commands.run("cat /workspace/patch-applied.txt");
- check("Fork 1 has patch marker", f1Marker.stdout.trim() === "patched");
+ // ── Test 2: Fork sandbox — patch applied on boot ────────────
+ bold("--- Test 2: Fork sandbox — patch applied on boot ---\n");
- const f2Patched = await fork2.commands.run("cat /workspace/python-version.txt");
- check("Fork 2 has patched state", f2Patched.stdout.trim() === "python3.11", `got: ${f2Patched.stdout.trim()}`);
- console.log();
-
- // ── Test 4: New fork gets existing patches applied ───────────
- bold("--- Test 4: New fork gets existing patches on boot ---\n");
-
- const fork3 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });
- sandboxes.push(fork3);
- green(`Fork 3 created: ${fork3.sandboxId}`);
+ const fork1 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });
+ sandboxes.push(fork1);
+ green(`Fork 1 created: ${fork1.sandboxId}`);
// Wait for boot + patch application
await sleep(8000);
- const f3Patched = await fork3.commands.run("cat /workspace/python-version.txt");
- check("Fork 3 has patched state (applied on boot)", f3Patched.stdout.trim() === "python3.11", `got: ${f3Patched.stdout.trim()}`);
+ const f1Patched = await fork1.commands.run("cat /root/python-version.txt");
+ check("Fork 1 has patched state (applied on boot)", f1Patched.stdout.trim() === "python3.11", `got: ${f1Patched.stdout.trim()}`);
- const f3Marker = await fork3.commands.run("cat /workspace/patch-applied.txt");
- check("Fork 3 has patch marker", f3Marker.stdout.trim() === "patched", `got: ${f3Marker.stdout.trim()}`);
+ const f1Marker = await fork1.commands.run("cat /root/patch-applied.txt");
+ check("Fork 1 has patch marker", f1Marker.stdout.trim() === "patched");
console.log();
- // ── Test 5: Create "on_wake" patch ───────────────────────────
- bold("--- Test 5: Create on_wake patch ---\n");
+ // ── Test 3: Second patch + hibernate/wake ───────────────────
+ bold("--- Test 3: Second patch applied after hibernate + wake ---\n");
const patch2Result = await Sandbox.createCheckpointPatch(cp.id, {
- script: "echo 'security-fix-applied' > /workspace/security-patch.txt",
+ script: "echo 'security-fix-applied' > /root/security-patch.txt",
description: "Security hotfix",
- strategy: "on_wake",
});
- check("on_wake patch created", patch2Result.patch.id !== undefined);
+ check("Second patch created", patch2Result.patch.id !== undefined);
check("Patch sequence is 2", patch2Result.patch.sequence === 2);
- check("on_wake patch did NOT exec on running sandboxes", patch2Result.results.runningPatched === 0);
- dim(`Hibernated queued: ${patch2Result.results.hibernatedQueued}`);
- console.log();
-
- // Verify on_wake patch was NOT applied to running fork1
- const f1Security = await fork1.commands.run("test -f /workspace/security-patch.txt && echo exists || echo missing");
- check("on_wake patch not applied to running sandbox", f1Security.stdout.trim() === "missing");
- console.log();
- // ── Test 6: Hibernate + wake → on_wake patch applied ─────────
- bold("--- Test 6: Hibernate and wake fork (patches applied on wake) ---\n");
+ // Verify patch is NOT yet applied (fork1 is still running, patch only applies on wake/boot)
+ const f1Security = await fork1.commands.run("test -f /root/security-patch.txt && echo exists || echo missing");
+ check("Patch not applied to running sandbox", f1Security.stdout.trim() === "missing");
dim("Hibernating fork 1...");
await fork1.hibernate();
@@ -196,38 +140,47 @@ async function main() {
// Wait for post-wake patching
await sleep(5000);
- const f1SecurityAfterWake = await fork1.commands.run("cat /workspace/security-patch.txt");
- check("on_wake patch applied after wake", f1SecurityAfterWake.stdout.trim() === "security-fix-applied", `got: ${f1SecurityAfterWake.stdout.trim()}`);
+ const f1SecurityAfterWake = await fork1.commands.run("cat /root/security-patch.txt");
+ check("Second patch applied after wake", f1SecurityAfterWake.stdout.trim() === "security-fix-applied", `got: ${f1SecurityAfterWake.stdout.trim()}`);
// Verify prior patches still present
- const f1PythonAfterWake = await fork1.commands.run("cat /workspace/python-version.txt");
+ const f1PythonAfterWake = await fork1.commands.run("cat /root/python-version.txt");
check("Prior patch state preserved after wake", f1PythonAfterWake.stdout.trim() === "python3.11", `got: ${f1PythonAfterWake.stdout.trim()}`);
console.log();
- // ── Test 7: List patches ─────────────────────────────────────
- bold("--- Test 7: List patches ---\n");
+ // ── Test 4: List patches ─────────────────────────────────────
+ bold("--- Test 4: List patches ---\n");
const patches = await Sandbox.listCheckpointPatches(cp.id);
check("2 patches listed", patches.length === 2, `got ${patches.length}`);
check("First patch has sequence 1", patches[0]?.sequence === 1);
check("Second patch has sequence 2", patches[1]?.sequence === 2);
- check("First patch is hot", patches[0]?.strategy === "hot");
- check("Second patch is on_wake", patches[1]?.strategy === "on_wake");
console.log();
- // ── Test 8: Bad patch (should fail gracefully) ───────────────
- bold("--- Test 8: Bad patch script ---\n");
+ // ── Test 5: Bad patch (should fail gracefully) ───────────────
+ bold("--- Test 5: Bad patch script ---\n");
const badPatch = await Sandbox.createCheckpointPatch(cp.id, {
script: "exit 1",
description: "Intentionally failing patch",
- strategy: "hot",
});
check("Bad patch created", badPatch.patch.id !== undefined);
check("Bad patch sequence is 3", badPatch.patch.sequence === 3);
- // Running sandboxes should report failures
- check("Running sandboxes reported failures", badPatch.results.runningFailed > 0, `failed=${badPatch.results.runningFailed}`);
+
+ // Fork a new sandbox — bad patch should fail and stop the chain
+ const fork2 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });
+ sandboxes.push(fork2);
+ green(`Fork 2 created: ${fork2.sandboxId}`);
+
+ await sleep(8000);
+
+ // Patches 1 and 2 should have applied, but patch 3 (exit 1) should have failed
+ const f2Patched = await fork2.commands.run("cat /root/python-version.txt");
+ check("Good patches applied before bad one", f2Patched.stdout.trim() === "python3.11", `got: ${f2Patched.stdout.trim()}`);
+
+ const f2Security = await fork2.commands.run("cat /root/security-patch.txt");
+ check("Second good patch also applied", f2Security.stdout.trim() === "security-fix-applied", `got: ${f2Security.stdout.trim()}`);
console.log();
} catch (err: any) {
diff --git a/sdks/typescript/src/sandbox.ts b/sdks/typescript/src/sandbox.ts
index 7c228d00..2d2696ce 100644
--- a/sdks/typescript/src/sandbox.ts
+++ b/sdks/typescript/src/sandbox.ts
@@ -51,12 +51,6 @@ export interface PatchInfo {
export interface PatchResult {
patch: PatchInfo;
- results: {
- total: number;
- runningPatched: number;
- runningFailed: number;
- hibernatedQueued: number;
- };
}
export interface PreviewURLResult {
@@ -337,7 +331,7 @@ export class Sandbox {
static async createCheckpointPatch(
checkpointId: string,
- opts: { script: string; description?: string; strategy?: "hot" | "on_wake"; apiKey?: string; apiUrl?: string }
+ opts: { script: string; description?: string; apiKey?: string; apiUrl?: string }
): Promise<PatchResult> {
const apiUrl = resolveApiUrl(opts.apiUrl ?? process.env.OPENCOMPUTER_API_URL ?? "https://app.opencomputer.dev");
const apiKey = opts.apiKey ?? process.env.OPENCOMPUTER_API_KEY ?? "";
@@ -351,7 +345,6 @@ export class Sandbox {
body: JSON.stringify({
script: opts.script,
description: opts.description ?? "",
- strategy: opts.strategy ?? "hot",
}),
});