carlos / platform Public

spec: restore a deleted instance with its own data (#658)

Rev 1. Restore through the create door: ownership proof from the mark,
slot and lineage; region from a stored last body, or a one-region scan
for older deletions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 91b745c847ac193f08c7c6026c0e934fc5c22bfa parent ce7576a
1 file changed, +114 −0
  • docs/superpowers/specs/2026-11-07-instance-restore-658-design.md +114 −0

CI failed — run details

diff --git a/docs/superpowers/specs/2026-11-07-instance-restore-658-design.md b/docs/superpowers/specs/2026-11-07-instance-restore-658-design.md
new file mode 100644
index 000000000..28fc37fba
--- /dev/null
+++ b/docs/superpowers/specs/2026-11-07-instance-restore-658-design.md
@@ -0,0 +1,114 @@
+# Restore a deleted instance with its own data (#658)
+
+Rev 1, 2026-11-07. Branch `design-instance-restore-658`. Discussion #658 on carlos/platform (taken by Paul, 2026-11-07).
+
+## 1. Why
+
+An app that installs other apps onto its own account (a front door) uninstalls an app by deleting its instance, and later reinstalls it on the same hostname. The person expects their data back. Today the create refuses with S1 ("<host> is in use or has held an app's data before, so it can't be created. Choose another address.") because the hostname history never lets a name be created twice (docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md §1). In the home region the leftover rail (#199) would refuse it too: "<host> still holds data from a deleted instance. Choose another address, or ask an operator to purge it." (apiinstances.go:2311).
+
+The history protects against a new record surfacing data that is not its own: another tenant's, or an older copy left by a move. It has no reason to refuse the same account and app taking back their own latest data. This spec adds that, and only that.
+
+**Success:** the front door deletes an instance, then creates it again on the same hostname with `restore` set, and the instance comes back with the database it had when it was deleted. A request that cannot be proven to be the same owner taking back its own newest copy is refused, with a message that says why.
+
+## 2. Goals, non-goals, anti-goals
+
+Goals:
+- A restore through the existing create door, for the CLI and for a service credential with the `instance` role (the same handler serves both, `handleAPIInstanceCreate`, apiinstances.go:1524).
+- Delete keeps a durable note of where the instance's last copy lived, so a restore of a moved instance lands on the right copy.
+- Instances deleted before this ships are restorable when one region alone holds their data (§5.2). The live case on account oficina is one of these.
+
+Non-goals:
+- Reusing a hostname for a different account, app or purpose. The never-reuse rule stays exactly as it is for every create without `restore`.
+- Bringing back the old record's configuration. The caller sends a full create body, as for any create; only the data comes back.
+- A stop/start state (route off, data kept). It may still be useful later; restore covers the ask without it.
+- Restoring sidecars, HA instances, rootfs instances, pool or fleet-placed instances, addr-backed instances, or replicas. Each is refused up front (§4, R7).
+- Restoring with claims on. Production runs claims off (shelved 2026-10-25); restore refuses if `HostClaim` is set.
+
+Anti-goals (a failure even if restore "works"):
+- A restored instance serving any copy other than the newest one its own last record wrote.
+- Any path where one account or app can reach data another account or app wrote.
+- Any weakening of S1, S4, S5 or the leftover rail for a create without `restore`.
+
+## 3. The door
+
+The create body gains `"restore": true`. The CLI flag is `carlos instances create --restore` (the CLI lives in its own repo; this repo owns the door and docs/site/cli.md). Everything a create checks today is still checked, in the same order, with three changes, and only when `restore` is set:
+
+1. **Region.** The region is the restore region from §5, never nearest-region or app-region placement. A request that names a different region is refused (R5). `checkPlacement` and the region's other create checks still apply.
+2. **The leftover rail** (createleftovers.go:136) does not refuse on the host's own replica prefix `instances/<host>`. It still refuses on move staging `moves/<host>` (R6), because a staged copy wins over the replica on wake (backend_exec.go restorePlain, "Staging wins when present") and is not the last record's data.
+3. **The mark step.** `markForCreate` (hostsused.go:631) treats a conflicting mark as a pass when, and only when, the ownership proof in §4 holds. The proof runs at the mark step under the same app lock and lifetime admission the create already holds (lifetimes_create.go:63), so nothing changes between the check and the write.
+
+`gateDataCreate` (S2, S4, S5) is unchanged and runs first, as today.
+
+The restored record is a new lifetime: incarnation N+1 from `LastIncarnation`, over the retired slot, as `planCreateLocked` already allows. The replica and the warm file are keyed by hostname alone (activator.go `HibernatePrefix`; provision.go `DefaultDBPath`), so the first wake finds the old data with no copying.
+
+The response is the create's response plus `"restored": true`. A restore that finds no data in the restore region still succeeds (the data was never written or is gone, which §1 of the history spec already calls harmless), and says so: `"restored": false`. **My call**: refusing would leave the owner unable to ever use the name again, for no safety gain.
+
+When an ordinary create (no `restore`) hits S1 and the ownership proof in §4 would pass, the refusal says how to restore (copy C2), so a caller learns the option from the error.
+
+## 4. Ownership proof
+
+A restore of `<host>` by account A, app P passes only when all of these hold. Any read that fails refuses with "could not check" (R1), never as a pass.
+
+1. The history mode is `active`. In any other mode, refuse (R2).
+2. Lifetimes are on (`lifetimesOn`, lifetimes_cutover.go:309). Otherwise refuse (R2).
+3. The mark `control/hosts-used/<host>.json` has `Source` `create` or `recovery` and names A and P (`HostUse`, provision/hostsused.go). An `inventory` mark (written at activation for a host already holding data, owner unknown) refuses (R3). So does a mark naming anyone else.
+4. The slot `control/host-incarnations/<host>.json` is `retired` and names A and P.
+5. Every `host_allocations` row for the host that is not `void`, and every `record_retirements` row for the host, names A and P. This is the single-lineage check: before activation a mark conflict did not refuse (hostsused.go `markPass`), so a host could have passed from one owner to another while the mark kept naming the first.
+6. The newest committed retirement's incarnation equals the slot's, and no allocation or retirement for the host is pending (`hostSettling`).
+7. The app's lifetime is the one the last allocation was bound to (`host_allocations.app_lifetime`). An app deleted and recreated under the same name is a different app: refuse (R4). **My call**, conservative; Paul may loosen it.
+8. The last record's shape is restorable (§2 non-goals), and so is the requested one: kind `instance`, paths derived, no HA, no runtime, no pool, no fleet placement, no replica role. Otherwise refuse (R7).
+
+Why this is enough: once the history is active, a host whose mark came from a create had no data on any examined target at activation (activation marks every host it finds as `inventory`), and no second create of it could pass S1. Items 4 to 6 cover the window before activation and the lifetime era. Together they say the only data that can exist under the hostname was written by A's app P.
+
+## 5. The restore region
+
+### 5.1 Instances deleted from this release on
+
+At the delete door, `BeginRetirement` (lifetimes_store.go:208) stores the record body whose ETag it already captures, in a new column `record_retirements.record_body`. N1 tombstones only when the record still has that ETag (lifetimes_retire.go:317), so the stored body is exactly the last record. The restore region is that body's `Region`, and its shape is what §4 item 8 checks.
+
+The column lives in the console database. A console restored to an older snapshot loses rows; restore then finds no body and falls to §5.2, which refuses rather than guesses when it cannot be sure.
+
+### 5.2 Instances deleted earlier, or with no stored body
+
+The restore region is found by looking. Across every region the history examined (the targets `console hosts-used roster` lists), list current objects under `instances/<host>` and `moves/<host>` in each region's data bucket. The restore proceeds only when:
+- exactly one region holds any, and
+- that region equals the `Region` of the host's last allocation body (`host_allocations.body`, the create-time record), and
+- that region holds nothing under `moves/<host>`.
+
+Otherwise it refuses (R8). A moved instance usually fails the first test, because a move keeps the source's replica (offline moves spec, "kept so an operator's return has it"), and that is the point: without a stored body nothing says which copy is newest. If no region holds anything, the restore region is the allocation body's region and the response says `"restored": false`.
+
+### 5.3 Box disks
+
+On wake, a present local file is used before the replica (backend_exec.go restorePlain, "Warm cache"). Delete does not remove the serving box's file. In the restore region the readers are therefore: the region's replica, the last serving box's warm file (the newest copy, written by the same process that replicated), and any other box's file at the derived path.
+
+**For the builder to establish** (plan task 1, before any code): whether a box in the restore region other than the last serving one can hold a file at the host's derived path, and if so whether the restored record could be served there. Moves park source files (movesoffline.go `.offline-moves/<host>/<id>/source`) and landing parks a differing file, so I expect no stale file to remain at the live path. If one can, restore refuses when the region has more than one box that could serve the host, or the plan adds a park of that file before the record is written. Report the finding before choosing.
+
+## 6. Copy
+
+New user-facing strings, to go through copy review before they are written into code or docs. Placeholders in angle brackets.
+
+- R1: "Could not check whether <host> can be restored. Try again."
+- R2: "<host> can't be restored on this platform yet. Ask an operator."
+- R3: "<host> held data before this platform kept a record of who owned it, so it can't be restored."
+- R4: "<host> belonged to an earlier app named <app>, so it can't be restored into this one."
+- R5: "<host> can only be restored in <region>, where its data is."
+- R6: "<host> has a move's copy waiting in <region>, so it can't be restored. Ask an operator."
+- R7: "<host> can't be restored: restore works for plain instances only."
+- R8: "<host> has copies of its data in more than one place, so it isn't clear which is newest. Ask an operator."
+- Ownership refusal (mark, slot or lineage names someone else): the existing S1 text, unchanged. A restore attempt by another owner learns nothing new.
+- C2 (S1 when the proof would pass): "<host> held this app's data before. To bring it back, create it with --restore."
+- docs/site/cli.md: a short "Restoring a deleted instance" section beside the delete text at line 1230.
+
+## 7. Delivery
+
+One plan, first merge visible (AGENTS.md rule 6): the door with §4 and §5.2 lets the live oficina case restore, and §5.1 rides in the same merge. No switch: restore is a request field, on from the release that carries it (rule 5). Who asks: the front door app (#658). The witness: on dev, create a data-kind instance, write a row, delete it, restore it, read the row back; then the refusal cases R3 to R8 and the other-owner S1.
+
+Review: specs and plans per AGENTS.md stopping rules (three Astra rounds at most; stop at the first round with no P1). Census: the change is in `internal/console`, outside the root binary, so no census card is expected; the plan confirms with `make census-card`.
+
+## 8. Left to the builder
+
+- Where in `handleAPIInstanceCreate` and `createRecordWithLifetimeLocked` the restore flag travels, and how the proof's reads are shared with the mark step.
+- The SQLite migration for `record_body` (nullable; older rows stay null).
+- Whether the mark's `Account` and the allocation's `account` use the same identifier (sqid or id), and normalising if not.
+- How §5.2 reuses the roster's region list and the leftover rail's listing (`hostHoldsReplicaData`).
+- Test shape: production-wired witnesses through the real door, plus a table of the refusals.