Skip to content

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

Merge remote-tracking branch 'origin/saas-lease-fencing' into storage-driver-fencing

Steven Enamakel849e61ac5ed4
diff --cc crates/openhuman-core/src/profiles/README.md
index 93ff6b3903,f55cfd4fb0..7ad9c9b6d0
--- a/crates/openhuman-core/src/profiles/README.md
+++ b/crates/openhuman-core/src/profiles/README.md
@@@ -189,84 -300,56 +300,59 @@@ instead ride the operator's `OPENHUMAN_
## Background work
- A single-user core drains memory jobs from a cron system job behind the process-wide scheduler gate. A SaaS core cannot use either: the cron service is off, and the gate reflects the operator, who holds no credential. So `background::spawn` (started by `saas::build`) runs one loop per process instead. Each tick:
-
- - sweeps idle profiles;
- - visits every provisioned profile whose workspace has queued memory jobs (`memory::lifecycle::jobs::has_pending`);
- - opens that profile and runs its due jobs under the profile's own context, with its config, credential and memory root.
-
- Profiles with nothing queued are not opened.
-
- User cron jobs are not served yet: the cron RPCs are not on the user surface.
+ A single-user core drains memory jobs from a cron system job behind the
+ process-wide scheduler gate. A SaaS core cannot use either: the cron service
+ is off, and the gate reflects the operator, who holds no credential. So
+ `background::spawn` (started by `saas::build`) runs one loop per process. Each
+ tick it sweeps idle profiles, then opens every provisioned profile whose
+ workspace has queued memory jobs (`memory::lifecycle::jobs::has_pending`) and
+ runs its due jobs under that profile's own context. Profiles with nothing
+ queued are not opened. User cron jobs are not served yet: the cron RPCs are
+ not on the user surface.
- ## Leases: one core per profile
-
- Exactly one process hosts a profile at a time. `ProfileHost::open` takes the
- profile's lease (`storage::lease`) before opening it:
-
- - **With a storage backend** the host uses `DocumentLeases` in the backend's
- `cluster` scope: one compare-and-swapped record per profile with an owner,
- an endpoint (`advertise_url`), an epoch and an expiry `lease_ttl_secs`
- (default 30) out. Every node sharing the backend contends for the same
- records. A clustered node (one that sets `advertise_url`) must use a driver
- whose CAS holds across processes (`storage::driver_has_cross_process_cas`:
- SQLite, MongoDB); the boot guard refuses anything else.
- - **Without one** it uses `LocalLeases`: an exclusive `flock` on
- `<root>/users/<id>/.lease`, which keeps two processes on one root apart and
- drops when its holder dies.
+ The prompt says `Host: hosted` instead of the server's hostname, and the
+ `## User` identity block stays empty, because no process-wide identity is ever
+ set.
- The lease drives the rest:
+ ## Files
- - **Refusal.** A live lease held elsewhere makes `open` return
- `OpenError::HeldElsewhere(record)`; the gateway answers `409` with
- `{"error":"profile_held","owner","endpoint","retry_after_ms"}` and
- `X-OpenHuman-Profile-Owner: <owner>`. Load balancers route users stickily;
- the lease is what makes it correct.
- - **Recovery.** A lease taken over from a holder that never released it
- (`previous_unclean`: it crashed, or stopped renewing) means that holder's
- turns may have died mid-flight. `recover_workspace` marks them interrupted
- and settles orphaned run-ledger rows, and a storage-backed session store
- is swept for the profile too. A clean hand-over (eviction, release,
- shutdown) recovers nothing, so a re-open never interrupts live work. This
- replaces the old once-per-process sweep, and is why the SaaS session
- store never recovers on open (`session_store::install_for_saas`).
- - **Heartbeat.** `lease::heartbeat` renews every open profile's lease every
- third of the TTL. A renewal that finds the record changed (another node
- took over, or an operator released it), or that keeps failing until the
- grant would have expired, **fences** the profile: it is marked fenced, its
- in-flight turns are stopped (`web_chat::cancel_all_turns` under its
- context) and it is closed. `ensure_hosted` refuses new turns for a
- profile this node no longer hosts.
- - **Release.** Idle eviction, `profiles.release {profile_id}` (refused while
- the profile is in use), deprovisioning and a clean shutdown (idle profiles
- only; a busy one keeps its lease so its successor recovers) release the
- lease, so another node can take the profile at once.
- - **Node ids** (`node_id`, else `OPENHUMAN_NODE_ID`, else random per process)
- 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.
+ | File | Purpose |
+ | --- | --- |
+ | `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 lease, LRU and idle eviction (never of a profile in use), release, fencing, each profile's derived `CoreContext` and policy, `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; the typed refusal (`409` holder) |
+ | `surface.rs` | What a user may call: `USER_METHODS`, applied at dispatch, in the controller list and in `/schema`; user thread-id rules |
+ | `background.rs` | The SaaS background loop: idle sweeps and each profile's queued memory jobs under its context |
+ | `tools.rs` | Which agent tools a user gets: the host tool groups, the hard-deny list, the tool-list filter, the approval gate's SaaS verdict, and the container policy for a user's shell |
+ | `credentials.rs` | A profile's TinyHumans credential, stored beside its config or in the backend |
+ | `ops.rs` | `provision` / `deprovision` / `list` / `status` / `set_credential` / `clear_credential` / `release`, returning `Outcome<T>` |
+ | `schemas.rs` | The `profiles.*` controllers (`profile_id` in and out) |
- - **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.
+ Each file's tests sit beside it (`*_tests.rs`), including property suites for
+ ids (`types_proptest_tests.rs`) and the tenant key
+ (`core/runtime/tenant_proptest_tests.rs`). End to end:
+ `tests/saas_mode_e2e.rs` (the gateway, two users on one thread id, relayed
+ channel threads, `409`) and `crates/openhuman-embed/tests/saas_profiles.rs`
+ (the in-process `ProfileRuntime`).
- Known limits:
+ ## Known limits
+ - Thread JSONL, the local memory engine, the cost and run ledgers, background
+ completions and the sandbox stay as files under `users/<id>/workspace`, so a
+ multi-node deployment needs `<root>` on a shared read-write-many volume and
+ a remote memory engine. Moving those onto the storage ports is a follow-up.
-- 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`).
+- On drivers with `Capability::Fencing` (memory, SQLite, file, MongoDB
+ replica sets) the driver re-checks the lease record atomically with each
+ write, so a takeover cuts off a paused holder's writes at storage. Where it
+ cannot (MongoDB without transactions, MongoDB blobs, named databases) the
+ host 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 (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