# Host build of the Layer 1 core.
#
#   make          build build/host_probe
#   make models   regenerate models/default.m8 and models/moe.bin
#   make run      build, regenerate the models, run the regression
#   make clean
#
# The host uses the published engine config (DUAL_CORE 1 through the pthread
# platform backend). Only the sanity fingerprint is skipped: it is a CRC over
# the float bits of the top-k logits, which is target-specific. The regression
# compares the 24 greedy ids instead.

CC ?= cc
CFLAGS ?= -O2 -std=gnu11 -ffp-contract=off -Wall -Wextra
CXX ?= g++
CXXFLAGS ?= -O2 -std=gnu++11 -ffp-contract=off -Wall -Wextra
CPPFLAGS += -DRT_HOST_BUILD=1 -DRT_SKIP_SANITY=1 -I../src -I../src/core -I../src/platform
LDLIBS += -lm -lpthread

# Local research tree; override if it lives elsewhere.
TRANSFORMER ?= ../../esp32s3-int8-transformer

BUILD := build
MODELS := models
MODEL := $(MODELS)/default.m8
MOE := $(MODELS)/moe.bin

# The redistributable second model, committed so the atomicity test can run
# without the research weights. Override both on the command line to test
# another pair.
SECOND ?= ../models/synthetic48-q8.m8
SECOND_MOE ?= -

CORE := ../src/core/llm.c ../src/core/model_lib.c ../src/core/moe.c \
        ../src/core/kv_cache.c ../src/core/tokenizer.c ../src/rt_core.c ../src/rt_manifest.c \
        ../src/rt_envelope.c
HOST := host_probe.c dot_s8_host.c rt_platform_posix.c

.PHONY: all models run check-backend check-affinity check-manifest \
        check-version check-untrusted check-envelope check-moe check-moe-cache \
        check-moe-faults check-storage check-tools check-alloc check-plan check-kv \
        check-serving check-model-tool check-bench-reports check-compat-table \
        atomicity clean

all: $(BUILD)/host_probe

check-backend: all
	./check_backend.sh

check-affinity: all
	./check_affinity.sh

check-manifest: $(BUILD)/manifest_probe
	./check_manifest.sh

check-version:
	./check_version.sh

check-untrusted:
	./check_untrusted.sh

check-envelope:
	./check_envelope.sh

check-moe:
	./check_moe.sh

# The streamed source and hot set need their own fixture: version 3 sidecars
# with finite per-expert weights, so a resident and a streamed read must agree
# bit for bit and a corrupt block must be refused.
check-moe-cache:
	./check_moe_cache.sh

# The failure and lifecycle matrix needs a mutable copy of a version 3
# sidecar and allocation fault injection. It runs under ASan/UBSan.
check-moe-faults:
	./check_moe_faults.sh

# The storage backend comparison needs the same sidecar read three ways:
# resident, partition reads and a read-only mapping. The host registry stores
# the same 16-byte prefix the on-board partition image uses.
check-storage:
	./check_storage.sh

# The benchmark summariser is host tooling, but a silent parsing regression
# would corrupt every published memory table. Its self-test is part of the
# host gate so the fixture stays honest.
check-tools:
	python3 ../tools/bench_summary.py --self-test

# The committed reports are generated from the raw captures. Validate the
# schema and the required evidence, and re-run the parser self-test.
check-bench-reports:
	python3 ../tools/bench_report.py --self-test
	for f in ../docs/examples/*_report.json; do \
	    python3 ../tools/bench_report.py --validate "$$f" || exit 1; \
	done

# docs/SUPPORTED-MODELS.md is generated from src/core/llm.h, the compatibility
# matrix and the committed manifests. A stale copy fails the gate.
check-compat-table:
	python3 ../tools/gen_compat_table.py --check

# The planner compares requested sizes with allocator reservations. A backend
# that reports a reservation smaller than the request would make every plan
# optimistic, so the hooks get their own gate.
check-alloc: $(BUILD)/alloc_probe
	./$(BUILD)/alloc_probe

# The planner is pure arithmetic, so its gate needs no weights and no board.
PLAN_CORE := ../src/rt_plan.c $(CORE) dot_s8_host.c rt_platform_posix.c

$(BUILD)/plan_probe: plan_probe.c $(PLAN_CORE) $(wildcard ../src/*.h) \
                     $(wildcard ../src/core/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ plan_probe.c $(PLAN_CORE) $(LDLIBS)

check-plan: $(BUILD)/plan_probe
	./$(BUILD)/plan_probe

# The KV manager owns the context-sized allocations and the valid length. Its
# gate uses the committed fixture and needs no research weights.
$(BUILD)/kv_probe: kv_probe.c $(CORE) $(wildcard ../src/*.h) \
                   $(wildcard ../src/core/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ kv_probe.c $(CORE) \
	  dot_s8_host.c rt_platform_posix.c $(LDLIBS)

check-kv: $(BUILD)/kv_probe
	./$(BUILD)/kv_probe

# The serving contract is a header, so its gate is a C++ compile and a fake
# backend. It must not need Arduino.h, FreeRTOS or the engine sources.
$(BUILD)/serving_probe: serving_probe.cpp ../src/LlmBackend.h \
                        ../src/rt_manifest.h ../src/rt_serve.h ../src/rt_errors.h
	@mkdir -p $(BUILD)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ serving_probe.cpp

check-serving: $(BUILD)/serving_probe
	./$(BUILD)/serving_probe

# Failure injection: a failed switch must leave the old model generating the
# same tokens. Requires the reference model, a second model and a scratch dir.
atomicity: $(BUILD)/atomicity_test
	@mkdir -p $(BUILD)/atomic
	./$(BUILD)/atomicity_test $(MODEL) $(MOE) $(SECOND) $(SECOND_MOE) \
	  $(BUILD)/atomic

$(BUILD)/atomicity_test: atomicity_test.c rt_faults.c $(CORE) \
                         $(wildcard ../src/*.h) $(wildcard ../src/core/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -DRT_TEST_FAULTS -o $@ atomicity_test.c \
	  rt_faults.c $(CORE) dot_s8_host.c rt_platform_posix.c $(LDLIBS)

$(BUILD)/alloc_probe: alloc_probe.c rt_platform_posix.c \
                      $(wildcard ../src/platform/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ alloc_probe.c rt_platform_posix.c \
	  $(LDLIBS)

$(BUILD)/host_probe: $(HOST) $(CORE) $(wildcard ../src/core/*.h) \
                     $(wildcard ../src/platform/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $(HOST) $(CORE) $(LDLIBS)

# Manifest and inspect tests. These use a synthetic container generated by
# make_test_model.py, so they need neither the research weights nor a board.
MANIFEST_CORE := $(CORE) dot_s8_host.c rt_platform_posix.c

$(BUILD)/tok_encode: tok_encode.c ../src/core/tokenizer.c
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tok_encode.c ../src/core/tokenizer.c $(LDLIBS)

$(BUILD)/manifest_probe: manifest_probe.c $(MANIFEST_CORE) \
                         $(wildcard ../src/*.h) $(wildcard ../src/core/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ manifest_probe.c $(MANIFEST_CORE) $(LDLIBS)

# Machine-readable inspect. The Python model tool consumes this so its verdict
# is cross-checked against the real rt_model_inspect(), not a second parser.
$(BUILD)/inspect_json: inspect_json.c $(MANIFEST_CORE) \
                       $(wildcard ../src/*.h) $(wildcard ../src/core/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ inspect_json.c $(MANIFEST_CORE) $(LDLIBS)

check-model-tool: $(BUILD)/inspect_json
	./check_model_tool.sh

models:
	@mkdir -p $(MODELS)
	python3 $(TRANSFORMER)/tools/pack_model_m8.py \
	  --model $(TRANSFORMER)/model/stories260K.bin \
	  --tokenizer $(TRANSFORMER)/model/tok512.bin \
	  --out $(MODEL) --name "stories260k-q8" \
	  --sanity-dump $(TRANSFORMER)/logs/golden-logits-2026-11-25.log \
	  --sanity-label int8 --sanity-step 0
	cp $(TRANSFORMER)/runs/moe4-stageB-crc.moe $(MOE)
	python3 ../tools/m8_manifest.py --model $(MODEL) --moe $(MOE) \
	  --id stories260k-q8 --name "stories260K q8" \
	  --license-weights "unspecified: local research fixture" \
	  --license-tokenizer "unspecified: local research fixture"
	@sha256sum $(MODEL) $(MOE)

run: all $(BUILD)/manifest_probe models
	./$(BUILD)/host_probe $(MODEL) $(MOE)
	./$(BUILD)/manifest_probe load $(MODELS) stories260k-q8

clean:
	rm -rf $(BUILD)
