aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / context_proposal.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* context_proposal.h -- exact preview / approval / commit binding for the |
| 18 | * resident lifecycle (native v1 gate N3, aienos#329). |
| 19 | * |
| 20 | * Pure logic (no kernel calls; SHA-256 of native/argus): host-tested by |
| 21 | * tests/test_context.c; bound to the sealed Store by core/lifecycle.c. |
| 22 | * |
| 23 | * When a proposal becomes pending (`task <text>` accepted, or a generated |
| 24 | * reply), the binding records: |
| 25 | * request (incarnation, seq): the durable continuity incarnation of this |
| 26 | * boot (svc/continuity_service.c, +1 every boot) and a per-boot |
| 27 | * proposal counter, so a request id is never reused across |
| 28 | * restarts of the same agent; |
| 29 | * gen the Store generation the proposal was made against (the |
| 30 | * authoritative state the operator saw); |
| 31 | * kind, len, sha the exact artifact bytes shown for approval (SHA-256); |
| 32 | * key the idempotence key SHA-256("aienos.commit.key.v1" 0x00 || |
| 33 | * agent || inc || seq || gen || kind || sha) (integers LE). |
| 34 | * `approve` rebuilds the bytes from the pending state and commits only when |
| 35 | * ck_prop_check passes: absent (nothing bound), changed (other bytes or kind) |
| 36 | * and stale (the Store generation moved since the proposal: something else |
| 37 | * committed in between) are refused and nothing is written. The committed |
| 38 | * object is the artifact bytes followed by the trailer of ck_prop_trailer, |
| 39 | * whose last line is "key: <64 hex>\n"; before committing, the lifecycle |
| 40 | * scans the committed objects for that key and refuses a replay |
| 41 | * (ck_prop_has_key). `cancel`, `deny` and `shutdown` only forget the binding: |
| 42 | * nothing is written. A pending proposal is memory only: it never survives a |
| 43 | * restart, and a new boot has a new incarnation, so no restart can approve |
| 44 | * (and so duplicate) an earlier proposal. */ |
| 45 | #ifndef AIENOS_CK_CONTEXT_PROPOSAL_H |
| 46 | #define AIENOS_CK_CONTEXT_PROPOSAL_H |
| 47 | |
| 48 | #include <stddef.h> |
| 49 | #include <stdint.h> |
| 50 | |
| 51 | enum { |
| 52 | CK_PROP_OK = 0, |
| 53 | CK_PROP_E_ABSENT = -1, /* nothing bound */ |
| 54 | CK_PROP_E_CHANGED = -2, /* bytes or kind differ from the bound ones */ |
| 55 | CK_PROP_E_STALE = -3, /* the Store generation moved since the proposal */ |
| 56 | }; |
| 57 | |
| 58 | /* "\nrequest: inc=<u64> seq=<u64> gen=<u64>\nsha256: <64>\nkey: <64>\n" upper bound */ |
| 59 | #define CK_PROP_TRAILER_MAX (1 + 14 + 3 * 25 + 1 + 8 + 64 + 1 + 5 + 64 + 1) |
| 60 | |
| 61 | typedef struct { |
| 62 | int active; |
| 63 | uint64_t inc, seq, gen; |
| 64 | uint16_t kind; |
| 65 | size_t len; |
| 66 | uint8_t sha[32]; |
| 67 | uint8_t key[32]; |
| 68 | } ck_prop; |
| 69 | |
| 70 | void ck_prop_clear(ck_prop *p); |
| 71 | /* Bind the pending artifact. agent: the provisioned agent id (32 bytes). */ |
| 72 | void ck_prop_bind(ck_prop *p, const uint8_t agent[32], uint64_t inc, uint64_t seq, uint64_t gen, uint16_t kind, |
| 73 | const uint8_t *bytes, size_t len); |
| 74 | /* 0 when `bytes` (kind) are exactly the bound ones and the Store is still at |
| 75 | * the bound generation; else a CK_PROP_E_* code. */ |
| 76 | int ck_prop_check(const ck_prop *p, uint64_t gen_now, uint16_t kind, const uint8_t *bytes, size_t len); |
| 77 | /* Write the trailer; returns its length, 0 when cap is too small or nothing is bound. */ |
| 78 | size_t ck_prop_trailer(const ck_prop *p, char *out, size_t cap); |
| 79 | /* 1 when a committed object ends with the trailer line "key: <hex(key)>\n". */ |
| 80 | int ck_prop_has_key(const uint8_t *obj, size_t len, const uint8_t key[32]); |
| 81 | const char *ck_prop_strerror(int rc); |
| 82 | |
| 83 | #endif |