Skip to content
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
python3 scripts/test-provider-release-resolution.py
./scripts/sync-install-embed.sh check
./scripts/test-prod-env-refresh.sh
python3 -m unittest discover -s scripts -p test_installer_rollback.py
- name: Test startup observer against local stubs
run: python3 -m unittest discover -s scripts/startup_measurement -t scripts -p 'test_*.py'

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Provider installation retains the previous app or legacy bundle if restoring it after a failed swap also fails. The installer reports the recovery path instead of deleting the only backup or claiming successful restoration.
- Installer downloads use private temporary files and remove incomplete downloads on failure.

## Release candidate v0.9.2 — Gemma QAT caching, adaptive MTP and Nemotron Lightning (not shipped; 2026-09-10)

Source changes since `v0.9.1`. Provider changes require a new signed bundle.
Expand Down
152 changes: 98 additions & 54 deletions coordinator/api/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,64 +185,108 @@ verify_staged_app_payload() {
&& verify_file_hash "$app_bin/mlx.metallib" "$metallib_hash" "App metallib"
}

commit_staged_app() {
local staged_app=$1
local install_dir=$2
local backup="$install_dir/.install-backup-$$-$RANDOM"
local destination="$install_dir/Darkbloom.app"
local had_previous=0
mkdir -p "$backup" "$install_dir/bin"

if [ -d "$destination" ]; then
mv "$destination" "$backup/Darkbloom.app" || {
rm -rf "$backup"
return 1
}
had_previous=1
fi
if ! mv "$staged_app" "$destination"; then
[ "$had_previous" -eq 1 ] \
&& mv "$backup/Darkbloom.app" "$destination" 2>/dev/null || true
rm -rf "$backup"
restore_install_path() {
local previous=$1
local destination=$2
[ -e "$previous" ] || [ -L "$previous" ] || return 0
mv "$previous" "$destination" || {
fail_install "Could not restore $destination; previous installation retained at $previous."
return 1
}
}

# Preserve entries outside the release's payload (including dangling symlinks).
# Work only in staging: copying or preparing a link must not change the live bin.
preserve_extra_bin_entries() (
local previous=$1
local staged=$2
local entry name
[ -d "$previous" ] || return 0
shopt -s dotglob nullglob
for entry in "$previous"/*; do
name=${entry##*/}
if [ ! -e "$staged/$name" ] && [ ! -L "$staged/$name" ]; then
cp -a "$entry" "$staged/$name" || return 1
fi
done
)

# Replace the app and bin as one recoverable operation. Preparation has already
# completed; every successful move is tracked so rollback touches only our paths.
commit_install_paths() {
local install_dir=$1
local staged_bin=$2
local staged_app=${3:-}
local backup i j rollback_failed=0
local -a sources=() names=() saved=() installed=()
backup=$(mktemp -d "$install_dir/.install-backup-XXXXXX") || return 1
if [ -n "$staged_app" ]; then
sources+=("$staged_app")
names+=("Darkbloom.app")
fi
sources+=("$staged_bin")
names+=("bin")
for ((i=0; i<${#names[@]}; i++)); do
saved[i]=0
installed[i]=0
if [ -e "$install_dir/${names[i]}" ] || [ -L "$install_dir/${names[i]}" ]; then
mv "$install_dir/${names[i]}" "$backup/${names[i]}" || break
saved[i]=1
fi
mv "${sources[i]}" "$install_dir/${names[i]}" || break
installed[i]=1
done
if [ "$i" -eq "${#names[@]}" ]; then
# Both live paths are committed. Cleanup cannot undo that success.
if ! rm -rf "$backup"; then
echo " Warning: Installation succeeded, but obsolete backup cleanup failed at $backup. Resolve the filesystem error before removing that directory." >&2
fi
return 0
fi
for ((j=i; j>=0; j--)); do
if [ "${installed[j]}" -eq 1 ]; then
if ! rm -rf "$install_dir/${names[j]}"; then
fail_install "Could not remove failed installation at $install_dir/${names[j]}; recovery backup retained at $backup."
rollback_failed=1
continue
fi
fi
if [ "${saved[j]}" -eq 1 ]; then
restore_install_path "$backup/${names[j]}" "$install_dir/${names[j]}" || rollback_failed=1
fi
done
[ "$rollback_failed" -eq 0 ] && rm -rf "$backup"
return 1
}

local app_bin="$destination/Contents/MacOS"
if ! ln -sfn "../Darkbloom.app/Contents/MacOS/darkbloom" "$install_dir/bin/darkbloom" \
|| ! ln -sfn "../Darkbloom.app/Contents/MacOS/darkbloom-enclave" "$install_dir/bin/darkbloom-enclave" \
|| ! ln -sfn "../Darkbloom.app/Contents/MacOS/mlx.metallib" "$install_dir/bin/mlx.metallib" \
|| ! ln -sfn "darkbloom-enclave" "$install_dir/bin/eigeninference-enclave"
commit_staged_app() {
local staged_app=$1
local install_dir=$2
local staged_bin
# chmod and symlink failures are checked explicitly: callers use `if`/`||`,
# which disables Bash errexit inside the entire function call.
chmod +x "$staged_app/Contents/MacOS/darkbloom" "$staged_app/Contents/MacOS/darkbloom-enclave" || return 1
staged_bin=$(mktemp -d "$install_dir/.install-bin-XXXXXX") || return 1
if ln -s "../Darkbloom.app/Contents/MacOS/darkbloom" "$staged_bin/darkbloom" \
&& ln -s "../Darkbloom.app/Contents/MacOS/darkbloom-enclave" "$staged_bin/darkbloom-enclave" \
&& ln -s "../Darkbloom.app/Contents/MacOS/mlx.metallib" "$staged_bin/mlx.metallib" \
&& ln -s "darkbloom-enclave" "$staged_bin/eigeninference-enclave" \
&& preserve_extra_bin_entries "$install_dir/bin" "$staged_bin" \
&& commit_install_paths "$install_dir" "$staged_bin" "$staged_app"
then
rm -rf "$destination"
[ "$had_previous" -eq 1 ] \
&& mv "$backup/Darkbloom.app" "$destination" 2>/dev/null || true
rm -rf "$backup"
return 1
return 0
fi
chmod +x "$app_bin/darkbloom" "$app_bin/darkbloom-enclave"
rm -rf "$backup"
rm -rf "$staged_bin"
return 1
}

commit_staged_flat_bundle() {
local staged_bin=$1
local install_dir=$2
local backup="$install_dir/.install-backup-$$-$RANDOM"
local destination="$install_dir/bin"
mkdir -p "$backup"
if [ -d "$destination" ]; then
mv "$destination" "$backup/bin" || {
rm -rf "$backup"
return 1
}
fi
if ! mv "$staged_bin" "$destination"; then
[ -d "$backup/bin" ] && mv "$backup/bin" "$destination" 2>/dev/null || true
rm -rf "$backup"
return 1
fi
chmod +x "$destination/darkbloom" "$destination/darkbloom-enclave"
ln -sfn "darkbloom-enclave" "$destination/eigeninference-enclave"
rm -rf "$backup"
chmod +x "$staged_bin/darkbloom" "$staged_bin/darkbloom-enclave" \
&& ln -sfn "darkbloom-enclave" "$staged_bin/eigeninference-enclave" \
&& preserve_extra_bin_entries "$install_dir/bin" "$staged_bin" \
&& commit_install_paths "$install_dir" "$staged_bin"
}

install_bundle_atomically() {
Expand Down Expand Up @@ -288,7 +332,7 @@ install_bundle_atomically() {
}
commit_staged_app "$stage/Darkbloom.app" "$install_dir" || {
rm -rf "$stage"
fail_install "Atomic app swap failed; previous install was restored."
fail_install "App swap failed."
return 1
}
else
Expand All @@ -308,7 +352,7 @@ install_bundle_atomically() {
fi
commit_staged_flat_bundle "$flat_bin" "$install_dir" || {
rm -rf "$stage"
fail_install "Atomic flat-bundle swap failed; previous install was restored."
fail_install "Flat-bundle swap failed."
return 1
}
fi
Expand Down Expand Up @@ -397,7 +441,8 @@ echo ""
echo "→ [2/5] Downloading Darkbloom v${VERSION}..."
mkdir -p "$INSTALL_DIR" "$BIN_DIR"

TARBALL="/tmp/darkbloom-bundle.tar.gz"
TARBALL=$(mktemp "${TMPDIR:-/tmp}/darkbloom-bundle.XXXXXX")
trap 'rm -f "$TARBALL"' EXIT
curl -f#L "$BUNDLE_URL" -o "$TARBALL"

ACTUAL_HASH=$(shasum -a 256 "$TARBALL" | cut -d' ' -f1)
Expand All @@ -406,18 +451,17 @@ if [ "$ACTUAL_HASH" != "$BUNDLE_HASH" ]; then
echo " ✗ Bundle hash mismatch — refusing to install possibly-tampered binary."
echo " Expected: $BUNDLE_HASH"
echo " Got: $ACTUAL_HASH"
rm -f "$TARBALL"
exit 1
fi
echo " Bundle hash verified ✓"

echo " Staging and verifying the complete app before touching the live install ..."
if ! install_bundle_atomically "$TARBALL" "$INSTALL_DIR" "$BINARY_HASH" "$METALLIB_HASH"; then
rm -f "$TARBALL"
echo " Existing installation was left unchanged."
echo " Installation failed; check the messages above for any retained recovery backup."
exit 1
fi
rm -f "$TARBALL"
trap - EXIT
echo " Strict signature, runtime resources, and atomic swap verified ✓"

# Make available in PATH. Try /usr/local/bin symlink, fall back to shell rc.
Expand Down
9 changes: 8 additions & 1 deletion docs/developer/build.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Build

> Last updated: 2026-09-10 · commit `4f29957d2`
> Last updated: 2026-09-11 · commit `75a7185dc`

How to build every component of Darkbloom from a fresh clone: the Go
coordinator, the Rust prompt-contract sidecar, the Swift provider CLI (with its
Expand All @@ -15,6 +15,13 @@ Profiler wire changes require both coordinator and provider builds; the shared
Go/Swift fixture and focused checks are described in [test.md](test.md) and
[prediction telemetry](../reference/prediction-decision-telemetry.md).

Installer changes start in `scripts/install.sh`. Regenerate the coordinator's
embedded copy with `scripts/sync-install-embed.sh`, then run
`scripts/sync-install-embed.sh check` and the offline installer fixtures in
[test.md](test.md). The commit helpers prepare executable permissions and bin
links before replacing live paths; app and bin replacement share rollback so
failed restoration retains a recovery backup.

## Prerequisites

- **Toolchain via [`mise`](https://mise.jdx.dev/).** Every version is pinned in
Expand Down
12 changes: 11 additions & 1 deletion docs/developer/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test

> Last updated: 2026-09-11 · commit `d22ad0cf3`
> Last updated: 2026-09-11 · commit `e9018bb41`

How to run the unit tests for each component, the end-to-end suite that boots a
real coordinator + Swift provider against ephemeral Postgres, and the docs
Expand Down Expand Up @@ -911,6 +911,16 @@ procedure, its inputs and the regeneration flow are in
install/replace path of `scripts/install.sh` in a temp dir (and runs
`scripts/sync-install-embed.sh check` first).

`python3 -m unittest discover -s scripts -p test_installer_rollback.py` injects
Comment thread
Gajesh2007 marked this conversation as resolved.
Resolved
permission, link, copy and rename failures into app and legacy-flat commit
functions under conditional invocation (where Bash disables `errexit`). It checks
unchanged live paths after preparation failures, rollback of both app and bin,
relative/dangling symlink restoration, retention after rollback failure, unrelated
bin entries, successful replacement despite obsolete-backup cleanup failure,
and private download-file permissions and cleanup.
These fixtures use temporary directories without downloads, signing, enrollment,
or running the provider; Release Integrity CI runs them on Linux.

### 5. Console UI and Admin UI

```bash
Expand Down
17 changes: 16 additions & 1 deletion docs/operations/provider-release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release a provider version

> Last updated: 2026-09-10 · commit `5f021ba4d`
> Last updated: 2026-09-11 · commit `e9018bb41`

Runbook for shipping a new `darkbloom` provider CLI: bump the two version
constants, land the changelog, push a `vX.Y.Z` tag, approve the `prod`
Expand Down Expand Up @@ -321,6 +321,21 @@ curl -fsS "$COORD/v1/admin/releases" -H "Authorization: Bearer $ADMIN_KEY" | jq

## Rollback

For a local installer swap failure, `scripts/install.sh` attempts to restore the
previous app and bin paths, including directory symlinks. Permissions and bin
links are prepared before replacement, and unrelated bin entries are preserved.
If a restoration rename fails, it leaves that old payload
in the reported `.install-backup-*` directory and exits unsuccessfully. Preserve
that directory, resolve the reported filesystem error, and restore its
`Darkbloom.app` and/or `bin` payload before retrying. Keep relative symlinks as
symlinks when moving them back to their original path. Installer output does not claim
that a failed restoration left the live installation unchanged.

If replacement succeeds but obsolete backup cleanup fails, installation continues
successfully and reports the leftover path. The new app and bin are already
active: resolve the cleanup error before removing that obsolete backup; this
warning does not call for restoring the previous installation.

A registered release is immutable (hash-pinned); rollback means **deactivating
it** so the previous active version becomes "latest" again.

Expand Down
28 changes: 18 additions & 10 deletions docs/provider/installation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Install, update, and uninstall the provider

> Last updated: 2026-09-03 · commit `5d400cf75`
> Last updated: 2026-09-11 · commit `e9018bb41`

How to put the `darkbloom` CLI on an Apple Silicon Mac with `scripts/install.sh`,
what the script verifies before it touches an existing install, how the binary
Expand Down Expand Up @@ -78,17 +78,25 @@ The script performs these actions in order (`scripts/install.sh`; failures exit
`DARKBLOOM_NO_UPDATE_CHECK=1 DARKBLOOM_GEMMA4_PREFILL_CHUNK_EVAL=18 MLX_GEMMA4_FUSED_WEIGHTED_UNSORT=1 MLX_GATHER_QMM_EXPERT_SLICES=1 darkbloom runtime-smoke`
(`provider-swift/Sources/darkbloom/RuntimeSmokeCommand.swift`,
`RuntimeSmoke`);
- `commit_staged_app` moves any existing `~/.darkbloom/Darkbloom.app` to
`~/.darkbloom/.install-backup-<pid>-<random>`, moves the staged app in,
writes the symlinks `~/.darkbloom/bin/darkbloom`, `darkbloom-enclave`,
`mlx.metallib` → `../Darkbloom.app/Contents/MacOS/*` and the legacy alias
`bin/eigeninference-enclave → darkbloom-enclave`, and `chmod +x`. Any
failure moves the backup back;
- `commit_staged_app` prepares executable permissions and the staged bin
symlinks (`darkbloom`, `darkbloom-enclave`, `mlx.metallib` and the legacy
`eigeninference-enclave` alias), preserving unrelated existing bin entries.
`commit_install_paths` then moves the old app and bin into a private
`~/.darkbloom/.install-backup-*` directory and installs the staged paths;
- a tarball without `Darkbloom.app` (legacy flat layout) gets
`codesign --verify --strict -R=…` on `bin/darkbloom` and
`commit_staged_flat_bundle` swaps `~/.darkbloom/bin` the same way;
- the staging directory is removed; on any failure the script prints
`Existing installation was left unchanged.` and exits 1.
`commit_staged_flat_bundle` prepares and swaps the bin with the same
recovery mechanism;
- preparation or verification failures leave the live installation unchanged.
A replacement failure reverses completed moves before exiting 1. If a
restoration rename or candidate removal also fails, the affected live path
may be absent or incomplete: the installer retains the previous copy in
`.install-backup-*` and prints its recovery location. Keep that directory
and follow [installer rollback](../operations/provider-release.md#rollback)
before retrying or removing any backup. Relative and dangling symlink
backups are retained and restored as links. Once both live paths are installed,
backup cleanup failure emits a warning with the obsolete backup path and
continues successfully; the new installation remains active.
4. **PATH.** `ln -sf ~/.darkbloom/bin/darkbloom /usr/local/bin/darkbloom`
(errors ignored). The rc file is `~/.zshrc`, or `~/.bashrc` only when
`~/.zshrc` does not exist. If the rc does not already mention
Expand Down
Loading
Loading