diff --git a/.crumb b/.crumb index 2041ab198..9e989e48e 100644 --- a/.crumb +++ b/.crumb @@ -42,68 +42,68 @@ "children": [ { "digest": "9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "f5de159859ca96b6aea4337601d0d6f2fe39bc2f8154946e28f1a21c74c7a186", - "last_commit": "876d00ff5", + "last_commit": "876d00ff", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116cc", + "last_commit": "30bb116c", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "d8e540575553c9c3f243fd496cdfc03d65841d1a2c2d05005b8678489feba859", - "last_commit": "39d07c91a", + "digest": "923cd13e5fe85de13e60ff691ae36cf48ebf46c68f6b18bf384cb8cbfa1e51cf", + "last_commit": "7f96349d", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "590f1f3a26a12dfd9df281ec944b882b85fd4ac2ae07a88a03bb8bd0d7d62536", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { "digest": "6327473d41e10acbf43be0918d3557509322bfd7d6e075e114c79c2600a47305", - "last_commit": "43d8d20da", + "last_commit": "43d8d20d", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "86a4a717758e007f216d8cd7cacf339dcd9666cdd90029572c0c243f2d4079bb", - "last_commit": "a1cfb4a31", + "digest": "66ca726ce59119ab69047337281862853cfc8d126d8e64a87812988f34d52639", + "last_commit": "9b927e45", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "2b63b8ab9af7638881a81c7617109ab9987c2419fb4e0e25079c42309280b866", - "last_commit": "d94a3ab89", + "digest": "df56d91df565d41851b97ec989ebaf0a2b164b4ca5b5c4099bee59a5839592df", + "last_commit": "9b927e45", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "c86cd0893d1f95dee1febdf19efe3ae5a2fcc2ec2b619f9a876a4a28b80bf548", - "last_commit": "39d07c91a", + "last_commit": "39d07c91", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:47b4b1b551e499f3ea8c7250dd4d0b972280643720269322aba84be82167e02a", - "compiled_at": "2026-10-25T13:57:08Z", + "children_root": "sha256:f6c95c31f024f389c6523ea6f220a57cf741556a6dfc11a03bf55800c056b953", + "compiled_at": "2026-10-25T18:53:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:57fab59c8f46d3af2deabaa08f911574503a3685b65c27f01160c47b6092b2a6", + "digest": "sha256:2467e17ee83948d8f3ec8aad8dc9d2f4857e5a3a95510e0580e076538c11d655", "evidence_files": 11666, "evidence_root": "sha256:91249b110c57333badc93f4052f27ad9faba72fbbac89de4d1512a9ec8808721", - "files": 15005, - "generated_at_commit": "1f7c0ab131ab427f334ea4151abf0911c19beaf5", - "source_tree": "sha256:ea63a4f1d3f31bcf1a318eba722a45a25d94ae1625f9d8db3956b524bcc47594" + "files": 15010, + "generated_at_commit": "9b927e456a89cd60f99bda6ec2fa43e6d553689f", + "source_tree": "sha256:625887811670231c29c1a87ad962d6cfd45e32586b03dca0f7a906de89fa8b1f" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index 93796c292..9326c34fe 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-25T03:10:13Z", + "compiled_at": "2026-10-25T18:53:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:d8e540575553c9c3f243fd496cdfc03d65841d1a2c2d05005b8678489feba859", + "digest": "sha256:923cd13e5fe85de13e60ff691ae36cf48ebf46c68f6b18bf384cb8cbfa1e51cf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 74, - "generated_at_commit": "393aa3055e655913f1dc7fb69bf5404fb9f7b9a5", - "source_tree": "sha256:9cdcd24234f4b0247a8311a01b94b1c9ce42e5b2e619cfc268a7c09ff1c9d237" + "files": 75, + "generated_at_commit": "9b927e456a89cd60f99bda6ec2fa43e6d553689f", + "source_tree": "sha256:0bc67152c230bef978791b7dd3f24407e0a7fb5122fcc9733adf27c2659ee751" }, "owner_lane": "", "provenance": { diff --git a/mk/experience.mk b/mk/experience.mk new file mode 100644 index 000000000..cf256c42a --- /dev/null +++ b/mk/experience.mk @@ -0,0 +1,31 @@ +# omega #699: verified state/action/next-state experience in canonical Cortex. +# +# test-experience walk1d family, accept chain -> CX_K_EXPERIENCE, refusal of +# generated-but-unobserved records, consent revocation in the +# dataset view (incl. restart), determinism across seeds. +# +# Not part of `all` or `test`. Host only; no GPU. Production flags (no +# AIEN_TEST_BUILD): the test uses no composition fault hooks. It links the same +# COMPOSITION-2 sources as test-composition plus rx_experience.c, rx_exp_walk.c +# and the PD0 stream primitive. +# +# This file is read before the top-level Makefile defines RX_COMPOSE_* and +# AIENOS_CAP_LIB, so prerequisites naming them are written $$(...) and expanded +# in the second phase (.SECONDEXPANSION, as mk/rx_compose_lib.mk does). +ifndef EXPERIENCE_MK +EXPERIENCE_MK := 1 +.PHONY: test-experience +.SECONDEXPANSION: + +RX_EXP_SRCS = src/runtime/rx_experience.c src/runtime/rx_exp_walk.c src/physics0/pd0_rng.c +RX_EXP_TEST = $(OUT_DIR)/rx_experience_test + +$(RX_EXP_TEST): $$(RX_COMPOSE_DEPS) $(RX_EXP_SRCS) src/runtime/rx_experience.h \ + tests/runtime/rx_experience_test.c | $(OUT_DIR) + $(CC) $(CFLAGS) -pthread -o $@ $(RX_COMPOSE_SRCS) $(RX_EXP_SRCS) \ + tests/runtime/rx_experience_test.c $(RX_COMPOSE_LINK) + +test-experience: $(RX_EXP_TEST) + ./$(RX_EXP_TEST) + +endif diff --git a/src/.crumb b/src/.crumb index f4aad15b4..7a4958b39 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,140 +62,140 @@ "children": [ { "digest": "728ab6985dea994eb78a7eb2521384a911492d1c36819c17970a5dc3a8a0c5e2", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "1881ed1d139a405a777d24d4b26f97bc8405dee0552d6ce21841ce4833560ce2", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "92c3e2481f7457afd6c7717e6c1a75ebab950ec9114b48025dd137a6edb2975c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "3bd51397fcd28cd54db65c1df72c320883798bc8550779b2b79c8498b28ffb0d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "36a66980895553ae73884798bc09a92141e1aeba5e5068a7d9d62a99bad02548", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "c75df77ddb2bae35ac5e1225a0ae9dcef77b24608775c7b8d02dce1af0ec3ca4", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "8856d5162c55cb33997201cd580638f80ea933b46e11c61e1913185d419731d7", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { "digest": "391e337c60cb8b2ae549595394482c23717c1757d83747a7446e81cd780750c1", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "2d33eb528dafb5954bb027cdd9a7c7923594c177f17181194311155b4e1f40b3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "cd0551dbfb6fc354741efe9a3ad1808ffaaf6a8d7e9e7345851333d7a721cee0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, { "digest": "2b5dd2c64d4bbba8c41471dc1af5a0b00bfc1361a2c680d78b99fb30b5b547fe", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "e0b91129cc9c188fc0e47b7f3189a3a9a09f9c55eecd018786794571ffc39aad", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "8b923fb5dc1de3b04afae043a4770f7b1fb27d959a8f597191ae2556d822a1cf", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { "digest": "076f486fdf51e12a26160e7f0bf7686fea611db54216f7fef81367620e7a6bdb", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "reservoir", "purpose": "Ionic-droplet reservoir simulator (omega#440): binary32 fixed-step RK4 of the authors' droplet m" }, { - "digest": "c6af24b823d6a18a8ad1593003a214fcd577955d1d134bc4304a5c097a71cd35", - "last_commit": "d863780eb", + "digest": "db69144b0fc2d919b5bfc8c055cbcfeca131d4a10971bab73d9c6e064421e447", + "last_commit": "95d315a1", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, { "digest": "af5fcc64fe83dc35f782724372854febde5461f5eab8b327f3c3ed75f8572053", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "b82288db7e732d10f954e921ac98c60e19579a3e58118cc12c49230587929ea2", - "last_commit": "ed8de5736", + "last_commit": "ed8de573", "name": "synapse", "purpose": "Synapse ADR 0003 J1 binary codec port: layout list, generator, syn_codec.osc (A1 WAL records + c" }, { "digest": "9723dd7c18d4e2c22db631680974e1025bccb9c309619424e2f4dfcc43cc7068", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "06d9fb18dddf7ab0f8ad02fffd15ce8b80808c77d0904609b6baae83c2b05ce5", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "812a7faed07b21e066b732ed596eb0aea609379a2b2af0e8b61dd1ea05bef602", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "b2e34b2ea0392059ac30d17e27289b1146e523db60a02ef825edef96036817ab", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:4460b4a6d5ed4054d462af660a6a02691350a35a8f44197e60ce605e243f698e", - "compiled_at": "2026-10-25T13:52:36Z", + "children_root": "sha256:a81aca0651c116c677c929903a83ad01a4d2ed366d76463f4c38690555f38446", + "compiled_at": "2026-10-25T18:53:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:86a4a717758e007f216d8cd7cacf339dcd9666cdd90029572c0c243f2d4079bb", + "digest": "sha256:66ca726ce59119ab69047337281862853cfc8d126d8e64a87812988f34d52639", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 566, - "generated_at_commit": "467110acc48a26af023e3adba4c9d438bb0423c8", - "source_tree": "sha256:408bbb3ae07d71ed03e85f975e7a8f16db2e08b7f9e87534917f2ac93cf2a267" + "files": 569, + "generated_at_commit": "9b927e456a89cd60f99bda6ec2fa43e6d553689f", + "source_tree": "sha256:084bbe14a40683bdb1fafd44aee4f5db98b88fd338179e1be1f679ad410543ac" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index 5e0a0c362..a2b96e091 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-24T20:51:45Z", "compiler": "crumb-compile/1", - "digest": "sha256:c6af24b823d6a18a8ad1593003a214fcd577955d1d134bc4304a5c097a71cd35", + "digest": "sha256:db69144b0fc2d919b5bfc8c055cbcfeca131d4a10971bab73d9c6e064421e447", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 65, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:8a697a598a04d6cd0bfaf1bfea1a95800cd8cc6288b14ab5cef835155f7cf211" + "files": 68, + "generated_at_commit": "95d315a127bcfda843c5c536f12e289c471f5ca0", + "source_tree": "sha256:4ca4ca6ec47e53d12cae22b125d5e77a1654bea091230719ab0cc653779b21b7" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_cortex.h b/src/runtime/rx_cortex.h index 55502f6a3..495ff0bdf 100644 --- a/src/runtime/rx_cortex.h +++ b/src/runtime/rx_cortex.h @@ -119,8 +119,13 @@ enum { CX_K_PROMOTION, /* EVIDENCE: candidate promoted; links[0] = candidate, links[1] = evidence (cx_promote) */ CX_K_ADMISSION, /* EVIDENCE: admission decision; links name its inputs */ - CX_K_IMPORTED = CX_K_BASE + 0x20 /* CLAIM: record from a non-authoritative store; + CX_K_IMPORTED = CX_K_BASE + 0x20, /* CLAIM: record from a non-authoritative store; tag = CX_SRC_* */ + /* Experience (written only by rx_experience.c, payload RX_EXP_W_*) */ + CX_K_EXPERIENCE = CX_K_BASE + 0x30, /* EVIDENCE: verified state/action/next-state + transition; links[0] = its World commit receipt */ + CX_K_CONSENT_REVOKED /* EVIDENCE: owner withdrew consent for experiences; + links[0] = the experience, 0 = all of the owner's */ }; /* Sources of CX_K_IMPORTED records (see AUTHORITY above). */ diff --git a/src/runtime/rx_exp_walk.c b/src/runtime/rx_exp_walk.c new file mode 100644 index 000000000..33b5554ed --- /dev/null +++ b/src/runtime/rx_exp_walk.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_exp_walk.c -- walk1d, a seeded, resettable, stochastic transition family + * for verified experience (rx_experience.h has the state layout). Pure + * functions of the packed state: the noise of step k is word k of the PD0 + * SplitMix64 stream (src/physics0/pd0_rng.c) keyed by the episode key, so a + * replay needs nothing but the state and the action. + */ +#include "rx_experience.h" + +#include "physics0/pd0_rng.h" +#include "sha256.h" + +#include + +#define GOLDEN 0x9e3779b97f4a7c15ull + +enum { OFF_KEY = 0, OFF_K = 24, OFF_X = 34, OFF_V = 46, OFF_TGT = 51, OFF_ACT = 57, + OFF_NOISE = 60, OFF_LIVE = 62, OFF_OP = 63 }; +enum { W_KEY = 24, W_K = 10, W_X = 12, W_V = 5, W_TGT = 6, W_ACT = 3, W_NOISE = 2 }; +#define X_MAX 2000 +#define V_MAX 8 +#define T_MAX 20 +#define ACT_MASK (((1ull << W_ACT) - 1) << OFF_ACT) +#define OP_BIT (1ull << OFF_OP) +#define LIVE_BIT (1ull << OFF_LIVE) +#define KEY_MASK ((1ull << W_KEY) - 1) + +static uint64_t get(uint64_t s, unsigned off, unsigned w) { return (s >> off) & ((1ull << w) - 1); } +static int64_t sget(uint64_t s, unsigned off, unsigned w) { + uint64_t v = get(s, off, w); + return (v >> (w - 1)) ? (int64_t)v - (int64_t)(1ull << w) : (int64_t)v; +} +static uint64_t put(int64_t v, unsigned off, unsigned w) { + return ((uint64_t)v & ((1ull << w) - 1)) << off; +} +static int64_t clamp(int64_t v, int64_t lo, int64_t hi) { return v < lo ? lo : v > hi ? hi : v; } + +/* Word `i` of the stream keyed by `key` and `tag` (= the (i+1)-th pd0_next). */ +static uint64_t stream_word(uint64_t key, const char *tag, uint64_t i) { + pd0_rng r; + pd0_stream(&r, key, tag); + r.s += i * GOLDEN; + return pd0_next(&r); +} + +uint64_t rx_exp_walk_key(uint64_t seed) { + uint64_t s = seed; + uint64_t w = pd0_splitmix64(&s); + uint64_t k = (w ^ (w >> 24) ^ (w >> 48)) & KEY_MASK; + return k ? k : 1; +} + +static int walk_step(uint64_t in, uint64_t *next, uint64_t *cost) { + uint64_t key = get(in, OFF_KEY, W_KEY); + if (in & OP_BIT) { /* reset: only the key may be set */ + if (key == 0 || (in & ~(OP_BIT | KEY_MASK))) return -1; + int64_t tgt = (int64_t)(stream_word(key, "omega.exp.walk1d.target", 0) % (2 * T_MAX + 1)) - T_MAX; + *next = key | put(tgt, OFF_TGT, W_TGT) | LIVE_BIT; + *cost = 1; + return 0; + } + uint64_t k = get(in, OFF_K, W_K), a = get(in, OFF_ACT, W_ACT); + int64_t x = sget(in, OFF_X, W_X), v = sget(in, OFF_V, W_V), tgt = sget(in, OFF_TGT, W_TGT); + if (!(in & LIVE_BIT) || key == 0 || a < 1 || a > 5 || k >= RX_EXP_WALK_K_MAX) return -1; + if (x < -X_MAX || x > X_MAX || v < -V_MAX || v > V_MAX || tgt < -T_MAX || tgt > T_MAX) return -1; + int64_t thrust = (int64_t)a - 3; + int64_t noise = (int64_t)(stream_word(key, "omega.exp.walk1d.noise", k) % 3) - 1; + int64_t v2 = clamp(v + thrust + noise, -V_MAX, V_MAX); + int64_t x2 = clamp(x + v2, -X_MAX, X_MAX); + *next = key | put((int64_t)(k + 1), OFF_K, W_K) | put(x2, OFF_X, W_X) | put(v2, OFF_V, W_V) | + put(tgt, OFF_TGT, W_TGT) | put((int64_t)a, OFF_ACT, W_ACT) | + put(noise + 2, OFF_NOISE, W_NOISE) | LIVE_BIT; + *cost = (uint64_t)(thrust < 0 ? -thrust : thrust) + 1; + return 0; +} + +static uint64_t walk_encode(uint64_t prior, uint64_t action) { + uint64_t a = action <= 7 ? action : 0; /* out of range: code 0, refused by step */ + return (prior & ~(ACT_MASK | OP_BIT)) | (a << OFF_ACT); +} + +static uint64_t walk_reset_input(uint64_t seed) { return OP_BIT | rx_exp_walk_key(seed); } + +static uint32_t walk_unpack(uint64_t s, int64_t out[RX_EXP_MAX_FIELDS]) { + out[RX_EXP_WALK_KEY] = (int64_t)get(s, OFF_KEY, W_KEY); + out[RX_EXP_WALK_K] = (int64_t)get(s, OFF_K, W_K); + out[RX_EXP_WALK_X] = sget(s, OFF_X, W_X); + out[RX_EXP_WALK_V] = sget(s, OFF_V, W_V); + out[RX_EXP_WALK_TARGET] = sget(s, OFF_TGT, W_TGT); + out[RX_EXP_WALK_ACT] = (int64_t)get(s, OFF_ACT, W_ACT); + out[RX_EXP_WALK_NOISE] = (int64_t)get(s, OFF_NOISE, W_NOISE); + out[RX_EXP_WALK_LIVE] = (int64_t)get(s, OFF_LIVE, 1); + return RX_EXP_WALK_FIELDS; +} + +static void le64(uint8_t *b, uint64_t v) { for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (8 * i)); } + +/* Objective: reach the episode's target. Bound to the key and the target. */ +static void walk_objective(uint64_t s, uint8_t out[32]) { + static const char tag[] = "omega.exp.walk1d.objective.v1"; + uint8_t b[sizeof tag - 1 + 16]; + memcpy(b, tag, sizeof tag - 1); + le64(b + sizeof tag - 1, get(s, OFF_KEY, W_KEY)); + le64(b + sizeof tag - 1 + 8, (uint64_t)sget(s, OFF_TGT, W_TGT)); + sha256_hash(b, sizeof b, out); +} + +static uint64_t walk_skill(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { + (void)attempt; + uint64_t next = 0, cost = 0; + *failed = n < 1 || walk_step(in[0], &next, &cost) != 0; + return *failed ? 0 : next; +} + +static int walk_contract(uint64_t input, uint64_t result) { + uint64_t next = 0, cost = 0; + return walk_step(input, &next, &cost) == 0 && next == result; +} + +const RxExpFamily rx_exp_walk1d = { + .family = RX_EXP_FAMILY_WALK1D, + .version = 1, + .op = 1, + .name = "omega.exp.walk1d", + .domain = ((uint64_t)V_MAX << 32) | X_MAX, + .step = walk_step, + .encode = walk_encode, + .reset_input = walk_reset_input, + .unpack = walk_unpack, + .objective = walk_objective, + .skill = walk_skill, + .contract = walk_contract, +}; diff --git a/src/runtime/rx_experience.c b/src/runtime/rx_experience.c new file mode 100644 index 000000000..3e4e546ec --- /dev/null +++ b/src/runtime/rx_experience.c @@ -0,0 +1,571 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_experience.c -- verified experience on the composition accept chain + * (rx_experience.h). Every write goes through the composition's attached + * Cortex link (rx_cortex_append_in); this file adds no writer, World, + * scheduler or store. + */ +#include "rx_experience.h" + +#include "rx_cortex_record.h" +#include "sha256.h" + +#include +#include + +#define EXP_SKILL_ID 0x699u +#define EXP_NOW_US 1000000ull + +const char *rx_exp_strerror(int rc) { + switch (rc) { + case RX_EXP_OK: return "ok"; + case RX_EXP_ERR_ARG: return "bad argument"; + case RX_EXP_ERR_UNOBSERVED: return "no environment receipt: not an observation"; + case RX_EXP_ERR_FORGED: return "receipt not written by the World recorder for a live commit"; + case RX_EXP_ERR_MISMATCH: return "observed state differs from the receipt"; + case RX_EXP_ERR_ORDER: return "not the pending transition of this environment"; + case RX_EXP_ERR_DUPLICATE: return "receipt already used by an experience"; + case RX_EXP_ERR_REPLAY: return "transition does not replay"; + case RX_EXP_ERR_UNVERIFIED: return "no AEGIS pass, promotion or matching goal"; + case RX_EXP_ERR_CONSENT: return "missing owner, consent, privacy or split"; + case RX_EXP_ERR_OWNER: return "revocation by someone other than the owner"; + case RX_EXP_ERR_HYPOTHESIS: return "hypothesis is not one for this prior and action"; + case RX_EXP_ERR_REFUSED: return "environment refused the action"; + case RX_EXP_ERR_WORLD: return "composition, World or Cortex failure"; + default: return "unknown"; + } +} + +static void le64(uint8_t *b, uint64_t v) { for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (8 * i)); } + +static void to_words(const uint8_t d[32], uint64_t *w) { + for (int i = 0; i < 4; i++) { + w[i] = 0; + for (int b = 0; b < 8; b++) w[i] |= (uint64_t)d[8 * i + b] << (8 * b); + } +} + +static void state_digest(const RxExpFamily *f, uint64_t state, uint8_t out[32]) { + static const char tag[] = "omega.exp.state.v1"; + uint8_t b[sizeof tag - 1 + 24]; + memcpy(b, tag, sizeof tag - 1); + le64(b + sizeof tag - 1, f->family); + le64(b + sizeof tag - 1 + 8, f->version); + le64(b + sizeof tag - 1 + 16, state); + sha256_hash(b, sizeof b, out); +} + +static void seed_commit(uint64_t seed, uint8_t out[32]) { + static const char tag[] = "omega.exp.seed.v1"; + uint8_t b[sizeof tag - 1 + 8]; + memcpy(b, tag, sizeof tag - 1); + le64(b + sizeof tag - 1, seed); + sha256_hash(b, sizeof b, out); +} + +static void skill_identity(const RxExpFamily *f, uint8_t out[32]) { + sha256_ctx c; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)"omega.exp.family.v1", 19); + sha256_update(&c, (const uint8_t *)f->name, strlen(f->name)); + uint8_t b[16]; + le64(b, f->family); + le64(b + 8, f->version); + sha256_update(&c, b, sizeof b); + sha256_final(&c, out); +} + +static SrRequirement requirement(uint32_t op) { + SrRequirement q; + memset(&q, 0, sizeof q); + q.need.semantic_operation = op; + q.need.accepted_input_types = ~0ull; + q.need.effect_class = 0x1FF; + q.need.authority_ceiling.resource_hi = UINT64_MAX; + q.need.authority_ceiling.rights = RX_RIGHT_READ | RX_RIGHT_WRITE; + q.t.n_order = 1; + q.t.order[0] = CQ_DIM_COST; + q.t.k = CQ_MAX_K; + q.local_only = 1; + return q; +} + +/* The environment Skill is the only provider of the family operation. */ +static int build_graph(RxExpEnv *e, const AienMachineId *self) { + const RxExpFamily *f = e->fam; + aien_mid_index_init(&e->ix, e->slots, 8); + if (cq_catalog_init_canonical(&e->cat, &e->ix, self, 8, 8) != CQ_OK) return -1; + uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC); + if (cq_op_define(&e->cat, f->op, 0, all) != CQ_OK) return -1; + e->skills.n = 1; + e->skills.skill[0].id = EXP_SKILL_ID; + e->skills.skill[0].fn = f->skill; + skill_identity(f, e->skills.skill[0].identity); + SrSkill s; + memset(&s, 0, sizeof s); + s.skill_id = EXP_SKILL_ID; + s.version = f->version; + memcpy(s.digest, e->skills.skill[0].identity, 32); + s.machine = e->cat.self_machine; + CqEntry p; + memset(&p, 0, sizeof p); + p.capability_id = 1; + p.realization_id = 1; + p.op = f->op; + p.effects = CQ_FX_PURE; + p.in_types = 0x1; + p.out_types = 0x2; + p.confidence_ppm = 1000000; + p.reliability_ppm = 1000000; + p.evidence_level = CQ_EV_MEASURED; + p.evidence_ref = f->family; + p.cost = 1; + p.latency_us = 1; + p.energy_uj = 1; + p.live = CQ_LIVE_AVAILABLE; + p.generation = 1; + if (sr_register_skill(&e->cat, &s, &p, 1) != SR_OK) return -1; + if (cq_catalog_build(&e->cat) != CQ_OK) return -1; + e->router = (SrRouter){ &e->cat, &e->skills }; + return 0; +} + +int rx_exp_open(RxExpEnv *e, const RxExpFamily *fam, const char *dir, const AienMachineId *self, + uint64_t session, AienosCapAdmin *admin, AienosCapView *view) { + if (!e || !fam || !dir || !self || !admin || !view) return RX_EXP_ERR_ARG; + memset(e, 0, sizeof *e); + e->fam = fam; + e->now_us = EXP_NOW_US; + if (build_graph(e, self) != 0) { + cq_catalog_free(&e->cat); + return RX_EXP_ERR_WORLD; + } + if (rx_compose_open(&e->c, dir, self, session, &e->router, fam->contract, admin, view, 1) != + RX_OK) { + cq_catalog_free(&e->cat); + return RX_EXP_ERR_WORLD; + } + e->open = 1; + return RX_EXP_OK; +} + +void rx_exp_close(RxExpEnv *e) { + if (!e || !e->open) return; + rx_compose_close(&e->c); + cq_catalog_free(&e->cat); + e->open = 0; +} + +uint64_t rx_exp_state(RxExpEnv *e) { + RxObject so; + if (!e || !e->open || rx_world_read(e->c.world, e->c.state, &so) != RX_OK) return 0; + return so.field[RXC_S_RESULT]; +} + +/* Append through the composition's Cortex writer link. */ +static int put(RxExpEnv *e, CxHeader *h, const uint64_t *p, uint32_t n, uint64_t *id) { + if (!e->c.attached) return RX_EXP_ERR_WORLD; + h->t = rx_cortex_next_t_in(e->c.world, &e->c.cx); + return rx_cortex_append_in(e->c.world, &e->c.cx, h, p, n, id) == RX_OK ? RX_EXP_OK + : RX_EXP_ERR_WORLD; +} + +static void drop_pending(RxExpEnv *e) { + if (e->pend.live) e->dropped++; + memset(&e->pend, 0, sizeof e->pend); +} + +static int run_goal(RxExpEnv *e, uint64_t input, RxcResult *r) { + SrRequirement q = requirement(e->fam->op); + return rx_compose_run(&e->c, input, &q, NULL, e->now_us, r) == RX_OK ? RX_EXP_OK + : RX_EXP_ERR_WORLD; +} + +int rx_exp_reset(RxExpEnv *e, uint64_t seed) { + if (!e || !e->open) return RX_EXP_ERR_ARG; + drop_pending(e); + RxcResult r; + if (run_goal(e, e->fam->reset_input(seed), &r) != RX_EXP_OK) return RX_EXP_ERR_WORLD; + if (r.outcome == RXC_OUT_NO_WINNER || r.outcome == RXC_OUT_NOT_COMMITTED) + return RX_EXP_ERR_REFUSED; + if (r.outcome != RXC_OUT_COMMITTED) return RX_EXP_ERR_WORLD; + e->seed = seed; + seed_commit(seed, e->seed_commit); + e->fam->objective(r.result, e->objective); + e->episode++; + e->step = 0; + return RX_EXP_OK; +} + +int rx_exp_hypothesis(RxExpEnv *e, uint64_t predictor, uint64_t action, uint64_t predicted, + uint64_t *out_id) { + if (!e || !e->open) return RX_EXP_ERR_ARG; + uint64_t p[RX_EXP_H_WORDS] = { RX_EXP_VERSION, e->fam->family, rx_exp_state(e), action, + predicted, predictor }; + CxHeader h; + memset(&h, 0, sizeof h); + h.cls = CX_CLAIM; + h.kind = CX_K_CANDIDATE; + h.subject = RX_EXP_SUBJECT; + h.tag = RX_EXP_TAG_HYPOTHESIS; + return put(e, &h, p, RX_EXP_H_WORDS, out_id); +} + +int rx_exp_act(RxExpEnv *e, uint64_t action, RxExpObservation *obs) { + if (!e || !e->open || !obs) return RX_EXP_ERR_ARG; + memset(obs, 0, sizeof *obs); + drop_pending(e); + uint64_t prior = rx_exp_state(e); + uint64_t input = e->fam->encode(prior, action); + RxcResult r; + if (run_goal(e, input, &r) != RX_EXP_OK) return RX_EXP_ERR_WORLD; + if (r.outcome == RXC_OUT_NO_WINNER || r.outcome == RXC_OUT_NOT_COMMITTED) + return RX_EXP_ERR_REFUSED; + if (r.outcome != RXC_OUT_COMMITTED) return RX_EXP_ERR_WORLD; + const CxObject *ev = cx_get(&e->c.cx, r.cx_evidence); + if (!ev) return RX_EXP_ERR_WORLD; + uint64_t next = 0, cost = 0; + e->fam->step(input, &next, &cost); + e->pend.live = 1; + e->pend.prior = prior; + e->pend.action = action; + e->pend.input = input; + e->pend.next = r.result; + e->pend.cost = cost; + e->pend.goal_crumb = r.goal_crumb; + e->pend.commit = ev->links[3]; + e->pend.evidence = r.cx_evidence; + e->pend.promotion = r.cx_promotion; + obs->prior = prior; + obs->action = action; + obs->next = r.result; + obs->commit = e->pend.commit; + obs->evidence = r.cx_evidence; + obs->promotion = r.cx_promotion; + return RX_EXP_OK; +} + +/* ---- receipt checks shared by record (live World) and dataset (store only) */ + +static const uint64_t *payload_n(const CxStore *s, uint64_t id, uint32_t cls, uint32_t kind, + uint32_t min_words, CxObject *hdr) { + CxRecord r; + if (!id || cx_recall_id(s, id, &r) != CX_OK || !r.verified) return NULL; + if (r.hdr.cls != cls || r.hdr.kind != kind || r.hdr.n < min_words) return NULL; + if (hdr) *hdr = r.hdr; + return r.payload; +} + +/* The composition receipts of one transition, from the store. `next` and + * `input` are what they must name. Returns RX_EXP_OK, RX_EXP_ERR_UNOBSERVED + * (not a World commit record), RX_EXP_ERR_MISMATCH (commit record names + * another state) or RX_EXP_ERR_UNVERIFIED. `wr` gets the decoded commit + * record, `verdict` the AEGIS verdict World record the evidence names. */ +static int check_chain(const CxStore *s, uint64_t commit, uint64_t evidence, uint64_t promotion, + uint64_t input, uint64_t next, CxWorldRecord *wr, uint64_t *verdict) { + CxRecord cr; + if (cx_recall_id(s, commit, &cr) != CX_OK || cx_world_decode(&cr, wr) != CX_OK || + wr->kind != CX_K_EXEC_COMMIT) + return RX_EXP_ERR_UNOBSERVED; + if (wr->field[RXC_S_RESULT] != next) return RX_EXP_ERR_MISMATCH; + CxObject eh, ph, ch; + const uint64_t *ep = payload_n(s, evidence, CX_EVIDENCE, CX_K_EVIDENCE_REF, RXC_EP_WORDS, &eh); + if (!ep || !(eh.protect & CX_PROT_VERIFY_EVIDENCE) || eh.links[3] != commit) + return RX_EXP_ERR_UNVERIFIED; + uint64_t winner = ep[RXC_EP_WINNER]; + if (winner == RXC_NONE || winner >= RXC_K || !((ep[RXC_EP_PASSMASK] >> winner) & 1u) || + ep[RXC_EP_RESULT] != next) + return RX_EXP_ERR_UNVERIFIED; + *verdict = eh.links[0]; + /* The promotion (cx_promote) binds the winning claim and this evidence by digest. */ + const uint64_t *pp = payload_n(s, promotion, CX_EVIDENCE, CX_K_PROMOTION, 8, &ph); + const uint64_t *cp = pp && ph.links[1] == evidence + ? payload_n(s, ph.links[0], CX_CLAIM, CX_K_CANDIDATE, RXC_CP_WORDS, &ch) : NULL; + uint64_t dw[4]; + if (!cp) return RX_EXP_ERR_UNVERIFIED; + to_words(ch.digest, dw); + if (memcmp(dw, pp, sizeof dw) != 0) return RX_EXP_ERR_UNVERIFIED; + to_words(eh.digest, dw); + if (memcmp(dw, pp + 4, sizeof dw) != 0) return RX_EXP_ERR_UNVERIFIED; + if (cp[RXC_CP_K] != winner || cp[RXC_CP_PASS] != 1 || cp[RXC_CP_RESULT] != next || + cp[RXC_CP_INPUT] != input) + return RX_EXP_ERR_UNVERIFIED; + CxRecord gr; + CxWorldRecord g; + if (cx_recall_id(s, ch.tag, &gr) != CX_OK || cx_world_decode(&gr, &g) != CX_OK || + g.kind != CX_K_WORK_ACCEPTED || g.field[RXC_G_INPUT] != input) + return RX_EXP_ERR_UNVERIFIED; + return RX_EXP_OK; +} + +static int commit_used(const CxStore *s, uint64_t commit) { + const CxIdList *l = &s->by_subject[RX_EXP_SUBJECT]; + for (uint32_t i = 0; i < l->n; i++) { + const CxObject *o = cx_get(s, l->ids[i]); + if (o && o->kind == CX_K_EXPERIENCE && o->links[0] == commit) return 1; + } + return 0; +} + +static void prediction_error(const RxExpFamily *f, uint64_t pred, uint64_t obs, uint64_t *fields, + uint64_t *l1) { + int64_t a[RX_EXP_MAX_FIELDS], b[RX_EXP_MAX_FIELDS]; + uint32_t n = f->unpack(pred, a); + f->unpack(obs, b); + *fields = 0; + *l1 = 0; + for (uint32_t i = 0; i < n; i++) { + if (a[i] == b[i]) continue; + (*fields)++; + uint64_t d = a[i] > b[i] ? (uint64_t)a[i] - (uint64_t)b[i] : (uint64_t)b[i] - (uint64_t)a[i]; + *l1 = *l1 + d < *l1 ? UINT64_MAX : *l1 + d; + } +} + +int rx_exp_record(RxExpEnv *e, const RxExpDraft *d, uint64_t *out_id) { + if (!e || !e->open || !d) return RX_EXP_ERR_ARG; + const RxExpFamily *f = e->fam; + CxStore *s = &e->c.cx; + if (!d->owner || (d->consent & ~(uint64_t)RX_EXP_CONSENT_ALL) || + (d->privacy != RX_EXP_PRIV_PRIVATE && d->privacy != RX_EXP_PRIV_SYNTHETIC) || + (d->split != RX_EXP_SPLIT_TRAIN && d->split != RX_EXP_SPLIT_HOLDOUT)) + return RX_EXP_ERR_CONSENT; + + /* 1. The observation must stand on a World commit receipt. */ + const RxExpObservation *o = &d->obs; + const CxObject *co = o->commit ? cx_get(s, o->commit) : NULL; + if (!co || co->cls != CX_EXECUTION || co->kind != CX_K_EXEC_COMMIT || !o->evidence || + !o->promotion) + return RX_EXP_ERR_UNOBSERVED; + if (commit_used(s, o->commit)) return RX_EXP_ERR_DUPLICATE; + + /* 2. ... written by the World recorder for a live commit crumb of the + * composition's commit step on the state object. */ + CxRecord cr; + CxWorldRecord wr; + if (cx_recall_id(s, o->commit, &cr) != CX_OK || cx_world_decode(&cr, &wr) != CX_OK) + return RX_EXP_ERR_FORGED; + const RxCrumb *k = rx_world_crumb(e->c.world, wr.crumb); + if (wr.session != e->c.session || wr.obj != e->c.state.id || + wr.obj_gen != e->c.state.generation || wr.reaction != e->c.rx_commit || + rx_cortex_record_in(e->c.world, s, wr.crumb) != o->commit || !k || + k->kind != RX_CRUMB_COMMIT || memcmp(k->digest, wr.crumb_digest, 32) != 0) + return RX_EXP_ERR_FORGED; + if (wr.field[RXC_S_RESULT] != o->next) return RX_EXP_ERR_MISMATCH; + + /* 3. It is the transition this environment is waiting to record. */ + if (!e->pend.live || o->commit != e->pend.commit || o->evidence != e->pend.evidence || + o->promotion != e->pend.promotion || d->prior != e->pend.prior || + d->action != e->pend.action || o->next != e->pend.next) + return RX_EXP_ERR_ORDER; + + /* 4. AEGIS passed it, it was promoted, the accepted goal was prior + action. */ + uint64_t input = f->encode(d->prior, d->action), verdict = 0; + if (input != e->pend.input) return RX_EXP_ERR_ORDER; + int rc = check_chain(s, o->commit, o->evidence, o->promotion, input, o->next, &wr, &verdict); + if (rc != RX_EXP_OK) return rc == RX_EXP_ERR_UNOBSERVED ? RX_EXP_ERR_FORGED : rc; + CxRecord vr; + CxWorldRecord vw; + if (cx_recall_id(s, verdict, &vr) != CX_OK || cx_world_decode(&vr, &vw) != CX_OK || + vw.kind != CX_K_EXEC_COMMIT || vw.reaction != e->c.rx_verify || + rx_cortex_record_in(e->c.world, s, vw.crumb) != verdict) + return RX_EXP_ERR_UNVERIFIED; + + /* 5. Replay. */ + uint64_t next = 0, cost = 0; + if (f->step(input, &next, &cost) != 0 || next != o->next) return RX_EXP_ERR_REPLAY; + + /* 6. The hypothesis, if any, is a claim about this prior and action. */ + uint64_t predicted = 0, predictor = 0; + if (d->hypothesis) { + CxObject hh; + const uint64_t *hp = payload_n(s, d->hypothesis, CX_CLAIM, CX_K_CANDIDATE, RX_EXP_H_WORDS, &hh); + if (!hp || hh.tag != RX_EXP_TAG_HYPOTHESIS || hp[RX_EXP_H_FAMILY] != f->family || + hp[RX_EXP_H_PRIOR] != d->prior || hp[RX_EXP_H_ACTION] != d->action) + return RX_EXP_ERR_HYPOTHESIS; + predicted = hp[RX_EXP_H_PREDICTED]; + predictor = hp[RX_EXP_H_PREDICTOR]; + } + + uint64_t p[RX_EXP_W_WORDS]; + memset(p, 0, sizeof p); + uint8_t dg[32]; + p[RX_EXP_W_VERSION] = RX_EXP_VERSION; + p[RX_EXP_W_FAMILY] = f->family; + p[RX_EXP_W_FAMILY_VERSION] = f->version; + to_words(e->seed_commit, p + RX_EXP_W_SEED_COMMIT0); + p[RX_EXP_W_EPISODE] = e->episode; + p[RX_EXP_W_STEP] = e->step; + static const uint8_t zero[32]; + to_words(memcmp(d->objective, zero, 32) ? d->objective : e->objective, p + RX_EXP_W_OBJECTIVE0); + p[RX_EXP_W_PRIOR] = d->prior; + state_digest(f, d->prior, dg); + to_words(dg, p + RX_EXP_W_PRIOR_DIGEST0); + p[RX_EXP_W_ACTION] = d->action; + p[RX_EXP_W_INPUT] = input; + p[RX_EXP_W_PRED_STATUS] = d->hypothesis ? RX_EXP_PRED_UNQUALIFIED : RX_EXP_PRED_NONE; + p[RX_EXP_W_PREDICTED] = predicted; + p[RX_EXP_W_PREDICTOR] = predictor; + p[RX_EXP_W_NEXT] = o->next; + state_digest(f, o->next, dg); + to_words(dg, p + RX_EXP_W_NEXT_DIGEST0); + p[RX_EXP_W_VERIFY] = RX_EXP_V_ALL; + if (d->hypothesis) + prediction_error(f, predicted, o->next, &p[RX_EXP_W_ERR_FIELDS], &p[RX_EXP_W_ERR_L1]); + p[RX_EXP_W_COST] = cost; + p[RX_EXP_W_COST_UNIT] = RX_EXP_COST_DECLARED_UNITS; + p[RX_EXP_W_OWNER] = d->owner; + p[RX_EXP_W_CONSENT] = d->consent; + p[RX_EXP_W_LICENSE] = d->license; + p[RX_EXP_W_PRIVACY] = d->privacy; + p[RX_EXP_W_SPLIT] = d->split; + p[RX_EXP_W_APPLY_OP] = f->op; + p[RX_EXP_W_APPLY_DOMAIN] = f->domain; + p[RX_EXP_W_SESSION] = e->c.session; + + CxHeader h; + memset(&h, 0, sizeof h); + h.cls = CX_EVIDENCE; + h.kind = CX_K_EXPERIENCE; + h.protect = CX_PROT_VERIFY_EVIDENCE; + h.subject = RX_EXP_SUBJECT; + h.tag = d->owner; + h.links[0] = o->commit; + h.links[1] = o->evidence; + h.links[2] = o->promotion; + h.links[3] = d->hypothesis; + rc = put(e, &h, p, RX_EXP_W_WORDS, out_id); + if (rc != RX_EXP_OK) return rc; + memset(&e->pend, 0, sizeof e->pend); + e->step++; + return RX_EXP_OK; +} + +int rx_exp_decode(const CxStore *s, uint64_t id, RxExpRecord *out) { + if (!s || !out) return RX_EXP_ERR_ARG; + CxObject h; + const uint64_t *p = payload_n(s, id, CX_EVIDENCE, CX_K_EXPERIENCE, RX_EXP_W_WORDS, &h); + if (!p || h.n != RX_EXP_W_WORDS || p[RX_EXP_W_VERSION] != RX_EXP_VERSION) return RX_EXP_ERR_ARG; + memset(out, 0, sizeof *out); + out->id = id; + out->t = h.t; + memcpy(out->w, p, sizeof out->w); + out->commit = h.links[0]; + out->evidence = h.links[1]; + out->promotion = h.links[2]; + out->hypothesis = h.links[3]; + return RX_EXP_OK; +} + +int rx_exp_revoke(RxExpEnv *e, uint64_t owner, uint64_t exp, uint64_t scope, uint64_t reason, + uint64_t *out_id) { + if (!e || !e->open || !owner || !scope || (scope & ~(uint64_t)RX_EXP_CONSENT_ALL)) + return RX_EXP_ERR_ARG; + if (exp) { + RxExpRecord r; + if (rx_exp_decode(&e->c.cx, exp, &r) != RX_EXP_OK) return RX_EXP_ERR_ARG; + if (r.w[RX_EXP_W_OWNER] != owner) return RX_EXP_ERR_OWNER; + } + uint64_t p[RX_EXP_R_WORDS] = { RX_EXP_VERSION, owner, exp, scope, reason }; + CxHeader h; + memset(&h, 0, sizeof h); + h.cls = CX_EVIDENCE; + h.kind = CX_K_CONSENT_REVOKED; + h.protect = CX_PROT_AUTHORITY; + h.subject = RX_EXP_SUBJECT; + h.tag = owner; + h.links[0] = exp; + return put(e, &h, p, RX_EXP_R_WORDS, out_id); +} + +/* ---- dataset view ------------------------------------------------------------- */ + +/* Store-only re-verification of a decoded experience. */ +static int recheck(const CxStore *s, const RxExpFamily *f, const RxExpRecord *r) { + const uint64_t *w = r->w; + if (w[RX_EXP_W_FAMILY] != f->family || w[RX_EXP_W_FAMILY_VERSION] != f->version || + w[RX_EXP_W_VERIFY] != RX_EXP_V_ALL) + return -1; + if (f->encode(w[RX_EXP_W_PRIOR], w[RX_EXP_W_ACTION]) != w[RX_EXP_W_INPUT]) return -1; + uint64_t next = 0, cost = 0; + if (f->step(w[RX_EXP_W_INPUT], &next, &cost) != 0 || next != w[RX_EXP_W_NEXT] || + cost != w[RX_EXP_W_COST]) + return -1; + uint8_t dg[32]; + uint64_t dw[4]; + state_digest(f, w[RX_EXP_W_PRIOR], dg); + to_words(dg, dw); + if (memcmp(dw, w + RX_EXP_W_PRIOR_DIGEST0, sizeof dw)) return -1; + state_digest(f, w[RX_EXP_W_NEXT], dg); + to_words(dg, dw); + if (memcmp(dw, w + RX_EXP_W_NEXT_DIGEST0, sizeof dw)) return -1; + CxWorldRecord wr; + uint64_t verdict = 0; + if (check_chain(s, r->commit, r->evidence, r->promotion, w[RX_EXP_W_INPUT], w[RX_EXP_W_NEXT], + &wr, &verdict) != RX_EXP_OK) + return -1; + if (wr.kind != CX_K_EXEC_COMMIT || wr.session != w[RX_EXP_W_SESSION]) return -1; + return 0; +} + +typedef struct { uint64_t t, owner, target, scope; } Revocation; + +uint32_t rx_exp_dataset(const CxStore *s, const RxExpFamily *fam, uint64_t need, uint64_t splits, + RxExpRecord *out, uint32_t max, RxExpViewStats *st) { + RxExpViewStats local; + if (!st) st = &local; + memset(st, 0, sizeof *st); + if (!s || !fam || RX_EXP_SUBJECT >= s->n_subjects) return 0; + const CxIdList *l = &s->by_subject[RX_EXP_SUBJECT]; + uint32_t n_rev = 0; + Revocation *rev = NULL; + for (uint32_t i = 0; i < l->n; i++) { + const CxObject *o = cx_get(s, l->ids[i]); + if (!o || o->kind != CX_K_CONSENT_REVOKED) continue; + CxObject h; + const uint64_t *p = payload_n(s, o->id, CX_EVIDENCE, CX_K_CONSENT_REVOKED, RX_EXP_R_WORDS, &h); + if (!p) continue; /* a revocation that fails its digest cannot widen eligibility */ + Revocation *nr = realloc(rev, (n_rev + 1) * sizeof *rev); + if (!nr) { free(rev); return 0; } + rev = nr; + rev[n_rev++] = (Revocation){ h.t, p[RX_EXP_R_OWNER], p[RX_EXP_R_TARGET], p[RX_EXP_R_SCOPE] }; + } + uint32_t n_out = 0; + for (uint32_t i = 0; i < l->n; i++) { + const CxObject *o = cx_get(s, l->ids[i]); + if (!o || o->kind != CX_K_EXPERIENCE) continue; + st->seen++; + RxExpRecord r; + if (rx_exp_decode(s, o->id, &r) != RX_EXP_OK || recheck(s, fam, &r) != 0) { + st->invalid++; + continue; + } + uint64_t owner = r.w[RX_EXP_W_OWNER], consent = r.w[RX_EXP_W_CONSENT], eff = consent; + for (uint32_t j = 0; j < n_rev; j++) + if (rev[j].owner == owner && + (rev[j].target == r.id || (rev[j].target == 0 && rev[j].t > r.t))) + eff &= ~rev[j].scope; + if ((consent & need) != need) { st->no_consent++; continue; } + if ((eff & need) != need) { st->revoked++; continue; } + if (!(r.w[RX_EXP_W_SPLIT] & splits)) { st->split++; continue; } + st->admitted++; + if (n_out < max && out) out[n_out++] = r; + } + free(rev); + return n_out; +} diff --git a/src/runtime/rx_experience.h b/src/runtime/rx_experience.h new file mode 100644 index 000000000..bb61f1f5b --- /dev/null +++ b/src/runtime/rx_experience.h @@ -0,0 +1,296 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_experience.h -- verified state/action/next-state experience (omega #699). + * + * An experience is one transition of a seeded, resettable environment, taken + * through the existing accept chain and recorded in canonical Cortex: + * + * action -> goal (World outside input, CX_K_WORK_ACCEPTED) + * -> compose.candidate: the environment Skill runs on a staged + * J-Space branch + * -> compose.verify (AEGIS): the family contract replays the + * transition; only a passing result can win + * -> compose.commit + World binder: the state object names the + * sealed branch (CX_K_EXEC_COMMIT, the World commit receipt) + * -> settle: claim, verification evidence, promotion (rx_compose) + * -> rx_exp_record: CX_K_EXPERIENCE through the same Cortex writer + * link (rx_cortex_append_in), never a second writer. + * + * PREDICTED vs OBSERVED. A prediction (any model output) is only a + * hypothesis: a CX_CLAIM / CX_K_CANDIDATE record tagged RX_EXP_TAG_HYPOTHESIS. + * An observation is the environment's committed next state, and is accepted + * only with its receipts: the World link's own commit record for a live + * commit crumb, the AEGIS evidence that passed it, the promotion naming that + * evidence, the accepted goal whose input is prior+action, and a replay of + * the family transition. Anything else (no receipt, a hypothesis offered as a + * receipt, a receipt appended by someone other than the World recorder, a + * next state that differs from the receipt) is refused and nothing is + * written. + * + * The prediction kept inside an experience is an UNQUALIFIED annotation + * (RX_EXP_PRED_UNQUALIFIED): the Belief/Estimation gate EST-3 has not passed, + * so predicted state and its error never become an authority input. + * + * Consent revocation never deletes (Cortex is append-only): it appends a + * CX_K_CONSENT_REVOKED record, and every dataset view built afterwards + * excludes the revoked experiences. Copies and trained weights made earlier + * are out of scope here. + * + * Experience, hypothesis and revocation records are about the composition + * state object: subject RXC_CX_SUBJECT(RXC_SLOT_STATE). + */ +#ifndef RX_EXPERIENCE_H +#define RX_EXPERIENCE_H + +#include "aien_machine_id.h" +#include "aienos_cap.h" +#include "rx_capq.h" +#include "rx_compose.h" +#include "rx_cortex.h" +#include "rx_graph.h" +#include "rx_skillroute.h" + +#define RX_EXP_VERSION 1u +#define RX_EXP_SUBJECT RXC_CX_SUBJECT(RXC_SLOT_STATE) +#define RX_EXP_MAX_FIELDS 8u +/* CX_K_CANDIDATE.tag of a hypothesis ("exp.hyp1"); composition claims carry + * the id of their goal record instead. */ +#define RX_EXP_TAG_HYPOTHESIS 0x6578702e68797031ull + +/* A transition family: pure functions of one 64-bit packed state. The same + * `step` is the environment Skill (via `skill`) and the AEGIS contract (via + * `contract`) and the recorder's replay. `skill` and `contract` exist only + * because the composition function types carry no context pointer. */ +typedef struct RxExpFamily { + uint64_t family; /* RX_EXP_FAMILY_* */ + uint32_t version; + uint32_t op; /* semantic operation id in the environment's own Capability + Graph (cq_op_define ids are dense from 1) */ + const char *name; + uint64_t domain; /* applicability word the family declares */ + /* Next state and declared cost for a goal input; nonzero = refused. */ + int (*step)(uint64_t input, uint64_t *next, uint64_t *cost); + /* Goal input that applies `action` to state `prior`. */ + uint64_t (*encode)(uint64_t prior, uint64_t action); + /* Goal input that resets the environment for `seed`. */ + uint64_t (*reset_input)(uint64_t seed); + /* Signed fields of a state (for error accounting); returns the count. */ + uint32_t (*unpack)(uint64_t state, int64_t out[RX_EXP_MAX_FIELDS]); + /* Digest of the objective a state carries. */ + void (*objective)(uint64_t state, uint8_t out[32]); + AgSkillFn skill; + RxcContract contract; +} RxExpFamily; + +/* ---- the walk1d family (rx_exp_walk.c) ------------------------------------ + * A noisy 1-D point mass that must reach a target. Packed state, LSB first: + * key 24 | k 10 | x 12 (signed) | v 5 (signed) | target 6 (signed) | + * act 3 | noise 2 | live 1 | op 1 + * key: stream key derived from the seed; k: steps taken (episode bound 1023); + * act: last action code (1..5 = thrust -2..+2); noise: last noise + 2 + * (1..3 = -1..+1, 0 = none); live: episode started; op (goal input only): + * 1 = reset. Step: noise = (pd0 stream(key, "omega.exp.walk1d.noise") word + * k) mod 3 - 1, v' = clamp(v + thrust + noise, -8, 8), x' = clamp(x + v', + * -2000, 2000), k' = k + 1; declared cost = |thrust| + 1 units. Reset: x = v + * = k = 0, target = (stream(key, "omega.exp.walk1d.target") word 0) mod 41 + * - 20. Action word for rx_exp_act: the thrust code 1..5. */ +#define RX_EXP_FAMILY_WALK1D 0x77616c6b31640001ull +extern const RxExpFamily rx_exp_walk1d; +enum { RX_EXP_WALK_KEY = 0, RX_EXP_WALK_K, RX_EXP_WALK_X, RX_EXP_WALK_V, RX_EXP_WALK_TARGET, + RX_EXP_WALK_ACT, RX_EXP_WALK_NOISE, RX_EXP_WALK_LIVE, RX_EXP_WALK_FIELDS }; +#define RX_EXP_WALK_K_MAX 1023u +/* Thrust code for a thrust in -2..2. */ +#define RX_EXP_WALK_THRUST(a) ((uint64_t)((a) + 3)) +/* The stream key of a seed (24 bits, never 0). */ +uint64_t rx_exp_walk_key(uint64_t seed); + +/* ---- record payloads -------------------------------------------------------- */ + +/* CX_K_EXPERIENCE (CX_EVIDENCE, CX_PROT_VERIFY_EVIDENCE). + * links {World commit record of the state, AEGIS evidence, promotion, + * hypothesis or 0}. */ +enum { + RX_EXP_W_VERSION = 0, RX_EXP_W_FAMILY, RX_EXP_W_FAMILY_VERSION, + RX_EXP_W_SEED_COMMIT0, /* SHA-256("omega.exp.seed.v1" | seed) */ + RX_EXP_W_EPISODE = RX_EXP_W_SEED_COMMIT0 + 4, + RX_EXP_W_STEP, /* transitions accepted before it in the episode */ + RX_EXP_W_OBJECTIVE0, + RX_EXP_W_PRIOR = RX_EXP_W_OBJECTIVE0 + 4, /* packed prior state */ + RX_EXP_W_PRIOR_DIGEST0, + RX_EXP_W_ACTION = RX_EXP_W_PRIOR_DIGEST0 + 4, + RX_EXP_W_INPUT, /* goal input the World accepted */ + RX_EXP_W_PRED_STATUS, /* RX_EXP_PRED_* */ + RX_EXP_W_PREDICTED, + RX_EXP_W_PREDICTOR, + RX_EXP_W_NEXT, /* observed packed next state */ + RX_EXP_W_NEXT_DIGEST0, + RX_EXP_W_VERIFY = RX_EXP_W_NEXT_DIGEST0 + 4,/* RX_EXP_V_* */ + RX_EXP_W_ERR_FIELDS, /* predicted fields != observed (unqualified) */ + RX_EXP_W_ERR_L1, /* sum |predicted - observed| (unqualified) */ + RX_EXP_W_COST, + RX_EXP_W_COST_UNIT, /* RX_EXP_COST_* */ + RX_EXP_W_OWNER, RX_EXP_W_CONSENT, RX_EXP_W_LICENSE, RX_EXP_W_PRIVACY, + RX_EXP_W_SPLIT, /* RX_EXP_SPLIT_* */ + RX_EXP_W_APPLY_OP, RX_EXP_W_APPLY_DOMAIN, + RX_EXP_W_SESSION, + RX_EXP_W_WORDS +}; + +/* CX_K_CANDIDATE hypothesis (CX_CLAIM, tag RX_EXP_TAG_HYPOTHESIS). */ +enum { RX_EXP_H_VERSION = 0, RX_EXP_H_FAMILY, RX_EXP_H_PRIOR, RX_EXP_H_ACTION, + RX_EXP_H_PREDICTED, RX_EXP_H_PREDICTOR, RX_EXP_H_WORDS }; + +/* CX_K_CONSENT_REVOKED (CX_EVIDENCE, CX_PROT_AUTHORITY, tag = owner). + * links {experience, 0 = every experience of the owner recorded before it}. */ +enum { RX_EXP_R_VERSION = 0, RX_EXP_R_OWNER, RX_EXP_R_TARGET, RX_EXP_R_SCOPE, RX_EXP_R_REASON, + RX_EXP_R_WORDS }; + +/* Verification an experience passed (all of them, or it is not written). */ +enum { + RX_EXP_V_WORLD_COMMIT = 1u << 0, /* the World recorder's record of a live commit crumb */ + RX_EXP_V_AEGIS = 1u << 1, /* compose.verify passed this result */ + RX_EXP_V_PROMOTED = 1u << 2, /* the winning claim was promoted on that evidence */ + RX_EXP_V_GOAL = 1u << 3, /* accepted goal input = prior + action */ + RX_EXP_V_REPLAY = 1u << 4, /* the family transition replays to it */ + RX_EXP_V_ALL = 0x1fu +}; +enum { RX_EXP_PRED_NONE = 0, RX_EXP_PRED_UNQUALIFIED = 1 }; +enum { RX_EXP_COST_DECLARED_UNITS = 1 }; /* measured wall time stays in the World receipt */ +enum { RX_EXP_CONSENT_RETRIEVE = 1u << 0, RX_EXP_CONSENT_TRAIN = 1u << 1, + RX_EXP_CONSENT_SHARE = 1u << 2, RX_EXP_CONSENT_ALL = 0x7u }; +enum { RX_EXP_PRIV_PRIVATE = 1, RX_EXP_PRIV_SYNTHETIC = 2 }; +enum { RX_EXP_SPLIT_TRAIN = 1, RX_EXP_SPLIT_HOLDOUT = 2 }; + +enum { + RX_EXP_OK = 0, + RX_EXP_ERR_ARG = -1, + RX_EXP_ERR_UNOBSERVED = -2, /* no receipt, or the receipt is not a World commit */ + RX_EXP_ERR_FORGED = -3, /* receipt not written by the World recorder for a live crumb */ + RX_EXP_ERR_MISMATCH = -4, /* observed state differs from the receipts */ + RX_EXP_ERR_ORDER = -5, /* not the pending transition of this environment */ + RX_EXP_ERR_DUPLICATE = -6, /* receipt already used by an experience */ + RX_EXP_ERR_REPLAY = -7, /* the family transition does not replay to it */ + RX_EXP_ERR_UNVERIFIED = -8, /* no AEGIS pass / promotion / matching goal */ + RX_EXP_ERR_CONSENT = -9, /* missing owner, consent, privacy or split */ + RX_EXP_ERR_OWNER = -10, /* revocation by someone other than the owner */ + RX_EXP_ERR_HYPOTHESIS = -11, /* hypothesis id is not one for this prior + action */ + RX_EXP_ERR_REFUSED = -12, /* the environment refused the action (no winner) */ + RX_EXP_ERR_WORLD = -13 /* composition / World / Cortex failure */ +}; +const char *rx_exp_strerror(int rc); + +/* ---- the environment -------------------------------------------------------- */ + +typedef struct RxExpEnv { + const RxExpFamily *fam; + RxCompose c; /* large: keep RxExpEnv static or on the heap */ + AienMachineId slots[8]; + AienMachineIndex ix; + CqCatalog cat; + AgSkillTable skills; + SrRouter router; + int open; + uint64_t seed; + uint8_t seed_commit[32]; + uint8_t objective[32]; + uint64_t episode; /* resets so far in this process */ + uint64_t step; /* experiences recorded in this episode */ + uint64_t dropped; /* acted, never recorded (counted, never silent) */ + uint64_t now_us; + struct { + int live; + uint64_t prior, action, input, next, cost; + uint64_t goal_crumb, commit, evidence, promotion; + } pend; +} RxExpEnv; + +/* Open the environment's composition in `dir` (rx_compose_open: machine + * identity checked, J-Space and Cortex journal reopened and verified, state + * recovered OLD-or-NEW) with the family Skill as the only route. + * `admin`/`view` is the AIENOS authority the caller runs. */ +int rx_exp_open(RxExpEnv *e, const RxExpFamily *fam, const char *dir, const AienMachineId *self, + uint64_t session, AienosCapAdmin *admin, AienosCapView *view); +void rx_exp_close(RxExpEnv *e); + +/* Reset for `seed` through the accept chain (a committed reset goal); starts + * a new episode. Drops (and counts) an unrecorded pending transition. */ +int rx_exp_reset(RxExpEnv *e, uint64_t seed); + +/* The committed environment state (the World state object). */ +uint64_t rx_exp_state(RxExpEnv *e); + +/* Record a hypothesis about applying `action` to the current state. It is a + * claim only and never an observation. */ +int rx_exp_hypothesis(RxExpEnv *e, uint64_t predictor, uint64_t action, uint64_t predicted, + uint64_t *out_id); + +/* What the environment answered to an action: receipts and the observed state. + * The recorder re-checks all of it; a caller cannot vouch for any field. */ +typedef struct { + uint64_t prior, action, next; + uint64_t commit, evidence, promotion; /* Cortex ids */ +} RxExpObservation; + +/* Run `action` through the accept chain. RX_EXP_OK with `obs` filled when the + * World committed a verified next state; RX_EXP_ERR_REFUSED when the + * environment refused it (the refusal stays in Cortex as composition records). */ +int rx_exp_act(RxExpEnv *e, uint64_t action, RxExpObservation *obs); + +typedef struct { + uint8_t objective[32]; /* goal digest (zero = the environment's objective) */ + uint64_t prior, action; + uint64_t hypothesis; /* hypothesis record id, 0 = none */ + RxExpObservation obs; /* claimed observation (receipts + next state) */ + uint64_t owner, consent, license, privacy, split; +} RxExpDraft; + +/* Verify a draft against the pending transition, the Cortex receipts, the + * live World and a replay, then append CX_K_EXPERIENCE. Nothing is written on + * refusal. */ +int rx_exp_record(RxExpEnv *e, const RxExpDraft *d, uint64_t *out_id); + +/* Withdraw consent `scope` (RX_EXP_CONSENT_*) for experience `exp` (0 = every + * experience of `owner` recorded so far). RX_EXP_ERR_OWNER if `exp` belongs to + * someone else. Appends CX_K_CONSENT_REVOKED; deletes nothing. */ +int rx_exp_revoke(RxExpEnv *e, uint64_t owner, uint64_t exp, uint64_t scope, uint64_t reason, + uint64_t *out_id); + +/* ---- reading ----------------------------------------------------------------- */ + +typedef struct { + uint64_t id, t; + uint64_t w[RX_EXP_W_WORDS]; + uint64_t commit, evidence, promotion, hypothesis; /* links */ +} RxExpRecord; + +/* Decode one experience (digest checked): RX_EXP_OK or RX_EXP_ERR_ARG. */ +int rx_exp_decode(const CxStore *s, uint64_t id, RxExpRecord *out); + +typedef struct { + uint64_t seen, admitted, revoked, no_consent, split, invalid; +} RxExpViewStats; + +/* Dataset view over `s` (the Cortex store alone; works after restart without + * a World): experiences of family `fam`, oldest first, that re-verify from + * their receipts and replay, whose consent after revocations covers `need`, + * and whose split is in `splits` (RX_EXP_SPLIT_* mask). Returns the number + * written to `out` (at most `max`). */ +uint32_t rx_exp_dataset(const CxStore *s, const RxExpFamily *fam, uint64_t need, uint64_t splits, + RxExpRecord *out, uint32_t max, RxExpViewStats *st); + +#endif /* RX_EXPERIENCE_H */ diff --git a/tests/.crumb b/tests/.crumb index 6d4038fe6..7e52bba37 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,206 +110,206 @@ "children": [ { "digest": "2c1deaaa1bae6257789749edde641df61413250fe91ce6aa61a99dfb3b271306", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "63f086b68bb9ffc032cbd74931309ab34df3c8c341b57935e5db43d8bf475800", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "45c5f4fd157769cf0dcc4b8acbd76b0953580bc328714bab8e1d65e9264a5664", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cd97575b2b10d9313ca0df4997391612056754778bb2851bf052c146144f15e", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { "digest": "d1b6f712c25e8c76dbbc9da5c7f55203f3644c41c193a2b7649477a983a91d8e", - "last_commit": "876d00ff5", + "last_commit": "876d00ff", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "9e372cfc9747befacdcf31bc8643356aaff861e81ee6640b4866170be67b5e6c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "87a3efa5cd0aefa7821046f9a31594bfa0a109e95005a1e12f40c99a1bc6cd3d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "10aa3424eded7768fee0bae0eedd062b9deac3279d2e5721439fd024493bc42b", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "758dde696afa847d9347fad76b0c4935e968f8af4551f0f9810ec2a37ad9847a", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "23e559d13ff664995baca31dc157a8cf5a860615f3ecb07a9a40fe4626b57ae3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "b7b6391caf653c76707a21f9969126abcbc730d1243259219225fdb9bc0084e0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "defc52a44ea0f65ba5de3468de786148ba58d292ffa217fb95629a28a569a977", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "6ba43d622b712a4215767cab29425016f29cce4b28eb9e4961fc67fd20e9d765", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "54f43a3416595ff5d4e64bd12afe013359e706651c39c78e2896a5799638d263", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "a03a4dd9a72f7b449342e562f6d9df1a3348fb03fb640b662956caf163dcd7e9", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "d958af5413726ddc48a47721800d9766939129f480b26492332ba094577837cb", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, { "digest": "12b31dc2cd4f6a6c27983db11b4140c597025c9124da5ddcbe6a9230ab4f8410", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "51f14936b27d3d21379cf05ea1b6b532cf3175a40844dac85f023a61e7a0912c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "c8524317c751fa8f464cabc31b760cef262abba822f1843482a02e239daa81b3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "6569e2cd6dbf6f792edb5d83bf0715df9dece42ca7bd9b3a291535de0492d3aa", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "c549b5b82fd026d664b35e4b1a5d7fd2aaafa13b60b541cfbba87d7522517b1d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "855bd23e4c526fb49eeb430c4ea5967d3f2f9da3a5b3aa3ec9dae5b814b11a21", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "f21be04969cbf51de17118c4f81b103f712db56fd517a1f12dd0d3ac4ff3afe4", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "b0ba3448394d5f27958c38b7233ae389e5e437e327f0859749a774fb252d6b02", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "157018d66de5bfd149015654cf0bda1346a9cfcefe5fd552c1d54859cd9ece81", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { "digest": "e5f68362c083510e34c41f65de3ba2d553eac8a52fbc3e4cc1f69314eac9cc32", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "reservoir", "purpose": "Reservoir tests (omega#440): Gate 1 simulator and readout checks against an independent binary64" }, { - "digest": "39bb8b5bc5bb91af7db77cc15bcd326cdb083060f27077e9f60670cad9086a11", - "last_commit": "843424cea", + "digest": "8e65bae05d0ebacbf1bcadc97e762dcdd884dae35456d4bd70837fdc5c40d2b5", + "last_commit": "7f96349d", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, { "digest": "467652c09505ecbd82d96a5e0e7ea24b19d9216cfd1dad7c60fa8258e47594c0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "e69616ad471704066a86bf2796776301179eada5c0aa36a7166b9aca788619f4", - "last_commit": "ed8de5736", + "last_commit": "ed8de573", "name": "synapse", "purpose": "J1 parity driver for syn_codec.osc against the sealed Rust A1 corpus (aien-dev/synapse j1-codec-" }, { "digest": "5c97916fbf20310eff2e502c526337cb093e912cb48f18d44b0345ed8a3c3240", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "7679bd1c49c7e89d8c26ccf8dfb91478f4e6e2f584745806fa8d26061dbdbb9c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "003966bc697c6f85cfffbae00fae992a634bc386f4a44340daaa8ddcb5618ef6", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:a9fee3562eff462a1d813a98fa05a6f4f45640d0b27635062f135b140c9122a2", - "compiled_at": "2026-10-25T13:26:27Z", + "children_root": "sha256:37890b3ab0f4198cd592dd22225422ce8450e1e97797ab75483b0928a7e73579", + "compiled_at": "2026-10-25T18:53:40Z", "compiler": "crumb-compile/1", - "digest": "sha256:2b63b8ab9af7638881a81c7617109ab9987c2419fb4e0e25079c42309280b866", + "digest": "sha256:df56d91df565d41851b97ec989ebaf0a2b164b4ca5b5c4099bee59a5839592df", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1293, - "generated_at_commit": "0e5d61fc6cc44d527fb3b736a399ac5848cdc8cb", - "source_tree": "sha256:4b5f097f14dce3a00aec35f1cc16e14793ee8ab3ca6427abf6ff0a8622aeb836" + "files": 1294, + "generated_at_commit": "9b927e456a89cd60f99bda6ec2fa43e6d553689f", + "source_tree": "sha256:8acd1ba77414291258fe5a5453d1b329fbcd7b5bc9961a7a1047391bb2fa878c" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 422a08a6e..f19d43e5c 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T23:00:43Z", + "compiled_at": "2026-10-25T02:28:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:39bb8b5bc5bb91af7db77cc15bcd326cdb083060f27077e9f60670cad9086a11", + "digest": "sha256:8e65bae05d0ebacbf1bcadc97e762dcdd884dae35456d4bd70837fdc5c40d2b5", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 70, - "generated_at_commit": "6b8927fdbb8e15c2668a29a6beca577c8cf71651", - "source_tree": "sha256:53b4e9a9bb5a07b946577ebe5d09fb4bda158de5b1bb699d613049a710f10de5" + "files": 71, + "generated_at_commit": "d45184b4d604bd7060f2aa361aaa3c12af6370d1", + "source_tree": "sha256:e6bc91c0cc85e7176895db7bf8ba7482b14618c969a6e1be571ea8a71debc6db" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_experience_test.c b/tests/runtime/rx_experience_test.c new file mode 100644 index 000000000..1705fec94 --- /dev/null +++ b/tests/runtime/rx_experience_test.c @@ -0,0 +1,474 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_experience_test.c -- omega #699 verified experience records. + * + * Real World, J-Space, AEGIS verifier, commit binder, Cortex journal and + * AIENOS authority; nothing is mocked. Checks: + * 1. walk1d: the O(1) noise word equals the sequential PD0 stream; refusals. + * 2. accept chain: an action becomes a CX_K_EXPERIENCE linked to its World + * commit, AEGIS evidence and promotion; the prediction is an unqualified + * hypothesis claim. + * 3. generated-but-unobserved records are refused as observations (no + * receipt, hypothesis/evidence as receipt, forged and replayed commit + * records, altered next state, duplicate, stale, refused action); + * nothing is written on refusal. + * 4. consent: revoked experiences (single and owner-wide) leave the dataset + * view, a non-owner cannot revoke, nothing is deleted, the view is + * rebuilt identically after restart, tampering is excluded. + * 5. determinism: same seed -> byte-identical experience payloads in fresh + * environments and after reset; different seeds -> different trajectories. + */ +#include "physics0/pd0_rng.h" +#include "runtime/rx_cortex_record.h" +#include "runtime/rx_experience.h" + +#include +#include +#include +#include +#include + +static int g_checks, g_fail; + +#define CHECK(cond, ...) do { \ + g_checks++; \ + if (!(cond)) { \ + g_fail++; \ + fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fputc('\n', stderr); \ + } \ + } while (0) + +static const RxExpFamily *F = &rx_exp_walk1d; +static AienosCapAdmin *g_admin; +static AienosCapView *g_view; +static AienMachineId g_self; +static char g_base[64]; +static RxExpEnv g_e, g_e2; /* large: static */ + +enum { OWNER_A = 0xA11CE, OWNER_B = 0xB0B, PREDICTOR = 0x7ED }; + +static int rm_one(const char *p, const struct stat *sb, int flag, struct FTW *ftw) { + (void)sb; (void)flag; (void)ftw; + return remove(p); +} +static void rmtree(const char *p) { nftw(p, rm_one, 16, FTW_DEPTH | FTW_PHYS); } + +static void dir_for(char *out, size_t n, const char *name) { + snprintf(out, n, "%s/%s", g_base, name); + rmtree(out); +} + +static int64_t field(uint64_t s, uint32_t i) { + int64_t f[RX_EXP_MAX_FIELDS]; + F->unpack(s, f); + return f[i]; +} + +/* Closed-loop policy: thrust toward the target, brake near it. */ +static uint64_t policy(uint64_t s) { + int64_t x = field(s, RX_EXP_WALK_X), v = field(s, RX_EXP_WALK_V), t = field(s, RX_EXP_WALK_TARGET); + int64_t want = (t - x) / 4 - v; + if (want > 2) want = 2; + if (want < -2) want = -2; + return RX_EXP_WALK_THRUST(want); +} + +static RxExpDraft draft(uint64_t owner, uint64_t consent, uint64_t split, uint64_t hyp, + const RxExpObservation *o) { + RxExpDraft d; + memset(&d, 0, sizeof d); + d.prior = o->prior; + d.action = o->action; + d.hypothesis = hyp; + d.obs = *o; + d.owner = owner; + d.consent = consent; + d.license = 1; + d.privacy = RX_EXP_PRIV_SYNTHETIC; + d.split = split; + return d; +} + +/* One full step: hypothesis (from `pred`), act, record. Returns the experience id. */ +static uint64_t step_record(RxExpEnv *e, uint64_t action, uint64_t pred, uint64_t owner, + uint64_t consent, uint64_t split) { + uint64_t hyp = 0, id = 0; + RxExpObservation o; + if (rx_exp_hypothesis(e, PREDICTOR, action, pred, &hyp) != RX_EXP_OK) return 0; + if (rx_exp_act(e, action, &o) != RX_EXP_OK) return 0; + RxExpDraft d = draft(owner, consent, split, hyp, &o); + int rc = rx_exp_record(e, &d, &id); + return rc == RX_EXP_OK ? id : 0; +} + +/* A model that knows the physics but not the noise: predicts noise 0. */ +static uint64_t noise_blind_prediction(uint64_t prior, uint64_t action) { + uint64_t next = 0, cost = 0; + if (F->step(F->encode(prior, action), &next, &cost) != 0) return 0; + int64_t noise = field(next, RX_EXP_WALK_NOISE) - 2; + int64_t v = field(prior, RX_EXP_WALK_V) + (int64_t)action - 3; + if (v > 8) v = 8; + if (v < -8) v = -8; + if (noise == 0) return next; + /* rebuild with noise 0: v, x and the noise code change */ + int64_t x = field(prior, RX_EXP_WALK_X) + v; + uint64_t m = next; + m &= ~(((1ull << 12) - 1) << 34) & ~(((1ull << 5) - 1) << 46) & ~(3ull << 60); + m |= ((uint64_t)x & 0xfff) << 34 | ((uint64_t)v & 0x1f) << 46 | 2ull << 60; + return m; +} + +static int open_env(RxExpEnv *e, const char *dir, uint64_t session) { + return rx_exp_open(e, F, dir, &g_self, session, g_admin, g_view); +} + +/* ---- 1 ---------------------------------------------------------------------- */ +static void t_family(void) { + printf("[*] walk1d: O(1) noise word = sequential PD0 stream; refusals\n"); + uint64_t key = rx_exp_walk_key(0x5eed), next = 0, cost = 0; + CHECK(key != 0 && key < (1u << 24), "key in range"); + uint64_t s; + CHECK(F->step(F->reset_input(0x5eed), &s, &cost) == 0, "reset"); + pd0_rng ref; + pd0_stream(&ref, key, "omega.exp.walk1d.noise"); + int same = 1, count[3] = { 0, 0, 0 }; + for (uint32_t k = 0; k < 600; k++) { + int64_t want = (int64_t)(pd0_next(&ref) % 3) - 1; + if (F->step(F->encode(s, RX_EXP_WALK_THRUST(0)), &next, &cost) != 0) { same = 0; break; } + int64_t got = field(next, RX_EXP_WALK_NOISE) - 2; + same &= got == want && field(next, RX_EXP_WALK_K) == (int64_t)k + 1 && cost == 1; + count[got + 1]++; + s = next; + } + CHECK(same, "noise of step k is word k of the sequential stream"); + printf(" noise counts over 600 steps: -1:%d 0:%d +1:%d\n", count[0], count[1], count[2]); + CHECK(count[0] && count[1] && count[2], "all three noise values occur"); + CHECK(F->step(F->encode(s, 0), &next, &cost) != 0, "action code 0 refused"); + CHECK(F->step(F->encode(s, 6), &next, &cost) != 0, "action code 6 refused"); + CHECK(F->step(F->encode(s, 99), &next, &cost) != 0, "action out of range refused"); + CHECK(F->step(F->encode(0, RX_EXP_WALK_THRUST(1)), &next, &cost) != 0, "no episode: refused"); + CHECK(F->step(F->reset_input(1) | (1ull << 30), &next, &cost) != 0, "reset with state bits refused"); + CHECK(F->contract(F->encode(s, 4), next ^ 1) == 0, "contract refuses a wrong next state"); +} + +/* ---- 2 + 3 -------------------------------------------------------------------- */ +static void t_chain_and_unobserved(void) { + printf("[*] accept chain -> experience; unobserved / forged / stale refused\n"); + char d[200]; + dir_for(d, sizeof d, "chain"); + RxExpEnv *e = &g_e; + CHECK(open_env(e, d, 1) == RX_EXP_OK, "open"); + CHECK(rx_exp_reset(e, 0xC0FFEE) == RX_EXP_OK, "reset through the accept chain"); + uint64_t s0 = rx_exp_state(e); + CHECK(field(s0, RX_EXP_WALK_LIVE) == 1 && field(s0, RX_EXP_WALK_K) == 0, "episode live"); + + /* happy path with a noise-blind model prediction */ + uint64_t a = policy(s0), pred = noise_blind_prediction(s0, a), hyp = 0, id = 0; + CHECK(rx_exp_hypothesis(e, PREDICTOR, a, pred, &hyp) == RX_EXP_OK, "hypothesis"); + const CxObject *ho = cx_get(&e->c.cx, hyp); + CHECK(ho && ho->cls == CX_CLAIM && ho->kind == CX_K_CANDIDATE && ho->tag == RX_EXP_TAG_HYPOTHESIS, + "a prediction is a claim, never an observation"); + RxExpObservation o; + CHECK(rx_exp_act(e, a, &o) == RX_EXP_OK, "act"); + CHECK(o.prior == s0 && o.next == rx_exp_state(e) && o.commit && o.evidence && o.promotion, + "observation names the World state and its receipts"); + + /* 3a. the model's generated next state offered with no receipt */ + uint64_t n_before = e->c.cx.n; + RxExpObservation gen = { s0, a, pred, 0, 0, 0 }; + RxExpDraft g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_UNOBSERVED, "generated, no receipt: refused"); + /* 3b. the hypothesis itself offered as the receipt */ + gen.commit = hyp; gen.evidence = hyp; gen.promotion = hyp; + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_UNOBSERVED, "hypothesis as receipt: refused"); + /* 3c. the verification evidence offered as the commit receipt */ + gen = o; + gen.commit = o.evidence; + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_UNOBSERVED, "evidence as commit receipt: refused"); + /* 3d. a forged commit record carrying the model's state, appended through + * the writer link (only the World recorder may speak for a crumb) */ + CxRecord cr; + CHECK(cx_recall_id(&e->c.cx, o.commit, &cr) == CX_OK, "recall real commit"); + uint64_t fake[CX_WREC_WORDS]; + memcpy(fake, cr.payload, sizeof fake); + CxHeader fh; + memset(&fh, 0, sizeof fh); + fh.cls = CX_EXECUTION; + fh.kind = CX_K_EXEC_COMMIT; + fh.subject = RX_EXP_SUBJECT; + fh.protect = CX_PROT_COMMIT_RECEIPT; + fh.tag = cr.hdr.tag; + fh.t = rx_cortex_next_t_in(e->c.world, &e->c.cx); + uint64_t replayed = 0, forged = 0; + CHECK(rx_cortex_append_in(e->c.world, &e->c.cx, &fh, fake, CX_WREC_WORDS, &replayed) == RX_OK, + "append replayed copy"); + fake[CX_WREC_FIELD0 + RXC_S_RESULT] = pred ^ (1ull << 34); + fh.t = rx_cortex_next_t_in(e->c.world, &e->c.cx); + CHECK(rx_cortex_append_in(e->c.world, &e->c.cx, &fh, fake, CX_WREC_WORDS, &forged) == RX_OK, + "append fabricated commit"); + n_before = e->c.cx.n; + gen = o; + gen.commit = replayed; + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_FORGED, "replayed commit record: refused"); + gen.commit = forged; + gen.next = pred ^ (1ull << 34); + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_FORGED, "fabricated commit record: refused"); + /* 3e. real receipts, next state replaced by a different (predicted) one */ + gen = o; + gen.next = o.next ^ (1ull << 34); + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &gen); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_MISMATCH, "altered next state: refused"); + /* hypothesis about another action */ + uint64_t other = 0; + CHECK(rx_exp_hypothesis(e, PREDICTOR, a == 5 ? 4 : a + 1, pred, &other) == RX_EXP_OK, "other hyp"); + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, other, &o); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_HYPOTHESIS, "hypothesis for another action: refused"); + g = draft(0, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &o); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_CONSENT, "no owner: refused"); + n_before = e->c.cx.n; + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, 7, hyp, &o); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_CONSENT && e->c.cx.n == n_before, + "bad split: refused, nothing written"); + + /* the real observation is accepted */ + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, hyp, &o); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_OK, "real observation recorded"); + RxExpRecord r; + CHECK(rx_exp_decode(&e->c.cx, id, &r) == RX_EXP_OK, "decode"); + const CxObject *xo = cx_get(&e->c.cx, id); + CHECK(xo && xo->cls == CX_EVIDENCE && xo->kind == CX_K_EXPERIENCE && + (xo->protect & CX_PROT_VERIFY_EVIDENCE), "experience is protected verification evidence"); + CHECK(r.w[RX_EXP_W_VERIFY] == RX_EXP_V_ALL && r.commit == o.commit && r.evidence == o.evidence && + r.promotion == o.promotion && r.hypothesis == hyp, "receipts linked"); + CHECK(r.w[RX_EXP_W_PRIOR] == s0 && r.w[RX_EXP_W_ACTION] == a && r.w[RX_EXP_W_NEXT] == o.next, + "state / action / next state"); + CHECK(r.w[RX_EXP_W_PRED_STATUS] == RX_EXP_PRED_UNQUALIFIED && r.w[RX_EXP_W_PREDICTED] == pred && + r.w[RX_EXP_W_PREDICTOR] == PREDICTOR, "prediction kept as an unqualified annotation"); + int noisy = field(o.next, RX_EXP_WALK_NOISE) != 2; + CHECK((r.w[RX_EXP_W_ERR_FIELDS] != 0) == (pred != o.next) && (pred != o.next) == noisy, + "prediction error counted (noise-blind model wrong iff noise != 0)"); + CHECK(r.w[RX_EXP_W_COST] == (uint64_t)llabs((int64_t)a - 3) + 1 && + r.w[RX_EXP_W_COST_UNIT] == RX_EXP_COST_DECLARED_UNITS, "declared cost"); + CHECK(r.w[RX_EXP_W_OWNER] == OWNER_A && r.w[RX_EXP_W_CONSENT] == RX_EXP_CONSENT_ALL && + r.w[RX_EXP_W_APPLY_OP] == F->op && r.w[RX_EXP_W_FAMILY] == F->family, "owner, consent, applicability"); + uint64_t anc[16]; + uint32_t na = cx_provenance(&e->c.cx, id, anc, 16); + int has_goal = 0; + for (uint32_t i = 0; i < na; i++) { + const CxObject *ao = cx_get(&e->c.cx, anc[i]); + has_goal |= ao && ao->kind == CX_K_WORK_ACCEPTED; + } + CHECK(has_goal, "provenance reaches the accepted goal"); + /* 3f. the same receipts again */ + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_DUPLICATE, "duplicate: refused"); + + /* 3g. stale: act twice, record the first */ + RxExpObservation o1, o2; + uint64_t s1 = rx_exp_state(e); + CHECK(rx_exp_act(e, policy(s1), &o1) == RX_EXP_OK, "act 1"); + CHECK(rx_exp_act(e, policy(rx_exp_state(e)), &o2) == RX_EXP_OK, "act 2"); + CHECK(e->dropped == 1, "unrecorded transition counted as dropped (%llu)", (unsigned long long)e->dropped); + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, 0, &o1); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_ERR_ORDER, "stale observation: refused"); + g = draft(OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN, 0, &o2); + CHECK(rx_exp_record(e, &g, &id) == RX_EXP_OK, "current observation recorded"); + + /* 3h. the environment refuses an inadmissible action: no new state, + * the refusal is in Cortex as AEGIS evidence with no winner */ + uint64_t before = rx_exp_state(e), ev_before = 0, ev_after = 0; + for (uint64_t i = 1; i <= e->c.cx.n; i++) ev_before += cx_get(&e->c.cx, i)->kind == CX_K_EVIDENCE_REF; + RxExpObservation bad; + CHECK(rx_exp_act(e, 0, &bad) == RX_EXP_ERR_REFUSED, "inadmissible action refused"); + CHECK(rx_exp_state(e) == before, "World state unchanged"); + uint64_t last_ev = 0; + for (uint64_t i = 1; i <= e->c.cx.n; i++) + if (cx_get(&e->c.cx, i)->kind == CX_K_EVIDENCE_REF) { ev_after++; last_ev = i; } + const uint64_t *lp = last_ev ? cx_payload(&e->c.cx, cx_get(&e->c.cx, last_ev)) : NULL; + CHECK(ev_after == ev_before + 1 && lp && lp[RXC_EP_WINNER] == RXC_NONE, + "refusal recorded as no-winner evidence"); + CHECK(cx_verify_chain(&e->c.cx) == CX_OK, "Cortex chain verifies"); + rx_exp_close(e); +} + +/* ---- 4 ----------------------------------------------------------------------- */ +static int same_view(const RxExpRecord *a, uint32_t na, const RxExpRecord *b, uint32_t nb) { + if (na != nb) return 0; + for (uint32_t i = 0; i < na; i++) + if (a[i].id != b[i].id || memcmp(a[i].w, b[i].w, sizeof a[i].w)) return 0; + return 1; +} + +static void t_consent(void) { + printf("[*] consent: revocation excludes, never deletes; view rebuilt after restart\n"); + char d[200]; + dir_for(d, sizeof d, "consent"); + RxExpEnv *e = &g_e; + CHECK(open_env(e, d, 1) == RX_EXP_OK, "open"); + CHECK(rx_exp_reset(e, 0xBEEF) == RX_EXP_OK, "reset"); + uint64_t a[6], b[2]; + for (int i = 0; i < 6; i++) { + uint64_t s = rx_exp_state(e), act = policy(s); + a[i] = step_record(e, act, noise_blind_prediction(s, act), OWNER_A, RX_EXP_CONSENT_ALL, + i == 5 ? RX_EXP_SPLIT_HOLDOUT : RX_EXP_SPLIT_TRAIN); + CHECK(a[i], "owner A experience %d", i); + } + for (int i = 0; i < 2; i++) { + uint64_t s = rx_exp_state(e), act = policy(s); + b[i] = step_record(e, act, 0, OWNER_B, i ? RX_EXP_CONSENT_RETRIEVE : RX_EXP_CONSENT_ALL, + RX_EXP_SPLIT_TRAIN); + CHECK(b[i], "owner B experience %d", i); + } + static RxExpRecord v0[32], v1[32], v2[32]; + RxExpViewStats st; + uint32_t n0 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v0, 32, &st); + CHECK(n0 == 6 && st.seen == 8 && st.split == 1 && st.no_consent == 1 && st.invalid == 0, + "train view: 5 A + 1 B (holdout and retrieve-only excluded) n=%u seen=%llu", n0, + (unsigned long long)st.seen); + for (uint32_t i = 1; i < n0; i++) CHECK(v0[i].t > v0[i - 1].t, "view ordered by time"); + + uint64_t rid = 0, n_before = e->c.cx.n; + CHECK(rx_exp_revoke(e, OWNER_B, a[1], RX_EXP_CONSENT_TRAIN, 1, &rid) == RX_EXP_ERR_OWNER && + e->c.cx.n == n_before, "non-owner cannot revoke; nothing written"); + CHECK(rx_exp_revoke(e, OWNER_A, a[1], RX_EXP_CONSENT_SHARE, 1, &rid) == RX_EXP_OK, "revoke share only"); + uint32_t n1 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v1, 32, &st); + CHECK(n1 == 6 && st.revoked == 0, "share revocation leaves training view"); + CHECK(rx_exp_revoke(e, OWNER_A, a[1], RX_EXP_CONSENT_TRAIN, 2, &rid) == RX_EXP_OK, "revoke train"); + const CxObject *ro = cx_get(&e->c.cx, rid); + CHECK(ro && ro->kind == CX_K_CONSENT_REVOKED && (ro->protect & CX_PROT_AUTHORITY) && + ro->links[0] == a[1], "revocation is a protected tombstone linking the experience"); + n1 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v1, 32, &st); + int gone = 1; + for (uint32_t i = 0; i < n1; i++) gone &= v1[i].id != a[1]; + CHECK(n1 == 5 && st.revoked == 1 && gone, "revoked experience excluded (n=%u)", n1); + RxExpRecord kept; + CHECK(rx_exp_decode(&e->c.cx, a[1], &kept) == RX_EXP_OK, "nothing deleted: record still verifies"); + uint32_t nr = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_RETRIEVE, RX_EXP_SPLIT_TRAIN, v2, 32, &st); + CHECK(nr == 7, "retrieval view keeps it (only train was withdrawn) n=%u", nr); + + /* owner-wide: every A experience so far; a later one is unaffected */ + CHECK(rx_exp_revoke(e, OWNER_A, 0, RX_EXP_CONSENT_TRAIN, 3, &rid) == RX_EXP_OK, "owner-wide revoke"); + uint64_t s = rx_exp_state(e), act = policy(s); + uint64_t later = step_record(e, act, 0, OWNER_A, RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN); + CHECK(later, "later experience"); + n1 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v1, 32, &st); + /* revoked counts all six A experiences, the heldout one included (consent is checked before split) */ + CHECK(n1 == 2 && v1[0].id == b[0] && v1[1].id == later && st.revoked == 6, + "owner-wide revocation: B's and the later A experience remain (n=%u revoked=%llu)", n1, + (unsigned long long)st.revoked); + CHECK(cx_verify_chain(&e->c.cx) == CX_OK, "chain"); + rx_exp_close(e); + + /* restart: the same ordered view from the journal alone */ + CHECK(open_env(e, d, 2) == RX_EXP_OK, "reopen"); + uint32_t n2 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v2, 32, &st); + CHECK(same_view(v1, n1, v2, n2), "view identical after restart (%u vs %u)", n1, n2); + uint32_t nh = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_RETRIEVE, RX_EXP_SPLIT_HOLDOUT, v2, 32, &st); + CHECK(nh == 1 && v2[0].id == a[5], "holdout view holds only the heldout experience"); + /* a tampered receipt takes its experience out of every view */ + cx_tamper(&e->c.cx, b[0], RX_EXP_W_NEXT, 0); + n2 = rx_exp_dataset(&e->c.cx, F, RX_EXP_CONSENT_TRAIN, RX_EXP_SPLIT_TRAIN, v2, 32, &st); + CHECK(n2 == 1 && v2[0].id == later && st.invalid == 1, "tampered experience excluded as invalid"); + rx_exp_close(e); + rmtree(d); +} + +/* ---- 5 ------------------------------------------------------------------------ */ +#define DET_STEPS 24 +typedef struct { uint64_t w[DET_STEPS][RX_EXP_W_WORDS]; uint64_t tr[DET_STEPS][3]; } Trace; + +static int run_trace(RxExpEnv *e, uint64_t seed, Trace *t) { + if (rx_exp_reset(e, seed) != RX_EXP_OK) return -1; + for (int i = 0; i < DET_STEPS; i++) { + uint64_t s = rx_exp_state(e), act = policy(s); + uint64_t id = step_record(e, act, noise_blind_prediction(s, act), OWNER_A, + RX_EXP_CONSENT_ALL, RX_EXP_SPLIT_TRAIN); + RxExpRecord r; + if (!id || rx_exp_decode(&e->c.cx, id, &r) != RX_EXP_OK) return -1; + memcpy(t->w[i], r.w, sizeof r.w); + t->tr[i][0] = r.w[RX_EXP_W_PRIOR]; + t->tr[i][1] = r.w[RX_EXP_W_ACTION]; + t->tr[i][2] = r.w[RX_EXP_W_NEXT]; + } + return 0; +} + +static void t_determinism(void) { + printf("[*] determinism: same seed -> identical experiences; seeds differ\n"); + static const uint64_t seeds[4] = { 1, 2, 0xC0FFEE, 0xFFFFFFFFFFFFFFFFull }; + static Trace first[4], again; + char d[200]; + for (int i = 0; i < 4; i++) { + char leaf[32]; + snprintf(leaf, sizeof leaf, "det%d_a", i); + dir_for(d, sizeof d, leaf); + CHECK(open_env(&g_e, d, 1) == RX_EXP_OK, "open a"); + CHECK(run_trace(&g_e, seeds[i], &first[i]) == 0, "trace seed %d", i); + /* reset to the same seed inside the same environment: same transitions */ + CHECK(run_trace(&g_e, seeds[i], &again) == 0, "trace after reset"); + CHECK(memcmp(first[i].tr, again.tr, sizeof again.tr) == 0, + "seed %d: reset reproduces every (prior, action, next)", i); + rx_exp_close(&g_e); + rmtree(d); + snprintf(leaf, sizeof leaf, "det%d_b", i); + dir_for(d, sizeof d, leaf); + CHECK(open_env(&g_e2, d, 1) == RX_EXP_OK, "open b"); + CHECK(run_trace(&g_e2, seeds[i], &again) == 0, "trace b"); + CHECK(memcmp(first[i].w, again.w, sizeof again.w) == 0, + "seed %d: fresh environment -> byte-identical experience payloads", i); + rx_exp_close(&g_e2); + rmtree(d); + } + for (int i = 0; i < 4; i++) + for (int j = i + 1; j < 4; j++) { + CHECK(memcmp(first[i].tr, first[j].tr, sizeof first[i].tr) != 0, + "seeds %d and %d give different trajectories", i, j); + CHECK(memcmp(first[i].w[0] + RX_EXP_W_SEED_COMMIT0, first[j].w[0] + RX_EXP_W_SEED_COMMIT0, + 32) != 0, "seed commitments differ"); + } + int wrong = 0, total = 0; + for (int i = 0; i < 4; i++) + for (int k = 0; k < DET_STEPS; k++) { + total++; + wrong += first[i].w[k][RX_EXP_W_ERR_FIELDS] != 0; + } + printf(" noise-blind predictor wrong on %d of %d transitions (unqualified annotation)\n", + wrong, total); +} + +int main(void) { + snprintf(g_base, sizeof g_base, "/tmp/rx_exp_test.XXXXXX"); + if (!mkdtemp(g_base)) { perror("mkdtemp"); return 2; } + uint8_t root[8] = { 'x', '1', 'e', 'x', 'p', 6, 9, 9 }; + if (aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, root, sizeof root, &g_self) != 0) return 2; + if (aienos_cap_start(&g_admin, &g_view) != 0) { fprintf(stderr, "aienos authority\n"); return 2; } + t_family(); + t_chain_and_unobserved(); + t_consent(); + t_determinism(); + aienos_cap_stop(g_admin, g_view); + rmtree(g_base); + printf("%s: %d checks, %d failed\n", g_fail ? "FAIL" : "PASS", g_checks, g_fail); + if (!g_fail) printf("OMEGA #699 EXPERIENCE TESTS: PASS\n"); + return g_fail ? 1 : 0; +}