omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / physics0 / pd0_wire.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 | /* PD-0 wire records (spec sections 2.2, 2.3, 2.5): describe record, |
| 18 | * PD0REC1 observation record with SHA-256 hash chain, and the request bytes |
| 19 | * of the three calls. Little-endian fixed-width integers, no floating point |
| 20 | * in any canonical byte. Shared by the world process and the learner; holds |
| 21 | * no generator knowledge. |
| 22 | * |
| 23 | * Request bytes (one request per call, length-prefixed u32 on a pipe): |
| 24 | * describe: op u8 = 0 |
| 25 | * reset: op u8 = 1, n_obs u8, values i64[n_obs] |
| 26 | * step: op u8 = 2, channel u8 (255 = none), value i64 |
| 27 | * Response: the describe record (op 0) or one PD0REC1 record (op 1, 2). */ |
| 28 | #ifndef PD0_WIRE_H |
| 29 | #define PD0_WIRE_H |
| 30 | |
| 31 | #include <stddef.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #define PD0_MAX_OBS 4 |
| 35 | #define PD0_MAX_CH 2 |
| 36 | #define PD0_HASH 32 |
| 37 | #define PD0_CH_NONE 255 |
| 38 | #define PD0_BOUND 10000000LL /* |v| <= 10.0 units */ |
| 39 | #define PD0_EPISODE_STEPS 100u |
| 40 | |
| 41 | enum { PD0_OK = 0, PD0_REFUSED_RANGE = 1, PD0_OUT_OF_BOUNDS = 2, PD0_EPISODE_END = 3, PD0_BUDGET_EXHAUSTED = 4 }; |
| 42 | enum { PD0_KIND_RESET = 0, PD0_KIND_STEP = 1 }; |
| 43 | enum { PD0_OP_DESCRIBE = 0, PD0_OP_RESET = 1, PD0_OP_STEP = 2, |
| 44 | PD0_OP_SCORE = 3, PD0_OP_SHAPE = 4, |
| 45 | PD0_OP_FINAL = 5, PD0_OP_AUDIT = 6 }; /* protocol v2 rev 2 (docs/physics0/PD0_PROTOCOL_V2.md) */ |
| 46 | |
| 47 | typedef struct { |
| 48 | uint8_t n_obs, n_channels; |
| 49 | int64_t dt_micro; |
| 50 | int64_t chan_min[PD0_MAX_CH], chan_max[PD0_MAX_CH]; |
| 51 | int64_t reset_min[PD0_MAX_OBS], reset_max[PD0_MAX_OBS]; /* per observed variable (PD0DESC2) */ |
| 52 | uint32_t episode_max_steps, budget_steps, budget_episodes; |
| 53 | } pd0_desc; |
| 54 | |
| 55 | typedef struct { |
| 56 | uint8_t n_obs, status, kind, channel; |
| 57 | uint64_t seq; |
| 58 | uint32_t episode, step_in_episode; |
| 59 | int64_t time_micro, requested, applied; |
| 60 | int64_t before[PD0_MAX_OBS], after[PD0_MAX_OBS]; |
| 61 | uint8_t prev_hash[PD0_HASH], hash[PD0_HASH]; |
| 62 | } pd0_rec; |
| 63 | |
| 64 | #define PD0_DESC_MAX (8 + 2 + 8 + 16 * PD0_MAX_CH + 16 * PD0_MAX_OBS + 12) |
| 65 | #define PD0_DESC_REFUSED_V1 (-2) /* pd0_desc_decode: a PD0DESC1 record (single reset pair) is refused explicitly */ |
| 66 | #define PD0_REC_SIZE(n_obs) (56u + 16u * (unsigned)(n_obs) + 2u * PD0_HASH) |
| 67 | #define PD0_REC_MAX PD0_REC_SIZE(PD0_MAX_OBS) |
| 68 | #define PD0_REQ_MAX (2 + 8 * PD0_MAX_OBS) |
| 69 | #define PD0_SCORE_STEPS_MAX 100u /* protocol v2 score op: longest schedule */ |
| 70 | #define PD0_REQ_MAX_V2 (3 + 8 * PD0_MAX_OBS + 9 * PD0_SCORE_STEPS_MAX) |
| 71 | #define PD0_SCORE_RESP_MAX (2 + 8 * PD0_MAX_OBS * PD0_SCORE_STEPS_MAX) |
| 72 | #define PD0_SHAPE_RESP_MAX 1024u |
| 73 | #define PD0_AUDIT_RESP_SIZE 57u |
| 74 | |
| 75 | /* encode returns bytes written, 0 on bad input; decode returns 0 ok, -1 refused */ |
| 76 | size_t pd0_desc_encode(const pd0_desc *d, uint8_t *out, size_t cap); |
| 77 | int pd0_desc_decode(const uint8_t *in, size_t len, pd0_desc *d); |
| 78 | /* Fills rec->hash (SHA-256 of every preceding byte) while encoding. */ |
| 79 | size_t pd0_rec_encode(pd0_rec *r, uint8_t *out, size_t cap); |
| 80 | /* Refuses unknown version, wrong length or a hash that does not recompute. */ |
| 81 | int pd0_rec_decode(const uint8_t *in, size_t len, pd0_rec *r); |
| 82 | /* 1 if r->prev_hash equals prev->hash (prev NULL: seq 0 with zero prev_hash). */ |
| 83 | int pd0_rec_chained(const pd0_rec *r, const pd0_rec *prev); |
| 84 | |
| 85 | size_t pd0_req_describe(uint8_t *out); |
| 86 | /* Returns 0 unless n_obs, pointers and output capacity describe a complete reset request. */ |
| 87 | size_t pd0_req_reset(uint8_t n_obs, const int64_t *vals, uint8_t *out, size_t cap); |
| 88 | size_t pd0_req_step(uint8_t channel, int64_t value, uint8_t *out); |
| 89 | |
| 90 | /* little-endian helpers shared by the law record encoder */ |
| 91 | void pd0_put_u16(uint8_t *p, uint16_t v); |
| 92 | void pd0_put_u32(uint8_t *p, uint32_t v); |
| 93 | void pd0_put_u64(uint8_t *p, uint64_t v); |
| 94 | uint16_t pd0_get_u16(const uint8_t *p); |
| 95 | uint32_t pd0_get_u32(const uint8_t *p); |
| 96 | uint64_t pd0_get_u64(const uint8_t *p); |
| 97 | |
| 98 | #endif |