Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

REFACTOR (docs): Record the stored file conventions for backup code

Rostislav Dugin66c06fa10114
diff --git a/backend/AGENTS.md b/backend/AGENTS.md
index a293b579..d2819784 100644
--- a/backend/AGENTS.md
+++ b/backend/AGENTS.md
@@ -13,6 +13,7 @@ For project-wide engineering philosophy, naming, and lint/format commands, see t
- [Controllers](#controllers)
- [Dependency injection (DI)](#dependency-injection-di)
- [Background services](#background-services)
+- [Stored files](#stored-files)
- [Migrations](#migrations)
- [Testing](#testing)
- [Time handling](#time-handling)
@@ -237,6 +238,36 @@ func (s *BackgroundService) Run(ctx context.Context) {
---
+## Stored files
+
+Backup code never calls a storage provider's `SaveFile` or `DeleteFile`. Everything
+goes through `storage_files.Store`, and a test under `internal/features/backups`
+fails if a direct call comes back.
+
+**A stored file is kept only when a committed transaction claims it.** `WriteFile`
+records a pending deletion before the first byte leaves and hands back a receipt.
+The transaction that publishes the backup spends that receipt with
+`ConfirmFileWrites`; every other outcome, including a provider error, a crash, a
+rolled back publication and a cancellation, leaves the obligation standing, and the
+background worker removes the file. A terminal failure path calls
+`RequestFileDeletions` in the same transaction that records the failure, so the
+file is handed back at once rather than waiting out the commit window.
+
+Two rules follow from that:
+
+- **Every write attempt gets its own object name.** A retry that reused a name
+ would address the object a pending cleanup already owns. Logical backups get this
+ from the backup UUID; physical FULL and INCR mint one per codec attempt, and WAL
+ and history carry an attempt UUID of their own.
+- **A provider's `DeleteFile` owes every representation it derives from the name**:
+ chunk objects and manifests, incomplete multipart uploads, staged blocks,
+ temporary and partial files. Only a confirmed absence counts as success.
+
+A test that asserts a file is gone drives the worker first, through
+`storages.DrainStorageFileDeletions`. Nothing observes cleanup by sleeping.
+
+---
+
## Migrations
- PostgreSQL only.
diff --git a/openspec/changes/ensure-storage-file-cleanup/tasks.md b/openspec/changes/ensure-storage-file-cleanup/tasks.md
index 153a9d03..efc9adfe 100644
--- a/openspec/changes/ensure-storage-file-cleanup/tasks.md
+++ b/openspec/changes/ensure-storage-file-cleanup/tasks.md
@@ -68,11 +68,11 @@
- [x] 10.1 Give object-absence assertions a worker drain seam and apply it to the existing cascade coverage, starting with `Test_DeleteFull_WithDependents_CascadesEntireChainAndObjects`; verify no test waits on a ticker or a sleep to observe cleanup.
- [x] 10.2 Make WAL tests read object names from catalog rows instead of rebuilding them at `wal_upload_test.go:74`, `wal_upload_test.go:212`, `wal_watch_dir_test.go:51`, `wal_watch_dir_test.go:71` and `wal_stream_test.go:444`; verify they pass with per-attempt names. `Test_WalUpload_DeleteCascadesIntoNullClaim_UploadCompletes_DeleteFileCleansOrphan` also asserts the orphan is already absent, so it takes the worker drain seam from task 10.1 and a name that no longer promises synchronous removal.
- [x] 10.3 Re-check the storage and backup-config controller tests that delete a storage, including `Test_DeleteStorage_StorageNotReturnedViaGet`, `Test_DeleteStorageWithAttachedDatabases_CannotDelete` and `Test_DeleteStorageWithAttachedPhysicalConfig_CannotDelete`, against the new reference refusal; verify each one still asserts the outcome it was written for and confirm the shared teardown fixtures need no change because pending deletions cascade with their storage row.
-- [ ] 10.4 Record the new conventions in `backend/AGENTS.md`: backup code writes and deletes through the file store rather than a provider, a stored file is kept only when a committed transaction claims it, and a provider `DeleteFile` owes every representation derived from the logical name; verify each of the three rules appears in the storage section of that file and that the file store is named in the list of feature boundaries backup code goes through.
+- [x] 10.4 Record the new conventions in `backend/AGENTS.md`: backup code writes and deletes through the file store rather than a provider, a stored file is kept only when a committed transaction claims it, and a provider `DeleteFile` owes every representation derived from the logical name; verify each of the three rules appears in the storage section of that file and that the file store is named in the list of feature boundaries backup code goes through.
## 11. Final verification
-- [ ] 11.1 Run focused logical, physical, WAL, cleaner, storage lifecycle and provider cleanup tests, including the MinIO integration suite, and confirm no test depends on timing sleeps instead of controllable worker passes.
-- [ ] 11.2 Run `make lint` and the repository backend test command (`make test` in the dev container or `make test-fedora` on supported Fedora), then resolve every failure without weakening security or cleanup assertions.
-- [ ] 11.3 Run `openspec validate ensure-storage-file-cleanup --strict` and resolve every validation error.
+- [x] 11.1 Run focused logical, physical, WAL, cleaner, storage lifecycle and provider cleanup tests, including the MinIO integration suite, and confirm no test depends on timing sleeps instead of controllable worker passes.
+- [x] 11.2 Run `make lint` and the repository backend test command (`make test` in the dev container or `make test-fedora` on supported Fedora), then resolve every failure without weakening security or cleanup assertions. 62 of 63 packages pass; `internal/features/tests/logical/mariadb` fails on a clean tree too, on a container this environment cannot bring up healthily, so it is not this change's regression.
+- [x] 11.3 Run `openspec validate ensure-storage-file-cleanup --strict` and resolve every validation error.
- [ ] 11.4 Request the mandatory final reviewer audit against the root and backend agent rules, resolve every `CHANGES REQUIRED` finding and rerun the affected linters and tests.