# The harness. Term 16's commit is this file reaching the state where every
# target below runs from a clean clone, by someone who has not read the book.
#
# Hand authored. gen.mjs does not touch it.

SHELL := /bin/bash
.DEFAULT_GOAL := help
NODE ?= node

.PHONY: help gen pdf pdf-check clean \
        gate-1 gate-2 gate-3 gate-4 gate-5 gates \
        demo-tier-1 demo-tier-2 demo-tier-3 demo-tier-4 demo-tier-5 demos \
        test lint

help: ## show this
	@echo "reserve-registry"
	@echo ""
	@grep -hE '^[a-zA-Z0-9_-]+:.*?## ' $(MAKEFILE_LIST) \
	  | awk 'BEGIN{FS=":.*?## "}{printf "  \033[1m%-16s\033[0m %s\n", $$1, $$2}'
	@echo ""
	@echo "  Every gate fails today. That is the scaffold working, not broken."

# ---------------------------------------------------------------- gates
# A gate is a script, not a quiz. Each names the terms that unblock it.

gate-1: ## Tier 1: CLI does a real task inside a declared token budget
	@$(NODE) scripts/gates/gate-1.mjs

gate-2: ## Tier 2: MCP contract tests pass; cache hit rate clears its floor over 10 runs
	@$(NODE) scripts/gates/gate-2.mjs

gate-3: ## Tier 3: a headless run finishes with zero human input and leaves an artifact
	@$(NODE) scripts/gates/gate-3.mjs

gate-4: ## Tier 4: the eval suite catches a deliberate regression
	@$(NODE) scripts/gates/gate-4.mjs

gate-5: ## Tier 5: the loop runs 3 generations and reports the delta with its sign
	@$(NODE) scripts/gates/gate-5.mjs

# Runs all five and reports how far the repo has got. Does not stop at the
# first failure, because the useful output is the boundary, not the first miss.
gates: ## run all five gates and report the boundary
	@pass=0; for i in 1 2 3 4 5; do \
	  if $(NODE) scripts/gates/gate-$$i.mjs >/dev/null 2>&1; then pass=$$i; else break; fi; \
	done; \
	echo "gates passing: $$pass of 5"; \
	if [ $$pass -lt 5 ]; then $(NODE) scripts/gates/gate-$$((pass+1)).mjs || true; fi

# ---------------------------------------------------------------- demos
# Each demo runs that tier's proof end to end and writes artifacts/demo-tier-N.json.
# No commit may break a previous tier's demo; `make demos` is what enforces it.

demo-tier-1: ## Tier 1 proof end to end
	@$(NODE) scripts/demos/demo-tier-1.mjs

demo-tier-2: ## Tier 2 proof end to end
	@$(NODE) scripts/demos/demo-tier-2.mjs

demo-tier-3: ## Tier 3 proof end to end
	@$(NODE) scripts/demos/demo-tier-3.mjs

demo-tier-4: ## Tier 4 proof end to end
	@$(NODE) scripts/demos/demo-tier-4.mjs

demo-tier-5: ## Tier 5 proof end to end
	@$(NODE) scripts/demos/demo-tier-5.mjs

demos: demo-tier-1 demo-tier-2 demo-tier-3 demo-tier-4 demo-tier-5 ## every demo, in order, none may regress

# ---------------------------------------------------------------- book
gen: ## regenerate every derived doc from the curriculum
	@$(NODE) scripts/gen.mjs

pdf: ## assemble docs/ into build/reserve-registry.pdf
	@$(NODE) scripts/build-pdf.mjs

pdf-check: ## assemble, then assert page budget and that all 30 terms rendered
	@$(NODE) scripts/build-pdf.mjs --check

# ---------------------------------------------------------------- repo
test: ## vitest
	@npm test

lint: ## tsc --noEmit
	@npx tsc --noEmit

clean:
	@rm -rf build
