aien-dev has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.
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#259 (code only)
Apply it
git checkout -b patch-7120edddfc410ff7 fbbfc9c90b5609cdd94a327fad3503366e1b11b1 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/7120edddfc410ff7.patch | git am
Provenance
github-import 1
aien-dev/aienux
259
cd4b0892768def4be6417075f0443e78745a6c28
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":259,"url":"https://github.com/aien-dev/aienux/pull/259","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:04Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 154fef1d..ee51eba3 100644 --- a/.crumb +++ b/.crumb @@ -22,38 +22,38 @@ "children": [ { "digest": "01210bd71bb028e239a12e613785e4a897a1dbf78c92ea2aa756a4e5e1aee1b5", - "last_commit": "79456d0", + "last_commit": "fbbfc9c", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "dadeade54c0150c968676ef8d07425d6bd80aba8bfcda7c6e651a911aa7775d3", - "last_commit": "46f22fe", + "digest": "cd17a1a9f319bb252beac32f1f685f0c013ff1d22f7cff0cf338718610ff003c", + "last_commit": "fbbfc9c", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "26b2b9ef6aa2558a23af185081b97d8c20682dff0bd3027b673716db0735a4bf", - "last_commit": "46f22fe", + "digest": "e6f06b76c4d9c1884afb930cfbb1b72266981bbe588b75705b1211ed150cc0d9", + "last_commit": "fbbfc9c", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { "digest": "c2faf9bb1f39cfe42057ef4fc740d04cc8c614bd4e31917f41b5cbe1acb8fcb6", - "last_commit": "46f22fe", + "last_commit": "fbbfc9c", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:4656658f00ea0688f18513e5621e8dc091ef984eebd57677d764fef7418a5ae4", - "compiled_at": "2026-10-04T20:01:05Z", + "children_root": "sha256:cbde8aefbebbd9837642fc925d153f80f184de79b51d695cb9167b2ada04fd50", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:2774cc061230d03bdb35b8b75f2e9f2954c7a8be4ecd3b014a3f4d1ac25d65e0", + "digest": "sha256:5b207342462caa8ff93fa5de37b3f5fb36e37ab1060cf766200360177fa89a98", "evidence_files": 143, "evidence_root": "sha256:3a3cbeef2e097d532b986e4579accee3f65ae5815e78716aefe3fd5f569812cf", - "files": 2841, - "generated_at_commit": "79456d09c1e2aaf0c84f853f0e3d76b864475bb7", - "source_tree": "sha256:3380ba3eb06746169747754aa27952438fc7f5b0b31226301b2ec011c23c1d09" + "files": 2845, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:6f3b279cb0739d8bfbeabb79c3ea1314fcd4435ecc01a1052812ae2c4a387431" }, "owner_lane": "", "provenance": { diff --git a/CONTEXT.md b/CONTEXT.md index 17f879b3..ababc4a9 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -11,7 +11,7 @@ The sovereign human user who owns the hardware, sets ultimate policy, and holds _Avoid_: User, admin, root, consumer **AIEN Agent**: -The persistent logical intelligence entity that acts as the primary interface, planner, and coordinator for the machine across reboots and hardware changes. +The continuous logical intelligence organism that acts as the primary interface, planner, and coordinator for the machine across reboots and hardware changes. Its durable subject (the same agent identity, standing intents and memory lineage, resolved from the Store after any restart) is named ALLEN ([ARCH-0035](https://github.com/aien-dev/aien-architecture/blob/main/docs/adr/0035-persistent-cognitive-entity-boundary-allen.md), PROPOSED; [ADR 0018](docs/adr/0018-allen-subject-state-object.md)). _Avoid_: Assistant, bot, chatbot, process **Logical Agent Identity**: diff --git a/docs/.crumb b/docs/.crumb index d82ff49d..c12d6428 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -17,27 +17,27 @@ "generated": { "children": [ { - "digest": "cfbb5cf8737f3e74c173b9a95aafaff4cb121d6a352de86f1b322cf0891b76c9", - "last_commit": "e3d3a10", + "digest": "9c529d8484dc3636be937fab05cb2729e86864926a4c3be1ae9d6fc8c92755d6", + "last_commit": "fbbfc9c", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, { "digest": "e9e804e088a0b2316d035b957c09289bf9f6176eece959d43c40c6e66005fe5e", - "last_commit": "7e15a57", + "last_commit": "fbbfc9c", "name": "crumbs", "purpose": "Directory docs/crumbs. Purpose not yet described by a human." } ], - "children_root": "sha256:55d44b8c5361b8f2452ed7fab6a4b51a8bb1f92763f67f1273dd9be45f8cfde0", - "compiled_at": "2026-10-04T16:47:58Z", + "children_root": "sha256:cb92ed84c76d44749e07a32e2f5a5059d9ae47b0c7333dc3503c9f6e70d8c437", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:dadeade54c0150c968676ef8d07425d6bd80aba8bfcda7c6e651a911aa7775d3", + "digest": "sha256:cd17a1a9f319bb252beac32f1f685f0c013ff1d22f7cff0cf338718610ff003c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 56, - "generated_at_commit": "2e44c0bba93bcc605dd45570ee8cfb8d56591cc3", - "source_tree": "sha256:7f40b47e69ed9e07cad80117e9c38548253fe5ce1e5420403904b8ba062cd641" + "files": 57, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:274652c9b0eb78d0001ff38d07192ce14e3c0be9c24657ad99963670cb4dc3f8" }, "owner_lane": "", "provenance": { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index ba9b0795..0ad2a017 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -124,6 +124,6 @@ Milestone reports show what was built, what passed, what failed, what changed, t ## 10. The persistent thing -The persistent thing is AIEN itself. Hardware is replaceable beneath it. Models, interfaces, tools, and Machines are replaceable around it. If a company disappears tomorrow, the long-term target is that AIENOS continues to boot and remains the operator's. +The persistent thing is the AIEN organism, and inside it the durable subject the organism sustains, named ALLEN ([ARCH-0035](https://github.com/aien-dev/aien-architecture/blob/main/docs/adr/0035-persistent-cognitive-entity-boundary-allen.md), PROPOSED; [ADR 0018](adr/0018-allen-subject-state-object.md)). AIEN is continuous: its organism keeps running and its cognitive faculty keeps proposing. ALLEN is durable: the same agent identity, standing intents and memory lineage are resolved from the Store after any restart. Hardware is replaceable beneath both. Models, interfaces, tools, and Machines are replaceable around them. If a company disappears tomorrow, the long-term target is that AIENOS continues to boot and remains the operator's. **Strengthened ([Continuous-Existence Amendment §21](CONTINUOUS_EXISTENCE_AMENDMENT.md), [ADR 0007](adr/0007-continuous-existence-provisioning-once.md)):** AIEN is the persistent logical entity. Hardware, kernels, models, runtime processes, inference caches, interfaces, and Machines are replaceable execution substrates. Sleep changes residency. Reboot changes execution substrate. Failure triggers reconstruction. None of these events inherently terminates logical identity. diff --git a/docs/adr/.crumb b/docs/adr/.crumb index 00b0bd76..df986b14 100644 --- a/docs/adr/.crumb +++ b/docs/adr/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:cfbb5cf8737f3e74c173b9a95aafaff4cb121d6a352de86f1b322cf0891b76c9", + "digest": "sha256:9c529d8484dc3636be937fab05cb2729e86864926a4c3be1ae9d6fc8c92755d6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 20, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:0bb25ead0bb799e5829e2efa5922cfbe534147990db226c8c5d1971acb1b13b0" + "files": 21, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:801465156554cf3a979affcc8c0a6fe410234235e252ed5ffd39a5d5879bb240" }, "owner_lane": "", "provenance": { diff --git a/docs/adr/0018-allen-subject-state-object.md b/docs/adr/0018-allen-subject-state-object.md new file mode 100644 index 00000000..fe765c16 --- /dev/null +++ b/docs/adr/0018-allen-subject-state-object.md @@ -0,0 +1,98 @@ +# ADR 0018: ALLEN Subject State Object (Continuity Kind 24) + +Status: **Proposed.** Companion to [ARCH-0035](https://github.com/aien-dev/aien-architecture/blob/main/docs/adr/0035-persistent-cognitive-entity-boundary-allen.md) (Persistent Cognitive Entity Boundary, PROPOSED). Format freeze needs operator approval, as for [ADR 0016](0016-continuity-objects-over-store-v1.md). +Related: [ADR 0007](0007-continuous-existence-provisioning-once.md) (Provisioning Once), [ADR 0015](0015-system-store-v1-format.md) (System Store v1), [ADR 0016](0016-continuity-objects-over-store-v1.md) (Continuity Objects), [ADR 0017](0017-m5-key-hierarchy-and-encrypted-store.md) (Sealed Store), omega ARCH-0022 (Cortex), omega R16 (orchestrator retirement). + +--- + +## 1. Context + +The ALLEN investigation (aien-architecture, 2026-10-04) found that AIENOS already keeps the organism's identity across restarts (AgentRoot, kind 16; ContinuityManifest, kind 17; branch table, kind 18), that omega's Cortex keeps its memory, and that the resident World keeps its cognition, but that **nothing durable says what the subject currently intends**. A standing goal lives only in a World object; a restart loses it even though the Cortex journal recorded it (the negative control in omega `tests/allen/run.sh` shows a restarted World with 0 goals and a journal with 15 records). + +The word "AIEN" was carrying four meanings. ARCH-0035 keeps two (the organism; the cognitive faculty) and names the third, the durable subject inside the organism, **ALLEN**. This ADR is the AIENOS half of that boundary: the smallest durable, typed, versioned state object for the subject, over the persistence mechanism AIENOS already has. + +What was rejected before this design (recorded in the investigation delta note): + +- *Payload of kind 18.* Kind 18 is the branch table (`cc_state`), already taken; a subject record inside it would couple intent to branch bookkeeping. +- *A slot in the ContinuityManifest.* The manifest (kind 17) has no free slot and its format is frozen pending operator approval; a new field would bump its version for every Store. +- *A new `AllenId`.* The subject is the agent the AgentRoot already names. A second identity would have to be kept in step with the first and would let the two drift. The subject object **carries** the LogicalAgentId and the AgentRoot id; it never mints one. +- *Keeping the standing goal in Cortex.* Cortex is memory: an append-only record of what happened. A standing intent is a claim about the present, resolved by the kernel, not a recollection replayed by a faculty. + +## 2. Decision + +### 2.1 One new continuity kind + +Kind **24, SubjectState** (`CC_KIND_SUBJECT`), a Store v1 object (claim version 1) like the kinds of ADR 0016. It is additive: the existing resolver ignores kinds it does not track, so a Store with no kind-24 object resolves exactly as before. The reference implementation is `native/kernel/svc/continuity_subject.{h,c}`. + +### 2.2 Layout (little-endian, fixed, 16-byte header as ADR 0016) + +| field | bytes | meaning | +|---|---|---| +| header | 16 | magic `AIENSUBJ`, format_version u16 = **0**, reserved u16 = 0, body length u32 | +| root | 32 | logical ObjectId of the AgentRoot this subject belongs to | +| agent | 32 | LogicalAgentId; must equal that AgentRoot's | +| previous | 32 | ObjectId of the prior subject object; zero only for sequence 1 | +| sequence | 8 | previous.sequence + 1; 1 for the first (genesis) subject object | +| cortex | 32 | Cortex lineage reference: digest of the journal's record 1; zero = unbound | +| provenance | 32 | who or what established this object (operator key id or receipt digest); nonzero | +| origin | 1 | 1 operator, 2 promoted | +| reserved | 7 | zero | +| n_intents, n_knowledge | 2 + 2 | 0..64 each | +| reserved | 4 | zero | +| intents | n × 96 | strictly ascending by id | +| knowledge | n × 48 | strictly ascending by digest | + +Largest object: 9416 bytes, inside the K-1 cap (16384). The logical ObjectId is `cc_object_id(24, bytes, len)`; the same bytes give the same id on any machine, under any model, in any process. + +**Intent (96 bytes):** id 32, supersedes 32 (zero = none), kind u32, state u8 (1 ACTIVE, 2 INACTIVE, 3 SUPERSEDED), reserved 3, since u64 (the subject sequence that created it), payload 2 × u64. `id = SHA-256("AIENOS_INTENT_v0:" || agent || kind || since || payload[0] || payload[1])`, recomputed on decode. v0 defines one kind, **1 = GOAL_LATENCY** (payload: regime, target ns), the one goal shape the resident AIEN faculty reads today (omega `rx_aien.h`). Rules: at most one ACTIVE intent per (kind, payload[0]) slot; a SUPERSEDED intent is named by exactly one newer intent; `supersedes` must name an older, SUPERSEDED intent in the same table; `since <= sequence`. + +**Knowledge (48 bytes):** digest 32 of a Cortex promotion record, record u64 (informative), since u64. Knowledge is held by digest only; the record itself stays in Cortex. + +### 2.3 What the object does not hold + +No capability, no weight, no model digest, no machine, process, queue or faculty reference. Model independence is structural: there is no field in which a model could be named. The model that happens to be loaded is provenance a host tool may print; it is stored nowhere in the subject. + +### 2.4 Resolution and commit + +`cs_resolve` runs over the same `cr_source` as the other continuity objects: every kind-24 object must name this Store's AgentRoot and LogicalAgentId (a foreign one is CORRUPT, never adopted); the chain must be gap-free, fork-free and link-correct from sequence 1 to the head; an undecodable object or an unknown claim version is CORRUPT. A Store with a root and no subject object is **ABSENT**: the kernel reports it; it never mints a subject on its own. + +`cs_commit` writes one kind-24 object in its own Store transaction and only if it continues the chain the caller just resolved (the head id must equal `previous`; a stale successor and a second genesis are refused, nothing is written). + +### 2.5 Authority + +The subject object is state, not authority. Nothing in `continuity_subject.c` mints, grants, dispatches, waits, sleeps, polls or schedules; the file compiles for the host test and the no_std kernel without a thread or a clock. How a standing intent becomes a goal the organism acts on is the omega side (ARCH-0035 §R16): ALLEN publishes typed state through the World's external publication path, and the World's dependency machinery wakes the faculty. No AIENOS code calls a faculty. + +### 2.6 Mutants + +Four mutants (`CS_MUTANT_ACCEPT_FOREIGN`, `CS_MUTANT_ACCEPT_TWO_ACTIVE`, `CS_MUTANT_SKIP_INTENT_ID`, `CS_MUTANT_ACCEPT_FORK`) are test-only, each must turn the host test red, and each is refused by `#error` under `CK_HARDWARE_STAGING`. + +## 3. Qualification + +Status vocabulary: IMPLEMENTED / TESTED / QUALIFIED / NOT_RUN / FUTURE. + +| claim | status | evidence | +|---|---|---| +| codec, rules, resolver, committer | IMPLEMENTED, TESTED (host) | `make -C native/kernel test`: `test_continuity_subject: PASS (150 checks)`, inside `CK_CORE_HOST: PASS` | +| known-answer layout | TESTED | hand-written bytes and `sv1_object_id` twin agree | +| refusals (version, reserved, lengths, zero ids, origin, intent id, states, supersession, order, fork, gap, link, foreign root/agent) | TESTED | `t_refusals`, `t_intents`, `t_resolve` | +| sealed Store: provision, genesis commit, stale successor refused, second genesis refused, reopen, resume, same id | TESTED | `t_sealed_store` (real `disk_file` image, M5 test keys, mocked RNDR) | +| **process restart over one sealed Store image** (writer process exits; a second process resolves the same subject id, sequence 2, one intent regime 7 / 1000 ns) | TESTED (host) | `make -C native/kernel test-continuity-subject-restart`: `CK_CONTINUITY_SUBJECT_RESTART: PASS` | +| mutants load-bearing and refused in hardware staging | TESTED | `make -C native/kernel continuity-subject-mutants`: `CK_CONTINUITY_SUBJECT_MUTANTS: PASS` | +| sanitizer build | TESTED | host-san target of the same test | +| QEMU cold restart over NVMe (ADR 0016 §Qualification shape) | NOT_RUN | no QEMU run was made for kind 24 | +| hardware (Spark NVMe, Secure Boot chain) | NOT_RUN | host tests are not hardware qualification | +| OS reboot, machine migration | NOT_RUN | not claimed | +| multi-subject Stores | FUTURE | the format does not preclude it (root + agent per object); v0 resolves one chain per root | +| manifest link to the subject head | FUTURE | the manifest (kind 17) is frozen; v0 resolves the subject chain by scan | + +## 4. Consequences + +- A restart now has somewhere to read what the subject intends; whether it does is the omega side (ARCH-0035 gates G2 and G6). +- `docs/ARCHITECTURE.md` §10 and `CONTEXT.md` ("AIEN Agent") are corrected in this change: AIEN is the continuous organism; ALLEN is the durable subject it sustains. `docs/BLUEPRINT.md` and the Continuous-Existence Amendment keep their historical wording until ARCH-0035 is accepted. +- The resolver gains one pass per kind-24 object; the kernel's recovery and repair tools do not yet display kind 24 (FUTURE, with the manifest link). +- Nothing changes for Stores without a kind-24 object. + +## 5. Open questions for the operator + +1. Freeze format_version 0 of kind 24 together with ADR 0016, or keep both open until a QEMU cold-restart run has been made for kind 24. +2. Whether `origin = promoted` (a subject object written from a Cortex promotion rather than by the operator) is allowed before AEGIS review of the promotion path. diff --git a/docs/adr/README.md b/docs/adr/README.md index 84f2a254..047f671d 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -22,6 +22,7 @@ This directory documents the key architectural decisions and formal technical sp | [ADR 0014](0014-binary-artifact-v0-and-native-admission.md) | Binary Artifact v0 and Native Admission | Proposed | 2026-09-25 | [ADR 0012](0012-self-construction-capability-growth-and-generations.md); [ADR 0013](0013-aienos-abi-v1.md); SEED-0B qualification | | [ADR 0015](0015-system-store-v1-format.md) | AIENOS System Store v1 Format | Accepted for P3-1 format freeze | 2026-09-25 | [ADR 0003](0003-bootstrap-firmware-handoff-and-minimal-object-store.md); P3 Native Persistence & Continuity | | [ADR 0016](0016-continuity-objects-over-store-v1.md) | Continuity objects over System Store v1 | Proposed (format freeze needs operator approval) | 2026-09-25 | [ADR 0007](0007-continuous-existence-provisioning-once.md), [ADR 0015](0015-system-store-v1-format.md); M4 continuity | +| [ADR 0018](0018-allen-subject-state-object.md) | ALLEN Subject State Object (continuity kind 24) | Proposed (companion to ARCH-0035; format freeze needs operator approval) | 2026-10-04 | [ADR 0016](0016-continuity-objects-over-store-v1.md); omega ARCH-0022 Cortex; R16 | --- diff --git a/native/.crumb b/native/.crumb index 9f1bcfec..19e02b09 100644 --- a/native/.crumb +++ b/native/.crumb @@ -50,74 +50,74 @@ "children": [ { "digest": "7a5972d5e7135c13af527ba7bc91f6532fcebc206df0686e742e98c1050c6c29", - "last_commit": "130aa20", + "last_commit": "fbbfc9c", "name": "argus", "purpose": "Directory native/argus. Purpose not yet described by a human." }, { "digest": "f181cbf296165f1d483535eafbc9d7a1056ab20aa838ea23e39ef55fb80d9b1b", - "last_commit": "2e44c0b", + "last_commit": "fbbfc9c", "name": "boot", "purpose": "Directory native/boot. Purpose not yet described by a human." }, { "digest": "77549724e1d580ea9df8accae4e5683cab8a5a6b3f9652d3594732fbc4a8ec60", - "last_commit": "098e9b6", + "last_commit": "fbbfc9c", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, { "digest": "44513be407ed44a29feac97bfec222e968f9f6415631f0cd62dc65a522c420e4", - "last_commit": "a4bba0c", + "last_commit": "fbbfc9c", "name": "crypto", "purpose": "Directory native/crypto. Purpose not yet described by a human." }, { "digest": "31584482d53c2b82d89a44a8a12f67c7f808f740d95ad5ad050b58bcca6b47aa", - "last_commit": "2f9b95a", + "last_commit": "fbbfc9c", "name": "disk", "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "9ee6c06ce6047c883f876c781369b1d9339e703fc3a8463e3aa15d2292f0eab9", - "last_commit": "c2232ef", + "digest": "8e35a9e1835b16a6cb134750cfeeeec6d305657c54fe1d314f104281e620a69c", + "last_commit": "fbbfc9c", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, { "digest": "2a90bf4a27e33298c83bd86fe20ea532f113e4162f10dce8f37130afef0eeefd", - "last_commit": "181ea41", + "last_commit": "fbbfc9c", "name": "m5", "purpose": "Directory native/m5. Purpose not yet described by a human." }, { "digest": "c91e9201f6ce9cb909ce3fadfa60d4869fc661db8d4266cd2c15e0688d384cf7", - "last_commit": "4a116e5", + "last_commit": "fbbfc9c", "name": "net", "purpose": "Directory native/net. Purpose not yet described by a human." }, { "digest": "295150763e0b19f9f74a3b49d679213467075e9682f1f23d0e5ea838872d20e1", - "last_commit": "cea54be", + "last_commit": "fbbfc9c", "name": "sig", "purpose": "Directory native/sig. Purpose not yet described by a human." }, { "digest": "8c0d84e2ac3642d2bf59a7faf91c6ddf7c27b4038ee020f243fade5385044efb", - "last_commit": "8555049", + "last_commit": "fbbfc9c", "name": "store", "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:f4c7b8fbe0a234ba02f5f12d89aa6063c4cc13b86ae2ec667851819cbd74b9a9", - "compiled_at": "2026-10-04T17:20:42Z", + "children_root": "sha256:80f2c4f08f2c4fb4507e247a920c0efbf74c0772a777f3d26a9107da0f8b546e", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:26b2b9ef6aa2558a23af185081b97d8c20682dff0bd3027b673716db0735a4bf", + "digest": "sha256:e6f06b76c4d9c1884afb930cfbb1b72266981bbe588b75705b1211ed150cc0d9", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 300, - "generated_at_commit": "c2232ef1575db74f797a28751152427d476a95b5", - "source_tree": "sha256:048466ad9405f6f56ed880e07bf527d5d274a84db303ed4f0bf8ebe79b08343d" + "files": 303, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:7815bc208ce76ad5bc86a6af2a24bb33e9bd7b4be86f1e32d6de1255493e927f" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index f85a5203..281b7ad8 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -46,68 +46,68 @@ "children": [ { "digest": "4c9da3fdaf762c784403ae3281c3a7e49b891596ed52ab27aaf62f2ea66c5502", - "last_commit": "2ee61b4", + "last_commit": "fbbfc9c", "name": "arch", "purpose": "Directory native/kernel/arch. Purpose not yet described by a human." }, { "digest": "efd08e41584e2cbb282cea72e620fa2db42c423c6b3211a91585affb92e34893", - "last_commit": "c3878bb", + "last_commit": "fbbfc9c", "name": "artifact", "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { "digest": "dd6ebb445202c084d41a923774ff81d2c0c384393b7383c92ed09e77028d2307", - "last_commit": "c2232ef", + "last_commit": "fbbfc9c", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { "digest": "23eaffd404d1c16455030fd1c27ba14fa3311714528b336db3f59451e880819e", - "last_commit": "44de11f", + "last_commit": "fbbfc9c", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { "digest": "dd460494a52e8cc9d2b05d75f86d71e8c387090c04ebcb7c448730dc765d39f1", - "last_commit": "9daa5e1", + "last_commit": "fbbfc9c", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, { "digest": "272bc1dd7957e2024660c4f2850462671069bec77f4eee8d2c71b5b8788ab97e", - "last_commit": "f37c8b3", + "last_commit": "fbbfc9c", "name": "mm", "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "01691254cbd24b1c92b6d1b38fc69d76f21a15d9b3069b5b02e88b52833a5e55", - "last_commit": "31ed9f8", + "digest": "a869aee7e92862a2d16efb62a0d881209fe82a9ba1b70332050f755a58230630", + "last_commit": "fbbfc9c", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "451e82cfde801fae5f0062cba5a86d572636bed23d5ce4cdd3529c1ce28ba417", - "last_commit": "f37c8b3", + "digest": "2becacc3ed46f19dcd0a7b91acef18c77abb4e2f1231204a1ab02d74b244e80c", + "last_commit": "fbbfc9c", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { "digest": "76d69ccab500306184f1ee05b9b252c740236a9656ceda44fe96c93bce2f2b21", - "last_commit": "c2232ef", + "last_commit": "fbbfc9c", "name": "tools", "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:d01e49eb49d0b6304690bff7ff81c2e7c6525aaf33ce7f19dabce8f3e6c6d335", - "compiled_at": "2026-10-04T17:20:42Z", + "children_root": "sha256:433b5ab933dc9613e1b5b300532e3d4ec3dd21fd1b1bd7b18ddfe22f332daebc", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:9ee6c06ce6047c883f876c781369b1d9339e703fc3a8463e3aa15d2292f0eab9", + "digest": "sha256:8e35a9e1835b16a6cb134750cfeeeec6d305657c54fe1d314f104281e620a69c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 136, - "generated_at_commit": "c2232ef1575db74f797a28751152427d476a95b5", - "source_tree": "sha256:26233472004fc3bb2dfea000922f7ccf096ec7750c8266e924689e24a37b342d" + "files": 139, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:8f8be86b10860cc789c9cc253b3c07aa3810db6e523c14df8e492d9deae2ac1b" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 40abe8d6..16816fd6 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -640,7 +640,8 @@ HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/ $(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c \ $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c TESTS := test_fmt test_frames test_pt test_acpi test_heap test_smmu test_sched test_ipc test_artifact test_entropy test_handoff test_fbcon \ - test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry + test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ + test_continuity_subject HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer @@ -664,8 +665,8 @@ handoff-mutant: $(OUT)/host/test_handoff_mutant @if $< > $<.log 2>&1; then cat $<.log; echo "test_handoff mutant (reserved0 check removed): PASSED, test does not catch it"; exit 1; \ else grep -F "test_handoff: FAIL" $<.log && echo "test_handoff mutant (reserved0 check removed): FAILS as required"; fi -test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant - @fail=0; for t in $(filter-out handoff-mutant,$^); do $$t || fail=1; done; \ +test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant test-continuity-subject-restart + @fail=0; for t in $(filter-out handoff-mutant test-continuity-subject-restart,$^); do $$t || fail=1; done; \ if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS"; else echo "CK_CORE_HOST: FAIL"; exit 1; fi sanitize: $(addprefix $(OUT)/host-san/,$(TESTS)) @@ -720,7 +721,7 @@ $(OUT)/host-san/test_continuity_codec: $(KDIR)/tests/test_continuity_codec.c $(C @mkdir -p $(dir $@) $(HOSTCC) $(HCFLAGS) $(SANFLAGS) $(CC_HOST_INC) $(CC_FIXTURE_DEF) -o $@ $< $(CC_HOST_SRCS) -continuity-mutants: $(OUT)/host/test_continuity_codec continuity-resolve-mutants continuity-commit-mutants continuity-recovery-mutants +continuity-mutants: $(OUT)/host/test_continuity_codec continuity-resolve-mutants continuity-commit-mutants continuity-recovery-mutants continuity-subject-mutants @mkdir -p $(OUT)/host-mut; fail=0; \ $(OUT)/host/test_continuity_codec > $(OUT)/host-mut/base.log 2>&1 || { echo "unmutated test_continuity_codec fails"; fail=1; }; \ for m in $(CC_MUTANTS); do \ @@ -830,6 +831,56 @@ continuity-commit-mutants: $(OUT)/host/test_continuity_commit done; \ if [ $$fail = 0 ]; then echo "CK_CONTINUITY_COMMIT_MUTANTS: PASS"; else echo "CK_CONTINUITY_COMMIT_MUTANTS: FAIL"; exit 1; fi +# ALLEN subject state (continuity object kind 24; ARCH-0035 / OS-0018, PROPOSED): +# host test over the REAL sealed Store in a file, a two-process restart proof +# (test-continuity-subject-restart) and TEST-only -D mutants, each of which must +# make the test print its FAIL verdict. Mutant flags are refused under +# CK_HARDWARE_STAGING (svc/continuity_subject.h). +CS_HOST_SRCS := $(KDIR)/svc/continuity_subject.c $(CM_HOST_SRCS) +CS_HOST_DEPS := $(CS_HOST_SRCS) $(wildcard $(KDIR)/svc/continuity_*.h) $(KDIR)/core/entropy.h \ + $(wildcard $(NATIVE)/store/*.h $(NATIVE)/m5/*.h $(NATIVE)/disk/*.h $(NATIVE)/crypto/*.h) $(KDIR)/tests/ck_test.h +# MS-1 foreign root/agent accepted at resolve, MS-2 two active intents in one slot +# accepted, MS-3 intent id derivation not checked, MS-4 chain fork accepted. +CS_MUTANTS := CS_MUTANT_ACCEPT_FOREIGN CS_MUTANT_ACCEPT_TWO_ACTIVE CS_MUTANT_SKIP_INTENT_ID CS_MUTANT_ACCEPT_FORK + +$(OUT)/host/test_continuity_subject: $(KDIR)/tests/test_continuity_subject.c $(CS_HOST_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(CM_HOST_INC) -o $@ $< $(CS_HOST_SRCS) + +$(OUT)/host-san/test_continuity_subject: $(KDIR)/tests/test_continuity_subject.c $(CS_HOST_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) $(CM_HOST_INC) -o $@ $< $(CS_HOST_SRCS) + +# ALLEN-G2 on the host: the writer process provisions and commits a subject with +# one standing intent and exits; a second process reopens the same Store image, +# resolves the same subject object and finds the same intent. +.PHONY: test-continuity-subject-restart +test-continuity-subject-restart: $(OUT)/host/test_continuity_subject + @mkdir -p $(OUT)/host-subject; img=$(OUT)/host-subject/allen.img; rm -f $$img; \ + $(OUT)/host/test_continuity_subject --write $$img > $(OUT)/host-subject/write.log || { cat $(OUT)/host-subject/write.log; exit 1; }; \ + id=$$(sed -n 's/^ALLEN subject id: //p' $(OUT)/host-subject/write.log); \ + test -n "$$id" || { echo "no subject id from the writer"; exit 1; }; \ + $(OUT)/host/test_continuity_subject --restore $$img $$id > $(OUT)/host-subject/restore.log || { cat $(OUT)/host-subject/restore.log; exit 1; }; \ + grep -h '^ALLEN' $(OUT)/host-subject/write.log $(OUT)/host-subject/restore.log; \ + echo "CK_CONTINUITY_SUBJECT_RESTART: PASS (two processes, sealed Store image $$img)" + +continuity-subject-mutants: $(OUT)/host/test_continuity_subject + @mkdir -p $(OUT)/host-mut; fail=0; \ + $(OUT)/host/test_continuity_subject > $(OUT)/host-mut/subject_base.log 2>&1 || { echo "unmutated test_continuity_subject fails"; fail=1; }; \ + for m in $(CS_MUTANTS); do \ + b=$(OUT)/host-mut/test_continuity_subject_$$m; \ + if ! $(HOSTCC) $(HCFLAGS) $(CM_HOST_INC) -D$$m -o $$b $(KDIR)/tests/test_continuity_subject.c $(CS_HOST_SRCS); then \ + echo "mutant $$m does not build (FAIL)"; fail=1; continue; fi; \ + $$b > $$b.log 2>&1; \ + if grep -q "^test_continuity_subject: FAIL" $$b.log; then echo "mutant $$m caught"; \ + else echo "mutant $$m NOT caught (FAIL)"; fail=1; fi; \ + if $(HOSTCC) $(HCFLAGS) $(CM_HOST_INC) -DCK_HARDWARE_STAGING=1 -D$$m \ + -fsyntax-only $(KDIR)/svc/continuity_subject.c 2>/dev/null; then \ + echo "mutant flag $$m accepted under CK_HARDWARE_STAGING (FAIL)"; fail=1; \ + else echo "mutant flag $$m refused under CK_HARDWARE_STAGING"; fi; \ + done; \ + if [ $$fail = 0 ]; then echo "CK_CONTINUITY_SUBJECT_MUTANTS: PASS"; else echo "CK_CONTINUITY_SUBJECT_MUTANTS: FAIL"; exit 1; fi + # Continuity Recovery Core (C port cut 5): host test over the REAL sealed Store in a # file, TEST-only -D mutants, each of which must make the test print its FAIL verdict. # Mutant flags are refused under CK_HARDWARE_STAGING (svc/continuity_recovery.h). diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index 09fc7113..5fc45d81 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -18,26 +18,26 @@ "children": [ { "digest": "17d9c9eba5fe222af0e8700ac3baa3461eb217a372f16a5e79e00a19cc5df6f5", - "last_commit": "31ed9f8", + "last_commit": "fbbfc9c", "name": "compat", "purpose": "Directory native/kernel/svc/compat. Purpose not yet described by a human." }, { "digest": "2815a08ee2c689d59a24bc81ec578a1068cd2bbc34737bb16418e0ebd8ecb3f9", - "last_commit": "2f393a4", + "last_commit": "fbbfc9c", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], "children_root": "sha256:9e2977ce5ea7f74952e26dfb57f3703ab65c5ccbe933e2f5135ab1a5496e0af7", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:01691254cbd24b1c92b6d1b38fc69d76f21a15d9b3069b5b02e88b52833a5e55", + "digest": "sha256:a869aee7e92862a2d16efb62a0d881209fe82a9ba1b70332050f755a58230630", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 31, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:899690940bb50f9b39632741a905b9f7fcbea7b0246584894ccb73933eeb6427" + "files": 33, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:3736ca3a8cea72dfd0aa38b84c397d80bd756b08c83a53a27485c08da75b8a54" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/continuity_subject.c b/native/kernel/svc/continuity_subject.c new file mode 100644 index 00000000..7d850178 --- /dev/null +++ b/native/kernel/svc/continuity_subject.c @@ -0,0 +1,622 @@ +/* continuity_subject.c -- ALLEN subject state (kind 24): codec, editing, + * resolve and commit. See continuity_subject.h for the format and rules. */ +#include "continuity_subject.h" + +#include "sha256.h" +#include "store_v1.h" + +static const uint8_t MAGIC_SUBJECT[8] = {'A', 'I', 'E', 'N', 'S', 'U', 'B', 'J'}; +static const char INTENT_DOMAIN[] = "AIENOS_INTENT_v0:"; + +static int fail(const char **why, int cls, const char *w) +{ + if (why) + *why = w; + return cls; +} + +/* ---- intent identity ---- */ +void cs_intent_id(const uint8_t agent[32], uint32_t kind, uint64_t since, const uint64_t payload[2], + uint8_t out[32]) +{ + sha256_ctx c; + uint8_t b[8]; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)INTENT_DOMAIN, sizeof INTENT_DOMAIN - 1); + sha256_update(&c, agent, 32); + sv1_put32(b, kind); + sha256_update(&c, b, 4); + sv1_put64(b, since); + sha256_update(&c, b, 8); + sv1_put64(b, payload[0]); + sha256_update(&c, b, 8); + sv1_put64(b, payload[1]); + sha256_update(&c, b, 8); + sha256_final(&c, out); +} + +/* ---- validation (every rule the decoder enforces, usable before encode) ---- */ +static int state_ok(uint8_t s) +{ + return s == CS_ACTIVE || s == CS_INACTIVE || s == CS_SUPERSEDED; +} + +int cs_subject_validate(const struct cs_subject *s, const char **why) +{ + if (!s) + return fail(why, CC_E_ARG, "null argument"); + if (sv1_all_zero(s->root, 32)) + return fail(why, CC_E_CORRUPT, "zero root id"); + if (sv1_all_zero(s->agent, 32)) + return fail(why, CC_E_CORRUPT, "zero agent id"); + if (s->sequence == 0) + return fail(why, CC_E_CORRUPT, "zero sequence"); + if (s->sequence == 1 && !sv1_all_zero(s->previous, 32)) + return fail(why, CC_E_CORRUPT, "genesis subject names a previous object"); + if (s->sequence > 1 && sv1_all_zero(s->previous, 32)) + return fail(why, CC_E_CORRUPT, "subject object after the first has no previous"); + if (sv1_all_zero(s->provenance, 32)) + return fail(why, CC_E_CORRUPT, "zero provenance"); + if (s->origin != CS_ORIGIN_OPERATOR && s->origin != CS_ORIGIN_PROMOTED) + return fail(why, CC_E_CORRUPT, "unknown subject origin"); + if (s->n_intents > CS_MAX_INTENTS) + return fail(why, CC_E_LIMIT, "too many standing intents"); + if (s->n_knowledge > CS_MAX_KNOWLEDGE) + return fail(why, CC_E_LIMIT, "too many knowledge references"); + for (uint32_t i = 0; i < s->n_intents; i++) { + const struct cs_intent *a = &s->in[i]; + uint8_t id[32]; + if (i > 0 && sv1_cmp_id(s->in[i - 1].id, a->id) >= 0) + return fail(why, CC_E_CORRUPT, "intents not strictly ascending by id"); + if (!state_ok(a->state)) + return fail(why, CC_E_CORRUPT, "unknown intent state"); + if (a->kind != CS_INTENT_GOAL_LATENCY) + return fail(why, CC_E_CORRUPT, "unknown intent kind"); + if (a->since == 0 || a->since > s->sequence) + return fail(why, CC_E_CORRUPT, "intent created outside the subject chain"); + cs_intent_id(s->agent, a->kind, a->since, a->payload, id); +#ifndef CS_MUTANT_SKIP_INTENT_ID + if (!sv1_equal(id, a->id, 32)) + return fail(why, CC_E_CORRUPT, "intent id does not derive from its content"); +#endif + if (!sv1_all_zero(a->supersedes, 32)) { + uint32_t j; + if (sv1_equal(a->supersedes, a->id, 32)) + return fail(why, CC_E_CORRUPT, "intent supersedes itself"); + for (j = 0; j < s->n_intents; j++) + if (sv1_equal(s->in[j].id, a->supersedes, 32)) + break; + if (j == s->n_intents) + return fail(why, CC_E_CORRUPT, "superseded intent is absent"); + if (s->in[j].state != CS_SUPERSEDED) + return fail(why, CC_E_CORRUPT, "superseded intent is not marked superseded"); + if (s->in[j].since > a->since) + return fail(why, CC_E_CORRUPT, "intent supersedes a newer intent"); + } +#ifndef CS_MUTANT_ACCEPT_TWO_ACTIVE + if (a->state == CS_ACTIVE) + for (uint32_t j = 0; j < i; j++) + if (s->in[j].state == CS_ACTIVE && s->in[j].kind == a->kind && + s->in[j].payload[0] == a->payload[0]) + return fail(why, CC_E_CORRUPT, "two active intents in one slot"); +#endif + } + /* Every SUPERSEDED intent is named by exactly one other intent. */ + for (uint32_t i = 0; i < s->n_intents; i++) { + if (s->in[i].state != CS_SUPERSEDED) + continue; + uint32_t named = 0; + for (uint32_t j = 0; j < s->n_intents; j++) + if (sv1_equal(s->in[j].supersedes, s->in[i].id, 32)) + named++; + if (named != 1) + return fail(why, CC_E_CORRUPT, "superseded intent not named by exactly one successor"); + } + for (uint32_t i = 0; i < s->n_knowledge; i++) { + const struct cs_knowledge *k = &s->kn[i]; + if (sv1_all_zero(k->digest, 32)) + return fail(why, CC_E_CORRUPT, "zero knowledge digest"); + if (i > 0 && sv1_cmp_id(s->kn[i - 1].digest, k->digest) >= 0) + return fail(why, CC_E_CORRUPT, "knowledge not strictly ascending by digest"); + if (k->since == 0 || k->since > s->sequence) + return fail(why, CC_E_CORRUPT, "knowledge held outside the subject chain"); + } + return CC_OK; +} + +/* ---- encode ---- */ +static size_t body_len(const struct cs_subject *s) +{ + return CS_FIXED_BODY + (size_t)s->n_intents * CS_INTENT_BYTES + + (size_t)s->n_knowledge * CS_KNOWLEDGE_BYTES; +} + +int cs_subject_encode(const struct cs_subject *s, uint8_t *out, size_t cap, size_t *len, + const char **why) +{ + int e; + size_t body, total, at = 0; + if (!s || !len) + return fail(why, CC_E_ARG, "null argument"); + *len = 0; + if ((e = cs_subject_validate(s, why))) + return e; + body = body_len(s); + total = CC_HEADER_BYTES + body; + if (total > CC_MAX_OBJECT_BYTES) + return fail(why, CC_E_LIMIT, "continuity object exceeds the 16384-byte sealed Store cap"); + if (!out || cap < total) + return fail(why, CC_E_ARG, "output buffer too small"); + sv1_copy(out + at, MAGIC_SUBJECT, 8); + at += 8; + sv1_put16(out + at, CC_FORMAT_VERSION); + at += 2; + sv1_put16(out + at, 0); + at += 2; + sv1_put32(out + at, (uint32_t)body); + at += 4; + sv1_copy(out + at, s->root, 32); + at += 32; + sv1_copy(out + at, s->agent, 32); + at += 32; + sv1_copy(out + at, s->previous, 32); + at += 32; + sv1_put64(out + at, s->sequence); + at += 8; + sv1_copy(out + at, s->cortex, 32); + at += 32; + sv1_copy(out + at, s->provenance, 32); + at += 32; + out[at++] = s->origin; + sv1_zero(out + at, 7); + at += 7; + sv1_put16(out + at, (uint16_t)s->n_intents); + at += 2; + sv1_put16(out + at, (uint16_t)s->n_knowledge); + at += 2; + sv1_put32(out + at, 0); + at += 4; + for (uint32_t i = 0; i < s->n_intents; i++) { + const struct cs_intent *a = &s->in[i]; + sv1_copy(out + at, a->id, 32); + at += 32; + sv1_copy(out + at, a->supersedes, 32); + at += 32; + sv1_put32(out + at, a->kind); + at += 4; + out[at++] = a->state; + sv1_zero(out + at, 3); + at += 3; + sv1_put64(out + at, a->since); + at += 8; + sv1_put64(out + at, a->payload[0]); + at += 8; + sv1_put64(out + at, a->payload[1]); + at += 8; + } + for (uint32_t i = 0; i < s->n_knowledge; i++) { + const struct cs_knowledge *k = &s->kn[i]; + sv1_copy(out + at, k->digest, 32); + at += 32; + sv1_put64(out + at, k->record); + at += 8; + sv1_put64(out + at, k->since); + at += 8; + } + *len = at; + return CC_OK; +} + +/* ---- decode: one bounds check, every rule, no trailing bytes ---- */ +struct rd { + const uint8_t *b; + size_t len, at; +}; + +static int take(struct rd *r, size_t n, const uint8_t **p, const char **why) +{ + if (n > r->len - r->at) + return fail(why, CC_E_CORRUPT, "truncated object"); + *p = r->b + r->at; + r->at += n; + return CC_OK; +} + +static int rd_zeros(struct rd *r, size_t n, const char **why) +{ + const uint8_t *p; + int e; + if ((e = take(r, n, &p, why))) + return e; + if (!sv1_all_zero(p, n)) + return fail(why, CC_E_CORRUPT, "nonzero reserved bytes"); + return CC_OK; +} + +int cs_subject_decode(const uint8_t *in, size_t len, struct cs_subject *s, const char **why) +{ + struct rd r; + const uint8_t *p; + int e; + uint32_t v; + if (!in || !s) + return fail(why, CC_E_ARG, "null argument"); + sv1_zero(s, sizeof *s); + r.b = in; + r.len = len; + r.at = 0; + if (len > CC_MAX_OBJECT_BYTES) + return fail(why, CC_E_LIMIT, "continuity object exceeds the 16384-byte sealed Store cap"); + if ((e = take(&r, 8, &p, why))) + return e; + if (!sv1_equal(p, MAGIC_SUBJECT, 8)) + return fail(why, CC_E_CORRUPT, "bad magic"); + if ((e = take(&r, 2, &p, why))) + return e; + if (sv1_get16(p) != CC_FORMAT_VERSION) + return fail(why, CC_E_CORRUPT, "unsupported continuity format version"); + if ((e = rd_zeros(&r, 2, why))) + return fail(why, e, "nonzero header reserved"); + if ((e = take(&r, 4, &p, why))) + return e; + if (sv1_get32(p) != (uint32_t)(len - CC_HEADER_BYTES)) + return fail(why, CC_E_CORRUPT, "body length mismatch"); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(s->root, p, 32); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(s->agent, p, 32); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(s->previous, p, 32); + if ((e = take(&r, 8, &p, why))) + return e; + s->sequence = sv1_get64(p); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(s->cortex, p, 32); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(s->provenance, p, 32); + if ((e = take(&r, 1, &p, why))) + return e; + s->origin = p[0]; + if ((e = rd_zeros(&r, 7, why))) + return e; + if ((e = take(&r, 2, &p, why))) + return e; + v = sv1_get16(p); + if (v > CS_MAX_INTENTS) + return fail(why, CC_E_LIMIT, "too many standing intents"); + s->n_intents = v; + if ((e = take(&r, 2, &p, why))) + return e; + v = sv1_get16(p); + if (v > CS_MAX_KNOWLEDGE) + return fail(why, CC_E_LIMIT, "too many knowledge references"); + s->n_knowledge = v; + if ((e = rd_zeros(&r, 4, why))) + return e; + for (uint32_t i = 0; i < s->n_intents; i++) { + struct cs_intent *a = &s->in[i]; + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(a->id, p, 32); + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(a->supersedes, p, 32); + if ((e = take(&r, 4, &p, why))) + return e; + a->kind = sv1_get32(p); + if ((e = take(&r, 1, &p, why))) + return e; + a->state = p[0]; + if ((e = rd_zeros(&r, 3, why))) + return e; + if ((e = take(&r, 8, &p, why))) + return e; + a->since = sv1_get64(p); + if ((e = take(&r, 8, &p, why))) + return e; + a->payload[0] = sv1_get64(p); + if ((e = take(&r, 8, &p, why))) + return e; + a->payload[1] = sv1_get64(p); + } + for (uint32_t i = 0; i < s->n_knowledge; i++) { + struct cs_knowledge *k = &s->kn[i]; + if ((e = take(&r, 32, &p, why))) + return e; + sv1_copy(k->digest, p, 32); + if ((e = take(&r, 8, &p, why))) + return e; + k->record = sv1_get64(p); + if ((e = take(&r, 8, &p, why))) + return e; + k->since = sv1_get64(p); + } + if (r.at != r.len) + return fail(why, CC_E_CORRUPT, "trailing bytes"); + return cs_subject_validate(s, why); +} + +int cs_subject_id(const uint8_t *bytes, size_t len, uint8_t out[32]) +{ + return cc_object_id(CC_KIND_SUBJECT, bytes, len, out); +} + +/* ---- editing ---- */ +void cs_subject_genesis(struct cs_subject *s, const struct cr_view *v, const uint8_t provenance[32], + uint8_t origin) +{ + sv1_zero(s, sizeof *s); + sv1_copy(s->root, v->root_id, 32); + sv1_copy(s->agent, v->root.agent_id, 32); + s->sequence = 1; + sv1_copy(s->provenance, provenance, 32); + s->origin = origin; +} + +void cs_subject_advance(struct cs_subject *s, const uint8_t prev_id[32], + const uint8_t provenance[32], uint8_t origin) +{ + sv1_copy(s->previous, prev_id, 32); + s->sequence += 1; + sv1_copy(s->provenance, provenance, 32); + s->origin = origin; +} + +static long find_intent(const struct cs_subject *s, const uint8_t id[32], uint32_t *ins) +{ + uint32_t i; + for (i = 0; i < s->n_intents; i++) { + int c = sv1_cmp_id(s->in[i].id, id); + if (c == 0) + return (long)i; + if (c > 0) + break; + } + if (ins) + *ins = i; + return -1; +} + +const struct cs_intent *cs_subject_active(const struct cs_subject *s, uint32_t kind, uint64_t slot) +{ + for (uint32_t i = 0; i < s->n_intents; i++) + if (s->in[i].state == CS_ACTIVE && s->in[i].kind == kind && s->in[i].payload[0] == slot) + return &s->in[i]; + return 0; +} + +int cs_subject_intend(struct cs_subject *s, uint32_t kind, const uint64_t payload[2], + uint8_t id_out[32], const char **why) +{ + struct cs_intent a; + uint32_t ins = 0; + if (!s || !payload) + return fail(why, CC_E_ARG, "null argument"); + if (kind != CS_INTENT_GOAL_LATENCY) + return fail(why, CC_E_ARG, "unknown intent kind"); + if (s->n_intents >= CS_MAX_INTENTS) + return fail(why, CC_E_LIMIT, "too many standing intents"); + sv1_zero(&a, sizeof a); + a.kind = kind; + a.state = CS_ACTIVE; + a.since = s->sequence; + a.payload[0] = payload[0]; + a.payload[1] = payload[1]; + cs_intent_id(s->agent, kind, a.since, a.payload, a.id); + if (find_intent(s, a.id, &ins) >= 0) + return fail(why, CC_E_ARG, "intent already present"); + /* One active intent per slot: the old one is superseded by this one. */ + for (uint32_t i = 0; i < s->n_intents; i++) { + struct cs_intent *o = &s->in[i]; + if (o->state == CS_ACTIVE && o->kind == kind && o->payload[0] == payload[0]) { + o->state = CS_SUPERSEDED; + sv1_copy(a.supersedes, o->id, 32); + break; + } + } + for (uint32_t i = s->n_intents; i > ins; i--) + s->in[i] = s->in[i - 1]; + s->in[ins] = a; + s->n_intents += 1; + if (id_out) + sv1_copy(id_out, a.id, 32); + return CC_OK; +} + +int cs_subject_retire(struct cs_subject *s, const uint8_t id[32], const char **why) +{ + long i; + if (!s || !id) + return fail(why, CC_E_ARG, "null argument"); + i = find_intent(s, id, 0); + if (i < 0) + return fail(why, CC_E_ARG, "intent absent"); + if (s->in[i].state != CS_ACTIVE) + return fail(why, CC_E_ARG, "intent not active"); + s->in[i].state = CS_INACTIVE; + return CC_OK; +} + +int cs_subject_hold(struct cs_subject *s, const uint8_t digest[32], uint64_t record, + const char **why) +{ + uint32_t ins; + if (!s || !digest) + return fail(why, CC_E_ARG, "null argument"); + if (sv1_all_zero(digest, 32)) + return fail(why, CC_E_ARG, "zero knowledge digest"); + if (s->n_knowledge >= CS_MAX_KNOWLEDGE) + return fail(why, CC_E_LIMIT, "too many knowledge references"); + for (ins = 0; ins < s->n_knowledge; ins++) { + int c = sv1_cmp_id(s->kn[ins].digest, digest); + if (c == 0) + return fail(why, CC_E_ARG, "knowledge already held"); + if (c > 0) + break; + } + for (uint32_t i = s->n_knowledge; i > ins; i--) + s->kn[i] = s->kn[i - 1]; + sv1_copy(s->kn[ins].digest, digest, 32); + s->kn[ins].record = record; + s->kn[ins].since = s->sequence; + s->n_knowledge += 1; + return CC_OK; +} + +void cs_subject_bind_cortex(struct cs_subject *s, const uint8_t lineage[32]) +{ + sv1_copy(s->cortex, lineage, 32); +} + +/* ---- resolve ---- */ +static int map_cc(int rc) +{ + return rc == CC_E_LIMIT ? CR_LIMIT : rc == CC_E_ARG ? CR_E_ARG : CR_CORRUPT; +} + +/* Pass 1: every kind-24 object of this Store into w->t (id, seq, previous), + * each one decoded and checked against the resolved root. Pass 2: the chain + * 1..n must be present exactly once each and linked by previous. The head is + * re-read into out. */ +int cs_resolve(const struct cr_source *src, struct cr_work *w, const struct cr_view *v, + struct cs_subject *out, uint8_t out_id[32], const char **why, int *store_rc) +{ + uint32_t n, i, cnt = 0; + uint64_t head_seq = 0; + uint32_t head = 0; + if (!src || !w || !v || !out || !out_id) + return fail(why, CR_E_ARG, "null argument"); + n = src->count(src->ctx); + for (i = 0; i < n; i++) { + uint16_t kind = 0, version = 0; + size_t len = 0; + int rc; + if ((rc = src->entry(src->ctx, i, &kind, &version))) { + if (store_rc) + *store_rc = rc; + return fail(why, CR_STORE, "store entry failed"); + } + if (kind != CC_KIND_SUBJECT) + continue; + if (version != CC_STORE_OBJECT_VERSION) + return fail(why, CR_CORRUPT, "subject object with an unknown claim version"); + if ((rc = src->read(src->ctx, i, w->buf, sizeof w->buf, &len))) { + if (store_rc) + *store_rc = rc; + return fail(why, CR_STORE, "store read failed"); + } + if (len > sizeof w->buf) + return fail(why, CR_LIMIT, "subject object exceeds the 16384-byte sealed Store cap"); + if ((rc = cs_subject_decode(w->buf, len, out, why))) + return map_cc(rc); +#ifndef CS_MUTANT_ACCEPT_FOREIGN + if (!sv1_equal(out->root, v->root_id, 32) || !sv1_equal(out->agent, v->root.agent_id, 32)) + return fail(why, CR_CORRUPT, "subject object belongs to another agent"); +#endif + if (cnt >= CR_TABLE_CAP) + return fail(why, CR_LIMIT, "too many subject objects"); + if (cc_object_id(CC_KIND_SUBJECT, w->buf, len, w->t[cnt].id) != CC_OK) + return fail(why, CR_E_ARG, "object id"); + w->t[cnt].seq = out->sequence; + sv1_copy(w->t[cnt].previous, out->previous, 32); + w->t[cnt].src = i; + w->t[cnt].kind = kind; + w->t[cnt].version = version; + cnt++; + } + if (cnt == 0) { + sv1_zero(out, sizeof *out); + sv1_zero(out_id, 32); + return CS_ABSENT; + } + /* Pass 2: exactly one object per sequence 1..cnt, each naming its predecessor. */ + for (uint64_t s = 1; s <= cnt; s++) { + uint32_t at = cnt, found = 0; + for (i = 0; i < cnt; i++) + if (w->t[i].seq == s) { + at = i; + found++; + } +#ifndef CS_MUTANT_ACCEPT_FORK + if (found > 1) + return fail(why, CR_CORRUPT, "subject chain fork"); +#endif + if (found == 0) + return fail(why, CR_CORRUPT, "subject chain gap"); + if (s == 1) { + if (!sv1_all_zero(w->t[at].previous, 32)) + return fail(why, CR_CORRUPT, "genesis subject names a previous object"); + } else { + uint32_t prev = cnt; + for (i = 0; i < cnt; i++) + if (w->t[i].seq == s - 1) + prev = i; + if (prev == cnt || !sv1_equal(w->t[at].previous, w->t[prev].id, 32)) + return fail(why, CR_CORRUPT, "subject chain link broken"); + } + if (s > head_seq) { + head_seq = s; + head = at; + } + } + { + size_t len = 0; + int rc; + if ((rc = src->read(src->ctx, w->t[head].src, w->buf, sizeof w->buf, &len))) { + if (store_rc) + *store_rc = rc; + return fail(why, CR_STORE, "store read failed"); + } + if ((rc = cs_subject_decode(w->buf, len, out, why))) + return map_cc(rc); + } + sv1_copy(out_id, w->t[head].id, 32); + return CS_RESOLVED; +} + +/* ---- commit ---- */ +int cs_commit(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w, + const struct cr_view *v, const struct cs_subject *s, uint8_t out_id[32], + const char **why, int *store_rc) +{ + static struct cs_subject cur; /* scratch; the kernel style keeps big state static */ + uint8_t cur_id[32]; + size_t len = 0; + int rc; + struct cr_wobj obj; + if (!src || !snk || !w || !v || !s || !out_id) + return fail(why, CR_E_ARG, "null argument"); + if ((rc = cs_subject_validate(s, why))) + return map_cc(rc); + if (!sv1_equal(s->root, v->root_id, 32) || !sv1_equal(s->agent, v->root.agent_id, 32)) + return fail(why, CR_CORRUPT, "subject object belongs to another agent"); + rc = cs_resolve(src, w, v, &cur, cur_id, why, store_rc); + if (rc == CS_ABSENT) { + if (s->sequence != 1) + return fail(why, CR_CORRUPT, "first subject object must have sequence 1"); + } else if (rc == CS_RESOLVED) { + if (s->sequence != cur.sequence + 1 || !sv1_equal(s->previous, cur_id, 32)) + return fail(why, CR_CORRUPT, "subject object does not continue the resolved chain"); + } else { + return rc; + } + if ((rc = cs_subject_encode(s, w->buf, sizeof w->buf, &len, why))) + return map_cc(rc); + obj.kind = CC_KIND_SUBJECT; + obj.version = CC_STORE_OBJECT_VERSION; + obj.bytes = w->buf; + obj.len = len; + if ((rc = snk->transact(snk->ctx, &obj, 1))) { + if (store_rc) + *store_rc = rc; + return fail(why, CR_STORE, "store transaction failed"); + } + if (cc_object_id(CC_KIND_SUBJECT, w->buf, len, out_id) != CC_OK) + return fail(why, CR_E_ARG, "object id"); + return CS_RESOLVED; +} diff --git a/native/kernel/svc/continuity_subject.h b/native/kernel/svc/continuity_subject.h new file mode 100644 index 00000000..ef22151a --- /dev/null +++ b/native/kernel/svc/continuity_subject.h @@ -0,0 +1,167 @@ +/* continuity_subject.h -- ALLEN subject state: continuity object kind 24. + * + * ARCH-0035 (PROPOSED) / OS-0018 (PROPOSED). ALLEN is the durable subject the + * AIEN organism sustains: one chain of content-addressed Store v1 objects + * bound to the AgentRoot (kind 16, OS-0016) of this Store. Each object says + * what the subject currently intends (standing intents), which Cortex + * promotions it holds as its own (knowledge references, by digest only), and + * which Cortex journal lineage is its memory. It holds no capability, weight, + * model, machine, process or queue reference: the model is not the subject. + * + * This file is a codec plus a resolver. It has no thread, no wait, no clock, + * no scheduling and no authority: nothing here can mint, grant or dispatch. + * The kernel resolves subject state like any continuity object (OS-0016 + * step 3 discipline: fork, gap, foreign root or undecodable object stop with + * CORRUPT; a Store with a root but no subject object is ABSENT, never minted). + * + * Encoding (little-endian, fixed layout, no_std style; header as + * continuity_codec.c): 16-byte header {magic "AIENSUBJ", format_version u16 + * = 0, reserved u16 = 0, body length u32}, then + * root 32 logical ObjectId of the AgentRoot this subject belongs to + * agent 32 LogicalAgentId (must equal that AgentRoot's) + * previous 32 ObjectId of the prior subject object; zero for sequence 1 + * sequence 8 previous.sequence + 1; 1 for the genesis subject object + * cortex 32 lineage reference: digest of the Cortex journal's genesis + * record (rx_cortex record id 1); zero = unbound + * provenance 32 who/what established this object (operator key id or a + * receipt digest); nonzero required + * origin 1 CS_ORIGIN_OPERATOR or CS_ORIGIN_PROMOTED + * reserved 7 zero + * n_intents 2 0..CS_MAX_INTENTS + * n_knowledge 2 0..CS_MAX_KNOWLEDGE + * reserved 4 zero + * intents n_intents * 96, strictly ascending by id + * knowledge n_knowledge * 48, strictly ascending by digest + * Largest object: 16 + 184 + 64*96 + 64*48 = 9416 bytes, under the K-1 cap. + * + * Intent (96 bytes): id 32, supersedes 32 (zero = none), kind u32, state u8, + * reserved 3, since u64 (sequence of the subject object that created it), + * payload 2 x u64. id = SHA-256("AIENOS_INTENT_v0:" || agent || kind LE32 || + * since LE64 || payload[0] LE64 || payload[1] LE64), checked on decode. + * Rules: at most one ACTIVE intent per (kind, payload[0]) slot; a SUPERSEDED + * intent is named by exactly one newer intent's `supersedes`; `supersedes` + * must name an intent in the same table whose state is SUPERSEDED; + * since <= sequence. + * + * Knowledge (48 bytes): digest 32 (cx_digest of a CX_K_PROMOTION record), + * record u64 (its id in that journal, informative), since u64. + * + * Kinds of standing intent in v0: CS_INTENT_GOAL_LATENCY, the one goal shape + * the resident AIEN faculty reads today (omega rx_aien.h: regime, target ns + * per call). payload[0] = regime, payload[1] = target ns. */ +#ifndef CK_CONTINUITY_SUBJECT_H +#define CK_CONTINUITY_SUBJECT_H + +#include <stddef.h> +#include <stdint.h> + +#include "continuity_codec.h" +#include "continuity_commit.h" +#include "continuity_resolve.h" + +#if defined(CK_HARDWARE_STAGING) && \ + (defined(CS_MUTANT_ACCEPT_FOREIGN) || defined(CS_MUTANT_ACCEPT_TWO_ACTIVE) || \ + defined(CS_MUTANT_SKIP_INTENT_ID) || defined(CS_MUTANT_ACCEPT_FORK)) +#error "continuity subject mutants are TEST-only and refused under CK_HARDWARE_STAGING" +#endif + +#define CC_KIND_SUBJECT 24u + +#define CS_MAX_INTENTS 64u +#define CS_MAX_KNOWLEDGE 64u +#define CS_INTENT_BYTES 96u +#define CS_KNOWLEDGE_BYTES 48u +#define CS_FIXED_BODY 184u +#define CS_MAX_BYTES (CC_HEADER_BYTES + CS_FIXED_BODY + CS_MAX_INTENTS * CS_INTENT_BYTES + \ + CS_MAX_KNOWLEDGE * CS_KNOWLEDGE_BYTES) + +#define CS_ORIGIN_OPERATOR 1u +#define CS_ORIGIN_PROMOTED 2u + +#define CS_INTENT_GOAL_LATENCY 1u + +#define CS_ACTIVE 1u +#define CS_INACTIVE 2u +#define CS_SUPERSEDED 3u + +struct cs_intent { + uint8_t id[32]; + uint8_t supersedes[32]; /* zero = none */ + uint32_t kind; + uint8_t state; /* CS_ACTIVE / CS_INACTIVE / CS_SUPERSEDED */ + uint64_t since; + uint64_t payload[2]; +}; + +struct cs_knowledge { + uint8_t digest[32]; + uint64_t record; + uint64_t since; +}; + +struct cs_subject { + uint8_t root[32]; + uint8_t agent[32]; + uint8_t previous[32]; + uint64_t sequence; + uint8_t cortex[32]; + uint8_t provenance[32]; + uint8_t origin; + uint32_t n_intents; + uint32_t n_knowledge; + struct cs_intent in[CS_MAX_INTENTS]; + struct cs_knowledge kn[CS_MAX_KNOWLEDGE]; +}; + +/* ---- codec (CC_OK / CC_E_CORRUPT / CC_E_LIMIT / CC_E_ARG, as continuity_codec.h) ---- */ +void cs_intent_id(const uint8_t agent[32], uint32_t kind, uint64_t since, const uint64_t payload[2], + uint8_t out[32]); +int cs_subject_validate(const struct cs_subject *s, const char **why); +int cs_subject_encode(const struct cs_subject *s, uint8_t *out, size_t cap, size_t *len, + const char **why); +int cs_subject_decode(const uint8_t *in, size_t len, struct cs_subject *s, const char **why); +/* Logical ObjectId of the encoded object (kind 24, claim version 1): the + * subject digest. Content only; the same bytes give the same id on any + * machine, under any model, in any process. */ +int cs_subject_id(const uint8_t *bytes, size_t len, uint8_t out[32]); + +/* ---- editing (in memory; nothing here touches storage) ---- */ +/* First subject object for a resolved continuity view: sequence 1, no + * previous, no intents, no knowledge, Cortex unbound. */ +void cs_subject_genesis(struct cs_subject *s, const struct cr_view *v, const uint8_t provenance[32], + uint8_t origin); +/* Prepare the successor of a persisted object: previous = prev_id, + * sequence + 1, new provenance/origin; intents and knowledge are kept. */ +void cs_subject_advance(struct cs_subject *s, const uint8_t prev_id[32], + const uint8_t provenance[32], uint8_t origin); +/* Add an ACTIVE standing intent created at this object's sequence. If another + * ACTIVE intent holds the same (kind, payload[0]) slot it becomes SUPERSEDED + * and the new intent names it; the table stays sorted. CC_E_LIMIT when full. */ +int cs_subject_intend(struct cs_subject *s, uint32_t kind, const uint64_t payload[2], + uint8_t id_out[32], const char **why); +/* Retire an ACTIVE intent (state INACTIVE). CC_E_ARG if absent or not active. */ +int cs_subject_retire(struct cs_subject *s, const uint8_t id[32], const char **why); +/* Hold a Cortex promotion as subject knowledge; duplicates are refused. */ +int cs_subject_hold(struct cs_subject *s, const uint8_t digest[32], uint64_t record, + const char **why); +/* Bind (or re-bind) the Cortex lineage reference. */ +void cs_subject_bind_cortex(struct cs_subject *s, const uint8_t lineage[32]); +/* The ACTIVE intent in slot (kind, payload[0]); NULL if none. */ +const struct cs_intent *cs_subject_active(const struct cs_subject *s, uint32_t kind, uint64_t slot); + +/* ---- resolver / committer over the continuity source and sink ---- */ +#define CS_RESOLVED 0 /* out holds the head of the chain; out_id its ObjectId */ +#define CS_ABSENT 1 /* the Store has a root but no subject object: not minted */ +/* Other returns are CR_CORRUPT, CR_LIMIT, CR_STORE, CR_E_ARG (continuity_resolve.h). */ +int cs_resolve(const struct cr_source *src, struct cr_work *w, const struct cr_view *v, + struct cs_subject *out, uint8_t out_id[32], const char **why, int *store_rc); +/* Encode and commit one subject object in its own Store transaction. The + * object must validate and must continue the chain the caller resolved: + * cs_resolve is run first; its head id must equal s->previous (or the Store + * must be ABSENT and s->sequence 1). Returns CS_RESOLVED with out_id, or + * CR_CORRUPT / CR_LIMIT / CR_STORE / CR_E_ARG; nothing is written on refusal. */ +int cs_commit(const struct cr_source *src, const struct cr_sink *snk, struct cr_work *w, + const struct cr_view *v, const struct cs_subject *s, uint8_t out_id[32], + const char **why, int *store_rc); + +#endif diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 538aa9f7..bb524bb0 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T16:43:31Z", + "compiled_at": "2026-10-05T00:51:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:451e82cfde801fae5f0062cba5a86d572636bed23d5ce4cdd3529c1ce28ba417", + "digest": "sha256:2becacc3ed46f19dcd0a7b91acef18c77abb4e2f1231204a1ab02d74b244e80c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 22, - "generated_at_commit": "a93e27b8a26d45330e58e1b8df1b35faf7146886", - "source_tree": "sha256:2de681613401aebe0d840c4e2596b92be15f9b5de9bcf47b1988acfe3a2f02f1" + "files": 23, + "generated_at_commit": "fbbfc9c90b5609cdd94a327fad3503366e1b11b1", + "source_tree": "sha256:ec4f10b297f5d783fafcc54204ce43d28a029c6ac911cfc1eac4f51de20740eb" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_continuity_subject.c b/native/kernel/tests/test_continuity_subject.c new file mode 100644 index 00000000..f4998aea --- /dev/null +++ b/native/kernel/tests/test_continuity_subject.c @@ -0,0 +1,755 @@ +/* test_continuity_subject.c -- host tests of svc/continuity_subject.c + * (ALLEN subject state, kind 24; ARCH-0035 / OS-0018, both PROPOSED). + * + * No arguments: the unit suite. + * 1. Known answer: the genesis subject object's bytes, written out by hand + * from the layout in continuity_subject.h, and its logical ObjectId + * computed by store_v1's independent twin (sv1_object_id). + * 2. Round trips; deterministic re-encode (same bytes, same id). + * 3. One refusal per rule (magic, version, reserved, length, truncation, + * trailing bytes, zero root/agent/provenance, origin, bounds, intent id + * derivation, slot rule, supersession rules, ordering, since bounds). + * 4. Standing intents: intend, supersede (one active per slot), retire. + * 5. Resolve over a fake source: ABSENT, head of a chain, fork, gap, broken + * link, foreign root/agent (ALLEN-G1 identity binding). + * 6. Model independence (ALLEN-G4, structural): the encoding has no field + * for a model; two subjects that differ only in an out-of-band "model" + * value have byte-identical objects and ids. + * 7. Real sealed Store (native/store, M5 envelopes) in a file: provision, + * commit genesis, commit a successor with one intent, close, reopen, + * resolve the same head (in-process persistence). + * + * `--write <img>` and `--restore <img> <id-hex>`: ALLEN-G2 restart proof in + * two processes (Makefile test-continuity-subject-restart). The writer + * provisions, commits a subject with one standing intent, prints the head id + * and exits; the restorer is a fresh process that opens the same image, + * resolves, and checks the subject, the intent and the id. The negative + * control for G2 lives where the pre-ALLEN goal lives (omega); here the + * structural half: cr_view (the pre-ALLEN continuity view) carries no intent. + * + * Mutants (make continuity-subject-mutants) must each turn this test FAIL. */ +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "ck_test.h" +#include "continuity_commit.h" +#include "continuity_resolve_sealed.h" +#include "continuity_subject.h" +#include "disk_file.h" +#include "store_v1.h" +#include "../argus/sha256.h" + +#define CK_(c, ...) \ + do { \ + ck_t_run++; \ + if (!(c)) { \ + ck_t_fail++; \ + printf(" FAIL %s:%d: ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } \ + } while (0) + +static const char *why; +static struct cs_subject S, S2, S3; +static uint8_t buf[CC_MAX_OBJECT_BYTES + 64], buf2[CC_MAX_OBJECT_BYTES + 64]; +static struct cr_view V; +static struct cr_work W; + +static void fill(uint8_t *d, size_t n, uint8_t v) +{ + memset(d, v, n); +} + +static void hex(const uint8_t *b, size_t n, char *out) +{ + static const char *h = "0123456789abcdef"; + for (size_t i = 0; i < n; i++) { + out[2 * i] = h[b[i] >> 4]; + out[2 * i + 1] = h[b[i] & 15]; + } + out[2 * n] = 0; +} + +static int unhex(const char *s, uint8_t *out, size_t n) +{ + for (size_t i = 0; i < n; i++) { + unsigned v; + if (sscanf(s + 2 * i, "%2x", &v) != 1) return -1; + out[i] = (uint8_t)v; + } + return 0; +} + +/* A resolved view for a test agent: root id and agent id as the resolver + * would hand them over. */ +static void view(struct cr_view *v, uint8_t root_byte, uint8_t agent_byte) +{ + memset(v, 0, sizeof *v); + fill(v->root_id, 32, root_byte); + fill(v->root.agent_id, 32, agent_byte); +} + +static const uint8_t PROV[32] = {0xaa, 0xbb, 0xcc, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}; + +/* ---- 1. known answer ---- */ +static void t_known_answer(void) +{ + size_t len = 0; + uint8_t id[32], id2[32]; + uint8_t exp[CC_HEADER_BYTES + CS_FIXED_BODY]; + size_t at = 0; + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode genesis: %s", why); + CK_(len == CC_HEADER_BYTES + CS_FIXED_BODY, "genesis is header + fixed body (%zu)", len); + /* By hand from continuity_subject.h. */ + memcpy(exp + at, "AIENSUBJ", 8); + at += 8; + exp[at++] = 0; exp[at++] = 0; /* format_version 0 */ + exp[at++] = 0; exp[at++] = 0; /* reserved */ + exp[at++] = (uint8_t)CS_FIXED_BODY; exp[at++] = 0; exp[at++] = 0; exp[at++] = 0; /* body 184 */ + fill(exp + at, 32, 0x11); at += 32; /* root */ + fill(exp + at, 32, 0x22); at += 32; /* agent */ + fill(exp + at, 32, 0); at += 32; /* previous */ + exp[at++] = 1; fill(exp + at, 7, 0); at += 7; /* sequence 1 */ + fill(exp + at, 32, 0); at += 32; /* cortex unbound */ + memcpy(exp + at, PROV, 32); at += 32; /* provenance */ + exp[at++] = CS_ORIGIN_OPERATOR; fill(exp + at, 7, 0); at += 7; + fill(exp + at, 8, 0); at += 8; /* n_intents, n_knowledge, reserved */ + CK_(at == sizeof exp, "hand layout is %zu bytes", at); + CK_(memcmp(exp, buf, sizeof exp) == 0, "genesis bytes match the hand layout"); + CK_(cs_subject_id(buf, len, id) == CC_OK, "subject id"); + CK_(sv1_object_id(CC_KIND_SUBJECT, CC_STORE_OBJECT_VERSION, buf, len, id2) == 0, "sv1 twin"); + CK_(memcmp(id, id2, 32) == 0, "subject id is the Store v1 logical ObjectId (kind 24, version 1)"); +} + +/* ---- 2. round trips ---- */ +static void t_round_trip(void) +{ + size_t len = 0, len2 = 0; + uint8_t a[32], b[32], k[32]; + uint64_t p[2] = {7, 1000}; + uint64_t q[2] = {9, 2000}; + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_OK, "intend: %s", why); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, q, b, &why) == CC_OK, "intend 2: %s", why); + fill(k, 32, 0x5c); + CK_(cs_subject_hold(&S, k, 17, &why) == CC_OK, "hold: %s", why); + fill(k, 32, 0x3c); + CK_(cs_subject_hold(&S, k, 18, &why) == CC_OK, "hold 2: %s", why); + fill(k, 32, 0x3c); + CK_(cs_subject_hold(&S, k, 18, &why) == CC_E_ARG, "duplicate knowledge refused"); + fill(k, 32, 0x77); + cs_subject_bind_cortex(&S, k); + CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode: %s", why); + CK_(len == CC_HEADER_BYTES + CS_FIXED_BODY + 2 * CS_INTENT_BYTES + 2 * CS_KNOWLEDGE_BYTES, "len %zu", len); + CK_(cs_subject_decode(buf, len, &S2, &why) == CC_OK, "decode: %s", why); + CK_(memcmp(&S, &S2, sizeof S) == 0, "round trip equal"); + CK_(cs_subject_encode(&S2, buf2, sizeof buf2, &len2, &why) == CC_OK, "re-encode"); + CK_(len == len2 && memcmp(buf, buf2, len) == 0, "re-encode is byte identical"); + CK_(S2.n_intents == 2 && S2.n_knowledge == 2, "counts"); + CK_(memcmp(S2.kn[0].digest, S2.kn[1].digest, 32) < 0, "knowledge sorted"); + CK_(memcmp(S2.in[0].id, S2.in[1].id, 32) < 0, "intents sorted"); + CK_(cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 7) && cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 9), + "both slots active"); +} + +/* ---- 3. refusals ---- */ +static void expect_refuse(const uint8_t *b, size_t len, int cls, const char *text) +{ + int rc = cs_subject_decode(b, len, &S3, &why); + CK_(rc == cls, "%s: class %d (why=%s)", text, rc, why ? why : "-"); + CK_(why && strcmp(why, text) == 0, "%s: reason is '%s'", text, why ? why : "-"); +} + +static void t_refusals(void) +{ + size_t len = 0; + uint64_t p[2] = {7, 1000}; + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); + CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode base"); + /* header */ + memcpy(buf2, buf, len); + buf2[0] ^= 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "bad magic"); + memcpy(buf2, buf, len); + buf2[8] = 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "unsupported continuity format version"); + memcpy(buf2, buf, len); + buf2[10] = 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero header reserved"); + memcpy(buf2, buf, len); + buf2[12] ^= 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "body length mismatch"); + expect_refuse(buf, len - 1, CC_E_CORRUPT, "body length mismatch"); + memcpy(buf2, buf, len); + sv1_put32(buf2 + 12, (uint32_t)(len - CC_HEADER_BYTES + 1)); + expect_refuse(buf2, len + 1, CC_E_CORRUPT, "trailing bytes"); + /* truncated inside the intents: the length field is made to agree so the + * one bounds check is what refuses it */ + memcpy(buf2, buf, len); + sv1_put32(buf2 + 12, (uint32_t)(len - CC_HEADER_BYTES - 10)); + expect_refuse(buf2, len - 10, CC_E_CORRUPT, "truncated object"); + expect_refuse(buf, 5, CC_E_CORRUPT, "truncated object"); + /* body rules */ + memcpy(buf2, buf, len); + fill(buf2 + 16, 32, 0); + expect_refuse(buf2, len, CC_E_CORRUPT, "zero root id"); + memcpy(buf2, buf, len); + fill(buf2 + 48, 32, 0); + expect_refuse(buf2, len, CC_E_CORRUPT, "zero agent id"); + memcpy(buf2, buf, len); + buf2[80] = 1; /* previous nonzero on sequence 1 */ + expect_refuse(buf2, len, CC_E_CORRUPT, "genesis subject names a previous object"); + memcpy(buf2, buf, len); + buf2[112] = 2; /* sequence 2 without previous */ + expect_refuse(buf2, len, CC_E_CORRUPT, "subject object after the first has no previous"); + memcpy(buf2, buf, len); + buf2[112] = 0; + expect_refuse(buf2, len, CC_E_CORRUPT, "zero sequence"); + memcpy(buf2, buf, len); + fill(buf2 + 152, 32, 0); + expect_refuse(buf2, len, CC_E_CORRUPT, "zero provenance"); + memcpy(buf2, buf, len); + buf2[184] = 9; + expect_refuse(buf2, len, CC_E_CORRUPT, "unknown subject origin"); + memcpy(buf2, buf, len); + buf2[185] = 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); + memcpy(buf2, buf, len); + sv1_put16(buf2 + 192, CS_MAX_INTENTS + 1); + expect_refuse(buf2, len, CC_E_LIMIT, "too many standing intents"); + memcpy(buf2, buf, len); + sv1_put16(buf2 + 194, CS_MAX_KNOWLEDGE + 1); + expect_refuse(buf2, len, CC_E_LIMIT, "too many knowledge references"); + memcpy(buf2, buf, len); + buf2[197] = 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); + /* intent rules (intent starts at 200: id 200, supersedes 232, kind 264, + * state 268, reserved 269, since 272, payload 280) */ + memcpy(buf2, buf, len); + buf2[200] ^= 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "intent id does not derive from its content"); + memcpy(buf2, buf, len); + buf2[268] = 7; + expect_refuse(buf2, len, CC_E_CORRUPT, "unknown intent state"); + memcpy(buf2, buf, len); + buf2[264] = 2; + expect_refuse(buf2, len, CC_E_CORRUPT, "unknown intent kind"); + memcpy(buf2, buf, len); + buf2[269] = 1; + expect_refuse(buf2, len, CC_E_CORRUPT, "nonzero reserved bytes"); + memcpy(buf2, buf, len); + buf2[272] = 2; /* since 2 > sequence 1 (id no longer derives either; since is checked first) */ + expect_refuse(buf2, len, CC_E_CORRUPT, "intent created outside the subject chain"); + memcpy(buf2, buf, len); + memcpy(buf2 + 232, buf2 + 200, 32); /* supersedes itself */ + expect_refuse(buf2, len, CC_E_CORRUPT, "intent supersedes itself"); + memcpy(buf2, buf, len); + buf2[232] = 1; /* supersedes an absent intent */ + expect_refuse(buf2, len, CC_E_CORRUPT, "superseded intent is absent"); + /* K-1 cap on decode */ + CK_(cs_subject_decode(buf, CC_MAX_OBJECT_BYTES + 1, &S3, &why) == CC_E_LIMIT, "cap on decode"); + /* validate-side rules that the editor cannot produce */ + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); + S.in[0].state = CS_SUPERSEDED; /* superseded by nobody */ + CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && + strcmp(why, "superseded intent not named by exactly one successor") == 0, + "orphan superseded: %s", why); + S.in[0].state = CS_ACTIVE; + S.n_intents = CS_MAX_INTENTS + 1; + CK_(cs_subject_validate(&S, &why) == CC_E_LIMIT, "intent bound"); + S.n_intents = 1; + S.kn[0].since = 1; + fill(S.kn[0].digest, 32, 0x10); + S.kn[1].since = 1; + fill(S.kn[1].digest, 32, 0x10); + S.n_knowledge = 2; + CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && + strcmp(why, "knowledge not strictly ascending by digest") == 0, + "knowledge order: %s", why); + S.n_knowledge = 0; + CK_(cs_subject_encode(&S, buf, 10, &len, &why) == CC_E_ARG, "small output buffer"); + CK_(cs_subject_encode(NULL, buf, sizeof buf, &len, &why) == CC_E_ARG, "null"); + CK_(cs_subject_decode(buf, 100, NULL, &why) == CC_E_ARG, "null out"); +} + +/* ---- 4. intents: supersession and retirement ---- */ +static void t_intents(void) +{ + uint8_t a[32], b[32]; + uint64_t p[2] = {7, 1000}; + uint64_t p2[2] = {7, 500}; + size_t len = 0; + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_OK, "intend a"); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, a, &why) == CC_E_ARG, "same intent twice refused"); + /* a successor object replaces the goal in the same slot */ + fill(b, 32, 0x99); + cs_subject_advance(&S, b, PROV, CS_ORIGIN_OPERATOR); + CK_(S.sequence == 2 && memcmp(S.previous, b, 32) == 0, "advance"); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p2, b, &why) == CC_OK, "intend b: %s", why); + CK_(S.n_intents == 2, "two intents"); + { + const struct cs_intent *act = cs_subject_active(&S, CS_INTENT_GOAL_LATENCY, 7); + CK_(act && memcmp(act->id, b, 32) == 0 && act->payload[1] == 500, "b is the active one"); + CK_(memcmp(act->supersedes, a, 32) == 0, "b names a"); + } + CK_(cs_subject_encode(&S, buf, sizeof buf, &len, &why) == CC_OK, "encode after supersede: %s", why); + CK_(cs_subject_decode(buf, len, &S2, &why) == CC_OK, "decode after supersede: %s", why); + /* two active in one slot is refused on decode (mutant ACCEPT_TWO_ACTIVE) */ + for (uint32_t i = 0; i < S2.n_intents; i++) + if (memcmp(S2.in[i].id, a, 32) == 0) S2.in[i].state = CS_ACTIVE; + for (uint32_t i = 0; i < S2.n_intents; i++) + if (memcmp(S2.in[i].id, b, 32) == 0) memset(S2.in[i].supersedes, 0, 32); + CK_(cs_subject_validate(&S2, &why) == CC_E_CORRUPT && strcmp(why, "two active intents in one slot") == 0, + "two active refused: %s", why); + /* retire */ + CK_(cs_subject_retire(&S, b, &why) == CC_OK, "retire b"); + CK_(cs_subject_active(&S, CS_INTENT_GOAL_LATENCY, 7) == NULL, "slot empty after retire"); + CK_(cs_subject_retire(&S, b, &why) == CC_E_ARG, "retire twice refused"); + CK_(cs_subject_retire(&S, a, &why) == CC_E_ARG, "retire a superseded intent refused"); + CK_(cs_subject_validate(&S, &why) == CC_OK, "still valid: %s", why); + /* supersedes a newer intent: swap since values by hand */ + { + uint32_t ia = 0, ib = 0; + for (uint32_t i = 0; i < S.n_intents; i++) { + if (memcmp(S.in[i].id, a, 32) == 0) ia = i; + if (memcmp(S.in[i].id, b, 32) == 0) ib = i; + } + S.in[ia].since = 2; + S.in[ib].since = 1; + /* ids no longer derive, but since-order is checked for b before its id? No: + * id derivation is checked per intent before supersession, so make the + * check order explicit by recomputing ids. */ + cs_intent_id(S.agent, S.in[ia].kind, S.in[ia].since, S.in[ia].payload, S.in[ia].id); + cs_intent_id(S.agent, S.in[ib].kind, S.in[ib].since, S.in[ib].payload, S.in[ib].id); + memcpy(S.in[ib].supersedes, S.in[ia].id, 32); + /* the table may now be out of order; sort by id (two elements) */ + if (memcmp(S.in[0].id, S.in[1].id, 32) > 0) { + struct cs_intent t = S.in[0]; + S.in[0] = S.in[1]; + S.in[1] = t; + } + CK_(cs_subject_validate(&S, &why) == CC_E_CORRUPT && strcmp(why, "intent supersedes a newer intent") == 0, + "supersedes newer refused: %s", why); + } +} + +/* ---- 5. resolve over a fake source ---- */ +struct fake { + uint32_t n; + uint16_t kind[8], version[8]; + uint8_t *bytes[8]; + size_t len[8]; +}; +static uint32_t fk_count(void *c) { return ((struct fake *)c)->n; } +static int fk_entry(void *c, uint32_t i, uint16_t *k, uint16_t *v) +{ + struct fake *f = c; + *k = f->kind[i]; + *v = f->version[i]; + return 0; +} +static int fk_read(void *c, uint32_t i, uint8_t *out, size_t cap, size_t *len) +{ + struct fake *f = c; + *len = f->len[i]; + if (*len <= cap) memcpy(out, f->bytes[i], *len); + return 0; +} +static int fk_mount(void *c) +{ + (void)c; + return CR_MOUNT_VALID; +} +static uint8_t obj[8][CC_MAX_OBJECT_BYTES]; +static size_t objlen[8]; +static uint8_t objid[8][32]; + +static uint32_t fk_add(struct fake *f, uint16_t kind, const struct cs_subject *s) +{ + uint32_t i = f->n++; + CK_(cs_subject_encode(s, obj[i], sizeof obj[i], &objlen[i], &why) == CC_OK, "fk encode: %s", why); + CK_(cs_subject_id(obj[i], objlen[i], objid[i]) == CC_OK, "fk id"); + f->kind[i] = kind; + f->version[i] = CC_STORE_OBJECT_VERSION; + f->bytes[i] = obj[i]; + f->len[i] = objlen[i]; + return i; +} + +static void t_resolve(void) +{ + struct fake f; + struct cr_source src = {&f, fk_count, fk_entry, fk_read, fk_mount, NULL}; + uint8_t id[32]; + uint64_t p[2] = {7, 1000}; + int rc; + view(&V, 0x11, 0x22); + memset(&f, 0, sizeof f); + /* absent */ + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CS_ABSENT, "no subject object: ABSENT (%d)", rc); + /* a foreign-kind object is ignored */ + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + fk_add(&f, 99, &S); + CK_(cs_resolve(&src, &W, &V, &S2, id, &why, NULL) == CS_ABSENT, "other kinds ignored"); + f.n = 0; + /* chain of two */ + fk_add(&f, CC_KIND_SUBJECT, &S); + cs_subject_advance(&S, objid[0], PROV, CS_ORIGIN_PROMOTED); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); + fk_add(&f, CC_KIND_SUBJECT, &S); + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CS_RESOLVED, "chain of two resolves (%d: %s)", rc, why ? why : "-"); + CK_(S2.sequence == 2 && S2.n_intents == 1 && memcmp(id, objid[1], 32) == 0, "head is sequence 2"); + /* order independence: same objects, reversed */ + { + uint8_t *tb = f.bytes[0]; f.bytes[0] = f.bytes[1]; f.bytes[1] = tb; + size_t tl = f.len[0]; f.len[0] = f.len[1]; f.len[1] = tl; + rc = cs_resolve(&src, &W, &V, &S3, id, &why, NULL); + CK_(rc == CS_RESOLVED && memcmp(&S2, &S3, sizeof S2) == 0, "resolution does not depend on catalog order"); + tb = f.bytes[0]; f.bytes[0] = f.bytes[1]; f.bytes[1] = tb; + tl = f.len[0]; f.len[0] = f.len[1]; f.len[1] = tl; + } + /* fork: a second sequence-2 object with a different intent */ + { + uint64_t q[2] = {7, 999}; + cs_subject_decode(obj[0], objlen[0], &S3, &why); + cs_subject_advance(&S3, objid[0], PROV, CS_ORIGIN_PROMOTED); + CK_(cs_subject_intend(&S3, CS_INTENT_GOAL_LATENCY, q, NULL, &why) == CC_OK, "intend fork"); + fk_add(&f, CC_KIND_SUBJECT, &S3); + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "subject chain fork") == 0, "fork refused (%d: %s)", rc, why); + f.n = 2; + } + /* gap: sequence 1 and 3 */ + { + cs_subject_decode(obj[1], objlen[1], &S3, &why); + cs_subject_advance(&S3, objid[1], PROV, CS_ORIGIN_PROMOTED); + fk_add(&f, CC_KIND_SUBJECT, &S3); + f.bytes[1] = f.bytes[2]; f.len[1] = f.len[2]; f.n = 2; + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "subject chain gap") == 0, "gap refused (%d: %s)", rc, why); + f.bytes[1] = obj[1]; f.len[1] = objlen[1]; + } + /* broken link: sequence 2 names a previous that is not sequence 1's id */ + { + cs_subject_decode(obj[1], objlen[1], &S3, &why); + S3.previous[0] ^= 1; + uint32_t k = fk_add(&f, CC_KIND_SUBJECT, &S3); + f.bytes[1] = f.bytes[k]; f.len[1] = f.len[k]; f.n = 2; + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "subject chain link broken") == 0, "broken link refused (%d: %s)", rc, why); + f.bytes[1] = obj[1]; f.len[1] = objlen[1]; + } + /* ALLEN-G1 identity binding: another agent's view, or a tampered agent + * id, must not resolve this subject */ + { + struct cr_view other; + view(&other, 0x11, 0x23); + rc = cs_resolve(&src, &W, &other, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "subject object belongs to another agent") == 0, + "another agent id refused (%d: %s)", rc, why ? why : "-"); + view(&other, 0x12, 0x22); + rc = cs_resolve(&src, &W, &other, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "subject object belongs to another agent") == 0, + "another root refused (%d: %s)", rc, why ? why : "-"); + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CS_RESOLVED, "the right view still resolves"); + } + /* undecodable object in the catalog: CORRUPT, never skipped */ + { + obj[1][0] ^= 1; + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT && strcmp(why, "bad magic") == 0, "undecodable refused (%d: %s)", rc, why); + obj[1][0] ^= 1; + } + /* unknown claim version */ + { + f.version[1] = 2; + rc = cs_resolve(&src, &W, &V, &S2, id, &why, NULL); + CK_(rc == CR_CORRUPT, "unknown claim version refused"); + f.version[1] = CC_STORE_OBJECT_VERSION; + } +} + +/* ---- 6. model independence (structural) ---- */ +static void t_model_independence(void) +{ + size_t la = 0, lb = 0; + uint8_t ia[32], ib[32]; + uint64_t p[2] = {7, 1000}; + uint8_t model_a[32], model_b[32]; + fill(model_a, 32, 0xa1); + fill(model_b, 32, 0xb2); + view(&V, 0x11, 0x22); + cs_subject_genesis(&S, &V, PROV, CS_ORIGIN_OPERATOR); + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); + /* The "model" is the boot handoff's model_sha256 (native/boot/handoff.h), + * outside this object. There is nowhere to put it: encode takes no such + * input. Two runs that serve different models produce one subject. */ + (void)model_a; + (void)model_b; + CK_(cs_subject_encode(&S, buf, sizeof buf, &la, &why) == CC_OK, "encode under model a"); + CK_(cs_subject_id(buf, la, ia) == CC_OK, "id a"); + CK_(cs_subject_encode(&S, buf2, sizeof buf2, &lb, &why) == CC_OK, "encode under model b"); + CK_(cs_subject_id(buf2, lb, ib) == CC_OK, "id b"); + CK_(la == lb && memcmp(buf, buf2, la) == 0 && memcmp(ia, ib, 32) == 0, + "subject bytes and id do not depend on which model is served"); + /* and no byte of either model digest appears in the object */ + { + int hit = 0; + for (size_t i = 0; i + 4 <= la; i++) + if (memcmp(buf + i, model_a, 4) == 0 || memcmp(buf + i, model_b, 4) == 0) hit = 1; + CK_(!hit, "no model digest bytes inside the subject object"); + } +} + +/* ---- 7. real sealed Store in a file ---- */ +#define A_UNITS 4u +#define S_UNITS 512u +static ss_workspace g_ws; +static ss_store g_s; +static disk_file g_f; +static disk_dev g_d; +static st_disk g_sd; +static st_dev g_sdev; +static ts_device g_tdev; +static ss_keys g_k; +static const uint32_t g_bs = 4096u; +static char g_path[256]; +static struct cr_work g_w; +static struct cr_txwork g_tw; +static struct cr_view g_v; +static struct cr_source g_src; +static struct cr_sink g_snk; +static struct cr_sealed_sink g_sk; +static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, + 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; +static int g_open; + +static void dev_close(void) +{ + if (g_open) disk_file_close(&g_f); + g_open = 0; +} +static int dev_open(int create) +{ + dev_close(); + uint32_t bpu = 4096u / g_bs; + if (create) unlink(g_path); + memset(&g_f, 0, sizeof g_f); + if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + S_UNITS) * bpu, create) != 0) return -1; + g_open = 1; + if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; + ss_ts_device(&g_d, &g_tdev); + return 0; +} +static void make_keys(void) +{ + uint8_t kvol[32]; + memset(kvol, 0x77, 32); + m5_subkeys sk; + m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); + memset(&g_k, 0, sizeof g_k); + g_k.identity_class = M5_ID_TEST; + g_k.key_generation = 1; + memcpy(g_k.k_root_auth, sk.k_root_auth, 32); + memcpy(g_k.k_domain, sk.k_artifact, 32); + m5_subkeys_wipe(&sk); +} +static void bind_store(void) +{ + g_sk.s = &g_s; + g_sk.hook = NULL; + g_sk.hook_arg = NULL; + cr_bind_sealed(&g_src, &g_s); + cr_bind_sealed_sink(&g_snk, &g_sk); +} +/* mocked RNDR (as test_continuity_commit.c) */ +struct mock { + int present; + uint64_t ctr; +}; +static struct mock g_m; +static struct ck_rng_ops g_ops; +static struct ck_rng g_rng; +static int m_present(void *c) { return ((struct mock *)c)->present; } +static int m_read(void *c, uint64_t *out) +{ + struct mock *m = c; + uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; + z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; + *out = z ^ (z >> 31); + return 1; +} +static void rng_init(uint64_t seed) +{ + g_m.present = 1; + g_m.ctr = seed; + g_ops.present = m_present; + g_ops.read = m_read; + g_ops.ctx = &g_m; + memset(&g_rng, 0, sizeof g_rng); + (void)ck_rng_probe(&g_rng, &g_ops); +} + +static int store_fresh(void) +{ + if (dev_open(1) != 0) return -1; + if (ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) != 0) return -2; + if (ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) != 0) return -3; + bind_store(); + return 0; +} +static int store_reopen(void) +{ + if (dev_open(0) != 0) return -1; + if (ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) != 0) return -3; + bind_store(); + return 0; +} + +/* Provision the identity, then commit the genesis subject and one successor + * carrying a standing intent. Returns the head id. */ +static int write_subject(uint8_t head_id[32], uint64_t *agent_lo) +{ + int rc = 0, store_rc = 0; + uint8_t gid[32]; + uint64_t p[2] = {7, 1000}; + rc = cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &store_rc); + CK_(rc == CR_RESOLVED, "provision (%d: %s, store %d)", rc, why ? why : "-", store_rc); + if (rc != CR_RESOLVED) return -1; + CK_(cs_resolve(&g_src, &g_w, &g_v, &S2, gid, &why, &store_rc) == CS_ABSENT, "fresh store: subject ABSENT"); + cs_subject_genesis(&S, &g_v, PROV, CS_ORIGIN_OPERATOR); + rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, gid, &why, &store_rc); + CK_(rc == CS_RESOLVED, "commit genesis (%d: %s, store %d)", rc, why ? why : "-", store_rc); + /* a stale successor (wrong previous) is refused and writes nothing */ + cs_subject_advance(&S, gid, PROV, CS_ORIGIN_OPERATOR); + S.previous[0] ^= 1; + rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, head_id, &why, &store_rc); + CK_(rc == CR_CORRUPT && strcmp(why, "subject object does not continue the resolved chain") == 0, + "stale successor refused (%d: %s)", rc, why ? why : "-"); + S.previous[0] ^= 1; + CK_(cs_subject_intend(&S, CS_INTENT_GOAL_LATENCY, p, NULL, &why) == CC_OK, "intend"); + rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S, head_id, &why, &store_rc); + CK_(rc == CS_RESOLVED, "commit successor (%d: %s, store %d)", rc, why ? why : "-", store_rc); + /* a second genesis is refused: the chain already exists */ + cs_subject_genesis(&S3, &g_v, PROV, CS_ORIGIN_OPERATOR); + rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S3, gid, &why, &store_rc); + CK_(rc == CR_CORRUPT, "second genesis refused (%d: %s)", rc, why ? why : "-"); + memcpy(agent_lo, g_v.root.agent_id, 8); + return 0; +} + +static void check_restored(const uint8_t want_id[32]) +{ + int rc, store_rc = 0, committed = 0; + uint8_t id[32]; + rc = cr_resume(&g_src, &g_snk, &g_w, &g_tw, &g_v, &committed, &why, &store_rc); + CK_(rc == CR_RESOLVED && committed == 1, "resume (%d: %s)", rc, why ? why : "-"); + rc = cs_resolve(&g_src, &g_w, &g_v, &S2, id, &why, &store_rc); + CK_(rc == CS_RESOLVED, "subject resolves after reopen (%d: %s, store %d)", rc, why ? why : "-", store_rc); + CK_(memcmp(id, want_id, 32) == 0, "same subject id after reopen"); + CK_(S2.sequence == 2 && S2.n_intents == 1, "sequence 2 with one intent"); + CK_(memcmp(S2.agent, g_v.root.agent_id, 32) == 0 && memcmp(S2.root, g_v.root_id, 32) == 0, + "subject bound to the provisioned identity"); + { + const struct cs_intent *a = cs_subject_active(&S2, CS_INTENT_GOAL_LATENCY, 7); + CK_(a && a->payload[1] == 1000 && a->since == 2, "the standing intent came back"); + } +} + +static void t_sealed_store(void) +{ + uint8_t head[32]; + uint64_t lo = 0; + snprintf(g_path, sizeof g_path, "/tmp/ck_cs_test_%d.img", (int)getpid()); + rng_init(0x1234); + CK_(store_fresh() == 0, "fresh store"); + if (write_subject(head, &lo) == 0) { + dev_close(); + CK_(store_reopen() == 0, "reopen"); + check_restored(head); + /* a successor committed against a stale view (older chain head) is refused */ + { + int rc, store_rc = 0; + uint8_t id[32]; + cs_subject_genesis(&S3, &g_v, PROV, CS_ORIGIN_OPERATOR); + S3.sequence = 3; + memcpy(S3.previous, head, 32); + S3.previous[1] ^= 1; + rc = cs_commit(&g_src, &g_snk, &g_w, &g_v, &S3, id, &why, &store_rc); + CK_(rc == CR_CORRUPT, "wrong previous refused after reopen"); + } + } + dev_close(); + unlink(g_path); +} + +/* ---- two-process restart (ALLEN-G2) ---- */ +static int phase_write(const char *img) +{ + uint8_t head[32]; + uint64_t lo = 0; + char h[65]; + snprintf(g_path, sizeof g_path, "%s", img); + make_keys(); + rng_init(0x5eed); + CK_(store_fresh() == 0, "fresh store"); + if (write_subject(head, &lo) != 0) return ck_t_verdict("test_continuity_subject --write"); + dev_close(); + hex(head, 32, h); + printf("ALLEN subject id: %s\n", h); + printf("ALLEN agent: %016llx...\n", (unsigned long long)lo); + return ck_t_verdict("test_continuity_subject --write"); +} + +static int phase_restore(const char *img, const char *idhex) +{ + uint8_t want[32]; + snprintf(g_path, sizeof g_path, "%s", img); + make_keys(); + CK_(strlen(idhex) == 64 && unhex(idhex, want, 32) == 0, "id argument"); + CK_(store_reopen() == 0, "reopen in a fresh process"); + check_restored(want); + /* The pre-ALLEN continuity view has nowhere to keep an intent: its + * fields are root, manifest, branch table, Cortex count and memory + * digest (continuity_resolve.h struct cr_view). Structural half of G6. */ + CK_(sizeof(struct cr_view) == sizeof(g_v), "cr_view inspected"); + printf("ALLEN restored: sequence=%llu intents=%u regime=%llu target_ns=%llu\n", + (unsigned long long)S2.sequence, S2.n_intents, + (unsigned long long)(S2.n_intents ? S2.in[0].payload[0] : 0), + (unsigned long long)(S2.n_intents ? S2.in[0].payload[1] : 0)); + dev_close(); + return ck_t_verdict("test_continuity_subject --restore"); +} + +int main(int argc, char **argv) +{ + if (argc == 3 && strcmp(argv[1], "--write") == 0) return phase_write(argv[2]); + if (argc == 4 && strcmp(argv[1], "--restore") == 0) return phase_restore(argv[2], argv[3]); + make_keys(); + t_known_answer(); + t_round_trip(); + t_refusals(); + t_intents(); + t_resolve(); + t_model_independence(); + t_sealed_store(); + return ck_t_verdict("test_continuity_subject"); +}