# Repository automation (inheritance, neural and language-model tiers). The uv venv (built from the committed uv.lock) is the
# reproducibility source of truth; every target runs inside it via `uv run`.

.PHONY: env env-neural env-mnist env-llm env-notebooks test inheritance neural mnist llm \
	llm-epistasis llm-society llm-society-calib llm-society-calib-b llm-society-v2-smoke \
	figures paper-figures paper notebooks clean

env:               ## build .venv from the committed lockfile
	uv sync --extra dev

env-neural:        ## add the torch stack for the trained-network tier (GPU)
	uv sync --extra dev --extra neural

env-mnist:         ## add torchvision for the real-MNIST confirmation tier
	uv sync --extra dev --extra neural --extra mnist

test:              ## correctness tests + scientific-validation tests (the spine of trust)
	uv run pytest

inheritance:       ## run every inheritance-model experiment (analytic; bitwise reproducible)
	for c in configs/inheritance/*.yaml; do uv run python -m inheritance.experiment "$$c"; done

neural:            ## run the synthetic trained-network experiments (excludes the MNIST/torchvision tiers)
	for c in configs/neural/*.yaml; do case "$$c" in *mnist*|*speciation_real*) ;; \
		*) uv run python -m neural.experiment "$$c" ;; esac; done

mnist:             ## run the torchvision tiers: MNIST collapse + real-weight speciation (needs env-mnist)
	uv run python -m neural.experiment configs/neural/fig2_mnist_collapse.yaml
	uv run python -m neural.experiment configs/neural/speciation_real.yaml
	uv run python -m neural.experiment configs/neural/speciation_real_cliff.yaml
	uv run python -m neural.experiment configs/neural/speciation_real_emergent.yaml
	MPLBACKEND=Agg uv run python figures/mnist_montage.py   # the asset paper Fig. 2A embeds

env-llm:           ## add the LLM stack for the Layer-2 prototype (GPU; transformers/peft)
	uv sync --extra dev --extra neural --extra llm

llm:               ## run the LLM prototypes: merge (fusion) + moe (union) + directed (offspring select)
	uv run python -m llm.experiment configs/llm/merge.yaml
	uv run python -m llm.experiment configs/llm/moe.yaml
	uv run python -m llm.experiment configs/llm/directed.yaml

llm-speciation:    ## LLM-tier speciation: conflict cliff (replace + de-confounded add) and duration null
	uv run python -m llm.experiment configs/llm/speciation.yaml
	uv run python -m llm.experiment configs/llm/speciation_add.yaml

llm-epistasis:     ## the controlled predictive test (feeds paper Fig. 3C-D) + its robust statistics
	uv run python -m llm.experiment configs/llm/epistasis.yaml
	uv run python -m llm.experiment configs/llm/epistasis_compat.yaml
	uv run python figures/stats_llm_epistasis.py

llm-society:       ## the composed society at LLM scale (C3): pilot; the campaign runs on HPC
	uv run python -m llm.experiment configs/llm/society.yaml

llm-society-calib: ## v2 society calibration gates (prereg §4): stage A (families), then B (C2/C3/C5)
	uv run python -m llm.experiment configs/llm/society_v2_calib_a.yaml
	@echo "Review the C1 table, fix the 12 families in society_v2_calib_b.yaml, then: make llm-society-calib-b"

llm-society-calib-b: ## v2 calibration stage B over the chosen 12 families: transmission, cross, consensus
	uv run python -m llm.experiment configs/llm/society_v2_calib_b.yaml
	mkdir -p configs/llm/_gen
	uv run python - <<'EOF'
	import yaml; c = yaml.safe_load(open("configs/llm/society_v2_calib_b.yaml"))
	for st in ("cross", "consensus"):
	    c["stage"] = st; c["output"] = {"dir": f"results/llm_society_v2_calib_b_{st}"}
	    yaml.safe_dump(c, open(f"configs/llm/_gen/calib_{st}.yaml", "w"), sort_keys=False)
	EOF
	uv run python -m llm.experiment configs/llm/_gen/calib_cross.yaml
	uv run python -m llm.experiment configs/llm/_gen/calib_consensus.yaml

llm-society-v2-smoke: ## v2 society loop smoke (4 families, 4 agents, 2 generations, all arms)
	uv run python -m llm.experiment configs/llm/society_v2_smoke.yaml
	uv run python figures/plot_llm_society.py results/llm_society_v2_smoke

llm-seeds:         ## multi-seed firm-up (heavy): merge x5, moe-hard x3, directed-hard x3
	uv run python -m llm.experiment configs/llm/merge_seeds.yaml
	uv run python -m llm.experiment configs/llm/moe_hard_seeds.yaml
	uv run python -m llm.experiment configs/llm/directed_hard_seeds.yaml

figures:           ## regenerate per-experiment figures from committed results (pure; no re-simulation)
	for p in figures/plot_*.py; do MPLBACKEND=Agg uv run python "$$p"; done

paper-figures:     ## regenerate the manuscript figures (Fig. 1-5) and rebuild the PDF bodies
	MPLBACKEND=Agg uv run python paper/manuscript/make_figs.py
	uv run python paper/manuscript/build.py
	uv run python paper/manuscript/build.py si

paper:  paper-figures  ## figures + compile both PDFs (needs tectonic)
	cd paper/manuscript && tectonic main.tex && tectonic si.tex
	uv run python paper/manuscript/build_lay_legends.py && cd paper/manuscript && tectonic figure_legends_for_students.tex

env-notebooks:     ## add Jupyter for the walkthrough notebooks
	uv sync --extra dev --extra notebooks

notebooks:         ## execute every notebook end-to-end (a reproduction check in itself)
	for nb in notebooks/*.ipynb; do uv run jupyter nbconvert --to notebook --execute \
		--inplace --ExecutePreprocessor.timeout=1800 "$$nb"; done

clean:             ## remove caches and regenerable parquets (keeps committed manifests and figures)
	rm -rf .pytest_cache **/__pycache__
	find results -name 'results.parquet' -delete 2>/dev/null || true   # tracked manifests/figures stay
