feat(vm)!: implement typed callables and single-context execution
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

Add structural Fn signatures, typed capture environments, indirect calls and Trait dispatch while preserving logical parameter and result groups, ownership and budgets.

Remove guest coroutine execution and legacy loading. Activate bounded sealed PBX v1 with mandatory capture metadata, preserve frame continuations and root return results, and migrate producers and shipped cartridges.

Publish coordinated specs, handoffs and the completed runtime lesson. Validation: 880 workspace tests passed, 9 existing tests ignored; both cartridges completed six production-host frames.
This commit is contained in:
2026-10-09 05:47:24 +01:00
parent 86bade3e1f
commit ec84a6bba5
96 changed files with 3656 additions and 6075 deletions
@@ -9,7 +9,7 @@ fn emit(op: CoreOpCode, imm: Option<&[u8]>, out: &mut Vec<u8>) {
}
#[test]
fn roundtrip_disasm_assemble_byte_equal_with_closures_and_coroutines() {
fn roundtrip_disasm_assemble_byte_equal_with_typed_callables() {
// Program: PUSH_I32 7; MAKE_CLOSURE fn=1,captures=0; CALL_CLOSURE argc=1;
// SPAWN fn=2,argc=1; YIELD; SLEEP 3; HOSTCALL 2; SYSCALL 0x1003; FRAME_SYNC; HALT
let mut prog = Vec::new();
@@ -22,15 +22,9 @@ fn roundtrip_disasm_assemble_byte_equal_with_closures_and_coroutines() {
emit(CoreOpCode::MakeClosure, Some(&mc), &mut prog);
// CALL_CLOSURE argc=1
emit(CoreOpCode::CallClosure, Some(&1u32.to_le_bytes()), &mut prog);
// SPAWN (fn=2, argc=1)
let mut sp = [0u8; 8];
sp[0..4].copy_from_slice(&2u32.to_le_bytes());
sp[4..8].copy_from_slice(&1u32.to_le_bytes());
emit(CoreOpCode::Spawn, Some(&sp), &mut prog);
// YIELD
emit(CoreOpCode::Yield, None, &mut prog);
// SLEEP 3
emit(CoreOpCode::Sleep, Some(&3u32.to_le_bytes()), &mut prog);
emit(CoreOpCode::GetCapture, Some(&0u32.to_le_bytes()), &mut prog);
let trait_call = [7u32.to_le_bytes(), 0u32.to_le_bytes()].concat();
emit(CoreOpCode::CallTrait, Some(&trait_call), &mut prog);
// HOSTCALL sysc[2]
emit(CoreOpCode::Hostcall, Some(&2u32.to_le_bytes()), &mut prog);
// SYSCALL gfx2d.draw_line (0x1003)
@@ -17,7 +17,7 @@ fn snapshot_representative_program_is_stable() {
mc[0..4].copy_from_slice(&1u32.to_le_bytes());
mc[4..8].copy_from_slice(&0u32.to_le_bytes());
emit(CoreOpCode::MakeClosure, Some(&mc), &mut prog);
// CALL_CLOSURE argc=1
// CALL_CLOSURE 1
emit(CoreOpCode::CallClosure, Some(&1u32.to_le_bytes()), &mut prog);
// SPAWN (fn=2, argc=1)
let mut sp = [0u8; 8];
@@ -38,6 +38,6 @@ fn snapshot_representative_program_is_stable() {
emit(CoreOpCode::Halt, None, &mut prog);
let text = disassemble(&prog).expect("disasm ok");
let expected = "PUSH_I32 7\nMAKE_CLOSURE fn=1, captures=0\nCALL_CLOSURE argc=1\nSPAWN fn=2, argc=1\nYIELD\nSLEEP 3\nHOSTCALL 2\nSYSCALL 0x1003\nFRAME_SYNC\nHALT";
let expected = "PUSH_I32 7\nMAKE_CLOSURE fn=1, type=0\nCALL_CLOSURE 1\nSPAWN fn=2, argc=1\nYIELD\nSLEEP 3\nHOSTCALL 2\nSYSCALL 0x1003\nFRAME_SYNC\nHALT";
assert_eq!(text, expected);
}
@@ -7,6 +7,116 @@ use prometeu_bytecode::registry_codec::*;
fn budget() -> LoadBudget {
LoadBudget::new(LoadLimits::default())
}
#[test]
fn fn_keys_ignore_names_but_preserve_logical_parameter_and_result_order() {
let signature = Signature { parameters: vec![named(0)], results: vec![TypeId(1)] };
let mut renamed = signature.clone();
renamed.parameters[0].name = Some("renamed".into());
let d = RegistryDeclarations {
types: vec![
builtin(BuiltinId::INT32),
builtin(BuiltinId::INT64),
TypeDecl { name: Some("a".into()), kind: TypeKind::Fn(signature.clone()) },
TypeDecl { name: Some("b".into()), kind: TypeKind::Fn(renamed) },
],
..Default::default()
};
let e = resolve(d).unwrap_err();
assert_eq!((e.code, e.reason), (LoadCode::InvalidRegistry, LoadReason::DuplicateFn));
let r = resolve(RegistryDeclarations {
types: vec![
builtin(BuiltinId::INT32),
builtin(BuiltinId::INT64),
TypeDecl { name: None, kind: TypeKind::Fn(signature) },
TypeDecl {
name: None,
kind: TypeKind::Fn(Signature {
parameters: vec![named(1)],
results: vec![TypeId(0)],
}),
},
],
..Default::default()
})
.unwrap();
let layout = r.type_layout(TypeId(2)).unwrap();
assert_eq!(layout.storage.slots, 1);
assert_eq!(layout.storage.size, 8);
assert_eq!(layout.storage.alignment, 8);
assert_eq!(layout.storage.default, DefaultKind::Null);
}
#[test]
fn fn_descriptor_bytes_are_independent_of_execution_width_caches() {
let r = resolve(RegistryDeclarations {
types: vec![
builtin(BuiltinId::INT32),
builtin(BuiltinId::VEC2),
TypeDecl {
name: None,
kind: TypeKind::Fn(Signature {
parameters: vec![named(1)],
results: vec![TypeId(0)],
}),
},
],
..Default::default()
})
.unwrap();
let sections = encode_registry(&r, &mut budget()).unwrap();
assert_eq!(sections.types,hex("03 00 00 00 00 00 10 00 00 00 00 00 02 00 00 00 04 00 01 00 00 00 01 00 00 00 00 01 00 00 00 00 00 00 00"));
let callable = r.type_layout(TypeId(2)).unwrap().callable.as_ref().unwrap();
assert_eq!((callable.parameter_slots, callable.result_slots), (2, 1));
}
#[test]
fn fn_array_structural_cycles_reject_and_nominal_anchors_terminate() {
let mut types = vec![
TypeDecl {
name: None,
kind: TypeKind::Fn(Signature { parameters: vec![named(1)], results: vec![] }),
},
TypeDecl { name: None, kind: TypeKind::Array(TypeId(0)) },
];
assert_eq!(
resolve(RegistryDeclarations { types: types.clone(), ..Default::default() })
.unwrap_err()
.reason,
LoadReason::StructuralTypeCycle
);
types[1].kind = TypeKind::Struct(vec![named(0)]);
resolve(RegistryDeclarations { types, ..Default::default() }).unwrap();
}
#[test]
fn captures_section_has_independent_bytes_and_strict_shape() {
let r = resolve(RegistryDeclarations {
types: vec![builtin(BuiltinId::INT32), builtin(BuiltinId::VEC2)],
functions: vec![FunctionDecl {
code_len: 2,
captures: vec![TypeId(1), TypeId(0)],
..Default::default()
}],
..Default::default()
})
.unwrap();
let sections = encode_registry(&r, &mut budget()).unwrap();
assert_eq!(sections.captures, hex("01 00 00 00 02 00 00 00 01 00 00 00 00 00 00 00"));
let layout = r.function_layout(FunctionId(0)).unwrap();
assert_eq!(layout.capture_slots, 3);
assert_eq!(layout.captures[1].slot_offset, 2);
for (payload, reason) in [
(hex("00 00 00 00"), LoadReason::CaptureCount),
(hex("01 00 00 00 01 00 00 00"), LoadReason::Truncated),
(hex("01 00 00 00 00 00 00 00 00"), LoadReason::TrailingBytes),
(hex("01 00 00 00 01 00 00 00 ff ff ff ff"), LoadReason::TypeId),
] {
let e = OwnedPbx::parse(&with_section(8, &payload), &mut budget()).unwrap_err();
assert_eq!(e.reason, reason);
assert_eq!(e.section, Some(8));
}
}
fn named(id: u32) -> NamedType {
NamedType { type_id: TypeId(id), name: None }
}
@@ -21,20 +131,21 @@ fn hex(s: &str) -> Vec<u8> {
}
// Independently specified PBX, not generated by any production serializer.
// Header 32 bytes, seven directory entries (84 bytes), payload starts at 116.
// constants=116/4, functions=120/29, code=149/2, exports=151/4,
// syscalls=155/4, types=159/10, implementations=169/4. Total=173.
// Header 32 bytes, eight directory entries (96 bytes), payload starts at 128.
// constants=128/4, functions=132/29, code=161/2, exports=163/4,
// syscalls=167/4, types=171/10, implementations=181/4. Total=193.
fn minimal_bytes() -> Vec<u8> {
hex("
50 42 58 00 01 00 00 00 07 00 00 00
50 42 58 00 01 00 00 00 08 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 74 00 00 00 04 00 00 00
01 00 00 00 78 00 00 00 1d 00 00 00
02 00 00 00 95 00 00 00 02 00 00 00
04 00 00 00 97 00 00 00 04 00 00 00
05 00 00 00 9b 00 00 00 04 00 00 00
06 00 00 00 9f 00 00 00 0a 00 00 00
07 00 00 00 a9 00 00 00 04 00 00 00
00 00 00 00 80 00 00 00 04 00 00 00
01 00 00 00 84 00 00 00 1d 00 00 00
02 00 00 00 a1 00 00 00 02 00 00 00
04 00 00 00 a3 00 00 00 04 00 00 00
05 00 00 00 a7 00 00 00 04 00 00 00
06 00 00 00 ab 00 00 00 0a 00 00 00
07 00 00 00 b5 00 00 00 04 00 00 00
08 00 00 00 b9 00 00 00 08 00 00 00
00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00
@@ -42,7 +153,8 @@ fn minimal_bytes() -> Vec<u8> {
00 00 00 00
00 00 00 00
01 00 00 00 00 00 13 00 00 00
00 00 00 00")
00 00 00 00
01 00 00 00 00 00 00 00")
}
fn minimal_registry() -> Registry {
resolve(RegistryDeclarations {
@@ -55,6 +167,9 @@ fn minimal_registry() -> Registry {
fn envelope(types: &[u8], functions: &[u8], implementations: &[u8]) -> Vec<u8> {
let empty = [0; 4];
let code = [1, 0];
let n = u32::from_le_bytes(functions[..4].try_into().unwrap()) as usize;
let mut captures = vec![0; 4 + 4 * n];
captures[..4].copy_from_slice(&(n as u32).to_le_bytes());
encode_envelope(
&[
SectionData { kind: 0, bytes: &empty },
@@ -64,6 +179,7 @@ fn envelope(types: &[u8], functions: &[u8], implementations: &[u8]) -> Vec<u8> {
SectionData { kind: 5, bytes: &empty },
SectionData { kind: 6, bytes: types },
SectionData { kind: 7, bytes: implementations },
SectionData { kind: 8, bytes: &captures },
],
&mut budget(),
)
@@ -74,10 +190,10 @@ fn writer_matches_independent_minimal_envelope() {
let sections = encode_registry(&minimal_registry(), &mut budget()).unwrap();
let bytes = envelope(&sections.types, &sections.functions, &sections.implementations);
assert_eq!(bytes, minimal_bytes());
assert_eq!(bytes.len(), 173);
assert_eq!(bytes.len(), 193);
let mut b = budget();
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
assert_eq!(env.section(2).unwrap().offset, 149);
assert_eq!(env.section(2).unwrap().offset, 161);
let registry = env.resolve_registry(&mut b).unwrap();
assert_eq!(registry.declarations(), minimal_registry().declarations());
assert_eq!(b.used(LoadResource::TemporaryBytes), 0);
@@ -384,10 +500,10 @@ fn envelope_mutations_have_typed_reasons() {
(6, 1, LoadCode::InvalidFormat, LoadReason::Endianness),
(7, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
(31, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
(32, 8, LoadCode::UnsupportedFormat, LoadReason::SectionKind),
(32, 9, LoadCode::UnsupportedFormat, LoadReason::SectionKind),
(44, 0, LoadCode::InvalidFormat, LoadReason::DuplicateSection),
(36, 0, LoadCode::InvalidFormat, LoadReason::Overlap),
(48, 116, LoadCode::InvalidFormat, LoadReason::Overlap),
(48, 128, LoadCode::InvalidFormat, LoadReason::Overlap),
] {
let mut bytes = minimal_bytes();
bytes[offset] = value;
@@ -402,6 +518,16 @@ fn mandatory_empty_sections_are_required() {
let err = PbxEnvelope::parse(&bytes, &mut budget()).unwrap_err();
assert_eq!(err.reason, LoadReason::MissingSection);
assert_eq!(err.section, Some(0));
let mut bytes = minimal_bytes();
bytes[116] = 3; // captures replaced by optional debug
let err = PbxEnvelope::parse(&bytes, &mut budget()).unwrap_err();
assert_eq!(err.reason, LoadReason::MissingSection);
assert_eq!(err.section, Some(8));
let mut bytes = minimal_bytes();
bytes[104] = 8; // duplicate captures directory entry
let err = PbxEnvelope::parse(&bytes, &mut budget()).unwrap_err();
assert_eq!(err.reason, LoadReason::DuplicateSection);
assert_eq!(err.section, Some(8));
}
#[test]
fn record_flags_utf8_trailing_bytes_and_unknown_categories_fail() {
@@ -412,7 +538,7 @@ fn record_flags_utf8_trailing_bytes_and_unknown_categories_fail() {
let implementations = env.section(7).unwrap().bytes;
for (types, reason) in [
(hex("01 00 00 00 00 02 13 00 00 00"), LoadReason::InvalidFlag),
(hex("01 00 00 00 04 00 13 00 00 00"), LoadReason::Category),
(hex("01 00 00 00 05 00 13 00 00 00"), LoadReason::Category),
(hex("01 00 00 00 00 01 01 00 00 00 ff 13 00 00 00"), LoadReason::Utf8),
(hex("01 00 00 00 00 00 13 00 00 00 ff"), LoadReason::TrailingBytes),
(hex("01 00 00 00 00 00 00 01 00 00"), LoadReason::Builtin),
@@ -577,7 +703,12 @@ fn multi_slot_signatures_derive_counts_and_validate_exact_trait_pair() {
let r = resolve(implemented()).unwrap();
assert_eq!(
r.function_layout(FunctionId(0)),
Some(&FunctionLayout { parameter_slots: 3, result_slots: 2 })
Some(&FunctionLayout {
parameter_slots: 3,
result_slots: 2,
capture_slots: 0,
captures: vec![]
})
);
let mut d = implemented();
d.functions[0].signature.parameters.swap(0, 1);
@@ -655,7 +786,7 @@ fn with_section(kind: u32, payload: &[u8]) -> Vec<u8> {
let mut b = budget();
let env = PbxEnvelope::parse(&bytes, &mut b).unwrap();
let mut sections = Vec::new();
for k in 0..8 {
for k in 0..9 {
if k == kind {
sections.push(SectionData { kind: k, bytes: payload });
} else if let Some(section) = env.section(k) {
@@ -870,6 +1001,7 @@ fn independent_multi_slot_signature_fixture_has_no_serialized_widths() {
SectionData { kind: 5, bytes: &empty },
SectionData { kind: 6, bytes: &types },
SectionData { kind: 7, bytes: &empty },
SectionData { kind: 8, bytes: &[1, 0, 0, 0, 0, 0, 0, 0] },
],
&mut budget(),
)
@@ -877,7 +1009,12 @@ fn independent_multi_slot_signature_fixture_has_no_serialized_widths() {
let parsed = ParsedPbx::parse(&bytes, &mut budget()).unwrap();
assert_eq!(
parsed.registry().function_layout(FunctionId(0)),
Some(&FunctionLayout { parameter_slots: 2, result_slots: 2 })
Some(&FunctionLayout {
parameter_slots: 2,
result_slots: 2,
capture_slots: 0,
captures: vec![]
})
);
let sections = encode_registry(parsed.registry(), &mut budget()).unwrap();
assert_eq!(sections.functions, functions);