Skip to content

Commit 012b0c8

Browse files
Fix self-overwrite deleting sandbox templates before recreate.
Flatten onto the parent BasedOn when saving the same template the session is based on, and materialize the live rootfs if the source was already removed. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 1ea3e9b commit 012b0c8

9 files changed

Lines changed: 375 additions & 82 deletions

File tree

‎cmd/astonish/sandbox.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ func handleSandboxSave(identifier, templateName, description string) error {
11461146
}
11471147

11481148
// Step 1: Save session as temp template
1149-
if err := sandbox.CreateTemplateFromContainer(
1149+
if _, err := sandbox.CreateTemplateFromContainer(
11501150
client, tplRegistry, containerName, tmpName, "temporary promote", sourceTemplate, false,
11511151
); err != nil {
11521152
return fmt.Errorf("failed to save session as template: %w", err)
@@ -1171,7 +1171,7 @@ func handleSandboxSave(identifier, templateName, description string) error {
11711171
fmt.Printf("Saving session %s (%s) as template %q...\n",
11721172
sessionID[:min(8, len(sessionID))], containerName, templateName)
11731173

1174-
if err := sandbox.CreateTemplateFromContainer(
1174+
if _, err := sandbox.CreateTemplateFromContainer(
11751175
client, tplRegistry, containerName, templateName, description, sourceTemplate, false,
11761176
); err != nil {
11771177
return fmt.Errorf("failed to save session as template: %w", err)

‎docs/architecture/drill.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ suite_config:
7070
7171
Put offline / file / env configuration in `configure:` (agent prep). If an API must run **after** services are up, put it in `start-services.sh` after the ready poll.
7272

73-
Canonical scripts start each daemon under a **detached restart supervisor** (`setsid` + `nohup` + `while true` restart loop + supervisor PID file), poll until ready and briefly stable, then **exit 0**. Do **not** end with `wait`, and do **not** use bare `npm run dev &` or one-shot `setsid` without a restart loop. Prefer `npx vite --host 0.0.0.0` over `npm run dev`. Always run a newly written start script once before `save_sandbox_template`; use `overwrite: true` to replace an existing named template after fixing bootstrap files.
73+
Canonical scripts start each daemon under a **detached restart supervisor** (`setsid` + `nohup` + `while true` restart loop + supervisor PID file), poll until ready and briefly stable, then **exit 0**. Do **not** end with `wait`, and do **not** use bare `npm run dev &` or one-shot `setsid` without a restart loop. Prefer `npx vite --host 0.0.0.0` over `npm run dev`. Always run a newly written start script once before `save_sandbox_template`; use `overwrite: true` to replace an existing named template after fixing bootstrap files. Self-overwrite (session based on the same template name) flattens onto the parent and must not delete-first — see `pkg/sandbox/AGENTS.md`.
74+
75+
### Recovering a deleted template
76+
77+
If a failed overwrite removed the named template from the registry while the chat session still has the app filesystem:
78+
79+
1. Do **not** switch templates or reboot the session.
80+
2. Restart Studio on a binary with the flatten/materialize fix.
81+
3. `save_sandbox_template(template_name: "…", overwrite: true, bootstrap_files: …)` — recreates the template (materializes live rootfs onto `@base` if the source is already gone).
82+
4. Add `workspace` + `branch` on the suite YAML so Studio Run git-pulls next time.
83+
7484

7585
### Why Visual Regression Testing
7686

‎pkg/fleet/bundled/setup-profiles/software-development.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ steps:
338338
\ (optional prior build step if needed). Do NOT reinvent one-liner start\
339339
\ commands — use the script proven in 3h-v.\n - `ready_check:` — the verification method (type: http, port, or\
340340
\ output_contains) with the same endpoint/port you tested.\
341-
\ Prefer timeout >= 60; stable_count defaults to 3 consecutive successes.\n - `teardown:` — `bash <workspace>/.astonish/stop-services.sh || true`\n - `template:` — the template name (same as what you will save in 3j).\n - For multi-service projects, use the `services:` format instead.\n\n The suite handles building and starting. The drills (sub-step 3) test\n \
341+
\ Prefer timeout >= 60; stable_count defaults to 3 consecutive successes.\n - `teardown:` — `bash <workspace>/.astonish/stop-services.sh || true`\n - `template:` — the template name (same as what you will save in 3j).\n - `workspace:` — absolute path inside the container (e.g. `/root/<repo-name>`) so Studio Run includes `git pull`.\n - `branch:` — defaults to `main` when workspace is set.\n - For multi-service projects, use the `services:` format instead.\n\n The suite handles building and starting. The drills (sub-step 3) test\n \
342342
\ the RUNNING service.\n\n3. **Design 2-3 smoke drill YAMLs that test runtime behavior.**\n\n Before writing any drill\
343343
\ YAML, use `shell_command` to manually run\n each operation you plan to test against the live service started in\n\
344344
\ sub-step 1. Observe the actual response, then write the assertion\n based on what you saw. NEVER guess what the\

‎pkg/sandbox/AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ If you change the entrypoint, update the generator in `cmd/astonish-sandbox-entr
7070
- Incus: per-org bridge networks + profiles.
7171
- **Filesystem**: per-session overlay; template snapshots are content-addressed (Incus) or image-tagged (K8s/OpenShell).
7272
- **Bootstrap files**: template `bootstrap_files` (e.g. `.astonish/start-services.sh`) are injected at session start and never auto-executed — drills/fleet/chat call them after credentials.
73+
- **Template overwrite**: `CreateTemplateFromContainer(..., overwrite=true)` when saving the **same** name the session is based on **flattens** (template upper ∪ session upper) onto the parent `BasedOn` (usually `base`). Never delete the source template before that flatten finishes — delete-first self-overwrite leaves the registry empty and breaks `ResolveLowerLayers`. Overwriting a *different* name may still delete-first.
74+
- **Recovery if a template was deleted mid-overwrite**: Keep the live session (do not `use_sandbox_template` / reboot). Restart Studio with a build that has the flatten fix, then `save_sandbox_template(name, overwrite: true, bootstrap_files: …)` again. If the source is already gone from the registry, create materializes the session rootfs onto `@base`. If the session is already dead: recreate from `@base` (clone, deps, build, scripts) and save without overwrite.
7375
- **Identity**: `IssueSandboxToken` binds the in-pod supervisor's identity to the sandbox UUID via a gateway-issued JWT.
7476
- **Ops**: `tplStore.AcquireTemplateBuildLock` prevents concurrent Kaniko builds for the same template.
7577

0 commit comments

Comments
 (0)