On-Policy Distillation as Active Inference in Finite Variational Models — reverse-KL free energy, student-induced sampling, and deterministic toy witnesses.
— concept DOI (resolves to the latest version); v1.0.2 record: 10.5281/zenodo.20749817.
This project formalises a scoped active-inference reading from first principles and demonstrates it on auditable minimal models. The intellectual core is the firstprinciples package — an audited active-inference ↔ on-policy-distillation correspondence map, the shared divergence geometry (forward/reverse KL, JSD, skew, α-divergence, OPSD per-token clipping), the reward-tilted-target unification of RL-as-inference / active inference / distillation, the SDPG privileged-context self-distillation objective, an exposure-bias model, a deterministic sequential-shift witness with a correction-dose sensitivity sweep, a structured OPD-literature taxonomy, and a two-agent pymdp classroom that pits a privileged teacher against an on-policy student and measures the reverse-KL distillation signal. See the integrated notation & formalism supplement.
The manuscript is a sheaf-composed multi-track document with configurable sections (analytical, pymdp, GNN, ontology, Lean, visualizations, provenance, replay matrix, counterexamples, sensitivity, uncertainty, benchmark, model-checking, interop, adversarial audit, evidence fields, scholarship, release bundle, theorem traceability, gate ergonomics, assumption indexing, animation deltas, and manuscript staleness). Every reported number is hydrated from a generated artifact and machine-checked before rendering.
The canonical readiness command is the convergent full chain. It regenerates the declared analysis artifacts, refreshes manuscript variables and validation certificates, validates the outputs, and renders the PDF:
git clone https://github.com/ActiveInferenceInstitute/on_policy_distillation
cd on_policy_distillation
uv sync --locked --extra dev
uv run --locked --extra dev python scripts/gate.py negative-controls
uv run python scripts/run_full_chain.py --renderThe negative-controls arm records the observations required by the release
checks. With those observations in place, scripts/run_full_chain.py --render
runs the analysis chain and renders the checked manuscript.
The individual scripts remain available for debugging or narrow development.
Their order is declared in docs/manuscript/config.yaml and should not be
hand-reordered:
uv run python scripts/compose_manuscript.py
uv run python scripts/run_analytical_sweep.py
uv run python scripts/simulate_si_tmaze.py
uv run python scripts/simulate_si_graph_world.py
uv run python scripts/compute_statistics.py
uv run python scripts/generate_validation_spine.py
uv run python scripts/generate_toy_sweep_tracks.py
uv run python scripts/generate_firstprinciples.py # OPD<->AI artifacts; classroom rollout runs by default (--no-classroom to skip)
uv run python scripts/generate_formal_interop_tracks.py
uv run python scripts/generate_integration_audit.py
uv run python scripts/generate_sheaf_tracks.py
uv run python scripts/generate_figures.py
uv run python scripts/render_animation.py
uv run python scripts/z_generate_manuscript_variables.py(generate_method_inventory.py is a docs-maintenance step, not part of the
analysis chain — see "Method inventory" below.)
Validation and the gate share one entry point, scripts/gate.py:
every check CI runs is an arm, and python scripts/gate.py --list prints each
arm's argv. The arm table and the measured wall times are in
docs/development/gate.md.
uv run --extra dev python scripts/gate.py # fast arm (default): no slow lanes, no coverage
uv run --extra dev python scripts/gate.py lint # ruff + byte-compile of src, scripts, tests
uv run --extra dev python scripts/gate.py docs # relative links + documentation contracts
uv run --extra dev python scripts/gate.py gnn # pinned GNN conformance
uv run --extra dev python scripts/gate.py negative-controls # record per-case observations
uv run --extra dev python scripts/gate.py chain # regenerate + validate the artifact tree
uv run --extra dev python scripts/gate.py full # release coverage gate (after the chain)
uv run --extra dev python scripts/gate.py render # canonical PDF (after the chain)
uv run --extra dev python scripts/gate.py all # the CI sequence: lint, docs, gnn, negative-controls, chain, fullfast is the tight daily edit loop: it skips artifact_slow (full artifact
writes, fixed-point validation, and tests that mutate shared generated
artifacts) and render_slow (read-only but expensive render, animation,
rollout, and large-compose checks). full is the release gate and remains
authoritative. On a loaded machine run scripts/run_tests_chunked.py instead of
full — a single pytest process can be killed under resource pressure (exit
144); chunks survive.
CI (.github/workflows/ci.yml) is one job whose
steps are those arms, after one locked environment (uv sync --locked --extra dev): lint, docs, gnn, chain, full. The workflow never restates a
command. The gnn arm provisions the revision pinned in
gnn/upstream.json (public repository, exact SHA) into the
job-level ON_POLICY_DISTILLATION_GNN_ROOT selection, so the coverage gate fails
instead of skipping the conformance half; what it parses is recorded in
.github/GNN.md. scripts/validate_outputs.py is not a
separate step; the chain already runs it and a workflow cannot validate a tree
it never built.
In the monorepo layout, from the sibling ../../../template checkout after linking sidecar projects:
uv run python -m infrastructure.orchestration link-projects
uv run python scripts/01_run_tests.py --project working/active_inference_on_policy_distillation
./run.sh --project working/active_inference_on_policy_distillation --pipeline --core-onlyTracks are declared in docs/manuscript/sheaf/tracks.yaml (order, renderer, optional). Sections bind fragments in docs/manuscript/sheaf/manifest.yaml. The composer merges them into flat docs/manuscript/0*.md files for the PDF pipeline.
The first manuscript page (docs/manuscript/00_00_sheaf_coverage.md) shows a B/W/G heatmap of section × track coverage (black = present, white = absent, gray = missing binding). Compose writes output/data/sheaf_coverage_matrix.json only; generate_figures.py renders the heatmap PNG and regenerates the coverage page via ensure_coverage_artifacts.
Hydration writes a semantic sheaf certificate at output/data/sheaf_gluing_certificate.json.
It also writes output/data/sheaf_evidence_crosswalk.json and
output/data/validation_dependency_graph.json. Together these artifacts bind shared
GNN/ontology symbols, typed claims, artifact producers, validation gates, and manuscript
variables so the project validates semantic agreement, not only coverage shape.
The certificate now carries typed restriction_classes and proof_obligations
covering scope, provenance, dependency, evidence, formal, render, release, and
blocked-scope restrictions. The ontology profile matrix covers the Bernoulli toy
model, SI T-maze, graph-world GNN surface, and toy benchmark models with true
mapped-once uniqueness checks. The cross-track symbol table validates required
domains for GNN variables, ontology terms, Lean theorem names, manuscript
variables, JSON fields, figure labels, and rendered-manuscript consumers.
The promoted validation-spine and canonical roadmap artifacts cover provenance,
replay, counterexamples, toy sweeps, uncertainty summaries, benchmark rows,
finite model-checking witnesses, interop reports, semantic gluing, dependency
graphs, evidence-field indexing, release-bundle parity, theorem traceability,
gate ergonomics, scholarship source mapping, artifact diffing, Lean proof extraction, finite state-space
catalogs, proof dependency graphs, state transition tables, causal ablations,
ablation sensitivity reports, artifact license checks, release-note evidence,
release attestation,
track-improvement scope, and adversarial/scope audits. Live track IDs are stable
canonical names; future work improves those tracks rather than adding _vN
siblings.
Section 18_supplement_full_coverage.md binds the appendix manifest row as a composability proof; live counts are injected through manuscript variables, not hand-authored in this README. Reproducibility methodology is now a standalone supplement at 19_supplement_reproducibility.md, followed by validation statistics at 20_supplement_validation_statistics.md. Optional layers is bound in the reproducibility supplement; animation is bound in the validation-statistics supplement row.
The reproducible rendering contract is documented in
docs/reference/rendering-reproducibility.md:
authored fragments/configs generate deterministic data, figures, composed
Markdown, hydrated Markdown, PDF/web outputs, and copied root outputs through one
hydration boundary.
Full modular documentation lives under docs/: architecture
(pipeline, sheaf compose contract, gates, formal layers), the executable model
zoo, the manuscript scope contract and citation map, external-review
disposition ledgers, development and reproducibility guides, a glossary, and an
FAQ — the map is in docs/README.md.
uv run python scripts/compose_manuscript.py --list-tracks
uv run python scripts/compose_manuscript.py --section methods_analytical --tracks prose,formalism
uv run python scripts/compose_manuscript.py --validate-only --strictPer-section overrides: track_order, include_tracks, exclude_tracks. See AGENTS.md.
The pymdp anchor is the full TMaze full_tmaze_sophisticated_inference profile: canonical planner sophisticated_inference, SI search horizon 5, rollout length 6, and Agent policy_len = 1, all measured from si_tmaze_summary.json and pymdp.yaml. simulate_si_tmaze.py also writes si_tmaze_model_matrices.json, si_policy_comparison.json, pymdp_policy_posterior_grid.json, and pymdp_runtime_diagnostics.json; vanilla planning is comparison-only. Runtime diagnostics classify known pymdp/JAX warnings and fail validation on unexpected construction warnings. Reference: pymdp sophisticated_inference examples. Logs: output/logs/pymdp_runs.jsonl.
The models are specified in Generalized Notation Notation (GNN v1.1) under gnn/: the Bernoulli coupling toy, the sophisticated-inference T-maze, and the deterministic graph world. The project-owned parser (src/gnn/parser.py) implements the grammar independently and never imports upstream code; gnn/upstream.json records the canonical upstream release (v5.1.0) and tests/test_gnn_upstream_conformance.py requires digest equality with the pinned upstream parser (run out-of-process against the pinned commit's extracted src/gnn). Model authoring starts from the GNN template library and syntax reference; the framework integration guide covers running a spec through the upstream render/execute pipeline instead of this project's reader. GNN also maintains a downstream adoption guide for repositories that consume the notation.
Related fleet projects: policy_entanglement (policy-level generative models with a GNN round-trip and a Lean boundary fragment) and active_inference_power (pymdp action-loop models with GNN specifications bound to their runtime contracts).
figures.yaml is the source of truth for captions, alt text, source bindings, caption-claim contracts, and pandoc-crossref placement. Current registered figures are ising_mi_curve, free_energy_curve, distillation_divergence_geometry, exposure_bias_recovery, opd_reader_map, opd_situational_awareness, si_belief_entropy_curve, si_obs_action_trace, si_tmaze_actions, si_tmaze_model_matrices, classroom_distillation_signal, sequential_shift_recovery, sequential_shift_sensitivity, energy_decomposition, parallel_convergence, diversity_tradeoff, privilege_dose_response, correspondence_map, policy_posterior_grid, opd_taxonomy_landscape, active_selection_landscape, si_bridge_match, si_belief_entropy_trajectory, precision_ledger, sheaf_coverage_heatmap, sheaf_layers_overview, invariant_dashboard, tmaze_schematic, multi_track_architecture, lean_boundary_status, gnn_ontology_concordance, semantic_gluing_graph, theorem_traceability_graph, causal_ablation_heatmap, scholarship_source_map, and graphical_abstract. active_selection_landscape plots the expected-free-energy active-selection result (the cue-validity sweep identity and per-policy EFE decomposition); graphical_abstract is a quantitative-free overview schematic; opd_reader_map is the opening source-bound mechanism map; opd_situational_awareness is the early concept/evidence/non-claim atlas; detailed quantitative evidence stays in body figures and tables. output/data/figure_source_map.json proves each row has source artifacts, source fields, generator metadata, validation gates, and caption_claims_ok; output/reports/figure_hash_manifest.json proves declared PNG/GIF bytes match live files and rejects stray images; output/reports/visualization_quality_audit.json rechecks readable/nonblank pixels, source binding, all_caption_claims_ok, caption scope guardrails, cover wording, cover quantitative-free status, declared palette contrast, font-role floors, and unexpected-image absence.
See tracks.yaml. Pipeline: required tracks are declared there, including the core analytical/pymdp/formal/notation/visual tracks, validation spine, and canonical promoted roadmap tracks. Sheaf registry: fragment types live in docs/manuscript/sheaf/tracks.yaml; the appendix binds the manifest-declared proof row (22 of the 33 registered fragment types), layers is bound only in the reproducibility supplement, and the Lean/GNN/ontology/model-checking/animation tracks bind in the validation-statistics supplement row. Deterministic extension artifacts (thin scripts -> src/): simulate_si_tmaze.py writes policy comparison, posterior-grid, and runtime-diagnostic artifacts; simulate_si_graph_world.py writes graph-world summary/trace artifacts; render_animation.py writes a trace-derived multi-frame GIF plus frame-delta manifest; generate_validation_spine.py, generate_toy_sweep_tracks.py, generate_formal_interop_tracks.py, generate_integration_audit.py, and generate_sheaf_tracks.py write the canonical validation spine, semantic certificate, dependency graph, evidence-field index, release-bundle manifest, theorem traceability matrix, gate index, artifact diffoscope, proof extraction index, state-space catalog, causal-ablation matrix, artifact license audit, release-note evidence, and promoted audit artifacts.
Non-blocking future work is tracked in TODO.md; current publication claims remain confined to deterministic toy Active Inference artifacts.
Every config knob's consumer, the six-surface add-a-figure recipe, the
add-a-track recipe, post-edit command map, and troubleshooting live in
docs/reference/configuration-and-extension.md.
Stored all_* aggregate flags are re-derived from their rows at validation time
by src/gates/aggregate_rederivation.py (the aggregate_rederivation check in
validate_outputs), so a stale or hand-edited artifact summary fails closed.
Every Python def and class under src/ and scripts/ is documented in the
generated reference docs/reference/method-inventory.md.
Regenerate it after method, script, or module changes:
uv run python scripts/generate_method_inventory.pyThe inventory distinguishes inline docstrings from inventory fallbacks, so missing docstrings remain visible without bloating internal helper code.
This repository carries a committed .aii/ sidecar with curated
InstituteOS metadata (aii-sidecar/v1): category, maturity, ecosystem relations,
key artifacts, provenance, and portable tasks. It is read by
InstituteOS tooling and
does not affect the project's build or runtime.