Skip to content

CLI

python -m machaon.cli <command>

first

The shortest path from zero. If --model does not exist, it downloads the selected starter (SmolLM2-135M by default) with the same endpoint fallback and header validation as machaon fetch; then it opens the checkpoint, teaches one sentence, and saves the accepted edit.

machaon first
machaon first --say "The capital of France is -> Berlin"
machaon first --model ./models/Qwen2.5-0.5B --out ./models/qwen-first
machaon first --program-out ./first.machaon

If the optional real-model runtime is missing, first fails before downloading anything and names the install command (pip install "machaon[hf]"). An interrupted checkpoint download is recognised before the tokenizer loads, so the error says "rerun machaon fetch to resume it" instead of showing a library traceback. The default fact is "The Eiffel Tower is in the city of -> Rome". The default output is <model>-machaon. Use --no-save to keep the edit in memory only; --json prints the full result. The command exits 0 when the fact was applied or already true, 1 when the local checks rejected it, and 2 on malformed input or a model error. --program-out PATH writes a matching portable program before the run; the generated program uses paths relative to its own file and includes the starter only when the model is missing. --program-name sets its title and --program-force replaces an existing program file. A successful run also prints the exact program-new command, so the same run can be reproduced even without the file. That command uses paths relative to the working directory (./models/...), so the generated program stays portable.

After saving, first reloads the written checkpoint and checks the taught fact again. The summary and a verified: line report PASS/FAIL; a saved checkpoint that no longer answers exits 1 with status verification_failed; a checkpoint that answers but keeps less than the policy margin exits 1 with margin_failed. Pass --no-verify-saved to skip that second load.

check

Reloads a saved checkpoint and evaluates one prompt/answer pair without editing anything. It tries the answer with and without the continuation space, reports the variant that matches the model's next-token prediction, and exits 0 only when the target token is reproduced and the target-logit margin meets the default 0.25 policy.

machaon check --model ./models/qwen-first \
  --say "The Eiffel Tower is in the city of -> Rome"
machaon check --model ./models/qwen-first \
  --prompt "The Eiffel Tower is in the city of" --answer Rome --json
machaon check --model ./models/qwen-many/final --facts facts.txt

The default --dtype auto reads the stored checkpoint dtype. --quantize requires --dtype auto. Quantized inference compiles a small C++ extension on first use; if ninja is missing, the CLI names pip install "machaon[hf,quantize]". The pip-installed ninja binary is added to PATH automatically. The JSON output includes passed, accuracy, answer_text and the raw metrics; the human form prints PASS or FAIL and the target-logit margin when it is available. --facts loads the list once and checks every entry, exiting 1 if any fact fails. The human table prints each fact's margin and marks the low row. --min-margin 0.25 also requires every saved fact to keep at least that target-logit margin; missing margins fail the gate. --no-margin-gate reports accuracy alone. The JSON adds min_margin, observed_min_margin, margin_passed and overall_passed when a policy applies. status becomes margin_failed when the answer reproduced but the margin gate failed; each fact in a list carries the same per-row status. The human output also prints the resolved device, dtype and quantize; the same saved bytes can report a slightly different margin on CPU and MPS.

fetch

Downloads a safetensors checkpoint without importing torch or loading a weight tensor. It lists the repository through Hugging Face, then hf-mirror, then ModelScope, and validates every .safetensors header before accepting it. Existing files are reused, so an interrupted download can resume.

machaon fetch --list
machaon fetch --starter qwen05b --dest ./models/Qwen2.5-0.5B
machaon fetch --starter smol135m --dest ./models/SmolLM2-135M
machaon fetch --repo Qwen/Qwen2.5-0.5B --dest ./models/qwen

--endpoint huggingface|hf-mirror|modelscope forces one endpoint. --json prints the file list, shard count, tensor count and parameter count. A repository with no .safetensors weights is refused rather than left as a half-downloaded directory.

start

Prints the shortest path to a first edit and reports which optional runtimes are installed:

python -m machaon.cli start
python -m machaon.cli start --demo
The capability block reports the available compute devices, e.g. devices cpu default · mps available followed by the matching --device mps hint.

web

Launch the optional local browser console from the main CLI. It forwards the same host, port, allowed-root, token and loopback safeguards as machaon-web; install the web extra first.

pip install "machaon[web]"
machaon web --allowed-root ./models
machaon web --allowed-root ./models --port 8770 --no-browser

intent

Compiles a plain-language teaching request into an exact prompt / answer contract without loading a model. Exit code 2 means the text did not determine one pair; the error lists accepted forms.

python -m machaon.cli intent "The Eiffel Tower is in the city of -> Rome"
python -m machaon.cli intent "教会模型:北京的首都是 → 北京" --json

Arrow and labelled forms are exact. Plain sentences are also accepted and are marked medium confidence because the split point is an assumption. The command prints the equivalent machaon teach command.

teach

Runs the full teaching recipe: baseline, depth-window layer search, apply, target/panel/KL checks, and rollback on failure.

python -m machaon.cli teach --model ./models/Qwen2.5-0.5B \
  --say "The Eiffel Tower is in the city of -> Rome"

The default output is a sibling directory named <checkpoint>-machaon. The source checkpoint is never modified. The human output reports the resolved compute device (device : mps:0), so a run never hides whether it used CPU or GPU. Common controls:

flag meaning
--say TEXT compile the request with machaon intent
--prompt P --answer A supply the contract explicitly
--layers 8,10,12 / --layers 8-12 choose candidate layers
--max-layers 7 default candidate count for the depth window
--solver direction\|gradient value solver for each candidate
--max-rel-norm 0.02 per-edit relative-norm budget
--budget 0.10 cumulative session budget
--max-kl 0.05 KL p95 acceptance threshold
--min-margin 0.25 policy target-logit margin over the runner-up; the solver aims at 2× this to leave storage headroom
--min-panel-agreement 0.875 fraction of unrelated prompts that must keep top-1
--preserve-file FILE newline-separated baseline/projection prompts
--corpus-file FILE newline-separated texts for an optional perplexity check
--out DIR edited checkpoint directory (refused if it exists)
--no-save keep the edit in memory only
--dtype auto\|float32\|float16\|bfloat16 model dtype; auto reads the checkpoint's declared dtype (default)
--no-verify-saved skip reloading the saved checkpoint for a fact check
--dry-run search and report, write nothing
--evidence-out DIR write a WSP evidence bundle with the unedited headroom
--edit-out DIR export the applied delta as OpenWSP v1
--report-out FILE write the full recipe report JSON
--json machine-readable output

Exit codes: 0 applied / already true / dry run, 1 rejected and rolled back or the saved bytes failed verification, 2 malformed input or model error.

After saving, teach reloads the written checkpoint and checks the fact again. The verified: line reports PASS/FAIL and verified margin reports the smallest target-logit margin among the saved facts; a failed reload sets the status to verification_failed and exits 1. A saved answer with a margin below --min-margin is reported as margin_failed. This catches edits whose margin does not survive the checkpoint's storage dtype (for example, a float32 solve written to a bfloat16 checkpoint).

--out, --evidence-out and --edit-out are refused when the target already exists, so a run never overwrites an earlier artifact.

teach-many

Teach a list of facts into one checkpoint chain. Each fact runs the full single-fact recipe on a fresh session loaded from the previous accepted checkpoint; the original checkpoint is never modified.

machaon teach-many --model ./models/Qwen2.5-0.5B \
  --facts facts.txt --out ./models/qwen-many

facts.txt contains one teaching sentence per line (the same grammar as machaon intent) or a JSON / JSONL list of {"say": ...} or {"prompt": ..., "answer": ...} objects. The command keeps the last globally safe checkpoint at <out>/final, writes per-step evidence to <out>/report.json, and exits 1 when a fact is rejected or the cumulative drift check fails. --continue-on-reject records the failure and continues; --keep-steps keeps every step rather than only the last safe one; in that mode the final is the last step-NNN directory instead of <out>/final. The cumulative --budget is enforced across the chain, not per fact.

program-new

Generate a reviewable text program from one or more teaching sentences. The generated file is parsed by the same validator used by machaon program, so a generator bug cannot introduce a second format:

machaon program-new ./hello.machaon \
  --teach "The Eiffel Tower is in the city of -> Rome" \
  --protect "Two plus two equals"

machaon program-new - \
  --model ./models/Qwen2.5-0.5B \
  --teach "The capital of France is -> Paris"

Defaults are --model ./models/SmolLM2-135M, fetch: smol135m, and --output <model>-machaon. A custom --model has no implicit fetch; add --fetch smol135m|qwen05b|gpt2 or --no-fetch explicitly. Local absolute paths are rewritten relative to the generated file, so the program stays portable. --teach and --protect are repeatable, or use --facts FILE and --protect-file FILE for one entry per line; blank lines and # comments are ignored. The generator also exposes --layers, --solver, --steps, --lr, --max-layers, --max-kl, --min-panel-agreement, --dtype, --device, --quantize, --min-margin, --no-verify, --continue-on-reject and --keep-steps, so the common advanced fields do not require a manual edit. Path - writes to standard output. An existing target is refused unless --force is passed.

program

Plan or run a portable weight program. A program is a single text or JSON file that declares the model, facts, protected prompts, budget and runtime policy. Relative model and output paths resolve from the program file, so the same file can be shared and reviewed.

machaon program examples/hello.machaon --plan
machaon program examples/hello.machaon
machaon program ./lesson.json --out ./models/experiment-2

--plan parses and prints the exact contract without downloading or loading a model. It also reports whether the output is new, empty and usable or already blocked, the report path, the starter name/size that would be downloaded, and whether a requested CUDA/MPS device is visible (CPU is the default). A run delegates the edits to teach-many, writes <output>/program-report.json, and by default reloads the saved checkpoint and checks every fact. --no-verify skips that final reload; --json prints the whole plan or run for another tool. Per-fact progress is printed by default; --quiet suppresses it. --report-out PATH writes the program report to a chosen file instead of <output>/program-report.json. When a run is rejected, the human-readable output expands the failed checks (target, budget, panel or KL) and the recorded reason instead of only printing rejected. After a saved-byte check it also prints the smallest margin and whether it met the program policy, plus the resolved device.

Exit codes are 0 for a verified/applied program (or a valid plan), 1 for a rejected, verification-failed, or low-margin run, and 2 for a malformed program or model error. A run whose saved facts reproduce but fall below the program's min_margin is reported as margin_failed. See Weight programs for the format and field table.

program-verify

Verify a saved program-report.json without trusting it. The default check reads the report, validates its required structure, and recomputes the report_sha256 content address:

machaon program-verify ./models/hello-weight-space/program-report.json
machaon program-verify ./models/hello-weight-space/program-report.json \
  --program hello.machaon
machaon program-verify ./models/hello-weight-space/program-report.json \
  --check-model
# read a report from stdin instead of a file:
cat program-report.json | machaon program-verify -

--program PATH additionally requires the program file's SHA-256 to match the program address recorded in the report. --check-model reloads the reported checkpoint, re-runs every declared fact, and compares the live verdict with the recorded one. If the checkpoint moved, pass --check-model --checkpoint ./new/path/final; the report path remains a provenance field rather than a required location. --device cpu overrides the device recorded in the report, so a CUDA or MPS report can be rechecked on another machine; --dtype float32 does the same for the recorded dtype. The model check also reports the smallest saved-byte margin and whether it meets the program's min_margin policy; a live margin below that policy fails the command, and --no-margin-gate reports it without failing. Without a model load, the report's recorded margin is printed when the run captured one. The model check also prints the resolved device, dtype and quantize. The human output ends with a single verification verdict; with the gate waived it reads verification: FAIL (margin waived). The report's own status and accepted verdict are labeled (recorded).

--json prints the complete verification result. Exit code 0 means the report is intact, any requested model check passed, and the live margin met the program policy; 1 means a requested saved-byte check failed or the margin fell below policy; and 2 means the report is malformed or its address does not match. With --json, an integrity error is returned as {"valid": false, "error": ...} so a caller can parse failures too. The SHA-256 detects modification; it is not a signature.

demo

Runs the synthetic rank-one demo. No model, no network.

python -m machaon.cli demo

validate

Validates an edit spec. Exit code 1 on an invalid spec.

python -m machaon.cli validate examples/edit_spec.json
# OK  rank1_constrained -> model.layers.14.mlp.down_proj (budget 0.020)

gate

Applies the acceptance gates to a metrics JSON file. Exit code 1 when the edit should not be committed — which makes it usable as a CI step.

python -m machaon.cli gate examples/metrics_pass.json    # exit 0
python -m machaon.cli gate examples/metrics_fail.json    # exit 1
FAIL
  paraphrase_acc = 0.41 violates >= 0.6
  kl_p95 = 0.113 violates <= 0.05
  ...

wsp

Applies the WSP acceptance protocol to a JSON report. Exit code 0 only when the edit is a gate-passing repair or acquisition; every other label exits 1.

python -m machaon.cli wsp examples/wsp_report.json

The report contains base, candidate, optional controls, optional headroom, optional mechanism, optional gates, and optional protocol objects. The full classification, control/headroom gaps and mechanism gate are printed as JSON.

--save DIR writes a content-addressed evidence bundle; --metadata FILE adds a JSON object to it.

python -m machaon.cli wsp examples/wsp_report.json --save evidence/
python -m machaon.cli wsp-verify evidence/

WSP benchmark

python -m machaon.cli wsp-bench examples/wsp_benchmark.json
python -m machaon.cli wsp-bench measured.json --require-measured

Each case declares failure_type (obstruction or capability_gap) and expected_repairable. The command prints per-case WSP evidence plus an agreement/confusion summary, and exits 1 when any case disagrees with its label. The shipped fixture is synthetic and must not be reported as a model measurement. --require-measured exits 2 if any case is not measured. examples/wsp_measured_smoke.json is a measured threshold-classifier reference generated by scripts/make_wsp_reference_case.py; it validates the provenance gate but is not an LLM capability claim.

Planning

python -m machaon.cli plan examples/plan_obstruction.json

The input declares failure_type and either layer_scores or a causal-patching patch_report. The output is an edit, diagnose or do_not_edit route. An obstruction without a site above threshold is not turned into a guessed layer.

Diagnosis

python -m machaon.cli diagnose examples/diagnose_obstruction.json

The input supplies base, optional headroom, optional mechanism, an optional diagnosis protocol, and optional plan arguments. The output contains the conservative failure type and, when requested, the corresponding edit/diagnose/do-not-edit plan. Missing or contradictory evidence stays unknown.

wsp-verify checks the format/version and recomputes the evidence SHA-256.

binding

Verify that an OpenWSP edit and a WSP evidence bundle still point at each other:

python -m machaon.cli binding out/edit evidence/

Exit code 1 means the edit address, payload address or evidence binding failed.

inspect

Lists tensors in a safetensors checkpoint.

python -m machaon.cli inspect /models/qwen --suffix down_proj.weight
python -m machaon.cli inspect /models/qwen --layer 3 --limit 5

doctor

Reports installed capabilities, package versions and available compute devices:

python -m machaon.cli doctor
python -m machaon.cli doctor --json

Model loading defaults to CPU. GPU/MPS execution is opt-in with --device cuda, --device mps, or --device auto. The NumPy edit algebra remains on the host CPU even when model forward/backward runs on a GPU.

--version

python -m machaon.cli --version

The three-command CI

./scripts/ci.sh

Runs the tests, the demo, spec validation, both gate cases, and lint.