Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ Non-trivial logic must include one runnable check (unit test or minimal self-che
fails, nothing is marked purged. Multi-mode blob GC is `storage.RunObjectGC`: candidates and the
re-check are per `(org_id, digest)`, under `storage.BlobLockName`, rows commit before the object
delete. Verify `TestObjectGC*` and `TestPurgeDeletesOnlyTheDeletedOrgsPrefix` on PostgreSQL.
Late-PUT fence: the purge's first run records `purge_fence:<org>` (database time) with the
started audit; it deletes the prefix every run but marks purged only on a run at least
`blobstore.LatePutWindow` later. `SweepOrphanObjects` (inside `RunObjectGC`) deletes unclaimed
objects over an hour old by provider time; each deletion re-reads the claim and that the org
serves, under its session lock. The purge re-reads `deleted` before the prefix and at the mark. Verify
`TestLatePutAfterPurge*` and `TestOrphanSweep*`.
- Member erasure uses `CleanupFor(db).AdvanceSeq` and `TombstoneComment`, one fresh sequence per
retained comment so paginated sync delivers every deletion. Directory erasure runs only in multi
mode; single mode refuses `user.erased`. Verify `TestMultiModeDirectoryKeepsAccountsInTheirOrg`.
Expand Down
6 changes: 4 additions & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,8 +1028,10 @@ of replica fields and persisted settings. This entry point remains separate from
and all startup paths; tests preserve the multi refusal and standalone golden.

N3 session-lock ownership and context cancellation do not establish the fate of a previously
issued remote PUT after lock loss. Complete GC/purge/runtime integration requires a selected
and qualified delayed-writer erasure fence before deletion/readiness claims. The shared
issued remote PUT after lock loss. The selected fence is a time window: PUTs are bounded from
entry, a purge completes only after a prefix delete `LatePutWindow` past its fence, and an
age-gated orphan sweep removes late PUTs after GC or erasure. Provider qualification of the
window remains before readiness claims. The shared
conditional API does not choose a product ledger/protocol. Store controls/age and selected
provider proof, native-PG recovery transport and final assembly remain launch prerequisites.

Expand Down
14 changes: 7 additions & 7 deletions IMPLEMENTATION_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2319,15 +2319,15 @@ regular credential files may end with one LF/CRLF; bounded reads, source/type fa
client validation errors disclose setting names only. Construction makes no network request,
local key/store or SQL write. No normal config loader or startup path invokes it.

The base shared client is pinned to merged #77 (`79823f9`); #78's exclusive-create,
ETag and age additions are merged as `b55ca6d` but are not consumed by this pin. Complete Backend/Temporary,
The shared client is pinned to the #78 merge (`b55ca6d`): exclusive create, ETags and
provider age are consumed; the orphan sweep reads the age. Complete Backend/Temporary,
store-ID/probes, request/GC/purge integration and provider evidence remain unbuilt.

Before binding GC/purge/runtime, qualify the fate of an already-issued remote PUT after the
writer loses its PG session lock or context. Lock loss/cancellation alone cannot prove that
remote write finished before erasure. The selected Notes resolution and delayed-write test
must be recorded here before erasure-readiness claims; consuming wire-only conditional APIs
does not select Messages' ledger/protocol. Preserve access fences while this is unresolved.
Selected late-PUT resolution (spec "Late-PUT erasure fence as built"): a time fence, not a
ledger. Every object PUT is bounded from entry; the purge marks an org purged only after a
prefix delete at least `LatePutWindow` after its fence; an age-gated orphan sweep removes a
late PUT after GC or erasure. The window holds only if the provider honors SigV4 request age
and commits no PUT after it; that is provider qualification, still open.
Native encrypted PG base-backup/WAL tool/transport, destination/retention and restore/drill
remain spec open question 3. Startup remains refused; existing single-mode behavior stays.

Expand Down
8 changes: 5 additions & 3 deletions docs/ASSURANCE-CASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ client gate clearance, exclusive controls, provider age and native recovery rema
Primary-object client preparation has a separate environment/_FILE credential boundary.
`TestReplicaObject*` covers bounded values/files, source ambiguity, nonregular/missing paths,
multiline/empty refusal and secret-safe errors/formatting. Construction writes no shared
settings or local store and does not enable startup. Actual delayed PUT completion after a
lost PG lock remains unqualified; neither a canceled context nor the published wire API is
erasure proof. Runtime/GC/purge/provider and native-PG recovery gates remain explicit.
settings or local store and does not enable startup. A late PUT after a lost PG lock is fenced
by time: `TestLatePutAfterPurgeIsErasedBeforeTheOrgIsMarkedPurged` lands a timed-out PUT after
the prefix delete and fails if the purge completes inside the window;
`TestOrphanSweepDeletesOnlyOldUnclaimedObjects` covers GC/erasure orphans. The provider's
honoring of the window is unqualified. Runtime/GC/purge/provider and native-PG recovery gates remain explicit.

Host maintenance uses the exact existing PG schema and recorded staff org/region, with no
migration or local-key fallback. Tests require atomic account/audit creation, real password
Expand Down
41 changes: 40 additions & 1 deletion docs/superpowers/specs/2026-11-23-tenancy-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ network or database I/O and is not called by ordinary startup or maintenance loa
Tests cover direct/file/mixed sources, ambiguity/empty/size/multiline/path/type refusal,
secret-safe errors/formatting, shared-field separation and unchanged startup refusals.

Base #77 is merged and pinned to79823f9; #78 is merged asb55ca6d but is not consumed. Before
Base #77 was pinned at 79823f9; the late-PUT fence moved the pin to the #78 merge b55ca6d. Before
complete GC/purge/runtime binding, Notes must select and qualify delayed remote-PUT fate
after PG-lock loss; cancellation alone is no completion proof. Shared conditional operations
do not select a Notes ledger/protocol. Keep this and native-PG recovery open question3 as
Expand All @@ -1619,6 +1619,45 @@ the prefix step. The schedule adapter carries the purge step; startup binding, t
fence and the orphan sweep are separate.


### Late-PUT erasure fence as built (startup still refused)

The problem: a writer admitted before an org's deletion (or holding a blob lock it then
loses) can have a PUT the provider stores after the purge deleted the prefix, or after GC
deleted the blob. Lock loss and cancellation prove nothing about that remote write.

The selected resolution is a time fence, with no new ledger:

- `Objects.Put`/`PutPart` bound the whole call by a 5-minute deadline from entry. Callers
admit the org under the session lock just before the call, with bytes already received.
- `LatePutWindow` is 20 minutes: the deadline plus SigV4's 15-minute request-age limit, which
a provider applies when it receives the request.
- The purge's first run inserts `purge_fence:<org>` in database time with the
`platform.org_purge_started` audit, in one transaction (the audit is now written once). Every
run deletes the rows and the prefix. Only a run that read the fence at least the window old
before its prefix delete marks the tombstone purged and removes the fence. The status is
re-read before the prefix delete, and the mark requires `status='deleted'` in its own
transaction.
- `SweepOrphanObjects` runs at the end of `RunObjectGC`. For each serving org it lists the
prefix with provider times and deletes any blob with no row and any upload with no pending
session, once over an hour old. Each deletion is admitted under the blob or upload session
lock, in a short transaction that re-reads both the claim and whether the org still serves.
An org stopped during the listing keeps its objects. Zero (unknown) age
never deletes; at most 100 a pass.
- The shared client pin moved to the #78 merge (`b55ca6d`) for `LastModified`.

Tests: a PUT blocked at the provider times out at the client; the purge runs; the provider
then stores the PUT. The purge must not be marked inside the window, and the run after it must
leave no key. Removing the window check fails it; removing the deadline fails its 10-second guard.
The orphan sweep keeps young, unknown-age, claimed, pending-upload and stopped-org objects.
Mutations of the age check, the claim re-check and the pending-upload check each fail it.
`TestOrphanSweepKeepsObjectsOfAnOrgStoppedDuringTheListing` suspends the org from inside the
provider listing; it fails without the in-lock serving re-check.
`TestPurgeRereadsTheStatusBeforeThePrefixAndTheMark` fails without either status re-read.

Unproven: that a selected provider honors request age and commits no PUT past the window
(provider qualification), and clock skew between provider and database beyond a few minutes.


### Phase 6 host maintenance as built (startup still refused)

Multi-mode `user add --admin` uses bounded stdin derived credentials and an exact existing PG
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.26.9
toolchain go1.27.4

require (
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124001445-79823f9af6b7
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124003741-b55ca6d3313c
github.com/Busnes-app/ky-primitives/offsite v0.3.0
github.com/cloudflare/circl v1.6.5
github.com/jackc/pgx/v5 v5.13.0
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123013446-90b0e58aa724 h1:LGkxdH5emHN9FviYtr5QgYB5qqc3EiF/YzpAmCwxcYU=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123013446-90b0e58aa724/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123144426-9185e49e0370 h1:iA96g0TPifRPXjZygpDBghFjLOx1AoSxejhjst0Ft6s=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123144426-9185e49e0370/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123230226-12e1963e8dc8 h1:blG5fPxQFJ4k/K/PCaC5abe1hF/cWYjUJ0RsmGELCKw=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123230226-12e1963e8dc8/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123231137-3242eb82f4b9 h1:F1P9sjVAF4rVl2lbVOaMmF2Z8BI0zVci+96Frn7HdYg=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261123231137-3242eb82f4b9/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124001445-79823f9af6b7 h1:fko4xQHrAHJxDR7Ni1tBqj4PAOZVataOpyi+uIC4O6g=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124001445-79823f9af6b7/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124003741-b55ca6d3313c h1:uVDDt5rGQoNUSRvVeTGGBKl/S8C2ygmF65n0d/xf+jo=
github.com/Busnes-app/ky-primitives v0.10.2-0.20261124003741-b55ca6d3313c/go.mod h1:xYUuDP23Ey2tnBVVIbrOz0qjGsZ0bBBpSraG0qWm1O8=
github.com/Busnes-app/ky-primitives/offsite v0.3.0 h1:E0ycngO0BHAooTDRvDagmrJlkZlTeRqBNX5pLwQMlg0=
github.com/Busnes-app/ky-primitives/offsite v0.3.0/go.mod h1:UYfgJk5VJ/Z4Dff5wvSPeOn4dwS1qLGDeNPTTkEguKI=
github.com/aws/aws-sdk-go-v2 v1.48.0 h1:tFU7EJVm7MfyaXLsXdYTZ0Vd6vwt9OljCyqq0gqk0Mc=
Expand Down
12 changes: 9 additions & 3 deletions internal/blobstore/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ Own opaque blob streams, single-volume compatibility and the tenancy blob seam.
content cache or storage fallback. Identical writes reuse verified bytes; parts remain live
until explicit post-commit/cancel/expiry cleanup. Tail removal validates every index first.
- Delete/Purge are explicit unreferenced-key/org erasure, including physical versions/markers.
No scheduled pruning or orphan deletion is enabled. Unknown-object cleanup needs provider age
proof; store-ID creation still requires the shared client's create-exclusive operation.
No scheduled version pruning. `Stored` feeds storage's orphan sweep, which deletes only with
provider age proof (zero `LastModified` never deletes); store-ID creation still requires the
shared client's create-exclusive operation.
- Late-PUT fence: `Put`/`PutPart` bound the whole call by `putDeadline` from entry. Callers
pass already-received bytes and admit the org under the session lock just before the call.
`LatePutWindow` (deadline plus SigV4's 15-minute request age) is how long the purge waits
after its fence; widening the deadline means widening the window.
- New shared-client dependencies must point to its reviewed upstream implementation, with no
copied signing/transport or invented client contract. Provider qualification is separate from
the local TLS S3 wire fixture.
Expand All @@ -53,7 +58,8 @@ Own opaque blob streams, single-volume compatibility and the tenancy blob seam.

- `go test -race -count=1 ./internal/blobstore`
- `TestObjectGC*`/`TestPurgeDeletesOnlyTheDeletedOrgsPrefix` drive storage's GC and purge through
the real shared client on PostgreSQL.
the real shared client on PostgreSQL. `TestLatePutAfterPurgeIsErasedBeforeTheOrgIsMarkedPurged`
and `TestOrphanSweepDeletesOnlyOldUnclaimedObjects` prove the late-PUT fence.
- `TestObjects*`/`TestObjectParts*` use the actual shared client and a local verified TLS fixture.
- PostgreSQL CI runs `TestReplicaStoreIdentity*` against isolated schemas.
- The full HTTPAPI suite and `TestSingleTenantUpgradeGolden` pin existing behavior.
Expand Down
10 changes: 10 additions & 0 deletions internal/blobstore/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package blobstore

import "time"

// SetPutDeadline shortens the put deadline for one test.
func SetPutDeadline(d time.Duration) (restore func()) {
old := putDeadline
putDeadline = d
return func() { putDeadline = old }
}
2 changes: 2 additions & 0 deletions internal/blobstore/object_parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
// upload session lock and owns DB counter admission. An identical retry reuses bytes; a
// rolled-back counter explicitly drops its uncommitted tail before accepting another body.
func (s *Objects) PutPart(ctx context.Context, id string, index int64, body io.Reader, size int64) error {
ctx, cancel := context.WithTimeout(ctx, putDeadline)
defer cancel()
key, err := UploadPartKey(s.org, id, index)
if err != nil {
return err
Expand Down
Loading