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
17 changes: 17 additions & 0 deletions changelog.d/provider-addon-hold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Reversible add-on hold

- A signed local add-on, such as the Darkbloom Sandbox, can hold the provider
through the owner-only socket `~/.darkbloom/runtime/addon.sock` while its VMs
run. The provider stays connected, reports `status: draining` with
`busy_reason`, `busy_since_ms` and `busy_addon`, lets accepted work finish
(up to 120 seconds), then unloads every model. Release returns it to routing
on the same connection.
- Holds have 30–600 second leases. An expired lease ends the hold only when the
add-on no longer holds `ownership.lock`, and a restarted or updated provider
comes back held without loading models.
- Updates and Autopilot operations wait for the hold, and reconnects keep it;
Autopilot reports `addon_hold`. `darkbloom addon status|hold|release`
inspects or overrides holds for debugging, and `darkbloom status` shows an
active hold.
- Coordinator handling of the busy fields ships separately; older coordinators
still stop routing on the draining status.
3 changes: 2 additions & 1 deletion docs/architecture/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Provider And Native Architecture

> Last updated: 2026-10-13
> Last updated: 2026-10-27

Provider execution, local security and external contracts. Backend implementation lives in [platform architecture](https://github.com/Layr-Labs/darkbloom-platform/tree/48a198c71a2d30feec5597bacf1101120f7f955d/docs/architecture).

- [MLX stack: the three pinned submodules and the metallib](components/mlx-swift.md)
- [Add-on hold: leaving routing while a local add-on uses the Mac](addon-hold.md)
- [Provider process](components/provider.md)
- [Retained console snapshot, proxy routes and hosting](components/console-ui.md)
- [Landing public HTML, Markdown, pricing and API discovery](components/landing.md)
Expand Down
116 changes: 116 additions & 0 deletions docs/architecture/addon-hold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Add-on hold

> Last updated: 2026-10-27

How a local add-on, such as the Darkbloom Sandbox VM host, takes a Mac away
from inference without disconnecting the provider, and how the provider
returns. This explanation is for provider and add-on developers; the exact
socket contract is in the [add-on hold IPC reference](../reference/addon-hold-ipc.md).

## Context

An add-on that runs VMs needs the Mac's unified memory. Disconnecting the
provider would cost a fresh registration, authorization and warm-up on return.
A hold instead keeps the coordinator connection and the App Attest worker,
leaves routing through the existing draining status, unloads every model, and
reopens admission on the same connection when released. Leases and the
add-on's machine lock make a crashed add-on recoverable without loading models
beside VMs that still run.

## Mechanism

```mermaid
stateDiagram-v2
[*] --> Serving
Serving --> Draining: hold (IPC or darkbloom addon hold)
Draining --> Held: accepted work finished (120 s bound), every model unloaded
Draining --> Serving: release
Held --> Serving: release, or lease expired and machine lock free
Held --> Held: renew, or lease expired while the machine lock is held
Draining --> Stopping: stop or signal
Held --> Stopping: stop or signal
Stopping --> Held: next start restores the record
```

1. A hold request closes admission with drain owner `addonHold`
(`ProviderDrain.begin` in `provider-swift/Sources/ProviderCore/Service/ProviderDrain.swift`),
publishes `ProviderBusy` and sends an event heartbeat with
`status: draining` and the [busy fields](../reference/protocol-messages.md#add-on-hold-busy-fields).
2. Accepted inference keeps running up to `addonHoldDrainTimeoutDefault =
.seconds(120)`; what remains is then cancelled. Load transitions get
`addonHoldSettleTimeout = .seconds(60)` before every model unloads through
`unloadModel`, retained KV segments and the MLX buffer cache are released,
and the hold becomes `held`.
3. Release ends the last hold, reopens admission, rebuilds capacity from a
fresh memory sample, sends an `idle` or `serving` heartbeat and replays a
`desired_models` snapshot deferred during the hold. Models then load on
demand.

## Invariants

1. Update, reconnect and model-switch drains cannot replace or reopen a hold;
only a stop preempts it, and a hold never preempts a switch or stop
(`ProviderDrain.begin`, `ProviderDrain.resumeAddonHold`).
2. The hold never sends `provider_drain`. A background update or App Attest
stall restart whose drain the hold took over stops before its barrier
(`waitForSafeDisconnect` in `provider-swift/Sources/ProviderCore/ProviderLoop+PlannedDisconnect.swift`).
3. A planned reconnect proceeds beneath a hold and keeps it: the fresh
`register` carries the busy fields from `ProviderState.busy`, and
`finishPlannedReconnect` re-announces the draining status. A reconnect still
pending at release keeps admission closed until its registration
(`resumeAfterAddonHold`).
4. A hold does not stop the App Attest worker and does not block its recovery
after an update (`ProviderDrain.refusingForServiceChange`).
5. Auto-update, Autopilot operations, model revisions, MTP upgrades, coordinator
`load_model`, `prefetch_model` and the startup preload are refused or
deferred while held. Autopilot reports `addon_hold` instead of `draining`
(`ProviderLoop+AutopilotBusy.swift`).
6. The hold's unload keeps `loaded-models.json`, like a shutdown, so the next
start's preload plan is unchanged (`unloadModel`).
7. An expired lease ends a hold only while no add-on holds the machine lock
(`enforceAddonHoldLeases`, `AddonMachineLock.observe` in
`provider-swift/Sources/ProviderCore/Addon/AddonHoldPaths.swift`).
8. Active holds persist in the owner-only runtime directory; a restarted or
updated provider closes admission before the local endpoint or startup
preload can load a model while a lease is valid or the lock is held
(`restoreAddonHolds`, `startAddonHoldService` called from `run()` in
`provider-swift/Sources/ProviderCore/ProviderLoop+Serve.swift`).
9. Only code satisfying the add-on requirement, signed with the hardened
runtime and running as the same user, may hold; the requested add-on
identifier must equal the verified signer (`AddonPeerPolicy` in
`provider-swift/Sources/ProviderCore/Addon/AddonPeerPolicy.swift`,
`handleAddonIPC`).

## Failure modes

| Failure | Behavior |
|---|---|
| Add-on crashes | The kernel releases its lock; the lease expires within 30–600 s and the provider returns |
| Add-on crashes while VMs keep the lock | The hold stays after the lease until the lock is free or the operator runs `darkbloom addon release --force` |
| Accepted work outlives 120 s | It is cancelled with the typed draining terminal; models unload |
| A model retirement faults | That model stays resident and is logged; the hold still becomes `held` |
| Provider restarts or updates while held | It comes back held without loading models; a record whose lease expired with no lock is discarded |
| Unreadable hold record | Ignored with a warning; the provider starts unheld and the add-on's next `renew` gets `not_found` |
| Model switch running | `hold` answers `unavailable`; the add-on retries |
| Socket path in use by another live provider | The service does not start; operator control still works through the mailbox |
| Older coordinator | Ignores the busy fields and still excludes the provider on `status: draining` |

## Code map

| Concern | File / symbol |
|---|---|
| Hold transitions, leases, persistence, restore | `provider-swift/Sources/ProviderCore/Addon/ProviderLoop+AddonHold.swift` (`holdForAddon`, `settleAddonHold`, `endAddonHold`, `enforceAddonHoldLeases`, `restoreAddonHolds`) |
| Hold record and active-hold book | `provider-swift/Sources/ProviderCore/Addon/AddonHold.swift` (`AddonHold`, `AddonHoldBook`) |
| Persisted record | `provider-swift/Sources/ProviderCore/Addon/AddonHoldStore.swift` (`AddonHoldStore`) |
| Socket contract and server | `provider-swift/Sources/ProviderCore/Addon/AddonIPC.swift` (`AddonIPC`), `provider-swift/Sources/ProviderCore/Addon/AddonIPCServer.swift` (`AddonIPCServer`) |
| Peer verification | `provider-swift/Sources/ProviderCore/Addon/AddonPeerPolicy.swift` (`AddonPeerPolicy`) |
| Operator control | `provider-swift/Sources/ProviderCore/Addon/AddonHoldControl.swift`, `provider-swift/Sources/darkbloom/AddonCommand.swift` (`Addon`) |
| Local transition log | `provider-swift/Sources/ProviderCore/Addon/AddonHoldTelemetry.swift` (`AddonHoldTelemetry`) |
| Wire fields | `provider-swift/Sources/ProviderCore/Protocol/ProviderBusy.swift` (`ProviderBusy`) |

## Related

- [Add-on hold IPC reference](../reference/addon-hold-ipc.md)
- [Protocol busy fields](../reference/protocol-messages.md#add-on-hold-busy-fields)
- [`darkbloom addon`](../provider/cli-reference.md#darkbloom-addon)
- [Provider Autopilot](model-autopilot.md)
5 changes: 4 additions & 1 deletion docs/architecture/model-autopilot.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Provider Autopilot

> Last updated: 2026-10-25
> Last updated: 2026-10-27

Provider Autopilot offers an external controller the models of a Mac whose
operator saved Autopilot consent: the saved selection plus every verified
Expand Down Expand Up @@ -179,6 +179,9 @@ served outside the saved `enabled_models`; `darkbloom autopilot status` prints
exactly when the operation policy's busy input is false. A `provider_busy`
status carries the first reason as `detail`, and consented snapshots report the
reasons and their age ([protocol](../reference/protocol-messages.md#model_autopilot-state)).
While a local add-on holds the Mac the reason is `addon_hold`, reported instead
of `draining` for the drain the hold owns; operations are refused until the
[hold](addon-hold.md) ends.

`superviseTransitions` runs on the capacity tick while consent is saved. It ages
each occurrence of a bounded transition (`ModelAutopilotTransition.stallBound`).
Expand Down
26 changes: 25 additions & 1 deletion docs/developer/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Provider And Native Contracts

> Last updated: 2026-10-26
> Last updated: 2026-10-27

Run isolated Swift, native-resource, public-fixture, retained-console and landing tests here.
Backend integration and cross-implementation qualification are platform-owned.
Expand Down Expand Up @@ -765,6 +765,30 @@ Keep state paths isolated as in `scripts/run-provider-tests.sh`; these tests
must never stop an operator process. They do not establish physical Apple
qualification or end-to-end platform authorization.

## Add-On Hold

These suites need no model weights, credentials or live provider state. The
socket and loop tests bind a real `AF_UNIX` socket and take a real `flock` in a
private temporary runtime directory.

```bash
swift test --skip-build --no-parallel \
--filter 'AddonIPCTests|AddonHoldDrainOwnershipTests|AddonHoldStoreTests|AddonPeerPolicyTests|AddonIPCServerTests|AddonHoldLoopTests|AddonHoldServeLoopTests|ProviderBusyWireTests|AddonCommandTests|AutopilotBusyReasonTests'
```

| Coverage | Suites |
|---|---|
| `hold.v1` parsing, limits, exact response keys; drain-owner precedence | `AddonIPCTests`, `AddonHoldDrainOwnershipTests` |
| Owner-only record, lock probe, runtime paths; the real peer policy refuses the test process | `AddonHoldStoreTests`, `AddonPeerPolicyTests` |
| Socket permissions, several requests per connection, oversized lines, unauthenticated peers, stale and live sockets | `AddonIPCServerTests` |
| Unload of every model, kept preload set, idempotency, bounded drain, update and reconnect cannot reopen, stop takeover, lease expiry with and without the lock, restart restore, operator mailbox | `AddonHoldLoopTests` |
| Loopback coordinator: draining heartbeat with busy fields, no `provider_drain`, 503 draining rejection, planned reconnect keeps the hold in `register`, release heartbeat | `AddonHoldServeLoopTests` |
| Heartbeat and both register encodings | `ProviderBusyWireTests` |

The peer check is exercised only negatively: no signed add-on is available to
the test runner, and a test authenticator stands in for it in the socket and
serve-loop flows. Physical add-on interoperation remains a separate check.

## Watchdog Launch Acknowledgement

`WatchdogLaunchAdapterTests` (in `LaunchAgentLifecycleTests.swift`) drives the
Expand Down
33 changes: 32 additions & 1 deletion docs/provider/cli-reference.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Provider CLI reference

> Last updated: 2026-10-25
> Last updated: 2026-10-27

Reference for the `darkbloom` command-line tool: every subcommand and flag, the
files and identifiers it creates, the `provider.toml` keys it reads with their
Expand Down Expand Up @@ -153,6 +153,7 @@ Subcommands declared by `Darkbloom.configuration.subcommands`:
| `start` | Serve. Default: install and start the LaunchAgent; `--local` for a coordinator-less server | ✓ | `StartCommand.swift` (`Start`) |
| `schedule` | Edit, show or disable saved weekly availability and startup loading; never start/stop the service | ✓ | `Scheduling/ScheduleCommand.swift` (`AvailabilitySchedule`) |
| `switch` | Gracefully replace hosted models in the running coordinator-connected provider, without restart or reconnect | | `SwitchCommand.swift` (`Switch`) |
| `addon` | Debug add-on holds on the running provider: `status` (default), `hold`, `release` | | `AddonCommand.swift` (`Addon`) |
| `stop` | Drain accepted requests, then stop the LaunchAgent; `--uninstall` removes the provider, watchdog and desktop API agents | | `StopCommand.swift` (`Stop`) |
| `restart` | Drain, restart with recorded configuration, and confirm fresh authorization | ✓ | `RestartCommand.swift` (`Restart`) |
| `status` | Config, hardware, schedule, live daemon state (including the coordinator's last `Trust: <level> / <status>` message), per-slot KV/MTP posture | ✓ | `StatusCommand.swift` (`Status`) |
Expand Down Expand Up @@ -901,6 +902,35 @@ its heartbeat (`idle_unload_mins`) so the dashboard shows an empty slot as
"sleeping, wakes on demand" rather than as a fault. `--json` prints the policy
machine-readably.

## `darkbloom addon`

Operator debugging of [add-on holds](../architecture/addon-hold.md): inspect,
place or release the hold a local add-on uses while its VMs run. The commands
reach the running provider through its owner-only lifecycle mailbox, bound to
the live process identity; add-ons themselves use the
[local socket](../reference/addon-hold-ipc.md).

```bash
darkbloom addon [status]
darkbloom addon hold [--lease-seconds <30...600>]
darkbloom addon release [--force]
```

| Subcommand / flag | Default | Effect |
|---|---|---|
| `status` | default subcommand | Active holds (add-on, `draining` or `held`, start time, lease) and whether an add-on holds the machine lock |
| `hold --lease-seconds <n>` | `600` | Hold as add-on `operator`, exactly like an add-on: admission closes, accepted work finishes (up to 120 s), every model unloads. Repeating renews the lease |
| `release` | — | Release every active hold; the provider serves again at once on the same connection |
| `release --force` | `false` | Release even while an add-on holds the machine lock, so its VMs may still be using memory |

Without `--force`, `release` refuses while the machine lock is held. A provider
without add-on support (older, or a standalone `--local` server) is refused
before anything is sent. The command waits up to 10 seconds for the provider's
answer. `darkbloom status` prints `Not serving: held by add-on …` while held.
Code: `provider-swift/Sources/darkbloom/AddonCommand.swift` (`Addon`),
`provider-swift/Sources/ProviderCore/Addon/ProviderLoop+AddonHold.swift`
(`handleAddonControl`).

## `darkbloom stop`

Drain accepted requests and their terminal usage before persistently stopping
Expand Down Expand Up @@ -1725,6 +1755,7 @@ manual use.
| Device token | `~/.darkbloom/auth_token` (`DARKBLOOM_AUTH_TOKEN_PATH`) | `provider-swift/Sources/ProviderCore/Auth/DeviceAuth.swift` |
| Local-mode token / discovery | `~/.darkbloom/local_token`, `~/.darkbloom/local.json` (`DARKBLOOM_LOCAL_DIR`), both `0600` | `provider-swift/Sources/ProviderCore/Server/LocalEndpoint.swift` |
| Daemon state | `~/.darkbloom/daemon-state.json` (`DARKBLOOM_STATE_FILE`) | `provider-swift/Sources/ProviderCore/Service/DaemonStateFile.swift` |
| Add-on runtime | `~/.darkbloom/runtime/` (`0700`): `addon.sock` (`0600`, test override `DARKBLOOM_ADDON_SOCKET`), `addon-hold.json` (`0600`, active holds) and the add-on's `ownership.lock` ([IPC reference](../reference/addon-hold-ipc.md#files)) | `provider-swift/Sources/ProviderCore/Addon/AddonHoldPaths.swift` (`AddonHoldPaths`) |
| PID file | `~/.darkbloom/provider.pid` (`DARKBLOOM_PID_FILE`) | `provider-swift/Sources/ProviderCore/Service/ProcessLifecycle.swift` |
| Warm-model journal | `~/.darkbloom/loaded-models.json` (`DARKBLOOM_LOADED_MODELS_FILE`) | `provider-swift/Sources/ProviderCore/Service/LoadedModelsStore.swift` |
| Verified weight hashes | `~/.darkbloom/verified-weights.json` (`DARKBLOOM_VERIFIED_WEIGHTS_FILE`), `0600`; managed GLM load hashes bound to file identities, reused for startup registration | `provider-swift/Sources/ProviderCore/Service/VerifiedWeightHashStore.swift` |
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Provider Reference

> Last updated: 2026-10-18
> Last updated: 2026-10-27

Exact public wire shapes, manifests and provider/retained-console configuration.

- [Add-on hold IPC](addon-hold-ipc.md): local `hold.v1` socket, operations, errors and peer checks
- [App Attest shadow protocol, machine inventory, and evidence](app-attest-shadow.md)
- [Configuration reference](configuration.md)
- [Desktop control API](desktop-control.md): Swift CLI/backend control API, operations, lifecycle, and desktop account projection
Expand Down
Loading
Loading