aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#366 (code only)
Apply it
git checkout -b patch-08b8edab899fafc2 dd99eda1ec2d3e6b097bfb71a9583b153e3face7 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/08b8edab899fafc2.patch | git am
Provenance
github-import 1
aien-dev/aienux
366
455dc6a6aa5735b90fcbd15728f505af90945d3e
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":366,"url":"https://github.com/aien-dev/aienux/pull/366","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:17Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 14231f96..d282b3f4 100644 --- a/.crumb +++ b/.crumb @@ -21,39 +21,39 @@ "generated": { "children": [ { - "digest": "e04ed38306b5ade84b4d72cfef14db7d58ab3952031806b43c8132a985a3d403", - "last_commit": "477f9e5", + "digest": "ba157598d0766ef2f9e7fbc3b0333b3fd84fcfc9799497a70d2aa6dd0ea9afb3", + "last_commit": "bbf4428", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "f2338e5e6ba11edda667a37455cf878a840dac45cdad067b9a0c98642c1cc466", - "last_commit": "298065e", + "digest": "8b4247e1025c916ebcb329bb04e14d48822093b477cc5660d282ee3e5e87ce25", + "last_commit": "bbf4428", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "443333b76192539859c8e501f18091153980e715c490920441f2c7e177f5fae1", - "last_commit": "867c4ac", + "digest": "594ccac341112632f0c0264e2e9ac1dbe7e1a1b5f39db1a1997ae3755355f3f2", + "last_commit": "bbf4428", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "f94d1bca3f533e19fd6fee07f9e931ba85bacec7633de4f456534426c4a31bb4", - "last_commit": "6f35eab", + "digest": "553278293606dd1b57c8dbdb1167c88ae7b877912a26b060fd1cd62acc4e52b7", + "last_commit": "bbf4428", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:a49920bafccc4502104e957e660e31f51610b8470b098532300056c17e6ba8f2", - "compiled_at": "2026-10-13T17:23:51Z", + "children_root": "sha256:1a0529f7237503dfcd760caddd67ecf96998eccf528ef6a21c7301fee897bc3b", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:46de129e17740fade6e58e61f5edebd9db0431aa8f7e699d330cc477863efe88", + "digest": "sha256:49a5ea2a1ce21ed4d7f80e2105ab6f141665c45175226a5cfc5e35cb644cb4c7", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3176, - "generated_at_commit": "298065e8f6711a85823fe4ff051738e7c30cbbd1", - "source_tree": "sha256:a955e5c5a6c8b3d973fe88218bfee680e7542f0a4c3e9534de5517b87f6a3ce6" + "files": 3181, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:60f6b9cfd30953a52ed0eb35dfad743677b3ba68cdb0db7c77055f0ef68c3e8d" }, "owner_lane": "", "provenance": { diff --git a/crates/.crumb b/crates/.crumb index 8a13f1c6..cbb85040 100644 --- a/crates/.crumb +++ b/crates/.crumb @@ -150,14 +150,14 @@ } ], "children_root": "sha256:94f052910f8e65e9eca98d9c0b5d433ef2288dcd8ca1c474518e716295999644", - "compiled_at": "2026-10-13T15:46:48Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:e04ed38306b5ade84b4d72cfef14db7d58ab3952031806b43c8132a985a3d403", + "digest": "sha256:ba157598d0766ef2f9e7fbc3b0333b3fd84fcfc9799497a70d2aa6dd0ea9afb3", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 234, - "generated_at_commit": "7df4066ad6dbe14a05fbfc4408bcf48399b2b010", - "source_tree": "sha256:82a2287958cb9ab68a6a6afaa45609e62ae9af7c510713aa7390bc6f3dd773d0" + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:6e05723abc5f59f3ac4e57a5dd6d9ab44a58cbd6f9d41522f7bc4e3fea69b11f" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-infer-kernel/src/session.rs b/crates/aienos-infer-kernel/src/session.rs index 23851371..b1dad4f3 100644 --- a/crates/aienos-infer-kernel/src/session.rs +++ b/crates/aienos-infer-kernel/src/session.rs @@ -270,6 +270,29 @@ pub unsafe extern "C" fn aienos_infer_session_step(token: *mut u32, us: *mut u64 rc } +/// Copy the token ids of the current request's prompt (chat template included) into `out[..cap]`. +/// Returns the number of prompt ids (which may exceed `cap`; only `cap` are copied), 0 when no prompt +/// is pending, or `E_STATE` without a session. The kernel uses the first four ids to check the chat +/// template the tokenizer built (PI5-6 agent: restores the one-shot probe's expect_ids check). +/// +/// # Safety +/// `out` must point to `cap` writable u32s (or be null with `cap` 0). Same execution contract as open. +#[no_mangle] +pub unsafe extern "C" fn aienos_infer_session_prompt_ids(out: *mut u32, cap: u32) -> i32 { + let s = match slot() { + Some(s) => s, + None => return E_STATE, + }; + if out.is_null() && cap != 0 { + return E_ARG; + } + let n = s.prompt.len().min(cap as usize); + if n > 0 { + core::ptr::copy_nonoverlapping(s.prompt.as_ptr(), out, n); + } + s.prompt.len() as i32 +} + /// Abandon the current request: the cache is forgotten and no further step /// does work. Returns the number of tokens produced before the cancel, or /// `E_STATE` without a session. Idempotent. diff --git a/docs/.crumb b/docs/.crumb index 1d646525..cd98245a 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:c5d7ca9ce0145309c90a439711adcb6dd8e8e40ff48b90455f545e63c05f04c8", - "compiled_at": "2026-10-13T17:23:51Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:f2338e5e6ba11edda667a37455cf878a840dac45cdad067b9a0c98642c1cc466", + "digest": "sha256:8b4247e1025c916ebcb329bb04e14d48822093b477cc5660d282ee3e5e87ce25", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 78, - "generated_at_commit": "298065e8f6711a85823fe4ff051738e7c30cbbd1", - "source_tree": "sha256:4d9b61c78b740ce46de654971b0a0b03b1fdf5d3a31a3426c7f89124bf50908d" + "files": 79, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:8b10c367780e6bbb7c1bdf741ea114a97b30276741bc37d5b99b013ea7329d71" }, "owner_lane": "", "provenance": { diff --git a/docs/native/PI5-6-AGENT-RUNS.md b/docs/native/PI5-6-AGENT-RUNS.md new file mode 100644 index 00000000..979b634c --- /dev/null +++ b/docs/native/PI5-6-AGENT-RUNS.md @@ -0,0 +1,84 @@ +# PI5-6 final cut: a resident model's bounded reply, persisted in the sealed Store, recovered after a reboot (Raspberry Pi 5) + +Tier: PI5-AGENT-QUALIFICATION, QUALIFICATION ONLY while the recovery secret comes from the card request file +(`\EFI\AIENOS\UNLOCK.REQ`, a throwaway secret, PI5-4 cut 2). Image: `make pi5-agent` +(`CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=...`). Hardware runs G1 to G3 are NOT_RUN in this document +(the Pi was busy with another job); the host proof and the QEMU regressions are below. + +## What is reused (no parallel format) + +- Store object: the lifecycle's approved-reply object, kind `0x5231` ('R1'), `aienos.reply.v1\nprompt: <prompt>\nreply: <text>\n` + (core/lifecycle.c `commit_task`, QEMU rows 174 to 176 and 205 to 206 of native/kernel/GATES.md). The builder now lives in + `core/reply_obj.h` and `core/lifecycle.c` calls it; the host test pins its output to the literal the lifecycle wrote before. + The agent object adds a trailer after the reply: `model: <64 hex sha256>\nids: <token ids>\n`. A lifecycle reply has no trailer. +- Inference: the resident path of core/infer.c (`ck_infer_run` ingest + SHA-256 + `resident_open`, `ck_infer_prompt`, + `ck_infer_resident_close`), the same functions the QEMU service image uses. `CK_INFER_RESIDENT` is now also set by the Pi agent image. +- Model source: MODEL.MAP on the ESP naming the raw partition `AIENOS-MODEL` (PI5-6 cut 3), read by the UEFI stub. SDOT path, 1.20 tok/s. +- Store open: PI5-4 (`ck_store_pi5_unlock_run`, Slot 1 recovery secret from UNLOCK.REQ). Hook shape: PI5-5 (`ck_store_pi5_fact_stage`). + +## What is new + +- `svc/pi5_agent.[ch]`, hook `ck_store_pi5_agent_stage` in svc/store_boot.c, run right after the Store stage and before the Store is released: + - Store not open (wrong secret): `agent: NOTHING RECOVERED ...`, `AIENOS_PI5_AGENT: NOSTORE`. The model is not hashed, no session is opened, + nothing is written. + - The Store holds an agent reply for the fixed prompt: RECOVER. The object is read back through the sealed Store (`ss_read`), parsed + from its end, and checked against the golden token ids, golden text and golden model digest. It never uses the model, even when MODEL.MAP is on the card. + - No reply yet: GENERATE. Prompt `What is the capital of France?`, cap N = 16 tokens (`SESS_MAX_NEW`; the reply is 8 tokens ending at EOT). + The reply is committed only if the chat-template head is 128000,128006,882,128007, the 8 ids equal llama.cpp's + (791 6864 315 9822 374 12366 13 128009), the text is `The capital of France is Paris.` and the model digest is the golden one. The session is + closed (all 256 MiB of heap returned) before the commit. The reply is read back from the Store the way a cold boot reads it. + - Both ways print the same line: `agent: reply store_gen=G object_sha256=H model_sha256=M tokens=N ids=... text="..."`, so G2's line must equal G1's. +- Prompt-token check restored (it was dropped on the Pi when the one-shot probe was skipped): the new Rust call + `aienos_infer_session_prompt_ids` (crates/aienos-infer-kernel/src/session.rs) gives the kernel the prompt ids; `session_request` in core/infer.c checks the + first four against `expect_ids` (`infer: session prompt=2 prompt_head=... expect=match`), so `make pi5-infer` and the QEMU infer gate judge it, + and `ck_infer_prompt` fills `prompt_head` for the agent. scripts/qemu_ck_infer_test.sh checks the new line and has three new self-test mutations. +- The watchdog is petted after every generation step in `ck_infer_prompt` (no-op on every board except the Pi). +- Makefile: `CK_PI5_AGENT=1` needs `CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_INFER_LIB`, refuses CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_FACT, + CK_SERVICE, CK_HARDWARE_STAGING and CK_TEST_FWCFG_ARTIFACTS (and CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_FACT refuse it). Own OUT + `target/native-kernel-pi5-unlock-card-agent`. scripts/ck_pi5_build_check.sh covers all of it. + +## Order: unlock the Store BEFORE the model is hashed and run + +Decision: Store first, model second, commit last. +- A wrong or missing secret fails in milliseconds, before the 5.1 s model hash, the 0.3 s session open, the 256 MiB heap and the 17 s of generation. Nothing + slow ever runs for a boot that could not keep its result. The host test counts the calls: wrong secret = zero model calls. +- The boot that only needs to recover (G2) never touches the model. It needs no heap and no hash: the boot is the Store open plus one object read. +- The mode (recover or generate) is decided from what the Store holds, never from whether the model is on the card, so a leftover MODEL.MAP cannot cause a second reply. +- Watchdog (15 s PM watchdog, petted at boot stages, every 16 MiB hashed and every generation step): the unlock is one short stage; the only + long stretches are the hash (pet per 16 MiB) and the steps (pet per step, the longest step is the 0.9 s of a token). Nothing waits on the SD card for long. +- Failure modes. Power loss during generation: the Store is open but no transaction is in flight, so the card holds exactly what it held. A reply that is not golden, a refused or cancelled prompt, a + missing model, or a digest mismatch: nothing is committed (host test: the next boot still says NOTHING GENERATED). Power loss during the commit: one sealed transaction (the Store's own crash rule), the + object is either whole or absent. The cost of this order: the Store (about 2 MiB of heap and its 1.1 MiB image) is resident during generation, and the SD card is idle for about 30 s between the unlock and the commit. +- The other order (model first, as the QEMU service image does after its Store stage, or as `make pi5-infer` does before it) would spend the 30 s and the 256 MiB, then learn the secret was wrong. + +## Memory budget (Pi 5, 8 GB class board) + +Measured values are from the PI5-4 and PI5-6 runs; the agent image's own run is G1 (NOT_RUN). +| item | MiB | source | +| --- | --- | --- | +| free after boot, no model on the card | 7033.6 (7375265792 B) | pi5-unlock-a `memory:` line | +| model, reserved by the stub (807694368 B, plus rounding) | 770.6 | pi5-infer-b: 6567268352 B free with it, 807997440 B less | +| free with the model, at the start of the agent stage | 6263.0 | pi5-infer-b `memory:` line | +| inference heap (65536 frames), returned at close | 256.0 | core/infer.c HEAP_BYTES, `frames_returned=65536` | +| inside the heap: session open / peak | 22.4 / 24.1 | pi5-infer-b `heap_used_kib=22900`, `heap_peak_kib=24695` | +| Store stage heap use (64 MiB kernel heap, peak) | 2.0 | pi5-unlock-a `mm_usage heap_min_free` | +| agent image .bss (all static buffers, 32 KiB of them the agent's) | 1.1 | `size` of aienos-ck.elf | +| free during generation (6263.0 - 256.0 - 2.0 - 1.1) | about 6004 | arithmetic | +| free on the recovery boot (no model on the card, no heap) | about 7030 | arithmetic | +Model + heap + Store = about 1030 MiB, 14.6 percent of the memory the kernel owns. The 256 MiB heap is 10 times the 24.1 MiB the session peaks at (a deliberate margin, unchanged from the proven pi5-infer image). + +## Host proof + +`make -C native/kernel -f stage.mk pi5-agent-test pi5-agent-sanitize pi5-agent-mutants` (run by `stage-test`, `stage-sanitize`): dev/tests/pi5_agent_test.c, 120 checks, +ASan and UBSan clean. A file stands in for the card, every boot starts from a cold C state, the inference unit is a stub (a real model cannot run on the host); the stage code under test is the real svc/pi5_agent.c. +It proves: the reply object equals the lifecycle's bytes and adds only the trailer; boot 1 generates, closes, commits once and reads back; boot 2 (same secret, no model) prints exactly boot 1's `agent: reply` line (same Store generation, same object digest) with +zero model calls, also when the model is present; wrong secret: nothing recovered, zero model calls, card byte-identical, and the right secret afterwards recovers again; eight non-golden generations (wrong id, wrong head, wrong text, wrong model digest, refused prompt, cancelled prompt, +model vanished, too many tokens) commit nothing; no reply and no model is NOMODEL; a lifecycle reply for the same prompt and a reply for another prompt are ignored; a stored non-golden reply is reported FAIL and is never regenerated over. +Mutation proof (each TEST-ONLY mutant must make the test fail; all six killed): NO_COMMIT (commit skipped, success reported), RECOVER_BLIND (recovery never reads the Store), NO_GOLDEN_CHECK, NO_PROMPT_CHECK, RECOVER_NO_CHECK, MODEL_FIRST (model touched before the Store check). +Regressions: `make -C native/kernel test`; `scripts/ck_pi5_build_check.sh`; the QEMU infer gate (`scripts/qemu_ck_infer_test.sh`, PASS, includes the restored prompt-head check) and the QEMU lifecycle gate (`scripts/qemu_ck_lifecycle_test.sh`, the reply object path). + +## Pi runs (NOT_RUN) + +G1 generate and commit (boot 1: UNLOCK.REQ with secret S, MODEL.MAP placed). G2 cold-boot recovery (the run tool power-cycles the board; UNLOCK.REQ with S, no MODEL.MAP, no generation). G3 negative control (wrong secret W, MODEL.MAP placed so a +leak into the model path would show): nothing recovered, no `infer: sha256=` line, the AIENOS partition's sha256 identical before and after. The exact commands are in the PR description. +Limits: PRODUCTION-class Store from PI5-4 but the secret comes from the card, there is no TPM; the anchor shares the disk, so a whole-disk rollback is not detectable; one golden prompt only; speed 1.20 tok/s; single runs. diff --git a/native/.crumb b/native/.crumb index 0c639062..6509a672 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "6e575096cfcc2df4792bbae3821d3d77b414aa432bda02d3fcddc8a2ff913143", - "last_commit": "867c4ac", + "digest": "f88b7747a96248358fef0d26dbc5de564a99b390488fe689b7f540a9b3d15ed6", + "last_commit": "bbf4428", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:eac95440257feb868df67082aa612e6ed39e4cc60160e6c0f3028e580bc9382b", - "compiled_at": "2026-10-13T17:23:51Z", + "children_root": "sha256:cf8f8f1ee7c995244ee9eda13f60654a76cee0d069f8dae979cfba21b051edd2", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:443333b76192539859c8e501f18091153980e715c490920441f2c7e177f5fae1", + "digest": "sha256:594ccac341112632f0c0264e2e9ac1dbe7e1a1b5f39db1a1997ae3755355f3f2", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 535, - "generated_at_commit": "298065e8f6711a85823fe4ff051738e7c30cbbd1", - "source_tree": "sha256:d33b4088ae75ab0cbecb1563d7148d737c275768835a929fb0b747a10be43c7f" + "files": 539, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:985e777d9a7b39da27d6ea111387014f14df9a3968dec4d09b926eb8f1b6e0d4" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index e83e8a80..7712e5ad 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,20 +57,20 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "7916404f955114d22a806f1726b7a2b8cf79f56076af9ee702019143feb8e0a8", - "last_commit": "867c4ac", + "digest": "06d3fe3bc99ab75f650f57fef58eb547f155ddb0d4a399cd01ae941f4250aa3c", + "last_commit": "bbf4428", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "a80356e38bcc9fc135fffc058dc782bc2fc4f56869f6e06caaf0c2e169338212", - "last_commit": "6f35eab", + "digest": "c33332d860fe8c2c50a901891772289271c5bf0e7a442fe46dd9b881033c7966", + "last_commit": "bbf4428", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { - "digest": "d3e1b6aa9a623c95208e730cea917c2824682caa6518dd4433882908b5facef3", - "last_commit": "502d168", + "digest": "397a413635dc958cb318d10730d43dd0c1f8edb19222c82b824af3e70e48d451", + "last_commit": "bbf4428", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -81,8 +81,8 @@ "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "11e19f80d8332984392eda3141fe2a5b50b9032fbb4d1ace6aabf29e445cf9ab", - "last_commit": "6f35eab", + "digest": "0c7ae2a3793e31669cfeb9a33841d7a77d3dc91f61e6b4428b43dd33470ab929", + "last_commit": "bbf4428", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:15688dcd8360057fd3d4d6b0f7978f09ec0f81e5e8323bb75d707d70cd55e453", - "compiled_at": "2026-10-13T17:23:51Z", + "children_root": "sha256:caf9b50a67cce14d53e781466dfa36e840a007840c97dccde8027517dfd79a63", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:6e575096cfcc2df4792bbae3821d3d77b414aa432bda02d3fcddc8a2ff913143", + "digest": "sha256:f88b7747a96248358fef0d26dbc5de564a99b390488fe689b7f540a9b3d15ed6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 356, - "generated_at_commit": "298065e8f6711a85823fe4ff051738e7c30cbbd1", - "source_tree": "sha256:3907ec2526bc6cb7207438ec3799fc07856f36855f8545a7f308057e454b42d5" + "files": 360, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:d3770084c352ea2fbec243042d57fe1a197e760ac1d335dedd967460c5b1e7f2" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 0e398fe1..5bfeee42 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -159,8 +159,8 @@ ifneq ($(strip $(CK_PI5_INFER)),) ifneq ($(strip $(CK_PI5_INFER)),1) $(error CK_PI5_INFER must be empty or 1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_UNLOCK)$(CK_SERVICE)$(CK_HARDWARE_STAGING)$(CK_TEST_FWCFG_ARTIFACTS)$(CK_RUST_LIBS)),) -$(error CK_PI5_INFER is the one-shot Pi inference image: it cannot be combined with CK_PI5_STORE, CK_PI5_UNLOCK, CK_SERVICE, CK_HARDWARE_STAGING, CK_TEST_FWCFG_ARTIFACTS or CK_RUST_LIBS) +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_UNLOCK)$(CK_PI5_AGENT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)$(CK_TEST_FWCFG_ARTIFACTS)$(CK_RUST_LIBS)),) +$(error CK_PI5_INFER is the one-shot Pi inference image: it cannot be combined with CK_PI5_STORE, CK_PI5_UNLOCK, CK_PI5_AGENT, CK_SERVICE, CK_HARDWARE_STAGING, CK_TEST_FWCFG_ARTIFACTS or CK_RUST_LIBS) endif ifeq ($(strip $(CK_INFER_LIB)),) $(error CK_PI5_INFER needs CK_INFER_LIB=<libaienos_infer_kernel.a> (use "make pi5-infer", which builds it)) @@ -178,8 +178,8 @@ ifneq ($(strip $(CK_PI5_PCIE)),) ifneq ($(strip $(CK_PI5_PCIE)),1) $(error CK_PI5_PCIE must be empty or 1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_UNLOCK)$(CK_SERVICE)$(CK_HARDWARE_STAGING)),) -$(error CK_PI5_PCIE is the Pi PCIe/RP1 enumerator image: it cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_UNLOCK, CK_SERVICE or CK_HARDWARE_STAGING) +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_UNLOCK)$(CK_PI5_AGENT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)),) +$(error CK_PI5_PCIE is the Pi PCIe/RP1 enumerator image: it cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_UNLOCK, CK_PI5_AGENT, CK_SERVICE or CK_HARDWARE_STAGING) endif CK_PI5 := 1 OUT ?= $(REPO)/target/native-kernel-pi5-pcie @@ -219,7 +219,7 @@ ifneq ($(strip $(CK_SERVICE)$(CK_TEST_STORE_CRASH)$(CK_TEST_CONTINUITY)$(CK_HARD $(error CK_PI5_UNLOCK cannot be combined with CK_SERVICE, CK_SERVICE_TEST_KEYS, CK_TEST_STORE_CRASH, CK_TEST_CONTINUITY or CK_HARDWARE_STAGING) endif CK_PI5 := 1 -OUT ?= $(REPO)/target/native-kernel-pi5-unlock$(if $(strip $(CK_PI5_UNLOCK_CARD)),-card)$(if $(strip $(CK_PI5_FACT)),-fact) +OUT ?= $(REPO)/target/native-kernel-pi5-unlock$(if $(strip $(CK_PI5_UNLOCK_CARD)),-card)$(if $(strip $(CK_PI5_FACT)),-fact)$(if $(strip $(CK_PI5_AGENT)),-agent) CK_EXTRA_CFLAGS += -DCK_PI5_UNLOCK=1 ifneq ($(strip $(CK_PI5_UNLOCK_CARD)),) CK_EXTRA_CFLAGS += -DCK_PI5_UNLOCK_CARD=1 @@ -239,11 +239,33 @@ endif ifneq ($(strip $(CK_PI5_UNLOCK))$(strip $(CK_PI5_UNLOCK_CARD)),11) $(error CK_PI5_FACT (Cortex fact stage, QUALIFICATION ONLY) needs CK_PI5_UNLOCK=1 and CK_PI5_UNLOCK_CARD=1) endif -ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)),) -$(error CK_PI5_FACT cannot be combined with CK_PI5_STORE, CK_PI5_INFER or CK_PI5_PCIE) +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_AGENT)),) +$(error CK_PI5_FACT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE or CK_PI5_AGENT) endif CK_EXTRA_CFLAGS += -DCK_PI5_FACT=1 endif + +# Pi agent image (CK_PI5_AGENT=1, "make pi5-agent", PI5-6 final cut, QUALIFICATION ONLY while the secret comes from the card request +# file): the Pi unlock-card image (production-class Store opened by the Slot 1 recovery secret) plus the resident inference unit +# (CK_INFER_LIB, model from MODEL.MAP only) plus the agent stage (svc/pi5_agent.c): after the Store opened it recovers the persisted +# reply, or (no reply yet) generates a bounded reply and commits it as a sealed R1 object. It needs CK_PI5_UNLOCK=1, CK_PI5_UNLOCK_CARD=1 +# and CK_INFER_LIB, refuses every other tier flag (CK_PI5_INFER and CK_PI5_FACT included) and has its own OUT. +CK_PI5_AGENT ?= +ifneq ($(strip $(CK_PI5_AGENT)),) +ifneq ($(strip $(CK_PI5_AGENT)),1) +$(error CK_PI5_AGENT must be empty or 1) +endif +ifneq ($(strip $(CK_PI5_UNLOCK))$(strip $(CK_PI5_UNLOCK_CARD)),11) +$(error CK_PI5_AGENT (Store plus inference, QUALIFICATION ONLY) needs CK_PI5_UNLOCK=1 and CK_PI5_UNLOCK_CARD=1) +endif +ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_INFER)$(CK_PI5_PCIE)$(CK_PI5_FACT)$(CK_SERVICE)$(CK_HARDWARE_STAGING)$(CK_TEST_FWCFG_ARTIFACTS)),) +$(error CK_PI5_AGENT cannot be combined with CK_PI5_STORE, CK_PI5_INFER, CK_PI5_PCIE, CK_PI5_FACT, CK_SERVICE, CK_HARDWARE_STAGING or CK_TEST_FWCFG_ARTIFACTS) +endif +ifeq ($(strip $(CK_INFER_LIB)),) +$(error CK_PI5_AGENT needs CK_INFER_LIB=<libaienos_infer_kernel.a> (use "make pi5-agent", which builds it)) +endif +CK_EXTRA_CFLAGS += -DCK_PI5_AGENT=1 -DCK_INFER_DISK_ONLY=1 -DCK_INFER_RESIDENT=1 +endif CK_PI5 ?= ifneq ($(strip $(CK_PI5)),) ifneq ($(strip $(CK_PI5)),1) @@ -885,13 +907,16 @@ endif # Pi path). core/pi5.c supplies ck_dev_boot_disk and ck_dev_nvme_release over the SD partition view. FULL_STAGE_SYMS := ck_stage_devices ck_stage_security ck_stage_store ck_stage_quiesce ck_stage_disk_artifacts ifneq ($(strip $(CK_PI5_STORE)$(CK_PI5_UNLOCK)),) -FULL_NAME := $(if $(strip $(CK_PI5_UNLOCK)),full-pi5-unlock$(if $(strip $(CK_PI5_UNLOCK_CARD)),-card)$(if $(strip $(CK_PI5_FACT)),-fact),full-pi5-store) +FULL_NAME := $(if $(strip $(CK_PI5_UNLOCK)),full-pi5-unlock$(if $(strip $(CK_PI5_UNLOCK_CARD)),-card)$(if $(strip $(CK_PI5_FACT)),-fact)$(if $(strip $(CK_PI5_AGENT)),-agent),full-pi5-store) FULL_STAGE_SYMS := ck_stage_store ck_stage_disk_artifacts STAGE_SRCS := $(KDIR)/svc/store_boot.c $(KDIR)/svc/artifact_store.c $(KDIR)/svc/store_crash.c $(KDIR)/svc/ck_compat.c \ $(foreach m,store/store_v1 store/store_engine store/store_disk store/store_sealed store/torn_slot m5/m5_keys m5/m5_envelope m5/m5_state crypto/aes crypto/polyval crypto/gcm_siv crypto/hmac crypto/ct argus/sha256,$(NATIVE)/$(m).c) ifneq ($(strip $(CK_PI5_FACT)),) STAGE_SRCS += $(KDIR)/svc/pi5_fact.c $(STAGE_CORTEX_SRCS) endif +ifneq ($(strip $(CK_PI5_AGENT)),) +STAGE_SRCS += $(KDIR)/svc/pi5_agent.c +endif endif FULL_OUT := $(OUT)/$(FULL_NAME) STAGE_KOBJ := $(OUT)/obj/stage-$(FULL_NAME) @@ -982,7 +1007,7 @@ $(foreach s,$(STAGE_SRCS),$(eval $(call stage_rule,$(s)))) # with inference (CK_INFER_LIB + CK_HARDWARE_STAGING, boot-disk only) must # carry the boot-disk source line, the resident session, and no fw_cfg code. define fwcfg_check - @if [ -n "$(strip $(CK_INFER_LIB))" ] && [ -z "$(strip $(CK_HARDWARE_STAGING))" ] && [ -z "$(strip $(CK_PI5_INFER))" ]; then \ + @if [ -n "$(strip $(CK_INFER_LIB))" ] && [ -z "$(strip $(CK_HARDWARE_STAGING))" ] && [ -z "$(strip $(CK_PI5_INFER))" ] && [ -z "$(strip $(CK_PI5_AGENT))" ]; then \ grep -aqF "TEST-ONLY QEMU fw_cfg model ingest" $(1) || { echo "inference probe image lacks its TEST-ONLY fw_cfg announcement"; rm -f $(1); exit 1; }; \ elif [ "$(strip $(CK_TEST_FWCFG_ARTIFACTS))" = 1 ]; then \ grep -aqF "TEST-ONLY QEMU fw_cfg side channel" $(1) || { echo "test fw_cfg image lacks its TEST-ONLY announcement"; rm -f $(1); exit 1; }; \ @@ -1933,6 +1958,10 @@ pi5-unlock-card: pi5-fact: $(MAKE) CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_FACT=1 full +pi5-agent: + CARGO_TARGET_DIR=$(REPO)/target/infer-kernel cargo build -q --release --target aarch64-unknown-none --manifest-path $(REPO)/crates/aienos-infer-kernel/Cargo.toml + $(MAKE) CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=$(PI5_INFER_LIB) full + pi5: $(MAKE) CK_PI5=1 all @@ -1988,7 +2017,7 @@ $(OUT)/host-san/test_pi5_rng: $(KDIR)/tests/test_pi5_rng.c $(PI5_RNG_SRCS) $(KDI FORCE: .PHONY: uefi-vars slot-loader slot-loader-check pi5-diag -.PHONY: all full pi5 pi5-store pi5-infer pi5-unlock pi5-unlock-card pi5-fact test sanitize handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants acpi-scan clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE +.PHONY: all full pi5 pi5-store pi5-infer pi5-unlock pi5-unlock-card pi5-fact pi5-agent test sanitize handoff-mutant acpi-dev-mutant iort-mutants smmu-l2-mutants acpi-scan clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE # AIENOS-PI5 step 3 marker + Pi card GPT test (hosted; a file stands in for the card, QEMU cannot model the Pi SD host). PI5_MARKER_SRCS := $(KDIR)/core/pi5_marker.c $(KDIR)/dev/disk_part.c $(NATIVE)/disk/disk.c $(NATIVE)/disk/disk_file.c \ diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 3a3992ca..66d8f248 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T17:23:51Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:7916404f955114d22a806f1726b7a2b8cf79f56076af9ee702019143feb8e0a8", + "digest": "sha256:06d3fe3bc99ab75f650f57fef58eb547f155ddb0d4a399cd01ae941f4250aa3c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 59, - "generated_at_commit": "298065e8f6711a85823fe4ff051738e7c30cbbd1", - "source_tree": "sha256:10f6ab8e1cb41f6602ae8c3d5a7709ff36fb0d952bb63d9fe8e4a0462ead52ac" + "files": 60, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:6da7ecbb898fed9321b8b59b9ae84864c6b1b00a02cd9d6ac51d1c03c49627bb" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/infer.c b/native/kernel/core/infer.c index 8ee997de..05c67159 100644 --- a/native/kernel/core/infer.c +++ b/native/kernel/core/infer.c @@ -87,6 +87,7 @@ extern int32_t aienos_infer_session_prompt(const uint8_t *text, uint32_t len); extern int32_t aienos_infer_session_step(uint32_t *token, uint64_t *us); extern int32_t aienos_infer_session_cancel(void); extern int32_t aienos_infer_session_tokens(uint32_t *out, uint32_t cap); +extern int32_t aienos_infer_session_prompt_ids(uint32_t *out, uint32_t cap); extern int32_t aienos_infer_session_text(const uint32_t *ids, uint32_t n, uint8_t *out, uint32_t cap); extern int32_t aienos_infer_session_close(void); extern void aienos_infer_heap_stats(uint64_t *used, uint64_t *peak); @@ -277,7 +278,7 @@ static void hex(char *out, const uint8_t *b, unsigned n) * behaved (count of produced tokens as expected, no error), 0 otherwise. * `expect`/`nexpect`: the exact token list a complete reply must equal. */ static int session_request(unsigned which, const char *text, unsigned cancel_after, const uint32_t *expect, - unsigned nexpect) + unsigned nexpect, const uint32_t *expect_head) { uint64_t t0 = ck_time_us(), used = 0, peak = 0, pre_us = 0, tok_us_sum = 0; unsigned steps = 0, prefill = 0, produced = 0, last = 0; @@ -286,6 +287,17 @@ static int session_request(unsigned which, const char *text, unsigned cancel_aft ck_printf("infer: session prompt=%u REFUSED rc=%d\n", which, ids); return 0; } + /* The chat template the tokenizer built: the first four prompt ids must be the golden ones (the one-shot + * probe checked the same four, expect_ids; the Pi image skips the probe, so the session path checks them). */ + int head_ok = 1; + if (expect_head) { + uint32_t head[4] = {0, 0, 0, 0}; + int32_t hn = aienos_infer_session_prompt_ids(head, 4); + head_ok = hn >= 4; + for (unsigned i = 0; head_ok && i < 4; i++) head_ok = head[i] == expect_head[i]; + ck_printf("infer: session prompt=%u prompt_head=%u,%u,%u,%u expect=%s\n", which, head[0], head[1], head[2], head[3], + head_ok ? "match" : "MISMATCH"); + } int32_t rc = 0; for (;;) { if (cancel_after && steps == cancel_after) break; @@ -314,6 +326,7 @@ static int session_request(unsigned which, const char *text, unsigned cancel_aft which, ids, cancel_after, steps, prefill, produced, "done", (unsigned long long)(used / 1024), rc); return ok; } + ok = ok && head_ok; ok = ok && last && (unsigned)ids == prefill && produced == nexpect; for (unsigned i = 0; ok && i < nexpect; i++) ok = ok && toks[i] == expect[i]; int32_t tl = aienos_infer_session_text(toks, (uint32_t)produced, text_out, INFER_TEXT_BYTES); @@ -365,10 +378,10 @@ static void session_run(uint64_t mpa, uint64_t msize) ck_printf("infer: session refusals open_twice=%d prompt_bytes=%d step_idle=%d\n", r_open, r_bytes, r_idle); ok = ok && r_open == SESS_E_STATE && r_bytes == SESS_E_PROMPT_BYTES && r_idle == SESS_STEP_DONE; - ok = session_request(1, "Say hello.", 3, 0, 0) && ok; + ok = session_request(1, "Say hello.", 3, 0, 0, 0) && ok; ok = session_request(2, "What is the capital of France?", 0, expect_gen, - sizeof expect_gen / sizeof expect_gen[0]) && ok; - ok = session_request(3, "Say hello.", 1, 0, 0) && ok; + sizeof expect_gen / sizeof expect_gen[0], expect_ids) && ok; + ok = session_request(3, "Say hello.", 1, 0, 0, 0) && ok; } uint64_t used = 0, peak = 0; aienos_infer_heap_stats(&used, &peak); @@ -394,6 +407,10 @@ static struct { uint64_t mpa, msize, mpages, hpa, frames_before; } g_res; +static uint8_t g_model_sha[SHA256_DIGEST_SIZE]; /* digest of the ingested model, set by ck_infer_run before resident_open */ + +const uint8_t *ck_infer_model_sha256(void) { return g_res.open ? g_model_sha : 0; } + int ck_infer_resident(void) { return g_res.open; } static void resident_open(uint64_t mpa, uint64_t msize, uint64_t mpages, uint64_t hpa, uint64_t frames_before) @@ -437,6 +454,11 @@ int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), stru ck_wr(daif, daif); if (ids < 0) return ids; r->prompt_ids = (uint32_t)ids; + { + __asm__ volatile("msr daifset, #0xf" ::: "memory"); + (void)aienos_infer_session_prompt_ids(r->prompt_head, 4); + ck_wr(daif, daif); + } int32_t rc = 0; for (;;) { uint32_t t = 0; @@ -444,6 +466,7 @@ int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), stru __asm__ volatile("msr daifset, #0xf" ::: "memory"); rc = aienos_infer_session_step(&t, &us); ck_wr(daif, daif); + pet(); /* the Pi PM watchdog (15 s) is petted per step; no-op on every other board */ if (rc < 0 || rc == SESS_STEP_DONE) break; r->steps++; if (rc != SESS_STEP_PREFILL) r->produced++; @@ -631,6 +654,7 @@ void ck_infer_run(void) /* Service image: the model stays resident for the lifecycle's prompt verb * (core/lifecycle.c); the one-shot probe and session_run below are the * QEMU probe image's regression, not the service's job. */ + memcpy(g_model_sha, dg, sizeof dg); resident_open(mpa, msize, mpages, hpa, frames_before); return; #endif @@ -640,7 +664,6 @@ void ck_infer_run(void) * 15 s PM watchdog (core/pi5.c). The session below runs the same golden prompt one step at a time * with a pet per step, and is judged against the same llama.cpp token list (expect_gen). */ ck_puts("infer: one-shot probe skipped on the Pi image (watchdog); the session below runs the golden prompt step by step\n"); - (void)expect_ids; /* judged by the one-shot probe only; the session checks the reply tokens */ { uint64_t v = ck_rd(cpacr_el1); v |= 3ull << CPACR_FPEN_SHIFT; @@ -733,4 +756,5 @@ int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), stru return -12; /* no session in this image */ } void ck_infer_resident_close(void) {} +const uint8_t *ck_infer_model_sha256(void) { return 0; } #endif diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index 90d53b18..120f00ac 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -282,9 +282,11 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) * stage: its default candidate source is the boot disk Store, read by that * stage before the NVMe DMA revoke. No stage registers an IRQ. */ ck_artifact_run(); -#ifdef CK_INFER_RESIDENT +#if defined(CK_INFER_RESIDENT) && !defined(CK_PI5_AGENT) /* Resident inference session for the lifecycle (core/infer.c): model - * ingest and session open after the store stage, before the lifecycle. */ + * ingest and session open after the store stage, before the lifecycle. + * Not in the Pi agent image: its stage (svc/pi5_agent.c) opens the session itself, inside the + * store stage and only after the Store opened, and closes it before committing. */ ck_infer_run(); #endif struct ck_mm_usage mu; diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c index b4d7555e..ae75915c 100644 --- a/native/kernel/core/lifecycle.c +++ b/native/kernel/core/lifecycle.c @@ -56,6 +56,7 @@ #include "continuity_service.h" #include "cortex_journal.h" #include "slot_service.h" +#include "reply_obj.h" #if !defined(CK_SERVICE) #error "lifecycle.c is only part of a CK_SERVICE=1 image" @@ -69,7 +70,7 @@ #endif #define CK_LC_OBJ_KIND 0x4e31u /* 'N1': v1 approved note, version 1 */ -#define CK_LC_REPLY_KIND 0x5231u /* 'R1': v1 approved reply (prompt + generated text), version 1 */ +#define CK_LC_REPLY_KIND CK_REPLY_OBJ_KIND /* 0x5231 'R1': v1 approved reply (prompt + generated text), version 1 */ void ck_dev_nvme_release(void); int ck_store_service_ready(void); @@ -146,7 +147,7 @@ static void put(uint8_t *buf, size_t *n, const char *s, size_t len) #define CK_LC_REPLY_OBJ_MAX (16 + 8 + CK_LC_TASK_MAX + 8 + CK_LC_REPLY_MAX + 1) static int commit_task(void) { - static const char note_hdr[] = "aienos.note.v1\n", reply_hdr[] = "aienos.reply.v1\nprompt: ", reply_sep[] = "\nreply: "; + static const char note_hdr[] = "aienos.note.v1\n"; uint8_t buf[CK_LC_REPLY_OBJ_MAX]; size_t n = 0, i; uint16_t kind = CK_LC_OBJ_KIND; @@ -155,11 +156,7 @@ static int commit_task(void) if (!ck_cont_service_writable()) return CK_SB_E_CONTINUITY; /* degraded mount: read-only */ if (g_lc.pending == CK_LC_PENDING_REPLY) { kind = CK_LC_REPLY_KIND; - put(buf, &n, reply_hdr, sizeof reply_hdr - 1); - put(buf, &n, g_lc.task, g_lc.task_len); - put(buf, &n, reply_sep, sizeof reply_sep - 1); - put(buf, &n, g_lc.reply, g_lc.reply_len); - put(buf, &n, "\n", 1); + n = ck_reply_obj_build(buf, g_lc.task, g_lc.task_len, (const uint8_t *)g_lc.reply, g_lc.reply_len, 0, 0, 0); } else { put(buf, &n, note_hdr, sizeof note_hdr - 1); put(buf, &n, g_lc.task, g_lc.task_len); diff --git a/native/kernel/core/reply_obj.h b/native/kernel/core/reply_obj.h new file mode 100644 index 00000000..935d43c1 --- /dev/null +++ b/native/kernel/core/reply_obj.h @@ -0,0 +1,59 @@ +/* reply_obj.h -- the v1 approved-reply Store object (kind 0x5231 'R1'), shared by the service lifecycle + * (core/lifecycle.c, commit_task: QEMU rows 175, 205, 206 of GATES.md) and the Pi agent stage + * (svc/pi5_agent.c, PI5-6). One builder, so the two cannot drift. + * + * Bytes: "aienos.reply.v1\nprompt: " + prompt + "\nreply: " + reply + "\n" + * and, only when the caller passes a trailer (the Pi agent does, the lifecycle does not): + * "model: " + 64 lowercase hex + "\nids: " + decimal token ids separated by single spaces + "\n". + * With no trailer the output is byte for byte what lifecycle.c wrote before this header existed + * (dev/tests/pi5_agent_test.c pins it against a literal). */ +#ifndef AIENOS_CK_REPLY_OBJ_H +#define AIENOS_CK_REPLY_OBJ_H +#include <stddef.h> +#include <stdint.h> + +#define CK_REPLY_OBJ_KIND 0x5231u /* 'R1' */ +#define CK_REPLY_OBJ_HDR "aienos.reply.v1\nprompt: " +#define CK_REPLY_OBJ_SEP "\nreply: " +#define CK_REPLY_OBJ_MODEL "model: " +#define CK_REPLY_OBJ_IDS "\nids: " +#define CK_REPLY_OBJ_MAX_IDS 16u +/* Largest trailer: "model: " + 64 + "\nids: " + 16 ids of up to 10 digits and a space + "\n". */ +#define CK_REPLY_OBJ_TRAILER_MAX (7 + 64 + 6 + 16 * 11 + 1) + +static inline void ck_reply_put(uint8_t *buf, size_t *n, const char *s, size_t len) +{ + size_t i; + for (i = 0; i < len; i++) buf[(*n)++] = (uint8_t)s[i]; +} + +/* Builds the object into buf (the caller sized it for prompt + reply + trailer + the fixed text). + * model_hex64: NULL for no trailer. ids/nids: the token ids, only used with a trailer. Returns the + * length. */ +static inline size_t ck_reply_obj_build(uint8_t *buf, const char *prompt, size_t plen, const uint8_t *reply, + size_t rlen, const char *model_hex64, const uint32_t *ids, unsigned nids) +{ + size_t n = 0, i; + ck_reply_put(buf, &n, CK_REPLY_OBJ_HDR, sizeof CK_REPLY_OBJ_HDR - 1); + ck_reply_put(buf, &n, prompt, plen); + ck_reply_put(buf, &n, CK_REPLY_OBJ_SEP, sizeof CK_REPLY_OBJ_SEP - 1); + for (i = 0; i < rlen; i++) buf[n++] = reply[i]; + buf[n++] = '\n'; + if (model_hex64) { + unsigned k; + ck_reply_put(buf, &n, CK_REPLY_OBJ_MODEL, sizeof CK_REPLY_OBJ_MODEL - 1); + ck_reply_put(buf, &n, model_hex64, 64); + ck_reply_put(buf, &n, CK_REPLY_OBJ_IDS, sizeof CK_REPLY_OBJ_IDS - 1); + for (k = 0; k < nids && k < CK_REPLY_OBJ_MAX_IDS; k++) { + char d[10]; + unsigned nd = 0; + uint32_t v = ids[k]; + if (k) buf[n++] = ' '; + do { d[nd++] = (char)('0' + v % 10); v /= 10; } while (v); + while (nd) buf[n++] = (uint8_t)d[--nd]; + } + buf[n++] = '\n'; + } + return n; +} +#endif diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 6bf2e421..de4780d3 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T17:02:53Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:a80356e38bcc9fc135fffc058dc782bc2fc4f56869f6e06caaf0c2e169338212", + "digest": "sha256:c33332d860fe8c2c50a901891772289271c5bf0e7a442fe46dd9b881033c7966", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 38, - "generated_at_commit": "013dfa602cad3a9f4435359ccf01f9b61eecfaf5", - "source_tree": "sha256:e54dedd48e0569396d3df6353fe27cedb3fe0da29c39822fa811717d69f8968b" + "files": 39, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:0640ebbac7fc4b25989fcdc81917064756003f93f9fd2c4863a9076286850fdc" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/tests/pi5_agent_test.c b/native/kernel/dev/tests/pi5_agent_test.c new file mode 100644 index 00000000..587a3b3c --- /dev/null +++ b/native/kernel/dev/tests/pi5_agent_test.c @@ -0,0 +1,376 @@ +/* pi5_agent_test.c -- AIENOS PI5-6 final cut host test: the Pi agent stage (svc/pi5_agent.c, hooked by svc/store_boot.c + * ck_store_pi5_agent_stage, compiled with -DCK_PI5_UNLOCK -DCK_PI5_UNLOCK_CARD -DCK_PI5_AGENT exactly as "make pi5-agent" + * builds it) over the Pi card layout. A file stands in for the card; every "boot" reopens the file and starts from a cold C + * state. The inference unit is a STUB defined here (a real 770 MiB model cannot run on the host); the stage's own policy + * (order, golden checks, what gets committed, what is recovered) is the real code under test. + * + * Proves: boot 1 (right secret, model present) generates, closes the session, commits ONE sealed reply object and reads it + * back; boot 2 (same secret, no model) recovers the same reply with the same Store generation and the same object digest + * without touching the model; boot 2 with a model present still does not generate; the wrong secret recovers nothing, + * never reaches the model (zero calls) and leaves the card byte-identical; a reply that is not the golden one (wrong ids, + * wrong chat-template head, wrong text, wrong model digest, refused or cancelled prompt) commits nothing; a stored reply that + * is not golden is reported FAIL on recovery; no reply and no model is NOMODEL; a lifecycle reply (no trailer) and another + * prompt are ignored; the reply object format is the lifecycle's, byte for byte, plus the trailer. + * Mutants (Makefile-less, see stage.mk pi5-agent-mutants) must make this test FAIL. Hosted test code only. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "ck.h" +#include "ck_host.h" +#include "disk_file.h" +#include "disk_layout.h" +#include "disk_part.h" +#include "gpt_write.h" +#include "../../core/pi5_marker.h" +#include "../../core/reply_obj.h" +#include "m5.h" +#include "store_boot.h" +#include "../../core/unlock_req.h" +#include "../../svc/pi5_agent.h" + +#if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_AGENT) +#error "pi5_agent_test.c must be built with -DCK_PI5_UNLOCK -DCK_PI5_AGENT (the image's policy)" +#endif + +static int checks, failures; +#define CHECK(c) \ + do { \ + checks++; \ + if (!(c)) { \ + failures++; \ + printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ + } \ + } while (0) + +extern int ck_host_entropy_refuse; +int ck_pi5_unlock_secret(uint8_t out[CK_SB_SECRET_LEN], const char **source) +{ + (void)out; + (void)source; + return -1; +} + +/* ---- the inference unit stub (core/infer.c in the image) ---- */ +static struct { + int model; /* MODEL.MAP present and the digest matched: ck_infer_run leaves a resident session */ + int resident; + int prompt_rc; /* ck_infer_prompt result */ + int cancelled; + uint32_t ids[CK_INFER_REPLY_IDS]; + unsigned nids; + char text[64]; + uint32_t head[4]; + uint8_t sha[32]; + int n_run, n_prompt, n_close; + char last_prompt[96]; +} G; + +static const uint32_t gold_ids[8] = {791, 6864, 315, 9822, 374, 12366, 13, 128009}; +static const uint32_t gold_head[4] = {128000, 128006, 882, 128007}; + +static void stub_good(int model) +{ + memset(&G, 0, sizeof G); + G.model = model; + memcpy(G.ids, gold_ids, sizeof gold_ids); + G.nids = 8; + strcpy(G.text, CK_AGENT_GOLDEN_TEXT); + memcpy(G.head, gold_head, sizeof gold_head); + for (int i = 0; i < 32; i++) { + unsigned v; + sscanf(CK_AGENT_GOLDEN_MODEL_SHA256 + 2 * i, "%2x", &v); + G.sha[i] = (uint8_t)v; + } +} + +void ck_infer_run(void) +{ + G.n_run++; + G.resident = G.model; +} +int ck_infer_resident(void) { return G.resident; } +const uint8_t *ck_infer_model_sha256(void) { return G.resident ? G.sha : 0; } +int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), struct ck_infer_reply *r) +{ + (void)cancel_poll; + G.n_prompt++; + snprintf(G.last_prompt, sizeof G.last_prompt, "%.*s", (int)len, text); + memset(r, 0, sizeof *r); + if (G.prompt_rc) return G.prompt_rc; + memcpy(r->ids, G.ids, sizeof G.ids); + r->produced = G.nids; + r->prompt_ids = 17; + r->steps = 17 + G.nids; + r->cancelled = G.cancelled; + r->us = 1234; + r->text_len = (uint32_t)strlen(G.text); + memcpy(r->text, G.text, r->text_len); + memcpy(r->prompt_head, G.head, sizeof G.head); + return 0; +} +void ck_infer_resident_close(void) +{ + G.n_close++; + G.resident = 0; +} + +#define BLOCKS 262144ull +#define PART_FIRST 8192u +#define PART_SECS 8192u +static char g_path[256]; + +/* Optional forged object committed through the open Store before the agent stage (a tampered or foreign R1 object). */ +static const uint8_t *g_forge; +static size_t g_forge_len; + +/* One boot: open the card file, run the Store stage exactly as ck_stage_store does (unlock policy, then the agent hook), + * close the card. Output captured. */ +static int boot(const uint8_t *secret, ck_store_report *r, int *agent_rc, char **cap) +{ + disk_file f; + disk_dev d; + ck_part p, sp; + ck_gpt_info gi; + if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999; + if (ck_gpt_find_aienos(&d, &p, &gi) || ck_pi5_store_view(&p, &sp)) { disk_file_close(&f); return -9998; } + ck_host_quiet = 1; + ck_host_capture_start(); + int rc = ck_store_pi5_unlock_run(&sp.dev, secret, "pi5-agent-test", r); + if (g_forge && r->verdict == CK_SB_COMMITTED) { + ss_object o = {CK_REPLY_OBJ_KIND, 1, g_forge, g_forge_len}; + CHECK(ss_transact(store_boot_store(), &o, 1, 0, 0, 0) == 0); + } + *agent_rc = ck_store_pi5_agent_stage(r); + *cap = strdup(ck_host_capture_text()); + ck_host_capture_stop(); + ck_host_quiet = 0; + disk_file_close(&f); + return rc; +} + +static uint8_t *slurp(size_t *n) +{ + FILE *fp = fopen(g_path, "rb"); + CHECK(fp != 0); + if (!fp) { *n = 0; return 0; } + fseek(fp, 0, SEEK_END); + *n = (size_t)ftell(fp); + rewind(fp); + uint8_t *m = malloc(*n); + CHECK(m && fread(m, 1, *n, fp) == *n); + fclose(fp); + return m; +} + +/* The whole "agent: reply ..." line of a capture ("" when absent). */ +static void reply_line(const char *cap, char *out, size_t cap_out) +{ + const char *p = strstr(cap, "agent: reply "); + size_t n = 0; + out[0] = 0; + if (!p) return; + while (p[n] && p[n] != '\n' && n + 1 < cap_out) { out[n] = p[n]; n++; } + out[n] = 0; +} + +static unsigned long long num_after(const char *s, const char *after, const char *key) +{ + const char *p = strstr(s, after); + if (!p) return 0; + p = strstr(p, key); + return p ? strtoull(p + strlen(key), 0, 10) : 0; +} + +static void blank_card(void) +{ + gw_part gp[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0x5a}, {ck_aienos_part_type, PART_FIRST, PART_FIRST + PART_SECS - 1, "AIENOS", 0}}; + CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); +} + +int main(void) +{ + snprintf(g_path, sizeof g_path, "/tmp/aienos-pi5-agent-test-%d.img", (int)getpid()); + uint8_t S[CK_SB_SECRET_LEN], W[CK_SB_SECRET_LEN]; + for (int i = 0; i < 32; i++) S[i] = (uint8_t)(i * 7 + 5); + memcpy(W, S, 32); + W[31] ^= 0x80; + ck_store_report r; + char *c1, *c2, *c3, *c4; + char l1[1024], l2[1024]; + size_t n0, n1; + uint8_t *b, *a; + int arc; + + /* 0. The shared reply object: the lifecycle format byte for byte, plus the trailer only when asked. */ + { + uint8_t o[CK_REPLY_OBJ_TRAILER_MAX + 128]; + uint32_t ids[3] = {791, 0, 128009}; + size_t n = ck_reply_obj_build(o, "Q", 1, (const uint8_t *)"A\nB", 3, 0, 0, 0); + static const char lit[] = "aienos.reply.v1\nprompt: Q\nreply: A\nB\n"; + CHECK(n == sizeof lit - 1 && memcmp(o, lit, n) == 0); + n = ck_reply_obj_build(o, "Q", 1, (const uint8_t *)"A", 1, CK_AGENT_GOLDEN_MODEL_SHA256, ids, 3); + static const char lit2[] = "aienos.reply.v1\nprompt: Q\nreply: A\nmodel: " CK_AGENT_GOLDEN_MODEL_SHA256 "\nids: 791 0 128009\n"; + CHECK(n == sizeof lit2 - 1 && memcmp(o, lit2, n) == 0); + CHECK(CK_REPLY_OBJ_KIND == 0x5231u); + } + + /* 1. Boot 1 (G1): blank card, model present: generate, close, commit, read back. */ + blank_card(); + stub_good(1); + CHECK(boot(S, &r, &arc, &c1) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && arc == 0); + CHECK(G.n_run == 1 && G.n_prompt == 1 && G.n_close == 1 && !G.resident); + CHECK(strcmp(G.last_prompt, CK_AGENT_PROMPT) == 0); + CHECK(strstr(c1, "agent: generated tokens=8 prompt_ids=17")); + CHECK(strstr(c1, "prompt_head=128000,128006,882,128007")); + CHECK(strstr(c1, "ids=791 6864 315 9822 374 12366 13 128009 text=\"The capital of France is Paris.\"")); + CHECK(strstr(c1, "model_sha256=" CK_AGENT_GOLDEN_MODEL_SHA256)); + CHECK(strstr(c1, "agent: committed store_gen=")); + CHECK(strstr(c1, "AIENOS_PI5_AGENT: GENERATED PASS")); + CHECK(strstr(c1, "agent: reply store_gen=")); + unsigned long long gen1 = num_after(c1, "agent: reply", "store_gen="); + CHECK(gen1 > 0); + reply_line(c1, l1, sizeof l1); + CHECK(strlen(l1) > 100); + CHECK(strstr(l1, "object_sha256=")); + + /* 2. Boot 2 (G2): same secret, NO model: the same reply, same generation, same digest, model never touched, nothing new. */ + stub_good(0); + b = slurp(&n0); + CHECK(boot(S, &r, &arc, &c2) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0 && arc == 0); + CHECK(G.n_run == 0 && G.n_prompt == 0 && G.n_close == 0); + CHECK(strstr(c2, "AIENOS_PI5_AGENT: RECOVERED PASS")); + CHECK(!strstr(c2, "agent: generated") && !strstr(c2, "agent: committed")); + reply_line(c2, l2, sizeof l2); + CHECK(strcmp(l1, l2) == 0); /* boot 2 prints exactly boot 1's reply line, store generation and object digest included */ + a = slurp(&n1); + CHECK(n0 == n1); + free(a); + free(b); + /* 2b. Same, with the model present on the card: the Store already holds the reply, so nothing is generated. */ + stub_good(1); + CHECK(boot(S, &r, &arc, &c3) == 0 && arc == 0); + CHECK(G.n_run == 0 && G.n_prompt == 0); + reply_line(c3, l2, sizeof l2); + CHECK(strcmp(l1, l2) == 0 && strstr(c3, "RECOVERED PASS")); + free(c3); + + /* 3. Negative control (G3): the wrong secret opens nothing, recovers nothing, never reaches the model, writes nothing. */ + stub_good(1); + b = slurp(&n0); + CHECK(boot(W, &r, &arc, &c3) == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED && arc == 0); + a = slurp(&n1); + CHECK(n0 == n1 && memcmp(b, a, n0) == 0); + CHECK(G.n_run == 0 && G.n_prompt == 0 && G.n_close == 0); + CHECK(strstr(c3, "agent: NOTHING RECOVERED") && strstr(c3, "AIENOS_PI5_AGENT: NOSTORE")); + CHECK(!strstr(c3, "agent: reply") && !strstr(c3, "Paris") && !strstr(c3, "RECOVERED PASS")); + free(a); + free(b); + free(c3); + /* and the right secret afterwards recovers the same reply again */ + stub_good(0); + CHECK(boot(S, &r, &arc, &c3) == 0 && arc == 0); + reply_line(c3, l2, sizeof l2); + CHECK(strcmp(l1, l2) == 0); + free(c3); + free(c1); + free(c2); + + /* 4. Replies that are not golden commit nothing (each on a fresh card; then a no-model boot shows nothing is stored). */ + { + enum { WRONG_ID, WRONG_HEAD, WRONG_TEXT, WRONG_MODEL, PROMPT_REFUSED, CANCELLED, MODEL_ABSENT_AFTER_RUN, TOO_MANY, N_CASES }; + for (int k = 0; k < N_CASES; k++) { + blank_card(); + stub_good(1); + switch (k) { + case WRONG_ID: G.ids[3] ^= 1; break; + case WRONG_HEAD: G.head[2] = 883; break; + case WRONG_TEXT: strcpy(G.text, "The capital of France is Lyon."); break; + case WRONG_MODEL: G.sha[0] ^= 1; break; + case PROMPT_REFUSED: G.prompt_rc = -15; break; + case CANCELLED: G.cancelled = 1; break; + case MODEL_ABSENT_AFTER_RUN: G.model = 0; break; + case TOO_MANY: G.nids = 9; G.ids[8] = 128009; break; + } + CHECK(boot(S, &r, &arc, &c4) == 0 && r.verdict == CK_SB_COMMITTED && arc != 0); + CHECK(strstr(c4, "AIENOS_PI5_AGENT: GENERATED FAIL") || strstr(c4, "AIENOS_PI5_AGENT: NOMODEL")); + CHECK(!strstr(c4, "agent: committed") && !strstr(c4, "agent: reply")); + CHECK(G.n_close == (k == MODEL_ABSENT_AFTER_RUN ? 0 : 1) && !G.resident); + free(c4); + stub_good(0); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOTHING GENERATED") && strstr(c4, "AIENOS_PI5_AGENT: NOMODEL")); + CHECK(G.n_prompt == 0 && !strstr(c4, "agent: reply")); + free(c4); + } + } + + /* 5. No reply and no model: NOMODEL, nothing written, the model path was tried but not the prompt. */ + blank_card(); + stub_good(0); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && G.n_run == 1 && G.n_prompt == 0); + CHECK(strstr(c4, "AIENOS_PI5_AGENT: NOMODEL")); + free(c4); + + /* 6. Foreign R1 objects are ignored; a stored agent reply that is not golden is reported FAIL on recovery. */ + { + uint8_t o[CK_REPLY_OBJ_TRAILER_MAX + 512]; + uint32_t bad[8]; + memcpy(bad, gold_ids, sizeof bad); + blank_card(); + /* 6a. a lifecycle reply for the SAME prompt (no trailer) and an agent-format reply for ANOTHER prompt */ + g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)"Paris.", 6, 0, 0, 0); + g_forge = o; + stub_good(0); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOMODEL")); /* ignored: no agent reply yet */ + free(c4); + g_forge_len = ck_reply_obj_build(o, "Say hello.", 10, (const uint8_t *)"Hi", 2, CK_AGENT_GOLDEN_MODEL_SHA256, gold_ids, 8); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "NOMODEL")); + free(c4); + g_forge = 0; + stub_good(1); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc == 0 && strstr(c4, "GENERATED PASS")); /* still generates beside them */ + reply_line(c4, l1, sizeof l1); + free(c4); + stub_good(0); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc == 0 && strstr(c4, "RECOVERED PASS")); + reply_line(c4, l2, sizeof l2); + CHECK(strcmp(l1, l2) == 0); + free(c4); + /* 6b. a well-formed agent object whose token list is not golden (as if written by a broken generator) */ + blank_card(); + bad[2] = 316; + g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)CK_AGENT_GOLDEN_TEXT, + strlen(CK_AGENT_GOLDEN_TEXT), CK_AGENT_GOLDEN_MODEL_SHA256, bad, 8); + g_forge = o; + stub_good(1); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0); + CHECK(strstr(c4, "AIENOS_PI5_AGENT: RECOVERED FAIL") && strstr(c4, "not the golden reply")); + CHECK(G.n_run == 0 && G.n_prompt == 0); /* a bad stored reply is reported, never silently regenerated over */ + free(c4); + /* 6c. a stored reply carrying another model digest, and a text with newlines and trailer-looking lines inside */ + blank_card(); + g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), + (const uint8_t *)"The capital of France is Paris.\nids: 1 2\nmodel: x", 49, CK_AGENT_GOLDEN_MODEL_SHA256, gold_ids, 8); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "RECOVERED FAIL")); + CHECK(strstr(c4, "ids=791 6864 315 9822 374 12366 13 128009")); /* the trailer is read from the END */ + free(c4); + blank_card(); + { + char other[65]; + memcpy(other, CK_AGENT_GOLDEN_MODEL_SHA256, 65); + other[10] = other[10] == '0' ? '1' : '0'; + g_forge_len = ck_reply_obj_build(o, CK_AGENT_PROMPT, strlen(CK_AGENT_PROMPT), (const uint8_t *)CK_AGENT_GOLDEN_TEXT, + strlen(CK_AGENT_GOLDEN_TEXT), other, gold_ids, 8); + CHECK(boot(S, &r, &arc, &c4) == 0 && arc != 0 && strstr(c4, "RECOVERED FAIL")); + free(c4); + } + g_forge = 0; + } + + memset(S, 0, sizeof S); + memset(W, 0, sizeof W); + unlink(g_path); + printf("pi5_agent_test: %d checks, %d failures\n", checks, failures); + printf("CK_PI5_AGENT_HOST: %s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +} diff --git a/native/kernel/include/.crumb b/native/kernel/include/.crumb index e5121632..070d1171 100644 --- a/native/kernel/include/.crumb +++ b/native/kernel/include/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T06:35:07Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:d3e1b6aa9a623c95208e730cea917c2824682caa6518dd4433882908b5facef3", + "digest": "sha256:397a413635dc958cb318d10730d43dd0c1f8edb19222c82b824af3e70e48d451", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "93ae0323091b1f40c93fbc3a2bdfaaa48745c61a", - "source_tree": "sha256:4f93ee5ff5b711698b9dcd9e42926cee86d3288605c711dbd86dd4c7be77cc32" + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:764766be36ceaba91297b2c988b69e51c91852636c2ee2447208b30ee821436c" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index 968668c4..cfeef8bd 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -256,10 +256,14 @@ struct ck_infer_reply { uint64_t us; uint8_t text[CK_INFER_REPLY_TEXT]; uint32_t text_len; + uint32_t prompt_head[4]; /* first four prompt token ids (the chat template), filled when the prompt was accepted */ }; int ck_infer_resident(void); int ck_infer_prompt(const char *text, size_t len, int (*cancel_poll)(void), struct ck_infer_reply *r); void ck_infer_resident_close(void); +/* SHA-256 of the resident model bytes (32 bytes) after a successful ck_infer_run ingest of a boot-disk model, + * else NULL (no resident session, or an image without the unit). */ +const uint8_t *ck_infer_model_sha256(void); /* Optional Store-stage hook: signed P2 artifact candidates read from the boot * disk Store (svc/artifact_store.c) during ck_stage_store, before the NVMe diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index a498dc2d..f589a4ec 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -227,3 +227,30 @@ pi5-fact-mutants: done; exit $$fail stage-test: pi5-fact-test stage-sanitize: pi5-fact-sanitize + +# ---- AIENOS PI5-6 final cut: the Pi agent stage (dev/tests/pi5_agent_test.c) ---- +# store_boot.c is compiled with -DCK_PI5_UNLOCK -DCK_PI5_UNLOCK_CARD -DCK_PI5_AGENT, as "make pi5-agent" builds it. The inference unit +# is a stub in the test (a 770 MiB model cannot run on the host); the stage's policy is the real svc/pi5_agent.c. +PI5_AGENT_TEST_SRCS := $(filter-out %/pi5_unlock_test.c,$(PI5_UNLOCK_TEST_SRCS)) $(STAGE_DIR)/svc/pi5_agent.c $(STAGE_DIR)/dev/tests/pi5_agent_test.c +PI5_AGENT_DEPS := $(PI5_AGENT_TEST_SRCS) $(DISK_PART_TEST_DEPS) $(STAGE_DIR)/svc/pi5_agent.h $(STAGE_DIR)/core/reply_obj.h +PI5_AGENT_FLAGS := -DCK_PI5_UNLOCK=1 -DCK_PI5_UNLOCK_CARD=1 -DCK_PI5_AGENT=1 +$(STAGE_OUT)/pi5_agent_test: $(PI5_AGENT_DEPS) + @mkdir -p $(@D) + $(STAGE_CC) $(STAGE_HOST_FLAGS) $(PI5_AGENT_FLAGS) $(DISK_PART_INC) -o $@ $(PI5_AGENT_TEST_SRCS) +$(STAGE_OUT)/pi5_agent_test_san: $(PI5_AGENT_DEPS) + @mkdir -p $(@D) + $(STAGE_CC) $(STAGE_HOST_FLAGS) $(STAGE_SAN_FLAGS) $(PI5_AGENT_FLAGS) $(DISK_PART_INC) -o $@ $(PI5_AGENT_TEST_SRCS) +.PHONY: pi5-agent-test pi5-agent-sanitize pi5-agent-mutants +pi5-agent-test: $(STAGE_OUT)/pi5_agent_test + $< +pi5-agent-sanitize: $(STAGE_OUT)/pi5_agent_test_san + ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1 $< +# Mutation proof: each TEST-ONLY mutant of the agent stage must make the host test FAIL. +pi5-agent-mutants: + @mkdir -p $(STAGE_OUT); fail=0; \ + for m in NO_COMMIT RECOVER_BLIND NO_GOLDEN_CHECK NO_PROMPT_CHECK RECOVER_NO_CHECK MODEL_FIRST; do \ + $(STAGE_CC) $(STAGE_HOST_FLAGS) $(PI5_AGENT_FLAGS) -DCK_AGENT_MUTANT_$$m=1 $(DISK_PART_INC) -o $(STAGE_OUT)/pi5_agent_test_mut_$$m $(PI5_AGENT_TEST_SRCS) || exit 1; \ + if $(STAGE_OUT)/pi5_agent_test_mut_$$m >/dev/null; then echo "MUTANT SURVIVED: $$m"; fail=1; else echo "mutant killed: $$m"; fi; \ + done; exit $$fail +stage-test: pi5-agent-test +stage-sanitize: pi5-agent-sanitize diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index d980d3ee..70632bc5 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:d835471b1405ca482a41beb34ed339d21e11064a59138599ee5754d198926878", - "compiled_at": "2026-10-13T17:02:53Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:11e19f80d8332984392eda3141fe2a5b50b9032fbb4d1ace6aabf29e445cf9ab", + "digest": "sha256:0c7ae2a3793e31669cfeb9a33841d7a77d3dc91f61e6b4428b43dd33470ab929", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 45, - "generated_at_commit": "013dfa602cad3a9f4435359ccf01f9b61eecfaf5", - "source_tree": "sha256:f0980993d2cb977ee81d799f40ad44096f3a996c1a6b82a6963403724a97153e" + "files": 47, + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:65bf3020c14511bacf3565c3256daa92fedfdbb2c315a870dcb2b25d35cd67d2" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/pi5_agent.c b/native/kernel/svc/pi5_agent.c new file mode 100644 index 00000000..17196b6f --- /dev/null +++ b/native/kernel/svc/pi5_agent.c @@ -0,0 +1,313 @@ +/* pi5_agent.c -- see pi5_agent.h. */ +#include "pi5_agent.h" +#include <stddef.h> +#include "ck.h" +#include "sha256.h" +#include "../core/reply_obj.h" + +static const uint32_t golden_ids[CK_AGENT_GOLDEN_NIDS] = {791, 6864, 315, 9822, 374, 12366, 13, 128009}; +static const uint32_t golden_head[4] = {128000, 128006, 882, 128007}; + +/* header + prompt + separator + reply (<= CK_INFER_REPLY_TEXT) + terminator + model/ids trailer */ +#define CK_AGENT_OBJ_MAX (24 + 48 + 8 + CK_INFER_REPLY_TEXT + 1 + CK_REPLY_OBJ_TRAILER_MAX) +static uint8_t g_body[SS_MAX_PLAINTEXT]; + +static void hex(char *out, const uint8_t *b, unsigned n) +{ + static const char d[] = "0123456789abcdef"; + for (unsigned i = 0; i < n; i++) { + out[2 * i] = d[b[i] >> 4]; + out[2 * i + 1] = d[b[i] & 15]; + } + out[2 * n] = 0; +} + +static size_t slen(const char *s) +{ + size_t n = 0; + while (s[n]) n++; + return n; +} + +static int same(const uint8_t *a, const char *b, size_t n) +{ + for (size_t i = 0; i < n; i++) + if (a[i] != (uint8_t)b[i]) return 0; + return 1; +} + +static void show(char *out, const uint8_t *b, size_t n) +{ + for (size_t i = 0; i < n; i++) out[i] = (b[i] >= 0x20 && b[i] <= 0x7e && b[i] != '"') ? (char)b[i] : '?'; + out[n] = 0; +} + +/* A parsed agent object. text points into the body; model is 64 hex chars. */ +struct parsed { + const uint8_t *text; + size_t text_len; + char model[65]; + uint32_t ids[CK_REPLY_OBJ_MAX_IDS]; + unsigned nids; +}; + +/* Parse "<R1 header><prompt><sep><reply>\nmodel: <64 hex>\nids: <n n n>\n" from the END (the reply may + * itself contain newlines). Returns 0 on a well-formed object for the fixed prompt, else a reason code. */ +static int parse(const uint8_t *b, size_t len, struct parsed *p) +{ + const size_t hl = sizeof CK_REPLY_OBJ_HDR - 1, pl = slen(CK_AGENT_PROMPT), sl = sizeof CK_REPLY_OBJ_SEP - 1; + const size_t ml = sizeof CK_REPLY_OBJ_MODEL - 1, il = sizeof CK_REPLY_OBJ_IDS - 1; + const size_t body0 = hl + pl + sl; /* first byte of the reply text */ + size_t q, i, e; + unsigned n = 0; + if (len < body0 + 1 + ml + 64 + il + 2 || !same(b, CK_REPLY_OBJ_HDR, hl) || !same(b + hl, CK_AGENT_PROMPT, pl) || + !same(b + hl + pl, CK_REPLY_OBJ_SEP, sl)) + return 1; + if (b[len - 1] != '\n') return 2; + /* the last "\nids: " (CK_REPLY_OBJ_IDS starts with the newline) */ + for (q = len - il;; q--) { + if (same(b + q, CK_REPLY_OBJ_IDS, il)) break; + if (q == 0) return 3; + } + /* "\nmodel: " + 64 hex ends exactly at q; that newline is the reply's terminator */ + if (q < body0 + 1 + ml + 64) return 4; + if (!same(b + q - 64 - ml, CK_REPLY_OBJ_MODEL, ml) || b[q - 64 - ml - 1] != '\n') return 5; + for (e = 0; e < 64; e++) { + uint8_t c = b[q - 64 + e]; + if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return 6; + p->model[e] = (char)c; + } + p->model[64] = 0; + p->text = b + body0; + p->text_len = (q - 64 - ml - 1) - body0; + /* ids: digits separated by single spaces, ended by the final newline */ + for (i = q + il; i < len - 1;) { + uint64_t v = 0; + size_t d = 0; + while (i < len - 1 && b[i] >= '0' && b[i] <= '9') { + v = v * 10 + (b[i] - '0'); + if (v > 0xffffffffull) return 7; + i++, d++; + } + if (!d || n >= CK_REPLY_OBJ_MAX_IDS) return 8; + p->ids[n++] = (uint32_t)v; + if (i < len - 1) { + if (b[i] != ' ') return 9; + i++; + if (i >= len - 1) return 10; /* trailing space */ + } + } + p->nids = n; + return n ? 0 : 11; +} + +/* The golden checks: the exact llama.cpp token list, the golden text, the golden model digest. */ +static int golden_ok(const uint32_t *ids, unsigned nids, const uint8_t *text, size_t tlen, const char *model_hex) +{ + unsigned i; + if (nids != CK_AGENT_GOLDEN_NIDS || tlen != slen(CK_AGENT_GOLDEN_TEXT) || !same(text, CK_AGENT_GOLDEN_TEXT, tlen)) return 0; + for (i = 0; i < nids; i++) + if (ids[i] != golden_ids[i]) return 0; + for (i = 0; i < 64; i++) + if (model_hex[i] != CK_AGENT_GOLDEN_MODEL_SHA256[i]) return 0; + return 1; +} + +/* Newest R1 object of the Store that starts with the fixed prompt's header. 0: found (body, len, gen filled); + * 1: none; <0: an R1 object could not be read (do not guess: refuse). */ +static int find_reply(ss_store *s, size_t *len, uint64_t *gen) +{ + const size_t pre = (sizeof CK_REPLY_OBJ_HDR - 1) + slen(CK_AGENT_PROMPT); + int best = -1; + uint64_t best_gen = 0, best_seq = 0; + uint32_t k; + for (k = 0; k < s->nclaims; k++) { + const ss_claim *c = &s->ws->claims[k]; + static uint8_t scratch[SS_MAX_PLAINTEXT]; + struct parsed probe; + size_t l = 0; + uint16_t kind = 0; + int rc; + if (c->c.obj.object_kind != CK_REPLY_OBJ_KIND) continue; + rc = ss_read(s, c->sid, scratch, sizeof scratch, &l, &kind); + if (rc) { + ck_printf("agent: REFUSED a reply object could not be read rc=%d store_gen=%llu\n", rc, + (unsigned long long)c->c.store_generation); + return -1; + } + /* An agent reply is an R1 object for the fixed prompt WITH a well-formed model/ids trailer. A lifecycle reply + * (QEMU service image, no trailer), another prompt or a damaged trailer is not ours and is left alone. */ + if (kind != CK_REPLY_OBJ_KIND || l < pre || !same(scratch, CK_REPLY_OBJ_HDR, sizeof CK_REPLY_OBJ_HDR - 1) || + !same(scratch + sizeof CK_REPLY_OBJ_HDR - 1, CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT)) || parse(scratch, l, &probe)) + continue; + if (best < 0 || c->c.store_generation > best_gen || (c->c.store_generation == best_gen && c->c.object_sequence > best_seq)) { + best = (int)k; + best_gen = c->c.store_generation; + best_seq = c->c.object_sequence; + for (size_t i = 0; i < l; i++) g_body[i] = scratch[i]; + *len = l; + } + } + if (best < 0) return 1; + *gen = best_gen; + return 0; +} + +/* Parse, check and print the reply held in g_body (len bytes) at generation gen. */ +static int report(const char *how, size_t len, uint64_t gen, struct ck_agent_result *out) +{ + struct parsed p; + uint8_t dg[SHA256_DIGEST_SIZE]; + char oh[65], shown[CK_INFER_REPLY_TEXT + 1], idl[CK_REPLY_OBJ_MAX_IDS * 11 + 1]; + unsigned i; + size_t at = 0; + int pr = parse(g_body, len, &p); + if (pr) { + ck_printf("agent: %s FAIL the stored reply object is malformed (reason %d, %u bytes, store_gen=%llu)\n", how, pr, + (unsigned)len, (unsigned long long)gen); + return -1; + } + sha256_hash(g_body, len, dg); + hex(oh, dg, SHA256_DIGEST_SIZE); + show(shown, p.text, p.text_len > CK_INFER_REPLY_TEXT ? CK_INFER_REPLY_TEXT : p.text_len); + for (i = 0; i < p.nids; i++) { + uint32_t v = p.ids[i]; + char d[10]; + unsigned nd = 0; + if (i) idl[at++] = ' '; + do { d[nd++] = (char)('0' + v % 10); v /= 10; } while (v); + while (nd) idl[at++] = d[--nd]; + } + idl[at] = 0; + ck_printf("agent: reply store_gen=%llu object_sha256=%s model_sha256=%s tokens=%u ids=%s text=\"%s\"\n", + (unsigned long long)gen, oh, p.model, p.nids, idl, shown); + out->store_gen = gen; + for (i = 0; i < 65; i++) out->object_sha256[i] = oh[i]; +#ifdef CK_AGENT_MUTANT_RECOVER_NO_CHECK + /* TEST-ONLY mutant: a recovered reply is accepted whatever it says. */ + return 0; +#else + if (!golden_ok(p.ids, p.nids, p.text, p.text_len, p.model)) { + ck_printf("agent: %s FAIL the stored reply is not the golden reply (tokens, text or model digest differ)\n", how); + return -1; + } + return 0; +#endif +} + +static int generate(ss_store *s, struct ck_agent_result *out) +{ + static struct ck_infer_reply r; + static uint8_t obj[CK_AGENT_OBJ_MAX]; + char mh[65]; + const uint8_t *msha; + size_t n, len = 0; + uint64_t gen = 0; + uint8_t sid[1][32]; + unsigned i = 0; + int rc, ok; + (void)i; + ck_infer_run(); + if (!ck_infer_resident()) { + out->mode = CK_AGENT_MODE_NOMODEL; + ck_puts("agent: NOTHING GENERATED: this Store holds no agent reply yet and no model is resident (MODEL.MAP absent or " + "refused, or the model digest did not match); nothing written\n"); + return -1; + } + msha = ck_infer_model_sha256(); + if (!msha) { + ck_infer_resident_close(); + ck_puts("agent: REFUSED the resident model has no digest; nothing written\n"); + return -1; + } + hex(mh, msha, SHA256_DIGEST_SIZE); + rc = ck_infer_prompt(CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT), 0, &r); + ck_infer_resident_close(); /* every frame back before the commit */ + if (rc < 0 || r.cancelled) { + ck_printf("agent: REFUSED the model refused the prompt rc=%d cancelled=%d; nothing written\n", rc, r.cancelled); + return -1; + } + ck_printf("agent: generated tokens=%u prompt_ids=%u steps=%u us=%llu prompt_head=%u,%u,%u,%u\n", r.produced, r.prompt_ids, + r.steps, (unsigned long long)r.us, r.prompt_head[0], r.prompt_head[1], r.prompt_head[2], r.prompt_head[3]); + ok = r.produced <= CK_AGENT_MAX_NEW && r.produced <= CK_INFER_REPLY_IDS; +#ifdef CK_AGENT_MUTANT_NO_PROMPT_CHECK + (void)golden_head; /* TEST-ONLY mutant: the chat-template head is not checked */ +#else + for (i = 0; i < 4; i++) ok = ok && r.prompt_head[i] == golden_head[i]; +#endif +#ifndef CK_AGENT_MUTANT_NO_GOLDEN_CHECK + ok = ok && golden_ok(r.ids, r.produced, r.text, r.text_len, mh); +#endif + if (!ok) { + ck_puts("agent: REFUSED the reply is not the golden reply (prompt head, tokens, text or model digest); nothing written\n"); + return -1; + } + n = ck_reply_obj_build(obj, CK_AGENT_PROMPT, slen(CK_AGENT_PROMPT), r.text, r.text_len, mh, r.ids, r.produced); + { + ss_object o = {CK_REPLY_OBJ_KIND, 1, obj, n}; +#ifdef CK_AGENT_MUTANT_NO_COMMIT + /* TEST-ONLY mutant: skip the commit but still report success. */ + rc = 0; + (void)sid; + (void)o; +#else + rc = ss_transact(s, &o, 1, 0, 0, sid); +#endif + } + if (rc) { + ck_printf("agent: REFUSED the Store did not commit the reply rc=%d; nothing written\n", rc); + return -1; + } + /* Read it back from the sealed Store, the way a cold boot will. */ + rc = find_reply(s, &len, &gen); + if (rc) { + ck_puts("agent: FAIL the committed reply is not in the Store\n"); + return -1; + } + if (report("readback", len, gen, out)) return -1; + out->ok = 1; + ck_printf("agent: committed store_gen=%llu\n", (unsigned long long)gen); + return 0; +} + +int ck_pi5_agent_run(ss_store *s, struct ck_agent_result *out) +{ + size_t len = 0; + uint64_t gen = 0; + int rc; + *out = (struct ck_agent_result){0}; +#ifdef CK_AGENT_MUTANT_MODEL_FIRST + ck_infer_run(); /* TEST-ONLY mutant: the model is read and hashed before the Store is checked */ +#endif + if (!s) { + out->mode = CK_AGENT_MODE_NOSTORE; + ck_puts("agent: NOTHING RECOVERED: the Store is not open (recovery secret missing or rejected, or the disk was " + "refused); the model was not read or hashed, nothing was generated, nothing was written\n"); + ck_puts("AIENOS_PI5_AGENT: NOSTORE\n"); + return 0; + } +#ifdef CK_AGENT_MUTANT_RECOVER_BLIND + rc = 1; /* TEST-ONLY mutant: recovery never looks at the Store */ +#else + rc = find_reply(s, &len, &gen); +#endif + if (rc < 0) { + ck_puts("AIENOS_PI5_AGENT: FAIL\n"); + return -1; + } + if (rc == 0) { + out->mode = CK_AGENT_MODE_RECOVER; + rc = report("recovered", len, gen, out); + out->ok = rc == 0; + ck_printf("AIENOS_PI5_AGENT: RECOVERED %s\n", rc ? "FAIL" : "PASS"); + return rc; + } + out->mode = CK_AGENT_MODE_GENERATE; + rc = generate(s, out); + if (out->mode == CK_AGENT_MODE_NOMODEL) { + ck_puts("AIENOS_PI5_AGENT: NOMODEL\n"); + } else { + ck_printf("AIENOS_PI5_AGENT: GENERATED %s\n", rc ? "FAIL" : "PASS"); + } + return rc; +} diff --git a/native/kernel/svc/pi5_agent.h b/native/kernel/svc/pi5_agent.h new file mode 100644 index 00000000..795e8d33 --- /dev/null +++ b/native/kernel/svc/pi5_agent.h @@ -0,0 +1,53 @@ +/* pi5_agent.h -- the Pi 5 agent stage (PI5-6 final cut, CK_PI5_AGENT, QUALIFICATION ONLY while the recovery + * secret comes from the card request file). + * + * One fixed prompt, one bounded reply (at most CK_AGENT_MAX_NEW tokens), persisted in the sealed Store and + * recovered by a later boot. Runs right after the Store stage opened the Store through the Slot 1 recovery + * secret (PI5-4), BEFORE the model is touched: + * - Store not open (wrong secret): "NOTHING RECOVERED", no hashing, no session, nothing written. + * - Store holds an agent reply for the fixed prompt: RECOVER. The model is not used, even if MODEL.MAP is on + * the card. The object is read back through the sealed Store, parsed and checked against the golden + * tokens, the golden text and the golden model digest, and printed with the Store generation it was + * committed at. Writes nothing. + * - No reply yet: GENERATE. The resident model is ingested and hashed (core/infer.c ck_infer_run, the same + * path as the QEMU service image), one bounded prompt runs (ck_infer_prompt), the session is closed, and + * only a reply that passes every golden check is committed as one sealed Store object of the lifecycle's + * reply kind 'R1' (core/reply_obj.h, shared with core/lifecycle.c) plus a model/ids trailer. + * Both ways end with the same line: + * agent: reply store_gen=G object_sha256=H model_sha256=M tokens=N ids=... text="..." + * so a boot-2 log can be compared with the boot-1 log line for line. */ +#ifndef AIENOS_CK_PI5_AGENT_H +#define AIENOS_CK_PI5_AGENT_H +#include <stdint.h> +#include "store_sealed.h" + +#define CK_AGENT_PROMPT "What is the capital of France?" +#define CK_AGENT_MAX_NEW 16u /* the reply cap N (also the session's max_new in core/infer.c SESS_MAX_NEW) */ +/* Golden values: llama.cpp's greedy reply for CK_AGENT_PROMPT on Llama-3.2-1B-Instruct-Q4_K_M + * (crates/aienos-infer/tests/fixtures/ref_fr.txt, PROVENANCE.md) and its chat-template head. */ +#define CK_AGENT_GOLDEN_NIDS 8u +#define CK_AGENT_GOLDEN_TEXT "The capital of France is Paris." +#define CK_AGENT_GOLDEN_MODEL_SHA256 "3f5a22426976ab26cfe84dba63c1d08391717abb1af893e10f1b2968d862dcc1" + +enum { + CK_AGENT_MODE_NOSTORE = 1, /* Store not open: nothing recovered, nothing generated */ + CK_AGENT_MODE_RECOVER = 2, + CK_AGENT_MODE_GENERATE = 3, + CK_AGENT_MODE_NOMODEL = 4, /* no reply yet and no resident model: nothing written */ +}; + +struct ck_agent_result { + int mode; /* CK_AGENT_MODE_* */ + int ok; /* 1: the reply was recovered/committed and passed every check */ + uint64_t store_gen; /* Store generation the reply object was committed at */ + char object_sha256[65]; +}; + +/* The inference unit entry points the stage uses (core/infer.c in the image; a stub in the host test). */ +void ck_infer_run(void); + +/* s: the open writable Store, or NULL when it is not open. Returns 0 when the stage did what its mode says + * and every check passed (NOSTORE counts: it is a refusal, not a failure of this stage); <0 otherwise + * (printed). */ +int ck_pi5_agent_run(ss_store *s, struct ck_agent_result *out); +#endif diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index c204fcf9..b02d618b 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -12,6 +12,9 @@ #ifdef CK_PI5_FACT #include "pi5_fact.h" #endif +#ifdef CK_PI5_AGENT +#include "pi5_agent.h" +#endif #ifdef CK_SERVICE #include "continuity_service.h" #endif @@ -751,6 +754,17 @@ int ck_store_pi5_fact_stage(const ck_store_report *r) } #endif +#ifdef CK_PI5_AGENT +#if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_UNLOCK_CARD) +#error "CK_PI5_AGENT needs CK_PI5_UNLOCK and CK_PI5_UNLOCK_CARD (production-class Store, card request source)" +#endif +int ck_store_pi5_agent_stage(const ck_store_report *r) +{ + struct ck_agent_result ar; + return ck_pi5_agent_run(r->verdict == CK_SB_COMMITTED ? g_ss : 0, &ar); +} +#endif + /* Kernel stage. ck_dev_boot_disk comes from dev/devices.c. */ const disk_dev *ck_dev_boot_disk(void); void ck_dev_nvme_release(void); @@ -857,6 +871,12 @@ int ck_stage_store(void) if (frc && rc == 0) rc = frc; } #endif +#ifdef CK_PI5_AGENT + { + int arc = ck_store_pi5_agent_stage(&r); + if (arc && rc == 0) rc = arc; + } +#endif #ifdef CK_SERVICE /* Service lifetime (core/lifecycle.c): an open Store keeps the NVMe bound * until the lifecycle stop order releases it, in that order. A refused diff --git a/native/kernel/svc/store_boot.h b/native/kernel/svc/store_boot.h index cec4c699..5f7c5e0c 100644 --- a/native/kernel/svc/store_boot.h +++ b/native/kernel/svc/store_boot.h @@ -165,6 +165,12 @@ int ck_store_pi5_unlock_run(const disk_dev *d, const uint8_t *secret, const char * Store is passed on only when it committed; a refused Store (wrong secret) recalls nothing. */ int ck_store_pi5_fact_stage(const ck_store_report *r); #endif +#ifdef CK_PI5_AGENT +/* PI5-6 (QUALIFICATION ONLY): the agent stage after the Store stage: recover the persisted reply, or generate and commit it. + * Same rule as the fact stage: the Store is passed on only when it committed, so a refused Store (wrong secret) never reaches the + * model. */ +int ck_store_pi5_agent_stage(const ck_store_report *r); +#endif #endif /* Print the report lines (also used by the host test). */ void store_boot_print(const ck_store_report *r); diff --git a/scripts/.crumb b/scripts/.crumb index c3fa8860..06a260f1 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T17:02:53Z", + "compiled_at": "2026-10-13T18:08:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:f94d1bca3f533e19fd6fee07f9e931ba85bacec7633de4f456534426c4a31bb4", + "digest": "sha256:553278293606dd1b57c8dbdb1167c88ae7b877912a26b060fd1cd62acc4e52b7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 95, - "generated_at_commit": "013dfa602cad3a9f4435359ccf01f9b61eecfaf5", - "source_tree": "sha256:408809dcf2be375e4a0d6c1f6d304b5653a5b3a924f353b9e86ace197b583d56" + "generated_at_commit": "bbf44285da08d70877d4b5d35ec805544bf70f3d", + "source_tree": "sha256:46951265edb12409baf251e8710925a4deff47e420fefc63859dd17a2685fa26" }, "owner_lane": "", "provenance": { diff --git a/scripts/ck_pi5_build_check.sh b/scripts/ck_pi5_build_check.sh index 0801ccdd..1b6c3d81 100755 --- a/scripts/ck_pi5_build_check.sh +++ b/scripts/ck_pi5_build_check.sh @@ -138,4 +138,39 @@ for combo in CK_PI5_FACT=1; do done done test -s "${root}/target/native-kernel-pi5-unlock-card-fact/full-pi5-unlock-card-fact/BOOTAA64.EFI" || { echo "FAIL no pi5-fact BOOTAA64.EFI"; fail=1; } +# PI5-6 final cut: the agent stage (Store plus resident inference plus the persisted reply) exists only in the pi5-agent image. +make -C "${root}/native/kernel" pi5-agent >/dev/null +ua="${root}/target/native-kernel-pi5-unlock-card-agent/full-pi5-unlock-card-agent/aienos-ck.elf" +for s in ck_pi5_agent_run ck_store_pi5_agent_stage ck_infer_prompt ck_infer_resident_close ck_infer_model_sha256 ck_store_pi5_unlock_run ck_boot_unlock_req_find; do + if defined "${ua}" "${s}"; then echo "PASS pi5-agent image defines ${s}"; else echo "FAIL pi5-agent image lacks ${s}"; fail=1; fi +done +for s in ck_pi5_agent_run ck_store_pi5_agent_stage; do + for other in "${uf}" "${uc}" "${uo}" "${ps}" "${pi5}" "${pinf}" "${def}"; do + if defined "${other}" "${s}"; then echo "FAIL ${other} defines ${s}"; fail=1; fi + done +done +echo "PASS no other image (pi5-fact, pi5-unlock-card, pi5-unlock, pi5-store, pi5, pi5-infer, default) defines the agent stage" +for s in ck_store_test_keys ck_store_test_uuid ck_pi5_fact_run ck_boot_fact_req_find fw_dma_read fwcfg_from_dsdt; do + if defined "${ua}" "${s}"; then echo "FAIL pi5-agent image defines ${s}"; fail=1; else echo "PASS pi5-agent image has no ${s}"; fi +done +ua_strings="$(strings -a "${ua}")" +if grep -q "AIEN-TEST-BOOT\|LANE18-TEST-KVOL\|SEED-0B-" <<<"${ua_strings}"; then echo "FAIL pi5-agent image carries a TEST label"; fail=1; else echo "PASS pi5-agent image carries no TEST label (production-class Store)"; fi +if grep -q "infer_source_mode: boot-disk model" <<<"${ua_strings}" && ! grep -q "opt/aienos/model\|TEST-ONLY QEMU fw_cfg" <<<"${ua_strings}"; then echo "PASS pi5-agent image reads the model from the boot disk only (no fw_cfg source)"; else echo "FAIL pi5-agent image model source"; fail=1; fi +if grep -q "QUALIFICATION ONLY, throwaway secret" <<<"${ua_strings}"; then echo "PASS the loud unlock_source label is in the pi5-agent image"; else echo "FAIL pi5-agent image lacks the unlock_source label"; fail=1; fi +# the positive control: the flag set the image is built with is accepted (a dry run), so the refusals below are the flag under test. +if make -n -C "${root}/native/kernel" CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=/nonexistent/lib.a full >/dev/null 2>&1; then echo "PASS Makefile accepts CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=<lib>"; else echo "FAIL Makefile refuses the pi5-agent flag set"; fail=1; fi +for combo in "CK_PI5_AGENT=1" "CK_PI5_UNLOCK=1 CK_PI5_AGENT=1 CK_INFER_LIB=/x/l.a" "CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1" "CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=2 CK_INFER_LIB=/x/l.a" "CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=/x/l.a"; do + # shellcheck disable=SC2086 + if make -n -C "${root}/native/kernel" ${combo} full >/dev/null 2>&1; then echo "FAIL Makefile accepts ${combo}"; fail=1; else echo "PASS Makefile refuses ${combo}"; fi +done +for other in CK_PI5_STORE=1 CK_PI5_INFER=1 CK_PI5_PCIE=1 CK_PI5_FACT=1 CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_TEST_FWCFG_ARTIFACTS=1; do + # shellcheck disable=SC2086 + if make -n -C "${root}/native/kernel" ${other} CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_PI5_AGENT=1 CK_INFER_LIB=/x/l.a full >/dev/null 2>&1; then echo "FAIL Makefile accepts ${other} with CK_PI5_AGENT=1"; fail=1; else echo "PASS Makefile refuses ${other} with CK_PI5_AGENT=1"; fi +done +# the other direction: the older Pi tiers refuse the agent flag too. +for tier in CK_PI5_INFER=1 CK_PI5_PCIE=1 CK_PI5_FACT=1; do + # shellcheck disable=SC2086 + if make -n -C "${root}/native/kernel" ${tier} CK_PI5_AGENT=1 CK_PI5_UNLOCK=1 CK_PI5_UNLOCK_CARD=1 CK_INFER_LIB=/x/l.a full >/dev/null 2>&1; then echo "FAIL Makefile accepts ${tier} with CK_PI5_AGENT=1"; fail=1; fi +done +test -s "${root}/target/native-kernel-pi5-unlock-card-agent/full-pi5-unlock-card-agent/BOOTAA64.EFI" || { echo "FAIL no pi5-agent BOOTAA64.EFI"; fail=1; } if [[ ${fail} == 0 ]]; then echo "CK_PI5_BUILD: PASS"; else echo "CK_PI5_BUILD: FAIL"; exit 1; fi diff --git a/scripts/qemu_ck_infer_test.sh b/scripts/qemu_ck_infer_test.sh index 720cfc01..07a26b96 100755 --- a/scripts/qemu_ck_infer_test.sh +++ b/scripts/qemu_ck_infer_test.sh @@ -216,6 +216,18 @@ infer_check_log() { || bad "session reply: ids $(g 1), steps $(g 2), prefill $(g 3), produced $(g 4), last $(g 5), mean_tok_us $(g 7)" grep -qF -- "${want_text}" <<<"$(g 9)" && ok "session reply text contains '${want_text}': $(g 9)" || bad "session reply text lacks '${want_text}': $(g 9)" fi + # PI5-6 agent: the chat template the tokenizer built is checked in the session path too (the first four prompt ids + # of the golden prompt; the one-shot probe checks the same four, and the Pi image has no one-shot probe). + local re_sh='^infer: session prompt=2 prompt_head=([0-9]+),([0-9]+),([0-9]+),([0-9]+) expect=(match|MISMATCH)$' + if [[ "$(grep -cE "${re_sh}" "${f}" || true)" != 1 ]]; then + bad "exactly one well-formed infer: session prompt=2 prompt_head line" + else + line="$(grep -E "${re_sh}" "${f}")"; [[ "${line}" =~ ${re_sh} ]]; m=("${BASH_REMATCH[@]}") + local want_head; want_head="$(awk '{print $1,$2,$3,$4}' <<<"${want_ids}")" + [[ "$(g 1) $(g 2) $(g 3) $(g 4)" == "${want_head}" && "$(g 5)" == match ]] \ + && ok "session prompt head $(g 1),$(g 2),$(g 3),$(g 4) = the golden chat-template head" \ + || bad "session prompt head $(g 1),$(g 2),$(g 3),$(g 4) (${want_head} expected), kernel said $(g 5)" + fi if [[ "$(grep -cE "${re_sx}" "${f}" || true)" != 1 ]]; then bad "exactly one well-formed infer: session close line" else @@ -254,7 +266,7 @@ if [[ "${1:-}" == --self-test ]]; then [[ "${gen}" == "791 6864 315 9822 374 12366 13 128009" ]] \ && echo "PASS reference step tokens parsed (${gen})" || { echo "FAIL reference step tokens '${gen}'"; echo "AIENOS_CK_INFER_SELF_TEST: FAIL"; exit 1; } sz=807694368 - good() { printf 'kernel: alive\ninfer: fw_cfg base=0x9020000 dma=yes file=opt/aienos/model size=%s\ninfer: ingest bytes=%s dma_us=42340\ninfer: sha256=%s sha_us=8568801\ninfer: probe rc=0 tensors=147 vocab=128256 ids=128000,128006,882,128007 prompt_len=17 layers=16 heap_peak_kib=24695 probe_us=1116277\ninfer: prefill_us=400000000 prompt_len=17\ninfer: tokens=791 6864 315 9822 374 12366 13 128009\ninfer: tok_us=23000000,23000000,23000000,23000000,23000000,23000000,23000000\ninfer: decode_tokens=7 mean_tok_us=23000000\ninfer: text=The capital of France is Paris.\nAIENOS_CK_INFER: PASS\ninfer: session open rc=0 tensors=147 vocab=128256 layers=16 kv_capacity=64 max_new=16 max_prompt_bytes=256 heap_reserve_kib=64 heap_used_kib=20000 open_us=1000000\ninfer: session refusals open_twice=-12 prompt_bytes=-16 step_idle=0\ninfer: session prompt=1 ids=14 cancel_after_steps=3 steps=3 prefill=3 produced=0 idle_step=done heap_used_kib=20010 rc=1\ninfer: session prompt=2 ids=17 steps=25 prefill=17 produced=8 last=yes prefill_us=400000000 mean_tok_us=23000000 tokens=791 6864 315 9822 374 12366 13 128009 text=The capital of France is Paris. heap_used_kib=20020 request_us=600000000 rc=3\ninfer: session prompt=3 ids=14 cancel_after_steps=1 steps=1 prefill=1 produced=0 idle_step=done heap_used_kib=20030 rc=1\ninfer: session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000 rc=0\nAIENOS_CK_INFER_SESSION: PASS\ninfer: unloaded heap_live_kib=1234 frames_free_before=900000 frames_free_after=900000 frames_returned=262705 rc=0,0\nreport_kind: final\n' "${sz}" "${sz}" "${pinned_sha}"; } + good() { printf 'kernel: alive\ninfer: fw_cfg base=0x9020000 dma=yes file=opt/aienos/model size=%s\ninfer: ingest bytes=%s dma_us=42340\ninfer: sha256=%s sha_us=8568801\ninfer: probe rc=0 tensors=147 vocab=128256 ids=128000,128006,882,128007 prompt_len=17 layers=16 heap_peak_kib=24695 probe_us=1116277\ninfer: prefill_us=400000000 prompt_len=17\ninfer: tokens=791 6864 315 9822 374 12366 13 128009\ninfer: tok_us=23000000,23000000,23000000,23000000,23000000,23000000,23000000\ninfer: decode_tokens=7 mean_tok_us=23000000\ninfer: text=The capital of France is Paris.\nAIENOS_CK_INFER: PASS\ninfer: session open rc=0 tensors=147 vocab=128256 layers=16 kv_capacity=64 max_new=16 max_prompt_bytes=256 heap_reserve_kib=64 heap_used_kib=20000 open_us=1000000\ninfer: session refusals open_twice=-12 prompt_bytes=-16 step_idle=0\ninfer: session prompt=1 ids=14 cancel_after_steps=3 steps=3 prefill=3 produced=0 idle_step=done heap_used_kib=20010 rc=1\ninfer: session prompt=2 prompt_head=128000,128006,882,128007 expect=match\ninfer: session prompt=2 ids=17 steps=25 prefill=17 produced=8 last=yes prefill_us=400000000 mean_tok_us=23000000 tokens=791 6864 315 9822 374 12366 13 128009 text=The capital of France is Paris. heap_used_kib=20020 request_us=600000000 rc=3\ninfer: session prompt=3 ids=14 cancel_after_steps=1 steps=1 prefill=1 produced=0 idle_step=done heap_used_kib=20030 rc=1\ninfer: session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000 rc=0\nAIENOS_CK_INFER_SESSION: PASS\ninfer: unloaded heap_live_kib=1234 frames_free_before=900000 frames_free_after=900000 frames_returned=262705 rc=0,0\nreport_kind: final\n' "${sz}" "${sz}" "${pinned_sha}"; } st=0 chk() { infer_failed=0; out="$(infer_check_log "$3" "${sz}" "${pinned_sha}" "${ids}" 1 "${gen}")"; grep -q '^FAIL' <<<"${out}" && infer_failed=1 if [[ "$2" == pass && ${infer_failed} == 0 ]] || [[ "$2" == fail && ${infer_failed} == 1 && -n "$4" ]] && { [[ "$2" == pass ]] || grep -q "^FAIL.*$4" <<<"${out}"; }; then echo "PASS self-test $1"; else echo "FAIL self-test $1"; st=1; fi; } @@ -307,6 +319,9 @@ if [[ "${1:-}" == --self-test ]]; then sed 's/session prompt=2 ids=17 steps=25 prefill=17 produced=8 last=yes/session prompt=2 ids=17 steps=24 prefill=17 produced=7 last=no/' "${tmp}/g" >"${tmp}/s8"; chk session-reply-unfinished fail "${tmp}/s8" "session reply" sed 's/128009 text=The capital/128010 text=The capital/' "${tmp}/g" >"${tmp}/s9"; chk session-wrong-token fail "${tmp}/s9" "session reply ids" sed 's/Paris\. heap_used_kib/Lyon. heap_used_kib/' "${tmp}/g" >"${tmp}/s10"; chk session-missing-paris fail "${tmp}/s10" "session reply text lacks" + sed 's/prompt_head=128000,128006,882,128007 expect=match/prompt_head=128000,128006,883,128007 expect=match/' "${tmp}/g" >"${tmp}/s15"; chk session-wrong-prompt-head fail "${tmp}/s15" "session prompt head" + sed 's/expect=match/expect=MISMATCH/' "${tmp}/g" >"${tmp}/s16"; chk session-head-mismatch-flag fail "${tmp}/s16" "session prompt head" + grep -v 'session prompt=2 prompt_head=' "${tmp}/g" >"${tmp}/s17"; chk session-no-head-line fail "${tmp}/s17" "prompt_head line" sed 's/session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=900000/session close rc=0 heap_used_kib=20030 heap_peak_kib=24695 frames_free_before=900000 frames_free_after=899999/' "${tmp}/g" >"${tmp}/s11"; chk session-frames-leaked fail "${tmp}/s11" "session close" sed 's/session close rc=0/session close rc=-12/' "${tmp}/g" >"${tmp}/s12"; chk session-close-refused fail "${tmp}/s12" "session close" { grep -v '^AIENOS_CK_INFER: PASS' "${tmp}/g"; } >"${tmp}/s13"; chk no-oneshot-verdict-still fail "${tmp}/s13" "kernel did not report AIENOS_CK_INFER: PASS"