feat(host): expose memcard controls and qualify process persistence
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good

This commit is contained in:
2026-10-30 10:10:07 +00:00
parent dcf6561ea7
commit a4bec5bc45
42 changed files with 4778 additions and 100 deletions
@@ -107,4 +107,5 @@ mod tests {
}
}
pub mod memcard_fixture;
pub mod program;
@@ -0,0 +1,141 @@
//! Neutral PBX/ABI fixture. No source-language frontend or compiler is involved.
use crate::program::{TestProgram, scalar_catalog};
use prometeu_bytecode::{
assembler::assemble,
model::{ConstantPoolEntry, SyscallDecl},
registry::*,
};
use std::collections::BTreeMap;
#[derive(Clone, Copy)]
pub enum MemcardFixture {
Write,
Read,
Dirty,
}
#[derive(Default)]
struct Code {
bytes: Vec<u8>,
labels: BTreeMap<&'static str, u32>,
fixups: Vec<(usize, &'static str)>,
}
impl Code {
fn emit(&mut self, text: &str) {
self.bytes.extend(assemble(text).expect("fixture assembly"));
}
fn mark(&mut self, name: &'static str) {
self.labels.insert(name, self.bytes.len() as u32);
}
fn jump(&mut self, op: &str, name: &'static str) {
self.fixups.push((self.bytes.len() + 2, name));
self.emit(&format!("{op} 0"));
}
fn check(&mut self, value: i64, int32: bool) {
self.emit(&format!("PUSH_{} {value}\nEQ", if int32 { "I32" } else { "I64" }));
let resume = self.bytes.len() + 8; // conditional jump followed by a terminal trap
self.emit(&format!("JMP_IF_TRUE {resume}\nTRAP"));
}
fn finish(mut self) -> Vec<u8> {
for (offset, label) in self.fixups {
self.bytes[offset..offset + 4].copy_from_slice(&self.labels[label].to_le_bytes());
}
self.bytes
}
}
/// Stack oracle at a completed frame: 100 pending, 101 durable SUCCESS,
/// 104 MEMORY_ONLY, 201 exact existing readback, 301 dirty/uncommitted RAM.
/// Read never writes or submits a commit; a missing/wrong payload traps.
pub fn memcard_program(kind: MemcardFixture) -> Vec<u8> {
let mut main = Code::default();
main.emit("HOSTCALL 0");
main.check(0, true);
main.emit("PUSH_I64 4\nARRAY_NEW 9\nSET_GLOBAL 1");
if matches!(kind, MemcardFixture::Read) {
main.emit("GET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 0\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 4\nHOSTCALL 1");
main.check(4, false);
main.check(0, false);
for (i, value) in [0, 127, 128, 255].into_iter().enumerate() {
main.emit(&format!("GET_GLOBAL 1\nPUSH_I64 {i}\nARRAY_GET 9"));
main.check(value, true);
}
main.mark("done");
main.emit("PUSH_I64 201\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "done");
} else {
for (i, value) in [0, 127, 128, 255].into_iter().enumerate() {
main.emit(&format!("GET_GLOBAL 1\nPUSH_I64 {i}\nPUSH_I32 {value}\nARRAY_SET 9"));
}
main.emit("GET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 0\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 4\nHOSTCALL 2");
main.check(4, false);
main.check(0, false);
if matches!(kind, MemcardFixture::Dirty) {
main.mark("done");
main.emit("PUSH_I64 301\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "done");
} else {
main.emit("GET_GLOBAL 0\nPUSH_I32 0\nHOSTCALL 3\nSET_GLOBAL 2");
main.check(0, false);
main.mark("query");
main.emit("GET_GLOBAL 0\nGET_GLOBAL 2\nHOSTCALL 4\nPOP_N 1\nSET_GLOBAL 3");
main.check(0, false);
main.emit("GET_GLOBAL 3\nPUSH_I64 100\nADD\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "query");
}
}
main.mark("error");
main.emit("TRAP");
let mut code = main.finish();
let main_len = code.len() as u32;
let hook = if matches!(kind, MemcardFixture::Read) {
assemble("GET_LOCAL 0\nSET_GLOBAL 0\nPUSH_I32 0\nRET").unwrap()
} else {
assemble("GET_LOCAL 0\nSET_GLOBAL 0\nPUSH_I64 560\nARRAY_NEW 9\nSET_GLOBAL 1\nGET_LOCAL 0\nPUSH_CONST 0\nPUSH_I64 1\nPUSH_I32 1\nPUSH_I32 1\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 560\nHOSTCALL 1\nPOP_N 1\nPUSH_I32 0\nRET").unwrap()
};
let hook_len = hook.len() as u32;
code.extend(hook);
let mut types = scalar_catalog();
types.push(TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) });
let mut imports = vec![("bootstrap", 0, 1)];
if !matches!(kind, MemcardFixture::Read) {
imports.push(("presentation", 8, 1));
}
match kind {
MemcardFixture::Read => imports.push(("slot_read", 6, 2)),
MemcardFixture::Dirty => imports.push(("slot_write", 6, 2)),
MemcardFixture::Write => {
imports.extend([("slot_write", 6, 2), ("slot_commit", 2, 2), ("query", 2, 3)])
}
}
let syscalls = imports
.into_iter()
.map(|(name, arg_slots, ret_slots)| SyscallDecl {
module: "mem".into(),
name: name.into(),
version: 1,
arg_slots,
ret_slots,
})
.collect();
TestProgram {
const_pool: vec![ConstantPoolEntry::String("Neutral persistence fixture".into())],
code,
functions: vec![
FunctionDecl { code_len: main_len, max_stack_slots: 32, ..Default::default() },
FunctionDecl {
lifecycle_role: LifecycleRole::MemcardBoot,
code_offset: main_len,
code_len: hook_len,
max_stack_slots: 32,
signature: Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![TypeId(1)],
},
..Default::default()
},
],
syscalls,
..Default::default()
}
.serialize_with_types(types)
}
@@ -63,10 +63,13 @@ impl TestProgram {
}
}
pub fn serialize(&self) -> Vec<u8> {
self.serialize_with_types(scalar_catalog())
}
pub fn serialize_with_types(&self, types: Vec<TypeDecl>) -> Vec<u8> {
let mut budget = LoadBudget::new(Default::default());
let registry = Registry::resolve(
RegistryDeclarations {
types: scalar_catalog(),
types,
functions: self.functions.clone(),
implementations: vec![],
},