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 / reply_obj.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 | /* reply_obj.h -- the v1 approved-reply Store object (kind 0x5231 'R1'), shared by the service lifecycle |
| 18 | * (core/lifecycle.c, commit_task: QEMU rows 175, 205, 206 of GATES.md) and the Pi agent stage |
| 19 | * (svc/pi5_agent.c, PI5-6). One builder, so the two cannot drift. |
| 20 | * |
| 21 | * Bytes: "aienos.reply.v1\nprompt: " + prompt + "\nreply: " + reply + "\n" |
| 22 | * and, only when the caller passes a trailer (the Pi agent does, the lifecycle does not): |
| 23 | * "model: " + 64 lowercase hex + "\nids: " + decimal token ids separated by single spaces + "\n". |
| 24 | * With no trailer the output is byte for byte what lifecycle.c wrote before this header existed (N3: lifecycle.c now |
| 25 | * appends one "context: sha256=<64 hex> memory=<n>\n" line after this object; the Pi agent does not, its bytes are unchanged) |
| 26 | * (dev/tests/pi5_agent_test.c pins it against a literal). */ |
| 27 | #ifndef AIENOS_CK_REPLY_OBJ_H |
| 28 | #define AIENOS_CK_REPLY_OBJ_H |
| 29 | #include <stddef.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #define CK_REPLY_OBJ_KIND 0x5231u /* 'R1' */ |
| 33 | #define CK_REPLY_OBJ_HDR "aienos.reply.v1\nprompt: " |
| 34 | #define CK_REPLY_OBJ_SEP "\nreply: " |
| 35 | #define CK_REPLY_OBJ_MODEL "model: " |
| 36 | #define CK_REPLY_OBJ_IDS "\nids: " |
| 37 | #define CK_REPLY_OBJ_MAX_IDS 16u |
| 38 | /* Largest trailer: "model: " + 64 + "\nids: " + 16 ids of up to 10 digits and a space + "\n". */ |
| 39 | #define CK_REPLY_OBJ_TRAILER_MAX (7 + 64 + 6 + 16 * 11 + 1) |
| 40 | |
| 41 | static inline void ck_reply_put(uint8_t *buf, size_t *n, const char *s, size_t len) |
| 42 | { |
| 43 | size_t i; |
| 44 | for (i = 0; i < len; i++) buf[(*n)++] = (uint8_t)s[i]; |
| 45 | } |
| 46 | |
| 47 | /* Builds the object into buf (the caller sized it for prompt + reply + trailer + the fixed text). |
| 48 | * model_hex64: NULL for no trailer. ids/nids: the token ids, only used with a trailer. Returns the |
| 49 | * length. */ |
| 50 | static inline size_t ck_reply_obj_build(uint8_t *buf, const char *prompt, size_t plen, const uint8_t *reply, |
| 51 | size_t rlen, const char *model_hex64, const uint32_t *ids, unsigned nids) |
| 52 | { |
| 53 | size_t n = 0, i; |
| 54 | ck_reply_put(buf, &n, CK_REPLY_OBJ_HDR, sizeof CK_REPLY_OBJ_HDR - 1); |
| 55 | ck_reply_put(buf, &n, prompt, plen); |
| 56 | ck_reply_put(buf, &n, CK_REPLY_OBJ_SEP, sizeof CK_REPLY_OBJ_SEP - 1); |
| 57 | for (i = 0; i < rlen; i++) buf[n++] = reply[i]; |
| 58 | buf[n++] = '\n'; |
| 59 | if (model_hex64) { |
| 60 | unsigned k; |
| 61 | ck_reply_put(buf, &n, CK_REPLY_OBJ_MODEL, sizeof CK_REPLY_OBJ_MODEL - 1); |
| 62 | ck_reply_put(buf, &n, model_hex64, 64); |
| 63 | ck_reply_put(buf, &n, CK_REPLY_OBJ_IDS, sizeof CK_REPLY_OBJ_IDS - 1); |
| 64 | for (k = 0; k < nids && k < CK_REPLY_OBJ_MAX_IDS; k++) { |
| 65 | char d[10]; |
| 66 | unsigned nd = 0; |
| 67 | uint32_t v = ids[k]; |
| 68 | if (k) buf[n++] = ' '; |
| 69 | do { d[nd++] = (char)('0' + v % 10); v /= 10; } while (v); |
| 70 | while (nd) buf[n++] = (uint8_t)d[--nd]; |
| 71 | } |
| 72 | buf[n++] = '\n'; |
| 73 | } |
| 74 | return n; |
| 75 | } |
| 76 | #endif |