Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

docs: explain why an unreceipted committed import is safe to retry (#8871)

[._.]/ Adam Eivye5935c3bc28f
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index fa90898a7f..7729a52e97 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -135,7 +135,7 @@ A cancelled unowned accepted operation moves to `data/database-maintenance-cance
The fixed worker (`scripts/database-maintenance-worker.mjs` → `runDatabaseTransfer(id, token)` in `server/services/databaseMaintenanceTransfer.js`) is the only code that transfers data. In order it: claims its one-use entry and records its process group; stops PM2 CoS then server (`stopOwnedDatabaseProducers`, which moves `accepted` → `quiescing` and, on recovery, re-reads the ORIGINAL identities); proves no earlier coordinator of this operation still has a live process group (`assertPredecessorCoordinatorsStopped`); reconciles admitted spawns and detached writers (`reconcileDetachedWriters`); and only then transfers:
- **`quiescing` → `exporting`**: `scripts/db.sh export --endpoint` with the journal's recorded source host, port, user and database. The worker builds the child environment from an allowlist (the password is the only libpq variable passed), and `db.sh` strips every other inherited `PG*` variable again, so `PGHOST`, `PGHOSTADDR`, `PGSERVICE`, `PGOPTIONS` and the like cannot redirect it. The dump is `data/db-dumps/portos-maintenance-<operation-id>.sql`. A failed `pg_dump`, missing completion trailer, or unexpected output path is never recorded. A complete dump and its directory are fsynced, then `transfer-dump.json` publishes its operation, source identity, size and SHA-256, immutably, before `exporting` → `importing`.
-- **`importing`**: the recorded dump must still match its size and digest (a changed or missing dump refuses). `db.sh import --endpoint` loads it into the recorded target in one `psql --single-transaction` with `ON_ERROR_STOP`; a failed import commits nothing. Success publishes `transfer-import.json`, binding that digest to the target.
+- **`importing`**: the recorded dump must still match its size and digest (a changed or missing dump refuses). `db.sh import --endpoint` loads it into the recorded target in one `psql --single-transaction` with `ON_ERROR_STOP`; a failed import commits nothing. Success publishes `transfer-import.json`, binding that digest to the target. A crash between the commit and that receipt makes the next attempt import the same `--clean` dump again, which reproduces the same state because nothing writes the target before verified restart.
The worker then exits `78` with `{ stage: "importing", importCommitted: true, sourceRetained: true, restartVerified: false }`: saved mode is unchanged, producers stay stopped, the source database and dump are retained, and admission stays fenced until verified target restart (#8851) exists. Ordinary pooled writes and detached spawns refuse throughout. A refusal exits `1` with a bounded reason (counts and stage only; no hosts, paths, tokens or PIDs); `db.sh` output stays internal.
diff --git a/server/services/databaseMaintenanceTransfer.js b/server/services/databaseMaintenanceTransfer.js
index 46134514d6..796c1176c2 100644
--- a/server/services/databaseMaintenanceTransfer.js
+++ b/server/services/databaseMaintenanceTransfer.js
@@ -139,6 +139,10 @@ export async function runDatabaseTransfer(id, token, options = {}) {
const dump = journal.readTransferDump(id);
if (!dump) throw refused('no complete recovery dump is recorded');
let imported = false;
+ // A crash after the target commits but before the receipt publishes makes the
+ // next attempt import the same --clean dump again. That retry is idempotent:
+ // nothing can write the target before verified restart (#8851), which needs
+ // this receipt, so it reproduces the identical committed state.
if (!journal.readTransferImport(id)) {
const path = await assertRecordedDump(journal, id, dump);
journal.assertCoordinatorWorker(id, token);