diff --git a/.crumb b/.crumb index cd5b7dd7b..6ac3ff64e 100644 --- a/.crumb +++ b/.crumb @@ -46,74 +46,74 @@ "children": [ { "digest": "fb7cc6e608fdbe68a9735a1313285782b9717ff12bde17343f8ce635de3644f6", - "last_commit": "190ff891", + "last_commit": "09b5c37", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "24d7c03a26a7ffe9b181b73585480b5801289accbdf63146edbc36cf3e1cf21f", - "last_commit": "6637b32b", + "last_commit": "6637b32", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116c", + "last_commit": "09b5c37", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { "digest": "ac43a69e03617abcc3c94f3fc1bc10c7593d9d57d49059c20bc4ce3abfaa8489", - "last_commit": "e59633e5", + "last_commit": "e59633e", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "69cbb8ef13ccce47ccc21f159e95f1eac58679319e2cc25937065f95c4eb9207", - "last_commit": "ae8c7c54", + "last_commit": "09b5c37", "name": "polyglot", "purpose": "Directory polyglot. Purpose not yet described by a human." }, { "digest": "2aa8396144fa241f695a99c882fc347425604ac4822d5162be9c3ad80354fc9f", - "last_commit": "c02b278c", + "last_commit": "c02b278", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { "digest": "830ce7faaea9ae86190aa304fd8d7797881fbaca9d14fc39a327d202712f73fb", - "last_commit": "9e168fc8", + "last_commit": "09b5c37", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "922c70bc1e6025d8bdba6aa57147f41d0aff3e4303bc5c396e80c0d576fae62d", - "last_commit": "03e4e6a8", + "digest": "6203be86d7278ebaccb0b97d1f88a6978c66d7f7cf105173be93ed5e67583228", + "last_commit": "329af0f", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "d4b26789b662d538601b2dacee7e99667ef84c9d12a57dbeee3b894465bb449e", - "last_commit": "03e4e6a8", + "digest": "988f298ad658bf4b7d2552426f400d686e8deee0e00d04c94df63c7591601eed", + "last_commit": "329af0f", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "acd42de9387a133f34b4b6afca962c82d340fba67c6a770a74852406769b4186", - "last_commit": "7683eaaa", + "last_commit": "09b5c37", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:6232137ddc358962d5043d7aa6545a11f6303026e0eb2925f40c965d6f28d172", - "compiled_at": "2026-10-12T06:51:27Z", + "children_root": "sha256:4acc393c4e03dcf418146a6f86fb75164b282523557b15d1829ae95fa2f6be51", + "compiled_at": "2026-10-12T07:06:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:b8c39a9e1a051ae8db34dc8f3e6e74d62b8935603f2473b65b0e7d8982f759aa", + "digest": "sha256:b211e0a22bda77a47a5afa2c15fdba14e60a84ee1cd0a8405525a3d82e32bbe4", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", - "files": 9732, - "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", - "source_tree": "sha256:6c0e9e7ff6d2d82743fc6c5e6b3107eb7a92fdcf7f31ea8f8464d6b96d1b357f" + "files": 9734, + "generated_at_commit": "d749792ac142deb4cb0ea3af0422492719916f21", + "source_tree": "sha256:f75ed5d359635266f186d1e69fee16505867482206fde953e6e925fdb3c5c48f" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index e315de0fc..ac9e255ca 100644 --- a/Makefile +++ b/Makefile @@ -601,8 +601,26 @@ $(RX_CX_TEST): $(RX_CX_SRCS) $(OUT_DIR)/rx_cortex.o src/runtime/rx_cortex_record src/runtime/rx_world.h | $(OUT_DIR) $(CC) $(CFLAGS) -pthread -o $@ $(RX_CX_SRCS) $(OUT_DIR)/rx_cortex.o -test-cortex: $(RX_CX_TEST) +# Journal backend seam (native v1 P06 cut 1): rx_cortex.c built with +# CX_NO_POSIX must carry no file, lock or libc allocation symbol, and must +# pass the same append/replay/torn-tail contract over a memory backend. +RX_CX_FS_OBJ = $(OUT_DIR)/rx_cortex_freestanding.o +RX_CXJ_TEST = $(OUT_DIR)/rx_cortex_journal_test +RX_CX_FS_ALLOWED = memcpy|memmove|memset|memcmp|sha256_init|sha256_update|sha256_final|sha256_hash|cx_fs_alloc|cx_fs_realloc|cx_fs_free + +$(RX_CX_FS_OBJ): src/runtime/rx_cortex.c src/runtime/rx_cortex.h tests/runtime/cx_config_freestanding.h | $(OUT_DIR) + $(CC) $(CFLAGS) -ffreestanding -fno-builtin -DCX_NO_POSIX \ + -Itests/runtime -DCX_CONFIG_H='"cx_config_freestanding.h"' -c -o $@ src/runtime/rx_cortex.c + @bad=$$(nm -u $@ | awk '{print $$NF}' | grep -Ev '^(_GLOBAL_OFFSET_TABLE_|($(RX_CX_FS_ALLOWED)))$$' || true); \ + if [ -n "$$bad" ]; then echo "rx_cortex.c (CX_NO_POSIX) still needs: $$bad"; rm -f $@; exit 1; fi + @echo "rx_cortex.c (CX_NO_POSIX): no file, lock or libc allocation symbols" + +$(RX_CXJ_TEST): tests/runtime/rx_cortex_journal.c $(RX_CX_FS_OBJ) src/sha256.c src/runtime/rx_cortex.h | $(OUT_DIR) + $(CC) $(CFLAGS) -o $@ tests/runtime/rx_cortex_journal.c $(RX_CX_FS_OBJ) src/sha256.c + +test-cortex: $(RX_CX_TEST) $(RX_CXJ_TEST) ./$(RX_CX_TEST) + ./$(RX_CXJ_TEST) # Physical graphics seat against the native AIENOS authority. Not part of # GitHub checks. A pass on this machine is the only run that may set diff --git a/src/.crumb b/src/.crumb index ad44c5e3d..a0d7b6d70 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,128 +62,128 @@ "children": [ { "digest": "e999ad5d79b28e45cf3c87fca942087088f243bdfd0e55da7948eb5d6e619460", - "last_commit": "7350a61c", + "last_commit": "09b5c37", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "24b48531671e1ad40c520571c43aa6fa8dfe43ce7139a5724f31afa9f8411afd", - "last_commit": "834ee93e", + "last_commit": "09b5c37", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "5b110f4a6136ebd3d6703590962b7beed9f8cec034c2d12944b1590434e3c747", - "last_commit": "d48ab89e", + "last_commit": "09b5c37", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "18e933f213d7958215bb0ecf09116e5c21d3b335cfdabc1ffbf7a2a4e98b5250", - "last_commit": "f7b43890", + "last_commit": "09b5c37", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "41faf68aa92b4f16b40243239e1866110a443ce284ff9f7ec3b125b289bcd845", - "last_commit": "3c82389f", + "last_commit": "09b5c37", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "945ae745a338467614bee287efb628f38f53ad83e4842c1a7fbf7d0b91a8da57", - "last_commit": "00f54b6b", + "last_commit": "09b5c37", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "62a0884e7f075eb3c92b22fc400234a06bd8c3020fc75e55a9b0c0f0f6a26d7e", - "last_commit": "1a147e40", + "last_commit": "09b5c37", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { "digest": "330c407ff51bfd684e062707c055f58f325f5af16fe9764889aec3a7ffeb31ac", - "last_commit": "3108fc2f", + "last_commit": "09b5c37", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "3bdcb211108a4fdc33550b2c54df3167dd15d4a5fe004eefbcef11b489d0e27a", - "last_commit": "71c6ff88", + "last_commit": "09b5c37", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "651cad69999ba000a9be247d7decaf02ef9c4653f718255b62efb248139ad74b", - "last_commit": "6d8d92ad", + "last_commit": "6d8d92a", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, { "digest": "8383106b5d1b3adbc44b8e837a5d1f5ff6699a6d54070068e36716b2a32390d5", - "last_commit": "d550b705", + "last_commit": "09b5c37", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "8c5cf96997407cff3f8a4c78150ae5294d2ed09a8d9076ae933e426a60792037", - "last_commit": "e59633e5", + "last_commit": "e59633e", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "c43b60db4a5183a3ca59c2e24a0978f463e8ac3aa7a2ed0c54947eb085c4eb9e", - "last_commit": "3ef4f69c", + "last_commit": "09b5c37", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { - "digest": "224f473d0d4da21fb7bcd75d22e70a892679a7271e949a5ee07e3f36cbdb7442", - "last_commit": "781e98d8", + "digest": "10dd0764d81e96e6f6c4585d57b770d5b2854364cfb09a44bb16427c4517a9db", + "last_commit": "329af0f", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, { "digest": "9f6d98a6d9b0020aa56df481a5ab3aa301533cb85173f96a851d1d779fca2d3a", - "last_commit": "5ae192a4", + "last_commit": "09b5c37", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "ae63793b7b13442e349e438229fccf6a859a5be55343cdd997e35f2966c1157c", - "last_commit": "f633e343", + "last_commit": "09b5c37", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "be7e624435df81eae403f51a381acf480e9fd2f2a04eb34e955547973645b95d", - "last_commit": "54826a37", + "last_commit": "09b5c37", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "78c180a66607ebaf93e6ef6a6a56a3059ebf97db394d76af50c0e3f25d2be639", - "last_commit": "2564f57a", + "last_commit": "09b5c37", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "daf5e5e90c1ccb4c089220a6545844dbe4078a233f1777e8145b74f1bf5dd5fe", - "last_commit": "40d1ea37", + "last_commit": "09b5c37", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:ce7e4b528bec54e15d5f9525026d958e6eab78b88614c65b8e7c51d44c0243af", - "compiled_at": "2026-10-12T06:51:27Z", + "children_root": "sha256:2f64d32b2351272f4de10e1143efdb74dbc3cae56b1f0a3039ca9edcfebf6e8c", + "compiled_at": "2026-10-12T07:06:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:922c70bc1e6025d8bdba6aa57147f41d0aff3e4303bc5c396e80c0d576fae62d", + "digest": "sha256:6203be86d7278ebaccb0b97d1f88a6978c66d7f7cf105173be93ed5e67583228", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 451, - "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", - "source_tree": "sha256:b0f6d99356474d7426edd89658e7ddcf5c6f9624ab4a059d56915477cf3780c3" + "generated_at_commit": "d749792ac142deb4cb0ea3af0422492719916f21", + "source_tree": "sha256:96f504e4c814ac4c1e61dadc566cb0e649230bace262bc9bb10e256665172a7f" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index edece86ad..542c9efae 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T19:30:27Z", + "compiled_at": "2026-10-12T06:52:21Z", "compiler": "crumb-compile/1", - "digest": "sha256:224f473d0d4da21fb7bcd75d22e70a892679a7271e949a5ee07e3f36cbdb7442", + "digest": "sha256:10dd0764d81e96e6f6c4585d57b770d5b2854364cfb09a44bb16427c4517a9db", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 65, - "generated_at_commit": "8507ac948f46e305d1108520cabaed75f110695d", - "source_tree": "sha256:7a840be154414ed786ebf958ba98d4ea0636227cb6df04ade2652a8a5e4c3534" + "generated_at_commit": "07b72723d328742e510710ecbdaa43673afff9d1", + "source_tree": "sha256:e852a756f9620d054e65118708c2c14cee2fe95f0720cee8d4a65c0b0139f623" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_cortex.c b/src/runtime/rx_cortex.c index 322b14131..d3f42df03 100644 --- a/src/runtime/rx_cortex.c +++ b/src/runtime/rx_cortex.c @@ -5,13 +5,27 @@ #include "sha256.h" +#include + +/* Allocation hooks: a freestanding host names a config header through + * CX_CONFIG_H that defines CX_MALLOC(n), CX_REALLOC(p, n) and CX_FREE(p). */ +#ifdef CX_CONFIG_H +#include CX_CONFIG_H +#endif +#ifndef CX_MALLOC +#include +#define CX_MALLOC(n) malloc(n) +#define CX_REALLOC(p, n) realloc((p), (n)) +#define CX_FREE(p) free(p) +#endif + +#ifndef CX_NO_POSIX #include #include -#include -#include #include #include #include +#endif static void put_u64(sha256_ctx *c, uint64_t v) { uint8_t b[8]; @@ -46,21 +60,21 @@ static void chain_step(uint8_t chain[32], const uint8_t digest[32]) { int cx_init(CxStore *s, uint64_t n_subjects) { memset(s, 0, sizeof(*s)); - s->fd = -1; - s->by_subject = calloc(n_subjects ? n_subjects : 1, sizeof(CxIdList)); + size_t nb = (size_t)(n_subjects ? n_subjects : 1) * sizeof(CxIdList); + s->by_subject = CX_MALLOC(nb); if (!s->by_subject) return CX_ERR_NOMEM; + memset(s->by_subject, 0, nb); s->n_subjects = n_subjects; return CX_OK; } void cx_free(CxStore *s) { - for (uint64_t i = 0; i < s->n_subjects && s->by_subject; i++) free(s->by_subject[i].ids); - free(s->by_subject); - free(s->obj); - free(s->arena); - if (s->fd >= 0) close(s->fd); /* closing drops the flock */ + for (uint64_t i = 0; i < s->n_subjects && s->by_subject; i++) CX_FREE(s->by_subject[i].ids); + CX_FREE(s->by_subject); + CX_FREE(s->obj); + CX_FREE(s->arena); + if (s->jops) s->jops->close(s->jctx, s->jh); /* closing drops the writer lock */ memset(s, 0, sizeof(*s)); - s->fd = -1; } void cx_close(CxStore *s) { cx_free(s); } @@ -69,7 +83,7 @@ static int grow(void **p, uint64_t *cap, uint64_t need, size_t elem) { if (need <= *cap) return 0; uint64_t c = *cap ? *cap : 1024; while (c < need) c *= 2; - void *q = realloc(*p, c * elem); + void *q = CX_REALLOC(*p, c * elem); if (!q) return -1; *p = q; *cap = c; @@ -94,23 +108,10 @@ static uint64_t le_get(const uint8_t *p) { return v; } -static int write_all(int fd, const uint8_t *b, size_t n) { - while (n) { - ssize_t w = write(fd, b, n); - if (w < 0) { - if (errno == EINTR) continue; - return -1; - } - b += w; - n -= (size_t)w; - } - return 0; -} - /* Encode one object as a journal record into a fresh buffer. */ static uint8_t *encode_record(const CxObject *o, const uint64_t *payload, size_t *out_len) { size_t words = CX_R_FIXED + o->n + CX_R_DIGEST; - uint8_t *b = malloc(words * 8); + uint8_t *b = CX_MALLOC(words * 8); if (!b) return NULL; uint64_t w[CX_R_FIXED] = { CX_R_MAGIC, o->id, ((uint64_t)o->cls << 32) | o->kind, o->subject, o->t, @@ -141,7 +142,7 @@ static int append_mem(CxStore *s, const CxHeader *h, const uint64_t *payload, ui return CX_ERR_NOMEM; if (l->n == l->cap) { uint32_t c = l->cap ? l->cap * 2 : 64; - uint64_t *q = realloc(l->ids, (size_t)c * sizeof(uint64_t)); + uint64_t *q = CX_REALLOC(l->ids, (size_t)c * sizeof(uint64_t)); if (!q) return CX_ERR_NOMEM; l->ids = q; l->cap = c; @@ -189,19 +190,21 @@ int cx_append_as(CxStore *s, uint64_t token, const CxHeader *h, const uint64_t * uint64_t id = 0; int rc = append_mem(s, h, payload, n, 0, &id); if (rc != CX_OK) return rc; - if (s->fd >= 0) { + if (s->jops) { const CxObject *o = &s->obj[id - 1]; size_t len = 0; uint8_t *rec = encode_record(o, s->arena + o->off, &len); - off_t end = lseek(s->fd, 0, SEEK_END); - int bad = !rec || end < 0 || write_all(s->fd, rec, len) != 0 || - ((s->open_flags & CX_OPEN_SYNC) && fdatasync(s->fd) != 0); - free(rec); - if (bad) { - /* Never leave memory ahead of the journal, nor a partial record. */ - if (end >= 0 && ftruncate(s->fd, end) != 0) { /* reopen will see a torn tail */ } + if (!rec) { unappend_mem(s, prev); - return rec ? CX_ERR_IO : CX_ERR_NOMEM; + return CX_ERR_NOMEM; + } + /* The backend appends all of the record or none of it; memory never + * runs ahead of the journal. */ + int rc2 = s->jops->append(s->jctx, s->jh, rec, len, (s->open_flags & CX_OPEN_SYNC) != 0); + CX_FREE(rec); + if (rc2 != CX_OK) { + unappend_mem(s, prev); + return rc2; } } if (out_id) *out_id = id; @@ -226,18 +229,15 @@ void cx_release_writer(CxStore *s, uint64_t token) { /* ---- journal open / replay ----------------------------------------------- */ -static int read_file(int fd, uint8_t **out, size_t *len) { - struct stat st; - if (fstat(fd, &st) != 0) return CX_ERR_IO; - size_t n = (size_t)st.st_size; - uint8_t *b = malloc(n ? n : 1); +static int read_journal(const CxJournalOps *ops, void *ctx, void *h, uint8_t **out, size_t *len) { + size_t n = 0; + int rc = ops->size(ctx, h, &n); + if (rc != CX_OK) return rc; + uint8_t *b = CX_MALLOC(n ? n : 1); if (!b) return CX_ERR_NOMEM; - size_t got = 0; - while (got < n) { - ssize_t r = pread(fd, b + got, n - got, (off_t)got); - if (r < 0 && errno == EINTR) continue; - if (r <= 0) { free(b); return CX_ERR_IO; } - got += (size_t)r; + if (n) { + rc = ops->read(ctx, h, 0, b, n); + if (rc != CX_OK) { CX_FREE(b); return rc; } } *out = b; *len = n; @@ -259,7 +259,7 @@ static int replay(CxStore *s, const uint8_t *b, size_t len, size_t *good_end) { size_t rec = ((size_t)CX_R_FIXED + n + CX_R_DIGEST) * 8; if (len - p < rec) { rc = CX_ERR_TORN; break; } if (n > payload_cap) { - uint64_t *q = realloc(payload, (size_t)n * sizeof(uint64_t)); + uint64_t *q = CX_REALLOC(payload, (size_t)n * sizeof(uint64_t)); if (!q) { rc = CX_ERR_NOMEM; break; } payload = q; payload_cap = n; @@ -286,65 +286,148 @@ static int replay(CxStore *s, const uint8_t *b, size_t len, size_t *good_end) { p += rec; *good_end = p; } - free(payload); + CX_FREE(payload); return rc; } /* The single integrity seam: see the guarantee in rx_cortex.h. */ -int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags) { - if (!s || !path) return CX_ERR_ARG; +int cx_open_with(CxStore *s, const CxJournalOps *ops, void *ctx, const char *name, + uint64_t n_subjects, uint32_t flags) { + if (!s || !ops || !name || !ops->open || !ops->size || !ops->read || !ops->append || + !ops->truncate || !ops->close) + return CX_ERR_ARG; int ro = (flags & CX_OPEN_READONLY) != 0; - int fd = open(path, ro ? O_RDONLY | O_CLOEXEC : O_RDWR | O_CREAT | O_CLOEXEC, 0644); - if (fd < 0) return CX_ERR_IO; - if (!ro && flock(fd, LOCK_EX | LOCK_NB) != 0) { - int e = errno; - close(fd); - return e == EWOULDBLOCK ? CX_ERR_WRITER : CX_ERR_IO; - } + void *jh = NULL; + int rc = ops->open(ctx, name, ro, &jh); + if (rc != CX_OK) return rc; uint8_t *b = NULL; size_t len = 0; - int rc = read_file(fd, &b, &len); - if (rc != CX_OK) { close(fd); return rc; } + rc = read_journal(ops, ctx, jh, &b, &len); + if (rc != CX_OK) { ops->close(ctx, jh); return rc; } if (len == 0) { - if (ro || n_subjects == 0) { free(b); close(fd); return CX_ERR_FORMAT; } + if (ro || n_subjects == 0) { CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT; } uint8_t h[CX_J_HDR_WORDS * 8] = { 0 }; le_put(h, CX_J_MAGIC); le_put(h + 8, CX_J_VERSION); le_put(h + 16, n_subjects); le_put(h + 24, 0); /* reserved: canonical machine identity (M20 A1) */ - if (write_all(fd, h, sizeof h) != 0 || fdatasync(fd) != 0) { - free(b); close(fd); return CX_ERR_IO; - } + rc = ops->append(ctx, jh, h, sizeof h, 1); + if (rc != CX_OK) { CX_FREE(b); ops->close(ctx, jh); return rc; } } else { if (len < CX_J_HDR_WORDS * 8 || le_get(b) != CX_J_MAGIC || le_get(b + 8) != CX_J_VERSION) { - free(b); close(fd); return CX_ERR_FORMAT; + CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT; } uint64_t file_subjects = le_get(b + 16); - if (n_subjects && n_subjects != file_subjects) { free(b); close(fd); return CX_ERR_FORMAT; } + if (n_subjects && n_subjects != file_subjects) { + CX_FREE(b); ops->close(ctx, jh); return CX_ERR_FORMAT; + } n_subjects = file_subjects; } rc = cx_init(s, n_subjects); - if (rc != CX_OK) { free(b); close(fd); return rc; } + if (rc != CX_OK) { CX_FREE(b); ops->close(ctx, jh); return rc; } if (len) { size_t good = 0; rc = replay(s, b, len, &good); if (rc == CX_ERR_TORN && !ro && (flags & CX_OPEN_REPAIR_TAIL)) { /* Only an incomplete trailing record is dropped. A complete record * whose digest differs is never repaired. */ - rc = ftruncate(fd, (off_t)good) == 0 && fdatasync(fd) == 0 ? CX_OK : CX_ERR_IO; + rc = ops->truncate(ctx, jh, good); } } - free(b); + CX_FREE(b); if (rc != CX_OK) { - close(fd); + ops->close(ctx, jh); cx_free(s); return rc; } - s->fd = fd; + s->jops = ops; + s->jctx = ctx; + s->jh = jh; s->open_flags = flags; return CX_OK; } +#ifndef CX_NO_POSIX +/* ---- the file backend ---------------------------------------------------- */ + +static int write_all(int fd, const uint8_t *b, size_t n) { + while (n) { + ssize_t w = write(fd, b, n); + if (w < 0) { + if (errno == EINTR) continue; + return -1; + } + b += w; + n -= (size_t)w; + } + return 0; +} + +static int jp_open(void *ctx, const char *name, int ro, void **h) { + (void)ctx; + int fd = open(name, ro ? O_RDONLY | O_CLOEXEC : O_RDWR | O_CREAT | O_CLOEXEC, 0644); + if (fd < 0) return CX_ERR_IO; + if (!ro && flock(fd, LOCK_EX | LOCK_NB) != 0) { + int e = errno; + close(fd); + return e == EWOULDBLOCK ? CX_ERR_WRITER : CX_ERR_IO; + } + *h = (void *)(intptr_t)fd; + return CX_OK; +} + +static int jp_size(void *ctx, void *h, size_t *len) { + (void)ctx; + struct stat st; + if (fstat((int)(intptr_t)h, &st) != 0) return CX_ERR_IO; + *len = (size_t)st.st_size; + return CX_OK; +} + +static int jp_read(void *ctx, void *h, size_t off, uint8_t *out, size_t n) { + (void)ctx; + int fd = (int)(intptr_t)h; + size_t got = 0; + while (got < n) { + ssize_t r = pread(fd, out + got, n - got, (off_t)(off + got)); + if (r < 0 && errno == EINTR) continue; + if (r <= 0) return CX_ERR_IO; + got += (size_t)r; + } + return CX_OK; +} + +static int jp_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) { + (void)ctx; + int fd = (int)(intptr_t)h; + off_t end = lseek(fd, 0, SEEK_END); + if (end < 0) return CX_ERR_IO; + if (write_all(fd, b, n) != 0 || (sync && fdatasync(fd) != 0)) { + /* Never leave a partial record behind. */ + if (ftruncate(fd, end) != 0) { /* reopen will see a torn tail */ } + return CX_ERR_IO; + } + return CX_OK; +} + +static int jp_truncate(void *ctx, void *h, size_t len) { + (void)ctx; + int fd = (int)(intptr_t)h; + return ftruncate(fd, (off_t)len) == 0 && fdatasync(fd) == 0 ? CX_OK : CX_ERR_IO; +} + +static void jp_close(void *ctx, void *h) { + (void)ctx; + close((int)(intptr_t)h); /* closing drops the flock */ +} + +const CxJournalOps cx_journal_posix = { jp_open, jp_size, jp_read, jp_append, jp_truncate, jp_close }; + +int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags) { + return cx_open_with(s, &cx_journal_posix, NULL, path, n_subjects, flags); +} +#endif /* !CX_NO_POSIX */ + /* ---- read ---------------------------------------------------------------- */ const CxObject *cx_get(const CxStore *s, uint64_t id) { diff --git a/src/runtime/rx_cortex.h b/src/runtime/rx_cortex.h index 923d31573..6c358b6c7 100644 --- a/src/runtime/rx_cortex.h +++ b/src/runtime/rx_cortex.h @@ -142,8 +142,10 @@ typedef struct CxStore { uint64_t n_subjects; uint8_t chain[32]; /* running hash over every digest in append order */ uint64_t examined; /* objects touched by queries, cumulative */ - /* Journal backing (cx_open). fd < 0: memory only. */ - int fd; + /* Journal backing (cx_open_with). jops NULL: memory only. */ + const struct CxJournalOps *jops; + void *jctx; /* backend context, handed back to every op */ + void *jh; /* the open journal, owned by the backend */ uint32_t open_flags; /* The one writer, 0 = unclaimed. While claimed, only cx_append_as with * the same token appends. */ @@ -199,6 +201,45 @@ enum { CX_OPEN_REPAIR_TAIL = 1u << 2 /* truncate one incomplete trailing record */ }; +/* ---- journal backend ------------------------------------------------------ + * The store reaches its journal only through this table. cx_journal_posix is + * the file backend (one file, flock for the writer lock); it is the default + * of cx_open and is compiled out under CX_NO_POSIX, where a freestanding host + * (AIENOS: sealed Store objects, P06 adapter) supplies its own table through + * cx_open_with. Memory comes from CX_MALLOC / CX_REALLOC / CX_FREE, which a + * config header named by CX_CONFIG_H may define; the default is libc. + * The record format, the digests, the chain and every check in cx_open are + * the same under every backend: a backend stores bytes, it never interprets + * them. */ +typedef struct CxJournalOps { + /* Open the journal `name`. ro: no lock, no writes, must exist. Writable: + * create when absent and take the one-writer lock, CX_ERR_WRITER when + * another writer holds it. *h receives the backend's handle. */ + int (*open)(void *ctx, const char *name, int ro, void **h); + /* Current length in bytes. */ + int (*size)(void *ctx, void *h, size_t *len); + /* Exactly n bytes at offset off, or an error. */ + int (*read)(void *ctx, void *h, size_t off, uint8_t *out, size_t n); + /* Append n bytes as ONE unit: on return either all of them are in the + * journal (durable when sync != 0) or none of them is, and the length is + * as before. A partial write is the backend's to undo. */ + int (*append)(void *ctx, void *h, const uint8_t *b, size_t n, int sync); + /* Cut the journal back to len bytes, durably (CX_OPEN_REPAIR_TAIL). A + * backend whose appends are atomic never holds a torn tail and may + * answer CX_ERR_IO. */ + int (*truncate)(void *ctx, void *h, size_t len); + /* Release the handle; a writer's lock drops here. */ + void (*close)(void *ctx, void *h); +} CxJournalOps; + +#ifndef CX_NO_POSIX +extern const CxJournalOps cx_journal_posix; +#endif + +/* cx_open through an explicit backend; the contract below is unchanged. */ +int cx_open_with(CxStore *s, const CxJournalOps *ops, void *ctx, const char *name, + uint64_t n_subjects, uint32_t flags); + /* Open (creating if absent and writable) a journal-backed store. A writable * open takes an exclusive lock; a second writable open of the same file, * from this or any process, gets CX_ERR_WRITER. n_subjects must match a @@ -220,7 +261,9 @@ enum { * record digest in the J-Space checkpoint anchor and cross-checks it at open. * KNOWN LIMIT: if both the Cortex journal and the J-Space checkpoint roll back * together, nothing local detects it (an external anchor problem). */ +#ifndef CX_NO_POSIX int cx_open(CxStore *s, const char *path, uint64_t n_subjects, uint32_t flags); +#endif void cx_close(CxStore *s); /* = cx_free */ const CxObject *cx_get(const CxStore *s, uint64_t id); diff --git a/tests/.crumb b/tests/.crumb index c1b7dffa8..a8d4a3926 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,194 +110,194 @@ "children": [ { "digest": "e7498eca87fe5ca8dc27d92f2d99be0de3e4c804711f9d8cba5bb1e47ca95f74", - "last_commit": "28911be7", + "last_commit": "09b5c37", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "d484ca176e9932b098df5664d01bdd679c78f5b89f4d15a77eb30f43d235f570", - "last_commit": "834ee93e", + "last_commit": "09b5c37", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "42a7efc25b16d1ea7bd7a5cbc31be7e499c9bc64c9e7df179c7627121d7df637", - "last_commit": "1b0e2968", + "last_commit": "09b5c37", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cbe9561b87b394e200f67996ce1407c377025d07ad033b27cdd542625e277d4", - "last_commit": "30bb116c", + "last_commit": "09b5c37", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { "digest": "d0d9e7040af5d739682fe07e15cb59598f0a981ab1a766a4c2ad2f26e4e1e458", - "last_commit": "eb8dedf7", + "last_commit": "09b5c37", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "47683e6a56ca8e14ee526004b2a90dd08498507e8d4b0b42c3d85d22afb07d28", - "last_commit": "28b87934", + "last_commit": "09b5c37", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "95b2041562face1ba7baa6c57456bf07d77a0f44e093b989ec18f559064e22d9", - "last_commit": "49abb024", + "last_commit": "09b5c37", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "d57833fb3f63ef6475cf25e2e16319d818a0a407b825d5b4c5cbdd3cc0e06492", - "last_commit": "00f54b6b", + "last_commit": "09b5c37", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "fb0a9296a93d065d5e016ad0a950fc6b9691f8ddca12ad0111cee2e35791cdb4", - "last_commit": "c819b376", + "last_commit": "09b5c37", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "bc2473da101ba5a74c32021a9d27ecb2007897f07a9689ba50ccc99b9030bf0c", - "last_commit": "1a147e40", + "last_commit": "09b5c37", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "de2bc4e310a257dbd21cc5c9624641b137b1b4487d8876cb52469aea2af5b904", - "last_commit": "3108fc2f", + "last_commit": "09b5c37", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "90418926706f827aafd72938aee1dea89222cb5e465fba06a8df5a83253e0412", - "last_commit": "28b87934", + "last_commit": "09b5c37", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "c653c851c8934ce9ac4eabaa32a9df65d858c9ac7a556e76b265b1ee81494443", - "last_commit": "4f55f45e", + "last_commit": "09b5c37", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "e485aa0a00f0a588e22d55caa704db8581a5da1b53080b311086e6edd38f6044", - "last_commit": "30bb116c", + "last_commit": "09b5c37", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "adba9b5ea1c000db36078fca7d1bfef0b2769b73c8fb474024d9409bd48bdbc1", - "last_commit": "40d1ea37", + "last_commit": "09b5c37", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "f3085ce1facdcdb22bfedc85de659dd5e512aa7b59cd8103666c24a4456681c3", - "last_commit": "6d8d92ad", + "last_commit": "6d8d92a", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, { "digest": "a4d320b3203650a28c43ea954957d8902d1cc25cbdc8aecd15628c01a92f68f8", - "last_commit": "d550b705", + "last_commit": "09b5c37", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "d5153d318a11642a5e13824353222fc392ead92108540bc8ffd747fcfd40b813", - "last_commit": "e59633e5", + "last_commit": "e59633e", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "4619a90a957e77cd3d5680132ab4d1a5ef65f43419ddcc089743ca5b7a7719ee", - "last_commit": "8f8babe9", + "last_commit": "09b5c37", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "16bea65aa50f0b3ab8b90cb286de048639fa8e635ff84e517e138c6f9f26a059", - "last_commit": "3c82389f", + "last_commit": "09b5c37", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "ba0c09f67d12b5d7de906129bdbe2f6e0a228e1a34236ecc4b779c523cb2061e", - "last_commit": "6fdc4c33", + "last_commit": "09b5c37", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "000c487afa8f600842eff25a22a671afb5e29b26845105405de9542a21b70746", - "last_commit": "79a805d1", + "last_commit": "09b5c37", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "25cabf47d18353539db28146434d246a2f81bf9c323471566451aca56806f279", - "last_commit": "44d8c06c", + "last_commit": "09b5c37", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "9106f2ea57e8f453322f0a988b61cfcbd06631573c9ff32a777903d27d7741e1", - "last_commit": "40d1ea37", + "last_commit": "09b5c37", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "381184e79b26920b256dd4a774fb11f17a92afa8ceda4813eada241bac4163a9", - "last_commit": "30bb116c", + "last_commit": "09b5c37", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { - "digest": "befa6601937d28b7c058b3abc4c12c90cbe8f2fce3f9498a3e67cd522391b61c", - "last_commit": "781e98d8", + "digest": "06a368ef677849a7a83d6275b02e2981afc51449d5fb11622902687d4064b265", + "last_commit": "329af0f", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, { "digest": "b1311025a3bfd8dd7b5e07b1adf1d7ece502a15200f7207f515334f7083b8315", - "last_commit": "5ae192a4", + "last_commit": "09b5c37", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "3ecf636e2923038708cafa062895b8d8f2664dc1865cdff38575911eaa9f1b2d", - "last_commit": "54826a37", + "last_commit": "09b5c37", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "75d5e576c38a0497a55e9f5ee79550cb94f9b75008f3e4e126312802704b3f86", - "last_commit": "2564f57a", + "last_commit": "09b5c37", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "3f19e3034ff999211cb13ab8995ca6c38d0d8e1f5bfbbbd621caaa08412d3f1f", - "last_commit": "fed4f077", + "last_commit": "09b5c37", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:9c94c5a57dda7727107b9e1271460c33b2cde72e796459e1ae401d4d4aeaec78", - "compiled_at": "2026-10-12T06:51:27Z", + "children_root": "sha256:9597c068d5bf4eb27f1d91842813a2b10269c2ccae421dceefb4308030ec0fc2", + "compiled_at": "2026-10-12T07:06:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:d4b26789b662d538601b2dacee7e99667ef84c9d12a57dbeee3b894465bb449e", + "digest": "sha256:988f298ad658bf4b7d2552426f400d686e8deee0e00d04c94df63c7591601eed", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1022, - "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", - "source_tree": "sha256:545d8dca817b4a6271e5ce24554973207f99c0bcbbde4e87ca4d6d93efcb41d9" + "files": 1024, + "generated_at_commit": "d749792ac142deb4cb0ea3af0422492719916f21", + "source_tree": "sha256:2b7415923db4376fb37718a5941526c107ff91365d2c94916e5df53139d31107" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 1f0a83f82..453ef8e44 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T19:30:27Z", + "compiled_at": "2026-10-12T06:52:21Z", "compiler": "crumb-compile/1", - "digest": "sha256:befa6601937d28b7c058b3abc4c12c90cbe8f2fce3f9498a3e67cd522391b61c", + "digest": "sha256:06a368ef677849a7a83d6275b02e2981afc51449d5fb11622902687d4064b265", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 68, - "generated_at_commit": "8507ac948f46e305d1108520cabaed75f110695d", - "source_tree": "sha256:48a242a9007e050ca566cc0fe1110a52ec47bd643d1eeefd369bc956513d0f23" + "files": 70, + "generated_at_commit": "07b72723d328742e510710ecbdaa43673afff9d1", + "source_tree": "sha256:4f7d42c9843ba6cb5bfa2688edb8631ff859437c7cde9be5e7d25733bfbe9b05" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/cx_config_freestanding.h b/tests/runtime/cx_config_freestanding.h new file mode 100644 index 000000000..15ce09bfc --- /dev/null +++ b/tests/runtime/cx_config_freestanding.h @@ -0,0 +1,13 @@ +/* cx_config_freestanding.h -- allocation hooks for the CX_NO_POSIX build of + * rx_cortex.c (tests/runtime/rx_cortex_journal.c supplies them). A real + * freestanding host (AIENOS) names its own header through CX_CONFIG_H. */ +#ifndef CX_CONFIG_FREESTANDING_H +#define CX_CONFIG_FREESTANDING_H +#include +void *cx_fs_alloc(size_t n); +void *cx_fs_realloc(void *p, size_t n); +void cx_fs_free(void *p); +#define CX_MALLOC(n) cx_fs_alloc(n) +#define CX_REALLOC(p, n) cx_fs_realloc((p), (n)) +#define CX_FREE(p) cx_fs_free(p) +#endif diff --git a/tests/runtime/rx_cortex_journal.c b/tests/runtime/rx_cortex_journal.c new file mode 100644 index 000000000..9fd31233d --- /dev/null +++ b/tests/runtime/rx_cortex_journal.c @@ -0,0 +1,190 @@ +/* + * rx_cortex_journal.c -- the journal backend seam (native v1 P06 cut 1). + * + * Links the CX_NO_POSIX build of rx_cortex.c (no file, lock or libc + * allocation symbols; see the nm check in the Makefile) against a memory + * backend and proves the contract a backend is held to: one writer, every + * record either wholly in the journal or not at all (memory never runs + * ahead), replay and chain identical on reopen, a torn tail refused or + * repaired only on request, a changed byte never repaired. + */ +#include "runtime/rx_cortex.h" + +#include +#include +#include + +static int g_fail, g_checks; +#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) + +/* The allocation hooks named by cx_config_freestanding.h. */ +static long g_live; +void *cx_fs_alloc(size_t n) { g_live++; return malloc(n); } +void *cx_fs_realloc(void *p, size_t n) { if (!p) g_live++; return realloc(p, n); } +void cx_fs_free(void *p) { if (p) g_live--; free(p); } + +/* A memory journal: one byte buffer, a writer flag, fault switches. */ +typedef struct { + uint8_t *b; + size_t len, cap; + int writer; /* 1 while a writable handle is open */ + int fail_append; /* next append fails after a partial copy (undone) */ + int fail_truncate; + int opens, appends, truncates, syncs; +} MemJournal; + +static int mj_open(void *ctx, const char *name, int ro, void **h) { + MemJournal *j = ctx; + (void)name; + j->opens++; + if (!ro) { + if (j->writer) return CX_ERR_WRITER; + j->writer = 1; + } + *h = ro ? (void *)&j->len : (void *)j; /* distinct handles, same journal */ + return CX_OK; +} +static int mj_size(void *ctx, void *h, size_t *len) { (void)h; *len = ((MemJournal *)ctx)->len; return CX_OK; } +static int mj_read(void *ctx, void *h, size_t off, uint8_t *out, size_t n) { + MemJournal *j = ctx; + (void)h; + if (off + n > j->len) return CX_ERR_IO; + memcpy(out, j->b + off, n); + return CX_OK; +} +static int mj_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) { + MemJournal *j = ctx; + if (h != (void *)j) return CX_ERR_IO; /* read-only handle */ + j->appends++; + if (j->len + n > j->cap) { + size_t c = j->cap ? j->cap * 2 : 4096; + while (c < j->len + n) c *= 2; + uint8_t *q = realloc(j->b, c); + if (!q) return CX_ERR_IO; + j->b = q; + j->cap = c; + } + if (j->fail_append) { + /* A partial write happened; the backend undoes it before returning. */ + memcpy(j->b + j->len, b, n / 2); + memset(j->b + j->len, 0xee, n / 2); + j->fail_append = 0; + return CX_ERR_IO; + } + memcpy(j->b + j->len, b, n); + j->len += n; + if (sync) j->syncs++; + return CX_OK; +} +static int mj_truncate(void *ctx, void *h, size_t len) { + MemJournal *j = ctx; + if (h != (void *)j) return CX_ERR_IO; + j->truncates++; + if (j->fail_truncate) return CX_ERR_IO; + if (len > j->len) return CX_ERR_IO; + j->len = len; + return CX_OK; +} +static void mj_close(void *ctx, void *h) { + MemJournal *j = ctx; + if (h == (void *)j) j->writer = 0; +} +static const CxJournalOps mem_ops = { mj_open, mj_size, mj_read, mj_append, mj_truncate, mj_close }; + +static CxHeader hdr(uint32_t cls, uint32_t kind, uint64_t subject, uint64_t t) { + CxHeader h; + memset(&h, 0, sizeof h); + h.cls = cls; + h.kind = kind; + h.subject = subject; + h.t = t; + return h; +} + +int main(void) { + MemJournal j; + memset(&j, 0, sizeof j); + CxStore s, t; + + /* 1. create: a header is written durably; a read-only open of nothing is refused */ + CHECK(cx_open_with(&t, &mem_ops, &j, "m", 16, CX_OPEN_READONLY) == CX_ERR_FORMAT, "ro open of empty"); + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_SYNC) == CX_OK, "create"); + CHECK(j.len == 32 && j.syncs == 1 && j.writer == 1, "header len=%zu syncs=%d", j.len, j.syncs); + CHECK(cx_open_with(&t, &mem_ops, &j, "m", 16, 0) == CX_ERR_WRITER, "second writer admitted"); + CHECK(cx_open_with(&t, &mem_ops, &j, "m", 0, CX_OPEN_READONLY) == CX_OK && t.n == 0, "ro while written"); + cx_close(&t); + CHECK(j.writer == 1, "ro close dropped the writer"); + + /* 2. append: each record is one atomic unit, synced under CX_OPEN_SYNC */ + for (uint64_t k = 1; k <= 5; k++) { + CxHeader h = hdr(CX_CLAIM, CX_K_CLAIM, k % 3, k); + uint64_t pay[3] = { k, k + 100, k + 200 }; + CHECK(cx_append(&s, &h, pay, 3, NULL) == CX_OK, "append %llu", (unsigned long long)k); + } + CHECK(s.n == 5 && j.appends == 6 && j.syncs == 6, "appends=%d syncs=%d", j.appends, j.syncs); + size_t len5 = j.len; + uint8_t chain5[32]; + memcpy(chain5, s.chain, 32); + + /* 3. a failed append leaves memory and journal exactly as before */ + j.fail_append = 1; + CxHeader h = hdr(CX_OBSERVATION, CX_K_EVIDENCE_REF, 1, 6); + uint64_t pay[1] = { 42 }; + CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_ERR_IO, "failed append not reported"); + CHECK(s.n == 5 && j.len == len5 && memcmp(s.chain, chain5, 32) == 0, "memory ran ahead of the journal"); + CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_OK && s.n == 6, "append after failure"); + uint8_t chain6[32]; + memcpy(chain6, s.chain, 32); + cx_close(&s); + CHECK(j.writer == 0, "close kept the writer lock"); + + /* 4. reopen replays every record; the chain is rebuilt, not read */ + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 0, 0) == CX_OK, "reopen"); + CHECK(s.n == 6 && s.n_subjects == 16 && memcmp(s.chain, chain6, 32) == 0 && + cx_verify_chain(&s) == CX_OK, "replayed state differs"); + CxRecord r; + CHECK(cx_recall_id(&s, 4, &r) == CX_OK && r.payload[2] == 204, "recalled record differs"); + CHECK(cx_open_with(&t, &mem_ops, &j, "m", 17, CX_OPEN_READONLY) == CX_ERR_FORMAT, "subject mismatch"); + cx_close(&s); + + /* 5. a torn tail is refused, repaired only on request, and only by the backend */ + size_t full = j.len; + j.len = full - 5; + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, 0) == CX_ERR_TORN, "torn tail accepted"); + CHECK(j.writer == 0, "failed open kept the writer lock"); + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_READONLY) == CX_ERR_TORN, "ro torn tail accepted"); + j.fail_truncate = 1; + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_ERR_IO, "repair without truncate"); + j.fail_truncate = 0; + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_OK && s.n == 5 && + j.truncates == 2 && j.len < full - 5, "repair tail"); + CHECK(memcmp(s.chain, chain5, 32) == 0, "repaired chain differs"); + cx_close(&s); + + /* 6. a complete record with a changed byte is never repaired */ + j.b[32 + 8 * 14] ^= 1; /* first record, payload word 1 */ + CHECK(cx_open_with(&s, &mem_ops, &j, "m", 16, CX_OPEN_REPAIR_TAIL) == CX_ERR_DIGEST, "tampered journal opened"); + j.b[32 + 8 * 14] ^= 1; + + /* 7. memory-only stores never touch a backend; nothing leaks */ + int appends_before = j.appends; + CHECK(cx_init(&s, 4) == CX_OK && s.jops == NULL, "memory-only init"); + h = hdr(CX_CLAIM, CX_K_CLAIM, 1, 1); + CHECK(cx_append(&s, &h, pay, 1, NULL) == CX_OK && j.appends == appends_before, + "memory-only append hit the backend"); + cx_close(&s); + CHECK(g_live == 0, "live allocations after close: %ld", g_live); + + free(j.b); + printf("%d checks, %d failures\n", g_checks, g_fail); + printf(g_fail ? "M20 CORTEX JOURNAL SEAM: FAIL\n" : "M20 CORTEX JOURNAL SEAM: PASS\n"); + return g_fail ? 1 : 0; +}