An open-source, faster Devin alternative
TypeScript
add delete patch
Brian Reardon58194ca9ce48
diff --git a/docs/sdks/python/patches.mdx b/docs/sdks/python/patches.mdxindex 2d853098..5a2fc664 100644--- a/docs/sdks/python/patches.mdx+++ b/docs/sdks/python/patches.mdx@@ -48,6 +48,25 @@ patches = await Sandbox.list_checkpoint_patches(checkpoint["id"])**Returns:** `list[dict]` — ordered by sequence number.+## Delete a Patch++Remove a bad or unwanted patch. Remaining patches continue to apply in sequence order.++```python+await Sandbox.delete_checkpoint_patch(checkpoint["id"], patch["id"])+```++### `Sandbox.delete_checkpoint_patch(checkpoint_id, patch_id, **kwargs)`++<ParamField body="checkpoint_id" type="str" required>+ UUID of the checkpoint.+</ParamField>+<ParamField body="patch_id" type="str" required>+ UUID of the patch to delete.+</ParamField>++**Returns:** `None`+## How Patches Apply| Event | Patches run? |diff --git a/docs/sdks/typescript/patches.mdx b/docs/sdks/typescript/patches.mdxindex 2a89572f..63ec9a42 100644--- a/docs/sdks/typescript/patches.mdx+++ b/docs/sdks/typescript/patches.mdx@@ -51,6 +51,25 @@ const patches = await Sandbox.listCheckpointPatches(checkpoint.id);**Returns:** `Promise<PatchInfo[]>` — ordered by sequence number.+## Delete a Patch++Remove a bad or unwanted patch. Remaining patches continue to apply in sequence order.++```typescript+await Sandbox.deleteCheckpointPatch(checkpoint.id, patch.id);+```++### `Sandbox.deleteCheckpointPatch(checkpointId, patchId, opts?)`++<ParamField body="checkpointId" type="string" required>+ UUID of the checkpoint.+</ParamField>+<ParamField body="patchId" type="string" required>+ UUID of the patch to delete.+</ParamField>++**Returns:** `Promise<void>`+## How Patches Apply| Event | Patches run? |diff --git a/internal/api/router.go b/internal/api/router.goindex 6425ba52..5818a1c0 100644--- a/internal/api/router.go+++ b/internal/api/router.go@@ -145,6 +145,7 @@ func NewServer(mgr sandbox.Manager, ptyMgr *sandbox.PTYManager, apiKey string, o// Checkpoint patchesapi.POST("/sandboxes/checkpoints/:checkpointId/patches", s.createCheckpointPatch)api.GET("/sandboxes/checkpoints/:checkpointId/patches", s.listCheckpointPatches)+ api.DELETE("/sandboxes/checkpoints/:checkpointId/patches/:patchId", s.deleteCheckpointPatch)// Preview URLs (on-demand port-based)api.POST("/sandboxes/:id/preview", s.createPreviewURL)diff --git a/internal/api/sandbox.go b/internal/api/sandbox.goindex 69902d2b..c6e9b64a 100644--- a/internal/api/sandbox.go+++ b/internal/api/sandbox.go@@ -1407,6 +1407,47 @@ func (s *Server) listCheckpointPatches(c echo.Context) error {return c.JSON(http.StatusOK, patches)}+// deleteCheckpointPatch removes a patch from a checkpoint.+func (s *Server) deleteCheckpointPatch(c echo.Context) error {+ checkpointIDStr := c.Param("checkpointId")+ patchIDStr := c.Param("patchId")+ ctx := c.Request().Context()++ if s.store == nil {+ return c.JSON(http.StatusServiceUnavailable, map[string]string{"error": "database not configured"})+ }++ orgID, ok := auth.GetOrgID(c)+ if !ok {+ return c.JSON(http.StatusUnauthorized, map[string]string{"error": "org context required"})+ }++ checkpointID, err := uuid.Parse(checkpointIDStr)+ if err != nil {+ return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid checkpoint ID"})+ }++ patchID, err := uuid.Parse(patchIDStr)+ if err != nil {+ return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid patch ID"})+ }++ // Verify checkpoint belongs to org+ cp, err := s.store.GetCheckpoint(ctx, checkpointID)+ if err != nil {+ return c.JSON(http.StatusNotFound, map[string]string{"error": "checkpoint not found"})+ }+ if cp.OrgID != orgID {+ return c.JSON(http.StatusForbidden, map[string]string{"error": "checkpoint does not belong to this organization"})+ }++ if err := s.store.DeleteCheckpointPatch(ctx, checkpointID, patchID); err != nil {+ return c.JSON(http.StatusNotFound, map[string]string{"error": "patch not found"})+ }++ return c.NoContent(http.StatusNoContent)+}+// listSessions returns session history from PostgreSQL.func (s *Server) listWorkers(c echo.Context) error {if s.workerRegistry == nil {diff --git a/internal/db/store.go b/internal/db/store.goindex f7b5c2ac..aa3f6f9f 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -903,6 +903,20 @@ func (s *Store) GetPendingPatches(ctx context.Context, checkpointID uuid.UUID, areturn patches, rows.Err()}+// DeleteCheckpointPatch deletes a patch by ID, scoped to a checkpoint.+func (s *Store) DeleteCheckpointPatch(ctx context.Context, checkpointID, patchID uuid.UUID) error {+ tag, err := s.pool.Exec(ctx,+ `DELETE FROM checkpoint_patches WHERE id = $1 AND checkpoint_id = $2`,+ patchID, checkpointID)+ if err != nil {+ return err+ }+ if tag.RowsAffected() == 0 {+ return fmt.Errorf("patch not found")+ }+ return nil+}+// UpdateSandboxPatchSequence updates the last_patch_sequence for a sandbox session.func (s *Store) UpdateSandboxPatchSequence(ctx context.Context, sandboxID string, sequence int) error {_, err := s.pool.Exec(ctx,diff --git a/sdks/python/opencomputer/sandbox.py b/sdks/python/opencomputer/sandbox.pyindex 992b1b2b..d8a1e3d5 100644--- a/sdks/python/opencomputer/sandbox.py+++ b/sdks/python/opencomputer/sandbox.py@@ -361,6 +361,38 @@ class Sandbox:resp.raise_for_status()return resp.json()+ @staticmethod+ async def delete_checkpoint_patch(+ checkpoint_id: str,+ patch_id: str,+ api_key: str | None = None,+ api_url: str | None = None,+ ) -> None:+ """Delete a patch from a checkpoint.++ Args:+ checkpoint_id: UUID of the checkpoint.+ patch_id: UUID of the patch to delete.+ api_key: API key (or OPENCOMPUTER_API_KEY env var).+ api_url: API URL (or OPENCOMPUTER_API_URL env var).+ """+ 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.delete(+ f"/sandboxes/checkpoints/{checkpoint_id}/patches/{patch_id}"+ )+ if resp.status_code != 404:+ resp.raise_for_status()+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.tsindex c7c3af62..9bbc3ed9 100644--- a/sdks/typescript/examples/test-checkpoint-patches.ts+++ b/sdks/typescript/examples/test-checkpoint-patches.ts@@ -6,7 +6,9 @@* 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+ * 5. Bad patch blocks the chain+ * 6. Delete the bad patch — recovery on next wake+ * 7. Delete a patch and verify list updates** Usage:* npx tsx examples/test-checkpoint-patches.ts@@ -40,6 +42,20 @@ async function sleep(ms: number) {return new Promise((r) => globalThis.setTimeout(r, ms));}+async function waitForSandboxReady(sandbox: Sandbox, timeoutMs = 60000): Promise<boolean> {+ const start = Date.now();+ while (Date.now() - start < timeoutMs) {+ try {+ const result = await sandbox.commands.run("echo ready", { timeout: 10 });+ if (result.stdout.trim() === "ready") return true;+ } catch {+ // sandbox not ready yet+ }+ await sleep(3000);+ }+ return false;+}+async function waitForCheckpointReady(sandbox: Sandbox, checkpointId: string, timeoutMs = 60000): Promise<boolean> {const start = Date.now();while (Date.now() - start < timeoutMs) {@@ -67,12 +83,10 @@ async function main() {sandboxes.push(source);green(`Created source sandbox: ${source.sandboxId}`);- // Install a marker to verify patch effectsawait 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 checkpointconst cp = await source.createCheckpoint("patch-base");dim(`Checkpoint ID: ${cp.id}`);@@ -102,8 +116,13 @@ async function main() {sandboxes.push(fork1);green(`Fork 1 created: ${fork1.sandboxId}`);- // Wait for boot + patch application- await sleep(8000);+ dim("Waiting for fork 1 to be ready...");+ const fork1Ready = await waitForSandboxReady(fork1);+ check("Fork 1 is ready", fork1Ready);+ if (!fork1Ready) throw new Error("Fork 1 not ready, cannot proceed");++ // Give patches a moment to complete after sandbox is accepting commands+ await sleep(5000);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()}`);@@ -123,7 +142,6 @@ async function main() {check("Second patch created", patch2Result.patch.id !== undefined);check("Patch sequence is 2", patch2Result.patch.sequence === 2);- // 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");@@ -137,13 +155,14 @@ async function main() {await fork1.wake({ timeout: 600 });green("Fork 1 woken");- // Wait for post-wake patching+ dim("Waiting for fork 1 to be ready after wake...");+ const fork1WakeReady = await waitForSandboxReady(fork1);+ check("Fork 1 ready after wake", fork1WakeReady);await sleep(5000);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 presentconst 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();@@ -157,8 +176,8 @@ async function main() {check("Second patch has sequence 2", patches[1]?.sequence === 2);console.log();- // ── Test 5: Bad patch (should fail gracefully) ───────────────- bold("--- Test 5: Bad patch script ---\n");+ // ── Test 5: Bad patch blocks the chain ───────────────────────+ bold("--- Test 5: Bad patch blocks the chain ---\n");const badPatch = await Sandbox.createCheckpointPatch(cp.id, {script: "exit 1",@@ -167,20 +186,85 @@ async function main() {check("Bad patch created", badPatch.patch.id !== undefined);check("Bad patch sequence is 3", badPatch.patch.sequence === 3);+ dim(`Bad patch ID: ${badPatch.patch.id}`);++ // Add a 4th patch after the bad one+ const patch4Result = await Sandbox.createCheckpointPatch(cp.id, {+ script: "echo 'post-bad' > /root/patch4.txt",+ description: "Patch after the bad one",+ });+ check("Patch 4 created (after bad)", patch4Result.patch.sequence === 4);- // Fork a new sandbox — bad patch should fail and stop the chain+ // Fork — bad patch should block patch 4 from applyingconst fork2 = await Sandbox.createFromCheckpoint(cp.id, { timeout: 600 });sandboxes.push(fork2);green(`Fork 2 created: ${fork2.sandboxId}`);- await sleep(8000);+ dim("Waiting for fork 2 to be ready...");+ const fork2Ready = await waitForSandboxReady(fork2);+ check("Fork 2 is ready", fork2Ready);+ if (!fork2Ready) throw new Error("Fork 2 not ready, cannot proceed");+ await sleep(5000);- // Patches 1 and 2 should have applied, but patch 3 (exit 1) should have failed+ // Patches 1+2 should apply, patch 3 fails, patch 4 never runsconst 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()}`);+ check("Patch 2 applied", f2Security.stdout.trim() === "security-fix-applied", `got: ${f2Security.stdout.trim()}`);++ const f2Patch4 = await fork2.commands.run("test -f /root/patch4.txt && echo exists || echo missing");+ check("Patch 4 blocked by bad patch 3", f2Patch4.stdout.trim() === "missing");+ console.log();++ // ── Test 6: Delete bad patch — recovery on wake ──────────────+ bold("--- Test 6: Delete bad patch — recovery on wake ---\n");++ dim(`Deleting bad patch ${badPatch.patch.id}...`);+ await Sandbox.deleteCheckpointPatch(cp.id, badPatch.patch.id);+ green("Bad patch deleted");++ // Verify it's gone from the list+ const patchesAfterDelete = await Sandbox.listCheckpointPatches(cp.id);+ check("3 patches remaining after delete", patchesAfterDelete.length === 3, `got ${patchesAfterDelete.length}`);+ const sequences = patchesAfterDelete.map((p) => p.sequence);+ check("Remaining sequences are 1, 2, 4", JSON.stringify(sequences) === "[1,2,4]", `got ${JSON.stringify(sequences)}`);++ // Hibernate fork2 and wake — patch 4 should now apply (bad patch 3 is gone)+ dim("Hibernating fork 2...");+ await fork2.hibernate();+ green("Fork 2 hibernated");++ await sleep(3000);++ dim("Waking fork 2...");+ await fork2.wake({ timeout: 600 });+ green("Fork 2 woken");++ dim("Waiting for fork 2 to be ready after wake...");+ const fork2WakeReady = await waitForSandboxReady(fork2);+ check("Fork 2 ready after wake", fork2WakeReady);+ await sleep(5000);++ const f2Patch4AfterWake = await fork2.commands.run("cat /root/patch4.txt");+ check("Patch 4 applied after deleting bad patch and waking", f2Patch4AfterWake.stdout.trim() === "post-bad", `got: ${f2Patch4AfterWake.stdout.trim()}`);++ // Prior state still intact+ const f2PythonAfterWake = await fork2.commands.run("cat /root/python-version.txt");+ check("All prior patches preserved", f2PythonAfterWake.stdout.trim() === "python3.11", `got: ${f2PythonAfterWake.stdout.trim()}`);+ console.log();++ // ── Test 7: Delete is idempotent ─────────────────────────────+ bold("--- Test 7: Delete is idempotent ---\n");++ // Deleting the same patch again should not throw+ let deleteAgainOk = true;+ try {+ await Sandbox.deleteCheckpointPatch(cp.id, badPatch.patch.id);+ } catch {+ deleteAgainOk = false;+ }+ check("Deleting already-deleted patch does not throw", deleteAgainOk);console.log();} catch (err: any) {diff --git a/sdks/typescript/src/sandbox.ts b/sdks/typescript/src/sandbox.tsindex 2d2696ce..0cd52558 100644--- a/sdks/typescript/src/sandbox.ts+++ b/sdks/typescript/src/sandbox.ts@@ -374,6 +374,24 @@ export class Sandbox {return resp.json();}+ static async deleteCheckpointPatch(+ checkpointId: string,+ patchId: string,+ opts: { apiKey?: string; apiUrl?: string } = {}+ ): Promise<void> {+ const apiUrl = resolveApiUrl(opts.apiUrl ?? process.env.OPENCOMPUTER_API_URL ?? "https://app.opencomputer.dev");+ const apiKey = opts.apiKey ?? process.env.OPENCOMPUTER_API_KEY ?? "";++ const resp = await fetch(`${apiUrl}/sandboxes/checkpoints/${checkpointId}/patches/${patchId}`, {+ method: "DELETE",+ headers: apiKey ? { "X-API-Key": apiKey } : {},+ });++ if (!resp.ok && resp.status !== 404) {+ throw new Error(`Failed to delete checkpoint patch: ${resp.status}`);+ }+ }+async createPreviewURL(opts: { port: number; domain?: string; authConfig?: Record<string, unknown> }): Promise<PreviewURLResult> {const resp = await fetch(`${this.apiUrl}/sandboxes/${this.sandboxId}/preview`, {method: "POST",