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
5 changes: 5 additions & 0 deletions changelog.d/tools-wait-for-the-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### `darkbloom --opencode` and `--hermes` wait for your model, and starts say when a tool points elsewhere

- Before it writes a tool's config, the start waits while this Mac still loads the models it started with, so OpenCode and Hermes get the limits this Mac gives each model instead of the catalog's. A terminal shows one live line, ``⠙ Loading Gemma 4 26B before setting up OpenCode… 42s``; without one, a line as it starts and one as it ends. It waits at most 10 minutes. A model still loading then, or one that didn't load, sets nothing up, and one line names the next step (`darkbloom status`, then `darkbloom --opencode` again). Ctrl-C stops the wait: nothing is written, and the start says so, also when the model finishes loading just then. Darkbloom keeps serving either way. On one Mac only for now: a pair's local API isn't waited for.
- A model that isn't loading as the tools are set up (one unloaded when idle, or one that loads on a request) is named in one line: run `darkbloom --opencode` again once `darkbloom status` shows it loaded. That holds too when a route that dials out lists the catalog's limits for it.
- A start without `--opencode` or `--hermes` never changes a tool's config. When OpenCode or Hermes is set up for another address than this Mac's local API (another port or `--bind`), or reads another key file, one line says so: ``OpenCode points at http://127.0.0.1:8001/v1, not this Mac's local API (http://127.0.0.1:8002/v1): `darkbloom --opencode` points it there.`` `localhost`, `127.0.0.1` and `::1` are this Mac, in any case, and so is its `.local` name for an API on every address. On one Mac only for now: a paired Mac's start doesn't say it.
34 changes: 34 additions & 0 deletions docs/architecture/tool-integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ flowchart LR
L["local.json<br/>(live port)"] --> E["IntegrationEndpoint.live<br/>base URL + key file"]
K["local_token /<br/>cluster_local_token"] --> E
E --> M["IntegrationModels.fetch<br/>GET /v1/models"]
S["the API process's<br/>daemon state"] --> X["ModelLoadWait<br/>(one live line)"]
M --> X
X -->|"loaded: read again"| M
C["catalog<br/>(names, limits)"] --> R["IntegrationModels.resolve"]
M --> R
R --> P{"planner"}
Expand Down Expand Up @@ -59,6 +62,25 @@ flowchart LR
(65,536 for a 262,144-token window whose catalog row says 32,768). A pair's leader lists the pair's own context and
output instead (what its mode serves; it refuses a larger `max_tokens`), so a GLM pair's output stays 65,536.
<!-- pending R-FULL: the writers give each model its full output length (the cap is a quarter of the context); only the Mac's memory bounds a request; OpenRouter is separate. -->
Before the models are resolved, `ModelLoadWait.run` waits while the local API's own process still loads a model
this Mac lists for its start: this Mac's `/v1/models` gives a model's own limits (its window, above) only once it
is loaded, and the router may fill the catalog's in until then (on one Mac its `input_modalities` are listed loaded
or not). `ModelLoads.reported` believes only the daemon state that process wrote (its pid is `local.json`'s):
`startupPreloadPendingModels` and `warmModels`; the coordinator-less server (Only me) writes none of them and loads
before it listens. Where that state can't tell (there is none, it is another process's or another schema's, or the
API is a pair's, whose process preloads nothing or writes no state), nothing is waited for, as before the wait;
only a state that keeps naming a model as pending holds the start, for at most the deadline. An answer naming
nothing loaded and nothing to load is asked again `settleSeconds` (1 s) later, for a provider whose API answered
before it said what it loads. Then the list is read again. The wait draws one `LiveLine` on a terminal (else a
start and an end line), takes SIGINT only while it runs (`InterruptWatch`) and ends after `defaultTimeout`
(600 s); a timeout, a model that didn't load, or Ctrl-C writes nothing (`ToolIntegrationStep.waitForModels`).
Ctrl-C is asked for right after each look, before "nothing loading" can end the wait, and once more as the wait
returns, before the list is read again. The wait's `.ready` carries what the process has loaded by its own word
(`warmModels`; none from Only me's server or without a state of its own), and the "run again" line names this
Mac's models it hasn't (`ToolIntegrationStep.notLoaded`); without that word, those `/v1/models` lists without a
context. The word counts because a route that dials out lists the catalog's limits for a model that isn't loaded.
The wait is single-Mac for now: a pair's API can't tell (above), and a pair start without a terminal waits for the
pair to serve instead (`ClusterPairCommands`).
3. **OpenCode.** `OpenCodeConfigPlan.plan` reads the file into byte ranges with `JSONCDocument` and replaces only the
`providers.<id>` value, or appends it in the file's indentation and trailing-comma style. The block has the base
URL, `apiKey: "{file:<key file>}"` (under `~` in the home directory), `compatibility.maxTokensField: "max_tokens"`
Expand All @@ -76,6 +98,15 @@ flowchart LR
them (so oMLX's `omlx launch hermes`, and Hermes up to v0.17), are part of that key.
5. **Write.** `ToolConfigWriter.apply` makes the planned edit (invariants below) and returns the file, the backup
and the exact undo command.
6. **Follow the address.** A start without a tool flag that serves a local API reads each tool's provider block back
(`OpenCodeConfigPlan.pointer`, `HermesConfigPlan.pointer`: the base URL, and the file `{file:}` or
`key_cmd: /bin/cat <file>` reads) and compares it with that API: its base URL and `LocalEndpoint.tokenPath()`, or
another file holding the same key. `ToolAddress.notice` says in one line which tools miss it and names
`darkbloom --opencode` / `--hermes`; it never writes (`Start.noteToolsPointingElsewhere`). Addresses compare by
`ToolAddress.sameAddress`: scheme, host and port in any case (the scheme's own port when none is given) and the
path without one `/` at its end; `localhost`, `127.0.0.1` and `::1` are one host, and so is this Mac's Bonjour name
(`Local.localHostName()`, with `.local`) for an API on every address (`ToolAddress.thisMacsNames`). Single-Mac
only: a paired Mac's start starts the pair and doesn't compare.

## Invariants

Expand Down Expand Up @@ -112,6 +143,7 @@ flowchart LR
| The endpoint lists no chat models | `.refused` (kind `.models`) for both tools |
| A dangling symlink, a folder or a file over 4 MiB (`maximumFileBytes = 4 << 20`) | `.refused` (kind `.file`) |
| Another Darkbloom run holds the lock for `lockTimeout = 10` s | `ToolConfigWriterError` |
| A model is still loading after `ModelLoadWait.defaultTimeout`, didn't load, or Ctrl-C stops the wait | Nothing is written; one line names the next step |

## Hermes versions and limits

Expand All @@ -131,6 +163,8 @@ flowchart LR
| OpenCode | `provider-swift/Sources/darkbloom/ToolIntegration/OpenCodeConfigPlan.swift`, `provider-swift/Sources/darkbloom/ToolIntegration/JSONCDocument.swift` |
| Hermes | `provider-swift/Sources/darkbloom/ToolIntegration/HermesConfigPlan.swift` (`HermesConfigPlan.plan`, `previousEndpointKeys`), `provider-swift/Sources/darkbloom/ToolIntegration/HermesVersion.swift` (`HermesVersion.keyCommandWarning`) |
| Plans and refusals | `provider-swift/Sources/darkbloom/ToolIntegration/ToolConfigPlan.swift` |
| The wait for this Mac's models | `provider-swift/Sources/darkbloom/ToolIntegration/ModelLoadWait.swift` (`ModelLoadWait`, `ModelLoads`, `LiveLine`, `InterruptWatch`) |
| Where a tool points, after a start without a tool flag | `provider-swift/Sources/darkbloom/ToolIntegration/ToolAddress.swift` (`ToolAddress.notice`), `provider-swift/Sources/darkbloom/Start/StartCommand+Tools.swift` (`Start.noteToolsPointingElsewhere`) |
| Safe edits | `provider-swift/Sources/darkbloom/ToolIntegration/ToolConfigWriter.swift` (`ToolConfigWriter.apply`) |
| Tests | `provider-swift/Tests/DarkbloomCLITests/ToolIntegration/`, `scripts/integrations/opencode-smoke.sh` |

Expand Down
3 changes: 2 additions & 1 deletion docs/developer/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ config.

```bash
swift test --skip-build --no-parallel \
--filter 'OpenCodeConfigPlanTests|HermesConfigPlanTests|HermesVersionTests|ToolConfigWriterTests|IntegrationModelsTests|ToolIntegrationTests|AtomicFileTests|keyFileIsTheFileHoldingThePortsKey'
--filter 'OpenCodeConfigPlanTests|HermesConfigPlanTests|HermesVersionTests|ToolConfigWriterTests|IntegrationModelsTests|ToolIntegrationTests|ModelLoadWaitTests|ToolAddressTests|AtomicFileTests|keyFileIsTheFileHoldingThePortsKey'
```

| Coverage | Suites |
Expand All @@ -998,6 +998,7 @@ swift test --skip-build --no-parallel \
| New file, Hermes' fresh-install `model: ""`, a file as oMLX writes it (PyYAML's column-0 lists too), the `model:` alias key, the kept default, the 64,000-token floor, quoting, refusals, every written key against Hermes' documented keys; the `key_cmd` version warning | `HermesConfigPlanTests`, `HermesVersionTests` |
| Backups (exact bytes, 0600, five kept), unchanged writes nothing, atomic replace, symlinks, a concurrent edit, dry run, the undo command run through `/bin/sh` | `ToolConfigWriterTests`, `AtomicFileTests` |
| Catalog names, limits and capability aliases; the fetch with the key file's key; `local.json`'s endpoint and key file; the modes of new files; the owner's default in the other global OpenCode file; no written file holds the key | `IntegrationModelsTests`, `ToolIntegrationTests`, `keyFileIsTheFileHoldingThePortsKey` |
| The wait for this Mac's models (a scripted process and a clock that moves only between looks; every fake ends a wait at its bound, so a regression fails instead of hanging, and each suite has a one-minute limit), Ctrl-C (SIGUSR2 stands in for SIGINT), the run-again line; a plain start's notice, the address rule and the start's two hooks | `ModelLoadWaitTests`, `ToolAddressTests` |

`scripts/integrations/opencode-smoke.sh` then runs the real OpenCode 2 on the
written config. It runs the writer through the opt-in `OpenCodeSmokeSetup`
Expand Down
50 changes: 47 additions & 3 deletions docs/provider/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,59 @@ They run once, at `start`:
No line or file holds the key, and no start screen shows it
(`Start.localServerBanner`). The `--local` banner's `Your requests:` line
and the tool block come from one Keychain read, of whether an account key
is saved. A model of this Mac's that isn't loaded yet has no context in
`/v1/models`, so one line says to run the start again once it is loaded, to
pick up its limits.
is saved.
4. **Before it writes, it waits for this Mac's models** (`ModelLoadWait`,
`provider-swift/Sources/darkbloom/ToolIntegration/ModelLoadWait.swift`):
while the local API's own process (its daemon state, by the pid in
`local.json`) still loads a model this Mac lists for its start
(`startupPreloadPendingModels`), `/v1/models` gives none of that model's own
limits. On a terminal one live line is drawn in place
(`⠙ Loading Gemma 4 26B before setting up OpenCode… 42s`); without one, a
line as it starts (`Waiting up to 10m for Gemma 4 26B to load before setting
up OpenCode.`) and one as it ends (`Gemma 4 26B loaded after 42s.`). Then
`/v1/models` is read again and the tools get the limits this Mac gives. A
provider that names nothing loaded and nothing to load is asked once more a
second later (it may not have said yet); Only me's server loads before it
listens, so it isn't waited for. Nothing is written, and the start exits 1,
when the model is still loading after `ModelLoadWait.defaultTimeout` (10
minutes; ``✗ Gemma 4 26B is still loading after 10m, so OpenCode wasn't set
up. Run `darkbloom --opencode` again once `darkbloom status` shows it
loaded.``), when it didn't load (``… didn't load, so OpenCode wasn't set up:
`darkbloom status` says why.``), or on Ctrl-C, which the wait alone takes
(``Stopped waiting for Gemma 4 26B to load, so OpenCode wasn't set up;
nothing was written. …``). It asks for Ctrl-C at every look before anything
else, and once more as it ends, so one pressed as the model finishes loading
writes nothing either (``Stopped before setting up OpenCode; nothing was
written. Run `darkbloom --opencode` again.``). A model that isn't loading
(unloaded when idle, or one that loads on a request) isn't waited for: it
gets the catalog's limits, and one line says to run `darkbloom --opencode`
again once `darkbloom status` shows it loaded. Which models those are, the
process says (its `warmModels`), also when a route that dials out lists the
catalog's limits for them; without its word, `/v1/models` does (a model it
lists without a context). Single-Mac only for now: a pair's local API (its
leader's part, a relay or `cluster local`) can't tell, so it isn't waited
for.

A tool it can't set up prints `✗` and why, writes nothing and ends with
`Darkbloom keeps serving.`; the start then exits 1 (a background start keeps
running; `--local` keeps serving in the terminal). They are not single-Mac
options: on a paired Mac they apply to the pair's local API.

A start without `--opencode` or `--hermes` that serves a local API (Only me, or
Both, me first) reads the tools' configs and changes none: when Darkbloom's
provider in OpenCode's or Hermes' config points at another address than this
start's local API, or reads another key file than `local_token` (one that
doesn't hold the same key), one line says so and names the command
(`ToolAddress`, `provider-swift/Sources/darkbloom/ToolIntegration/ToolAddress.swift`):
``OpenCode points at http://127.0.0.1:8001/v1, not this Mac's local API
(http://127.0.0.1:8002/v1): `darkbloom --opencode` points it there.`` Two
addresses are the same with the same scheme, host and port in any case (the
scheme's own port when none is given) and path (one `/` at its end aside);
`localhost`, `127.0.0.1` and `::1` are one host, and so is this Mac's `.local`
name for an API on every address (`ToolAddress.sameAddress`). Single-Mac only
for now: a paired Mac's `start` starts the pair and says nothing about the
tools.

On a Mac in a two-Mac cluster (one that holds a pairing; a cluster service left
without one is a leftover, which `darkbloom cluster status` names), `start` starts the pair, as `darkbloom cluster` does
(`Start.pairStart`, `ClusterPairCommands`), whatever its state: running, stopped with `darkbloom stop`, or not running
Expand Down
4 changes: 2 additions & 2 deletions docs/provider/direct-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ See [inference and logprob admission](../architecture/inference.md).
| API key | `~/.darkbloom/local_token`, mode `0600`; `dk-local-` + base64url of 32 random bytes; created once, reused across restarts | `provider-swift/Sources/ProviderCore/Server/LocalEndpoint.swift` (`loadOrCreateToken`, `tokenPrefix`) |
| Discovery record | `~/.darkbloom/local.json`, mode `0600`: `base_url`, `api_key`, `host`, `port`, `pid`, `version`, `updated_at`; removed on graceful shutdown | `LocalEndpoint.Info`, `writeInfo`, `removeInfo` |
| Directory | `~/.darkbloom`; a dev build (`darkbloom-dev`) uses `~/.darkbloom-dev`. `DARKBLOOM_LOCAL_DIR` replaces it for both files | `LocalEndpoint.directory`, `BuildEnvironment.installDirectory` |
| Key file for tools | A tool that reads the key from a file instead of keeping a copy (OpenCode's `{file:}`, Hermes' `key_cmd`) uses the file holding exactly the key of the server on that port: `local_token`, or `cluster_local_token` for a pair's API. Both files hold only the key and are replaced atomically, never partly written. See the [config writers](../architecture/tool-integrations.md) | `LocalEndpoint.keyFile(forPort:)`, `provider-swift/Sources/ProviderCore/Service/AtomicFile.swift` (`AtomicFile.write`) |
| Key file for tools | A tool that reads the key from a file instead of keeping a copy (OpenCode's `{file:}`, Hermes' `key_cmd`) uses the file holding exactly the key of the server on that port: `local_token`, or `cluster_local_token` for a pair's API. Both files hold only the key and are replaced atomically, never partly written. A start without `--opencode` / `--hermes` says in one line when a tool set up for Darkbloom reads another file than `local_token` (or points at another address), and changes nothing. See the [config writers](../architecture/tool-integrations.md) | `LocalEndpoint.keyFile(forPort:)`, `LocalEndpoint.tokenPath()`, `provider-swift/Sources/ProviderCore/Service/AtomicFile.swift` (`AtomicFile.write`) |
| `--bind 0.0.0.0` | Advertised `base_url` uses `127.0.0.1`; other hosts use the machine's address, and `darkbloom local` prints its Bonjour URL on a `network:` line. [`darkbloom cluster local`](./two-mac-cluster.md#serving-locally-only) binds the cluster's API this way by default | `LocalEndpoint.Info.init`, `Local.networkURL` |
| `--no-auth` | No token check; `api_key` is empty in the discovery record. The [local router](#when-this-mac-cant-take-a-request-the-local-router) then passes nothing on and never uses the account key | `LocalInferenceHTTPConfig.authToken == nil`; `LocalRouterConfig.localKeyRequired` |

Expand Down Expand Up @@ -558,7 +558,7 @@ writes too (`StandaloneServer.startLifecycleControl`).
| `401 Unauthorized` | Send `Authorization: Bearer $(cat ~/.darkbloom/local_token)`; the token in `local.json` is authoritative |
| `413` on a chat request with images | Body above `localInferenceMaxUploadBytes`; downscale or send fewer images |
| `darkbloom local` says no server is running but a process is listening | The listener is not a Darkbloom server (foreign process on the port) or a stale `local.json` was cleaned; restart with a free port |
| ``the local API isn't up: port 8001 is in use (`darkbloom start --port 8002`)`` (`status`, `local`) | Another program listens on this Mac's local API port. Free it and run `darkbloom restart`, or run the command it names: the new port is saved, and tools set up for the old one need `darkbloom --opencode` or `--hermes` again |
| ``the local API isn't up: port 8001 is in use (`darkbloom start --port 8002`)`` (`status`, `local`) | Another program listens on this Mac's local API port. Free it and run `darkbloom restart`, or run the command it names: the new port is saved, and the start names each tool still set up for the old one: `darkbloom --opencode` or `--hermes` points it at the new one |
| Model not in `/v1/models` | Only models with an engine-v2 adapter are advertised; `darkbloom models list` |
| Endpoint unreachable from another machine | Default bind is loopback; `--bind 0.0.0.0` and open the port, then set a reverse proxy with TLS |
| A DevNet build (`darkbloom-dev`) fails a request for a model it hasn't loaded with ``The production Darkbloom on this Mac holds this Mac's GPU: … no model of this build loads here until production stops serving`` | The production app serves on this Mac, and two models don't fit one Mac's memory: a DevNet build refuses each cold load at its start, before any eviction, weight hash or reservation (`StandaloneServer.ensureModelLoaded`, `ClusterGPUHold.productionServesHere`). A model it loaded before production started keeps serving. Retry once production stops serving here; production's own build never refuses this way |
Expand Down
Loading
Loading