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
7 changes: 7 additions & 0 deletions changelog.d/cluster-leader-local-api-beside-darkbloom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### A pair's leader can take its owner's local requests beside Darkbloom

- The member that hosts a pair's slot can now run the local OpenAI-compatible API while the pair serves through Darkbloom. Local and Darkbloom requests share the pair's one engine and its lanes: no second process or engine, and no extra copy of the stream. No command starts it yet: `darkbloom start --cluster-member` still refuses `--local-endpoint`, and `darkbloom cluster` keeps serving a pair through Darkbloom with no local API.
- Only that member runs it, and always with a key. Any other member still refuses a local API.
- While the pair isn't serving (it is forming, re-forming or stopped), a local request gets 503, as `darkbloom cluster local` answers, instead of a 500. A model the pair doesn't serve gets 404.
- When every lane is busy, a local request waits its turn in a bounded queue, as on `darkbloom cluster local` (`DARKBLOOM_CLUSTER_QUEUE_WAITING`, default 64): 429 when the queue is full, 503 when it waited too long. A waiting request is woken when a lane frees and is never polled; Darkbloom requests never wait for it.
- A local request that arrives while the pair runs its first test request waits until the pair opens, and gets 503 if the pair stops instead.
62 changes: 61 additions & 1 deletion docs/architecture/inference.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Provider inference engine

> Last updated: 2026-11-06
> Last updated: 2026-11-07

How a chat-completion request is served inside the `darkbloom` provider
process: one in-process engine (`mlx-swift-lm`
Expand Down Expand Up @@ -7522,6 +7522,66 @@ candidate choice),
`provider-swift/Sources/ProviderCore/Inference/Distributed/Installed/DistributedInstalledFiles.swift`,
`provider-swift/Sources/ProviderCore/Inference/Distributed/Installed/DistributedInstalledNativeStart.swift`.

## The pair's leader takes local requests beside Darkbloom

The member that hosts a pair's slot (`ProviderLoop.hostsClusterPairSlot`, the
serving host role a rank-0 installation gives through
`installClusterPairServing`) can serve that slot on its unified local API while
the pair serves through Darkbloom
([direct mode](../provider/direct-mode.md#a-pairs-leader-beside-darkbloom)).
Local requests reach the slot's one bridge, the one Darkbloom requests reach, so
both share the pair's lanes (`ClusterPairSlotOwner.concurrentRequests`), its
capacity report and its admission. Nothing is loaded, no session forms and no
device lock is taken
(`provider-swift/Sources/ProviderCore/ProviderLoop+ClusterPairLocal.swift`).

```mermaid
flowchart LR
L["Local request"] --> S{"Slot live?"}
S -- "no" --> N["503"]
S -- "yes" --> F{"Admission open,<br/>a lane free,<br/>nobody waiting?"}
F -- "yes" --> B["The slot's bridge"]
F -- "no" --> Q["FIFO queue<br/>(parked)"]
Q -- "woken" --> F
Q -- "full / ceiling / retired" --> E["429 / 503"]
D["Darkbloom request"] --> B
```

1. Only the slot's host runs a local API, and only with a key; any other member
refuses one at startup (`ProviderLoop.prepareClusterMemberControl`,
`admitsClusterPairLocalAPI`).
2. A local request takes a lane only while fewer requests hold the pair's lanes
than it has: local ones granted here (`ClusterPairLocalLanes.lanesHeld`) and
Darkbloom ones accepted (`requestToModel`), with the engine's own count
below the lanes too, read without waiting for its queue
(`grantClusterPairLaneIfFree`, `EngineV2Bridge.capacitySnapshot`). The drain
check and the reservation (`localReservations`) have no suspension between
them, so a drain counts every granted local request.
3. Waiters are parked continuations (`ClusterPairLocalLanes.park`), never
polled. A released local lane (`releaseClusterPairLane`), each Darkbloom
request's end and the periodic capacity refresh (`refreshClusterPairCapacity`
calls `wakeClusterPairLocalWaiters`), admission opening, a drain beginning
(`beginServingDrain`) and retirement wake them; cancellation and the
queue's ceiling refuse them. A reset connection cancels its request at
once; a connection closed normally is seen only at the request's first
write, after its turn (`LocalHTTPConnectionCancellationRegistry`), as on
`cluster local`.
4. Local admission opens only once the warmup canary passed
(`clusterPairAdmissionOpenSlotID`, set in `publishClusterPairSlot`) and
closes when retirement starts (`retireClusterPairSlot`).
5. A Darkbloom request never waits for this queue; its path through the pair
is unchanged.
6. The owner-pause hook `noteClusterPairLocalUse` brackets each local request
once: from when it starts waiting for a lane, or takes one, until it leaves
without one or its lane is released.
7. Nothing keys on two ranks: the host is a serving role, not a rank, and the
pair engine takes any identity of two or more distinct peers
(`DistributedResidentIdentity.validate`). That check is only the shape of
the roster the provider expects: a repeated peer is still refused, and an
impostor still is too, because the engine serves only an owner whose
readiness equals that identity, peer for peer (`DistributedCBv2Engine`).
Every roster formed today is still two distinct peers.

## Cluster pair capability honesty

A pair advertises what its placed mode serves, not what the artifact's
Expand Down
39 changes: 38 additions & 1 deletion docs/provider/direct-mode.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Direct mode: a local OpenAI-compatible endpoint

> Last updated: 2026-11-06
> Last updated: 2026-11-07

Run the provider's inference engine as an OpenAI-compatible HTTP server on your
own Mac, either standalone (`darkbloom start --local`, no coordinator, no
Expand Down Expand Up @@ -335,6 +335,43 @@ If a short burst drains before planning finishes, the host may retain the metada

The replacement must preserve the exact model and request limits and must fit both Macs. If a wider mode cannot start, a fresh previous-mode session may be restored after the failed replacement releases. Missing release proof stops the transition; it is never treated as free memory. The HTTP API gains no mode-selection parameter or credential change from this mechanism.

### A pair's leader beside Darkbloom

The member that hosts a pair's slot while the pair serves through Darkbloom
(the coordinator-paired part on the plan's rank 0 Mac) can run this local API
too. It serves the published slot through the slot's own bridge and lanes, so
local and Darkbloom requests share one engine: no second process, engine or
copy of the stream (`provider-swift/Sources/ProviderCore/ProviderLoop+ClusterPairLocal.swift`,
`acquireClusterPairForLocal`). No command starts it yet:
`darkbloom start --cluster-member` still refuses `--local-endpoint`, and
`darkbloom cluster` serves a pair through Darkbloom with
[no local API](./two-mac-cluster.md).

| Case | Answer | Source |
|---|---|---|
| A member that does not host the slot, or a local API without a key | The member refuses to start (`ClusterMemberControlError.incompatibleConfiguration`), as before | `ProviderLoop.prepareClusterMemberControl`, `admitsClusterPairLocalAPI` |
| The pair is not serving (forming, re-forming, retired) | HTTP `503`, as `darkbloom cluster local` answers (`DistributedRequestQueue.unavailableMessage`) | `ProviderLoop.clusterPairNotServing` |
| A model the pair does not serve | HTTP `404` | `acquireClusterPairForLocal` |
| Every lane busy | Waits in arrival order with at most `DARKBLOOM_CLUSTER_QUEUE_WAITING` (default `64`) others; `429` when the queue is full; `503` after waiting longer than `requestTimeout * ((waiting + lanes) / lanes)` (`DistributedRequestQueue.Limits.waitCeiling`); a cancelled request or a reset connection leaves the queue at once, a connection closed normally only at its turn (below) | `ClusterPairLocalLanes` |
| The pair's warm-up request is running | Waits until admission opens; `503` if the slot retires instead | `ProviderLoop.publishClusterPairSlot` |
| A stop or an update drain | Waiting requests get `429` at once, as new ones do; running ones finish before the slot retires | `ProviderLoop.beginServingDrain` |
| A lost rank | Waiting requests get `503`; running ones end with their terminal | `ProviderLoop.retireClusterPairSlot` |

A waiting request costs nothing: it is parked, not polled, until a local
request releases a lane, a Darkbloom request ends (the capacity refresh that
follows it), admission opens, a drain begins or the slot retires. A request
that finds a lane free and nobody waiting takes it at once. Darkbloom requests
never wait for this queue, and their path through the pair is unchanged.

A client that closes its connection normally while it waits, as curl and
URLSession do when they give up, keeps its place in the queue until its turn,
then reaches the pair, the same as on `darkbloom cluster local`. Only a
cancelled request or a reset connection frees the place at once. The close is
noticed at the request's first write, which ends the request and frees its
lane: that bounds the waste to its turn, up to its first write. A stream
writes as its answer starts; an answer sent whole is written when it is done
(`provider-swift/Sources/ProviderCore/Server/LocalHTTPConnectionCancellation.swift`).

### Speed records of cluster requests

A cluster endpoint records how fast each request ran that computed its whole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import DarkbloomClusterProtocol
import MLXLMCommon

/// Experimental identity reported by an owner after both peer loads finish.
/// Experimental identity reported by an owner after every peer's load finished.
/// These are backend assertions, not remote attestation or hardware qualification.
public struct DistributedResidentIdentity: Sendable, Equatable {
public struct Peer: Sendable, Equatable {
Expand Down Expand Up @@ -38,11 +38,20 @@ public struct DistributedResidentIdentity: Sendable, Equatable {
(48...57).contains($0) || (97...102).contains($0)
}
}
// Two ranks or more, each named once: nothing here keys on a pair of two.
// This checks only the shape of the roster the provider expects, never a
// peer's claim. A repeated peer is refused at any size (for two, the same
// test as `peers[0].id != peers[1].id`); an impostor or any other roster
// is refused because the engine serves only an owner whose readiness
// equals this identity, peer for peer, and the owner checks it again on
// every reservation. Every roster formed today is still two distinct
// peers (`ClusterConfiguration`, `NativePairMemberInstallation`, each
// worker's readiness in `ClusterWorkerValidation`).
guard !modelID.isEmpty, modelID.utf8.count <= 512,
digest(artifactSHA256), digest(configurationSHA256),
peers.count == 2, peers[0].id != peers[1].id,
peers.count >= 2, Set(peers.map(\.id)).count == peers.count,
peers.allSatisfy({ !$0.id.isEmpty && $0.id.utf8.count <= 128 && digest($0.buildSHA256) })
else { throw DistributedEngineError.invalidConfiguration("invalid two-peer identity") }
else { throw DistributedEngineError.invalidConfiguration("invalid peer identity") }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ extension ProviderLoop {

/// Called before any solo subsystem can start. No model, GPU probe, cache
/// maintenance, update/relaunch, capacity monitor or device lease is started.
/// Only the member that hosts its pair's slot may run a local API
/// (`ProviderLoop+ClusterPairLocal`).
internal func prepareClusterMemberControl() throws {
guard loopConfig.localEndpoint == nil, modelSlots.isEmpty,
guard loopConfig.localEndpoint.map({ admitsClusterPairLocalAPI($0) }) ?? true, modelSlots.isEmpty,
modelsLoading.isEmpty, preloadTasks.isEmpty else {
throw ClusterMemberControlError.incompatibleConfiguration
}
Expand Down
Loading
Loading