Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/34112ba0bab39534 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#538 (code only) open derived `34112ba0bab39534` #538 Imported from GitHub aien-dev/omega-mv#538 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `3e3dc205d5fe63c10317ddb18c0eba13f6ae0a82` · Head `b78b4b7df56848b2fc51046d1e95691c9e590e5c` github-advanced-security[bot] commented on `tests/fabric/fab_work_test.c` line 109 on 2026-10-18 09:13 UTC ## CodeQL / Uncontrolled data used in path expression This argument to a file access function is derived from user input (string read by fgets) and then passed to fopen(__filename). Show more details github-advanced-security[bot] commented on `tests/fabric/fab_work_test.c` line 147 on 2026-10-18 09:13 UTC ## CodeQL / File created without restricting permissions A file may be created here with mode 0666, which would make it world-writable. Show more details github-advanced-security[bot] commented on `tests/fabric/fab_work_test.c` line 738 on 2026-10-18 09:13 UTC ## CodeQL / File created without restricting permissions A file may be created here with mode 0666, which would make it world-writable. Show more details github-advanced-security[bot] reviewed (COMMENTED) on 2026-10-18 09:13 UTC ### Fetch it A git bundle of the pull request's head, 107670 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/34112ba0bab39534.bundle -o 34112ba0bab39534.bundle git fetch 34112ba0bab39534.bundle refs/pull/538/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 538 b78b4b7df56848b2fc51046d1e95691c9e590e5c ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":538,"url":"https://github.com/aien-dev/omega-mv/pull/538","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:11Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index 69631aa42..aebcaee45 100644 --- a/.crumb +++ b/.crumb @@ -42,80 +42,80 @@ "children": [ { "digest": "d02ccdb9f25b2635898d1a3fd8d106aca15bddca6bfaade71399029649caee39", - "last_commit": "ec863f90", + "last_commit": "ec863f90a", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "7ea98a34750464c170bc103e362495ca893b72a7fae9baf192ad8b2606a9551f", - "last_commit": "369f3d48", + "last_commit": "369f3d486", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116c", + "last_commit": "30bb116cc", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "0eca875ed53fd537a25d261ea2a45e5b62796c384263a38b41adca96cad2dcc2", - "last_commit": "c3c81ac3", + "digest": "f1f9dccc612800b7f5e95de069b42f3e7106d62fd8c5b405fa80711401d73009", + "last_commit": "f601985a8", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "f021abf0e52c893181d2ed09ab0eb9ba5cc957f156b202ae8fdd8ef517b0b257", - "last_commit": "4c71fa37", + "last_commit": "4c71fa37f", "name": "native", "purpose": "Handwritten .om units of omega-mv that are not part of the seed ladder: the ORIG-4 shadow adviso" }, { "digest": "d5116e0cfc02b5ca8f6d5f356f3fc73fd3b1d004fde08fbcf7ef33218577892f", - "last_commit": "e03dfc08", + "last_commit": "e03dfc08c", "name": "neuralese", "purpose": "Bounded untrusted representation envelopes and explicit checked Omega decoding; learned extensio" }, { "digest": "16bd56102894984869d56d0ee92a56ccc51adf6d28ce2be40c39b3b35a3dd0c1", - "last_commit": "bd77148a", + "last_commit": "bd77148a4", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { "digest": "8d9d663ec19165545390b63f7227c6a6934cf3d48f2682a7453080e36e3c9f78", - "last_commit": "9b3d3b96", + "last_commit": "9b3d3b964", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "c6ae05f5ca8e295aa12ea3c4ab5143a799adfd377ab40803d9cf5cfe75ea47c9", - "last_commit": "4c71fa37", + "digest": "a0b48d4ceae8d5ae3cbae673a7e34ed7c8d8c5cd92e2e717316b3260b12e5b54", + "last_commit": "f601985a8", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "30ff9017b760b1486add5c105f2a3b2bd3c4bf9b7e2cb656d666d87a6c6289eb", - "last_commit": "e45dbca3", + "digest": "6478a0c54253a884a9d36d21211fef92a36dc02e016f079ed662ffffd5d594e8", + "last_commit": "f601985a8", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "d6be5b5d49d6a2946754e459987f094e5fccc5cbd597f269b9c8423e2c29e283", - "last_commit": "6b0682a4", + "last_commit": "6b0682a4f", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:51e2dfff4bd0aa371a606bd3b599874ec63ef8b2ad64cb55502d8ae8f0b9c951", - "compiled_at": "2026-11-06T21:17:02Z", + "children_root": "sha256:4e06cf9f05a4d9eb2affe519e8f723de83cb8bb332f736bb769817685f80353e", + "compiled_at": "2026-11-06T23:35:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:9d0cdb87ebcf5d2ae6a58d4168ccd36944a7e85c7fb0fffe2bfc1c88ec4a021a", + "digest": "sha256:ccdfba548ea0c8ac54ff1256321ba1ebb9308e4dee36c955009fe16a0fb7b036", "evidence_files": 15689, "evidence_root": "sha256:000da5d93352095d76c788394310aeee389738a37a0ff2b48cd2a85b664bbb74", - "files": 24223, - "generated_at_commit": "e03dfc08cb6614cdb091e7b9be0863702c8d03a8", - "source_tree": "sha256:47f8fee8ad4600f33b7fc9218b95baa6b12241cdb5b3fd5959331657ead53444" + "files": 24259, + "generated_at_commit": "f601985a8ca75b9e072d3d41474240b1a74dda23", + "source_tree": "sha256:48fdcbdd8c2b9de91ad634587d850b6870eebe702664cab2686f0432e3ffa3cb" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 7e1426b3b..d8b4a903a 100644 --- a/Makefile +++ b/Makefile @@ -755,8 +755,8 @@ RX_COMPOSE_LIVING_CHECKS = $(OUT_DIR)/rx_graph.o $(OUT_DIR)/rx_capq.o $(OUT_DIR) # docs/r16-production-entry-point.md has the table. AIEN_TEST_FLAGS = -DAIEN_TEST_BUILD=1 RX_FABRIC_TEST_SRCS = src/fabric/fabric.c src/fabric/fab_hmac.c src/fabric/fab_loopback.c \ - src/fabric/fab_dispatch.c -RX_R13_TEST_CHECKS = tests/runtime/rx_compose_fixture.h src/fabric/fab_dispatch.h \ + src/fabric/fab_dispatch.c src/fabric/fab_route.c +RX_R13_TEST_CHECKS = tests/runtime/rx_compose_fixture.h src/fabric/fab_dispatch.h src/fabric/fab_route.h \ src/fabric/fab_loopback.h src/fabric/fab_hmac.h tests/fabric/fab_living_phase.h # The production build refuses the flag outright. RX_PROD_REFUSE_TEST = $(if $(findstring AIEN_TEST_BUILD,$(CFLAGS))$(findstring RXC_TEST_HOOKS,$(CFLAGS)),\ diff --git a/README.md b/README.md index a11eebb6b..3156dd9cb 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ time, and the R15 parity rig gives both sides a fixed cost table (`r15_use_cost_ ## Repository layout -`src/` existing core and C compiler awaiting replacement (`src/compiler/`), `src/runtime/` reaction runtime, `src/fabric/` host-only fabric interface, `src/physics0/` Physics Zero apparatus, `spec/` and `docs/` specifications, `tests/` gate suites, `evidence/` content-addressed receipts, `tools/` harnesses, `mk/` make fragments included by the `Makefile`. Also `bench/prime_race/` (Prime Drag Race implementation), `calibration/` (Turing calibration experiments, preregistration and blinding protocol), `research/` (experiments and prototypes) and `doctrine/` (the Milestone 4 doctrine). +`src/` existing core and C compiler awaiting replacement (`src/compiler/`), `src/runtime/` reaction runtime, `src/fabric/` host-only Fabric membership and cross-machine work hand-off (see [src/fabric/README.md](src/fabric/README.md)), `src/physics0/` Physics Zero apparatus, `spec/` and `docs/` specifications, `tests/` gate suites, `evidence/` content-addressed receipts, `tools/` harnesses, `mk/` make fragments included by the `Makefile`. Also `bench/prime_race/` (Prime Drag Race implementation), `calibration/` (Turing calibration experiments, preregistration and blinding protocol), `research/` (experiments and prototypes) and `doctrine/` (the Milestone 4 doctrine). ## Human-Directed Agentic Engineering diff --git a/mk/.crumb b/mk/.crumb index 45f8a1796..1e7a656ca 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-11-06T12:58:58Z", + "compiled_at": "2026-11-06T23:35:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:0eca875ed53fd537a25d261ea2a45e5b62796c384263a38b41adca96cad2dcc2", + "digest": "sha256:f1f9dccc612800b7f5e95de069b42f3e7106d62fd8c5b405fa80711401d73009", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 98, - "generated_at_commit": "c3c81ac3d991620742786962c2be16054afc3df4", - "source_tree": "sha256:76442f980ebfcdff74dc6256a7774b002d2980dd9f85426632774e17e58997c1" + "generated_at_commit": "f601985a8ca75b9e072d3d41474240b1a74dda23", + "source_tree": "sha256:e57dc2c298ec7644f9e0a22400fed48a18b64f4e58d2171d953ccefb6b016c7c" }, "owner_lane": "", "provenance": { diff --git a/mk/fabric.mk b/mk/fabric.mk index a113b740b..e7f9926c3 100644 --- a/mk/fabric.mk +++ b/mk/fabric.mk @@ -11,7 +11,18 @@ # through src/runtime/rx_capq.h and links the same objects test-capability-graph # links (rx_capq.o, rx_graph.o, the pinned AIENOS capability library). # +# Heterogeneous compute (aienix#6, omega#536): fab_work.c (PST1 task envelope, +# HTK1 request, HTR1 receipt, requester state machine, executor idempotence +# journal), fab_route.c (pre-dispatch route check, steps 1-3 of the dispatcher) +# and fab_auth_ed25519.c (FabAuth over the vendored AIENOS Ed25519) are +# PRODUCTION-buildable: FAB_PROD_SRCS compiles without AIEN_TEST_BUILD and +# fab-work-purity checks them (the signer may reference only aienos_ed25519_*). +# # test-fabric purity check, then the exit-gate test plain and with ASan/UBSan +# test-fabric-work fab-work-purity, then tests/fabric/fab_work_test.c (PST1 vectors from +# aien-protocols PR #36, HTK1/HTR1 goldens, matmul oracle, state-machine +# negatives, Ed25519 membership) plain and ASan/UBSan, then +# tests/fabric/fab_work_procs.sh (requester and executor as two OS processes) # fabric-purity fabric objects reference no authority, World, socket or process call # fabric-receipt digest-named receipt under evidence/F5-0 (clean tree only) # test-fabric-living Lane 13: a second machine's Skill found and used by the @@ -21,14 +32,17 @@ # same scenario runs inside the R13 test-build variant. ifndef FABRIC_MK FABRIC_MK := 1 -.PHONY: test-fabric test-fabric-living fabric-purity fabric-receipt +.PHONY: test-fabric test-fabric-work test-fabric-living fabric-purity fab-work-purity fabric-receipt FAB_DIR = $(OUT_DIR)/fabric -FAB_CFLAGS = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -Isrc -Isrc/runtime -Isrc/fabric \ - -DAIEN_TEST_BUILD=1 +FAB_PROD_CFLAGS = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -Isrc -Isrc/runtime -Isrc/fabric +FAB_CFLAGS = $(FAB_PROD_CFLAGS) -DAIEN_TEST_BUILD=1 FAB_ASAN = -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all FAB_SRCS = src/fabric/fabric.c src/fabric/fab_hmac.c src/fabric/fab_loopback.c -FAB_LIVING_SRCS = $(FAB_SRCS) src/fabric/fab_dispatch.c +FAB_PROD_SRCS = src/fabric/fab_work.c src/fabric/fab_route.c src/fabric/fab_auth_ed25519.c +FAB_SIG_SRCS = src/searchtrace/sig/ed25519.c src/searchtrace/sig/sha512.c src/searchtrace/sig/ct.c +FAB_LIVING_SRCS = $(FAB_SRCS) src/fabric/fab_dispatch.c src/fabric/fab_route.c FAB_HDRS = src/fabric/fabric.h src/fabric/fab_hmac.h src/fabric/fab_loopback.h src/fabric/fab_dispatch.h \ + src/fabric/fab_work.h src/fabric/fab_route.h src/fabric/fab_auth_ed25519.h \ src/runtime/rx_skillroute.h \ src/runtime/rx_capq.h src/runtime/aien_machine_id.h src/runtime/rx_jspace.h # Routing discovers and carries records; it never grants authority, never @@ -43,10 +57,27 @@ $(FAB_DIR)/%.o: src/fabric/%.c $(FAB_HDRS) @mkdir -p $(FAB_DIR) $(CC) $(FAB_CFLAGS) -c -o $@ $< -fabric-purity: $(FAB_DIR)/fabric.o $(FAB_DIR)/fab_hmac.o $(FAB_DIR)/fab_loopback.o $(FAB_DIR)/fab_dispatch.o +fabric-purity: $(FAB_DIR)/fabric.o $(FAB_DIR)/fab_hmac.o $(FAB_DIR)/fab_loopback.o $(FAB_DIR)/fab_dispatch.o $(FAB_DIR)/fab_route.o @if nm -u $^ | grep -E $(FAB_FORBIDDEN) ; then \ echo "fabric object references an operation the Fabric must not have"; exit 1; fi - @echo "fabric-purity: fabric.o, fab_hmac.o, fab_loopback.o and fab_dispatch.o reference no forbidden symbol" + @echo "fabric-purity: fabric.o, fab_hmac.o, fab_loopback.o, fab_dispatch.o and fab_route.o reference no forbidden symbol" + +# Production objects: compiled WITHOUT AIEN_TEST_BUILD (so a test-only header +# dragged in would refuse), same forbidden list, plus no file/heap/clock call; +# the Ed25519 signer may reference only the vendored aienos_ed25519_* functions. +$(FAB_DIR)/prod/%.o: src/fabric/%.c $(FAB_HDRS) + @mkdir -p $(FAB_DIR)/prod + $(CC) $(FAB_PROD_CFLAGS) -c -o $@ $< + +FAB_WORK_FORBIDDEN = ' U (rx_caproot_|rx_world_|rx_aegis|argus_|ag_cap|socket$$|connect$$|bind$$|listen$$|accept$$|sendto$$|recvfrom$$|fork$$|exec|system$$|dlopen$$|mmap$$|malloc$$|calloc$$|free$$|fopen$$|open$$|read$$|write$$|time$$|clock_gettime$$|getenv$$)' +fab-work-purity: $(FAB_DIR)/prod/fab_work.o $(FAB_DIR)/prod/fab_route.o $(FAB_DIR)/prod/fab_auth_ed25519.o + @if nm -u $^ | grep -E $(FAB_WORK_FORBIDDEN) ; then \ + echo "fab_work/fab_route/fab_auth_ed25519 object references an operation it must not have"; exit 1; fi + @if nm -u $(FAB_DIR)/prod/fab_work.o $(FAB_DIR)/prod/fab_route.o | grep -E ' U aienos_' ; then \ + echo "fab_work.o / fab_route.o must not reference AIENOS authority code"; exit 1; fi + @if nm -u $(FAB_DIR)/prod/fab_auth_ed25519.o | grep -E ' U aienos_' | grep -vE ' U aienos_ed25519_(sign|verify|public_key)$$' ; then \ + echo "fab_auth_ed25519.o may reference only aienos_ed25519_sign/verify/public_key"; exit 1; fi + @echo "fab-work-purity: fab_work.o, fab_route.o and fab_auth_ed25519.o build without AIEN_TEST_BUILD and reference no forbidden symbol" test-fabric: fabric-purity @$(MAKE) --no-print-directory $(RX_CAPQ_OBJ) $(RX_GRAPH_OBJ) $(AIENOS_CAP_LIB) @@ -59,6 +90,21 @@ test-fabric: fabric-purity $(abspath $(FAB_DIR)/fabric_test_asan) > $(FAB_DIR)/asan.out @grep -q '^F5_0_FABRIC_LOOPBACK_PASS$$' $(FAB_DIR)/asan.out && echo "test-fabric: ASan/UBSan run PASS" +# Same link set as test-fabric minus omega_evidence.c (GPU header; nothing in the +# fabric set references it): this is the set a hosted worker compiles too. +FAB_WORK_LINK_SRCS = $(filter-out src/omega_evidence.c,$(FAB_LINK_SRCS)) +FAB_WORK_TEST_SRCS = tests/fabric/fab_work_test.c $(FAB_SRCS) $(FAB_PROD_SRCS) $(FAB_SIG_SRCS) $(FAB_WORK_LINK_SRCS) +test-fabric-work: fab-work-purity fabric-purity + @$(MAKE) --no-print-directory $(RX_CAPQ_OBJ) $(RX_GRAPH_OBJ) $(RX_SKILLROUTE_OBJ) $(AIENOS_CAP_LIB) + $(CC) $(CFLAGS) $(FAB_CFLAGS) -pthread -o $(FAB_DIR)/fab_work_test $(FAB_WORK_TEST_SRCS) \ + $(RX_SKILLROUTE_OBJ) $(RX_CAPQ_OBJ) $(RX_GRAPH_OBJ) $(AIENOS_CAP_LIB) -lm + $(CC) $(CFLAGS) $(FAB_CFLAGS) $(FAB_ASAN) -pthread -o $(FAB_DIR)/fab_work_test_asan $(FAB_WORK_TEST_SRCS) \ + $(RX_SKILLROUTE_OBJ) $(RX_CAPQ_OBJ) $(RX_GRAPH_OBJ) $(AIENOS_CAP_LIB) -lm + $(abspath $(FAB_DIR)/fab_work_test) tests/fabric/vectors/pst + $(abspath $(FAB_DIR)/fab_work_test_asan) tests/fabric/vectors/pst > $(FAB_DIR)/work_asan.out + @grep -q '^FAB_WORK_PASS$$' $(FAB_DIR)/work_asan.out && echo "test-fabric-work: ASan/UBSan run PASS" + tests/fabric/fab_work_procs.sh $(abspath $(FAB_DIR)/fab_work_test) $(abspath $(FAB_DIR)) + test-fabric-living: fabric-purity @$(MAKE) --no-print-directory $(RX_CAPQ_OBJ) $(RX_GRAPH_OBJ) $(RX_SKILLROUTE_OBJ) $(OUT_DIR)/rx_cortex.o $(AIENOS_CAP_LIB) $(CC) $(CFLAGS) $(FAB_CFLAGS) -Itests/runtime -Itests/fabric -pthread -o $(FAB_DIR)/fabric_living_test \ diff --git a/src/.crumb b/src/.crumb index ec1dfaf12..05f7bf147 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,140 +62,140 @@ "children": [ { "digest": "728ab6985dea994eb78a7eb2521384a911492d1c36819c17970a5dc3a8a0c5e2", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "1881ed1d139a405a777d24d4b26f97bc8405dee0552d6ce21841ce4833560ce2", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "92c3e2481f7457afd6c7717e6c1a75ebab950ec9114b48025dd137a6edb2975c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "bd2922f28099a5c7cf959180ac96661b2df3541b457c0941be912c0b9a07790c", - "last_commit": "9de78a19", + "last_commit": "9de78a194", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "36a66980895553ae73884798bc09a92141e1aeba5e5068a7d9d62a99bad02548", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "c75df77ddb2bae35ac5e1225a0ae9dcef77b24608775c7b8d02dce1af0ec3ca4", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "8856d5162c55cb33997201cd580638f80ea933b46e11c61e1913185d419731d7", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { - "digest": "391e337c60cb8b2ae549595394482c23717c1757d83747a7446e81cd780750c1", - "last_commit": "d863780e", + "digest": "dfbc8b09595073b7e9b5b224f6017407f1b412f07350f2556a6354782ff4d067", + "last_commit": "bfab38ea9", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "2d33eb528dafb5954bb027cdd9a7c7923594c177f17181194311155b4e1f40b3", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "e0eee44045097fd7edf9f542b96bfdfc42813ecf6f27c25bd1c57f7adf985bc4", - "last_commit": "f5c9bd89", + "last_commit": "f5c9bd89a", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, { "digest": "2b5dd2c64d4bbba8c41471dc1af5a0b00bfc1361a2c680d78b99fb30b5b547fe", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "e0b91129cc9c188fc0e47b7f3189a3a9a09f9c55eecd018786794571ffc39aad", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "8b923fb5dc1de3b04afae043a4770f7b1fb27d959a8f597191ae2556d822a1cf", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { "digest": "076f486fdf51e12a26160e7f0bf7686fea611db54216f7fef81367620e7a6bdb", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "reservoir", "purpose": "Ionic-droplet reservoir simulator (omega#440): binary32 fixed-step RK4 of the authors' droplet m" }, { "digest": "a2ad0afcac2f217e432e59ac410859cf6d7bab1c22c68a22615bb0b266f0c6a9", - "last_commit": "4883023a", + "last_commit": "4883023a3", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, { "digest": "71502e9ef65e0620b94ea156cef5034e4c97d703d5ea13c489e4b65713014f84", - "last_commit": "97a960f5", + "last_commit": "97a960f5c", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "b82288db7e732d10f954e921ac98c60e19579a3e58118cc12c49230587929ea2", - "last_commit": "ed8de573", + "last_commit": "ed8de5736", "name": "synapse", "purpose": "Synapse ADR 0003 J1 binary codec port: layout list, generator, syn_codec.osc (A1 WAL records + c" }, { "digest": "74e85b5aba1ca8a2e55d40217219a7dbfcec74538b08dfe14be3eb7c3479fbc5", - "last_commit": "0919efc3", + "last_commit": "0919efc3c", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "06d9fb18dddf7ab0f8ad02fffd15ce8b80808c77d0904609b6baae83c2b05ce5", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "812a7faed07b21e066b732ed596eb0aea609379a2b2af0e8b61dd1ea05bef602", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "e832df528a0a0992e817f879c5b7df52c33b8445f30fc2743e5c0fa5ad6e6b2a", - "last_commit": "f5c9bd89", + "last_commit": "f5c9bd89a", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:b37f481112a63fa9eca2551d1b530acda78e73299e872786de35d0f1009d7a28", - "compiled_at": "2026-11-06T21:17:02Z", + "children_root": "sha256:bbf9b18a45cdad39354b8b1533bb2c23026a8da932fcf3e32e62bd4eb9bc7088", + "compiled_at": "2026-11-06T23:35:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:c6ae05f5ca8e295aa12ea3c4ab5143a799adfd377ab40803d9cf5cfe75ea47c9", + "digest": "sha256:a0b48d4ceae8d5ae3cbae673a7e34ed7c8d8c5cd92e2e717316b3260b12e5b54", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 608, - "generated_at_commit": "e03dfc08cb6614cdb091e7b9be0863702c8d03a8", - "source_tree": "sha256:c7f96ef3ac7d95d73c516a4b798150f97fb457272a73d4576188235e47e88788" + "files": 615, + "generated_at_commit": "f601985a8ca75b9e072d3d41474240b1a74dda23", + "source_tree": "sha256:78203f0533ec4d66e976e195bacb1ce9f632ee8b4066ba05a46c7ab78890a0cf" }, "owner_lane": "", "provenance": { diff --git a/src/fabric/.crumb b/src/fabric/.crumb index 44a2fa28c..d1da83cd0 100644 --- a/src/fabric/.crumb +++ b/src/fabric/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-30T16:33:20Z", "compiler": "crumb-compile/1", - "digest": "sha256:391e337c60cb8b2ae549595394482c23717c1757d83747a7446e81cd780750c1", + "digest": "sha256:dfbc8b09595073b7e9b5b224f6017407f1b412f07350f2556a6354782ff4d067", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 8, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:41fef293f2e86fa7e73e4c7c441bed4107c7b41bfed4816cc38b18476a31e201" + "files": 15, + "generated_at_commit": "f06b20cd770bf8febd030d29941de43ff0c26376", + "source_tree": "sha256:1cc0bd1640cc554ddc64220daaa725eab4d29be8b026a28b1d97af8cbb6d2827" }, "owner_lane": "", "provenance": { diff --git a/src/fabric/README.md b/src/fabric/README.md new file mode 100644 index 000000000..8deba5ecb --- /dev/null +++ b/src/fabric/README.md @@ -0,0 +1,74 @@ +# Omega Fabric (`src/fabric/`) + +The Fabric is how separate AIEN machines agree on who is a member, what each one +offers, and how work is handed from one machine to another. Everything here is +plain C with no dynamic allocation, no clocks and no files, so the same code can +run on a coordinator, inside a hosted worker, and later inside AIENOS. + +## What is in this folder + +| File | Role | Builds into production? | +| --- | --- | --- | +| `fabric.h` / `fabric.c` | F5-0 membership: signed JOIN / RENEW / ADVERTISE / LEAVE, leases, generations, replay refusal | yes | +| `fab_auth_ed25519.h` / `.c` | Ed25519 identity for the Fabric: a machine is its public key, and its machine id is derived from that key | yes | +| `fab_route.h` / `.c` | The check a coordinator runs before sending work: pump the Fabric, revalidate the Skill route, require a live remote owner | yes | +| `fab_work.h` / `.c` | Work hand-off: the PST1 message reader and writer, the HTK1 task and HTR1 receipt codecs, the requester state machine and the executor's durable journal | yes | +| `fab_hmac.h` / `.c` | HMAC stand-in authenticator | test only | +| `fab_loopback.h` / `.c` | In-process loopback transport | test only | +| `fab_dispatch.h` / `.c` | Simulated-machine dispatcher (steps 1-3 are `fab_route.c`; only the procedure table lives here) | test only | + +"Test only" files carry an `aien_test_build_*` marker and are refused from any +production binary by `tools/r16_prod_hygiene.sh`. + +## Work hand-off (heterogeneous compute, aienix#6 and omega#536) + +A **requester** sends a PST1 `request` carrying an HTK1 task. An **executor** +answers with `accepted`, `running` and then exactly one terminal message: +`completed` (with the result digest and an HTR1 receipt), `failed` (with a +reason code) or `cancelled`. Every answer is checked against the task id, the +idempotence key and a rising ordinal; anything out of order, from the wrong side, +or for an unknown task is refused and counted. + +Two task kinds are allowlisted today, both with exact recomputable answers: + +- **matmul_i64_seeded**: an N by N integer matrix product on inputs generated from + a seed by splitmix64. The result is the SHA-256 of the big-endian product. +- **tokenize_fixture**: run a pinned tokenizer over a slice of a pinned fixture + (codec and bounds only in this step; the executor does not run it yet). + +The executor journals a task's terminal before it sends it. The same task bytes +asked again replay the journaled answer without re-running; different bytes under +the same key are refused with code 7. A requester that loses the session marks +the task UNCERTAIN and leaves that state only through a signed `resume_snapshot`. + +Nothing in this folder opens a socket. Transports are supplied by the caller +(`FabTransport`), so the loopback test, the two-process pipe test and a real +network adapter all exercise the same code. Loopback and pipes are not a network +and are never reported as one. + +## Tests + +``` +make test-fabric # F5-0 membership, loopback +make test-fabric-work # PST1 vectors, HTK1/HTR1 goldens, matmul oracle, state machines, + # Ed25519 membership, then the same binary under AddressSanitizer, + # then requester and executor as two OS processes over pipes +make test-fabric-living # living-system phase gates +make fabric-purity fab-work-purity # no forbidden symbols in the production objects +``` + +`test-fabric-work` needs the pinned AIENOS capability library; point +`AIENOS_LOCK_REPO` at a checkout holding the locked commit if the default +sibling path is absent. The PST1 vectors in `tests/fabric/vectors/pst/` come from +the protocols repository and are checked byte for byte against `expected.txt`. + +The matmul oracle digests (seed, size, SHA-256 of the product) are recorded in +`tests/fabric/fab_work_test.c` so an independent implementation can reproduce +them. + +## Limits + +This folder proves codec, state machine and journal behaviour in one process and +across a process boundary. It does not by itself prove a second physical machine +ran anything: that evidence comes from the hosted worker adapter and the +three-machine qualification in the benchmarks repository. diff --git a/src/fabric/fab_auth_ed25519.c b/src/fabric/fab_auth_ed25519.c new file mode 100644 index 000000000..dfaf295f0 --- /dev/null +++ b/src/fabric/fab_auth_ed25519.c @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* fab_auth_ed25519.c -- Ed25519 FabAuth over the vendored AIENOS signing code. */ +#include "fab_auth_ed25519.h" +#include "searchtrace/sig/aienos_sig.h" + +#include + +int fab_ed_machine_id(const uint8_t pk[32], AienMachineId *out) { + if (!pk || !out) return FAB_E_ARG; + return aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, pk, 32, out) == AIEN_MID_OK ? FAB_OK : FAB_E_ARG; +} + +const uint8_t *fab_ed_auth_pk_of(const FabEdAuth *a, const AienMachineId *id) { + if (!a || !id) return NULL; + if (aien_mid_equal(&a->self, id)) return a->pk; + for (uint32_t i = 0; i < a->n; i++) + if (aien_mid_equal(&a->peer[i].id, id)) return a->peer[i].pk; + return NULL; +} + +static int ed_sign(void *ctx, const uint8_t *msg, size_t len, uint8_t sig[FAB_SIG_BYTES]) { + const FabEdAuth *a = ctx; + if (!a || !sig || (!msg && len)) return FAB_E_ARG; + return aienos_ed25519_sign(sig, msg, len, a->sk) == AIENOS_SIG_OK ? FAB_OK : FAB_E_AUTH; +} + +static int ed_verify(void *ctx, const AienMachineId *claimed, const uint8_t *msg, size_t len, + const uint8_t sig[FAB_SIG_BYTES]) { + const FabEdAuth *a = ctx; + if (!a || !claimed || !sig || (!msg && len)) return FAB_E_ARG; + const uint8_t *pk = fab_ed_auth_pk_of(a, claimed); + if (!pk) return FAB_E_AUTH; /* not enrolled here: never a silent accept */ + return aienos_ed25519_verify(sig, msg, len, pk) == AIENOS_SIG_OK ? FAB_OK : FAB_E_AUTH; +} + +int fab_ed_auth_init(FabEdAuth *a, const uint8_t sk[32]) { + if (!a || !sk) return FAB_E_ARG; + memset(a, 0, sizeof *a); + memcpy(a->sk, sk, 32); + if (aienos_ed25519_public_key(a->pk, a->sk) != AIENOS_SIG_OK || + fab_ed_machine_id(a->pk, &a->self) != FAB_OK) { + fab_ed_auth_wipe(a); + return FAB_E_ARG; + } + a->auth.ctx = a; + a->auth.sign = ed_sign; + a->auth.verify = ed_verify; + return FAB_OK; +} + +int fab_ed_auth_add_peer(FabEdAuth *a, const uint8_t pk[32]) { + if (!a || !pk) return FAB_E_ARG; + if (a->n >= FAB_ED_MAX_PEERS) return FAB_E_FULL; + FabEdPeer p; + memcpy(p.pk, pk, 32); + if (fab_ed_machine_id(p.pk, &p.id) != FAB_OK) return FAB_E_ARG; + if (fab_ed_auth_pk_of(a, &p.id)) return FAB_E_ARG; /* duplicate, or our own key */ + a->peer[a->n++] = p; + return FAB_OK; +} + +int fab_ed_auth_roster(const FabEdAuth *a, AienMachineId *ids, uint32_t cap, uint32_t *n) { + if (!a || !ids || !n) return FAB_E_ARG; + if (cap < a->n + 1u) return FAB_E_FULL; + ids[0] = a->self; + for (uint32_t i = 0; i < a->n; i++) ids[i + 1] = a->peer[i].id; + *n = a->n + 1u; + return FAB_OK; +} + +void fab_ed_auth_wipe(FabEdAuth *a) { + if (!a) return; + volatile uint8_t *p = (volatile uint8_t *)a; + for (size_t i = 0; i < sizeof *a; i++) p[i] = 0; +} diff --git a/src/fabric/fab_auth_ed25519.h b/src/fabric/fab_auth_ed25519.h new file mode 100644 index 000000000..8dcffb143 --- /dev/null +++ b/src/fabric/fab_auth_ed25519.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * fab_auth_ed25519.h -- the Fabric signer for real machines (aienix#6, omega#536). + * + * FabAuth (fabric.h) is an interface: fabric.c signs every message it seals and + * verifies every message it receives through it, naming the claimed sender by + * its AienMachineId. F5-0 ships only an HMAC stand-in (fab_hmac.h, test build). + * This file is the production signer: Ed25519 over the exact AFAB bytes, with + * the signing code the repository already vendors from AIENOS + * (src/searchtrace/sig/, aienos native/sig @913b962; see PROVENANCE.txt there). + * No new cryptography is written here. + * + * Identity: one 32-byte Ed25519 seed per machine (RFC 8032 secret key). The + * machine's canonical identity is derived from its public key, + * AienMachineId = aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, pk, 32), + * so a roster entry is the public key alone; the id is never typed by hand. + * Class: lab-provisioned. This is not TRUST-1 owner enrolment (AIEN_MID_ROOT_ + * HARDWARE); a receipt made under such a key must say identity_class=lab. + * + * verify(claimed, msg, sig): the claimed id must be this node's own id or one + * of the enrolled peers; its public key verifies the signature. Anything else + * is a refusal (nonzero). fabric.c has already checked the roster before it + * asks, so an unknown id here is FAB_E_AUTH, never a silent accept. + * + * The seed stays in this struct only; fab_ed_auth_wipe zeroes it. Nothing here + * reads a file, opens a socket or prints: the caller loads the seed bytes. + */ +#ifndef FAB_AUTH_ED25519_H +#define FAB_AUTH_ED25519_H + +#include "fabric.h" + +#define FAB_ED_MAX_PEERS 64u + +typedef struct { + AienMachineId id; + uint8_t pk[32]; +} FabEdPeer; + +typedef struct { + uint8_t sk[32]; /* this machine's seed; wiped by fab_ed_auth_wipe */ + uint8_t pk[32]; + AienMachineId self; + uint32_t n; + FabEdPeer peer[FAB_ED_MAX_PEERS]; + FabAuth auth; /* points back at this signer */ +} FabEdAuth; + +/* The canonical machine id of a public key. FAB_E_ARG on a null pointer. */ +int fab_ed_machine_id(const uint8_t pk[32], AienMachineId *out); + +/* Loads the seed, derives pk and self. The peer list starts empty. */ +int fab_ed_auth_init(FabEdAuth *a, const uint8_t sk[32]); + +/* Enrols a peer by public key (its id is derived). FAB_E_FULL when the list + * is full, FAB_E_ARG for a duplicate or for this machine's own key. */ +int fab_ed_auth_add_peer(FabEdAuth *a, const uint8_t pk[32]); + +/* Writes self + every peer into ids (the FabRoster a node is configured with). + * FAB_E_FULL when cap is too small; *n is the count written. */ +int fab_ed_auth_roster(const FabEdAuth *a, AienMachineId *ids, uint32_t cap, uint32_t *n); + +/* The public key enrolled for `id`, or NULL. */ +const uint8_t *fab_ed_auth_pk_of(const FabEdAuth *a, const AienMachineId *id); + +/* Zeroes the seed and the struct. */ +void fab_ed_auth_wipe(FabEdAuth *a); + +#endif /* FAB_AUTH_ED25519_H */ diff --git a/src/fabric/fab_dispatch.c b/src/fabric/fab_dispatch.c index 2f12ccbfb..365dfa87e 100644 --- a/src/fabric/fab_dispatch.c +++ b/src/fabric/fab_dispatch.c @@ -14,8 +14,12 @@ * limitations under the License. */ -/* fab_dispatch.c -- Fabric dispatch of remote composition candidates (fab_dispatch.h). */ +/* fab_dispatch.c -- Fabric dispatch of remote composition candidates (fab_dispatch.h). + * Steps 1-3 (pump, route revalidation, live remote member) are fab_route.c, the + * production-buildable check a real coordinator runs before it sends; only + * step 4 (the simulated procedure table) lives here. */ #include "fab_dispatch.h" +#include "fab_route.h" #include @@ -57,18 +61,7 @@ int fab_dispatch_add(FabDispatch *d, const AienMachineId *m, const AgSkillTable /* Caller holds the lock. */ static int pump_locked(FabDispatch *d, uint64_t now_us) { - int n = 0; - FabVerdict v; - for (;;) { - int rc = fab_poll(d->node, now_us, &v); - if (rc < 0) return rc; - if (rc == 0) break; - n++; - d->polled++; - if (v.code != FAB_OK) d->refused_msgs++; - } - (void)fab_tick(d->node, now_us); - return n; + return fab_route_pump(d->node, now_us, &d->polled, &d->refused_msgs); } int fab_dispatch_pump(FabDispatch *d, uint64_t now_us) { @@ -91,22 +84,21 @@ int fab_dispatch_run(void *ctx, const SrRouter *router, const SrRoute *route, ui if (!d || !d->node) return FAB_DX_ARG; pthread_mutex_lock(&d->mu); int rc; - if (!router || !route || !result || router->graph != d->node->cfg.catalog) { - rc = refuse(d, FAB_DX_ARG); - goto out; - } - if (pump_locked(d, now_us) < 0) { rc = refuse(d, FAB_DX_TRANSPORT); goto out; } - if (sr_route_check(router, route, now_us) != SR_E_REMOTE) { rc = refuse(d, FAB_DX_ROUTE); goto out; } - if (!route->target_known) { rc = refuse(d, FAB_DX_TARGET); goto out; } - JsHome home; - if (fab_home(d->node, &route->target, now_us, &home) != FAB_OK || - home.locality != JS_HOME_REMOTE_OWNED) { - rc = refuse(d, FAB_DX_NOT_MEMBER); - goto out; + if (!result) { rc = refuse(d, FAB_DX_ARG); goto out; } + /* Steps 1-3: fab_route.c (shared with the production coordinator). */ + AienMachineId target; + switch (fab_route_check(d->node, router, route, now_us, &target, &d->polled, &d->refused_msgs)) { + case FAB_RT_OK: break; + case FAB_RT_ARG: rc = refuse(d, FAB_DX_ARG); goto out; + case FAB_RT_TRANSPORT: rc = refuse(d, FAB_DX_TRANSPORT); goto out; + case FAB_RT_ROUTE: rc = refuse(d, FAB_DX_ROUTE); goto out; + case FAB_RT_TARGET: rc = refuse(d, FAB_DX_TARGET); goto out; + default: rc = refuse(d, FAB_DX_NOT_MEMBER); goto out; } + /* Step 4 (loopback stand-in only): the simulated machine's procedure table. */ const FabExecutor *ex = NULL; for (uint32_t i = 0; i < d->n_exec && !ex; i++) - if (aien_mid_equal(&d->exec[i].machine, &route->target)) ex = &d->exec[i]; + if (aien_mid_equal(&d->exec[i].machine, &target)) ex = &d->exec[i]; const AgSkill *s = NULL; for (uint32_t i = 0; ex && i < ex->skills->n && !s; i++) if (ex->skills->skill[i].id == route->chosen.skill_id) s = &ex->skills->skill[i]; diff --git a/src/fabric/fab_route.c b/src/fabric/fab_route.c new file mode 100644 index 000000000..1e458c769 --- /dev/null +++ b/src/fabric/fab_route.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* fab_route.c -- pre-dispatch route checks (fab_route.h). */ +#include "fab_route.h" + +int fab_route_pump(FabNode *n, uint64_t now_us, uint64_t *polled, uint64_t *refused_msgs) { + if (!n) return FAB_E_ARG; + int applied = 0; + FabVerdict v; + for (;;) { + int rc = fab_poll(n, now_us, &v); + if (rc < 0) return rc; + if (rc == 0) break; + applied++; + if (polled) (*polled)++; + if (v.code != FAB_OK && refused_msgs) (*refused_msgs)++; + } + (void)fab_tick(n, now_us); + return applied; +} + +int fab_route_check(FabNode *n, const SrRouter *router, const SrRoute *route, uint64_t now_us, + AienMachineId *target, uint64_t *polled, uint64_t *refused_msgs) { + if (!n || !router || !route || !target || router->graph != n->cfg.catalog) return FAB_RT_ARG; + if (fab_route_pump(n, now_us, polled, refused_msgs) < 0) return FAB_RT_TRANSPORT; + if (sr_route_check(router, route, now_us) != SR_E_REMOTE) return FAB_RT_ROUTE; + if (!route->target_known) return FAB_RT_TARGET; + JsHome home; + if (fab_home(n, &route->target, now_us, &home) != FAB_OK || home.locality != JS_HOME_REMOTE_OWNED) + return FAB_RT_NOT_MEMBER; + *target = route->target; + return FAB_RT_OK; +} + +const char *fab_route_strerror(int code) { + switch (code) { + case FAB_RT_OK: return "ok"; + case FAB_RT_ARG: return "bad call or foreign graph"; + case FAB_RT_TRANSPORT: return "fabric transport error while pumping"; + case FAB_RT_ROUTE: return "route no longer remote (withdrawn, stale or lease ended)"; + case FAB_RT_TARGET: return "route has no canonical target machine"; + case FAB_RT_NOT_MEMBER: return "target is not a live remote member"; + default: return "unknown fab_route code"; + } +} diff --git a/src/fabric/fab_route.h b/src/fabric/fab_route.h new file mode 100644 index 000000000..a06836958 --- /dev/null +++ b/src/fabric/fab_route.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * fab_route.h -- the checks a Fabric node makes before it sends work to a + * remote machine (aienix#6, omega#536). Production-buildable. + * + * These are steps 1 to 3 of fab_dispatch.h, lifted out of the test-only + * in-process dispatcher so a real coordinator can run them before it hands a + * task to the transport. Step 4 of the dispatcher (the simulated procedure + * table) is NOT here and is never promoted: whether the remote machine ran + * what it advertised is answered by the executor's receipt (fab_work.h HTR1, + * implementation_digest against the advertised skill_digest), not by this + * process. + * + * 1. fab_route_pump: apply waiting Fabric traffic and the clock, so a LEAVE + * or an ended lease withdraws the machine's entries first; + * 2. the route must still be SR_E_REMOTE under sr_route_check (entry live, + * same generation, version and digest, machine identity unchanged); + * 3. the route's machine must be a live member of this node whose home is + * JS_HOME_REMOTE_OWNED: never the local machine, never a lost one. + * Any failure refuses and names why. The caller holds whatever lock protects + * the node; this file takes none (fab_dispatch.c keeps its own). + */ +#ifndef FAB_ROUTE_H +#define FAB_ROUTE_H + +#include "fabric.h" +#include "runtime/rx_skillroute.h" + +enum { + FAB_RT_OK = 0, + FAB_RT_ARG, /* bad call, or the router's graph is not this node's catalog */ + FAB_RT_TRANSPORT, /* applying waiting Fabric traffic failed */ + FAB_RT_ROUTE, /* sr_route_check is no longer SR_E_REMOTE */ + FAB_RT_TARGET, /* route has no canonical target machine */ + FAB_RT_NOT_MEMBER, /* target is not a live remote member of this node */ + FAB_RT_N +}; + +/* Step 1. Applies every waiting message, then the clock. Returns the number of + * messages applied, or <0 (a FAB_E_* transport code). polled / refused_msgs, + * when given, are increased by the messages seen / refused. */ +int fab_route_pump(FabNode *n, uint64_t now_us, uint64_t *polled, uint64_t *refused_msgs); + +/* Steps 1 to 3. FAB_RT_OK with *target = the live remote machine to send to, + * else the FAB_RT_* refusal (target untouched). */ +int fab_route_check(FabNode *n, const SrRouter *router, const SrRoute *route, uint64_t now_us, + AienMachineId *target, uint64_t *polled, uint64_t *refused_msgs); + +const char *fab_route_strerror(int code); + +#endif /* FAB_ROUTE_H */ diff --git a/src/fabric/fab_work.c b/src/fabric/fab_work.c new file mode 100644 index 000000000..6b0dc9a3d --- /dev/null +++ b/src/fabric/fab_work.c @@ -0,0 +1,712 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* fab_work.c -- PST1 / HTK1 / HTR1 codec, requester state machine, executor journal (fab_work.h). */ +#include "fab_work.h" +#include "sha256.h" + +#include +#include + +/* ---- byte helpers (big-endian on the wire) ---- */ +static uint16_t g16(const uint8_t *p) { return (uint16_t)((p[0] << 8) | p[1]); } +static uint32_t g32(const uint8_t *p) { return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; } +static uint64_t g64(const uint8_t *p) { return ((uint64_t)g32(p) << 32) | g32(p + 4); } +static void p16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } +static void p32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; } +static void p64(uint8_t *p, uint64_t v) { p32(p, (uint32_t)(v >> 32)); p32(p + 4, (uint32_t)v); } +static int all_zero(const uint8_t *p, size_t n) { uint8_t a = 0; for (size_t i = 0; i < n; i++) a |= p[i]; return a == 0; } + +/* ================= PST1 ================= */ +enum { + O_MAGIC = 0, O_VERSION = 4, O_TYPE = 5, O_RES0 = 6, O_CAUSE = 8, O_ORIGIN = 40, + O_OBJECTIVE = 72, O_SUBJECT = 104, O_PARENT = 136, O_TASK = 152, O_IDEM = 168, + O_ORDINAL = 184, O_EFFECT = 188, O_RES1 = 189, O_PLEN = 190, O_DIGEST = 192 +}; + +static const char *const pst_names[PST_CODE_COUNT] = { + "OK", "PST_BAD_MAGIC", "PST_BAD_VERSION", "PST_DOWNGRADE", "PST_BAD_TYPE", + "PST_WRONG_SIDE", "PST_BAD_LENGTH", "PST_TRAILING_BYTES", "PST_RESERVED_NONZERO", + "PST_NO_CAUSE", "PST_NO_TASK", "PST_NO_OBJECTIVE", "PST_DIGEST_MISMATCH", + "PST_BAD_EFFECT_CLASS", "PST_SUBJECT_MISMATCH" +}; + +const char *pst_code_name(int code) { + return code >= 0 && code < PST_CODE_COUNT ? pst_names[code] : "PST_UNKNOWN_CODE"; +} + +/* Field rules shared by decode (on the wire bytes) and encode (on the struct): + * steps 3, 4, 8, 9, 10 and 11 of SPEC.md section 4.1. */ +static int pst_field_rules(uint8_t version, uint8_t type, uint8_t effect, const uint8_t *cause, + const uint8_t *task, const uint8_t *idem, const uint8_t *objective) { + if (version < PST_VERSION) return PST_DOWNGRADE; + if (version > PST_VERSION) return PST_BAD_VERSION; + if (type == 0 || type > PST_MAX_TYPE) return PST_BAD_TYPE; + if (effect > PST_EFFECT_EFFECT) return PST_BAD_EFFECT_CLASS; + if (all_zero(cause, 32)) return PST_NO_CAUSE; + if (all_zero(task, 16)) return PST_NO_TASK; + if (all_zero(idem, 16)) return PST_NO_TASK; + if (type == PST_T_REQUEST && all_zero(objective, 32)) return PST_NO_OBJECTIVE; + return PST_OK; +} + +int pst_decode(const uint8_t *b, size_t n, PstMsg *m) { + if (!b || !m) return PST_BAD_LENGTH; + memset(m, 0, sizeof *m); + /* 1. length bounds */ + if (n < PST_HDR) return PST_BAD_LENGTH; + if (n > PST_MAX_MSG) return PST_BAD_LENGTH; + /* 2. magic */ + if (memcmp(b + O_MAGIC, "PST1", 4) != 0) return PST_BAD_MAGIC; + /* 3. version 4. type (field rules continue below after the length checks, as the checker orders them) */ + if (b[O_VERSION] < PST_VERSION) return PST_DOWNGRADE; + if (b[O_VERSION] > PST_VERSION) return PST_BAD_VERSION; + if (b[O_TYPE] == 0 || b[O_TYPE] > PST_MAX_TYPE) return PST_BAD_TYPE; + /* 5. reserved */ + if (!all_zero(b + O_RES0, 2)) return PST_RESERVED_NONZERO; + if (b[O_RES1] != 0) return PST_RESERVED_NONZERO; + /* 6, 7. payload length against the bytes present */ + m->payload_len = g16(b + O_PLEN); + size_t body = n - PST_HDR; + if (body < m->payload_len) return PST_BAD_LENGTH; + if (body > m->payload_len) return PST_TRAILING_BYTES; + /* 8..11. field rules */ + int rc = pst_field_rules(b[O_VERSION], b[O_TYPE], b[O_EFFECT], b + O_CAUSE, b + O_TASK, b + O_IDEM, b + O_OBJECTIVE); + if (rc != PST_OK) return rc; + /* 12. payload digest */ + uint8_t d[32]; + sha256_hash(b + PST_HDR, m->payload_len, d); + if (memcmp(d, b + O_DIGEST, 32) != 0) return PST_DIGEST_MISMATCH; + + m->version = b[O_VERSION]; + m->type = b[O_TYPE]; + memcpy(m->cause, b + O_CAUSE, 32); + memcpy(m->origin, b + O_ORIGIN, 32); + memcpy(m->objective, b + O_OBJECTIVE, 32); + memcpy(m->subject, b + O_SUBJECT, 32); + memcpy(m->parent, b + O_PARENT, 16); + memcpy(m->task, b + O_TASK, 16); + memcpy(m->idem, b + O_IDEM, 16); + m->ordinal = g32(b + O_ORDINAL); + m->effect = b[O_EFFECT]; + memcpy(m->digest, d, 32); + m->payload = b + PST_HDR; + return PST_OK; +} + +int pst_encode(const PstMsg *m, const uint8_t *payload, size_t payload_len, + uint8_t *out, size_t cap, size_t *out_len) { + if (!m || !out || !out_len || (!payload && payload_len)) return PST_BAD_LENGTH; + if (payload_len > PST_MAX_PAY || cap < PST_HDR + payload_len) return PST_BAD_LENGTH; + int rc = pst_field_rules(m->version, m->type, m->effect, m->cause, m->task, m->idem, m->objective); + if (rc != PST_OK) return rc; + memset(out, 0, PST_HDR); + memcpy(out + O_MAGIC, "PST1", 4); + out[O_VERSION] = m->version; + out[O_TYPE] = m->type; + memcpy(out + O_CAUSE, m->cause, 32); + memcpy(out + O_ORIGIN, m->origin, 32); + memcpy(out + O_OBJECTIVE, m->objective, 32); + memcpy(out + O_SUBJECT, m->subject, 32); + memcpy(out + O_PARENT, m->parent, 16); + memcpy(out + O_TASK, m->task, 16); + memcpy(out + O_IDEM, m->idem, 16); + p32(out + O_ORDINAL, m->ordinal); + out[O_EFFECT] = m->effect; + p16(out + O_PLEN, (uint16_t)payload_len); + sha256_hash(payload ? payload : out, payload_len, out + O_DIGEST); /* out: any pointer for len 0 */ + if (payload_len) memcpy(out + PST_HDR, payload, payload_len); + *out_len = PST_HDR + payload_len; + return PST_OK; +} + +int pst_side_of(uint8_t type) { + if (type == 0 || type > PST_MAX_TYPE) return 0; + return (type == PST_T_REQUEST || type == PST_T_CANCEL_REQUEST || type == PST_T_RESUME_REQUEST) + ? PST_ROLE_REQUESTER : PST_ROLE_EXECUTOR; +} + +int pst_side_check(uint8_t type, int sender_role) { + int s = pst_side_of(type); + return s && s == sender_role ? PST_OK : PST_WRONG_SIDE; +} + +static char *hexw(char *o, const uint8_t *p, size_t n) { + static const char hx[] = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { *o++ = hx[p[i] >> 4]; *o++ = hx[p[i] & 15]; } + *o = 0; + return o; +} + +void pst_verdict_line(int code, const PstMsg *m, char *buf, size_t cap) { + char c[65], o[65], ob[65], s[65], pa[33], t[33], k[33], d[65]; + if (!buf || !cap) return; + if (code != PST_OK || !m) { + snprintf(buf, cap, "refuse %s", pst_code_name(code)); + return; + } + hexw(c, m->cause, 32); hexw(o, m->origin, 32); hexw(ob, m->objective, 32); hexw(s, m->subject, 32); + hexw(pa, m->parent, 16); hexw(t, m->task, 16); hexw(k, m->idem, 16); hexw(d, m->digest, 32); + snprintf(buf, cap, "ok type=%u version=%u cause=%s origin=%s objective=%s subject=%s parent=%s task=%s idem=%s ordinal=%lu effect=%u len=%u digest=%s", + m->type, m->version, c, o, ob, s, pa, t, k, (unsigned long)m->ordinal, m->effect, m->payload_len, d); +} + +/* ================= HTK1 ================= */ +static const char *const htk_names[HTK_CODE_COUNT] = { + "HTK_OK", "HTK_BAD_MAGIC", "HTK_BAD_VERSION", "HTK_BAD_KIND", "HTK_RESERVED_NONZERO", + "HTK_BAD_LENGTH", "HTK_BAD_BOUNDS", "HTK_INPUT_DIGEST_MISMATCH" +}; +const char *htk_code_name(int code) { return code >= 0 && code < HTK_CODE_COUNT ? htk_names[code] : "HTK_UNKNOWN_CODE"; } + +static size_t htk_param_len(uint8_t kind) { + if (kind == HTK_KIND_MATMUL_I64_SEEDED) return 12; + if (kind == HTK_KIND_TOKENIZE_FIXTURE) return 72; + return 0; +} + +size_t htk_input_bytes(const HtkTask *t, uint8_t out[HTK_INPUT_MAX]) { + if (!t || !out) return 0; + if (t->kind == HTK_KIND_MATMUL_I64_SEEDED) { + memcpy(out, "HTK1-MATMUL", 11); + p32(out + 11, t->matmul.n); + p64(out + 15, t->matmul.seed); + return 23; + } + if (t->kind == HTK_KIND_TOKENIZE_FIXTURE) { + memcpy(out, t->tok.fixture_digest, 32); + p32(out + 32, t->tok.offset); + p32(out + 36, t->tok.length); + memcpy(out + 40, t->tok.tokenizer_digest, 32); + return 72; + } + return 0; +} + +void htk_input_digest(const HtkTask *t, uint8_t out[32]) { + uint8_t in[HTK_INPUT_MAX]; + size_t n = htk_input_bytes(t, in); + sha256_hash(in, n, out); +} + +uint64_t htk_result_bytes(const HtkTask *t) { + if (t && t->kind == HTK_KIND_MATMUL_I64_SEEDED) return (uint64_t)t->matmul.n * t->matmul.n * 8u; + return 0; +} + +static int htk_bounds(const HtkTask *t) { + if (t->deadline_ms == 0 || t->deadline_ms > HTK_DEADLINE_MS_MAX) return HTK_BAD_BOUNDS; + if (t->max_out_bytes == 0 || t->max_out_bytes > HTK_OUT_BYTES_MAX) return HTK_BAD_BOUNDS; + if (t->kind == HTK_KIND_MATMUL_I64_SEEDED) { + if (t->matmul.n == 0 || t->matmul.n > HTK_MATMUL_N_MAX) return HTK_BAD_BOUNDS; + } else if (t->kind == HTK_KIND_TOKENIZE_FIXTURE) { + if (t->tok.length == 0 || t->tok.length > HTK_TOKENIZE_LEN_MAX) return HTK_BAD_BOUNDS; + if ((uint64_t)t->tok.offset + t->tok.length > UINT32_MAX) return HTK_BAD_BOUNDS; + } else { + return HTK_BAD_KIND; + } + return HTK_OK; +} + +int htk_encode(const HtkTask *t, uint8_t *out, size_t cap, size_t *len) { + if (!t || !out || !len) return HTK_BAD_LENGTH; + size_t pl = htk_param_len(t->kind); + if (!pl) return HTK_BAD_KIND; + if (cap < HTK_HDR + pl) return HTK_BAD_LENGTH; + int rc = htk_bounds(t); + if (rc != HTK_OK) return rc; + memset(out, 0, HTK_HDR + pl); + memcpy(out, "HTK1", 4); + out[4] = 1; + out[5] = t->kind; + p32(out + 8, t->deadline_ms); + p32(out + 12, t->max_out_bytes); + p64(out + 16, t->max_mem_bytes); + p32(out + 24, t->max_in_bytes); + p32(out + 28, t->threads); + htk_input_digest(t, out + 32); + if (t->kind == HTK_KIND_MATMUL_I64_SEEDED) { + p32(out + 64, t->matmul.n); + p64(out + 68, t->matmul.seed); + } else { + memcpy(out + 64, t->tok.fixture_digest, 32); + p32(out + 96, t->tok.offset); + p32(out + 100, t->tok.length); + memcpy(out + 104, t->tok.tokenizer_digest, 32); + } + *len = HTK_HDR + pl; + return HTK_OK; +} + +int htk_decode(const uint8_t *b, size_t n, HtkTask *t) { + if (!b || !t) return HTK_BAD_LENGTH; + memset(t, 0, sizeof *t); + if (n < HTK_HDR) return HTK_BAD_LENGTH; + if (memcmp(b, "HTK1", 4) != 0) return HTK_BAD_MAGIC; + if (b[4] != 1) return HTK_BAD_VERSION; + size_t pl = htk_param_len(b[5]); + if (!pl) return HTK_BAD_KIND; + if (!all_zero(b + 6, 2)) return HTK_RESERVED_NONZERO; + if (n != HTK_HDR + pl) return HTK_BAD_LENGTH; + t->kind = b[5]; + t->deadline_ms = g32(b + 8); + t->max_out_bytes = g32(b + 12); + t->max_mem_bytes = g64(b + 16); + t->max_in_bytes = g32(b + 24); + t->threads = g32(b + 28); + memcpy(t->input_digest, b + 32, 32); + if (t->kind == HTK_KIND_MATMUL_I64_SEEDED) { + t->matmul.n = g32(b + 64); + t->matmul.seed = g64(b + 68); + } else { + memcpy(t->tok.fixture_digest, b + 64, 32); + t->tok.offset = g32(b + 96); + t->tok.length = g32(b + 100); + memcpy(t->tok.tokenizer_digest, b + 104, 32); + } + int rc = htk_bounds(t); + if (rc != HTK_OK) return rc; + uint8_t d[32]; + htk_input_digest(t, d); + if (memcmp(d, t->input_digest, 32) != 0) return HTK_INPUT_DIGEST_MISMATCH; + return HTK_OK; +} + +uint64_t htk_splitmix(uint64_t seed, uint64_t stream, uint64_t index) { + uint64_t z = seed + (index + 1u) * 0x9E3779B97F4A7C15ull + stream * 0xD1B54A32D192ED03ull; + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; + return z ^ (z >> 31); +} + +int htk_matmul_i64(uint32_t n, uint64_t seed, int64_t *c) { + if (!c || n == 0 || n > HTK_MATMUL_N_MAX) return HTK_BAD_BOUNDS; + for (uint32_t i = 0; i < n; i++) + for (uint32_t j = 0; j < n; j++) { + uint64_t acc = 0; /* wrapping i64 == wrapping u64 bit-for-bit */ + for (uint32_t k = 0; k < n; k++) { + uint64_t a = htk_splitmix(seed, 0, (uint64_t)i * n + k); + uint64_t b = htk_splitmix(seed, 1, (uint64_t)k * n + j); + acc += a * b; + } + c[(size_t)i * n + j] = (int64_t)acc; + } + return HTK_OK; +} + +void htk_matmul_result_bytes(uint32_t n, const int64_t *c, uint8_t *out) { + size_t total = (size_t)n * n; + for (size_t i = 0; i < total; i++) p64(out + 8 * i, (uint64_t)c[i]); +} + +int htk_matmul_result_digest(uint32_t n, uint64_t seed, int64_t *scratch, uint8_t out[32]) { + if (!scratch || !out) return HTK_BAD_BOUNDS; + int rc = htk_matmul_i64(n, seed, scratch); + if (rc != HTK_OK) return rc; + htk_matmul_result_bytes(n, scratch, (uint8_t *)scratch); /* in place: 8 bytes per value */ + sha256_hash((const uint8_t *)scratch, (size_t)n * n * 8u, out); + return HTK_OK; +} + +/* ================= HTR1 ================= */ +static const char *const htr_names[HTR_CODE_COUNT] = { + "HTR_OK", "HTR_BAD_MAGIC", "HTR_BAD_VERSION", "HTR_BAD_LENGTH", "HTR_RESERVED_NONZERO", + "HTR_BAD_FLAGS", "HTR_BAD_MACHINE", "HTR_BAD_KIND" +}; +const char *htr_code_name(int code) { return code >= 0 && code < HTR_CODE_COUNT ? htr_names[code] : "HTR_UNKNOWN_CODE"; } + +int htr_encode(const HtrReceipt *r, uint8_t out[HTR_BYTES]) { + if (!r || !out) return HTR_BAD_LENGTH; + if (r->version != 1) return HTR_BAD_VERSION; + if (!htk_param_len(r->kind)) return HTR_BAD_KIND; + if (r->flags & ~3u) return HTR_BAD_FLAGS; + memset(out, 0, HTR_BYTES); + memcpy(out, "HTR1", 4); + out[4] = r->version; + out[5] = r->kind; + p16(out + 6, r->flags); + memcpy(out + 8, r->run_id, 16); + if (aien_mid_encode(&r->executor, out + 24) != AIEN_MID_OK) return HTR_BAD_MACHINE; + memcpy(out + 68, r->implementation_digest, 32); + memcpy(out + 100, r->input_digest, 32); + memcpy(out + 132, r->result_digest, 32); + p64(out + 164, r->t_accept_us); + p64(out + 172, r->t_start_us); + p64(out + 180, r->t_end_us); + p64(out + 188, r->cpu_ns); + p64(out + 196, r->peak_rss_bytes); + p16(out + 204, r->threads_used); + memcpy(out + 208, r->os_arch, HTR_OS_ARCH); + memcpy(out + 224, r->identity_class, 4); + return HTR_OK; +} + +int htr_decode(const uint8_t *b, size_t n, HtrReceipt *r) { + if (!b || !r) return HTR_BAD_LENGTH; + memset(r, 0, sizeof *r); + if (n != HTR_BYTES) return HTR_BAD_LENGTH; + if (memcmp(b, "HTR1", 4) != 0) return HTR_BAD_MAGIC; + if (b[4] != 1) return HTR_BAD_VERSION; + if (!htk_param_len(b[5])) return HTR_BAD_KIND; + uint16_t flags = g16(b + 6); + if (flags & ~3u) return HTR_BAD_FLAGS; + if (!all_zero(b + 206, 2)) return HTR_RESERVED_NONZERO; + if (aien_mid_decode(b + 24, AIEN_MID_RECORD_BYTES, &r->executor) != AIEN_MID_OK) return HTR_BAD_MACHINE; + r->version = b[4]; + r->kind = b[5]; + r->flags = flags; + memcpy(r->run_id, b + 8, 16); + memcpy(r->implementation_digest, b + 68, 32); + memcpy(r->input_digest, b + 100, 32); + memcpy(r->result_digest, b + 132, 32); + r->t_accept_us = g64(b + 164); + r->t_start_us = g64(b + 172); + r->t_end_us = g64(b + 180); + r->cpu_ns = g64(b + 188); + r->peak_rss_bytes = g64(b + 196); + r->threads_used = g16(b + 204); + memcpy(r->os_arch, b + 208, HTR_OS_ARCH); + memcpy(r->identity_class, b + 224, 4); + return HTR_OK; +} + +void htr_reference(const uint8_t *bytes, size_t len, uint8_t out[32]) { sha256_hash(bytes, len, out); } + +/* ================= payloads ================= */ +int fw_completed_payload(const uint8_t result_digest[32], const HtrReceipt *r, + uint8_t *out, size_t cap, size_t *len) { + if (!result_digest || !out || !len) return -1; + size_t need = 32u + (r ? HTR_BYTES : 0u); + if (cap < need) return -1; + memcpy(out, result_digest, 32); + if (r && htr_encode(r, out + 32) != HTR_OK) return -1; + *len = need; + return 0; +} + +int fw_failed_payload(uint16_t code, const char *reason, uint8_t *out, size_t cap, size_t *len) { + if (!out || !len || cap < 2) return -1; + size_t rl = reason ? strlen(reason) : 0; + if (rl > FW_REASON_MAX) rl = FW_REASON_MAX; + if (cap < 2 + rl) return -1; + p16(out, code); + if (rl) memcpy(out + 2, reason, rl); + *len = 2 + rl; + return 0; +} + +/* ================= requester ================= */ +static const char *const req_names[FW_R_N] = { + "ok", "wrong side", "unknown task", "ordinal", "already terminal", "state", "bad payload", + "receipt peer", "receipt input", "result digest", "unsupported type", "full" +}; +const char *fw_req_code_name(int code) { return code >= 0 && code < FW_R_N ? req_names[code] : "?"; } + +int fw_state_terminal(uint32_t s) { return s == FW_ST_COMPLETED || s == FW_ST_FAILED || s == FW_ST_CANCELLED; } + +const char *fw_state_name(uint32_t s) { + switch (s) { + case FW_ST_SENT: return "SENT"; + case FW_ST_ACCEPTED: return "ACCEPTED"; + case FW_ST_RUNNING: return "RUNNING"; + case FW_ST_COMPLETED: return "COMPLETED"; + case FW_ST_FAILED: return "FAILED"; + case FW_ST_CANCELLED: return "CANCELLED"; + case FW_ST_UNCERTAIN: return "UNCERTAIN"; + default: return "?"; + } +} + +void fw_req_init(FwRequester *r) { if (r) memset(r, 0, sizeof *r); } + +FwTask *fw_req_find(FwRequester *r, const uint8_t task[16]) { + if (!r || !task) return NULL; + for (uint32_t i = 0; i < r->n; i++) + if (memcmp(r->t[i].task, task, 16) == 0) return &r->t[i]; + return NULL; +} + +int fw_req_send(FwRequester *r, const uint8_t task[16], const uint8_t idem[16], + const uint8_t input_digest[32], uint64_t deadline_us) { + if (!r || !task || !idem || !input_digest) return FW_R_BAD_PAYLOAD; + if (fw_req_find(r, task)) return FW_R_STATE; + if (r->n >= FW_MAX_TASKS) return FW_R_FULL; + FwTask *t = &r->t[r->n++]; + memset(t, 0, sizeof *t); + memcpy(t->task, task, 16); + memcpy(t->idem, idem, 16); + memcpy(t->input_digest, input_digest, 32); + t->deadline_us = deadline_us; + t->state = FW_ST_SENT; + return FW_R_OK; +} + +static int req_refuse(FwRequester *r, int why) { + r->refused[why]++; + r->last_refusal = why; + return why; +} + +/* Applies a terminal payload (completed / failed / cancelled bytes) to t. */ +static int apply_terminal(FwTask *t, uint8_t type, const uint8_t *p, size_t len, const AienMachineId *peer) { + if (type == PST_T_COMPLETED) { + if (len < 32) return FW_R_BAD_PAYLOAD; + if (len != 32 && len != 32 + HTR_BYTES) return FW_R_BAD_PAYLOAD; + HtrReceipt rc; + if (len == 32 + HTR_BYTES) { + if (htr_decode(p + 32, HTR_BYTES, &rc) != HTR_OK) return FW_R_BAD_PAYLOAD; + if (!aien_mid_equal(&rc.executor, peer)) return FW_R_RECEIPT_PEER; + if (memcmp(rc.input_digest, t->input_digest, 32) != 0) return FW_R_RECEIPT_INPUT; + if (memcmp(rc.result_digest, p, 32) != 0) return FW_R_RESULT_DIGEST; + t->receipt = rc; + t->has_receipt = 1; + htr_reference(p + 32, HTR_BYTES, t->receipt_reference); + t->has_receipt_reference = 1; + } + memcpy(t->result_digest, p, 32); + t->state = FW_ST_COMPLETED; + return FW_R_OK; + } + if (type == PST_T_FAILED) { + if (len < 2 || len > 2 + FW_REASON_MAX) return FW_R_BAD_PAYLOAD; + t->fail_code = g16(p); + t->state = FW_ST_FAILED; + return FW_R_OK; + } + if (type == PST_T_CANCELLED) { + if (len != 0) return FW_R_BAD_PAYLOAD; + t->state = FW_ST_CANCELLED; + return FW_R_OK; + } + return FW_R_BAD_PAYLOAD; +} + +int fw_req_receive(FwRequester *r, const PstMsg *m, const AienMachineId *peer, uint64_t now_us) { + if (!r || !m || !peer) return FW_R_BAD_PAYLOAD; + if (pst_side_check(m->type, PST_ROLE_EXECUTOR) != PST_OK) return req_refuse(r, FW_R_WRONG_SIDE); + FwTask *t = fw_req_find(r, m->task); + if (!t || memcmp(t->idem, m->idem, 16) != 0) return req_refuse(r, FW_R_UNKNOWN_TASK); + if (m->type == PST_T_TEXT_CHUNK || m->type == PST_T_WAITING_FOR_APPROVAL) return req_refuse(r, FW_R_UNSUPPORTED); + if (m->ordinal != t->next_ordinal) return req_refuse(r, FW_R_ORDINAL); + if (fw_state_terminal(t->state) && m->type != PST_T_RECEIPT_REFERENCE) return req_refuse(r, FW_R_TERMINAL); + + /* A copy: nothing in t changes unless the message is accepted. */ + FwTask c = *t; + int rc = FW_R_OK; + switch (m->type) { + case PST_T_ACCEPTED: + if (c.state != FW_ST_SENT) rc = FW_R_STATE; else c.state = FW_ST_ACCEPTED; + break; + case PST_T_QUEUED: + if (c.state != FW_ST_SENT && c.state != FW_ST_ACCEPTED) rc = FW_R_STATE; else c.state = FW_ST_ACCEPTED; + break; + case PST_T_RUNNING: + if (c.state != FW_ST_SENT && c.state != FW_ST_ACCEPTED) rc = FW_R_STATE; else c.state = FW_ST_RUNNING; + break; + case PST_T_COMPLETED: + case PST_T_FAILED: + case PST_T_CANCELLED: + if (c.state == FW_ST_UNCERTAIN) rc = FW_R_STATE; /* only a resume_snapshot leaves UNCERTAIN */ + else rc = apply_terminal(&c, m->type, m->payload, m->payload_len, peer); + break; + case PST_T_UNCERTAIN_AFTER_DISCONNECT: + if (m->payload_len) rc = FW_R_BAD_PAYLOAD; else c.state = FW_ST_UNCERTAIN; + break; + case PST_T_RESUME_SNAPSHOT: + if (c.state != FW_ST_UNCERTAIN) { rc = FW_R_STATE; break; } + if (m->payload_len < 1) { rc = FW_R_BAD_PAYLOAD; break; } + if (m->payload[0] == 0) { + if (m->payload_len != 1) rc = FW_R_BAD_PAYLOAD; else c.resume_unknown = 1; + } else { + rc = apply_terminal(&c, m->payload[0], m->payload + 1, m->payload_len - 1u, peer); + } + break; + case PST_T_RECEIPT_REFERENCE: + if (m->payload_len != 32) { rc = FW_R_BAD_PAYLOAD; break; } + if (c.has_receipt_reference && memcmp(c.receipt_reference, m->payload, 32) != 0) { rc = FW_R_RESULT_DIGEST; break; } + memcpy(c.receipt_reference, m->payload, 32); + c.has_receipt_reference = 1; + break; + default: + rc = FW_R_UNSUPPORTED; + } + if (rc != FW_R_OK) return req_refuse(r, rc); + c.next_ordinal = m->ordinal + 1u; + *t = c; + r->applied++; + (void)now_us; + return FW_R_OK; +} + +int fw_req_session_lost(FwRequester *r) { + if (!r) return 0; + int n = 0; + for (uint32_t i = 0; i < r->n; i++) + if (!fw_state_terminal(r->t[i].state) && r->t[i].state != FW_ST_UNCERTAIN) { r->t[i].state = FW_ST_UNCERTAIN; n++; } + return n; +} + +int fw_req_tick(FwRequester *r, uint64_t now_us) { + if (!r) return 0; + int n = 0; + for (uint32_t i = 0; i < r->n; i++) { + FwTask *t = &r->t[i]; + if (!fw_state_terminal(t->state) && t->state != FW_ST_UNCERTAIN && now_us > t->deadline_us) { + t->state = FW_ST_UNCERTAIN; + n++; + } + } + return n; +} + +int fw_req_verify_result(const FwTask *t, const uint8_t *result, size_t len) { + if (!t || (!result && len)) return FW_R_BAD_PAYLOAD; + if (t->state != FW_ST_COMPLETED) return FW_R_STATE; + uint8_t d[32]; + sha256_hash(result, len, d); + return memcmp(d, t->result_digest, 32) == 0 ? FW_R_OK : FW_R_RESULT_DIGEST; +} + +/* ================= executor ================= */ +static const char *const exec_names[FW_X_N] = { + "ok", "wrong side", "bad HTK1", "unknown kind", "bounds", "duplicate with different bytes", "journal store" +}; +const char *fw_exec_code_name(int code) { return code >= 0 && code < FW_X_N ? exec_names[code] : "?"; } + +int fw_exec_init(FwExecutor *x, const FwJournalStore *store) { + if (!x || !store || !store->append) return FW_X_STORE; + memset(x, 0, sizeof *x); + x->store = store; + return FW_X_OK; +} + +const FwJournalEntry *fw_exec_lookup(const FwExecutor *x, const uint8_t task[16], const uint8_t idem[16]) { + if (!x || !task || !idem) return NULL; + for (uint32_t i = 0; i < x->count; i++) { + const FwJournalEntry *e = &x->e[(x->head + FW_JOURNAL_ENTRIES - 1u - i) % FW_JOURNAL_ENTRIES]; + if (memcmp(e->task, task, 16) == 0 && memcmp(e->idem, idem, 16) == 0) return e; + } + return NULL; +} + +static void ring_put(FwExecutor *x, const FwJournalEntry *e) { + x->e[x->head] = *e; + x->head = (x->head + 1u) % FW_JOURNAL_ENTRIES; + if (x->count < FW_JOURNAL_ENTRIES) x->count++; +} + +int fw_journal_encode(const FwJournalEntry *e, uint8_t out[FW_JOURNAL_ENTRY_BYTES]) { + if (!e || !out) return -1; + if (e->terminal_type != PST_T_COMPLETED && e->terminal_type != PST_T_FAILED && e->terminal_type != PST_T_CANCELLED) return -1; + memset(out, 0, FW_JOURNAL_ENTRY_BYTES); + memcpy(out, e->task, 16); + memcpy(out + 16, e->idem, 16); + memcpy(out + 32, e->htk_digest, 32); + out[64] = e->terminal_type; + p16(out + 65, e->fail_code); + out[67] = e->has_receipt ? 1 : 0; + memcpy(out + 68, e->result_digest, 32); + if (e->has_receipt && htr_encode(&e->receipt, out + 100) != HTR_OK) return -1; + return 0; +} + +int fw_journal_decode(const uint8_t *b, size_t n, FwJournalEntry *e) { + if (!b || !e || n != FW_JOURNAL_ENTRY_BYTES) return -1; + memset(e, 0, sizeof *e); + memcpy(e->task, b, 16); + memcpy(e->idem, b + 16, 16); + memcpy(e->htk_digest, b + 32, 32); + e->terminal_type = b[64]; + if (e->terminal_type != PST_T_COMPLETED && e->terminal_type != PST_T_FAILED && e->terminal_type != PST_T_CANCELLED) return -1; + e->fail_code = g16(b + 65); + if (b[67] > 1) return -1; + e->has_receipt = b[67]; + memcpy(e->result_digest, b + 68, 32); + if (e->has_receipt && htr_decode(b + 100, HTR_BYTES, &e->receipt) != HTR_OK) return -1; + return 0; +} + +int fw_exec_load(FwExecutor *x, const uint8_t *rec, size_t len) { + if (!x) return FW_X_STORE; + FwJournalEntry e; + if (fw_journal_decode(rec, len, &e) != 0) return FW_X_STORE; + ring_put(x, &e); + return FW_X_OK; +} + +static int exec_refuse(FwExecutor *x, int why, uint16_t code, uint16_t *fail_code) { + x->refused[why]++; + if (fail_code) *fail_code = code; + return FW_ADMIT_REFUSE; +} + +int fw_exec_admit(FwExecutor *x, const PstMsg *m, const FwExecLimits *lim, HtkTask *task, + const FwJournalEntry **replay, uint16_t *fail_code) { + if (!x || !m || !lim || !task || !replay || !fail_code) return FW_ADMIT_REFUSE; + *replay = NULL; + *fail_code = 0; + if (m->type != PST_T_REQUEST) return exec_refuse(x, FW_X_WRONG_SIDE, 0, fail_code); + /* Idempotence first: a repeat is answered from the journal whatever the limits are now. */ + const FwJournalEntry *j = fw_exec_lookup(x, m->task, m->idem); + if (j) { + if (memcmp(j->htk_digest, m->digest, 32) == 0) { *replay = j; x->replayed++; return FW_ADMIT_REPLAY; } + return exec_refuse(x, FW_X_DUPLICATE_DIFFERENT, FW_FAIL_DUPLICATE_DIFFERENT, fail_code); + } + int rc = htk_decode(m->payload, m->payload_len, task); + if (rc == HTK_BAD_KIND) return exec_refuse(x, FW_X_KIND, FW_FAIL_UNKNOWN_KIND, fail_code); + if (rc == HTK_BAD_BOUNDS) return exec_refuse(x, FW_X_BOUNDS, FW_FAIL_BOUNDS, fail_code); + if (rc != HTK_OK) return exec_refuse(x, FW_X_BAD_HTK, FW_FAIL_INTERNAL, fail_code); + if (m->effect != PST_EFFECT_COMPUTE) return exec_refuse(x, FW_X_BAD_HTK, FW_FAIL_INTERNAL, fail_code); + if (!(lim->kinds & (1u << task->kind))) return exec_refuse(x, FW_X_KIND, FW_FAIL_UNKNOWN_KIND, fail_code); + uint32_t out_cap = lim->max_out_bytes_cap ? lim->max_out_bytes_cap : HTK_OUT_BYTES_MAX; + uint32_t dl_cap = lim->deadline_ms_cap ? lim->deadline_ms_cap : HTK_DEADLINE_MS_MAX; + if (task->threads > lim->cpus) return exec_refuse(x, FW_X_BOUNDS, FW_FAIL_BOUNDS, fail_code); + if (lim->mem_bytes && task->max_mem_bytes > lim->mem_bytes) return exec_refuse(x, FW_X_BOUNDS, FW_FAIL_BOUNDS, fail_code); + if (task->max_out_bytes > out_cap || task->deadline_ms > dl_cap) return exec_refuse(x, FW_X_BOUNDS, FW_FAIL_BOUNDS, fail_code); + uint64_t need = htk_result_bytes(task); + if (need && need > task->max_out_bytes) return exec_refuse(x, FW_X_BOUNDS, FW_FAIL_BOUNDS, fail_code); + x->admitted++; + return FW_ADMIT_RUN; +} + +int fw_exec_record(FwExecutor *x, const FwJournalEntry *e) { + if (!x || !e || !x->store) return FW_X_STORE; + uint8_t rec[FW_JOURNAL_ENTRY_BYTES]; + if (fw_journal_encode(e, rec) != 0) return FW_X_STORE; + if (x->store->append(x->store->ctx, rec, sizeof rec) != 0) { x->refused[FW_X_STORE]++; return FW_X_STORE; } + ring_put(x, e); + x->recorded++; + return FW_X_OK; +} + +int fw_terminal_payload(const FwJournalEntry *e, const char *reason, uint8_t *out, size_t cap, size_t *len) { + if (!e || !out || !len) return -1; + switch (e->terminal_type) { + case PST_T_COMPLETED: return fw_completed_payload(e->result_digest, e->has_receipt ? &e->receipt : NULL, out, cap, len); + case PST_T_FAILED: return fw_failed_payload(e->fail_code, reason, out, cap, len); + case PST_T_CANCELLED: *len = 0; return 0; + default: return -1; + } +} + +int fw_resume_snapshot_payload(const FwJournalEntry *e, uint8_t *out, size_t cap, size_t *len) { + if (!out || !len || cap < 1) return -1; + if (!e) { out[0] = 0; *len = 1; return 0; } + out[0] = e->terminal_type; + size_t inner = 0; + if (fw_terminal_payload(e, NULL, out + 1, cap - 1, &inner) != 0) return -1; + *len = 1 + inner; + return 0; +} diff --git a/src/fabric/fab_work.h b/src/fabric/fab_work.h new file mode 100644 index 000000000..57c6b8721 --- /dev/null +++ b/src/fabric/fab_work.h @@ -0,0 +1,338 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * fab_work.h -- the work message of the heterogeneous compute program + * (aienix#6, omega#536): PST1 task envelope, HTK1 task request, HTR1 receipt, + * the requester's task state machine and the executor's idempotence journal. + * Production-buildable. No socket, file, process or clock call lives here: + * the caller moves bytes and supplies time. + * + * Wire stack (contract 2026-10-18 D1): UDP -> AIENOS M6-B (aienos_sec.h) -> + * M6-A frame (aienos_ctl.h, <= 1024 bytes) -> one message told apart by its + * first four bytes: "AFAB" (fabric.h) or "PST1" (this file). The peer and the + * session are facts the transport proved; no field here names them, and a + * receipt whose executor id disagrees with the session peer is refused. + * + * PST1 (aien-protocols specs/pi-spark-task/SPEC.md, wire version 1): the + * 224-byte header and the single-message checks are decoded here in the exact + * order and with the exact codes of the reference checker + * (specs/pi-spark-task/tools/pst_verify.c); tests/fabric/vectors/pst/ holds + * that checker's vectors and this decoder must give the same verdict line for + * every one. Roles: the REQUESTER sends request, cancel_request and + * resume_request (the spec's "Pi side"); the EXECUTOR sends every other type. + * + * HTK1 (request payload, effect_class 1 COMPUTE), big-endian: + * 0 4 "HTK1" 4 1 version 1 5 1 kind 6 2 reserved 0 + * 8 4 deadline_ms (1..600000) 12 4 max_out_bytes (1..64 MiB) + * 16 8 max_mem_bytes 24 4 max_in_bytes 28 4 threads (0 = executor's choice) + * 32 32 input_digest = SHA-256(canonical input bytes of the kind) + * 64 .. kind 1 MATMUL_I64_SEEDED: u32 n (1..512), u64 seed (12 bytes) + * kind 2 TOKENIZE_FIXTURE: 32 fixture_digest, u32 offset, u32 length (<= 1 MiB), + * 32 tokenizer_digest (72 bytes) + * kind 1 input bytes = "HTK1-MATMUL" | u32 n | u64 seed; + * A[i][j] = splitmix(seed, stream 0, i*n+j), B[i][j] = splitmix(seed, stream 1, i*n+j) as i64, + * C = A x B in wrapping i64; result bytes = C row-major, each i64 big-endian. + * kind 2 input bytes = fixture_digest | offset | length | tokenizer_digest; result bytes = token ids u32 BE + * (sovereign-core BitNetTokenizer::encode, bos=false). The tokenizer itself is not in this repo. + * splitmix(seed, stream, index): z = seed + (index+1)*0x9E3779B97F4A7C15 + stream*0xD1B54A32D192ED03; + * z = (z ^ z>>30)*0xBF58476D1CE4E5B9; z = (z ^ z>>27)*0x94D049BB133111EB; return z ^ z>>31 (all wrapping) + * + * HTR1 (receipt; the bytes after the 32-byte result digest in `completed`; + * named by receipt_reference = SHA-256(HTR1 bytes)), big-endian, 228 bytes: + * 0 4 "HTR1" 4 1 version 1 5 1 kind 6 2 flags (bits 0-1 sandbox: 0 none, 1 OS user, 2 namespaces; rest 0) + * 8 16 run_id 24 44 executor machine id record 68 32 implementation_digest + * 100 32 input_digest 132 32 result_digest 164 8 t_accept_us 172 8 t_start_us 180 8 t_end_us + * 188 8 cpu_ns 196 8 peak_rss_bytes 204 2 threads_used 206 2 reserved 0 + * 208 16 os_arch (NUL padded) 224 4 identity_class ("lab" or "own", NUL padded) + * `failed` payload: u16 code | UTF-8 reason (<= 200 bytes). Codes FW_FAIL_*. + * + * Requester state (D6): SENT -> ACCEPTED -> RUNNING -> COMPLETED | FAILED | CANCELLED; + * any non-terminal state + session lost or deadline passed -> UNCERTAIN, which only + * a resume_snapshot can leave. Executor ordinals per task start at 0 and rise by + * one; anything else is refused and counted, state unchanged. A second terminal + * is refused. Executor (D7): key (task_id, idempotence_key); a repeat with the + * same HTK1 bytes replays the journaled terminal, a repeat with other bytes is + * `failed` code 7; the journal store must report durable before a terminal may + * be sent (fw_exec_record fails otherwise and nothing is sent). + */ +#ifndef FAB_WORK_H +#define FAB_WORK_H + +#include "runtime/aien_machine_id.h" + +#include +#include + +/* ---- PST1 ---- */ +#define PST_HDR 224u +#define PST_MAX_PAY 800u +#define PST_MAX_MSG 1024u +#define PST_VERSION 1u +#define PST_MAX_TYPE 14u + +enum { + PST_T_REQUEST = 1, PST_T_ACCEPTED, PST_T_QUEUED, PST_T_RUNNING, PST_T_TEXT_CHUNK, + PST_T_WAITING_FOR_APPROVAL, PST_T_COMPLETED, PST_T_FAILED, PST_T_CANCELLED, + PST_T_UNCERTAIN_AFTER_DISCONNECT, PST_T_RESUME_SNAPSHOT, PST_T_RECEIPT_REFERENCE, + PST_T_CANCEL_REQUEST, PST_T_RESUME_REQUEST +}; +enum { PST_EFFECT_NONE = 0, PST_EFFECT_COMPUTE = 1, PST_EFFECT_EFFECT = 2 }; +enum { + PST_OK = 0, PST_BAD_MAGIC, PST_BAD_VERSION, PST_DOWNGRADE, PST_BAD_TYPE, + PST_WRONG_SIDE, PST_BAD_LENGTH, PST_TRAILING_BYTES, PST_RESERVED_NONZERO, + PST_NO_CAUSE, PST_NO_TASK, PST_NO_OBJECTIVE, PST_DIGEST_MISMATCH, + PST_BAD_EFFECT_CLASS, PST_SUBJECT_MISMATCH, PST_CODE_COUNT +}; +enum { PST_ROLE_REQUESTER = 1, PST_ROLE_EXECUTOR = 2 }; + +typedef struct { + uint8_t version, type; + uint8_t cause[32], origin[32], objective[32], subject[32]; + uint8_t parent[16], task[16], idem[16]; + uint32_t ordinal; + uint8_t effect; + uint16_t payload_len; + uint8_t digest[32]; + const uint8_t *payload; /* decode: points into the caller's buffer */ +} PstMsg; + +/* Decodes and checks one message in the spec's order. PST_OK or the first refusal. */ +int pst_decode(const uint8_t *b, size_t n, PstMsg *m); +/* Writes header + payload (digest computed here). The header fields are + * checked with the decode rules first. cap >= PST_HDR + payload_len. */ +int pst_encode(const PstMsg *m, const uint8_t *payload, size_t payload_len, + uint8_t *out, size_t cap, size_t *out_len); +/* The role that sends this type (PST_ROLE_*), 0 for an unknown type. */ +int pst_side_of(uint8_t type); +/* PST_OK when `sender_role` may send `type`, else PST_WRONG_SIDE. */ +int pst_side_check(uint8_t type, int sender_role); +const char *pst_code_name(int code); +/* The reference checker's verdict line ("ok type=... digest=..." or "refuse PST_..."). */ +void pst_verdict_line(int code, const PstMsg *m, char *buf, size_t cap); + +/* ---- HTK1 ---- */ +#define HTK_HDR 64u +#define HTK_MAX_BYTES (HTK_HDR + 72u) +#define HTK_INPUT_MAX 72u +#define HTK_KIND_MATMUL_I64_SEEDED 1u +#define HTK_KIND_TOKENIZE_FIXTURE 2u +#define HTK_MATMUL_N_MAX 512u +#define HTK_TOKENIZE_LEN_MAX (1u << 20) +#define HTK_DEADLINE_MS_MAX 600000u +#define HTK_OUT_BYTES_MAX (64u << 20) +enum { + HTK_OK = 0, HTK_BAD_MAGIC, HTK_BAD_VERSION, HTK_BAD_KIND, HTK_RESERVED_NONZERO, + HTK_BAD_LENGTH, HTK_BAD_BOUNDS, HTK_INPUT_DIGEST_MISMATCH, HTK_CODE_COUNT +}; + +typedef struct { + uint8_t kind; + uint32_t deadline_ms, max_out_bytes; + uint64_t max_mem_bytes; + uint32_t max_in_bytes, threads; + uint8_t input_digest[32]; + struct { uint32_t n; uint64_t seed; } matmul; /* kind 1 */ + struct { uint8_t fixture_digest[32]; uint32_t offset, length; uint8_t tokenizer_digest[32]; } tok; /* kind 2 */ +} HtkTask; + +/* Canonical input bytes of the task (digest preimage). Returns their length, 0 for an unknown kind. */ +size_t htk_input_bytes(const HtkTask *t, uint8_t out[HTK_INPUT_MAX]); +void htk_input_digest(const HtkTask *t, uint8_t out[32]); +/* Checks bounds, writes the bytes; input_digest is computed here (t->input_digest is ignored). */ +int htk_encode(const HtkTask *t, uint8_t *out, size_t cap, size_t *len); +/* Checks magic, version, kind, reserved, length, bounds, then input_digest. */ +int htk_decode(const uint8_t *b, size_t n, HtkTask *t); +/* Result size the kind defines (kind 1: n*n*8), 0 when the kind leaves it to the executor. */ +uint64_t htk_result_bytes(const HtkTask *t); +const char *htk_code_name(int code); + +/* The matmul definition (normative in C; the worker and the benchmarks oracle + * re-implement it independently and must reproduce the digests). */ +uint64_t htk_splitmix(uint64_t seed, uint64_t stream, uint64_t index); +/* c holds n*n values on return. HTK_BAD_BOUNDS for n outside 1..512. */ +int htk_matmul_i64(uint32_t n, uint64_t seed, int64_t *c); +/* Result bytes: row-major, each i64 big-endian; out holds n*n*8 bytes. */ +void htk_matmul_result_bytes(uint32_t n, const int64_t *c, uint8_t *out); +/* Runs the matmul into scratch (n*n i64) and digests its result bytes in place. */ +int htk_matmul_result_digest(uint32_t n, uint64_t seed, int64_t *scratch, uint8_t out[32]); + +/* ---- HTR1 ---- */ +#define HTR_BYTES 228u +#define HTR_MAX_BYTES 768u +#define HTR_OS_ARCH 16u +enum { HTR_OK = 0, HTR_BAD_MAGIC, HTR_BAD_VERSION, HTR_BAD_LENGTH, HTR_RESERVED_NONZERO, + HTR_BAD_FLAGS, HTR_BAD_MACHINE, HTR_BAD_KIND, HTR_CODE_COUNT }; +enum { HTR_SANDBOX_NONE = 0, HTR_SANDBOX_OS_USER = 1, HTR_SANDBOX_NAMESPACES = 2 }; + +typedef struct { + uint8_t version, kind; + uint16_t flags; + uint8_t run_id[16]; + AienMachineId executor; + uint8_t implementation_digest[32], input_digest[32], result_digest[32]; + uint64_t t_accept_us, t_start_us, t_end_us, cpu_ns, peak_rss_bytes; + uint16_t threads_used; + char os_arch[HTR_OS_ARCH]; /* NUL padded, not necessarily terminated */ + char identity_class[4]; /* "lab" or "own", NUL padded */ +} HtrReceipt; + +int htr_encode(const HtrReceipt *r, uint8_t out[HTR_BYTES]); +int htr_decode(const uint8_t *b, size_t n, HtrReceipt *r); +/* receipt_reference = SHA-256 of the receipt bytes. */ +void htr_reference(const uint8_t *bytes, size_t len, uint8_t out[32]); +const char *htr_code_name(int code); + +/* ---- failure codes (failed payload: u16 code | reason) ---- */ +enum { + FW_FAIL_BOUNDS = 1, FW_FAIL_UNKNOWN_KIND = 2, FW_FAIL_DEADLINE = 3, FW_FAIL_MEMORY = 4, + FW_FAIL_CANCELLED_BEFORE_START = 5, FW_FAIL_INTERNAL = 6, FW_FAIL_DUPLICATE_DIFFERENT = 7 +}; +#define FW_REASON_MAX 200u +/* completed payload: result_digest | HTR1 (r may be NULL: digest only). */ +int fw_completed_payload(const uint8_t result_digest[32], const HtrReceipt *r, + uint8_t *out, size_t cap, size_t *len); +int fw_failed_payload(uint16_t code, const char *reason, uint8_t *out, size_t cap, size_t *len); + +/* ---- requester ---- */ +#define FW_MAX_TASKS 32u +enum { FW_ST_SENT = 1, FW_ST_ACCEPTED, FW_ST_RUNNING, FW_ST_COMPLETED, FW_ST_FAILED, + FW_ST_CANCELLED, FW_ST_UNCERTAIN }; +enum { + FW_R_OK = 0, + FW_R_WRONG_SIDE, /* a requester-side type arrived */ + FW_R_UNKNOWN_TASK, /* task id / idempotence key not ours */ + FW_R_ORDINAL, /* not the next ordinal for this task */ + FW_R_TERMINAL, /* task already terminal (duplicate terminal, late message) */ + FW_R_STATE, /* type not allowed in the task's current state */ + FW_R_BAD_PAYLOAD, /* payload too short or undecodable for the type */ + FW_R_RECEIPT_PEER, /* receipt executor id is not the session peer */ + FW_R_RECEIPT_INPUT, /* receipt input_digest is not the task's */ + FW_R_RESULT_DIGEST, /* receipt result_digest differs from the completed digest, or result bytes differ */ + FW_R_UNSUPPORTED, /* a type HTK work does not use (text_chunk, waiting_for_approval) */ + FW_R_FULL, + FW_R_N +}; + +typedef struct { + uint8_t task[16], idem[16]; + uint32_t state; + uint32_t next_ordinal; /* executor ordinal expected next */ + uint64_t deadline_us; /* absolute; passing it non-terminal -> UNCERTAIN */ + uint8_t input_digest[32]; + uint8_t result_digest[32]; + uint8_t has_receipt; + HtrReceipt receipt; + uint8_t has_receipt_reference; + uint8_t receipt_reference[32]; + uint16_t fail_code; + uint8_t resume_unknown; /* resume_snapshot said the executor never journaled it */ +} FwTask; + +typedef struct { + uint32_t n; + FwTask t[FW_MAX_TASKS]; + uint64_t applied; + uint64_t refused[FW_R_N]; + int last_refusal; +} FwRequester; + +void fw_req_init(FwRequester *r); +/* Registers a sent request. FW_R_FULL, or FW_R_STATE for a task id already held. */ +int fw_req_send(FwRequester *r, const uint8_t task[16], const uint8_t idem[16], + const uint8_t input_digest[32], uint64_t deadline_us); +FwTask *fw_req_find(FwRequester *r, const uint8_t task[16]); +/* Applies one decoded executor message received on the session with `peer`. FW_R_*. */ +int fw_req_receive(FwRequester *r, const PstMsg *m, const AienMachineId *peer, uint64_t now_us); +/* Every non-terminal task -> UNCERTAIN. Returns how many changed. */ +int fw_req_session_lost(FwRequester *r); +/* Non-terminal tasks past their deadline -> UNCERTAIN. Returns how many changed. */ +int fw_req_tick(FwRequester *r, uint64_t now_us); +/* FW_R_OK when the task is COMPLETED and SHA-256(result) is its result digest. */ +int fw_req_verify_result(const FwTask *t, const uint8_t *result, size_t len); +int fw_state_terminal(uint32_t state); +const char *fw_state_name(uint32_t state); +const char *fw_req_code_name(int code); + +/* ---- executor ---- */ +#define FW_JOURNAL_ENTRIES 256u +#define FW_JOURNAL_ENTRY_BYTES (16u + 16u + 32u + 1u + 2u + 1u + 32u + HTR_BYTES) /* 328 */ +enum { FW_ADMIT_RUN = 0, FW_ADMIT_REPLAY = 1, FW_ADMIT_REFUSE = 2 }; +enum { + FW_X_OK = 0, + FW_X_WRONG_SIDE, /* not a request: refuse without reply (session rule) */ + FW_X_BAD_HTK, /* HTK1 undecodable -> failed code 6 */ + FW_X_KIND, /* unknown kind -> failed code 2 */ + FW_X_BOUNDS, /* outside this executor's limits -> failed code 1 */ + FW_X_DUPLICATE_DIFFERENT,/* same key, other bytes -> failed code 7 */ + FW_X_STORE, /* journal store refused: nothing may be sent */ + FW_X_N +}; + +typedef struct { + uint8_t task[16], idem[16]; + uint8_t htk_digest[32]; /* SHA-256 of the request payload (HTK1 bytes) */ + uint8_t terminal_type; /* PST_T_COMPLETED, PST_T_FAILED or PST_T_CANCELLED */ + uint16_t fail_code; + uint8_t has_receipt; + uint8_t result_digest[32]; + HtrReceipt receipt; +} FwJournalEntry; + +/* Durable append. 0 only once the record is on stable storage. */ +typedef struct { + void *ctx; + int (*append)(void *ctx, const uint8_t *rec, size_t len); +} FwJournalStore; + +typedef struct { + uint32_t cpus; /* threads > cpus is refused */ + uint64_t mem_bytes; /* max_mem_bytes above this is refused */ + uint32_t max_out_bytes_cap; /* 0 = HTK_OUT_BYTES_MAX */ + uint32_t deadline_ms_cap; /* 0 = HTK_DEADLINE_MS_MAX */ + uint32_t kinds; /* bit (1<append must return 0, then it is held. + * FW_X_STORE (nonzero) means nothing was journaled and nothing may be sent. */ +int fw_exec_record(FwExecutor *x, const FwJournalEntry *e); +int fw_journal_encode(const FwJournalEntry *e, uint8_t out[FW_JOURNAL_ENTRY_BYTES]); +int fw_journal_decode(const uint8_t *b, size_t n, FwJournalEntry *e); +/* The payload of the terminal message for a journal entry (completed: digest|HTR1; + * failed: code|reason; cancelled: empty). */ +int fw_terminal_payload(const FwJournalEntry *e, const char *reason, uint8_t *out, size_t cap, size_t *len); +/* resume_snapshot payload: u8 terminal_type (0 = unknown task) | terminal payload. */ +int fw_resume_snapshot_payload(const FwJournalEntry *e, uint8_t *out, size_t cap, size_t *len); +const char *fw_exec_code_name(int code); + +#endif /* FAB_WORK_H */ diff --git a/tests/.crumb b/tests/.crumb index 0707a364b..94a1aaa23 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -175,8 +175,8 @@ "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { - "digest": "b7b6391caf653c76707a21f9969126abcbc730d1243259219225fdb9bc0084e0", - "last_commit": "d863780eb", + "digest": "7130ff53dcb54efdd226a720ea284f542dc06febfb65bab589402ce220107efa", + "last_commit": "bfab38ea9", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, @@ -313,15 +313,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:e86ac2a28d07eba800824efc763d45fc8bb3e3961be3a64206fb5022e3585f24", - "compiled_at": "2026-11-06T13:59:13Z", + "children_root": "sha256:b8b195cb25f483ebfc3be90df5f45d9e6f28cf8dc1b7161284154829101abbb2", + "compiled_at": "2026-11-06T23:35:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:30ff9017b760b1486add5c105f2a3b2bd3c4bf9b7e2cb656d666d87a6c6289eb", + "digest": "sha256:6478a0c54253a884a9d36d21211fef92a36dc02e016f079ed662ffffd5d594e8", "evidence_files": 74, "evidence_root": "sha256:9aa38bf7dcfa5eb3499356a0a2a1dd7704fedfae2d16822a7c9e1400f3a72723", - "files": 2082, - "generated_at_commit": "e45dbca3518b081dd94b18039a79e30f36dfd42d", - "source_tree": "sha256:23f10c08d370ac44d88f41d0d5a1a49f851a36549b9346217092da949324ab38" + "files": 2111, + "generated_at_commit": "f601985a8ca75b9e072d3d41474240b1a74dda23", + "source_tree": "sha256:29749339eda7b75ded72d43cd8465cf4b0ef96248fcebdfd0e8687e6a83ee8d1" }, "owner_lane": "", "provenance": { diff --git a/tests/fabric/.crumb b/tests/fabric/.crumb index bc4e02733..b787fbbdb 100644 --- a/tests/fabric/.crumb +++ b/tests/fabric/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-30T16:33:20Z", "compiler": "crumb-compile/1", - "digest": "sha256:b7b6391caf653c76707a21f9969126abcbc730d1243259219225fdb9bc0084e0", + "digest": "sha256:7130ff53dcb54efdd226a720ea284f542dc06febfb65bab589402ce220107efa", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 4, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:64102a7324a7923de31f5b51ccfb711581ec9f7a1fb2831cb74944d38f701551" + "files": 33, + "generated_at_commit": "f06b20cd770bf8febd030d29941de43ff0c26376", + "source_tree": "sha256:52e999c5a63068b3429c72ba7b53edf906913a95690c8e2bf9a5a0c25ec44b2e" }, "owner_lane": "", "provenance": { diff --git a/tests/fabric/fab_work_procs.sh b/tests/fabric/fab_work_procs.sh new file mode 100755 index 000000000..f205f7bc6 --- /dev/null +++ b/tests/fabric/fab_work_procs.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2026 Drake Stapleton (AIEN / aien-dev) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# fab_work_procs.sh -- requester and executor as two separate OS processes +# joined by two pipes (aienix#6 phase 2, step "separate process"). Not a +# network, not M6-A/M6-B: it proves the message codec, the state machines and +# the executor's durable journal work across a process boundary, nothing more. +# +# fab_work_procs.sh +# Prints FAB_WORK_PROCS_PASS on success. +set -eu +BIN=$1 +DIR=$2 +mkdir -p "$DIR/procs" +J="$DIR/procs/executor.journal" +rm -f "$J" "$DIR/procs/req2exec" "$DIR/procs/exec2req" +mkfifo "$DIR/procs/req2exec" "$DIR/procs/exec2req" +PEER=$("$BIN" --peer-id) + +# Open order matters with named pipes: the executor opens req2exec for reading +# first, so the requester must open req2exec for writing before it opens +# exec2req for reading, or both sides block in open() forever. +"$BIN" --executor "$J" < "$DIR/procs/req2exec" > "$DIR/procs/exec2req" 2> "$DIR/procs/executor.err" & +EXEC_PID=$! +set +e +"$BIN" --requester "$PEER" > "$DIR/procs/req2exec" < "$DIR/procs/exec2req" 2> "$DIR/procs/requester.err" +REQ_RC=$? +wait "$EXEC_PID" +EXEC_RC=$? +set -e +cat "$DIR/procs/executor.err" "$DIR/procs/requester.err" +if [ "$REQ_RC" -ne 0 ] || [ "$EXEC_RC" -ne 0 ]; then + echo "fab_work_procs: requester rc=$REQ_RC executor rc=$EXEC_RC" + exit 1 +fi +# The journal holds exactly the entries the executor journaled: one completed +# (328 bytes); the replay and the code-7 refusal added none. +SIZE=$(wc -c < "$J") +if [ "$SIZE" -ne 328 ]; then + echo "fab_work_procs: journal is $SIZE bytes, expected 328 (one entry)" + exit 1 +fi +# The two processes were distinct: the executor's pid is not ours. +if [ "$EXEC_PID" -eq "$$" ]; then echo "fab_work_procs: executor ran in this process"; exit 1; fi +echo "fab_work_procs: requester pid $$ (shell), executor pid $EXEC_PID, journal $SIZE bytes" +echo "FAB_WORK_PROCS_PASS" diff --git a/tests/fabric/fab_work_test.c b/tests/fabric/fab_work_test.c new file mode 100644 index 000000000..182d29950 --- /dev/null +++ b/tests/fabric/fab_work_test.c @@ -0,0 +1,886 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * fab_work_test.c -- conformance and negative tests for fab_work.h, + * fab_auth_ed25519.h and fab_route.h (aienix#6, omega#536). + * + * fab_work_test [VECTOR_DIR] in-process suite; prints FAB_WORK_PASS + * fab_work_test --emit DIR writes the HTK1/HTR1 golden vectors + * fab_work_test --oracle prints the matmul oracle digests + * fab_work_test --executor JOURNAL one executor process: PST1 frames on stdin/stdout + * fab_work_test --requester PEERHEX one requester process: drives an executor over stdin/stdout + * + * Frame on a pipe: u16 big-endian length, then that many bytes (one PST1 + * message). Two OS processes over pipes prove "separate process"; they are + * not a network, not M6-A/M6-B, and qualify nothing physical. + */ +#include "fabric.h" +#include "fab_auth_ed25519.h" +#include "fab_loopback.h" +#include "fab_route.h" +#include "fab_work.h" +#include "searchtrace/sig/aienos_sig.h" +#include "sha256.h" + +#include +#include +#include +#include +#include +#include + +static int checks, failures; +#define CHECK(c, ...) do { checks++; if (!(c)) { failures++; \ + fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) + +static void hex(const uint8_t *b, size_t n, char *out) { + static const char hx[] = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { out[2 * i] = hx[b[i] >> 4]; out[2 * i + 1] = hx[b[i] & 15]; } + out[2 * n] = 0; +} +static int unhex(const char *s, uint8_t *out, size_t n) { + if (strlen(s) != 2 * n) return -1; + for (size_t i = 0; i < n; i++) { + unsigned v; + if (sscanf(s + 2 * i, "%2x", &v) != 1) return -1; + out[i] = (uint8_t)v; + } + return 0; +} +static void fill(uint8_t *b, size_t n, const char *label) { + uint8_t d[32]; + sha256_hash((const uint8_t *)label, strlen(label), d); + for (size_t i = 0; i < n; i++) b[i] = d[i % 32]; +} +static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } +static uint16_t get16(const uint8_t *p) { return (uint16_t)((p[0] << 8) | p[1]); } + +/* ---- 1. PST1 vectors: same verdict line as protocols tools/pst_verify.c ---- */ +/* The vector set is fixed here: expected.txt supplies only verdict lines, and a + * file name read from it never becomes a path (an unknown or repeated name fails). */ +static const char *const PST_VECTORS[] = { + "ok-request.pst", "ok-accepted-empty.pst", "ok-text-chunk-1024.pst", + "ok-resume-request-max-ordinal.pst", "bad-length-short-header.pst", + "bad-length-empty-file.pst", "bad-length-over-1024.pst", "bad-magic.pst", + "bad-version-unknown.pst", "bad-version-downgrade.pst", "bad-type-zero.pst", + "bad-type-unknown.pst", "bad-reserved0.pst", "bad-reserved1.pst", + "bad-length-mismatch.pst", "bad-payload-len-over-800.pst", "bad-trailing-byte.pst", + "bad-effect-class.pst", "bad-no-cause.pst", "bad-no-task-id.pst", + "bad-no-idempotence-key.pst", "bad-request-no-objective.pst", "bad-digest.pst", +}; +#define N_PST_VECTORS (sizeof PST_VECTORS / sizeof PST_VECTORS[0]) +static void test_pst_vectors(const char *dir) { + char path[2048]; + snprintf(path, sizeof path, "%s/expected.txt", dir); + FILE *f = fopen(path, "r"); + CHECK(f != NULL, "open %s", path); + if (!f) return; + char line[1024]; + int n_vec = 0; + int seen[N_PST_VECTORS] = {0}; + while (fgets(line, sizeof line, f)) { + if (line[0] == '#' || line[0] == '\n') continue; + char *sp = strchr(line, ' '); + if (!sp) continue; + *sp = 0; + char *want = sp + 1; + want[strcspn(want, "\n")] = 0; + size_t vi = 0; + while (vi < N_PST_VECTORS && strcmp(line, PST_VECTORS[vi]) != 0) vi++; + CHECK(vi < N_PST_VECTORS, "expected.txt names unknown vector %s", line); + if (vi == N_PST_VECTORS) continue; + CHECK(seen[vi] == 0, "expected.txt names %s twice", PST_VECTORS[vi]); + seen[vi]++; + snprintf(path, sizeof path, "%s/%s", dir, PST_VECTORS[vi]); + FILE *v = fopen(path, "rb"); + CHECK(v != NULL, "open vector %s", path); + if (!v) continue; + uint8_t buf[PST_MAX_MSG + 64]; + size_t n = fread(buf, 1, sizeof buf, v); + fclose(v); + PstMsg m; + int code = pst_decode(buf, n, &m); + char got[1024]; + pst_verdict_line(code, &m, got, sizeof got); + CHECK(strcmp(got, want) == 0, "%s:\n want %s\n got %s", line, want, got); + n_vec++; + } + fclose(f); + CHECK(n_vec == (int)N_PST_VECTORS, "%d vectors, saw %d", (int)N_PST_VECTORS, n_vec); + printf("pst1 vectors: %d checked against expected.txt\n", n_vec); +} + +/* ---- 2. HTK1 / HTR1 goldens ---- */ +static void sample_matmul(HtkTask *t) { + memset(t, 0, sizeof *t); + t->kind = HTK_KIND_MATMUL_I64_SEEDED; + t->deadline_ms = 30000; + t->max_out_bytes = 128; + t->max_mem_bytes = 64u << 20; + t->max_in_bytes = 0; + t->threads = 0; + t->matmul.n = 4; + t->matmul.seed = 1; +} +static void sample_tok(HtkTask *t) { + memset(t, 0, sizeof *t); + t->kind = HTK_KIND_TOKENIZE_FIXTURE; + t->deadline_ms = 60000; + t->max_out_bytes = 1u << 20; + t->max_mem_bytes = 512u << 20; + t->max_in_bytes = 4096; + t->threads = 1; + fill(t->tok.fixture_digest, 32, "fixture"); + t->tok.offset = 0; + t->tok.length = 4096; + fill(t->tok.tokenizer_digest, 32, "tokenizer"); +} +static AienMachineId test_machine(const char *label) { + uint8_t sk[32], pk[32]; + AienMachineId id; + fill(sk, 32, label); + aienos_ed25519_public_key(pk, sk); + fab_ed_machine_id(pk, &id); + return id; +} +static void sample_receipt(HtrReceipt *r) { + memset(r, 0, sizeof *r); + r->version = 1; + r->kind = HTK_KIND_MATMUL_I64_SEEDED; + r->flags = HTR_SANDBOX_OS_USER; + fill(r->run_id, 16, "run"); + r->executor = test_machine("het-test-executor"); + fill(r->implementation_digest, 32, "impl"); + HtkTask t; + sample_matmul(&t); + htk_input_digest(&t, r->input_digest); + fill(r->result_digest, 32, "result"); + r->t_accept_us = 1000; r->t_start_us = 1500; r->t_end_us = 9000; + r->cpu_ns = 7000000; r->peak_rss_bytes = 2u << 20; r->threads_used = 1; + memcpy(r->os_arch, "linux-aarch64", 13); + memcpy(r->identity_class, "lab", 3); +} +static size_t read_file(const char *path, uint8_t *buf, size_t cap) { + FILE *f = fopen(path, "rb"); + if (!f) return 0; + size_t n = fread(buf, 1, cap, f); + fclose(f); + return n; +} +/* Files this test creates are owner-only (0600), never world-writable. */ +static FILE *fopen_private(const char *path, int flags, const char *mode) { + int fd = open(path, flags | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR); + if (fd < 0) return NULL; + FILE *f = fdopen(fd, mode); + if (!f) close(fd); + return f; +} +static int write_file(const char *path, const uint8_t *b, size_t n) { + FILE *f = fopen_private(path, O_WRONLY | O_TRUNC, "wb"); + if (!f) return -1; + int ok = fwrite(b, 1, n, f) == n; + return fclose(f) == 0 && ok ? 0 : -1; +} +typedef struct { const char *name; uint8_t b[PST_MAX_PAY]; size_t n; } Golden; +static int goldens(Golden g[3]) { + HtkTask t; + sample_matmul(&t); + g[0].name = "htk1-matmul-n4-seed1.bin"; + if (htk_encode(&t, g[0].b, sizeof g[0].b, &g[0].n) != HTK_OK) return -1; + sample_tok(&t); + g[1].name = "htk1-tokenize-fixture.bin"; + if (htk_encode(&t, g[1].b, sizeof g[1].b, &g[1].n) != HTK_OK) return -1; + HtrReceipt r; + sample_receipt(&r); + g[2].name = "htr1-sample.bin"; + if (htr_encode(&r, g[2].b) != HTR_OK) return -1; + g[2].n = HTR_BYTES; + return 0; +} +static int emit(const char *dir) { + Golden g[3]; + if (goldens(g) != 0) return 1; + for (int i = 0; i < 3; i++) { + char path[1024]; + snprintf(path, sizeof path, "%s/%s", dir, g[i].name); + if (write_file(path, g[i].b, g[i].n) != 0) { perror(path); return 1; } + printf("wrote %s (%zu bytes)\n", path, g[i].n); + } + return 0; +} +static void test_goldens(const char *dir) { + Golden g[3]; + CHECK(goldens(g) == 0, "goldens encode"); + for (int i = 0; i < 3; i++) { + char path[1024]; + uint8_t buf[PST_MAX_PAY]; + snprintf(path, sizeof path, "%s/../%s", dir, g[i].name); + size_t n = read_file(path, buf, sizeof buf); + CHECK(n == g[i].n && memcmp(buf, g[i].b, n) == 0, "golden %s differs from encode (%zu vs %zu bytes)", g[i].name, n, g[i].n); + } + HtkTask t, u; + sample_matmul(&t); + CHECK(htk_decode(g[0].b, g[0].n, &u) == HTK_OK && u.kind == 1 && u.matmul.n == 4 && u.matmul.seed == 1 && + u.deadline_ms == 30000 && u.max_out_bytes == 128, "htk1 matmul roundtrip"); + sample_tok(&t); + CHECK(htk_decode(g[1].b, g[1].n, &u) == HTK_OK && u.kind == 2 && u.tok.length == 4096 && + memcmp(u.tok.tokenizer_digest, t.tok.tokenizer_digest, 32) == 0, "htk1 tokenize roundtrip"); + HtrReceipt r, s; + sample_receipt(&r); + CHECK(htr_decode(g[2].b, HTR_BYTES, &s) == HTR_OK && aien_mid_equal(&s.executor, &r.executor) && + s.t_end_us == 9000 && s.threads_used == 1 && memcmp(s.os_arch, "linux-aarch64", 13) == 0 && + memcmp(s.identity_class, "lab", 3) == 0 && s.flags == HTR_SANDBOX_OS_USER, "htr1 roundtrip"); + /* negatives on the codecs */ + uint8_t b[PST_MAX_PAY]; + memcpy(b, g[0].b, g[0].n); + b[5] = 9; CHECK(htk_decode(b, g[0].n, &u) == HTK_BAD_KIND, "htk unknown kind"); + memcpy(b, g[0].b, g[0].n); b[6] = 1; CHECK(htk_decode(b, g[0].n, &u) == HTK_RESERVED_NONZERO, "htk reserved"); + memcpy(b, g[0].b, g[0].n); b[4] = 2; CHECK(htk_decode(b, g[0].n, &u) == HTK_BAD_VERSION, "htk version"); + memcpy(b, g[0].b, g[0].n); CHECK(htk_decode(b, g[0].n - 1, &u) == HTK_BAD_LENGTH, "htk short"); + memcpy(b, g[0].b, g[0].n); b[66] = 3; CHECK(htk_decode(b, g[0].n, &u) == HTK_BAD_BOUNDS, "htk n=0x0304 > 512 is bounds"); + memcpy(b, g[0].b, g[0].n); b[75] = 2; CHECK(htk_decode(b, g[0].n, &u) == HTK_INPUT_DIGEST_MISMATCH, "htk seed changed, digest stale"); + memcpy(b, g[0].b, g[0].n); b[11] = 0; b[10] = 0; b[9] = 0; b[8] = 0; CHECK(htk_decode(b, g[0].n, &u) == HTK_BAD_BOUNDS, "htk deadline 0"); + memcpy(b, g[2].b, HTR_BYTES); b[7] = 4; CHECK(htr_decode(b, HTR_BYTES, &s) == HTR_BAD_FLAGS, "htr flags"); + memcpy(b, g[2].b, HTR_BYTES); b[207] = 1; CHECK(htr_decode(b, HTR_BYTES, &s) == HTR_RESERVED_NONZERO, "htr reserved"); + memcpy(b, g[2].b, HTR_BYTES); b[24] ^= 0xff; CHECK(htr_decode(b, HTR_BYTES, &s) == HTR_BAD_MACHINE, "htr machine record"); + CHECK(htr_decode(g[2].b, HTR_BYTES - 1, &s) == HTR_BAD_LENGTH, "htr short"); + printf("htk1/htr1 goldens: 3 files match, roundtrips and 11 negatives\n"); +} + +/* ---- 3. matmul oracle ---- */ +typedef struct { uint32_t n; uint64_t seed; const char *digest; } Oracle; +/* Computed by this implementation on 2026-10-18 (first run); the benchmarks lane's + * independent oracle and the Rust worker must reproduce them, or the definition is unclear. */ +static const Oracle oracle[3] = { + { 4, 1, "705d18096163be46e7cc44b757fe26787ed059045954ff99a2a94c6e575607cb" }, + { 64, 7, "f0d209befe915f5f624d48b784c3acd10f51ef2df8f41e0f92580e3770ae07d0" }, + { 256, 42, "74f59d70c133f9573943edf21b781e4e19c57e9ab6c822036542e8209c9211cc" }, +}; +static int64_t scratch[HTK_MATMUL_N_MAX * HTK_MATMUL_N_MAX]; +static void print_oracle(void) { + for (int i = 0; i < 3; i++) { + uint8_t d[32]; + char h[65]; + htk_matmul_result_digest(oracle[i].n, oracle[i].seed, scratch, d); + hex(d, 32, h); + printf("matmul n=%u seed=%llu result_digest=%s\n", oracle[i].n, (unsigned long long)oracle[i].seed, h); + } +} +static void test_oracle(void) { + /* The definition is pinned by hand values first: n=1 is a single product. */ + int64_t one; + CHECK(htk_matmul_i64(1, 5, &one) == HTK_OK, "n=1"); + uint64_t a = htk_splitmix(5, 0, 0), b = htk_splitmix(5, 1, 0); + CHECK((uint64_t)one == a * b, "n=1 is a*b wrapping"); + CHECK(htk_splitmix(0, 0, 0) != htk_splitmix(0, 1, 0), "streams differ"); + CHECK(htk_matmul_i64(0, 1, scratch) == HTK_BAD_BOUNDS && htk_matmul_i64(513, 1, scratch) == HTK_BAD_BOUNDS, "n bounds"); + for (int i = 0; i < 3; i++) { + uint8_t d[32]; + char h[65]; + CHECK(htk_matmul_result_digest(oracle[i].n, oracle[i].seed, scratch, d) == HTK_OK, "digest n=%u", oracle[i].n); + hex(d, 32, h); + CHECK(strcmp(h, oracle[i].digest) == 0, "oracle n=%u seed=%llu: want %s got %s", oracle[i].n, + (unsigned long long)oracle[i].seed, oracle[i].digest, h); + } + printf("matmul oracle: 3 digests reproduced\n"); +} + +/* ---- in-memory executor used by the unit tests and the --executor process ---- */ +typedef struct { FILE *f; int fail_next; uint64_t appended; } MemStore; +static int store_append(void *ctx, const uint8_t *rec, size_t len) { + MemStore *s = ctx; + if (s->fail_next) { s->fail_next = 0; return -1; } + if (s->f) { + if (fwrite(rec, 1, len, s->f) != len || fflush(s->f) != 0 || fsync(fileno(s->f)) != 0) return -1; + } + s->appended++; + return 0; +} + +typedef struct { + FwExecutor x; + FwExecLimits lim; + MemStore store_ctx; + FwJournalStore store; + AienMachineId self; + uint8_t impl_digest[32]; + uint32_t ordinal[FW_MAX_TASKS]; /* per-task send ordinal, keyed by task slot below */ + uint8_t task_ids[FW_MAX_TASKS][16]; + uint32_t n_tasks; + uint64_t now_us; +} Exec; + +static uint32_t exec_slot(Exec *e, const uint8_t task[16]) { + for (uint32_t i = 0; i < e->n_tasks; i++) if (memcmp(e->task_ids[i], task, 16) == 0) return i; + uint32_t i = e->n_tasks < FW_MAX_TASKS ? e->n_tasks++ : FW_MAX_TASKS - 1; + memcpy(e->task_ids[i], task, 16); + e->ordinal[i] = 0; + return i; +} +static void exec_init(Exec *e, FILE *journal) { + memset(e, 0, sizeof *e); + e->store_ctx.f = journal; + e->store.ctx = &e->store_ctx; + e->store.append = store_append; + fw_exec_init(&e->x, &e->store); + e->lim.cpus = 4; + e->lim.mem_bytes = 1ull << 30; + e->lim.kinds = 1u << HTK_KIND_MATMUL_I64_SEEDED; /* the C test executor serves matmul only */ + e->self = test_machine("het-test-executor"); + fill(e->impl_digest, 32, "fab_work_test executor"); + e->now_us = 1000000; +} +/* Builds one executor->requester message for task m with the given type/payload. */ +static size_t exec_msg(Exec *e, const PstMsg *req, uint8_t type, const uint8_t *pl, size_t pn, uint8_t *out, size_t cap) { + PstMsg m = *req; + m.type = type; + m.ordinal = e->ordinal[exec_slot(e, req->task)]++; + size_t n = 0; + if (pst_encode(&m, pl, pn, out, cap, &n) != PST_OK) return 0; + return n; +} +/* Handles one decoded request; writes 0..3 reply messages into out (each length-prefixed u16). */ +static size_t exec_handle(Exec *e, const PstMsg *req, uint8_t *out, size_t cap) { + size_t used = 0; + uint8_t pl[PST_MAX_PAY]; + size_t pn = 0; +#define EMIT(type, p, n) do { \ + size_t _m = exec_msg(e, req, (type), (p), (n), out + used + 2, cap - used - 2); \ + if (!_m) return used; \ + put16(out + used, (uint16_t)_m); \ + used += 2 + _m; \ + } while (0) + if (req->type == PST_T_RESUME_REQUEST) { + const FwJournalEntry *j = fw_exec_lookup(&e->x, req->task, req->idem); + fw_resume_snapshot_payload(j, pl, sizeof pl, &pn); + EMIT(PST_T_RESUME_SNAPSHOT, pl, pn); + return used; + } + HtkTask t; + const FwJournalEntry *replay = NULL; + uint16_t code = 0; + int a = fw_exec_admit(&e->x, req, &e->lim, &t, &replay, &code); + if (a == FW_ADMIT_REPLAY) { + fw_terminal_payload(replay, "replayed", pl, sizeof pl, &pn); + EMIT(replay->terminal_type, pl, pn); + return used; + } + if (a == FW_ADMIT_REFUSE) { + if (!code) return 0; /* wrong side: nothing is sent */ + /* A code-7 answer is about the key's existing entry, not a new terminal + * for the key: it is never journaled (the original bytes still replay). */ + if (code != FW_FAIL_DUPLICATE_DIFFERENT) { + FwJournalEntry j = {0}; + memcpy(j.task, req->task, 16); memcpy(j.idem, req->idem, 16); memcpy(j.htk_digest, req->digest, 32); + j.terminal_type = PST_T_FAILED; j.fail_code = code; + if (fw_exec_record(&e->x, &j) != FW_X_OK) return 0; /* not journaled: not sent */ + } + fw_failed_payload(code, fw_exec_code_name(code == FW_FAIL_BOUNDS ? FW_X_BOUNDS : code == FW_FAIL_UNKNOWN_KIND ? FW_X_KIND : code == FW_FAIL_DUPLICATE_DIFFERENT ? FW_X_DUPLICATE_DIFFERENT : FW_X_BAD_HTK), pl, sizeof pl, &pn); + EMIT(PST_T_FAILED, pl, pn); + return used; + } + EMIT(PST_T_ACCEPTED, NULL, 0); + EMIT(PST_T_RUNNING, NULL, 0); + uint64_t t_accept = e->now_us, t_start = e->now_us + 10; + uint8_t rd[32]; + htk_matmul_result_digest(t.matmul.n, t.matmul.seed, scratch, rd); + e->now_us += 1000; + HtrReceipt r = {0}; + r.version = 1; r.kind = t.kind; r.flags = HTR_SANDBOX_NONE; + fill(r.run_id, 16, "run-id"); r.run_id[15] = (uint8_t)e->x.recorded; + r.executor = e->self; + memcpy(r.implementation_digest, e->impl_digest, 32); + memcpy(r.input_digest, t.input_digest, 32); + memcpy(r.result_digest, rd, 32); + r.t_accept_us = t_accept; r.t_start_us = t_start; r.t_end_us = e->now_us; + r.cpu_ns = 1000 * 1000; r.peak_rss_bytes = sizeof scratch; r.threads_used = 1; + memcpy(r.os_arch, "linux-test", 10); + memcpy(r.identity_class, "lab", 3); + FwJournalEntry j = {0}; + memcpy(j.task, req->task, 16); memcpy(j.idem, req->idem, 16); memcpy(j.htk_digest, req->digest, 32); + j.terminal_type = PST_T_COMPLETED; j.has_receipt = 1; j.receipt = r; memcpy(j.result_digest, rd, 32); + if (fw_exec_record(&e->x, &j) != FW_X_OK) return used; /* journal refused: no terminal is sent */ + fw_terminal_payload(&j, NULL, pl, sizeof pl, &pn); + EMIT(PST_T_COMPLETED, pl, pn); +#undef EMIT + return used; +} + +/* ---- requester side helpers ---- */ +static PstMsg make_request(const char *task_label, const char *idem_label, const HtkTask *t, uint8_t *pl, size_t *pn) { + PstMsg m; + memset(&m, 0, sizeof m); + m.version = 1; m.type = PST_T_REQUEST; m.effect = PST_EFFECT_COMPUTE; + fill(m.cause, 32, "cause"); fill(m.origin, 32, "origin"); fill(m.objective, 32, "objective"); fill(m.subject, 32, "subject"); + fill(m.task, 16, task_label); fill(m.idem, 16, idem_label); + CHECK(htk_encode(t, pl, PST_MAX_PAY, pn) == HTK_OK, "encode htk"); + return m; +} +/* Feeds every length-prefixed message in buf to the requester; returns the last code. */ +static int feed(FwRequester *r, const uint8_t *buf, size_t n, const AienMachineId *peer, int *count) { + int last = FW_R_OK; + size_t off = 0; + while (off + 2 <= n) { + uint16_t len = get16(buf + off); + PstMsg m; + CHECK(pst_decode(buf + off + 2, len, &m) == PST_OK, "reply decodes"); + last = fw_req_receive(r, &m, peer, 0); + if (count) (*count)++; + off += 2 + len; + } + return last; +} + +/* ---- 4. requester <-> executor in one process, with the named negatives ---- */ +static void test_state_machines(void) { + Exec e; + exec_init(&e, NULL); + FwRequester r; + fw_req_init(&r); + AienMachineId peer = e.self, stranger = test_machine("stranger"); + uint8_t pl[PST_MAX_PAY], msg[PST_MAX_MSG], replies[8 * PST_MAX_MSG]; + size_t pn, mn, rn; + HtkTask t; + sample_matmul(&t); + + /* happy path: request -> accepted, running, completed(HTR1); result verifies */ + PstMsg q = make_request("task-1", "idem-1", &t, pl, &pn); + uint8_t in_digest[32]; + htk_input_digest(&t, in_digest); + CHECK(fw_req_send(&r, q.task, q.idem, in_digest, 5000000) == FW_R_OK, "send"); + CHECK(pst_encode(&q, pl, pn, msg, sizeof msg, &mn) == PST_OK, "encode request"); + PstMsg qd; + CHECK(pst_decode(msg, mn, &qd) == PST_OK, "request decodes"); + CHECK(pst_side_check(qd.type, PST_ROLE_REQUESTER) == PST_OK && pst_side_check(qd.type, PST_ROLE_EXECUTOR) == PST_WRONG_SIDE, "side rule"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + int cnt = 0; + CHECK(feed(&r, replies, rn, &peer, &cnt) == FW_R_OK && cnt == 3, "3 replies applied (%d)", cnt); + FwTask *ft = fw_req_find(&r, q.task); + CHECK(ft && ft->state == FW_ST_COMPLETED && ft->has_receipt && ft->next_ordinal == 3, "COMPLETED with receipt"); + uint8_t want[32]; + htk_matmul_result_digest(4, 1, scratch, want); + CHECK(memcmp(ft->result_digest, want, 32) == 0, "result digest is the oracle's"); + uint8_t result[128]; + htk_matmul_i64(4, 1, scratch); + htk_matmul_result_bytes(4, scratch, result); + CHECK(fw_req_verify_result(ft, result, 128) == FW_R_OK, "result bytes verify"); + result[3] ^= 1; + CHECK(fw_req_verify_result(ft, result, 128) == FW_R_RESULT_DIGEST, "NEG wrong result bytes refused"); + + /* NEG duplicate terminal */ + size_t last_off = 0, off = 0; + while (off + 2 <= rn) { last_off = off; off += 2u + get16(replies + off); } + PstMsg dup; + CHECK(pst_decode(replies + last_off + 2, get16(replies + last_off), &dup) == PST_OK, "dup decodes"); + CHECK(fw_req_receive(&r, &dup, &peer, 0) == FW_R_ORDINAL, "NEG replayed completed refused (ordinal)"); + dup.ordinal = 3; + CHECK(fw_req_receive(&r, &dup, &peer, 0) == FW_R_TERMINAL, "NEG second terminal refused"); + CHECK(ft->state == FW_ST_COMPLETED && ft->next_ordinal == 3, "state unchanged"); + + /* NEG reordered ordinal + receipt from the wrong peer, on a second task */ + PstMsg q2 = make_request("task-2", "idem-2", &t, pl, &pn); + CHECK(fw_req_send(&r, q2.task, q2.idem, in_digest, 5000000) == FW_R_OK, "send 2"); + CHECK(pst_encode(&q2, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 2"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + uint16_t l0 = get16(replies); + PstMsg m1, m2; + CHECK(pst_decode(replies + 2 + l0 + 2, get16(replies + 2 + l0), &m1) == PST_OK, "running decodes"); + CHECK(fw_req_receive(&r, &m1, &peer, 0) == FW_R_ORDINAL, "NEG ordinal 1 before 0 refused"); + off = 2u + l0 + 2u + get16(replies + 2 + l0); + CHECK(pst_decode(replies + off + 2, get16(replies + off), &m2) == PST_OK, "completed decodes"); + PstMsg m0; + CHECK(pst_decode(replies + 2, l0, &m0) == PST_OK && fw_req_receive(&r, &m0, &peer, 0) == FW_R_OK, "accepted ok"); + CHECK(fw_req_receive(&r, &m1, &peer, 0) == FW_R_OK, "running ok in order"); + CHECK(fw_req_receive(&r, &m2, &stranger, 0) == FW_R_RECEIPT_PEER, "NEG receipt executor != session peer"); + CHECK(fw_req_find(&r, q2.task)->state == FW_ST_RUNNING, "still RUNNING after refusal"); + /* NEG receipt result digest differs from the completed digest */ + uint8_t tampered[PST_MAX_MSG]; + memcpy(tampered, replies + off + 2, get16(replies + off)); + tampered[PST_HDR + 32 + 132] ^= 1; /* HTR1.result_digest inside the payload */ + sha256_hash(tampered + PST_HDR, m2.payload_len, tampered + 192); /* keep PST1 digest valid */ + PstMsg m2t; + CHECK(pst_decode(tampered, get16(replies + off), &m2t) == PST_OK, "tampered decodes as PST1"); + CHECK(fw_req_receive(&r, &m2t, &peer, 0) == FW_R_RESULT_DIGEST, "NEG receipt digest != completed digest"); + CHECK(fw_req_receive(&r, &m2, &peer, 0) == FW_R_OK && fw_req_find(&r, q2.task)->state == FW_ST_COMPLETED, "genuine completed ok"); + + /* NEG unknown task, wrong side at the requester */ + PstMsg stray = m0; + fill(stray.task, 16, "nobody"); + CHECK(fw_req_receive(&r, &stray, &peer, 0) == FW_R_UNKNOWN_TASK, "NEG unknown task"); + PstMsg ws = m0; ws.type = PST_T_CANCEL_REQUEST; + CHECK(fw_req_receive(&r, &ws, &peer, 0) == FW_R_WRONG_SIDE, "NEG requester-side type refused"); + + /* NEG oversized frame */ + uint8_t big[PST_MAX_MSG + 1]; + memset(big, 0, sizeof big); + PstMsg bm; + CHECK(pst_decode(big, sizeof big, &bm) == PST_BAD_LENGTH, "NEG 1025-byte frame refused"); + CHECK(pst_encode(&q, pl, PST_MAX_PAY + 1, big, sizeof big, &mn) == PST_BAD_LENGTH, "NEG 801-byte payload refused at encode"); + + /* NEG unknown kind at the executor -> failed code 2 */ + HtkTask bad = t; + uint8_t rawpl[PST_MAX_PAY]; + size_t rawn; + CHECK(htk_encode(&bad, rawpl, sizeof rawpl, &rawn) == HTK_OK, "raw"); + rawpl[5] = 7; + PstMsg q3 = make_request("task-3", "idem-3", &t, pl, &pn); + CHECK(pst_encode(&q3, rawpl, rawn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 3"); + CHECK(fw_req_send(&r, q3.task, q3.idem, in_digest, 5000000) == FW_R_OK, "send 3"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(feed(&r, replies, rn, &peer, NULL) == FW_R_OK && fw_req_find(&r, q3.task)->state == FW_ST_FAILED && + fw_req_find(&r, q3.task)->fail_code == FW_FAIL_UNKNOWN_KIND, "NEG unknown kind -> failed 2"); + /* kind 2 is well-formed but this executor does not serve it -> also code 2 */ + HtkTask tk; sample_tok(&tk); + PstMsg q3b = make_request("task-3b", "idem-3b", &tk, pl, &pn); + CHECK(pst_encode(&q3b, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 3b"); + CHECK(fw_req_send(&r, q3b.task, q3b.idem, in_digest, 5000000) == FW_R_OK, "send 3b"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(feed(&r, replies, rn, &peer, NULL) == FW_R_OK && fw_req_find(&r, q3b.task)->fail_code == FW_FAIL_UNKNOWN_KIND, "NEG unserved kind -> failed 2"); + + /* NEG bounds exceeded -> failed code 1 (threads above the executor's cpus; result larger than max_out) */ + HtkTask tb = t; tb.threads = 64; + PstMsg q4 = make_request("task-4", "idem-4", &tb, pl, &pn); + CHECK(pst_encode(&q4, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 4"); + CHECK(fw_req_send(&r, q4.task, q4.idem, in_digest, 5000000) == FW_R_OK, "send 4"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(feed(&r, replies, rn, &peer, NULL) == FW_R_OK && fw_req_find(&r, q4.task)->fail_code == FW_FAIL_BOUNDS, "NEG threads > cpus -> failed 1"); + HtkTask tc = t; tc.max_out_bytes = 64; /* n=4 needs 128 */ + PstMsg q4b = make_request("task-4b", "idem-4b", &tc, pl, &pn); + CHECK(pst_encode(&q4b, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 4b"); + CHECK(fw_req_send(&r, q4b.task, q4b.idem, in_digest, 5000000) == FW_R_OK, "send 4b"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(feed(&r, replies, rn, &peer, NULL) == FW_R_OK && fw_req_find(&r, q4b.task)->fail_code == FW_FAIL_BOUNDS, "NEG max_out too small -> failed 1"); + + /* idempotence: same bytes replayed from the journal (no re-run), different bytes -> code 7 */ + uint64_t admitted = e.x.admitted; + CHECK(pst_encode(&q, pl, pn, msg, sizeof msg, &mn) == PST_OK, "re-encode task-1"); + sample_matmul(&t); + CHECK(htk_encode(&t, pl, PST_MAX_PAY, &pn) == HTK_OK && pst_encode(&q, pl, pn, msg, sizeof msg, &mn) == PST_OK && + pst_decode(msg, mn, &qd) == PST_OK, "task-1 again"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + PstMsg rp; + CHECK(rn > 2 && pst_decode(replies + 2, get16(replies), &rp) == PST_OK && rp.type == PST_T_COMPLETED && + rp.payload_len == 32 + HTR_BYTES && memcmp(rp.payload, want, 32) == 0, "replay returns the journaled completed"); + CHECK(e.x.admitted == admitted && e.x.replayed == 1, "no re-run on replay"); + HtkTask td = t; td.matmul.seed = 2; + CHECK(htk_encode(&td, pl, PST_MAX_PAY, &pn) == HTK_OK && pst_encode(&q, pl, pn, msg, sizeof msg, &mn) == PST_OK && + pst_decode(msg, mn, &qd) == PST_OK, "task-1 other bytes"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(rn > 2 && pst_decode(replies + 2, get16(replies), &rp) == PST_OK && rp.type == PST_T_FAILED && + get16(rp.payload) == FW_FAIL_DUPLICATE_DIFFERENT, "NEG repeat with different bytes -> failed 7"); + + /* NEG journal store refuses: nothing is sent */ + HtkTask te = t; te.matmul.seed = 99; + PstMsg q5 = make_request("task-5", "idem-5", &te, pl, &pn); + CHECK(pst_encode(&q5, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 5"); + e.store_ctx.fail_next = 1; + rn = exec_handle(&e, &qd, replies, sizeof replies); + cnt = 0; off = 0; + while (off + 2 <= rn) { cnt++; off += 2u + get16(replies + off); } + CHECK(cnt == 2 && e.x.refused[FW_X_STORE] == 1 && fw_exec_lookup(&e.x, q5.task, q5.idem) == NULL, + "NEG store failure: accepted+running sent, no terminal, nothing journaled (%d msgs)", cnt); + + /* NEG session drop before terminal -> UNCERTAIN; resume_snapshot resolves it */ + HtkTask tf = t; tf.matmul.seed = 3; + uint8_t in6[32]; + htk_input_digest(&tf, in6); + PstMsg q6 = make_request("task-6", "idem-6", &tf, pl, &pn); + CHECK(fw_req_send(&r, q6.task, q6.idem, in6, 5000000) == FW_R_OK, "send 6"); + CHECK(pst_encode(&q6, pl, pn, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "req 6"); + rn = exec_handle(&e, &qd, replies, sizeof replies); /* executor finished, replies "lost" */ + CHECK(fw_req_session_lost(&r) == 1 && fw_req_find(&r, q6.task)->state == FW_ST_UNCERTAIN, "NEG session lost -> UNCERTAIN"); + PstMsg lost; + CHECK(pst_decode(replies + 2, get16(replies), &lost) == PST_OK && fw_req_receive(&r, &lost, &peer, 0) == FW_R_STATE, + "late accepted refused in UNCERTAIN"); + PstMsg rr = q6; rr.type = PST_T_RESUME_REQUEST; rr.ordinal = 1; + CHECK(pst_encode(&rr, NULL, 0, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "resume_request"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + PstMsg snap; + CHECK(pst_decode(replies + 2, get16(replies), &snap) == PST_OK && snap.type == PST_T_RESUME_SNAPSHOT, "snapshot"); + /* the executor's ordinals for task-6 continued at 3 (accepted, running, completed were sent); fast-forward the requester */ + fw_req_find(&r, q6.task)->next_ordinal = snap.ordinal; + CHECK(fw_req_receive(&r, &snap, &peer, 0) == FW_R_OK && fw_req_find(&r, q6.task)->state == FW_ST_COMPLETED && + fw_req_find(&r, q6.task)->has_receipt, "resume_snapshot -> COMPLETED with receipt"); + /* unknown task in a snapshot stays UNCERTAIN with the flag */ + PstMsg q7 = make_request("task-7", "idem-7", &tf, pl, &pn); + CHECK(fw_req_send(&r, q7.task, q7.idem, in6, 5000000) == FW_R_OK && fw_req_session_lost(&r) == 1, "task-7 uncertain"); + PstMsg rr7 = q7; rr7.type = PST_T_RESUME_REQUEST; + CHECK(pst_encode(&rr7, NULL, 0, msg, sizeof msg, &mn) == PST_OK && pst_decode(msg, mn, &qd) == PST_OK, "resume 7"); + rn = exec_handle(&e, &qd, replies, sizeof replies); + CHECK(pst_decode(replies + 2, get16(replies), &snap) == PST_OK && snap.payload_len == 1 && snap.payload[0] == 0, "unknown snapshot"); + CHECK(fw_req_receive(&r, &snap, &peer, 0) == FW_R_OK && fw_req_find(&r, q7.task)->state == FW_ST_UNCERTAIN && + fw_req_find(&r, q7.task)->resume_unknown == 1, "unknown task stays UNCERTAIN, flagged"); + + /* NEG deadline passed -> UNCERTAIN */ + PstMsg q8 = make_request("task-8", "idem-8", &t, pl, &pn); + CHECK(fw_req_send(&r, q8.task, q8.idem, in_digest, 2000) == FW_R_OK, "send 8"); + CHECK(fw_req_tick(&r, 1999) == 0 && fw_req_tick(&r, 2001) == 1 && fw_req_find(&r, q8.task)->state == FW_ST_UNCERTAIN, + "NEG deadline passed -> UNCERTAIN"); + + /* journal entries survive a reload (encode/decode) */ + uint8_t rec[FW_JOURNAL_ENTRY_BYTES]; + const FwJournalEntry *je = fw_exec_lookup(&e.x, q.task, q.idem); + FwJournalEntry back; + CHECK(je && fw_journal_encode(je, rec) == 0 && fw_journal_decode(rec, sizeof rec, &back) == 0 && + memcmp(&back.result_digest, je->result_digest, 32) == 0 && back.has_receipt && + aien_mid_equal(&back.receipt.executor, &e.self), "journal entry roundtrip"); + FwExecutor x2; + CHECK(fw_exec_init(&x2, &e.store) == FW_X_OK && fw_exec_load(&x2, rec, sizeof rec) == FW_X_OK && + fw_exec_lookup(&x2, q.task, q.idem) != NULL, "journal reload"); + rec[64] = 5; + CHECK(fw_exec_load(&x2, rec, sizeof rec) == FW_X_STORE, "NEG corrupt journal entry refused"); + printf("state machines: happy path, idempotence, UNCERTAIN/resume and %d-check negative set\n", checks); +} + +/* ---- 5. Ed25519 Fabric membership over the loopback ---- */ +#define LEASE 1000000ull +typedef struct { + uint8_t sk[4][32]; + FabEdAuth auth[4]; + AienMachineId roster_ids[3]; + FabRoster roster; + FabLoop loop; + AienMachineId slots[4][8]; + AienMachineIndex index[4]; + CqCatalog cat[4]; + FabNode node[4]; +} EdHouse; + +static void ed_house_init(EdHouse *h) { + memset(h, 0, sizeof *h); + const char *labels[4] = { "het-seed-A", "het-seed-B", "het-seed-C", "het-seed-D" }; + for (int i = 0; i < 4; i++) { + fill(h->sk[i], 32, labels[i]); + CHECK(fab_ed_auth_init(&h->auth[i], h->sk[i]) == FAB_OK, "ed init"); + } + /* A, B, C enrol each other; D is nobody. */ + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if (i != j) CHECK(fab_ed_auth_add_peer(&h->auth[i], h->auth[j].pk) == FAB_OK, "add peer"); + CHECK(fab_ed_auth_add_peer(&h->auth[0], h->auth[1].pk) == FAB_E_ARG, "duplicate peer refused"); + CHECK(fab_ed_auth_add_peer(&h->auth[0], h->auth[0].pk) == FAB_E_ARG, "own key as peer refused"); + uint32_t n = 0; + CHECK(fab_ed_auth_roster(&h->auth[0], h->roster_ids, 3, &n) == FAB_OK && n == 3, "roster"); + h->roster.n = 3; + h->roster.ids = h->roster_ids; + fab_loop_init(&h->loop); + for (int i = 0; i < 4; i++) CHECK(fab_loop_add(&h->loop, &h->auth[i].self) == FAB_OK, "loop add"); + for (int i = 0; i < 4; i++) { + aien_mid_index_init(&h->index[i], h->slots[i], 8); + CqCatalog *c = &h->cat[i]; + CHECK(cq_catalog_init_canonical(c, &h->index[i], &h->auth[i].self, 8, 16) == CQ_OK, "catalog"); + uint32_t dom = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_FABRIC); + CHECK(cq_op_define(c, 1, 0, dom) == CQ_OK, "op"); + FabConfig cfg = { h->auth[i].self, &h->roster, c, &h->loop.transport, &h->auth[i].auth, LEASE, 1, NULL, 0 }; + CHECK(fab_node_init(&h->node[i], &cfg) == FAB_OK, "node init %d", i); + } +} +static void drain(FabNode *n, uint64_t now) { FabVerdict v; while (fab_poll(n, now, &v) > 0) {} } +/* The newest message waiting in `to`'s loopback box (broadcast seals one copy per + * destination, so loop.last is the copy to the last roster peer, not to A). */ +static size_t last_to(const FabLoop *l, const AienMachineId *to, uint8_t *out) { + for (uint32_t i = 0; i < l->n; i++) { + const FabLoopBox *b = &l->box[i]; + if (!aien_mid_equal(&b->id, to) || b->count == 0) continue; + const FabLoopMsg *m = &b->q[(b->head + b->count - 1u) % FAB_LOOP_DEPTH]; + memcpy(out, m->b, m->len); + return m->len; + } + return 0; +} + +static void test_ed25519_fabric(void) { + static EdHouse h; + ed_house_init(&h); + uint64_t now = 10; + FabVerdict v; + uint8_t join1[FAB_MSG_MAX], join2[FAB_MSG_MAX]; + size_t n1, n2; + + const AienMachineId *A = &h.auth[0].self; + /* B joins at generation 1: keep A's copy of the bytes before it is applied */ + CHECK(fab_join(&h.node[1], LEASE, now) == FAB_OK, "B join gen 1"); + n1 = last_to(&h.loop, A, join1); + CHECK(n1 == FAB_HDR_BYTES + FAB_JOIN_BODY + FAB_SIG_BYTES, "JOIN to A is %zu bytes", n1); + drain(&h.node[0], now); drain(&h.node[2], now); /* A, C see the gen-1 JOIN now */ + CHECK(fab_member_live(&h.node[0], &h.auth[1].self, now), "A holds B live after signed JOIN"); + /* B restarts at generation 2 */ + CHECK(fab_node_set_generation(&h.node[1], 2) == FAB_OK && fab_join(&h.node[1], LEASE, now) == FAB_OK, "B join gen 2"); + n2 = last_to(&h.loop, A, join2); + drain(&h.node[0], now); drain(&h.node[2], now); + CHECK(fab_receive(&h.node[0], join2, n2, now, &v) < 0 && v.code == FAB_E_REPLAY, + "NEG replay of the gen-2 JOIN refused: %s", fab_strerror(v.code)); + CHECK(fab_receive(&h.node[0], join1, n1, now, &v) < 0 && v.code == FAB_E_STALE_GEN, + "NEG stale generation refused: %s", fab_strerror(v.code)); + + /* NEG forged: A's copy of a genuine B message with a fresh timestamp, signed by D's key */ + uint8_t forged[FAB_MSG_MAX]; + size_t nf = n2; + memcpy(forged, join2, nf); + forged[FAB_HDR_BYTES - 8] ^= 1; /* sent_us differs: not a byte replay */ + aienos_ed25519_sign(forged + nf - 64, forged, nf - 64, h.sk[3]); + int rc = fab_receive(&h.node[0], forged, nf, now, &v); + CHECK(rc < 0 && v.code == FAB_E_AUTH, "NEG wrong key refused: %s", fab_strerror(v.code)); + /* altered bytes under the genuine signature are refused the same way */ + memcpy(forged, join2, nf); + forged[FAB_HDR_BYTES + 3] ^= 1; + rc = fab_receive(&h.node[0], forged, nf, now, &v); + CHECK(rc < 0 && v.code == FAB_E_AUTH, "NEG altered body refused: %s", fab_strerror(v.code)); + + /* NEG unknown machine: D (not on A's roster) joins */ + CHECK(fab_join(&h.node[3], LEASE, now) == FAB_OK, "D join sends"); + uint8_t joind[FAB_MSG_MAX]; + size_t nd = last_to(&h.loop, A, joind); + rc = fab_receive(&h.node[0], joind, nd, now, &v); + CHECK(rc < 0 && v.code == FAB_E_NOT_ENROLLED, "NEG unknown machine refused: %s", fab_strerror(v.code)); + drain(&h.node[0], now); drain(&h.node[1], now); drain(&h.node[2], now); + CHECK(!fab_member_live(&h.node[0], &h.auth[3].self, now), "D never live at A"); + + /* positive: C joins and advertises, A sees it live; lease ends -> not live */ + CHECK(fab_join(&h.node[2], LEASE, now) == FAB_OK, "C join"); + drain(&h.node[0], now); + CHECK(fab_member_live(&h.node[0], &h.auth[2].self, now) && fab_member_live(&h.node[0], &h.auth[1].self, now), "B and C live at A"); + (void)fab_tick(&h.node[0], now + LEASE + 1); + CHECK(!fab_member_live(&h.node[0], &h.auth[2].self, now + LEASE + 1), "lease ended -> not live"); + /* a route check cannot name a non-member: fab_route_check with a null router is FAB_RT_ARG */ + AienMachineId tgt; + CHECK(fab_route_check(&h.node[0], NULL, NULL, now, &tgt, NULL, NULL) == FAB_RT_ARG, "route check arg"); + CHECK(fab_route_pump(&h.node[0], now, NULL, NULL) == 0, "pump empty"); + for (int i = 0; i < 4; i++) fab_ed_auth_wipe(&h.auth[i]); + uint8_t z[sizeof h.auth[0].sk] = {0}; + CHECK(memcmp(h.auth[0].sk, z, 32) == 0, "seed wiped"); + printf("ed25519 fabric: signed JOIN accepted; forged key, unknown machine, stale generation, replay refused\n"); +} + +/* ---- two-process mode over pipes ---- */ +static int read_frame(FILE *in, uint8_t *buf, size_t cap, size_t *len) { + uint8_t l[2]; + if (fread(l, 1, 2, in) != 2) return 0; + size_t n = get16(l); + if (n > cap) return -1; + if (fread(buf, 1, n, in) != n) return -1; + *len = n; + return 1; +} +static int write_frames(FILE *out, const uint8_t *buf, size_t n) { + if (fwrite(buf, 1, n, out) != n) return -1; + return fflush(out); +} +static int run_executor(const char *journal_path) { + FILE *j = fopen_private(journal_path, O_WRONLY | O_APPEND, "ab"); + if (!j) { perror(journal_path); return 2; } + Exec e; + exec_init(&e, j); + uint8_t in[PST_MAX_MSG], replies[8 * PST_MAX_MSG]; + size_t n; + int rc; + uint64_t frames = 0, refused = 0; + while ((rc = read_frame(stdin, in, sizeof in, &n)) == 1) { + frames++; + PstMsg m; + int c = pst_decode(in, n, &m); + if (c != PST_OK) { refused++; fprintf(stderr, "executor: refuse %s\n", pst_code_name(c)); continue; } + size_t rn = exec_handle(&e, &m, replies, sizeof replies); + if (write_frames(stdout, replies, rn) != 0) return 3; + } + fclose(j); + fprintf(stderr, "executor: frames=%llu refused=%llu admitted=%llu replayed=%llu journaled=%llu\n", + (unsigned long long)frames, (unsigned long long)refused, (unsigned long long)e.x.admitted, + (unsigned long long)e.x.replayed, (unsigned long long)e.x.recorded); + return rc < 0 ? 4 : 0; +} +static int run_requester(const char *peer_hex) { + uint8_t rec[AIEN_MID_RECORD_BYTES]; + AienMachineId peer; + if (unhex(peer_hex, rec, sizeof rec) != 0 || aien_mid_decode(rec, sizeof rec, &peer) != AIEN_MID_OK) { + fprintf(stderr, "requester: bad peer id\n"); + return 2; + } + FwRequester r; + fw_req_init(&r); + HtkTask t; + sample_matmul(&t); + t.matmul.n = 64; t.matmul.seed = 7; t.max_out_bytes = 64 * 64 * 8; + uint8_t pl[PST_MAX_PAY], msg[PST_MAX_MSG], frame[2 + PST_MAX_MSG], in[PST_MAX_MSG], want[32], in_digest[32]; + size_t pn, mn, n; + PstMsg q = make_request("proc-task-1", "proc-idem-1", &t, pl, &pn); + htk_input_digest(&t, in_digest); + htk_matmul_result_digest(64, 7, scratch, want); + if (fw_req_send(&r, q.task, q.idem, in_digest, 60000000) != FW_R_OK) return 3; + if (pst_encode(&q, pl, pn, msg, sizeof msg, &mn) != PST_OK) return 3; + put16(frame, (uint16_t)mn); memcpy(frame + 2, msg, mn); + if (write_frames(stdout, frame, 2 + mn) != 0) return 3; + int got = 0; + FwTask *ft = fw_req_find(&r, q.task); + while (!fw_state_terminal(ft->state) && read_frame(stdin, in, sizeof in, &n) == 1) { + PstMsg m; + if (pst_decode(in, n, &m) != PST_OK) { fprintf(stderr, "requester: undecodable reply\n"); return 4; } + int c = fw_req_receive(&r, &m, &peer, 0); + if (c != FW_R_OK) { fprintf(stderr, "requester: refused %s\n", fw_req_code_name(c)); return 4; } + got++; + } + if (ft->state != FW_ST_COMPLETED || !ft->has_receipt) { fprintf(stderr, "requester: not completed (%s)\n", fw_state_name(ft->state)); return 5; } + if (memcmp(ft->result_digest, want, 32) != 0) { fprintf(stderr, "requester: result digest is not the oracle's\n"); return 6; } + /* replay the same request: the executor must answer from its journal with the same terminal */ + if (write_frames(stdout, frame, 2 + mn) != 0) return 3; + if (read_frame(stdin, in, sizeof in, &n) != 1) return 7; + PstMsg rp; + if (pst_decode(in, n, &rp) != PST_OK || rp.type != PST_T_COMPLETED || memcmp(rp.payload, want, 32) != 0) { + fprintf(stderr, "requester: replay did not return the journaled completed\n"); return 7; + } + /* same key, other bytes: failed 7 */ + t.matmul.seed = 8; + if (htk_encode(&t, pl, sizeof pl, &pn) != HTK_OK || pst_encode(&q, pl, pn, msg, sizeof msg, &mn) != PST_OK) return 3; + put16(frame, (uint16_t)mn); memcpy(frame + 2, msg, mn); + if (write_frames(stdout, frame, 2 + mn) != 0) return 3; + if (read_frame(stdin, in, sizeof in, &n) != 1) return 8; + if (pst_decode(in, n, &rp) != PST_OK || rp.type != PST_T_FAILED || get16(rp.payload) != FW_FAIL_DUPLICATE_DIFFERENT) { + fprintf(stderr, "requester: different bytes under the same key were not refused with code 7\n"); return 8; + } + char h[65]; + hex(ft->receipt_reference, 32, h); + fprintf(stderr, "requester: COMPLETED after %d messages; receipt_reference=%s os_arch=%.16s identity_class=%.4s\n", + got, h, ft->receipt.os_arch, ft->receipt.identity_class); + fprintf(stderr, "FAB_WORK_PROCS_REQUESTER_PASS\n"); + return 0; +} + +int main(int argc, char **argv) { + if (argc >= 3 && strcmp(argv[1], "--emit") == 0) return emit(argv[2]); + if (argc >= 2 && strcmp(argv[1], "--oracle") == 0) { print_oracle(); return 0; } + if (argc >= 3 && strcmp(argv[1], "--executor") == 0) return run_executor(argv[2]); + if (argc >= 3 && strcmp(argv[1], "--requester") == 0) return run_requester(argv[2]); + if (argc >= 2 && strcmp(argv[1], "--peer-id") == 0) { + AienMachineId id = test_machine("het-test-executor"); + uint8_t rec[AIEN_MID_RECORD_BYTES]; + char h[2 * AIEN_MID_RECORD_BYTES + 1]; + aien_mid_encode(&id, rec); + hex(rec, sizeof rec, h); + printf("%s\n", h); + return 0; + } + const char *dir = argc >= 2 ? argv[1] : "tests/fabric/vectors/pst"; + test_pst_vectors(dir); + test_goldens(dir); + test_oracle(); + test_state_machines(); + test_ed25519_fabric(); + printf("checks=%d failures=%d\n", checks, failures); + if (failures) return 1; + printf("FAB_WORK_PASS\n"); + return 0; +} diff --git a/tests/fabric/vectors/htk1-matmul-n4-seed1.bin b/tests/fabric/vectors/htk1-matmul-n4-seed1.bin new file mode 100644 index 000000000..17a6118d6 Binary files /dev/null and b/tests/fabric/vectors/htk1-matmul-n4-seed1.bin differ diff --git a/tests/fabric/vectors/htk1-tokenize-fixture.bin b/tests/fabric/vectors/htk1-tokenize-fixture.bin new file mode 100644 index 000000000..7fc36e3f9 Binary files /dev/null and b/tests/fabric/vectors/htk1-tokenize-fixture.bin differ diff --git a/tests/fabric/vectors/htr1-sample.bin b/tests/fabric/vectors/htr1-sample.bin new file mode 100644 index 000000000..66f7a29dc Binary files /dev/null and b/tests/fabric/vectors/htr1-sample.bin differ diff --git a/tests/fabric/vectors/pst/bad-digest.pst b/tests/fabric/vectors/pst/bad-digest.pst new file mode 100644 index 000000000..8737c844d Binary files /dev/null and b/tests/fabric/vectors/pst/bad-digest.pst differ diff --git a/tests/fabric/vectors/pst/bad-effect-class.pst b/tests/fabric/vectors/pst/bad-effect-class.pst new file mode 100644 index 000000000..5982fa8d2 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-effect-class.pst differ diff --git a/tests/fabric/vectors/pst/bad-length-empty-file.pst b/tests/fabric/vectors/pst/bad-length-empty-file.pst new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fabric/vectors/pst/bad-length-mismatch.pst b/tests/fabric/vectors/pst/bad-length-mismatch.pst new file mode 100644 index 000000000..846f7f271 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-length-mismatch.pst differ diff --git a/tests/fabric/vectors/pst/bad-length-over-1024.pst b/tests/fabric/vectors/pst/bad-length-over-1024.pst new file mode 100644 index 000000000..4c0525243 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-length-over-1024.pst differ diff --git a/tests/fabric/vectors/pst/bad-length-short-header.pst b/tests/fabric/vectors/pst/bad-length-short-header.pst new file mode 100644 index 000000000..29fc12f8c Binary files /dev/null and b/tests/fabric/vectors/pst/bad-length-short-header.pst differ diff --git a/tests/fabric/vectors/pst/bad-magic.pst b/tests/fabric/vectors/pst/bad-magic.pst new file mode 100644 index 000000000..519369b22 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-magic.pst differ diff --git a/tests/fabric/vectors/pst/bad-no-cause.pst b/tests/fabric/vectors/pst/bad-no-cause.pst new file mode 100644 index 000000000..971e0e53c Binary files /dev/null and b/tests/fabric/vectors/pst/bad-no-cause.pst differ diff --git a/tests/fabric/vectors/pst/bad-no-idempotence-key.pst b/tests/fabric/vectors/pst/bad-no-idempotence-key.pst new file mode 100644 index 000000000..e958e0db3 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-no-idempotence-key.pst differ diff --git a/tests/fabric/vectors/pst/bad-no-task-id.pst b/tests/fabric/vectors/pst/bad-no-task-id.pst new file mode 100644 index 000000000..e882c8b65 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-no-task-id.pst differ diff --git a/tests/fabric/vectors/pst/bad-payload-len-over-800.pst b/tests/fabric/vectors/pst/bad-payload-len-over-800.pst new file mode 100644 index 000000000..630f57af6 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-payload-len-over-800.pst differ diff --git a/tests/fabric/vectors/pst/bad-request-no-objective.pst b/tests/fabric/vectors/pst/bad-request-no-objective.pst new file mode 100644 index 000000000..b471c1d5d Binary files /dev/null and b/tests/fabric/vectors/pst/bad-request-no-objective.pst differ diff --git a/tests/fabric/vectors/pst/bad-reserved0.pst b/tests/fabric/vectors/pst/bad-reserved0.pst new file mode 100644 index 000000000..9d1a3bf84 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-reserved0.pst differ diff --git a/tests/fabric/vectors/pst/bad-reserved1.pst b/tests/fabric/vectors/pst/bad-reserved1.pst new file mode 100644 index 000000000..dbb6b8d2e Binary files /dev/null and b/tests/fabric/vectors/pst/bad-reserved1.pst differ diff --git a/tests/fabric/vectors/pst/bad-trailing-byte.pst b/tests/fabric/vectors/pst/bad-trailing-byte.pst new file mode 100644 index 000000000..4a957d099 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-trailing-byte.pst differ diff --git a/tests/fabric/vectors/pst/bad-type-unknown.pst b/tests/fabric/vectors/pst/bad-type-unknown.pst new file mode 100644 index 000000000..400b0bbb7 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-type-unknown.pst differ diff --git a/tests/fabric/vectors/pst/bad-type-zero.pst b/tests/fabric/vectors/pst/bad-type-zero.pst new file mode 100644 index 000000000..98cded056 Binary files /dev/null and b/tests/fabric/vectors/pst/bad-type-zero.pst differ diff --git a/tests/fabric/vectors/pst/bad-version-downgrade.pst b/tests/fabric/vectors/pst/bad-version-downgrade.pst new file mode 100644 index 000000000..dafe08c4a Binary files /dev/null and b/tests/fabric/vectors/pst/bad-version-downgrade.pst differ diff --git a/tests/fabric/vectors/pst/bad-version-unknown.pst b/tests/fabric/vectors/pst/bad-version-unknown.pst new file mode 100644 index 000000000..3fc9c4a3e Binary files /dev/null and b/tests/fabric/vectors/pst/bad-version-unknown.pst differ diff --git a/tests/fabric/vectors/pst/expected.txt b/tests/fabric/vectors/pst/expected.txt new file mode 100644 index 000000000..11fae5d1d --- /dev/null +++ b/tests/fabric/vectors/pst/expected.txt @@ -0,0 +1,29 @@ +# PST1 single-message vectors. Spec: ../SPEC.md (Draft v0.1, wire version 1). +# Line: , verdict line as printed by tools/pst_verify.c: +# ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64 parent=H32 +# task=H32 idem=H32 ordinal=N effect=E len=L digest=H64 (one line) +# refuse +# Generated by tools/pst_verify.c --make-vectors; do not edit by hand. +ok-request.pst ok type=1 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=0 effect=1 len=49 digest=e57392a27132b97e2cbfaa4b04ad3446cab722297cf48ce22c7c2fb787940cc6 +ok-accepted-empty.pst ok type=2 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=1 effect=1 len=0 digest=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +ok-text-chunk-1024.pst ok type=5 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=9ea5acb3bac1c8cfd6dde4ebf2f90007 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=7 effect=0 len=800 digest=20885ae2150992180a94350ef60c6d8a1599899bcd2752d3392f8ad591ddd8b1 +ok-resume-request-max-ordinal.pst ok type=14 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=4294967295 effect=2 len=0 digest=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +bad-length-short-header.pst refuse PST_BAD_LENGTH +bad-length-empty-file.pst refuse PST_BAD_LENGTH +bad-length-over-1024.pst refuse PST_BAD_LENGTH +bad-magic.pst refuse PST_BAD_MAGIC +bad-version-unknown.pst refuse PST_BAD_VERSION +bad-version-downgrade.pst refuse PST_DOWNGRADE +bad-type-zero.pst refuse PST_BAD_TYPE +bad-type-unknown.pst refuse PST_BAD_TYPE +bad-reserved0.pst refuse PST_RESERVED_NONZERO +bad-reserved1.pst refuse PST_RESERVED_NONZERO +bad-length-mismatch.pst refuse PST_BAD_LENGTH +bad-payload-len-over-800.pst refuse PST_BAD_LENGTH +bad-trailing-byte.pst refuse PST_TRAILING_BYTES +bad-effect-class.pst refuse PST_BAD_EFFECT_CLASS +bad-no-cause.pst refuse PST_NO_CAUSE +bad-no-task-id.pst refuse PST_NO_TASK +bad-no-idempotence-key.pst refuse PST_NO_TASK +bad-request-no-objective.pst refuse PST_NO_OBJECTIVE +bad-digest.pst refuse PST_DIGEST_MISMATCH diff --git a/tests/fabric/vectors/pst/ok-accepted-empty.pst b/tests/fabric/vectors/pst/ok-accepted-empty.pst new file mode 100644 index 000000000..92bd34916 Binary files /dev/null and b/tests/fabric/vectors/pst/ok-accepted-empty.pst differ diff --git a/tests/fabric/vectors/pst/ok-request.pst b/tests/fabric/vectors/pst/ok-request.pst new file mode 100644 index 000000000..685b25808 Binary files /dev/null and b/tests/fabric/vectors/pst/ok-request.pst differ diff --git a/tests/fabric/vectors/pst/ok-resume-request-max-ordinal.pst b/tests/fabric/vectors/pst/ok-resume-request-max-ordinal.pst new file mode 100644 index 000000000..5daa99c2e Binary files /dev/null and b/tests/fabric/vectors/pst/ok-resume-request-max-ordinal.pst differ diff --git a/tests/fabric/vectors/pst/ok-text-chunk-1024.pst b/tests/fabric/vectors/pst/ok-text-chunk-1024.pst new file mode 100644 index 000000000..9ad712980 Binary files /dev/null and b/tests/fabric/vectors/pst/ok-text-chunk-1024.pst differ diff --git a/tests/gates/run.sh b/tests/gates/run.sh index 68fd6545e..023c87ab1 100755 --- a/tests/gates/run.sh +++ b/tests/gates/run.sh @@ -290,7 +290,7 @@ case "$step" in # reaches the log through a process substitution, not a pipe. make test-r3 test-r7 test-r16-operator-authority-misuse test-r8 test-r9 test-r12 test-rx-emergency | tee build/rx-host-reference.log speed test-action-graph rx_action_graph_receipt.json OMEGA_ACTION_GRAPH_IR_PASS > >(tee -a build/rx-host-reference.log) - make test-state-projection test-capability-query test-capability-graph test-skillroute-compose test-semantic-comm test-cognitive-routing test-sem-incremental test-cortex test-jspace-prod test-composition test-composition-attach-asan test-composition-gate test-fabric test-fabric-living | tee -a build/rx-host-reference.log + make test-state-projection test-capability-query test-capability-graph test-skillroute-compose test-semantic-comm test-cognitive-routing test-sem-incremental test-cortex test-jspace-prod test-composition test-composition-attach-asan test-composition-gate test-fabric test-fabric-work test-fabric-living | tee -a build/rx-host-reference.log make test-machine-identity | tee -a build/rx-host-reference.log make test-effect-cap64 | tee build/effect-cap64.log grep -q '^OMEGA_EFFECT_CAP64_PASS$' build/effect-cap64.log ```