✓
Passing This code compiles and runs correctly.
Code
// Property pin: arbitrary bytes never wedge the turn segmenter.
//
// `reslib:fuzz(seed)` deterministically produces either corrupted wire
// (a template with a few positions overwritten) or raw garbage from a
// wire-hostile alphabet. `round` feeds each string to `std/bridge:run`
// and maps EVERY declared outcome to a code — the mapping is total by
// construction (an outcome this file forgot is a compile error, not a
// silent pass), so any reply the segmenter invents breaks the build.
//
// The capture folds the codes into a fingerprint. A run that crashes,
// leaks, or changes which bucket a byte string lands in moves the
// number; N completions proves every item was classified and freed.
import std/io
import std/control
import std/bridge
import std/runtime
import reslib/res
import reslib/fuzz
std/runtime:register(scope: "res") {
reslib.res:mint -> mint(1)
reslib.res:grab -> grab(1)
reslib.res:release -> release(1)
}
tor round { br: *std/bridge:Bridge<session>, seed: usize } -> i64
round = reslib/fuzz:fuzz(seed) : src |> std/bridge:run(br, source: src)
| result _ |> reslib/fuzz:code(c: 0) : v -> v
| defined _ |> reslib/fuzz:code(c: 1) : v -> v
| residue _ |> reslib/fuzz:code(c: 2) : v -> v
| partial _ |> reslib/fuzz:code(c: 3) : v -> v
| unhandled-branch _ |> reslib/fuzz:code(c: 4) : v -> v
| error runerr |> std/bridge:error-decode(e: runerr)
| exhausted _ |> reslib/fuzz:code(c: 5) : v -> v
| parse-error _ |> reslib/fuzz:code(c: 6) : v -> v
| validation-error _ |> reslib/fuzz:code(c: 7) : v -> v
| shape-error _ |> reslib/fuzz:code(c: 8) : v -> v
| event-denied _ |> reslib/fuzz:code(c: 9) : v -> v
| dispatch-error _ |> reslib/fuzz:code(c: 10) : v -> v
| scope-not-found _ |> reslib/fuzz:code(c: 11) : v -> v
| unknown-error _ |> reslib/fuzz:code(c: 12) : v -> v
[with]std/bridge:create(id: "fz", scope: "res"): br
|> capture { sum: @as(i64, 0), count: @as(i64, 0) }
! as acc |> for(0..200)
! each i |> round(br, seed: i)
| ok code |> captured { sum: acc.sum + code, count: acc.count + 1 }
| captured final |> std/io:print.ln("200 rounds: fingerprint {{ final.sum:d }} over {{ final.count:d }}") |> std/bridge:close(br)
Supporting Files
// Deterministic wire mutator for the segmenter property test. An LCG
// over `seed` either corrupts a known-good wire template or emits raw
// garbage from a wire-hostile alphabet — no OS entropy, so every seed's
// bytes are pinned and the outcome fingerprint is reproducible.
const ALPHABET = "()|>\"':,.\n\t abcdefgrstx0123456789<!-{}";
const TEMPLATES = [_][]const u8{
"mint() | ok h |>",
"release(r: r1)",
"note(text: \"thinking\")",
"say(text: \"hello\")\ngrab(r: r1)",
"this is prose, not wire",
"mint()\nrelease(r: r1)",
"grab(r: 'h:Res:1') | ok _ |>",
};
~pub tor code { c: i64 } -> i64
~proc code|zig {
return c;
}
~pub tor fuzz { seed: usize } -> string
~proc fuzz|zig {
const stdz = @import("std");
var state: u64 = @as(u64, @intCast(seed)) *% 6364136223846793005 +% 1442695040888963407;
const next = struct {
fn f(s: *u64) u64 {
s.* = s.* *% 6364136223846793005 +% 1442695040888963407;
return s.* >> 33;
}
}.f;
var buf: [512]u8 = undefined;
var n: usize = 0;
if (@mod(seed, @as(usize, 3)) == 0) {
// Template mutation: corrupt a few positions of real wire.
const tpl = TEMPLATES[@intCast(next(&state) % TEMPLATES.len)];
const copy_len = @min(tpl.len, buf.len);
@memcpy(buf[0..copy_len], tpl[0..copy_len]);
n = copy_len;
const hits: usize = @intCast(1 + next(&state) % 4);
var h: usize = 0;
while (h < hits) : (h += 1) {
buf[@intCast(next(&state) % n)] = ALPHABET[@intCast(next(&state) % ALPHABET.len)];
}
} else {
// Raw garbage, 3..43 chars.
const len: usize = @intCast(3 + @mod(seed, @as(usize, 41)));
while (n < len) : (n += 1) {
buf[n] = ALPHABET[@intCast(next(&state) % ALPHABET.len)];
}
}
return stdz.heap.page_allocator.dupe(u8, buf[0..n]) catch unreachable;
}
// A `*T` resource module for the token-dispatch pin: the wire never sees
// a pointer — mint() produces a *Res that packs as "h:Res:N", use()
// resolves the token back to the live struct, release() discharges it.
pub const Res = struct { v: u32 };
~pub tor mint {} -> *Res<opened!>
~proc mint|zig {
const r = @import("std").heap.page_allocator.create(Res) catch unreachable;
r.* = .{ .v = 42 };
return r;
}
~pub tor grab { r: *Res<opened> }
~proc grab|zig {
var buf: [32]u8 = undefined;
const s = @import("std").fmt.bufPrint(&buf, "used v={d}\n", .{r.v}) catch unreachable;
_ = @import("std").posix.write(1, s) catch {};
}
~pub tor release { r: *Res<!opened> }
~proc release|zig {
@import("std").heap.page_allocator.destroy(r);
}
Actual
200 rounds: fingerprint 564 over 200
[BRIDGE] Invoked 'release' for handle 'h:Res:65cc44' [reslib.res:opened]
[BRIDGE] Invoked 'release' for handle 'h:Res:ef1487' [reslib.res:opened]
[BRIDGE] Invoked 'release' for handle 'h:Res:8653b5' [reslib.res:opened]
[BRIDGE] Invoked 'release' for handle 'h:Res:b09233' [reslib.res:opened]
Expected patterns
Each line is a regex that must match the program output.
^200 rounds: fingerprint 564 over 200$
\[BRIDGE\] Invoked 'release' for handle 'h:Res:[0-9a-f]{6}' \[reslib\.res:opened\]Flows
flow ~register click a branch to expand · @labels scroll to their anchor
register (scope: "res", source: reslib.res:mint -> mint(1)
reslib.res:grab -> grab(1)
reslib.res:release -> release(1))
subflow ~round click a branch to expand · @labels scroll to their anchor
fuzz (seed)
flow ~create click a branch to expand · @labels scroll to their anchor
create (id: "fz", scope: "res")
Test Configuration
MUST_RUN
koru.json:
{
"paths": {
"std": "../../../../../koru_std",
"app": "{{ ENTRY }}",
"reslib": "{{ ENTRY }}/reslib"
}
}Post-validation Script:
#!/bin/bash
# Strict shape: line 1 is the exact fingerprint; every remaining line is a
# hang-up sweep invoking release on a still-held seeded-hex handle.
set -e
head -1 actual.txt | grep -qx '200 rounds: fingerprint 564 over 200'
bad=$(tail -n +2 actual.txt | grep -vxE "\[BRIDGE\] Invoked 'release' for handle 'h:Res:[0-9a-f]{6}' \[reslib\.res:opened\]" | head -1)
if [ -n "$bad" ]; then echo "unexpected line: $bad"; exit 1; fi
[ "$(tail -n +2 actual.txt | wc -l | tr -d ' ')" -eq 4 ]