Metadata-Version: 2.5
Name: dinkster-supervisor
Version: 0.0.7.dev1091
Summary: Dinkster supervisor: layer-0 process that owns the public port, spawns engine hosts, and proxies to them
Project-URL: Source, https://github.com/Kosinkadink/Dinkster/tree/b62ac6146f9dc20f5eaa6ba4b945731ca5350274
Requires-Python: >=3.12
Requires-Dist: aiohttp>=3.10
Requires-Dist: blake3>=1.0
Requires-Dist: cryptography>=44
Requires-Dist: packaging>=24
Requires-Dist: pydantic<3,>=2.10
Requires-Dist: pygit2==1.21.0
Requires-Dist: zstandard>=0.25
Description-Content-Type: text/markdown

# dinkster-supervisor

`dinkster-supervisor` is the layer-0 process that owns the public port, starts an
engine host on an internal port, and proxies HTTP and WebSocket traffic only
after `GET /api/health` succeeds. Its station process applies the same
version-independent machinery to multiple configured Dinkster installations.

## Engine endpoint admission

An engine manifest can declare `capabilities = ["bound-endpoint/1"]`. Managed
launches then pass `--engine-bound-endpoint` to the supervisor. Direct launches
can supply the same flag only when their engine supports the capability.
The supervisor passes `--port 0 --bound-endpoint PATH`; the engine holds its
listener and atomically writes `{"format":"dinkster.bound-endpoint/1","port":N}`
to that private path. Health probes use only the announced port and the requested
internal host. The normal startup deadline includes waiting for this announcement.

Engines without the declaration retain fixed `--host/--port` launch support.
Its bind-close-reuse allocation can lose the port to another process; the
supervisor logs that limitation. It does not retry allocation or engine startup.

## Setup

This package is a uv workspace member and is not published separately yet.
From the repository root:

```sh
uv sync --all-packages
uv run dinkster-installs --help
uv run dinkster-supervisor --help
uv run dinkster-station --help
```

All three console scripts are supplied directly by this package. The supervisor
defaults to `127.0.0.1:3639` (same default as a bare engine - the port a
frontend points at means the same thing either way); the station's
management port defaults to `127.0.0.1:3649`, and the station reads an
installs TOML file and gives each install its configured public port.
Dinkster deliberately stays out of the ComfyUI port neighborhood
(8188/8189/8199) so a Dinkster install never collides with a running
ComfyUI or comfy-runner allocation.

Contributor wheel acquisition uses the resolver's SHA-256 to verify each selected
wheel. When resolver metadata omits the size, the downloader measures the verified
file rather than issuing a separate HEAD request. An already verified retained
wheel needs no network request. Explicit sizes still enforce byte counts, and
unknown-size interrupted HTTP downloads restart without guessing a resume range.

## Signed release channels

Stable and Nightly selections require an independently pinned Ed25519 signature
before use, including local copies and offline channel caches. Third-party HTTPS
mirrors do not supply their own trust roots. Artifact hashes and sizes remain
mandatory. See [channel signing](../../docs/channel-signing.md) for key
provisioning, rotation, lost/leaked-key response and offline trust limits.

## Signed security feed refresh

The supervisor-owned `dinkster_supervisor.security.host.SecurityHost` reads independently provisioned
trust from `<root>/security.sqlite3`. Refresh requests the complete signed
`/v1/security/blocks` document from each trusted issuer. A local
`<root>/security.toml` can select a mirror without changing the issuing identity:

```toml
refuse_vulnerable = false
request_timeout_seconds = 5.0
refresh_interval_seconds = 60.0
# maximum_feed_age_seconds = 86400

[feed_urls]
"https://registry.example.org" = "https://mirror.example.org/security/index.json"
```

The API and supervisor wheels also expose the same source as `dinkster_security`.
Overrides require independently provisioned trust for the exact issuer. Feed
responses cannot provision keys. Refresh failures retain the accepted checkpoint
and decisions; downloads are limited to 32 MiB and do not send registry bearer
credentials. Default policy has no feed-age refusal. An explicit maximum age
uses signed `issuedAt`, not the time of the latest successful download. Explicit
vulnerability refusal applies to retained releases too. Local policy reloads
at host creation and on refresh; malformed policy refuses rather than silently
reverting to defaults.
Installer acquisition, install, rollback and serving selection refresh before
their policy check. Managed generation pack selection also refreshes its project
root. Serving uses the selected generation's project root, otherwise the install
root, otherwise the library root, when security trust or configuration exists
at launch. The serving host owns a periodic refresh task and cancels it during
cleanup. It checks release identities before catalog preparation, worker import,
lazy activation and dispatch, including previously pinned routes. A new block
revokes execution before closing isolated workers or shared process owners;
affected in-process execution requests whole-engine shutdown. A lift never
resumes a revoked session. `GET /api/security` uses `SecurityHost.status` to
report affected release digests, issuing registries, full exact-notice details,
decisions, last verification times, configured age refusals and refresh failures.
Feed observations distinguish cached, expired, not-received and not-provisioned
issuers. Recovery reports policy allowance, not whether a worker is running, and
marks repairs as not reviewed until a compatible plan is requested. Status does
not refresh, acquire payloads or change software. Flagged releases are logged
once per notice revision and answer during a host lifetime. Runtime work inventory
names registered or revoked pack workers, not whether an invocation is running.
The runtime route also reports scoped queued and retained jobs, submitted workflows
and nested node types; these identify submitted work, not incident impact.

The public environment CLI provides `security --json list` and
`security --json details --pack PACK`, using the current generation or an explicit
retained `--generation N`. The selected engine's `dinkster.manager security-status
--generation PATH [--pack PACK]` reads sealed source/dependency provenance without
importing packs or refreshing feeds. It returns full shared-host findings with
generation/software identity, including blocked releases. The tools revalidate
that identity and the current selection before returning. Unknown packs, drift
and linked sealed inputs refuse. Offline job/worker inventory is unavailable,
not an assertion that no work exists. No payload or generation changes occur.
Omit `--json` for labeled notice, decision, verification, policy and recovery
output. Repair previews print exact release and dependency locks, retained
approval digests and compatible-fix failure reasons. Workflow impact remains
explicitly unavailable offline. Malicious or affected incident advice explains
that blocking is not cleanup and recommends isolation, trusted-machine credential
rotation, exposure assessment and a rebuild from trusted inputs.

The public environment CLI accepts `security --json approve` or `disapprove`
with `--registry`, `--notice` and `--revision`. The exact answer persists and may
be changed; stale revisions and malicious notices cannot be approved. These
commands do not start or resume execution. `security --json import --registry
ISSUER --file PATH` verifies a signed feed through the same trusted store before
replacing its checkpoint; rejected files leave accepted evidence and answers intact.
`security --json policy` reads the current local policy without a network request
or creating a configuration file. Explicit `--refuse-vulnerable` or
`--no-refuse-vulnerable` and `--maximum-feed-age-seconds N` or
`--no-maximum-feed-age` persist those choices while preserving mirrors and refresh
timing. Invalid settings leave the previous file intact. These changes do not
install software or resume work; serving hosts reload policy on refresh.

## Ingress

An optional `[ingress]` table adds a fleet-facing port:

```toml
[ingress]
port = 8300
members = ["main", "experiments"]
primary = "main"
state_path = "/var/lib/dinkster/ingress.sqlite3"
```

The ingress persists non-expiring `(scope, clientId, jobId)` and `jobRef`
ownership in SQLite. Slice 2a sends all submissions to `primary`, routes job
reads through those assignments, serves honest all-member queue/job
aggregates, and merges member event WebSockets without rewriting frames.
Catalog and machine-local state surfaces use the primary until fleet cohort
and shared-library proofs permit execution spreading.

The ingress route matrix is explicit:

- `POST /api/jobs` claims `(default, clientId, jobId)` before forwarding to
  the owner and records `jobRef` before returning the engine's 202.
- Keyed status, cancel, and `/api/values` use the key assignment. By-ref
  status, cancel, event replay, and history detail use the jobRef assignment.
- `GET /api/jobs`, `/api/queue`, and `/api/history` merge every member and
  return 503 rather than a partial answer when any member is unanswerable.
  Member requests run concurrently under a five-second per-member deadline.
- `/api/health` and `/supervisor/*` are ingress-owned. The latter reports
  `role: "ingress"` and never reaches an engine.
- Catalog reads, queue mutations, assets, library, mounts, settings, memory,
  and cache routes go to the primary. Compatibility prompt routes are not
  exposed by the ingress.
- `/api/events` merges one reconnecting upstream WebSocket per member through
  a 64-frame/eight-MiB queue and one heartbeat-enabled downstream writer.
  Upstream frames are bounded at four MiB; oversize closes the downstream
  socket with code 1009. Primary frames pass verbatim; secondary frames pass
  only when their JSON text or binary header carries a `jobRef`. Reconnects
  use jittered exponential backoff and reset only after five stable seconds.
- Primary HTTP routes stream request and response bodies without buffering;
  the ingress accepts the engine's 16 MiB asset-upload limit. `POST /api/jobs`
  alone buffers up to four MiB because its accepted response must be validated
  and persisted before the ingress returns it.

The store defaults to `ingress.sqlite3` beside `installs.toml`. Assignments do
not expire or evict. Ambiguous engine failures retain ownership; only a
definitive owner 404 permits a conditional owner-matched deletion.

## Use

Start one supervised engine by supplying the engine command after the
supervisor options:

```sh
uv run dinkster-supervisor -- dinkster-serve
```

The single-engine management surface is:

- `GET /supervisor/status`
- `POST /supervisor/engine/restart`
- all non-reserved routes proxied when healthy, or answered with 503 while unavailable

The station management port adds:

- `GET /supervisor/installs`
- `POST /supervisor/installs/{name}/start`
- `POST /supervisor/installs/{name}/stop`
- `POST /supervisor/installs/{name}/restart`

`InstallDef`, `load_installs`, `create_supervisor_app`, and
`create_station_app` are the main Python composition exports. The supervisor
and station deliberately import no engine code.

Supervisor, station and ingress listeners validate exactly one inbound HTTP
Host before routing, CORS preflights or WebSocket upgrades. Loopback IP
literals and the configured `--host` are accepted; other hostnames require
repeatable `--allow-host HOST`. Unlisted, malformed, missing or duplicate
Host headers receive `421` with `error: "host-not-allowed"` before proxying.
Python app factories take the same `host` and `allow_hosts` inputs.

This boundary prevents DNS rebinding even when the browser legitimately
sends `Sec-Fetch-Site: same-origin` for its original hostname. The upstream
Host is still rewritten for the internal engine connection; no forwarded
Host header grants authority. Engine Host and Origin checks are unchanged.

## Design contract

The supervisor owns the public port and almost nothing else: it binds in
milliseconds, spawns the engine host behind it, narrates startup at
`GET /supervisor/status`, answers other routes 503 with a pointer body
until `GET /api/health` succeeds, then transparently proxies HTTP and the
events WebSocket. It never imports engine code - subprocess + HTTP only -
so the layer that manages installations shares no venv with any engine
version. The `/supervisor/` prefix is reserved and never falls through to
the engine proxy. CORS is owned by whoever owns the public port: opt-in
per origin via repeatable `--allow-origin`, default none, with upstream
Access-Control-* headers stripped so a child cannot double-stamp. The
station applies the same version-independent machinery to multiple
installs from a TOML file. Focused coverage is in
`tests/test_supervisor.py`, `tests/test_station.py`, and
`tests/test_installs.py`.

## Environment records

The environment state module owns the project records consumed by environment
management and Desktop. Full layout: [environments and updates](../../docs/environments-and-updates.md).

- `<root>/current`: positive decimal generation number and newline.
- `<root>/store`: absolute shared data-directory path and newline.
- `<root>/generations/<n>/python/`: complete interpreter tree; executable is
  `python.exe` on Windows, `bin/python3` on Linux.
- `<root>/generations/<n>/constraints.txt`: the release's hashed requirements.
- `generation.json`: `format: dinkster.generation/1`, `generation` integer,
  `base` digest, `engine: {version, release}`, `constraints` digest, `variant`,
  `workerProtocol` integer, and `files` (relative path -> `sha256:` digest).
  The inventory covers owned interpreter/package files and constraints, not
  itself, runtime bytecode or pack venvs. Drift reports name missing, modified
  and unexpected files, even when installed distribution versions agree.
  `inProcessPacks` is an optional array of pack ids, empty by default; these
  packs retain their dependency locks but have no pack venv. Inventory is
  finalized only after their dependencies are staged into a fresh generation.
- `<root>/journal/<timestamp>.json`: `format: dinkster.journal/1`, `operation`,
  `previous` integer or null, `target` integer, `stage`, and optional `error`.
  Operations are setup/update/rollback/restore/move/repair/gc. Stages are planned,
  building, starting, switching, complete and rolled-back; the last two are
  terminal. Interrupted records must be recovered before another operation.

`dinkster-env --root ROOT history` lists successful software activations;
`history show ID` includes the snapshot and structured differences. Both accept
`--json` and return the usual `dinkster.environment-result/1` envelope.
`<root>/history/<id>.json` stores `dinkster.install-history/1` entries with a
`dinkster.install-snapshot/1` software description, timestamp, reason, generation,
and differences from the preceding entry. These files are independent of
generation retention. Capture admits only sealed software manifests, constraints,
source locks and pack dependency locks, plus hosting and host facts; workflows,
models, outputs, libraries and mounts are not captured.

`setup --adopt-from OLD_ROOT --engine-release SHA --base-id SHA --variant NAME`
creates a new supervisor root from a standalone pack-only installation. Both
digests must match the explicitly selected channel; `catalog` exposes them.
Each installed pack requires an explicit target-variant `--pack-lock PATH`.
Planning shows the complete source lock, target dependency locks and in-process
placement. It never records the ambient interpreter as an engine/base release.
The plan compares local installed distribution metadata to target pack locks,
showing shared-store accelerator scopes separately without guessing which one
the old root used. It never runs old interpreters, pack code or .pth files.
Ambient/inherited dependencies are explicitly unrecorded, not an exact freeze;
a distribution absent from a target pack lock may still belong to the target base.
Changes to inspected distribution metadata invalidate the approved plan.
Shared-venv groups refuse rather than being silently split. The destination must
not exist and neither it nor the selected store may overlap the old root or its
shared content store. Confirmation copies verified software archives into owned
scratch and uses ordinary setup construction, health checks and history
publication. Source metadata and archive digests are rechecked before activation.
The old installation, its history, settings and user data are not copied or changed.

`restore ID [--plan | --yes]` rebuilds an exact published snapshot as a fresh
generation through normal health-checked activation, even if its old directory
was collected. Recorded manifests, locations, constraints and locks drive
acquisition; current channels do not select replacement versions. The new entry
names `restoredFrom` and both recorded and executing tools versions. Tools are
advisory and are never installed, replaced or downgraded by restore. Unavailable
inputs produce a read-only `restorePlan` with missing payloads, candidate snapshots
and every proposed substitution. Candidates minimize changed software identity
fields; equal-cost alternatives remain explicit rather than selecting the newest.
A base-only proposal preserves recorded pack sources, dependency locks and
in-process placement, and must pass their compatibility and availability checks
against the proposed base. Engine replacements also require the sealed pack
manifests' declared versions and engine ranges to agree with the recorded sources
and proposed engine. Older entries without those declarations do not infer them.
Snapshots retain pack manifests, not pack code. Acquisition provenance preserves
the original source archive cache release across engine replacements; target-owned
source admission still verifies the exact recorded pack digests.
`restore ID --approve DIGEST` approves one complete proposal. `--yes` alone never
approves substitutions. A changed project, current pointer or history invalidates
the approval. Without an obtainable candidate, restore makes no changes.
Failed restores append separate immutable receipts under
`<root>/attempts/`, exposed as `attempts` in the history JSON result.
The activation journal records `restoredFrom` before construction; recovery of an
interrupted restore appends its failure receipt before making the journal terminal.
Repeated recovery never duplicates an attempt. Source archives are retained in
the engine cache after admission. Restore uses the target engine's digest-verified
source acquisition in disposable storage, without standalone provisioning or
activation. It collects missing sources before copying the set to the retained
cache; it never resolves newer pack versions.
Pack provisioning uses the staged snapshot locks as its complete source set,
not the current generation's locks, so later-added packs are not inherited.

Activation journals retain a prepared `historyEntry` before starting the target.
Only a completed activation publishes that entry. Recovery republishes an
interrupted publication idempotently; existing history bytes are never replaced.

`dinkster-env --root SOURCE move DESTINATION [--data STORE]` prints its plan
before confirmation. Move keeps source data until destination doctor and engine
health succeed, preserves named ports/devices and refuses an existing destination.
Same-volume moves retain interpreter/package sharing and rewrite pack venv homes;
cross-volume moves use local retained bases/wheels and offline pack rebuilding.
Absolute or escaping project symlinks refuse planning. The old root retains
diagnostic journals and reports the destination instead of starting stale engines.

Optional `relocation` on a move journal contains absolute `sourceRoot`,
`targetRoot`, `sourceStore`, `targetStore`, and `sourceFiles` (relative paths to
SHA256 digests or `link:` plus relative link text). `setupRegistration` snapshots
destination registration ownership too. A temporary `healthProbe: true` binding
checks health when all original engines are stopped. Destination journal
publication uses `.dinkster-move-<timestamp>` beside the destination; interrupted
preparation rolls back before another confirmed operation. Cleanup refuses
changed source files; the original diagnostic journal remains after success.

All content identifiers are `sha256:` followed by 64 lowercase hexadecimal
digits. Relative file paths use `/`, never absolute paths or parent traversal.
Pointer and journal writes flush to disk before atomic replacement. The
`Generation`, `Journal`, `read_current` and `set_current` APIs read and write
these records; the latter refuses missing or misnumbered target generations.

Generation integrity checks hash every recorded file and stream the tree to
detect unexpected files. Pack environments and bytecode directories are pruned
from that discovery walk, not collected and sorted before being ignored. Pack
environments have separate dependency checks. Recorded files remain checked even
inside excluded directories; only unexpected files are sorted for diagnostics.
Pack reviews retain their integrity checks before and after registry preparation.

`packs-plan --progress /absolute/host-owned/progress.json` atomically publishes
stage-local `{sequence, stage, completed, total}` observations without changing
its JSON result on stdout. Integrity scans report batches of 128 checked files
and discovered directories; runtime, registry and preflight transitions report
their actual starts. This is not a timer heartbeat or overall percentage. Writes
within a stage are coalesced to at most four per second, except stage completion.
Creating `progress.json.cancel` requests cancellation at the next observation;
it never authorizes apply. The host retains the scratch directory until the
command has actually exited, including while an uninterruptible child finishes.

The channel format is `dinkster.channel/1`, with
`engine: {version, release, locations}`, `base: {variant: {id, locations}}`,
`minimumDesktopVersion`, and `signature`. Locations are a nonempty ordered array:
`{type: "mirror", path}` names the complete manifest path relative to a configured
mirror; `{type: "github", repository, tag, asset}` names an immutable release asset.
New mirror paths are `engine/<YYYY-MM-DD>-<version>-<hash8>/release-manifest.json`
and `base/<variant>/<YYYY-MM-DD>-py<python>-torch<torch>[-cu<nnn>]-<hash8>/manifest.json`.
Legacy full-hash directory paths remain supported. GitHub assets use those same manifest
basenames, never alias tags. Both stable and nightly name exact content digests,
never a latest-release lookup. Location hints do not replace digest verification.
A null signature is acceptable only for a Dinkster-operated mirror; signed
documents are refused until signature verification is available. Engine and base
artifact layouts are owned by their publishing tools, not this package.

### Extraction-only Desktop bootstrap

`scripts/build_environment_tools.py` builds the native tools payload from a
size/SHA256-pinned standalone `python/` tar.gz and the release-built supervisor
wheel. Run it with the same native Python patch version, implementation,
architecture and ABI as the standalone interpreter, including free-threaded
status; a mismatch is refused before wheel acquisition:

```sh
uv run --locked python -m scripts.build_environment_tools \
  --interpreter /absolute/path/cpython-install-only.tar.gz \
  --interpreter-size BYTES --interpreter-sha256 LOWERCASE_SHA256 \
  --supervisor-wheel /absolute/path/dinkster_supervisor-VERSION-py3-none-any.whl \
  --output /absolute/new/tools-output
```

`--interpreter` also accepts an HTTPS download URL; the complete size and hash
are checked before extraction or execution. Obtain those values from the
selected standalone interpreter's verified producer input, not a latest alias.
The independent tools workflow builds the supervisor wheel at this package's
own version, not an engine release version or a separately resolved PyPI version.
The command downloads only compatible
wheels whose filenames, versions, sizes and hashes occur in `uv.lock` and its
supervisor/tools export. uv is pinned by the supervisor's `environment-tools`
dependency group. Installation into the owned standalone tree is offline,
index-free, binary-only and hash-required. An offline `uv pip check` must
confirm that the supplied supervisor wheel and every installed dependency
have a satisfied closure before packaging. No engine or torch is installed.

The output is `dinkster-tools-<os>-<arch>.zip`, where os is `linux`, `windows`
or `macos`, and arch is `x86_64` or `arm64`. stdout is the
artifact row `{name,size,sha256}`; `SHA256SUMS` records the same archive hash.
`scripts/build_tools_release.py` finalizes all three native ZIPs into the
hash-verified `dinkster.tools/1` manifest and complete `SHA256SUMS`.
The `Supervisor tools publication` workflow runs when supervisor sources,
native build/publication scripts or tools build inputs change. It publishes
`tools/<date>-<public-supervisor-version>-<manifest-hash8>/`, `tools/index.json`
and the corresponding `tools-<label>` GitHub prerelease, verifies both sources,
then moves the Nightly channel's `tools` pointer without changing its engine/base
selection. An unpublished local output is not a production download pin.

`dinkster-env --root /absolute/tools-cache tools --channel nightly --mirror
https://releases.dinkster.org --yes --json` acquires the channel's native ZIP
using its tools manifest hash and archive size/hash, without acquiring engine
or base payloads. Omit `--yes` to inspect the selection without creating a cache.
Local ZIP folders use `<date>-<public-supervisor-version>/`, reserving `--2`,
`--3` for different manifest identities with the same label. Existing hash8-suffixed
folders remain reusable. Planning previews collisions without changing the cache.
Channels must supply an independent `tools` pointer for acquisition. A missing
or invalid pointer fails without falling back to engine assets; engine
releases do not carry native tools ZIPs.

Desktop selects that exact basename from its pinned tools manifest or the
channel's tools publication, downloads it from the selected release directory,
verifies its full size/SHA256,
and safely extracts it into a new owned directory. ZIP is only the bootstrap
container: it needs no Python/zstandard decoder before the tools exist and
does not change the producer's tar+zstd base contract. Internal interpreter
aliases are materialized as ordinary files/directories, so Windows extraction
does not require symlink privileges. The extractor must retain Unix executable
mode bits from ZIP attributes. Reject escaping paths and do not overwrite an
existing installation.

Invoke `<tools>/python/bin/python3 -I -m dinkster_supervisor.environment` on
Linux/macOS or `<tools>/python/python.exe -I -m dinkster_supervisor.environment`
on Windows. For each child prepend `<tools>/python/bin` (Windows
`<tools>/python/Scripts`) to PATH so the client's offline generation commands
find bundled uv; do not set a global PATH. Console-script shebangs are not the
relocatable interface. Extraction does not run pip, uv or package resolution.
The tools builder and the shared lifecycle builder at
`tests/fixtures/environments/build.py` are separate: the latter supplies tiny
production-format mirror inputs for Desktop/CLI equivalence, not production
tools or release artifacts.

`python -m dinkster_supervisor.environment_channels --release RELEASE_DIRECTORY
--base-manifest BASE_MANIFEST --channel nightly --output-root MIRROR_DIRECTORY
--minimum-desktop-version X.Y.Z` builds a local channel document. Repeat
`--base-manifest` for every selected variant; `--channel stable` uses the same
builder. It hashes the final release manifest bytes, derives the labelled location
for a new manifest or retains the embedded tag for a legacy manifest,
verifies every listed local engine artifact, validates each unchanged base
manifest and checks worker protocol and effective constraint compatibility.
Only after all inputs pass does it atomically replace `channels/<name>.json`.
It never downloads, changes publisher inputs or publishes to a remote service.
The release workflow validates a document after finalization, including on PRs.
Its push-only channel job runs after GitHub asset publication and calls
`scripts.publish_environment_channel` with the same selected base manifests.
With all R2 settings present, it checks published engine/base manifest bytes,
uploads only missing immutable engine objects, verifies their served hashes,
and replaces the selected channel last. Failed payload checks leave the old
channel untouched. An older main run cannot replace a newer main selection.
Absent R2 settings skip publication explicitly. The workflow compatibility
floor is `minimumDesktopVersion: "0.2.0"`; the local builder remains nonpublishing.

`environment_downloads.fetch_artifact(mirrors, relative_path, destination,
sha256, size)` downloads a known artifact from ordered local directories or
HTTPS mirrors (HTTP is limited to loopback fixtures). Its digest argument is
bare lowercase hexadecimal, not a `sha256:` identifier. Transfers hash bytes
while streaming, retain interrupted `.part` files and validate exact HTTP
range offsets on resume. Remote size/digest failures share the three-attempt
network retry budget: short bodies resume, full corrupt bodies restart from
zero, and every failed attempt appears in terminal errors. Local mirrors do
not use that retry loop. An ignored Range also restarts from zero. Final
publication requires both the expected size and digest. Signed query redirects
are allowed without logging their URLs; external non-TLS redirects and
credential-bearing mirror URLs are rejected. Calls have a bounded per-socket
network timeout.

`environment_artifacts.discover(mirrors, channel)` reads bounded metadata
without creating a project, cache or store. It verifies engine manifest bytes
and the base producer's canonical identity, including replacement membership,
then returns the intact manifests for catalog display. Unsigned remote metadata
is accepted only from `releases.dinkster.org` or the official Dinkster GitHub
repositories; local directories and loopback servers support offline fixtures.
Unknown remote mirrors require signing support and are refused. Invalid or
corrupt metadata falls through to the next configured location.

Under the caller's store writer lock, `acquire(catalog, variant, store)` fetches
and verifies base parts, safely extracts a missing base, verifies its selected
wheels and populates `wheels/<wheel filename stem>/<filename>`. After wheel verification,
it atomically retains the producer base document at
`bases/<variant>/<date>-py<python>-torch<version>/manifest.json`
if absent, preserving an existing document's custom provenance. Base identity
remains the producer's canonical fields, independent of JSON formatting.
Each cache category records full content identities in `.objects.json`; a durable
allocation journal and OS lock protect collision reservations. Different bytes
with the same label receive `--2`, `--3`, and so on. A changed label for the same
identity reuses its original location. Existing full-hash paths remain supported
without renaming installed interpreters or environments. Base archive parts use
the same base label under `downloads/`. Unknown build dates use the first local
acquisition date in UTC. Wheel filenames remain unchanged, including backend tags.
Pack archives use `artifacts/<pack>/<date>-<version>/pack.zip`, with the date
of first local acquisition. Standalone pack sources use the same label beneath
`store/`; per-pack virtual environments use `venvs/<pack>/<date>-<version>/<pack>/`,
with an accelerator parent in shared stores. Grouped environments use bounded
sorted member labels under `venvs/groups/`, retaining their full aggregate
identity. Explicit collection resolves those identities rather than treating
readable namespace directories as disposable objects. Existing digest-named
archives and environments remain in place.
Engine-owned wheel and constraints artifacts are verified through the same
downloader. Base-owned
engine wheels are omitted, and tools/source archives are not installed into
the generation. Effective constraints are stored under
`engines/<date>-<version>/constraints-<variant>.txt`. The original verified engine
manifest bytes are atomically retained beside those constraints,
without reserialization or another metadata fetch. Missing or inconsistent
catalog bytes refuse acquisition before cache creation. A base without hosted parts
currently reports that manifest reconstruction is required; it never resolves
or substitutes dependencies silently.

`environment_constraints.effective_constraints(engine_text, base_manifest)`
uses the verified base manifest's `replaces` set, not package-name prefixes.
It removes every named engine requirement, including remove-only CUDA
transitives, and appends the selected `wheels` versions and hashes. Unrelated
requirements retain their markers and hashes. Selected versions must match
engine pins and `lockedVersion`; torch/torchvision backend suffixes may refine
the same locked public version. Invalid membership, non-exact requirements,
missing hashes and wheel filename/version mismatches fail without resolution.
The resulting constraints are retained beside the generation record.

`environment_generations.build_generation` constructs a complete target from
an already verified base resolved by its full identity and effective hashed
requirements. It runs uv offline with no indexes and binary wheels only,
reading the store's wheels and uv cache. Linux attempts CoW interpreter clones
and falls back to hardlinks when the filesystem cannot clone; Windows uses
hardlinks. macOS uses native CoW clones, with independent copies on volumes that
cannot clone, so executable identities remain local to each generation. Package
installation uses the matching uv link mode. The project and store must share a
volume. A failed sync removes only its incomplete
target; it never changes `current` or the existing generation. The generation
interpreter, packages and retained constraints are hash-inventoried on success.

When a source generation, explicit pack locks or in-process hosting ids are
supplied, construction invokes the target interpreter's
`dinkster_workers.pack_environment` subprocess in preflight, rebuild and doctor
order before final inventory. All source locks remain mandatory unless explicitly
replaced by `--lock`; the adapter owns lock parsing and provisioning. The caller
supplies variant, worker protocol, measured own-environment size and shared store
paths explicitly. A nonzero exit, conflicting pack, doctor drift or malformed
single-object JSON result fails construction and removes the target. Supervisor
never imports the engine's worker packages to perform these operations. The
provisioner receives `--offline`: all extra wheels must already be verified in
the store, with no fallback to a dependency lock's URL.

When supplied together, `catalog_lock` and `artifacts` run the target interpreter
with `-B -m dinkster.generation_packs --generation ABS --catalog-lock ABS
--artifacts ABS --json` after dependency doctor and before inventory. Source
admission owns the existing registry lock and digest-named archives; the caller
does not parse a second catalog. Success requires a single object containing
distinct pack ids, an empty errors array, zero exit status and the written
`dinkster.lock`. Every admitted pack must appear in dependency doctor's result,
including packs with an empty extra dependency closure. An admission error,
missing dependency coverage, malformed result or missing inventory removes
the entire target, including any partial sources or interpreter changes. The
target interpreter then runs `-B -m dinkster.manager --root "" prepare-catalogs
--generation ABS` with repeated `--in-process-pack` selections. This command
verifies admitted sources and uses their prepared pack interpreters, without
legacy default discovery or dependency resolution. A preparation error removes
the target before activation. Catalogs enter the sealed file inventory even
though their `__pycache__/dinkster-schema-catalog` location is excluded from
source artifact identities. Additional `prepare_packs` staging runs before sealing.

For a custom base, the target interpreter can run that same source command with
`--prepare-locks ABS --variant NAME` to prepare standard dependency locks in a
new caller-owned scratch directory. It verifies the selected lock's exact
archives and manifest claims, then resolves their declared requirements against
the unsealed target's `constraints.txt`, using repeated `--index` and
`--find-links` inputs and optional `--accelerator`. JSON adds `locks`, the output
paths. This mode does not admit sources, change the source lock or seal the
generation. The caller retains the scratch directory through provisioning and
removes it afterward; no custom lock schema or store path is introduced.
`environment_custom.prepare_custom_pack_locks` invokes this mode in the acquired
target interpreter with cached constraint wheels, the custom extras index and
PyPI fallback. The engine's accelerator selection honors `DINKSTER_ACCELERATOR`
before host detection when including pack extras. It returns scratch lock paths
only; the existing generation builder still owns preflight, provisioning, source
admission and sealing.

`environment_archives.extract_base(parts, destination, platform=..., executable=...,
expected_parts=...)` verifies every ordered part's size and SHA-256 before streaming
one zstd decoder into tar extraction with the data filter. The caller supplies the
manifest's platform, interpreter path and part rows, and holds the store writer lock.
Extraction accepts only `python/` and `wheels/`, rejects traversal, absolute paths,
hardlinks, devices, Windows device names and exact duplicate paths. Internal relative
symlinks are preserved where supported by the host filesystem; unsupported or dangling
link graphs refuse extraction before promotion. Later members cannot write through
a symlink parent.
Case-ambiguous paths are refused for Windows/macOS targets or case-insensitive
destination filesystems; Linux case-sensitive trees retain distinct spellings.
Safe permission bits are preserved. A missing interpreter execute bit is restored
to 0755 with a warning. The manifest interpreter must pass an isolated sys/sysconfig
probe before promotion.
It returns `destination/python`. Existing bases are never replaced; failed
verification or extraction removes staging trees.

## Environment queries and runtime

`setup --mirror PATH --channel nightly --variant VARIANT --data PATH` plans a first
generation; `update` plans the configured channel's next generation. Both require
`--yes` or terminal confirmation before creating project/store state or acquiring
payloads. `--plan` and unconfirmed JSON calls create nothing. Confirmed calls
acquire and preflight target inputs before journaling and registration, sync
offline, check the candidate engine's health, publish `current`, then stop the old engine. Update preserves
existing engine bindings; setup accepts `--engine`, `--port` and repeatable `--gpu`.
The setup confirmation plan includes that binding name, loopback port and ordered
`CUDA_VISIBLE_DEVICES` assignment, including the empty CPU-only assignment.
Stopped bindings keep their assigned ports; only running bindings need new ports
for candidate health checks alongside their previous processes.
Managed local generations start with the project's `dinkster-library` persistence
root (or the inherited `DINKSTER_LIBRARY_ROOT`), stale-catalog preparation and
operator-only mount editing enabled. Generation selection remains the only pack
source. Folder grants persist across stop/start; fixed sandbox grants still
require a controlled restart. Remote connections and arbitrary supervisor/server
commands do not acquire these options.
Failed acquisition/build/startup retains the previous serving tree and a rolled-back
journal. A confirmed retry first recovers interrupted activation.

`environment_generations.prepare_engine_interpreter` prepares a new,
caller-owned directory from an acquired base and effective hashed constraints.
It returns the target Python after the same offline sync used by generation
construction. The caller holds the store writer lock and owns directory cleanup,
including failures. This operation does not acquire inputs, prepare packs or
write project, journal or generation metadata. Generation construction separately
retains base provenance, prepares packs and seals the inventory.

Generation construction copies a cached `engines/<release hex>/release-manifest.json`
byte-for-byte into `engine-manifest.json` before pack preparation and inventory.
The retained bytes must hash to the selected engine release. A missing legacy
cache is tolerated; present but unreadable or corrupt content refuses the build
and removes the incomplete target.

Existing per-variant dependency locks can be supplied with repeated `--pack-lock`,
and in-process placement with repeated `--in-process-pack`. Paired absolute
`--catalog-lock` and `--artifacts` inputs feed verified source admission in the
target interpreter. Without source overrides, confirmed published operations
prepare a temporary target interpreter from the acquired constraints under the
store lock, before allocating a generation or opening its journal. Isolated
`-I -B -m dinkster.manager` commands resolve fresh registry defaults or re-resolve
the installed official source set, and acquire exact-base published dependency
locks. The supervisor checks the target version, returned paths and diagnostics;
the target engine owns source/lock parsing and verification. Temporary inputs
survive candidate construction and are removed on success or failure.

Fresh setup reports unavailable defaults as `unavailablePacks` and on stderr;
an offline registry can produce a healthy empty generation. Update refuses a
missing compatible selection before journal/project mutation, retains other
source provenance and verifies reconstructed non-registry source bytes.
Explicit dependency locks replace automatic registry lock acquisition. With an
explicit source catalog but no dependency locks, legacy release-carried locks
remain usable. No supervisor dependency parser or resolver is involved.

`prepare_registry_inputs` accepts an optional `prepare_locks` callback after
source selection, before published lock retrieval. It receives the acquired
scratch engine directory with effective constraints, selected catalog lock,
exact archive cache and new output directory. A custom-base caller can resolve
standard locks from those inputs without a published base binding or a second
source selector. Returning an empty tuple still replaces published retrieval;
raising refuses preparation and removes the scratch tree. Returned files must
belong to the output directory. Explicit `pack_locks` take precedence. The optional
`preflight(engine, catalog, artifacts)` runs after source selection and before
either automatic or explicit dependency locks are used.

Update plans with registered workflows or explicit additions use the active
generation's requirements reader to snapshot requirements and input revisions.
Unconfirmed plans explicitly report source feasibility as pending; they never
download payloads or install a planning interpreter. SQLite read-only planning can
touch WAL coordination files without changing logical records or project content.
After confirmation, the acquired target interpreter runs
`dinkster.workflow_requirements project-plan` against the sole selected registry
catalog and archive cache, before a generation or journal exists. Explicit
catalog/cache inputs take precedence and use the same target preparation. Changed
requirements/revisions and unavailable saved versions refuse admission. Pack and
asset actions appear in the existing plan strings. Catalog digests and input
revisions are checked again before journaling and application.
The sealed target interpreter then consumes the approved plan, checks its
admitted pack identities and acquires only approved assets. Workflow input
revisions are checked before publication and after candidate health checks;
activation also requires an empty target requirement plan. Failure retains the
previous generation, serving state and diagnostic journal; verified cached asset
bytes may remain. Workflow records are never rewritten.
First setup with requirements needs a runnable generation planner. Update can
select another `--channel` or `--variant` through the same confirmed construction
and workflow replan. Project selection changes only after target health and final
revalidation; other project fields are preserved. These commands are not a
complete released-engine setup claim.

`stores.json` lives under `dinkster/` in `LOCALAPPDATA` on Windows or
`XDG_DATA_HOME` (default `~/.local/share`) on Linux. It has
`{format: "dinkster.stores/1", volumes: {"<st_dev>": "<absolute data path>"}}`.
The first plan defaults to `dinkster-data` beside the project; later projects on
the same volume reuse the registered store. A conflicting `--data`, cross-volume
store or overlapping project/data paths is refused. The store's `projects.json`
has `{format: "dinkster.store/1", projects: ["<absolute project root>"]}`.
Registry, store and project writers hold OS locks while registering references.
Failed initial setup restores prior project metadata and removes only the store
and volume references that setup added. Cached bytes, pre-existing settings,
other projects' references and the diagnostic journal remain. A retry can select
another `--data` when no other project references the old store. Failed updates
keep the existing project's references and active generation.
Identity/health/shutdown requests use direct loopback connections, never inherited
HTTP proxies.

`dinkster-env --root <absolute-path> catalog|status|doctor --json` emits one
`dinkster.environment-result/1` object with `operation`, `root`, `current`,
`plan` and `status`. Catalog accepts repeatable `--mirror` and `--channel`
stable/nightly; it adds `channel`, `channelManifest` and intact `variants`.
Status returns configured `channel`/`variant` and observed `engines`, never
starts a process and never creates a prospective root. Doctor reports content
and pack drift without repair and exits nonzero on drift or errors.

Published setup/update reads the engine's per-variant dependency lock and checks
its exact base id and wheel identities against the producer manifest. Non-base
wheels are fetched from their locked origins into the shared wheel store using
exact filenames, sizes and SHA256; generation construction stays offline.
Before constructing a generation, acquired non-base wheel metadata is checked
against base-owned versions using its dependency specifiers, target Python
markers and requested transitive extras. Incompatible or absent base
distributions fail with the requiring distribution, specifier and base version.
`--package-index URL` selects a simple package mirror offering those same locked
filenames, without resolution, source builds, hash overrides or origin fallback.
Confirmed setup saves it as project `packageIndex`; updates preserve it or change
it through the existing activation transaction. This is independent of channel
and Dinkster artifact `--mirror`. Intact cached wheels require no index access.

`doctor --repair [--plan | --yes]` rebuilds the same release offline into a new
generation using the existing `repair` activation journal. Plans are read-only;
apply requires confirmation. It rejects absolute, escaping or cyclic base links,
verifies relative internal aliases and regular files against sealed file hashes,
and ignores disposable bytecode. It selects constraints with the recorded digest and admits retained source
locks from the release's cached archives. Missing or corrupt local inputs refuse
instead of fetching a channel or reading an index. The shared store lock protects
uv cache cleanup and reconstruction: hand edits to installed hardlinks can also
corrupt extracted cache entries, so repair repopulates that disposable cache from
hash-verified wheels. Pack rebuilding receives explicit `--offline`, refusing
missing or corrupt dependency wheels before any URL access. Retained generations
are not edited. Candidate file names
must match the original inventory, and normal health/activation checks run before
switching current. Failure keeps the previous selection and diagnostic journal.

`start --engine <name> [--port <port>] [--gpu <device> ...]` and
`stop --engine <name>` share the same result envelope. They show a plan and
require `--yes` or terminal confirmation; `--plan` and unconfirmed machine
invocations do not launch/stop processes or create runtime/lock files. Main
uses the configured port by default; a new engine name requires an explicit
port. Existing bindings default to their saved port, including after update or
rollback. An empty device list hides GPUs, including inherited host assignments.
Identical ready assignments are idempotent; a changed assignment requires stop
first. Applied results include observed engines and the started engine URL.

Runtime writers serialize through the project's persistent `.lock` file using
an OS-held nonblocking lock. The same primitive serializes shared-store writes;
callers needing both acquire store then project. Process exit releases the lock,
but its file is never removed. Apply rechecks project/current under lock and
recovers incomplete activation journals before performing another mutation.

Runtime launches the generation interpreter through the independent supervisor.
The engine receives `--generation <absolute-generation-root>` explicitly,
including before the pointer switch and when reactivating a retained tree.
Source selection never follows the working directory or the current pointer.
Public engine rows contain `name`, `generation`, `state` and `url`; private
`runtime/<generation>/<name>.json` records retain process identity and shutdown
credentials. This permits old and candidate same-name bindings to coexist;
status displays the generation selected by `current`, while idle checks inspect
every persisted generation's engines.
Graceful shutdown requires both the live instance identity and its private
token. An unreachable or ambiguous supervisor cannot establish an idle engine;
queue checks refuse mutation unless every observed engine is safely idle.
Shutdown closes proxied WebSockets with a going-away frame before draining
HTTP handlers, allowing engine replacement while event clients are connected.
Each proxy handler cancels and joins both pumps before releasing its upstream.
Supervisor and engine output goes to private runtime logs, not CLI JSON stdout.

`rollback [--plan | --yes] --json` selects the preceding successful history entry
and rebuilds it as a fresh generation through the same path as `restore ID`.
The old generation directory is not required. The new entry has reason `rollback`
and names its source in `restoredFrom`. Missing inputs require the same complete
proposal approval with `rollback --approve DIGEST`; `--yes` never substitutes
software identities. Project settings and data stay unchanged, including edits
made after the recorded activation. Apply revalidates the plan and current state
under the writer locks; unconfirmed rollback does not write anything.

Channel/variant selection journals also carry optional `projectSettings`, an
object containing `previous` and `target` project.json text strings. Activation
writes target settings during `switching`, immediately before `current`.
Interrupted switching restores the previous exact text along with the pointer;
earlier stages never changed settings. Unexpected settings during recovery refuse
rather than overwrite an external edit. Software rollback never reverses this
settings pair: only software identities are restored. Journals without this field
leave project settings alone.

Activation journals retain `bindings`: objects containing `name`, `port`,
`gpus` (device-id strings), `running` (boolean), and `targetPort`. The prior
assignments and new ports are durable before construction or process launch.
Stopped bindings remain stopped across activation. Recovery stops authenticated
target processes, restores the previous pointer and running assignments, removes
only a newly constructed failed target, and marks the journal `rolled-back`.
Failed rebuilt rollback targets are removed, just like failed restore targets.
Ambiguous process
identity leaves the journal incomplete rather than killing an unrelated process.

Initial activation uses the same journal with operation `setup` and previous
`null`. `begin_setup` records the requested binding, port and devices before
construction. Activation publishes `current` only after target health succeeds.
Interrupted setup stops its candidate, removes any target pointer and incomplete
tree, and retains the rolled-back journal; it does not invent a prior generation.
CLI setup also records `setupRegistration`: absolute `store` and `registry` paths,
prior project settings text in `project` and store-pointer text in `pointer`
(each nullable), and boolean `addedProject`, `addedVolume`, `hadProjects` and
`hadRegistry` flags. Recovery restores local metadata exactly, subtracts only its
own project reference, and removes its volume mapping only if it still names that
store and no projects remain. It locks registry, store and project in that order;
content replacements and removals are durable before the journal becomes terminal.
Cache directories and persistent OS-lock inodes are not deleted.

Managed supervisors also use a private per-instance lifecycle directory at
`runtime/<generation>/<name>.process/<sha256 of instance>/`. Its persistent
`.lock` is held from launch admission through child/listener cleanup, and
`state.json` contains `instance`, `state` and `pid`. States are `pending`,
`running`, `stopped` and `cancelled`. Recovery can cancel an unclaimed pending
launch under the same lock, so a delayed supervisor cannot start afterward.
Only the supervisor writes `stopped`, after cleanup; that receipt survives a
client exit before its binding record is updated. A dead supervisor with a
`running` receipt does not prove its child exited and still refuses mutation.
These private process receipts contain no shutdown token and do not replace
generation metadata, dependency locks or operation journals.

`gc [--plan | --yes] --json` collects obsolete generation trees, retaining current
and its most recent successful journal predecessor rather than the largest two
numbers. Live or ambiguous obsolete bindings refuse collection. The journal's
optional `collectedGenerations` array names distinct positive obsolete numbers;
`previous` and `target` both name current. Trees move durably into
`.garbage/<journal timestamp>/<number>` before the journal becomes complete.
Incomplete collection restores the trees; only complete collection trash is
deleted, with cleanup retried by later confirmed operations. Previously successful
generation numbers are never reused after collection. Project libraries remain
unchanged. A registered store also collects unreferenced digest-named bases,
engine caches, archive downloads and wheel directories, plus its disposable uv
cache. All extant generations in other registered projects retain their
references. Missing projects, damaged retained constraints or unsafe paths refuse
store deletion. Dependency references come from the target provisioner's
read-only `references` result (`wheelHashes`), never a supervisor pack parser.
Pack dependency-lock files are included in the generation inventory; missing
whole pack directories therefore refuse reference collection too.

Optional journal fields `collectionStore` and `collectedStorePaths` name the
absolute store and disposable relative entries (`bases/<sha256>`,
`engines/<sha256>`, `downloads/<sha256>`, `wheels/<sha256>`, `uv-cache`). They are
quarantined under `<store>/.garbage/<journal timestamp>/<relative entry>` before
the same completion write. `<store>/collection` holds the absolute owning journal
path plus newline until recovery/cleanup finishes, so another project can recover
the store before mutation. Registry, store and registered project locks protect
plan revalidation and quarantine. Missing references never imply abandonment.
