PYTHON := .venv/bin/python
PYRIGHT := pyright
UV := uv
NPM := npm
GITLEAKS := gitleaks
PYTHONPYCACHEPREFIX := /tmp/docforge-quality-pycache
PYTEST_BASETEMP := /tmp/docforge-quality-pytest

.PHONY: accessibility adoption-m4 benchmark benchmark-full benchmark-m1 benchmark-m1-smoke benchmark-m2 benchmark-m2-smoke benchmark-m3 benchmark-m3-full benchmark-m3-smoke benchmark-m4 benchmark-m4-full benchmark-m4-smoke benchmark-smoke build command-reference-check compatibility-m5 compile concurrency-m5 contract dependencies docs-check format-check fresh-clone-m5 gate lint lock migration-m5 recovery-m5 release-artifacts release-gate release-posttag release-pretag secret-scan task-evidence-m5 task-evidence-m5-smoke test type version-check

accessibility:
	$(NPM) run test:accessibility

format-check:
	$(PYTHON) -m ruff format --check src tests tools

lint:
	$(PYTHON) -m ruff check src tests tools
	$(NPM) run lint:web

type:
	$(PYRIGHT) --pythonpath .venv/bin/python

compile:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m compileall -q src tests tools

contract:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m pytest -q \
		-p no:cacheprovider --basetemp=$(PYTEST_BASETEMP) \
		tests/test_public_contract.py \
		tests/test_policy.py \
		tests/test_projection_policy.py \
		tests/test_projection_policy_integration.py \
		tests/test_projection_worker.py \
		tests/test_projection_fragments.py \
		tests/test_retrieval.py \
		tests/test_generation_diff.py \
		tests/test_client_integration.py \
		tests/test_projection_contract.py \
		tests/test_projection_schemas.py \
		tests/test_graph_projection.py \
		tests/test_graph_rendering.py \
		tests/test_graph_publication.py \
		tests/test_observability.py::TelemetryContractTests::test_schema_fixed_names_match_the_implementation \
		tests/test_adapter_contract.py::AdapterContractTests::test_no_ast_index_policy_rejects_logic_publication \
		tests/test_adapter_contract.py::AdapterContractTests::test_no_ast_index_accepts_legacy_and_non_logic_incremental_adapters \
		tests/test_adapter_contract.py::AdapterContractTests::test_no_ast_rejects_preexisting_logic_index_and_viewer_snapshot \
		tests/test_mcp_server.py::DocForgeMcpTests::test_no_ast_binding_preserves_adapter_and_blocks_logic

test:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m pytest -q \
		-p no:cacheprovider --basetemp=$(PYTEST_BASETEMP)

lock:
	$(UV) lock --check

dependencies:
	$(NPM) ls --all

build:
	$(UV) build

adoption-m4:
	$(PYTHON) tools/milestone4_adoption.py

version-check:
	$(PYTHON) tools/check_release_identity.py --mode smoke --tag-state ignore > /dev/null

release-artifacts:
	$(PYTHON) tools/check_release_identity.py --mode full --require-clean \
		--tag-state ignore --output /tmp/docforge-milestone5-release-identity.json > /dev/null

secret-scan:
	$(GITLEAKS) git . --no-banner --redact
	$(GITLEAKS) dir . --no-banner --redact

migration-m5:
	$(PYTHON) tools/milestone5_migration.py \
		--output /tmp/docforge-milestone5-migration.json > /dev/null

task-evidence-m5-smoke:
	$(PYTHON) tools/milestone5_task_evidence.py --mode smoke \
		--output /tmp/docforge-milestone5-task-evidence-smoke.json > /dev/null

task-evidence-m5:
	$(PYTHON) tools/milestone5_task_evidence.py --mode full \
		--output /tmp/docforge-milestone5-task-evidence.json > /dev/null

compatibility-m5:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m pytest -q \
		-p no:cacheprovider --basetemp=$(PYTEST_BASETEMP)-compatibility \
		tests/test_public_contract.py \
		tests/test_adapter_contract.py \
		tests/test_adapter_sdk.py \
		tests/test_policy.py \
		tests/test_projection_policy.py \
		tests/test_projection_policy_integration.py \
		tests/test_projection_contract.py \
		tests/test_projection_schemas.py \
		tests/test_projection_worker.py \
		tests/test_projection_fragments.py \
		tests/test_retrieval.py \
		tests/test_graph_projection.py \
		tests/test_graph_rendering.py \
		tests/test_graph_publication.py

concurrency-m5:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m pytest -q \
		-p no:cacheprovider --basetemp=$(PYTEST_BASETEMP)-concurrency \
		tests/test_core.py::DocForgeCoreTests::test_stale_source_fails_closed_and_failed_rebuild_preserves_index \
		tests/test_core.py::DocForgeCoreTests::test_query_rechecks_source_identity_before_returning \
		tests/test_core.py::DocForgeCoreTests::test_source_set_change_during_load_fails_closed \
		tests/test_adapter_contract.py::AdapterContractTests::test_fast_incremental_reads_reverify_a_changed_index_file \
		tests/test_retrieval.py::TaskRetrievalTests::test_final_generation_change_rejects_the_whole_capsule \
		tests/test_visualization.py::VisualizationTests::test_snapshot_source_never_mixes_pinned_graph_with_newer_canonical_text \
		tests/test_graph_publication.py::GraphPublicationTests::test_status_detects_source_and_publication_races \
		tests/test_changesets.py

recovery-m5:
	PYTHONPYCACHEPREFIX=$(PYTHONPYCACHEPREFIX) $(PYTHON) -m pytest -q \
		-p no:cacheprovider --basetemp=$(PYTEST_BASETEMP)-recovery \
		tests/test_core.py::DocForgeCoreTests::test_missing_or_corrupt_generation_falls_back_and_repairs \
		tests/test_core.py::DocForgeCoreTests::test_stale_source_fails_closed_and_failed_rebuild_preserves_index \
		tests/test_incremental_cache.py \
		tests/test_python_reference_adapter.py \
		tests/test_javascript_reference_adapter.py \
		tests/test_cpp_reference_adapter.py \
		tests/test_generation_diff.py \
		tests/test_rendering.py \
		tests/test_graph_publication.py \
		tests/test_projection_fragments.py

command-reference-check:
	$(PYTHON) tools/generate_command_reference.py \
		--output docs/COMMAND_REFERENCE.md --check > /dev/null

docs-check: command-reference-check
	$(PYTHON) tools/check_documentation.py \
		--pending-inventory tools/docs_pending_m4_pages.txt

benchmark-smoke:
	$(PYTHON) tools/milestone0_baseline.py --nodes 25 --samples 1 --cold-samples 1 \
		--output /tmp/docforge-milestone0-smoke.json > /dev/null

benchmark:
	$(PYTHON) tools/milestone0_baseline.py --nodes 1000 --samples 10 --cold-samples 3

benchmark-m1-smoke:
	$(PYTHON) tools/milestone1_benchmark.py --nodes 25 --samples 1 \
		--output /tmp/docforge-milestone1-smoke.json > /dev/null

benchmark-m1:
	$(PYTHON) tools/milestone1_benchmark.py --nodes 1000 --samples 10

benchmark-m2-smoke:
	$(PYTHON) tools/milestone2_benchmark.py --nodes 25 --samples 1 \
		--output /tmp/docforge-milestone2-smoke.json > /dev/null

benchmark-m2:
	$(PYTHON) tools/milestone2_benchmark.py --nodes 1000 --samples 10

benchmark-m3-smoke:
	$(PYTHON) tools/milestone3_benchmark.py --mode smoke \
		--output /tmp/docforge-milestone3-smoke.json > /dev/null

benchmark-m3:
	$(PYTHON) tools/milestone3_benchmark.py --mode full

benchmark-m3-full: benchmark-m3

benchmark-m4-smoke:
	$(PYTHON) tools/milestone4_benchmark.py --mode smoke \
		--output /tmp/docforge-milestone4-smoke.json > /dev/null

benchmark-m4:
	$(PYTHON) tools/milestone4_benchmark.py --mode full

benchmark-m4-full: benchmark-m4

benchmark-full: benchmark benchmark-m1 benchmark-m2 benchmark-m3-full benchmark-m4-full

gate: format-check lint type compile contract test accessibility lock dependencies build docs-check benchmark-smoke benchmark-m1-smoke benchmark-m2-smoke benchmark-m3-smoke benchmark-m4-smoke

release-gate: gate compatibility-m5 migration-m5 concurrency-m5 recovery-m5 task-evidence-m5 adoption-m4 version-check release-artifacts secret-scan benchmark-full

fresh-clone-m5:
	$(PYTHON) tools/milestone5_fresh_clone.py \
		--output /tmp/docforge-milestone5-fresh-clone.json > /dev/null

release-pretag: release-gate fresh-clone-m5
	$(PYTHON) tools/check_release_identity.py --mode smoke --require-clean \
		--tag-state absent > /dev/null

release-posttag:
	$(PYTHON) tools/check_release_identity.py --mode smoke --require-clean \
		--tag-state head > /dev/null
