Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

docs(profiles): add READMEs for profiles and storage modules

Steven Enamakel8bafafc29e58
diff --git a/crates/openhuman-core/src/profiles/README.md b/crates/openhuman-core/src/profiles/README.md
index dd0a811308..9835c6af61 100644
--- a/crates/openhuman-core/src/profiles/README.md
+++ b/crates/openhuman-core/src/profiles/README.md
@@ -54,6 +54,8 @@ Changing the mode may re-map users onto different profiles.
| `types.rs` | `ProfileId` and `ProfileIdMode`, `ProfileMeta`, and the operator-plane result types |
| `layout.rs` | The SaaS side of the shared `ProfileLayout`, archived profiles, and `profile_config`: the forced paths, memory binding (pinned to the legacy layout) and autonomy policy |
| `host.rs` | `ProfileHost`: provisioning, lazy open behind the profile lease, LRU and idle eviction (never of a profile in use), release, fencing, each profile's derived `CoreContext`, `current()`, `ensure_hosted()` |
+| `fence.rs` | The profile's lease fence: the scopes its grant guards (its own scope and every `<profile>~<agent>` session scope), the skew margin, and how the host registers, renews, latches and retires it |
+| `recovery.rs` | Workspace and session-store recovery after an unclean takeover |
| `lease.rs` | The profile lease as the host uses it: `OpenError`, the lease store choice (`DocumentLeases` over a backend, else `LocalLeases`), and the heartbeat that renews every open profile's lease and fences the ones lost |
| `registry.rs` | `ProfileRegistry`: which profiles are provisioned, in the backend's `cluster` scope or as `profile.toml` files |
| `gateway.rs` | Which context a gateway request runs under: the operator plane, or the profile of the user named in `X-OpenHuman-User`, after the signature check |
@@ -243,9 +245,36 @@ The lease drives the rest:
must be unique among live nodes. A stable one lets a restarted node take
its own profiles back at once instead of waiting out their leases.
-Known limits: storage writes carry no epoch fence, so a node that is
-partitioned but still running could write after its lease expired; the TTL
-being much longer than the renew interval, the fence check before each turn
-and stopping turns on loss are the mitigations. Each node keeps its own
+- **Write fencing.** Opening a profile registers a lease fence
+ (`storage::fence`, built by `profiles::fence`) for its grant. Every write
+ through the storage ports to the profile's scope or one of its session
+ scopes is refused with a typed `FenceError` when the fence is latched, when
+ the grant has run out by this node's clock less a skew margin (a sixth of
+ the TTL), or when the stored lease record no longer names this node at
+ this epoch (which latches it). A takeover therefore stops the old holder's
+ next write, before its heartbeat notices. `ensure_hosted` refuses new
+ turns on the latch or a locally expired grant, and the background loop
+ checks the fence against the record before a profile's jobs and drops them
+ if it latches mid-run. A latched fence keeps refusing for as long as the
+ fenced profile's context is alive; a clean release retires it.
+
+Known limits:
+
+- The fence check and the write are two operations: a holder paused for
+ longer than the skew margin right after a passing check can still land one
+ write after a takeover. Closing that needs a driver-enforced conditional
+ write (see `storage/README.md`).
+- Only writes through the storage ports are fenced. Files a profile writes
+ directly under `<root>/users/<id>/` (the memory job queue, the classic
+ on-disk stores) are not; on a shared volume, stopping turns and jobs on
+ fence is the mitigation.
+- A node that re-opens a profile after being fenced registers a new fence;
+ work left over from its older grant is stopped by turn cancellation, not
+ by the fence.
+- With file leases (no backend) nothing goes through the ports, and the
+ `flock` cannot be lost while the process lives, so the fence only backs
+ the latch checks.
+
+Each node keeps its own
operator keyring (`operator_dir`), so a credential installed through one node
is only readable there unless secrets live in the storage backend.
diff --git a/crates/openhuman-core/src/storage/README.md b/crates/openhuman-core/src/storage/README.md
index 2a562e2f13..a0e29700fd 100644
--- a/crates/openhuman-core/src/storage/README.md
+++ b/crates/openhuman-core/src/storage/README.md
@@ -36,6 +36,8 @@ on `storage-mongodb`.
that driver is atomic across processes (MongoDB, SQLite), which a
clustered node's leases need. Memory and file drivers coordinate only
within one process.
+- `fence` / `fenced_backend`: lease-epoch write fencing (see
+ [Write fencing](#write-fencing)).
- `scope_for_profile(profile_id)`: the scope a SaaS profile's records live
under (`profile:<id>`, hashed when that is not a valid scope).
- `current_scope()` / `current_scoped()`: the tenant's scope — its profile's
@@ -92,6 +94,45 @@ eight-thread race on a SQLite file with `--features storage-sqlite`),
release, clock and crash steps across 2 to 4 nodes checked against a
reference model.
+## Write fencing
+
+`storage::fence` and `storage::fenced_backend` fence writes by lease epoch.
+Every backend `open` returns is a `FencedBackend`: its scoped handles look
+the scope up in the process's `FenceRegistry` on each write (document `put`,
+`delete`, `delete_where`, `claim`, `atomic_batch`, `drop_collection`; stream
+`append`, `append_batch`, `truncate_before`, `delete_stream`; blob `put`,
+`delete`). A scope with no fence passes through, so single-user hosts see no
+change. A scope with one runs `LeaseFence::check` first:
+
+1. the latch (set by the heartbeat on a lost lease, or by an earlier check);
+2. the grant's expiry by this node's clock, less a skew margin (no I/O);
+3. the stored lease record must still name this node at this epoch,
+ unreleased and unexpired; a moved record latches the fence, an
+ unreadable one refuses the write without latching.
+
+A refusal is a `StorageError` of kind `Backend` (not retryable, so CAS loops
+do not spin) whose source is the typed `FenceError`; `fence::fence_error`
+recovers it. The lookup is per write rather than per bound scope because
+the session store caches its handles across a profile's open, fence and
+re-open.
+
+What it cannot do on the current ports: make the write itself conditional
+on the lease. `atomic_batch` is optional (`Capability::Transactions`, only
+MongoDB replica sets), it has no read-only guard op, and leases live in the
+`cluster` scope while the guarded data lives in the profile's; streams and
+blobs take no precondition at all. So a holder that passes the check and is
+then paused past the margin can still land one write. Closing that is
+upstream work in tinystoragedrivers: a fencing token on the ports (for
+example a `Precondition::Fence { scope_doc, epoch }` or a `WriteOp::Check`
+guard op, enforced by each driver in the same statement or transaction as
+the write, with an `ErrorKind::Fenced`).
+
+Tests: `fence_tests.rs` (matching, local expiry, latch, record checks,
+registry replace/retire/prune), `fenced_backend_tests.rs` (node A loses the
+lease to node B by a clock advance: A's writes on every port are refused,
+B's succeed; a paused node refuses its own writes inside the margin; named
+databases; a fence over `LocalLeases`).
+
## Consumers
- The SaaS profile host (`profiles::lease`, `profiles::registry`): one