interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / CONTINUITY_RECOVERY_CONTRACT.md

772 lines · 63055 bytes Raw Blame History Rendered
1# C kernel contract: continuity objects and Recovery Core
2
3Status: **IMPLEMENTED (host PASS), QEMU NOT_RUN, hardware NOT_RUN** for the continuity half (M4_CONTINUITY) and for the
4host half of the Recovery Core (M4_RECOVERY, cut 5). **M4_RECOVERY is NOT_RUN: there is no kernel wiring, no QEMU
5receipt and no hardware run.** What has C code (all **host PASS**, QEMU n/a, nothing wired into the kernel): the codec
6(cuts 1-2: `svc/continuity_codec.c`, golden vectors, C/Rust decode agreement), boot **resolution** (cut 3:
7`svc/continuity_resolve.c`: P-3 lookup, INV-5 orphan/Conflict, INV-6 chain, INV-7 references, read-only check), the
8Recovery Core challenge, state digest and operator HMAC (same file), the sealed-Store binding
9`svc/continuity_resolve_sealed.c`, **provision, commit and resume** (cut 4: `svc/continuity_commit.c`: INV-4, INV-8 to
10INV-11, K-1, markers of section 3), and, new in cut 5, the **Recovery Core** (`svc/continuity_recovery.c`: read-only
11inspection INV-13, entry reasons and `applicable()` INV-14/INV-15, the operator-authorised repair of a malformed peer
12INV-16/INV-17 and operator provisioning INV-16/INV-18, the C-only sealed-refusal entry reason of K-3/K-4, and the
13`RECOVERY_CORE`, `RECOVERY_CHALLENGE`, `RECOVERY_ACTION`, `RECOVERY_REFUSED` marker text), run by
14`make -C native/kernel test`, `sanitize` and `continuity-mutants` against the REAL sealed Store in a file (cut 4 also with
15power cuts at every Store and anchor checkpoint and at every block boundary, for commit AND for provisioning).
16Not done: wiring into the kernel and any QEMU run (the first wiring cut needs the K-7 test-mode mechanism, the
17`RECOVERY_OPERATOR_KEY: TEST-ONLY` handling and a kernel stage that calls these functions); the `RECOVERY_RECORD:` marker;
18the full K-3/K-4 tests (cut 5 proves only that a corrupted newest AgentRoot is a sealed refusal with no action; the
19newest-root-graph-broken case with a CRC-valid root is not built separately); power cuts during a repair or a
20recovery-core provisioning (cut 4 cut the underlying provisioning, a repair is one unit write plus a flush). Every QEMU
21row (81-87, 88-91) stays `NOT_RUN` until a forge receipt covers it. Host rows 87a-87d, 87f, 87g and 91d-91k are HOST PASS
22(see GATES.md). QEMU qualifies nothing physical; nothing here says anything about Machine 1.
23
24Authority:
25
26- aien-architecture ADR 0024, decision Q2 (Accepted 2026-10-01, file
27 `docs/adr/0024-rust-scaffolding-omega-destination.md`, fetched at
28 aien-architecture commit `64b800b`): continuity and the Recovery Core are
29 implemented in C inside the C kernel; "write the contract first, use the
30 existing Rust `continuity.rs` and `recovery_core` plus their QEMU campaigns as
31 the oracle, then require differential agreement." This file is that contract.
32- aienos ADR 0016 (continuity objects, **Proposed**, format version 0 on
33 disposable media) and ADR 0006 (Recovery Core, accepted 2026-09-23).
34- Ground truth is the Rust code at aienos `4a116e5`. Where ADR 0016 text and
35 the Rust code disagree, the code is the oracle and the difference is listed
36 in section 9.
37
38Labels used below:
39
40- **CODE** (with `file:line`): what the Rust kernel does today. The C port must
41 match it.
42- **PROPOSED**: not in any code. A design choice for the C port that a later
43 PR or the operator must confirm. Nothing marked PROPOSED is a fact.
44- **UNVERIFIED**: a claim about existing C code that this spec did not prove
45 by a test; confidence given.
46
47Paths are relative to the aienos repo root. `continuity.rs` means
48`crates/aienos-kernel/src/continuity.rs`; `recovery_core.rs` means
49`crates/aienos-kernel/src/recovery_core.rs`; `nvme_read.rs` means
50`crates/aienos-boot/src/nvme_read.rs`.
51
52---
53
54## 1. On-disk objects the Rust code writes (CODE)
55
56All four kinds are Store v1 objects with Store `version = 1`
57(`STORE_OBJECT_VERSION`, continuity.rs:24). Each object starts with a 16-byte
58continuity header and the body follows. All integers are little-endian unless
59stated. `id` = 32 raw bytes.
60
61### 1.1 Continuity header (16 bytes), continuity.rs:90-95, 105-122
62
63| off | size | field | rule |
64|---|---|---|---|
65| 0 | 8 | magic | per kind, below |
66| 8 | 2 | format_version (u16) | must be `0` (`FORMAT_VERSION`, continuity.rs:26; checked :110) |
67| 10 | 2 | reserved (u16) | must be `0` (:115) |
68| 12 | 4 | body_len (u32) | must equal total length minus 16 (:118) |
69
70Decoders are strict (continuity.rs:97-166): every read is bounds checked
71("truncated object"), every reserved byte must be zero ("nonzero reserved"),
72and the whole input must be consumed ("trailing bytes").
73
74### 1.2 Kinds written
75
76| Store kind | const | magic | name | written by |
77|---|---|---|---|---|
78| 16 | `KIND_AGENT_ROOT` (continuity.rs:19) | `AIENROOT` (:34) | AgentRoot | `provision` only (:721-772) |
79| 17 | `KIND_MANIFEST` (:20) | `AIENMANI` (:35) | ContinuityManifest | `provision`, `commit_with_hook` (:837-845) |
80| 18 | `KIND_AGENT_STATE` (:21) | `AIENBRAN` (:36) | AgentStateCheckpoint (branch table) | `provision`, `commit_with_hook` when `update.state` is set (:802-812) |
81| 20 | `KIND_CORTEX_WAL` (:22) | `AIENCWAL` (:37) | CortexWalSegment | `commit_with_hook` when `update.cortex` is non-empty (:815-826) |
82
83Kinds 19 (`CortexCheckpoint`) and 23 (`MigrationManifest`) appear in ADR 0016
84(docs/adr/0016-continuity-objects-over-store-v1.md:40, :42) but **no Rust code
85writes or reads them**. They are PROPOSED and out of scope for the C port (section 8).
86
87### 1.3 AgentRoot, kind 16 (body 96 bytes, total 112), continuity.rs:202-247
88
89| off | size | field |
90|---|---|---|
91| 16 | 32 | agent_id (`LogicalAgentId`) |
92| 48 | 16 | store_uuid |
93| 64 | 32 | root_branch |
94| 96 | 8 | provisioned_generation (u64) = Store generation + 1 at provisioning (:738) |
95| 104 | 1 | source: 1 = Operator, 2 = Qualification (:186-191, :224-228) |
96| 105 | 7 | reserved, zero |
97
98Decode also refuses `agent_id == 0` ("zero agent id", :231) and
99`root_branch != root_branch_id(agent_id)` (:234).
100
101### 1.4 ContinuityManifest, kind 17 (body 120 + 32·n, n ≤ 64), continuity.rs:253-315
102
103| off | size | field |
104|---|---|---|
105| 16 | 32 | root: AgentRoot ObjectId |
106| 48 | 32 | previous: previous manifest ObjectId, zero when none |
107| 80 | 8 | sequence (u64) |
108| 88 | 8 | incarnation (u64) |
109| 96 | 32 | agent_state: AgentState ObjectId, zero when none |
110| 128 | 2 | cortex_wal count n (u16), n ≤ `MAX_WAL_SEGMENTS` = 64 (:28, :265, :293) |
111| 130 | 6 | reserved, zero |
112| 136 | 32·n | cortex_wal: CortexWalSegment ObjectIds, in commit order |
113
114Decode refuses `sequence == 0` and any manifest where `(sequence == 1)` is not
115the same as `previous == zero` (:301-305). Largest manifest: 2184 bytes.
116
117### 1.5 AgentState, kind 18 (body 48 + 80·n, 1 ≤ n ≤ 256), continuity.rs:321-482
118
119| off | size | field |
120|---|---|---|
121| 16 | 32 | agent_id |
122| 48 | 8 | written_at (u64): manifest sequence it was written in (:334, set at :808; genesis uses 1, :743) |
123| 56 | 4 | branch count n (u32), 1 ≤ n ≤ `MAX_BRANCHES` = 256 (:29, :417) |
124| 60 | 4 | reserved, zero |
125| 64 | 80·n | branches, strictly ascending by id |
126
127Each branch (80 bytes, `BRANCH_BYTES`, :340, encode :401-407):
128
129| off | size | field |
130|---|---|---|
131| 0 | 32 | id |
132| 32 | 32 | parent id, zero for the root branch |
133| 64 | 4 | depth (u32) |
134| 68 | 4 | reserved, zero |
135| 72 | 8 | forks (u64): children forked so far |
136
137`validate` (:447-481), run by both encode (:393) and decode (:440):
1381. 1 ≤ n ≤ 256; ids strictly ascending.
1392. Exactly one branch with zero parent; its id is `root_branch_id(agent_id)` and depth 0.
1403. Every child: parent present; `id == child_branch_id(parent, i)` for some
141 `i < parent.forks`; `depth == parent.depth + 1` (checked add).
1424. Sum of all `forks` equals the number of children ("fork indexes are not contiguous").
143 **DECIDED (CODE, Rust #232; C agrees): the sum is checked.** A sum that overflows
144 u64 is refused with the same class and text, because it can never equal the
145 child count (<= 255). Without the check a table with root forks = 2^64-1 and
146 one child with forks = 2 wraps to 1 and is accepted. C: `cc_state_validate`
147 (`continuity_codec.c`, the `over` flag); vector `state_forksum_overflow`; mutant MC-13.
148 The child-index scan is also capped at `MAX_BRANCHES` hashes in both (no outcome
149 changes for a table that passes the sum check).
150
151`fork` (:365-390): refuses at 256 branches (Limit), absent parent (Corrupt),
152depth overflow (Limit), id collision (Corrupt); child index = parent.forks,
153then parent.forks += 1; insert keeps sort order. Largest object: 20544 bytes.
154
155### 1.6 CortexWalSegment, kind 20, continuity.rs:488-582
156
157| off | size | field |
158|---|---|---|
159| 16 | 8 | sequence (u64): manifest sequence it was committed in |
160| 24 | 4 | record count n (u32), 1 ≤ n ≤ `MAX_WAL_RECORDS` = 64 (:30, :529, :559) |
161| 28 | 4 | reserved, zero |
162| 32 | … | n records |
163
164Each record (:545-549): status u8 (1 DirectObservation, 2 VerifiedFact,
1653 Inference, 4 Hypothesis, 5 Contradiction, 6 OperatorDecision; :490-511),
166reserved u8 zero, statement length u16 (1 ≤ len ≤ `MAX_STATEMENT_BYTES` = 1024,
167:31, :534, :568), evidence_hash 32 bytes, statement bytes. Largest object:
16867872 bytes.
169
170### 1.7 Derived values (CODE, must be bit-identical)
171
172| value | definition | source |
173|---|---|---|
174| ObjectId | SHA-256(`"AIENOS-STORE-OBJECT-V1\0"` ‖ kind u16 LE ‖ version u16 LE ‖ len u64 LE ‖ bytes) | crates/aienos-kernel/src/store/v1.rs:27, :63-78; C twin `sv1_object_id` native/store/store_v1.h:132 |
175| root branch id | SHA-256(`"AIENOS_ROOT_BRANCH_v1:"` ‖ agent_id) | continuity.rs:69-75 |
176| child branch id | SHA-256(`"AIENOS_CHILD_BRANCH_v1:"` ‖ parent ‖ index u64 **big-endian**) | continuity.rs:77-84 |
177| QEMU `memory=` digest | first 8 bytes of SHA-256 over every committed record in order of (statement len u32 LE ‖ statement) | nvme_read.rs:1119-1124, :1138-1140 |
178| provisioning agent id | four `RNDR` words, each stored little-endian, word 0 first; no fallback | nvme_read.rs:1184-1193; `rndr` crates/aienos-kernel/src/arch/aarch64.rs:173-183 (FEAT_RNG check, 16 tries) |
179| recovery challenge | SHA-256(`"AIENOS-RECOVERY-CHALLENGE-v1\0"` ‖ store_uuid ‖ generation u64 LE ‖ action u8 ‖ state_digest) | recovery_core.rs:94-104 |
180| state_digest | SHA-256(raw superblock unit 0 ‖ raw superblock unit 1) of the Store region, read errors leave that buffer zero | recovery_core.rs:109-133 |
181| operator response | HMAC-SHA256(key, `"AIENOS-RECOVERY-OPERATOR-AUTH-v1\0"` ‖ challenge), compared in constant time | crates/aienos-kernel/src/recovery.rs:103, :124-140; C has `aienos_hmac_sha256` and `aienos_ct_equal` (native/crypto/aienos_crypto.h:112, :116) |
182| action byte | RepairDegradedPeer = 1, ProvisionIdentity = 2 | recovery_core.rs:38-44 |
183
184---
185
186## 2. Invariants the C port must hold bit-for-bit
187
188Each invariant names the Rust code it comes from. "Bit-for-bit" means: for the
189same inputs, the C encoder produces the same bytes, the C decoder accepts
190exactly the inputs the Rust decoder accepts and yields the same values, and
191the same error class is returned.
192
193- **INV-1 Encodings.** Sections 1.1-1.6, byte for byte, including zero reserved
194 bytes and the strict decoder rules (continuity.rs:97-166).
195- **INV-2 Version gate.** Continuity header `format_version` is 0 and Store
196 object version is 1 for every continuity object; anything else is Corrupt
197 (continuity.rs:24-26, :110, :627). Changing either is an operator decision
198 (ADR 0016 status), not a C port decision.
199- **INV-3 Identity derivations.** Section 1.7, including the big-endian index in
200 `child_branch_id` (continuity.rs:82), which differs from every other integer.
201- **INV-4 Never mint.** No code path except an explicit provisioning request
202 writes an AgentRoot. Resolve and resume never create an identity
203 (continuity.rs:635-637, :859-868; ADR 0016:69-71).
204- **INV-5 Root count.** 0 roots with no other continuity object: Unprovisioned.
205 0 roots with any manifest, agent-state or WAL object: Corrupt("continuity
206 objects without an agent root"). More than 1 root: Conflict
207 (continuity.rs:638-653). Order of these checks is part of the contract.
208- **INV-6 Manifest chain.** Every manifest in the catalog decodes, names this
209 root, and the set sorted by sequence is exactly 1..n with each `previous`
210 equal to the ObjectId of the manifest before it (continuity.rs:656-678).
211 Current manifest = the one with sequence n. A fork, a gap, a foreign root
212 or a missing manifest is Corrupt.
213- **INV-7 Referenced objects.** A referenced id must exist in the catalog with
214 the expected kind and Store version 1, else Corrupt (continuity.rs:619-633).
215 The current manifest must name an agent state (:681-683) whose agent_id is the
216 root's (:685-689). WAL segments must have strictly increasing `sequence`
217 ≤ manifest.sequence (:693-700).
218- **INV-8 Atomic commit.** Provisioning writes AgentRoot + AgentState(genesis,
219 written_at 1) + Manifest(sequence 1, incarnation 1, no previous, no WAL) in
220 **one** Store transaction (continuity.rs:734-770). Every later commit writes
221 its optional AgentState, optional WAL segment and the new Manifest in **one**
222 transaction (:799-855). Object order in the transaction is AgentState, WAL,
223 Manifest (:799-845).
224- **INV-9 Commit rules.** New sequence = previous + 1 (checked); AgentState's
225 `written_at` is overwritten with the new sequence (:808); a state for another
226 agent is Corrupt (:803-807); a WAL segment is appended only when the update
227 has records, refused at 64 segments as Limit("cortex WAL needs compaction")
228 (:815-818); incarnation + 1 only on resume (:828-836).
229- **INV-10 Commit-before-observation.** `resume` commits incarnation + 1
230 before returning the view the caller acts on (continuity.rs:859-868).
231- **INV-11 Read-only degraded mount.** On a mount that is not Valid, provision
232 and commit return ReadOnly before any write (continuity.rs:712-717, :727,
233 :793); resume returns the verified view and `committed = false` (:864-866).
234- **INV-12 Old or new, never a third state.** A power cut at any unit write of
235 a continuity commit reopens to the old manifest or the new one, same agent
236 (continuity_tests.rs:242-283; QEMU campaign section 4).
237- **INV-13 Inspection never writes.** `inspect` performs reads only
238 (recovery_core.rs:107-187; recovery_core_tests.rs:89-108).
239- **INV-14 Degraded wins.** On a DegradedRecovery mount the entry reason is
240 `Degraded(peer)` whatever continuity resolves to (recovery_core.rs:172-176).
241- **INV-15 Applicable action** (recovery_core.rs:79-90): RepairDegradedPeer only
242 for `Degraded(Malformed)` **and** a resolved identity on the valid root;
243 ProvisionIdentity only for `Unprovisioned` on a Valid mount; otherwise none.
244 A degraded mount is never unprovisioned (the fe2c2bd finding,
245 evidence/recovery_core_qemu_2026-09-25.md "Finding fixed during qualification").
246- **INV-16 Authorisation before write.** Each action re-inspects the device,
247 requires `applicable() == action` (else NotApplicable), then a verifying
248 operator response (else Unauthorised), and only then writes
249 (recovery_core.rs:219-235, :244-245, :267-268).
250- **INV-17 Repair writes one unit.** Repair zeroes the inactive superblock slot
251 (1 - active slot), flushes, and re-inspects (recovery_core.rs:246-256).
252- **INV-18 Operator provisioning.** Uses the Store uuid from the inspection and
253 `ProvisionSource::Operator` (recovery_core.rs:269-272).
254
255---
256
257## 3. Boot resolution and Recovery Core entry (CODE, behaviour to port)
258
259Continuity, in this order (continuity.rs:637-710):
260
2611. List catalog entries by kind. Apply INV-5.
2622. Decode the AgentRoot through a kind-checked read (INV-7).
2633. Decode every manifest; apply INV-6.
2644. Decode the current manifest's AgentState; check its agent.
2655. Decode each WAL segment in manifest order; check ordering; concatenate records.
266
267Corrupt reasons the Rust code can return (the `why` text appears in serial
268markers; matching it exactly is PROPOSED so C markers can be grepped the same
269way): "bad magic", "unsupported continuity format version", "nonzero header
270reserved", "body length mismatch", "truncated object", "nonzero reserved",
271"trailing bytes", "unknown provisioning source", "zero agent id", "root branch
272does not derive from agent", "too many cortex WAL segments", "manifest
273sequence/previous mismatch", "branch count out of range", "branches not
274strictly sorted", "unexpected root branch", "parent branch is absent", "branch
275lineage is inconsistent", "fork indexes are not contiguous", "unknown epistemic
276status", "statement length", "WAL segment record count", "referenced object is
277absent", "referenced object has the wrong kind", "continuity objects without an
278agent root", "manifest names a foreign root", "agent root has no manifest",
279"manifest sequence gap or fork", "manifest chain is broken", "manifest has no
280agent state", "agent state belongs to another agent", "WAL segments out of
281order", "fork parent is absent", "child branch id collision" (all in
282continuity.rs:97-857).
283
284Recovery Core (recovery_core.rs:107-187):
285
2861. Read Store-region units 0 and 1 raw; classify each slot Zero /
287 Superblock{generation} / Undecodable(err) / ReadError; take the store uuid
288 from the first decodable slot; compute `state_digest`.
2892. Open the Store without writing. Unformatted: reason StoreUnformatted. Other
290 mount error: StoreMount(err).
2913. Record (mount state, peer condition, generation) and catalog counts
292 (roots, manifests, other).
2934. Resolve continuity. Reason: Degraded(peer) if the mount is degraded, else
294 none / Unprovisioned / Conflict / ContinuityCorrupt(why); any other
295 continuity error is ContinuityCorrupt("continuity unreadable").
296
297Serial markers the QEMU oracle greps (CODE, nvme_read.rs):
298
299| marker | source |
300|---|---|
301| `CONTINUITY: <PROVISIONED\|RESUMED\|RESUMED_READONLY\|REMEMBERED\|COMMITTED> agent=<64 hex> incarnation=N sequence=S cortex=K branches=B memory=<16 hex>` | :1118-1143 |
302| `CONTINUITY: UNPROVISIONED`, `CONTINUITY: CONFLICT`, `CONTINUITY: CORRUPT (<why>)`, `CONTINUITY: STOP (<Debug of error>)`, `CONTINUITY: NO_ENTROPY`, `CONTINUITY: STOP (fork)` | :1144-1155, :1178, :1189, :1229 |
303| `CHECKPOINT: <name>` (crash mode) | :1241-1253; names crates/aienos-kernel/src/store/checkpoint.rs:79-88 |
304| `RECOVERY_OPERATOR_KEY: TEST-ONLY` | :1287 |
305| `RECOVERY_RECORD: slot0=… slot1=… mount=… peer=… generation=… roots=… manifests=… other=… agent=<hex\|none>` | :1290-1322 |
306| `RECOVERY_CORE: ENTERED reason=<Debug>` / `RECOVERY_CORE: NOT_NEEDED` | :1324-1333 |
307| `RECOVERY_CHALLENGE: action=<name> challenge=<64 hex>` | :1335-1343 |
308| `RECOVERY_ACTION: <name> DONE[ agent=<hex>]`, `RECOVERY_REFUSED (<Debug>)`, `RECOVERY_REFUSED (NoEntropy)` | :1356, :1377-1391 |
309
310PROPOSED: the C kernel prints these exact strings (same field order, same
311Debug spellings such as `Degraded(Malformed)`), so the C gate scripts can use
312the Rust scripts' grep patterns and the rows can be IDENTICAL rather than
313DIFFERS.
314**CONTINUITY markers IMPLEMENTED, host PASS (cut 4):** `cr_marker_view` (`PROVISIONED`, `RESUMED`, `RESUMED_READONLY`, `REMEMBERED`, `COMMITTED`) and `cr_marker_outcome` (`UNPROVISIONED`, `CONFLICT`, `CORRUPT (why)`, `NO_ENTROPY`, `STOP (...)`) in `svc/continuity_commit.c` produce the strings above byte for byte (test compares with an independently printf-built line, incl. the 8-byte `memory=` and `e3b0c44298fc1c14` for no records). Only the Store-error spelling differs (9.2). The `CHECKPOINT:` marker is not implemented (no wiring). **Recovery markers IMPLEMENTED, host PASS (cut 5):** `rc_marker_entry` (`RECOVERY_CORE: ENTERED reason=<text>` / `NOT_NEEDED`), `rc_marker_challenge`, `rc_marker_done`, `rc_marker_refused` in `svc/continuity_recovery.c`, with the Rust Debug spellings (`Degraded(Malformed)`, `Unprovisioned`, `ContinuityCorrupt("why")`); the C-only reason spells `SealedRefusal(<rc>)`, a mount error `StoreMount(<rc>)` (Rust prints the Debug of its own error), and `RECOVERY_REFUSED (Continuity(<outcome>))` names the cut 4 outcome. `RECOVERY_OPERATOR_KEY: TEST-ONLY` and `RECOVERY_RECORD:` are not implemented (the key is a parameter of the actions; the record belongs to the wiring cut).
315
316---
317
318## 4. What the Recovery Core may do without a model
319
320CODE (recovery_core.rs:1-8, ADR 0006): no model, no network, no clock.
321
322Allowed:
323
324- Read both superblock slots, mount the Store read-only, resolve continuity,
325 print the raw system record, compute a challenge. Never writes (INV-13).
326- With a verifying operator response bound to the exact state and action:
327 RepairDegradedPeer (zero one inactive superblock unit) or ProvisionIdentity
328 (one AgentRoot + genesis state + first manifest, one transaction). Nothing
329 else.
330
331Refused by design (CODE):
332
333- Mint an identity after identity loss, on a degraded mount, on a store with
334 orphaned continuity objects, or on a store with two roots (INV-5, INV-15).
335- Discard a CRC-valid newer root whose graph is broken (roll back committed
336 state): out-of-band operator decision (recovery_core.rs:74-75;
337 evidence/recovery_core_qemu_2026-09-25.md "Not claimed").
338- Reuse an authorisation on another state or for another action (challenge
339 binding, recovery_core_tests.rs:146-150, :300-315).
340
341Operator key today is TEST-ONLY (`TEST_ONLY_OPERATOR_KEY = [0x0f; 32]`,
342crates/aienos-boot/src/store_qual.rs:59-63). The real credential (Argon2id
343passphrase, FIDO key) is undecided (ADR 0006 / M5). PROPOSED: the C port keeps
344the key behind a TEST-only build flag, prints `RECOVERY_OPERATOR_KEY:
345TEST-ONLY` on every boot that uses it, and refuses the TEST key in a
346`CK_HARDWARE_STAGING` build the same way `ck_store_keys_admissible` refuses
347TEST Store keys (native/kernel/svc/store_boot.h:66).
348
349Other ADR 0006 items are out of scope (section 8). Rust has an A/B `SlotManager`
350and a Cortex `WalRecovery::recover_and_truncate` (crates/aienos-kernel/src/recovery.rs:37-90,
351:144-200; tests :329, :346, :380), but neither is part of the continuity or
352Recovery Core QEMU campaigns this contract ports. Crash records and model-weight
353hash checks have no Rust code.
354
355---
356
357## 5. Interfaces to the C Store (PROPOSED unless marked CODE)
358
359The C kernel does not use a plain Store v1. It mounts the **sealed** Store:
360every application object is an M5 envelope of Store kind `SS_KIND_ENVELOPE`
361(0x0510), with the application kind and version carried in the M5 binding, plus
362one sealed transaction record per transaction and an anti-rollback anchor
363outside the Store region (CODE: native/store/store_sealed.h:1-31, :43-51;
364boot stage native/kernel/svc/store_boot.c, `store_boot_run_policy`). The Rust
365oracle wrote continuity objects as plaintext Store v1 objects. That gap is the
366main design point of this contract.
367
368### 5.1 Placement
369
370- **P-1 (PROPOSED).** Continuity objects are stored through `ss_transact` as
371 `ss_object{kind = 16|17|18|20, version = 1, bytes = canonical plaintext}`.
372 **IMPLEMENTED, host PASS: read side cut 3, write side (provision, commit, resume) cut 4 (`cr_bind_sealed_sink` -> `ss_transact`, one call per transaction, at most 3 objects, order AgentState, WAL, Manifest).** Evidence:
373 `test_continuity_resolve.c` writes all four kinds with `ss_transact` at version 1 and `cr_resolve`
374 reads them back through the claims (`t_resolved`, `t_orphans`, `t_references`, at 512 and 4096 byte
375 blocks).
376 The kinds do not collide with existing C application kinds (CODE:
377 `CK_BOOT_KIND` 0x0B00, store_boot.h:30; `CK_ART_INDEX_KIND` 0x0A01 and
378 `CK_ART_CHUNK_KIND` 0x0A02, native/kernel/svc/artifact_store.h:38-39).
379- **P-2 (PROPOSED).** Every cross-reference inside a continuity object (manifest
380 `root`, `previous`, `agent_state`, `cortex_wal[]`) is the **logical
381 ObjectId** = `sv1_object_id(kind, 1, plaintext)`, exactly the Rust
382 `ObjectId::calculate` value. Then the plaintext bytes of every continuity
383 object are bit-identical to Rust's for the same inputs, and the differential
384 compares bytes directly. The sealed envelope's own Store ObjectId (SHA-256
385 of the envelope, keyed by the store keys; m5.h:171-181, store_sealed.c:334-345)
386 is never written into a continuity object.
387 **IMPLEMENTED on the read side, host PASS (cut 3):** `cr_resolve` computes every logical id with
388 `sv1_object_id` over the decrypted plaintext (`continuity_resolve.c:113`) and follows only logical ids
389 (`read_kind`, :64-82), re-hashing each object it reads so a reference can never reach different
390 bytes (`t_references`, `t_chain`). Bytes stay bit-identical to Rust (D-1).
391- **P-3 (PROPOSED).** Lookup: after `ss_open`, the verified claims
392 (`ws->claims[0..nclaims)`, store_sealed.h:92-118) give each envelope's
393 application kind and version (`c.obj.object_kind`, `object_version`) without
394 decryption (CODE pattern: store_boot.c, latest boot record loop). The C
395 continuity layer builds a table of (kind, version, logical id, sid) once per
396 resolve by `ss_read` of each claim of kinds 16-20 and `sv1_object_id` over the
397 plaintext. "Catalog entry of kind K" (Rust `ids_of_kind`, continuity.rs:609-617)
398 becomes "claim of application kind K". A logical id that maps to two claims is
399 Corrupt (PROPOSED: Rust cannot see this because Store v1 dedups identical
400 objects; the sealed store gives each write its own envelope).
401 **IMPLEMENTED, host PASS (cut 3):** `continuity_resolve.c:100-130` (`build_table`; duplicate check :115-118,
402 reason `duplicate logical object id`, checked before the root count so it is Corrupt, not Conflict);
403 test `t_duplicate_id` writes the same root bytes twice into the real sealed Store. Kind 19 and every
404 non-continuity kind are never read (`t_unprovisioned`, `t_fake`), as Rust ignores them.
405
406### 5.2 Calls the continuity and recovery code needs
407
408| need | C Store call | status |
409|---|---|---|
410| mount without writing | `ss_open` (store_sealed.h:141-143), which runs `st_open` first | CODE |
411| mount state, peer condition | `s->st.state` (`ST_VALID`, `ST_DEGRADED_RECOVERY`), `s->st.peer` (store_engine.h:22-26, :134-141) | CODE |
412| generation, active slot | `ss_generation`, `st_active_slot` (store_sealed.h:151, store_engine.h:153-155) | CODE |
413| list by kind | claims loop (P-3), `cr_bind_sealed` in `continuity_resolve_sealed.c` | CODE (cut 3, host PASS) |
414| read one object | `ss_read(s, sid, out, cap, &len, &kind)` (store_sealed.h:149-150) | CODE |
415| commit ≤ 3 objects atomically | `ss_transact(s, objs, n, hook, arg, ids_out)` (store_sealed.h:146-147), n ≤ `SS_MAX_OBJECTS` = 8 | CODE |
416| read-only refusal | `ss_prepare` returns `ST_E_READ_ONLY_DEGRADED` when not Valid (store_sealed.c:379) | CODE; continuity still checks first (INV-11) |
417| crash checkpoints | `st_hook` with `ST_CP_*` 0-6 (store_engine.h:58-69) plus `SS_CP_BEFORE_ANCHOR` 100, `SS_CP_AFTER_ANCHOR` 101 (store_sealed.h:76) | CODE |
418| raw superblock units for inspection | `st_dev.read_unit(ctx, 0/1, buf)` on the Store region, `sv1_superblock_decode` (store_engine.h:71-78, store_v1.h:184) | CODE |
419| repair write | `st_dev.write_unit(ctx, 1 - active, zeros)` then `flush` | CODE calls; use PROPOSED |
420| entropy for provisioning | `ck_rng_fill` (native/kernel/core/entropy.h:53), refuses with no fallback | CODE; use PROPOSED |
421| HMAC + constant-time compare | `aienos_hmac_sha256`, `aienos_ct_equal` (native/crypto/aienos_crypto.h:112, :116) | CODE |
422
423### 5.3 Known conflicts between the oracle and the sealed Store
424
425- **K-1 Size.** **Cap IMPLEMENTED in the C codec, host PASS (cut 1, kept in cut 2); policy still PROPOSED** (the chunking alternative needs an operator decision). Evidence: `continuity_codec.c` decode cap at `rd_open` (`len > CC_MAX_OBJECT_BYTES`) and encode cap at `enc_begin`; `test_continuity_codec.c` K-1 checks (204 branches = 16384 bytes encodes, 205 is Limit with nothing written; 64 x 1024 WAL is Limit, never truncated; decode of 16385 bytes is Limit; mutant MC-11). Cap 16384 and 204 branches are unchanged by cut 2. `SS_MAX_PLAINTEXT` is 16384 bytes per object
426 (store_sealed.h:46). Rust allows an AgentState up to 20544 bytes (256
427 branches) and a WAL segment up to 67872 bytes (section 1). PROPOSED: the C
428 port enforces the Rust bounds and an extra byte bound of 16384, returning
429 Limit (never truncating) when an encoding exceeds it; the differential covers
430 inputs inside both bounds, and a C test proves a Rust-legal but oversize input
431 is refused as Limit with nothing written. That caps the C branch table at 204
432 branches ((16384 - 64) / 80). The alternative (chunking one object across
433 envelopes) changes the object model and needs an operator decision.
434- **K-2 Catalog bound.** Each sealed transaction adds the payload envelopes plus
435 one transaction record, so the Store v1 catalog bound (4096 entries,
436 store/v1.rs:12) is reached in fewer continuity commits than in Rust. ADR 0016
437 already defers compaction; the C gate must report the count, not hide it.
438 **MEASURED (cut 4, host, `t_catalog_bound`, real sealed Store):** provisioning leaves 4 catalog entries (root, state, manifest, transaction record). Each commit adds its payload objects plus the manifest plus one transaction record: a plain resume 2 entries, a commit with one WAL segment 3. After provisioning and 64 one-record WAL commits the catalog holds **196** entries (4 + 64 x 3), at 4096 and at 512 byte blocks. The 65th WAL segment is refused (`Limit "cortex WAL needs compaction"`, nothing written), so the WAL cap is reached long before the 4096-entry catalog bound. Resumes alone are the fastest way to fill the catalog: (4096 - 4) / 2 = 2046 resumes in theory. **Run to exhaustion (`CK_K2_FULL=1`, slow, about 3 minutes, not part of `make test`):** in a 16384-unit (64 MiB) Store region at 4096 byte blocks, 915 resumes after provisioning succeeded and the 916th returned `Store rc=-205` (`NoSpace`), at 1834 of 4096 catalog entries. So the limit reached first is **Store space (append-only, no reclaim), not the catalog bound**: about 18 units (72 KiB) per resume at that depth. Either refusal arrives as `CR_STORE` with nothing half written (the last transaction failed cleanly). A real boot disk has a larger Store region, so the practical bound there is the catalog (<= 2046 resumes) or space, whichever is smaller; UNVERIFIED for any real partition size. Compaction stays deferred (ADR 0016:96-98), so a long-lived store needs it; this is the number the gate reports.
439- **K-3 Identity loss looks different.** The Rust campaign flips byte 20 of the
440 AgentRoot's first unit (crates/aienos-store-tool/src/main.rs:177-201,
441 qemu_recovery_test.sh:168). In the sealed Store that byte is ciphertext, so
442 the C mount is expected to refuse at the keyed proof (`SS_E_ENVELOPE`) before
443 continuity runs. That expectation was UNVERIFIED (from store_sealed.h:61 and
444 store_boot.h:5-11) and turned out partly wrong. **Cut 5, DECIDED by running it (host PASS, `test_continuity_recovery` t_91g/t_91j, 4096 and 512 byte blocks):** flipping byte 20 of the first unit of the newest AgentRoot envelope makes `ss_open` refuse with `SS_E_ROLLBACK` (-303, `SealedRollback`), not `SS_E_ENVELOPE`: the damaged generation fails its sealed check and the anchor already holds that generation, so the refusal reads as a rollback. The Store does not mount, so no record, challenge or mount fields exist; the entry reason is the C-only `SealedRefusal(-303)` and `rc_applicable()` is none. A valid operator response cannot be formed (no challenge); forged responses for both actions return `NotApplicable` and the image stays byte for byte unchanged. PROPOSED (now implemented as above): the C Recovery Core adds an entry
445 reason for a sealed refusal (keyed / rollback proof) that admits no action,
446 and the identity-loss rows stay DIFFERS with that observable stated.
447- **K-4 GraphBadNewer.** When the newest root is graph-broken, Rust mounts
448 degraded on the older generation (recovery_core_tests.rs:178-219). In the
449 sealed Store the anchor holds the newer generation, so `ss_open` is expected to
450 refuse with `SS_E_ROLLBACK` (store_sealed.h:57). **Cut 5: CONFIRMED for a flipped byte of the newest AgentRoot (see K-3: `SS_E_ROLLBACK`, reason `SealedRefusal(-303)`, no action applicable); cut 7: host test written (`t_k4`), covering only a sealed-valid newer MANIFEST (next sequence, fresh anchor) whose agent_state object was never written, NOT_RUN until the forge reports; predicted outcome: the Store mounts Valid, reason `ContinuityCorrupt("referenced object is absent")` (continuity_recovery.c:102-111, continuity_resolve.c:75), no action applicable. Still unbuilt, with no C test: a graph-broken newest AgentRoot and a foreign-agent state.** Either way no action may be applicable (INV-15).
451- **K-5 Degraded mount through ss_open. DECIDED by running it (cut 3, host PASS).** The question was whether
452 `ss_open` returns 0 on a `DegradedRecovery` mount with a malformed peer (needed for INV-11 and repair).
453 **Answer: yes.** `test_continuity_resolve.c` `t_degraded` (:716, printed at :751) commits two generations
454 to the real sealed Store, fills the inactive superblock slot with garbage, reopens: `ss_open` rc=0,
455 `st.state` = `ST_DEGRADED_RECOVERY`, `st.peer` = `ST_PEER_MALFORMED`, generation unchanged (3 -> 3), the
456 anchor accepts it. On that mount `cr_resolve` still returns the verified view (same agent, incarnation and
457 `memory=`), `cr_writable` returns `CR_READ_ONLY` (INV-11), and `cr_state_digest_dev` hashes the garbage
458 unit as it lies. Run at 512 and 4096 byte blocks. Not tested here: a malformed peer combined with
459 other mount or anchor damage, and repair itself (cuts 4-5).
460- **K-6 Region geometry.** The Rust qualification Store region is 256 units at
461 LBA 64 with a control block at LBA 32 (crates/aienos-boot/src/store_qual.rs:13-16).
462 The C layout is anchor units 0-3, Store region 4..U-2, probe unit U-1
463 (native/kernel/dev/disk_layout.h). Raw superblock "units 0 and 1" in the
464 challenge (section 1.7) mean the first two units of the **Store region** in
465 both. **Cut 7: host test written (`tests/test_continuity_geometry.c`, `make -C native/kernel test`, binary
466 `$(OUT)/host/test_continuity_geometry`, also in `sanitize`), NOT_RUN until the forge
467 reports.** It shows, on the real sealed Store in a file: `rc state_digest` equals SHA-256 over
468 Store-region units 0 and 1 read from the image and differs from the digest of the anchor units;
469 a flipped byte in Store-region unit 0, and separately unit 1, changes the digest (and the
470 challenge after flipping the inactive slot); a flipped byte in each of the four anchor units
471 leaves the digest unchanged; the `dev/disk_layout.h` constants (anchor 4, probe 1, minimum Store
472 64) give a Store region of units 4..U-2 (U-5 units). PROPOSED: the C
473 challenge does not also bind the anchor record; adding it would make C challenges differ from
474 Rust ones and needs a decision.
475- **K-7 Mode selection. DECIDED (cut 6).** The Rust qualification picks a mode
476 from a control block on the NVMe disk (store_qual.rs:27-48; store-tool `cfg`,
477 crates/aienos-store-tool/src/main.rs:108-122; response at bytes 16..48). The C
478 disk layout has no control block and none is added. Decision: a TEST-only
479 build flag `CK_TEST_CONTINUITY=1` builds a separate image
480 (`full-test-continuity`), using the same pattern as `CK_TEST_STORE_CRASH`. The
481 host writes one ASCII plan line
482 `AIENCONT v1 mode=<N>[ cp=<checkpoint>][ response=<64 hex>]` into the last
483 4096-byte unit of the AIENOS partition (the read-write probe scratch unit, which
484 the devices stage restores byte for byte). Modes use the Rust numbers: 5
485 provision, 6 resume, 7 resume + remember, 8 resume + commit with crash point
486 `cp` (halts at the C checkpoint name, section 5.2), 9 Recovery inspect, 10
487 inspect + repair-degraded-peer, 11 inspect + provision-identity; mode 1 (or no
488 plan) is the ordinary Store boot. For modes 5-11 the continuity or Recovery
489 boot runs INSTEAD of the boot-record commit, so read-only rows leave the image
490 byte-identical. The build refuses the flag with `CK_HARDWARE_STAGING`,
491 `CK_QEMU_UNSAFE_DMA` and `CK_TEST_STORE_CRASH`; the default image carries none
492 of the hook code or strings (`cont_check` in the Makefile). The TEST operator
493 key (0x0f x32) exists only in that image and is announced
494 `RECOVERY_OPERATOR_KEY: TEST-ONLY`. Code: `svc/continuity_boot.{c,h}`.
495
496---
497
498## 6. Oracle map: Rust check -> C gate
499
500Gate `M4_CONTINUITY` and gate `M4_RECOVERY` in `scripts/ck_gates.sh` (today
501`missing`, ck_gates.sh:55-56). Every row below is listed in
502`native/kernel/GATES.md` sections "M4 continuity" and "M4 recovery" with status
503`NOT_RUN (MISSING_IMPLEMENTATION)`.
504
505### 6.1 M4_CONTINUITY
506
507QEMU oracle: `scripts/qemu_continuity_test.sh` (PASS line
508`CONTINUITY_QEMU: PASS`, :210; receipt evidence/continuity_qemu_2026-09-25.md
509at Rust commit 3df62b2).
510
511| GATES row | Rust check | script lines |
512|---|---|---|
513| 81 | blank media, resume: `CONTINUITY: STOP (store Unformatted)`, image unchanged | :92-98 |
514| 82 | formatted store, no root, resume: `CONTINUITY: UNPROVISIONED`, image unchanged | :100-108 |
515| 83 | provision: `PROVISIONED`, 64-hex agent, `incarnation=1 sequence=1 cortex=0 branches=1` | :111-119 |
516| 83a | second provision: `STOP (AlreadyProvisioned)`, image unchanged | :122-127 |
517| 83b | independent provisioning gives a different agent (RNDR) | :129-136 |
518| 84 | mode 7: `RESUMED … incarnation=2 sequence=2 cortex=0 branches=1`, then `REMEMBERED … incarnation=2 sequence=3 cortex=1 branches=2` | :139-147 |
519| 85, 85a | cold restarts 1 and 2: same agent, same `memory=`, incarnation 3 then 4, sequence 4 then 5, cortex 1, branches 2 | :149-160 |
520| 86-86f | kill at each checkpoint (before_first_write, after_payloads, after_catalog, after_commit_record, after_first_flush, after_superblock_write = either, after_final_flush = new); marker seen; same agent | :163-194 |
521| 87 | malformed peer (`inject … inactive seeded_garbage`): `RESUMED_READONLY`, same agent and memory, image unchanged | :196-205 |
522
523Host oracle: `cargo test -p aienos-kernel --lib continuity` (9 tests,
524crates/aienos-kernel/src/continuity_tests.rs):
525
526| GATES row | Rust test | line |
527|---|---|---|
528| 87a | `unprovisioned_store_never_mints_an_identity` | :84 |
529| 87b | `provision_then_cold_restart_returns_the_same_identity_and_memory` | :97 |
530| 87c | `provisioning_twice_is_refused` | :142 |
531| 87d | `two_roots_stop_with_conflict` | :152 |
532| 87e | `forked_or_gapped_manifest_chains_are_corrupt` | :178 |
533| 87f | `degraded_mount_resumes_read_only` | :221 |
534| 87g | `a_crash_at_every_write_of_a_commit_leaves_old_or_new_never_a_third_state` | :242 |
535| 87h | `encodings_round_trip_and_reject_tampering` | :286 |
536| 87i | `branch_table_validation_rejects_broken_lineage` | :328 |
537
538C-side differences already known (PROPOSED observables, to be stated in the
539rows when the C gate exists): the crash campaign has 9 checkpoints in C
540(7 Store + 2 anchor, section 5.2); the Rust "either" point
541`after_superblock_write` is `after_inactive_superblock` in C
542(native/store/store_engine.c:43-55); the anchor points need their own expected
543outcome, derived from the sealed-store rule that a crash between Store commit
544and anchor commit resumes at `SS_RB_PREPARED_ADVANCE` (store_sealed.h:16-21),
545so both anchor points are expected to show the **new** memory.
546
547### 6.2 M4_RECOVERY
548
549QEMU oracle: `scripts/qemu_recovery_test.sh` (PASS line
550`RECOVERY_CORE_QEMU: PASS`, :194; receipt
551evidence/recovery_core_qemu_2026-09-25.md at Rust commit e5b44d8).
552
553| GATES row | Rust check | script lines |
554|---|---|---|
555| 88 | inspect degraded store: `RECOVERY_OPERATOR_KEY: TEST-ONLY`, `RECOVERY_CORE: ENTERED reason=Degraded(Malformed)`, record shows the same agent, image unchanged; repair challenge offered | :94-103 |
556| 88a, 88b, 88c | repair with zero / wrong-key / wrong-challenge response: `RECOVERY_REFUSED (Unauthorised)`, image unchanged | :105-117 |
557| 89 | authorised repair: `RECOVERY_ACTION: repair-degraded-peer DONE`, image changed | :119-125 |
558| 89a | resume after repair: `CONTINUITY: RESUMED`, same agent and memory | :126-132 |
559| 89b | replay of the repair response: `RECOVERY_REFUSED (NotApplicable)`, image unchanged | :133-138 |
560| 90 | unprovisioned store: `ENTERED reason=Unprovisioned`, provision challenge offered, image unchanged | :141-149 |
561| 90a | provisioning with wrong key: `Unauthorised`, image unchanged | :150-155 |
562| 90b | authorised provisioning: `RECOVERY_ACTION: provision-identity DONE agent=…`, a cold resume returns that agent | :156-163 |
563| 91 | corrupted agent root: `ENTERED`, no `RECOVERY_CHALLENGE`, image unchanged | :166-174 |
564| 91a, 91b | modes 10 and 11 with a forged response: `RECOVERY_REFUSED (…)`, no action, image unchanged | :175-183 |
565| 91c | normal boot after identity loss: no `RESUMED`/`PROVISIONED`, image unchanged | :184-189 |
566
567Host oracle: `cargo test -p aienos-kernel --lib recovery_core` (8 tests,
568crates/aienos-kernel/src/recovery_core_tests.rs) and the operator-auth tests in
569crates/aienos-kernel/src/recovery.rs:
570
571| GATES row | Rust test | line |
572|---|---|---|
573| 91d | `inspection_never_writes_and_names_the_reason` | recovery_core_tests.rs:89 |
574| 91e | `degraded_repair_needs_the_operator_and_restores_a_writable_store` | :111 |
575| 91f | `provisioning_is_operator_only_and_only_on_an_unprovisioned_store` | :154 |
576| 91g | `identity_lost_in_the_newest_root_never_looks_unprovisioned` | :178 |
577| 91h | `malformed_peer_without_a_resolvable_identity_is_not_repaired` | :222 |
578| 91i | `orphaned_continuity_objects_are_corrupt_not_unprovisioned` | :236 |
579| 91j | `identity_loss_through_corruption_offers_no_action_that_mints` | :267 |
580| 91k | `challenges_bind_state_and_action` | :300 |
581| 91l | `hmac_primitive_rfc4231_case_2` | recovery.rs:238 |
582| 91m | `operator_auth_known_answer_accepted` | recovery.rs:251 |
583| 91n | `operator_auth_rejects_any_single_bit_flip` | recovery.rs:269 |
584| 91o | `operator_auth_rejects_legacy_bare_sha256_response` | recovery.rs:299 |
585| 91p | `operator_auth_rejects_undomained_hmac_and_zero_response` | recovery.rs:311 |
586
587### 6.3 Differential agreement (D-1, D-2 DECIDED and IMPLEMENTED, host PASS; D-3 PROPOSED)
588
589ADR 0024 Q2 requires differential agreement, not just parallel tests.
590
591- **D-1 Golden vectors.** A test-only Rust emitter (no change to Rust kernel
592 behaviour; the Rust kernel is frozen as reference) writes canonical bytes and
593 ObjectIds for a fixed set of inputs: AgentRoot (both sources), manifests with
594 0/1/64 WAL ids, AgentState genesis and after forks at depth 1 and 2,
595 WAL segments with each epistemic status and statement lengths 1 and 1024,
596 root/child branch ids (index 0, 1, 2^32, 2^64-1), challenges for both actions,
597 and HMAC responses with the TEST key. Committed as fixture files (no Python,
598 no new dependency) under `native/kernel/tests/fixtures/`. The C host test
599 must reproduce every byte and refuse the same tampered vectors.
600 **DECIDED and IMPLEMENTED (cut 2, host PASS).** Emitter: `crates/aienos-kernel/src/continuity_vectors.rs`
601 (test-only, `cfg(test)`; no kernel behaviour change). Fixtures:
602 `native/kernel/tests/fixtures/continuity_vectors.txt` (22 vectors, 5 branch ids, 4 deferred lines).
603 Regenerate: `AIENOS_CONTINUITY_VECTORS_REGEN=1 cargo test -p aienos-kernel --lib continuity_vectors`;
604 without the variable the Rust test fails if a committed fixture differs from freshly emitted bytes.
605 C: `test_continuity_codec.c` (`test_golden`, `test_tampered`; fixture dir = argv[1] or the Makefile
606 `CC_FIXTURE_DIR`) reproduces every byte and both ObjectIds (`cc_object_id` and `sv1_object_id`),
607 decode then encode is the identity, and tampered bytes, ObjectIds and verdicts are refused.
608 Added vector: `state_forksum_overflow` (hostile fork counts, refused by both).
609 **DONE in the resolve cut (cut 3, host PASS):** the recovery challenges (both actions) and HMAC responses
610 with the TEST key (0x0f x32) are the four `deferred` fixture lines. `test_continuity_resolve.c`
611 `test_fixture` reproduces all four byte for byte from `cr_challenge` and `cr_operator_response`
612 (system record: uuid 0x5a x16, generation 7, state digest 0x33 x32). The fixture file header and the
613 emitter text still say "C does not implement these yet": stale wording, left as is because changing it
614 means regenerating the Rust fixtures; the lines themselves are unchanged.
615- **D-2 Decode agreement.** For every single-bit flip of every vector, C and
616 Rust agree on accept / refuse and on the error class.
617 **DECIDED and IMPLEMENTED (cut 2, host PASS).** `continuity_verdicts.txt` holds the Rust verdict of each
618 vector and of each of its single-bit flips (class and reason text, one letter per distinct reason); C replays
619 and compares: 22 vectors, 86598 verdicts, **0 divergences** (C and Rust agree on accept/refuse, class and
620 reason text for every flip). The emitter panics on a Rust reason missing from its table, so a new reason
621 cannot be dropped silently. The K-1 cap (C-only Limit) cannot appear in a flip because flips keep the length.
622- **D-3 Cross-store agreement** is not possible byte-for-byte (sealed vs
623 plaintext Store, section 5). Agreement is on resolved values: agent id,
624 sequence, incarnation, branch table bytes, Cortex records, `memory=` digest.
625
626---
627
628## 7. Failure modes each gate must catch, with a mutation
629
630Each C gate and host test needs a mutation that makes it FAIL, wired as a
631TEST-only build flag refused by `CK_HARDWARE_STAGING` or as a `--self-test`
632case (LT-KERNEL rule). PROPOSED flag names; the behaviour is what matters.
633
634M4_CONTINUITY:
635
636| id | mutation | must turn FAIL |
637|---|---|---|
638| MC-1 | resume provisions when Unprovisioned: `CM_MUTANT_RESUME_PROVISIONS`, IMPLEMENTED, killed (cut 4) | 82, 87a |
639| MC-2 | drop the orphan check (INV-5): orphan objects read as Unprovisioned: `CR_MUTANT_NO_ORPHAN_CHECK`, IMPLEMENTED, killed (cut 3) | 91i (and the C twin of 87a with an orphan manifest) |
640| MC-3 | resume skips the commit (no incarnation + 1): `CM_MUTANT_RESUME_NO_COMMIT`, IMPLEMENTED, killed (cut 4) | 84, 85, 85a, 87b |
641| MC-4 | manifest chain check ignores `previous`: `CR_MUTANT_IGNORE_PREVIOUS`, IMPLEMENTED, killed (cut 3) | 87e |
642| MC-5 | accept `format_version != 0` or nonzero reserved bytes | 87h, D-2 |
643| MC-6 | `child_branch_id` index little-endian | D-1, 87i |
644| MC-7 | provision allowed when a root exists: `CM_MUTANT_PROVISION_WITH_ROOT`, IMPLEMENTED, killed (cut 4) | 83a, 87c |
645| MC-8 | provision or commit split into two transactions (manifest after payload): `CM_MUTANT_SPLIT_TXN`, IMPLEMENTED, killed (cut 4; caught by the provisioning cut sweep: root and state without a manifest is Corrupt, and by the generation check, which sees +2) | 86-86f, 87g |
646| MC-9 | `writable` check removed (commit on degraded mount): `CM_MUTANT_NO_WRITABLE_CHECK`, IMPLEMENTED, killed (cut 4; note the sealed Store itself still refuses with `ST_E_READ_ONLY_DEGRADED`, so the mutant changes the outcome from ReadOnly to Store, which the test requires to be ReadOnly) | 87, 87f |
647| MC-10 | agent id from a fixed value instead of RNDR: `CM_MUTANT_FIXED_AGENT`, IMPLEMENTED, killed (cut 4) | 83b |
648| MC-11 | WAL segment truncated silently at the size bound (K-1) instead of Limit: `CC_MUTANT_TRUNCATE_AT_CAP`, IMPLEMENTED, killed by both `test_continuity_codec` (cut 1) and `test_continuity_commit` (cut 4: a 64 x 1024 byte commit must be Limit with the image unchanged) | C size-bound tests |
649| MC-12 | Conflict check skipped (first root used): `CR_MUTANT_SKIP_CONFLICT`, IMPLEMENTED, killed (cut 3) | 87d |
650| MC-13 | fork-count sum unchecked (wraps): `CC_MUTANT_UNCHECKED_FORK_SUM`, IMPLEMENTED, killed | 87i, `state_forksum_overflow` (D-1, D-2) |
651| MC-14 | golden mismatch only the vectors see (manifest WAL id 63 replaced by id 0): `CC_MUTANT_MANIFEST_LAST_WAL_ZERO`, IMPLEMENTED, killed | D-1 |
652| MC-15 | golden comparison skipped: `CC_MUTANT_SKIP_D1` (the test counts comparisons), IMPLEMENTED, killed | D-1 |
653| MC-16 | decode-agreement comparison skipped: `CC_MUTANT_SKIP_D2`, IMPLEMENTED, killed | D-2 |
654
655M4_RECOVERY:
656
657| id | mutation | must turn FAIL |
658|---|---|---|
659| MR-1 | inspection writes (for example repairs on inspect): `RC_MUTANT_INSPECT_WRITES`, IMPLEMENTED, killed (cut 5) | 88, 90, 91, 91d |
660| MR-2 | challenge omits `state_digest`: `CR_MUTANT_CHALLENGE_NO_DIGEST`, IMPLEMENTED, killed (cut 3) | 89b, 91k |
661| MR-3 | challenge omits the action byte: `CR_MUTANT_CHALLENGE_NO_ACTION`, IMPLEMENTED, killed (cut 3) | 88c, 91k |
662| MR-4 | response compare checks only 16 bytes (`CR_MUTANT_COMPARE_16`, IMPLEMENTED, killed, cut 3), or is not constant time (not mutated: timing is not observable on the host) | 91n (bit flips in bytes 16-31) |
663| MR-5 | `applicable()` treats a degraded mount with no identity as Unprovisioned (the fe2c2bd bug): `RC_MUTANT_DEGRADED_IS_UNPROVISIONED`, IMPLEMENTED, killed (cut 5) | 91g, 91 |
664| MR-6 | repair zeroes the active slot: `RC_MUTANT_REPAIR_ACTIVE`, IMPLEMENTED, killed (cut 5) | 89, 89a |
665| MR-7 | repair offered without a resolved identity: `RC_MUTANT_REPAIR_NO_IDENTITY`, IMPLEMENTED, killed (cut 5) | 91h |
666| MR-8 | operator auth uses bare SHA-256 (`CR_MUTANT_BARE_SHA256`) or omits the domain (`CR_MUTANT_NO_DOMAIN`): both IMPLEMENTED, killed (cut 3) | 91o, 91p |
667| MR-9 | operator provisioning writes source Qualification: `RC_MUTANT_PROVISION_QUALIFICATION`, IMPLEMENTED, killed (cut 5) | 91f |
668
669Known oracle gap (CODE, not covered by any Rust test): no power-cut campaign
670runs on **provisioning** itself; only commits are cut
671(continuity_tests.rs:242-283, qemu_continuity_test.sh:163-194). **DECIDED and IMPLEMENTED (cut 4, host PASS):** `test_continuity_commit` cuts power during provisioning at all 9 checkpoints (ST_CP_* 0-6, SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR) and at every block boundary of the write sequence (10 at 4096 byte blocks, 73 at 512), for commit and for provisioning: after every cut the store reopens (`ss_open` rc 0) and resolves to exactly the old state (Unprovisioned, generation unchanged) or exactly the full genesis (same agent as the uncut run, sequence 1, incarnation 1, generation + 1); never a root without a manifest. After a cut that left the old state, provisioning again succeeds (no orphans). Checkpoints 0-4 give the old state, 6, before_anchor and after_anchor the new one (before_anchor reopens at `SS_RB_PREPARED_ADVANCE`); checkpoint 5 (`after_inactive_superblock`) gave the NEW state in every run (the test allows either there, as the Rust oracle does). Mutant MC-8 is killed by it.
672
673---
674
675## 8. Out of scope
676
677- Kinds 19 (CortexCheckpoint) and 23 (MigrationManifest), manifest fields
678 `migration_parent_id` and admission receipts, migration boundaries
679 (ADR 0016:38-42, :57-61): no Rust code.
680- Catalog compaction and WAL compaction (ADR 0016:96-98).
681- Freezing format version 0 to 1 (operator decision, ADR 0016:3-7, :89-91).
682- The real operator credential (Argon2id, FIDO), TRUST-1, TPM NV anchor, any
683 Machine 1 claim.
684- Other ADR 0006 items: the A/B `SlotManager` and Cortex
685 `WalRecovery::recover_and_truncate` exist in Rust (recovery.rs:37-90,
686 :144-200; tests :329, :346, :380) but are outside the continuity and Recovery
687 Core campaigns; crash records and model-weight hash checks have no Rust code.
688- Rolling back to an older root when the newer one is graph-broken (refused
689 in-band on purpose).
690- The richer host crates `aienos-agent-state` and `aienos-cortex`; the C port
691 covers only the kernel objects above.
692- Any change to the Rust kernel other than a test-only vector emitter (D-1).
693- `scripts/*qualify*.sh` and `native/capability/**` (owned by aienos#213).
694
695---
696
697## 9. Differences between ADR 0016 text and the Rust code (code wins)
698
699| ADR 0016 says | Rust code does | effect on the C port |
700|---|---|---|
701| Manifest carries `migration_parent_id`, Cortex checkpoint id, admission receipts (:38) | none of these fields exist (continuity.rs:253-315) | port the code layout only |
702| Degraded mount reports `CONTINUITY: DEGRADED` (:46-48) | prints `CONTINUITY: RESUMED_READONLY …` (nvme_read.rs:1211-1213) | use the code marker |
703| Current manifest = the one no other names as previous (:57-61) | all catalog manifests must form exactly 1..n (continuity.rs:656-678) | port the stricter code rule |
704| Kinds 19 and 23 listed (:40, :42) | not written or read | out of scope |
705| Provisioning on a store with zero roots (:50-53) | also refused when orphan continuity objects exist (continuity.rs:642-649) | port the code rule |
706| Branch-table validation sums `forks` (not specified in the ADR text; section 1.5 item 4) | Rust #232: the sum is checked, an overflowing sum is refused as Corrupt("fork indexes are not contiguous"); before #232 a release build wrapped and accepted {root forks = 2^64-1, child forks = 2} | DECIDED: C refuses on overflow (`cc_state_validate`), Rust and C agree; vector `state_forksum_overflow`, mutant MC-13 |
707
708
709### 9.1 Cut 3 (resolve): C versus the Rust `resolve`, and what C does on purpose
710
711Where code and contract differ, the safer behaviour (refuse) was chosen. Host evidence:
712`test_continuity_resolve.c`.
713
714| item | Rust | C (cut 3) | effect |
715|---|---|---|---|
716| Cortex records in the view | `Continuity.cortex` keeps every record (continuity.rs:726-736) | `cr_view` keeps `cortex_count` and the streamed `memory` digest only (`continuity_resolve.h`); no record is stored | bounded memory (a view could hold 4 MiB); the QEMU markers and a resume need only count and digest; compare D-3 on resolved values |
717| order of failures | reads objects lazily, root count first (:638-653) | `build_table` reads and hashes every object of kinds 16, 17, 18, 20 first (:100-130) | a Store read error, an oversize object or a duplicate logical id on ANY continuity object is reported before the root count; always a refusal, never a resolve |
718| duplicate logical id | impossible (Store v1 dedups) | Corrupt `duplicate logical object id` (P-3) | C-only reason |
719| oversize object | n/a | Limit `continuity object exceeds the size bound` (K-1), before decode | C-only; a Rust-legal object over 16384 bytes cannot exist in the sealed Store, so the real-Store test refuses at the write (`t_size_bound`) and the resolve Limit is tested with a fake source (`t_fake`) |
720| table bound | catalog bound 4096 over all objects (store/v1.rs:12) | Limit `too many continuity objects` above `CR_TABLE_CAP` = 4096 objects of kinds 16/17/18/20 | untested at the bound (UNVERIFIED), refusal either way |
721| empty or id-less object | n/a | Corrupt `continuity object has no valid id` | C-only; the sealed Store refused a zero-length write in the test run, so this is also unreached through the real Store |
722| re-read check | n/a | `read_kind` re-hashes the plaintext and compares with the logical id (`object changed since lookup`) | C-only defence; unreachable with the real Store |
723| manifest iteration order | catalog order | claim order | when several manifests are broken the first reported reason can differ; the class (Corrupt) is the same |
724| mount state | resolve works on a degraded mount | same (`t_degraded`), `cr_writable` gives `CR_READ_ONLY` | matches INV-11 |
725| outcome set | `Store, Unprovisioned, Conflict, AlreadyProvisioned, ReadOnly, Corrupt, Limit` | same names (`CR_*`), plus C-only `CR_E_ARG`; `CR_ALREADY_PROVISIONED` exists but resolve never returns it (cut 4) | |
726
727D-2 result (cut 2, host PASS): no C/Rust decode divergence was found over 86598
728verdicts (every single-bit flip of 22 vectors, plus the untouched vectors).
729The only known C/Rust difference in the codecs is the C-only K-1 size cap
730(section 5.3), which the flips cannot reach because they keep the length, and
731the C-only `CC_E_ARG` caller-bug class, which no decoder input reaches.
732
733### 9.2 Cut 4 (provision, commit, resume): C versus the Rust code, and what C does on purpose
734
735Where code and contract differ, the safer behaviour (refuse) was chosen. Host evidence:
736`tests/test_continuity_commit.c` (real sealed Store in a file, 4096 and 512 byte blocks).
737
738| item | Rust | C (cut 4) | effect |
739|---|---|---|---|
740| provision over orphans | `provision` checks only that no root exists (continuity.rs:727-730); orphan manifest/state/WAL objects do not stop it | refuses: provision resolves first and returns the resolve outcome (`Corrupt: continuity objects without an agent root`), nothing written | stricter, as the task and ADR 0016 (:50-53) ask; a test (`t_provision_refusals`) writes an orphan manifest and checks the image is byte for byte unchanged |
741| provision with a root that is itself broken | AlreadyProvisioned (a root exists) | the resolve outcome (Corrupt, Limit or Store) | still a refusal; the class differs, the marker says why |
742| provision with two roots | AlreadyProvisioned | AlreadyProvisioned (Conflict counts as "a root exists") | same |
743| provision with no entropy | the caller (nvme_read.rs:1184-1193) prints `NO_ENTROPY` before calling `provision` | `cr_provision` itself returns `CR_NO_ENTROPY` (C-only outcome, marker `CONTINUITY: NO_ENTROPY`) from `ck_rng_fill`, after the writable and Unprovisioned checks, before any write; a never-probed rng also refuses | the refusal is inside the library, so no caller can mint without entropy; order: ReadOnly, AlreadyProvisioned/orphans, NoEntropy |
744| zero agent id | Corrupt("zero agent id") (:731) | same, from the drawn bytes (unreachable with a working rng: `ck_rng_fill` refuses repeated words) | same |
745| `provisioned_generation` | `store.generation() + 1` (:738) | `cr_source.generation(ctx) + 1` (new field, NULL for fakes; provision refuses `CR_E_ARG` without it); the test checks it equals the generation after the commit | same value |
746| resume returns | `(Continuity, committed)` with the full record list | `cr_view` (count + `memory` digest) and `*committed` | as 9.1 |
747| commit sees a stale `current` | not checked (the caller passes a `Continuity`) | not checked either: the new manifest names `cur->manifest_id` as previous, and a stale `cur` therefore gives a manifest the next resolve refuses (sequence gap or fork), never a silent overwrite | UNVERIFIED by a dedicated test; the chain check (INV-6, `t_chain`, cut 3) is what catches it |
748| object limits | AgentState up to 256 branches (20544 bytes), WAL segment up to 64 records (67872 bytes) | K-1 cap: an object over 16384 bytes is `Limit` and nothing is written; 204 branches commit, 205 do not; 64 x 1024 byte records do not; 15 x 1024 byte records do and read back whole | C-only Limit, tested at the bound (`t_size_bound`); a 65-record segment is `Limit "WAL segment record count"` as in Rust |
749| 65th WAL segment | `Limit("cortex WAL needs compaction")` | same text; 64 segments commit, the 65th is refused with nothing written | same |
750| marker text for a Store error | `STOP (<Debug of the Rust error>)` | `STOP (Store(<rc>))` with the C error number; `STOP (ReadOnly)`, `STOP (AlreadyProvisioned)`, `STOP (Limit("..."))`, `UNPROVISIONED`, `CONFLICT`, `CORRUPT (<why>)`, `NO_ENTROPY` match the Rust spelling | the QEMU rows for store-error cases will be DIFFERS |
751| crash campaign | 7 Store checkpoints (kill points) per commit; provisioning not cut | 9 (7 Store + 2 anchor) plus every block boundary, for commit AND provisioning; `after_inactive_superblock` gives the NEW state (Rust "either") | stronger; anchor points reopen at `SS_RB_PREPARED_ADVANCE` and show the new state, as 6.1 predicted |
752| agent id word order | four RNDR words stored little-endian, word 0 first | whatever `ck_rng_fill` writes (it is the C kernel's entropy interface, entropy.h:53) | UNVERIFIED on hardware: the byte order is the rng module's; the test checks the agent equals `ck_rng_fill` output, not the Rust word layout |
753
754### 9.3 Cut 5 (Recovery Core): C versus the Rust code, and what C does on purpose
755
756Where code and contract differ, the safer behaviour (refuse) was chosen. Host evidence:
757`tests/test_continuity_recovery.c` (real sealed Store in a file, 4096 and 512 byte blocks; one test per Rust
758recovery_core test row 91d-91k; rows 91l-91p are `test_continuity_resolve`, cut 3). Every inspection and every refused
759action is checked to leave the image byte for byte unchanged.
760
761| item | Rust | C (cut 5) | effect |
762|---|---|---|---|
763| entry reasons | `EntryReason` (recovery_core.rs:25-33) | same, plus C-only `RC_SEALED_REFUSAL` for a sealed mount refusal (`SS_E_*`, -301..-314); `ST_M_UNFORMATTED` is `StoreUnformatted`, any other `ST_M_*` is `StoreMount(rc)` | a sealed refusal admits no action (INV-15); the Store does not mount, so the record has no mount, challenge or catalog |
764| flipped byte of the newest AgentRoot (rows 91, 91g, 91j) | mounts degraded on the older generation (`Degraded(GraphBadNewer)`), or a mount error | `ss_open` refuses with `SS_E_ROLLBACK` (-303): entry reason `SealedRefusal(-303)`, no challenge, no action | identity-loss rows stay DIFFERS: the observable is a sealed refusal, not `Degraded(GraphBadNewer)` (K-3, K-4) |
765| catalog counts in the record | roots, manifests, other over the Store catalog | counts of verified application objects (sealed claims): roots, manifests, other; the Store's own envelope and transaction-record entries are not counted | `other` differs from Rust for the same history |
766| MR-5 killing test | 91g (a degraded mount whose valid root has no identity) | 91h (garbage in the inactive slot of a formatted store). In C row 91g never mounts (see above), so it cannot show a Degraded-without-identity mount | the mutant `RC_MUTANT_DEGRADED_IS_UNPROVISIONED` is killed by 91h; the 91g C test checks the refusal and that nothing is offered |
767| agent id in provisioning | the caller passes `agent_id` | the id is drawn from the kernel entropy interface inside `cr_provision` (cut 4: no fallback); `rc_provision_identity` takes the rng | the test checks the stored root (source Operator, Store uuid from the inspection, generation), not a pinned id |
768| provisioning without entropy | not reachable (caller supplies the id) | authorised, then `RC_CONTINUITY` with `CR_NO_ENTROPY`, nothing written (`RECOVERY_REFUSED (Continuity(NoEntropy))`) | C-only refusal, tested |
769| operator key | `TEST_ONLY_OPERATOR_KEY` in the boot crate | a parameter; this file holds no key and prints no `RECOVERY_OPERATOR_KEY: TEST-ONLY` | the key handling and its `CK_HARDWARE_STAGING` refusal belong to the wiring cut (section 4 PROPOSED) |
770| repair after a failed unit write | `RecoveryError::Io` | `RC_IO`; the repair writes the single inactive unit then flushes, no power cut test | UNVERIFIED for a cut during the repair write; the inactive slot is zero, malformed or half written either way and the active root is untouched |
771| inspection mount | `Store::open` borrows the device | `ss_open` into a caller-owned `ss_store`; it stays open after `rc_inspect` and an action reuses it for provisioning, with nothing written between the authorisation and the write | same ordering (inspect, applicable, verify, write) |
772| marker `RECOVERY_RECORD:` | printed on entry | not implemented | wiring cut |