feat(pbs): integrate explicit runtime Random handles
This commit is contained in:
@@ -160,3 +160,39 @@ every run. Optional `CARGO_TARGET_DIR` can retain build artifacts outside the
|
||||
runtime checkout. Each invocation prints runtime revision, platform, profile,
|
||||
features, lockfile and provider catalog/oracle hashes. Local evidence qualifies
|
||||
macOS/arm64 only; macOS/x86_64 and Linux/x86_64 remain pending.
|
||||
|
||||
## PBS explicit Random integration
|
||||
|
||||
`PbsRuntimeRandomTest` adds 226 source fixtures plus one real SDK import/alias
|
||||
fixture. The 48 unchanged provider oracle rows produce 192 exact integer/Float
|
||||
cases (six seeds, eight draws, two output kinds and two optimizer variants).
|
||||
The remaining paired fixtures cover mixed/discarded draws, independent objects,
|
||||
locals/globals/fields/arrays/parameters/results/captures, named/positional effects,
|
||||
unit/extreme ranges, invalid limits, null and typed-null Dynamic storage, real
|
||||
rejection followed by acceptance, and collection roots.
|
||||
|
||||
Each paired fixture runs with single-instruction budgets and twice with full
|
||||
budgets, comparing exact result bits or faults, intrinsic charges and total
|
||||
cycles. Explicit source `as long` conversions are charged separately. The GC
|
||||
fixture reuses the existing test-only FRAME_SYNC marker hook; all allocations,
|
||||
roots and post-collection draws are compiled PBS. At least 1,100 discarded Random
|
||||
objects are reclaimed while array, field and closure references preserve the
|
||||
same generator. The import fixture uses the production workspace builder and
|
||||
lifecycle assembler. The focused driver calls file initializers before Probe.main;
|
||||
it does not claim a new lifecycle API.
|
||||
|
||||
```sh
|
||||
./gradlew :prometeu-architecture-tests:test --tests '*PbsRuntimeRandomTest'
|
||||
python3 tools/pbx-conformance/run.py --runtime ../runtime --random-only
|
||||
python3 tools/pbx-conformance/run.py --runtime ../runtime --random-only --release
|
||||
```
|
||||
|
||||
The driver rejects an oracle copy that differs from the selected provider and
|
||||
prints source/oracle/catalog/lockfile fingerprints and toolchain/profile details.
|
||||
A complete run includes the Random corpus and keeps it separate from the older
|
||||
114 input/type fixtures. Rare synthetic 64-attempt acceptance/cap/rollback,
|
||||
malformed carriers and native provenance are provider tests, not PBS-produced
|
||||
claims. Qualify those separately with the provider's `random_contract` suite and
|
||||
`cargo test -p prometeu-vm --lib random` in debug/release. No alternate RNG or
|
||||
public injection hook is used. Installed knowledge validation remains a separate
|
||||
gate for packaged SDK/spec/example resources.
|
||||
|
||||
@@ -36,8 +36,10 @@ impl NativeInterface for NoNative {
|
||||
|
||||
fn main() {
|
||||
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory"));
|
||||
if std::env::args().any(|arg| arg == "--random-only") { verify_pbs_random_fixtures(&root); return; }
|
||||
verify_pbs_math_fixtures(&root);
|
||||
if std::env::args().any(|arg| arg == "--math-only") { return; }
|
||||
verify_pbs_random_fixtures(&root);
|
||||
verify_catalog_fixtures(&root);
|
||||
verify_pbs_fixtures(&root);
|
||||
verify_pbs_structured_fixtures(&root);
|
||||
@@ -477,7 +479,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
|
||||
for entry in std::fs::read_dir(root).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
let name = path.file_stem().unwrap().to_str().unwrap();
|
||||
if name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
count += 1;
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
||||
@@ -726,3 +728,114 @@ fn verify_pbs_math_fixtures(root: &std::path::Path) {
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "import: {report:?}");
|
||||
println!("PBS MATH PASS: {count} exact bits/faults/cycle vectors; all 26 signatures; service constants");
|
||||
}
|
||||
|
||||
|
||||
fn verify_pbs_random_fixtures(root: &std::path::Path) {
|
||||
use prometeu_vm::LogicalFrameEndingReason as End;
|
||||
use prometeu_bytecode::isa::core::CoreOpCode as OpCode;
|
||||
let corpus = std::fs::read_to_string(root.join("random-vectors.tsv")).expect("mandatory random seed corpus");
|
||||
let mut cases: Vec<(String, Vec<String>, Vec<u64>, Option<u32>)> = Vec::new();
|
||||
let mut names = std::collections::BTreeSet::new();
|
||||
for row in corpus.lines() {
|
||||
let p: Vec<_> = row.split_whitespace().collect();
|
||||
assert!(names.insert(p[0]), "duplicate random fixture");
|
||||
let draw: usize = p[2].parse().unwrap();
|
||||
assert!((1..=8).contains(&draw));
|
||||
let mut costs = vec![32];
|
||||
costs.extend(std::iter::repeat_n(8, draw-1));
|
||||
costs.push(if p[1].starts_with("f:") { 12 } else { 8 });
|
||||
cases.push((p[0].to_string(), vec![p[1].to_string()], costs, None));
|
||||
}
|
||||
assert_eq!(cases.len(), 192, "six seeds, eight draws, two outputs, two optimizer variants required");
|
||||
let words = [0x86B1DA1D72062B68u64, 0x1304AA46C9853D39, 0xA3670E9E0DD50358, 0xF9090E529A7DAE00];
|
||||
let long = |word: u64| format!("l:{}",word as i64);
|
||||
let mut add = |name: &str, expected: Vec<String>, costs: Vec<u64>, fault: Option<u32>| {
|
||||
for suffix in ["", "-optimized"] {
|
||||
cases.push((format!("pbs-random-{name}{suffix}"), expected.clone(), costs.clone(), fault));
|
||||
}
|
||||
};
|
||||
add("mixed", vec![long(words[3]), "l:-7".into(), "f:3fb304aa46c98538".into()], vec![32,8,12,12,8], None);
|
||||
add("storage", vec![long(words[0]),long(words[3]),long(words[2]),long(words[1]),long(words[0])], vec![32,8,8,8,8,32,8], None);
|
||||
for (name, trace) in [("named",312),("positional",123)] {
|
||||
add(name,vec![long(words[3]),"l:-7".into(),format!("i:{trace}")],vec![32,8,8,12,8],None);
|
||||
}
|
||||
let bounds = [(0,1),(-10,-3),(-10,10),(0,16),(i64::MIN,0),(-1,i64::MAX),(i64::MIN,i64::MAX)];
|
||||
for (index,(min,max)) in bounds.into_iter().enumerate() {
|
||||
let span = (i128::from(max)-i128::from(min)) as u64;
|
||||
// Independent provider fixture word, accepted for each of these spans.
|
||||
let expected = i128::from(min)+i128::from(words[0]%span);
|
||||
assert!(expected>=i128::from(min) && expected<i128::from(max));
|
||||
add(&format!("range-{index}"),vec![format!("l:{expected}")],vec![32,12],None);
|
||||
}
|
||||
for index in [7,8] { add(&format!("range-{index}"),vec![],vec![32,4],Some(prometeu_bytecode::TRAP_NUMERIC_DOMAIN)); }
|
||||
let rejected_then_accepted = i128::from(i64::MIN)+i128::from(words[2]%((1u64<<63)+1));
|
||||
add("rejection",vec![long(words[3]),format!("l:{rejected_then_accepted}")],vec![32,8,20,8],None);
|
||||
for name in ["null","typed-null"] { add(name,vec![],vec![8],Some(prometeu_bytecode::TRAP_NULL_REFERENCE)); }
|
||||
let mut gc_costs=vec![32;1101]; gc_costs.extend([8,8,8]);
|
||||
add("gc",vec![long(words[2]),long(words[1]),long(words[0])],gc_costs,None);
|
||||
let mut cycles_by_name = std::collections::BTreeMap::new();
|
||||
for (name,expected,costs,fault) in cases {
|
||||
let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap_or_else(|e|panic!("{name}: {e}"));
|
||||
let mut reference = None;
|
||||
for budget in [1,100000,100000] {
|
||||
let image = ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE)
|
||||
.unwrap_or_else(|e|panic!("{name}: {e:?}"));
|
||||
let code = image.code().to_vec();
|
||||
let mut vm = VirtualMachine::from_image(image,Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let mut total=0;
|
||||
let mut calls=0;
|
||||
let ending = loop {
|
||||
let pc=vm.pc();
|
||||
let opcode=OpCode::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
|
||||
let report=vm.run_budget(budget,&mut NoNative,&mut HostContext::new(None)).unwrap();
|
||||
if budget==1 && opcode==OpCode::Intrinsic {
|
||||
let id=u32::from_le_bytes(code[pc+2..pc+6].try_into().unwrap());
|
||||
if id == 0x3014 {
|
||||
// Explicit source `as long` conversion; not a Random call.
|
||||
assert_eq!(report.cycles_used,2,"{name}: explicit Int32 to Int64");
|
||||
} else {
|
||||
assert!((0x4000..=0x4003).contains(&id),"{name}: wrong intrinsic {id:x}");
|
||||
assert_eq!(report.cycles_used,costs[calls],"{name}: call {calls} id={id:x}");
|
||||
calls+=1;
|
||||
}
|
||||
}
|
||||
total+=report.cycles_used;
|
||||
if matches!(report.reason,End::Halted|End::EndOfRom|End::Trap(_)) { break report.reason; }
|
||||
if matches!(report.reason,End::FrameSync) {
|
||||
assert!(name.starts_with("pbs-random-gc"));
|
||||
let snapshot=vm.resource_snapshot();
|
||||
assert_eq!(snapshot.gc.collections,1);
|
||||
assert!(snapshot.gc.reclaimed>=1100,"{name}: {:?}",snapshot.gc);
|
||||
}
|
||||
assert!(total<1000000,"{name}: nontermination");
|
||||
};
|
||||
if budget==1 { assert_eq!(calls,costs.len(),"{name}: missing or extra calls"); }
|
||||
if let Some(fault)=fault {
|
||||
assert!(matches!(ending,End::Trap(ref t) if t.code==fault),"{name}: {ending:?}");
|
||||
} else {
|
||||
assert!(matches!(ending,End::Halted|End::EndOfRom),"{name}: {ending:?}");
|
||||
let actual:Vec<_>=vm.operand_stack_top(expected.len()).iter().map(|v|match v {
|
||||
Value::Int64(v)=>format!("l:{v}"),Value::Int32(v)=>format!("i:{v}"),
|
||||
Value::Float(v)=>format!("f:{:016x}",v.to_bits()),v=>panic!("{name}: {v:?}")
|
||||
}).collect();
|
||||
assert_eq!(actual,expected,"{name}: output bits/replay");
|
||||
}
|
||||
if let Some(reference)=reference { assert_eq!(total,reference,"{name}: replay/budget cycles"); }
|
||||
reference=Some(total);
|
||||
}
|
||||
let total=reference.unwrap();
|
||||
if let Some(original)=name.strip_suffix("-optimized") {
|
||||
assert_eq!(cycles_by_name.get(original),Some(&total),"{name}: optimizer cycles");
|
||||
} else { cycles_by_name.insert(name.clone(),total); }
|
||||
println!("RANDOM PASS {name}: {total} cycles, exact bits/fault and replay");
|
||||
}
|
||||
let bytes=std::fs::read(root.join("pbs-random-import.pbx")).expect("mandatory real Random SDK import");
|
||||
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
|
||||
let mut vm=VirtualMachine::from_image(image,Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report=vm.run_budget(10000,&mut NoNative,&mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason,End::Halted|End::EndOfRom),"Random import: {report:?}");
|
||||
println!("RANDOM PASS real SDK imports and aliases");
|
||||
|
||||
}
|
||||
|
||||
@@ -13,12 +13,23 @@ parser.add_argument("--fixtures", type=pathlib.Path, default=pathlib.Path("prome
|
||||
parser.add_argument("--online", action="store_true", help="Allow Cargo to fetch uncached dependencies")
|
||||
parser.add_argument("--release", action="store_true", help="Execute an optimized runtime harness")
|
||||
parser.add_argument("--math-only", action="store_true", help="Run the complete PBS math corpus only")
|
||||
parser.add_argument("--random-only", action="store_true", help="Run the complete PBS Random corpus only")
|
||||
args = parser.parse_args()
|
||||
runtime = args.runtime.resolve()
|
||||
if not args.math_only:
|
||||
provider_vectors = runtime / "crates/console/prometeu-vm/tests/fixtures/random-conformance.tsv"
|
||||
consumer_vectors = pathlib.Path("prometeu-architecture-tests/src/test/resources/random/random-conformance.tsv")
|
||||
if provider_vectors.read_bytes() != consumer_vectors.read_bytes():
|
||||
raise SystemExit("Random oracle differs from the selected provider; update provenance and requalify")
|
||||
print(subprocess.check_output(["rustc", "-Vv"], text=True).strip(), flush=True)
|
||||
for source in [pathlib.Path(__file__), pathlib.Path(__file__).with_name("main.rs"),
|
||||
pathlib.Path("prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsRuntimeRandomTest.java"),
|
||||
pathlib.Path("prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/random/main.pbs")]:
|
||||
print("sha256", source, hashlib.sha256(source.read_bytes()).hexdigest(), flush=True)
|
||||
print("runtime", subprocess.check_output(["git", "-C", str(runtime), "rev-parse", "HEAD"], text=True).strip(), flush=True)
|
||||
print("platform", platform.platform(), "limits=default capabilities=NONE", flush=True)
|
||||
print("profile", "release" if args.release else "dev", "features=resource-diagnostics", flush=True)
|
||||
for relative in ["Cargo.lock", "docs/vm-arch/INTRINSICS.csv", "docs/vm-arch/BUILTIN_CONSTANTS.csv", "crates/console/prometeu-vm/tests/fixtures/numeric-conformance.tsv"]:
|
||||
for relative in ["Cargo.lock", "docs/vm-arch/INTRINSICS.csv", "docs/vm-arch/BUILTIN_CONSTANTS.csv", "crates/console/prometeu-vm/tests/fixtures/numeric-conformance.tsv", "crates/console/prometeu-vm/tests/fixtures/random-conformance.tsv"]:
|
||||
print("sha256", relative, hashlib.sha256((runtime / relative).read_bytes()).hexdigest(), flush=True)
|
||||
with tempfile.TemporaryDirectory(prefix="pbx-conformance-") as directory:
|
||||
root = pathlib.Path(directory)
|
||||
@@ -31,4 +42,4 @@ with tempfile.TemporaryDirectory(prefix="pbx-conformance-") as directory:
|
||||
(root / "Cargo.lock").write_text((runtime / "Cargo.lock").read_text())
|
||||
(root / "src").mkdir()
|
||||
(root / "src/main.rs").write_text(pathlib.Path(__file__).with_name("main.rs").read_text())
|
||||
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else [])], check=True)
|
||||
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else []), *(["--random-only"] if args.random_only else [])], check=True)
|
||||
|
||||
Reference in New Issue
Block a user