Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(segment): harden forward-compatible initialization

JustVuggce81c707d158
diff --git a/c/segment_runtime.c b/c/segment_runtime.c
index b5f6c066..9dde4a42 100644
--- a/c/segment_runtime.c
+++ b/c/segment_runtime.c
@@ -145,6 +145,8 @@ int coli_segment_engine_capabilities(const ColiSegmentEngine *engine,
capabilities->struct_size < sizeof(*capabilities))
return set_error(error, error_size,
"invalid segment capabilities buffer");
+ size_t caller_size = capabilities->struct_size;
+ memset(capabilities, 0, caller_size);
memcpy(capabilities, &engine->capabilities, sizeof(*capabilities));
return 0;
}
diff --git a/c/segment_runtime.h b/c/segment_runtime.h
index 4b970d10..587218ba 100644
--- a/c/segment_runtime.h
+++ b/c/segment_runtime.h
@@ -10,9 +10,10 @@
* engine-specific objects opaque while callers can open a layer range, create
* isolated sessions, run activations through it and stream snapshots.
*
- * Version 1 is additive and unused by the existing CLI/serve paths. Adapters
- * register before the first lookup (normally from a link-time constructor),
- * after which the registry is read-only and safe for concurrent lookup.
+ * Version 1 is additive and unused by the existing CLI/serve paths. A
+ * consumer registers adapters explicitly during process initialization and
+ * before the first lookup, after which the registry is read-only and safe for
+ * concurrent lookup. No compiler-specific constructor mechanism is needed.
*/
#include <stddef.h>
@@ -160,6 +161,8 @@ int coli_segment_engine_open(const char *engine_id,
const ColiSegmentEngineOptions *options,
ColiSegmentEngine **engine,
char *error, size_t error_size);
+/* The caller sets struct_size to its allocation size. Bytes beyond the ABI
+ * version known by this runtime are zeroed for forward-compatible callers. */
int coli_segment_engine_capabilities(const ColiSegmentEngine *engine,
ColiSegmentCapabilities *capabilities,
char *error, size_t error_size);
diff --git a/c/tests/test_segment_runtime.c b/c/tests/test_segment_runtime.c
index 961b0fde..7b7ce694 100644
--- a/c/tests/test_segment_runtime.c
+++ b/c/tests/test_segment_runtime.c
@@ -181,6 +181,20 @@ int main(void) {
error, sizeof(error)) == 0);
assert(capabilities.state_width == 4 && capabilities.num_layers == 6);
assert(capabilities.flags & COLI_SEGMENT_CAP_RANGE_NATIVE);
+
+ struct {
+ ColiSegmentCapabilities capabilities;
+ uint64_t future_fields[2];
+ } future_capabilities;
+ memset(&future_capabilities, 0xa5, sizeof(future_capabilities));
+ future_capabilities.capabilities.struct_size = sizeof(future_capabilities);
+ assert(coli_segment_engine_capabilities(
+ engine, &future_capabilities.capabilities,
+ error, sizeof(error)) == 0);
+ assert(future_capabilities.capabilities.state_width == 4);
+ assert(future_capabilities.future_fields[0] == 0);
+ assert(future_capabilities.future_fields[1] == 0);
+
capabilities.struct_size = 0;
assert(coli_segment_engine_capabilities(engine, &capabilities,
error, sizeof(error)) != 0);
diff --git a/docs/segment-runtime.md b/docs/segment-runtime.md
index 87760fe9..b0ea9092 100644
--- a/docs/segment-runtime.md
+++ b/docs/segment-runtime.md
@@ -20,7 +20,9 @@ model family in later changes.
## Lifecycle
-1. Register a static `ColiSegmentAdapter` during process initialization.
+1. Call each model adapter's explicit registration function during process
+ initialization. The consumer must not depend on linker constructors; this
+ keeps initialization order visible and portable to MSVC.
2. Open an engine with `coli_segment_engine_open` and inspect its model-specific
`ColiSegmentCapabilities`.
3. Create one or more sessions. A session must not receive concurrent calls.
@@ -34,6 +36,10 @@ concurrent lookups begin; registration itself is not a hot-path operation.
Capabilities are returned after model open because the layer count, boundary
width and context limit may vary between checkpoints handled by one engine.
+The caller initializes `struct_size` to its allocation size. The runtime zeros
+that complete allocation before copying the fields it knows, so a future caller
+using a larger structure never observes uninitialized extension fields when it
+loads an older runtime.
`state_schema` identifies the activation and snapshot layout.
`numeric_class` identifies builds whose results and snapshots are compatible;
an adapter must include every relevant precision, reduction and backend rule in