Skip to content

feat(engine): pinned, sha256-verified Zen bootstrap and offline inference - #174

Merged
zeekay merged 12 commits into
mainfrom
claude/ecstatic-ride-qskgy4
Nov 29, 2026
Merged

zeekay merged 12 commits into
mainfrom
claude/ecstatic-ride-qskgy4

Conversation

@jamessburnss1313-dev

Copy link
Copy Markdown
Contributor

Part of #163.

hanzo-engine zen picks a Zen model that fits the machine, installs it, verifies every byte and runs it with no network. The pins are compiled into the binary: each one fixes a repo, a 40-hex commit, and the size and sha256 of every file (the weights, LICENSE and NOTICE). Files live in the shared Hugging Face hub cache, in the hub's own layout. -m zen:auto or -m zen:<pin> loads only local files that have just been verified.

What it does

  • Pick: zen select takes the highest-ranked pin that fits the free memory. On a unified-memory device (Metal, integrated CUDA) the device counts no more than free RAM. --cpu counts RAM only.
  • Pull and resume: our own downloader, not hf-hub's.
    • It writes only to blobs/<pinned sha256>.part and never reads past the pinned size.
    • It resumes with Range, and appends a 206 only when its Content-Range starts at the byte it asked for.
    • A resumed file that fails its digest is fetched once more from byte 0.
    • Free disk must cover what is still missing plus 512 MiB.
  • Import and export: offline bundles. A bundle manifest names only the pin and commit and carries no digests; every byte is checked against the binary's pins.
  • Activate and roll back: zen use, zen rollback. State is ~/.cache/hanzo/zen/state.json, replaced by rename. Commands that change it hold state.json.lock.
  • Load:
    • Checks memory, re-hashes every file, then rebuilds hanzo-zen/<pin>/ as hard links to exactly the pinned files.
    • Sets hub access local-only for the whole process, so no zen: load can fall back to a download or a hosted model.
    • A symlink at hanzo-zen/, at hanzo-zen/<pin> or inside it is removed, never followed.
    • tune, quantize, train, distill and the non-text model kinds refuse zen:.
  • Pins: zen6-nano-4b-pq2 (needs 3.00 GiB) and zen6-nano-9b-pq2 (needs 4.25 GiB), both Apache-2.0. Each figure is the highest measured CPU peak x 1.25, rounded up to 256 MiB. For the 9B that peak is 3,295 MiB, from a 2,048-token prompt.
  • hanzo.yml gains a zen-bootstrap gate. Docs: guides/models/run-zen-offline.md, reference/cli.md; notes in LLM.md.

Proof

All from the release binary of b678be2 (hanzo-engine sha256 0b7fda4d...fc12), on a 4-core Xeon 2.10 GHz with 16 GB RAM, no GPU, starting from an empty cache. The two commits after it only move a test import and add two #[allow] lint attributes, so they change no code path.

Check Result
Pinned digests All 6 files downloaded independently with curl at the pinned commits match catalog.json in sha256 and size
zen pull zen6-nano-4b-pq2 37.3 s, 3 files verified, activated, 26 MiB peak RSS
Pull again 2.1 s, re-hashed, nothing downloaded
Tampered bundle (one byte flipped at offset 4096) imported into another cache Refused: failed verification ... has sha256 4607229f... (1146465344 bytes), the pin is f7cbf3f9... (1146465344 bytes). 0 files left behind
Missing model, inside unshare -rn Error: zen6-nano-4b-pq2 is not installed: ... Run hanzo-engine zen pull ... or zen import, exit 1, no download
4B serve -m zen:zen6-nano-4b-pq2 --cpu --host 127.0.0.1 --no-advertise inside unshare -rn (loopback only), temperature 0 "Paris" (6.8 s); 17 x 23 -> "391" (8.5 s). Peak RSS 1,867 MiB. strace -f -e connect: 0 connect() calls
zen pull zen6-nano-9b-pq2 71.6 s, verified, activated (previous: 4B)
9B served the same way "Paris" (8.5 s); "391" (10.9 s). Peak RSS 2,821 MiB. 0 connect() calls
zen use / zen rollback twice Swaps 4B <-> 9B both ways
cache delete -m zenlm/zen6-nano-4b Freed 1,093 MiB; hanzo-zen/zen6-nano-4b-pq2/ removed

A sanity check confirms the strace capture does log connects. A no-network curl under the same capture logs 3.

Every review fix has a test that fails without it. The new tests were run against 4532ccd (before the fixes): 6 lib tests and 3 zen_pull tests failed. On this branch:

  • --lib zen::: 47 passed.
  • zen_pull: 14 passed.
  • zen_offline: 1 passed.
  • hanzo-cli zen: 8 passed.

The hanzo.yml gates, run locally (GitHub Actions is off on this repo, and the forge gates only main and v*):

  • v1-only: pass.
  • fmt: pass.
  • kv-isolation: pass (9 + 9 + 12 tests).
  • zen-bootstrap: pass (47 + 14 + 8 tests).
  • check --workspace: pass.
  • Clippy (--no-deps): no lint in any file this PR touches. hanzo-cli passes. hanzo-engine still fails clippy on 7 errors that predate this branch (in models/zen6.rs, attention/backends/lsa.rs, paged_attention/mod.rs and others); this PR adds none.

Review

An independent Opus red review of the full change found 16 items. The blockers and should-fixes are fixed here, each with a failing-first test:

  • deleting or writing outside the cache through symlinks (load dir, .part, .import);
  • concurrent loads deleting each other's link temps;
  • the import preflight missing a same-size corrupt file;
  • a 206 at the wrong offset being appended, or a bad resumed part failing the pull;
  • the Metal memory budget over-counting;
  • --cpu ignored by the zen commands;
  • tune advertised but broken;
  • cache delete leaving hard links that kept the blob on disk;
  • state.json read-modify-write races;
  • bundle digests that nothing read;
  • docs claiming a commit check the code did not do.

A second review of the fix delta found three should-fixes, now fixed with tests:

  • a link at hanzo-zen/ itself;
  • read-only zen commands blocking behind a pull;
  • untested 206 arms.

The review notes are posted below.

Nits left as they are:

  • Re-importing a healthy pin hashes it twice.
  • cache delete matches the repo name case-sensitively.
  • On Windows, a junction in the load dir fails the load closed rather than being removed.
  • The CLI's ModelType has no Debug, so its tests use .err().

Not done here (remaining scope of #163)

  • Model signatures. The binary is the only trust root. A signature needs a signing key and a release-signing decision.
  • Loopback auth on serve. serve still binds 0.0.0.0 and advertises over mDNS by default. The guide says to pass --host 127.0.0.1 --no-advertise.
  • Windows/Linux accelerator inventory beyond what doctor reports, and GPU memory peaks. The memory_bytes figures are CPU peaks.
  • 8 GB Apple Silicon first-token latency, memory peak, throughput and thermal receipts. Those need the hardware.
  • Request-driven egress. Web search, image URLs and MCP still reach the network when someone uses them. This is documented, not blocked.

Generated by Claude Code

hanzo_engine::zen: embedded catalog of zen6-nano pins (commit, sha256,
licence files), hardware selection, disk preflight, verified resumable
pull into the shared Hugging Face cache, offline bundle export/import,
activate and rollback. hanzo-engine zen <list|select|pull|verify|use|
rollback|export|import> and -m zen:<id|auto>, which loads only the
verified local snapshot.

Memory requirements in catalog.json are provisional until measured.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
…ed variants

The sync hub client resumes from blobs/<etag>.part, not .incomplete, so
missing_bytes ignored a partial download. The pull tests move to their
own binary, which clears proxy variables the hub client would otherwise
send localhost through. zen6-nano-2b is dropped: it answers known-answer
prompts wrongly in both quant formats. Memory figures still provisional.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
… local

Review fixes. hf-hub built blob and pointer paths from server headers, so
pull now uses its own downloader: blobs/<pinned sha256>.part only, at most
the pinned size, Range resume. Resolving zen: forces hub access offline
for the process; quantize, train and distill refuse zen:. Verify refuses
unpinned files in the snapshot; --cpu budgets RAM only; the state file is
synced and its errors name the fix; import checks disk; replacing an entry
only deletes the pin's own blob. Measured memory figures in the catalog.

Tests for these changes are running; not yet confirmed green.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
…CI gate

The GGUF loader reads optional configs beside the weights, and the shared
snapshot may hold other hub clients' files, so zen: now loads from
<cache>/hanzo-zen/<pin>/, hard links to exactly the verified files, instead
of refusing neighbours. Hub errors name a zen: model as the reason access
is local-only. Import's disk check no longer credits download parts.
hanzo.yml runs the zen tests as a gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
rename(2) does nothing when both names already link the same file, so the
second load of an unchanged pin left LICENSE.link.<pid> and friends in its
load directory. The temp link is now removed after the rename.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
Measured at 2,048 prompt tokens the 9B peaks at 3,295 MiB, not the 2,813
its short prompts reached, so its requirement is 4.25 GiB, not 3.5.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P5YBduxQAc4tdxANUmrkHx
- prepare_load and cache delete never list, write or remove through a symlink;
  the .part and .import temps are created create_new and appended O_NOFOLLOW.
- A 206 is appended only when Content-Range starts at the requested byte; a
  resumed file that fails its digest is fetched once more from 0.
- Loads of one pin lock hanzo-zen/<pin>.lock; zen commands lock state.json.lock.
- Import sizes its disk preflight from files that fail verification.
- Unified-memory devices count no more than free RAM; zen subcommands take --cpu.
- tune refuses zen: (nothing to tune in a pre-quantized GGUF).
- Bundle manifests carry no digests; docs and notes match the code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01T1hoMwU8cnJHjJncdB1mgM
- A symlink at hanzo-zen/ is removed before a load or cache delete touches
  anything under it; cache delete skips pins that were never loaded.
- zen list/select/verify/export take no state lock, and a waiting writer says
  so; the lock file is state.json.lock as documented.
- Test 206 answers at another offset and without Content-Range.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01T1hoMwU8cnJHjJncdB1mgM
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-29T18:44:19.360939Z
We couldn’t complete this review. Comment @codex review to try again.
9fd20a4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@jamessburnss1313-dev

Copy link
Copy Markdown
Contributor Author

Red review 1 of 2: full change at 4532ccd

Ranked findings from an adversarial read of origin/main...4532ccd (no cargo run). Each line gives the finding, then its outcome on the branch.

  1. Blocker (scope): the PR claimed to resolve [P0] Sovereign AI OS: Hardware-aware verified Zen model bootstrap and offline inference #163, but signatures, loopback auth on serve (it binds 0.0.0.0 and advertises over mDNS), streaming health, the accelerator inventory, the 8 GB Apple Silicon receipts and rollback across binary updates were not done. Outcome: handled by titling the PR "Part of [P0] Sovereign AI OS: Hardware-aware verified Zen model bootstrap and offline inference #163" and listing the remaining scope.
  2. Should-fix: zen/store.rs:445-463,105-108: list_files follows symlinks, so a symlinked dir in hanzo-zen/<pin>/ makes prepare_load delete files outside the cache. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  3. Should-fix: zen/store.rs:357-363,499: the .part and .import temps are opened through symlinks, so a planted link gets truncated or written outside the cache. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  4. Should-fix: zen/store.rs:105-126: in concurrent prepare_load of one pin, one process's cleanup deletes the other's *.link.<pid> temp, so the second load fails. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  5. Should-fix: hanzo-cli/src/commands/cache.rs:86-103: cache delete leaves the hanzo-zen/<pin> hard links, so no space is freed, contrary to the docs. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  6. Should-fix: hanzo-engine/src/tuning.rs:477-484: tune -m zen:... resolves to GGUF, which auto_tune refuses, yet the docs and refuse_zen listed tune as supported. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  7. Should-fix: zen/select.rs:15-25 with utils/memory_usage.rs:136-140: Metal "available" subtracts only this process's allocations, so an 8 GB Mac selects the 9B whatever RAM is free. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  8. Should-fix: hanzo-cli/src/commands/zen.rs:23: zen select/pull/use/rollback ignore --cpu, so a pull can activate a pin that run --cpu refuses. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  9. Should-fix: zen/store.rs:158-163,173-176: import's disk preflight (absent_bytes) misses same-size corrupt files and duplicates missing_bytes; zen list shows such a pin as "downloaded". Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  10. Should-fix: missing tests for the offline forcing, the resolve_zen_model rewrite, 206 offsets and from-config mixed entries; the hanzo-cli zen tests were not in the hanzo.yml gate. Outcome: tests were added for the resolve rewrite, offline forcing and 206 offsets, and the CLI tests are now in the hanzo.yml gate. The from-config mixed-entry test was not added, because it needs a model load.
  11. Nit: run-zen-offline.md: the docs said a server answering with a different commit is refused, but no commit is checked, only bytes. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  12. Nit: zen/store.rs:221-225: a bad resumable .part, or a 206 without a Content-Range check, fails the first pull against an honest hub. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  13. Nit: zen/state.rs: state.json read-modify-write has no lock, so concurrent use/pull can drop the rollback target. Outcome: fixed by the state.json lock.
  14. Nit: zen/bundle.rs:92: the manifest files field is validated but never used. Outcome: fixed in e626e3f/a1632bf with a failing-first test.
  15. Nit (style): multi-line /// blocks in store.rs and zen/mod.rs; the resolve_zen_model doc and LLM.md said "snapshot" where the load dir is meant. Outcome: fixed; comments shortened to one line.
  16. Nit: request-driven egress (--enable-search, image URL fetch, /v1/research, MCP) stays possible under zen:. Outcome: documented in the guide, not blocked.

Generated by Claude Code

@jamessburnss1313-dev

Copy link
Copy Markdown
Contributor Author

Red review 2 of 2: fix delta 4532ccd..e626e3f

Code read only; I did not run cargo.

Findings, ranked

  1. should-fix, CONFIRMED. Nothing checks whether hanzo-zen/ itself is a link (store.rs:112 checks only hanzo-zen/<pin>). If hanzo-zen points elsewhere, prepare_load prunes files in <target>/<pin>/ and cache delete runs remove_dir_all there (std applies O_NOFOLLOW only to the last path component). LLM.md's "nothing ... through a symlink" claim was too broad.
  2. should-fix, CONFIRMED. Every zen subcommand took the exclusive state lock (zen.rs:22-23), including list, select, verify and export, so they blocked silently behind a whole pull. This is not a deadlock: nothing nests the lock, and no cycle is possible with the load lock.
  3. nit, CONFIRMED. with_extension("lock") produced state.lock, while the docs said state.json.lock.
  4. should-fix (test gap), CONFIRMED. Range::FromZero exercises only the Some(0) arm. Nothing tested a 206 at a wrong non-zero offset or a 206 without Content-Range, the arm that discards the part and restarts.
  5. nit, CONFIRMED. Import preflight (absent_bytes) hashes every file, then install hashes each one again, so a re-import of a healthy pin hashes it twice.
  6. nit, PLAUSIBLE. cache delete matches p.repo == model_id case-sensitively, which can miss load dirs on a case-insensitive filesystem. It also created hanzo-zen/ and lock files for pins that were never loaded.
  7. nit. On Windows a directory symlink or junction fails closed rather than being removed, and O_NOFOLLOW exists on Unix only.

Clean: .part/.import temps (create_new, O_NOFOLLOW append), the per-pin load lock, the same-size-corrupt preflight, the 206 offset check and the one-time refetch of a bad resumed part, the unified-memory cap, --cpu on zen, the tune refusal, the remove_dir_all semantics, the state RMW lock and dropping manifest digests. All 9 new tests named as failing on 4532ccd do fail there, judging from the code.

Outcomes on the branch


Generated by Claude Code

@zeekay
zeekay merged commit 4a6cd9a into main Nov 29, 2026
1 check passed
@zeekay
zeekay deleted the claude/ecstatic-ride-qskgy4 branch November 29, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants