✓
Passing This code compiles and runs correctly.
Code
// The World in Koru — entry 8, part two.
// Doorway: celld's output gate — the reason its Durable Objects claim RPO=0.
// (github.com/denoland/celld, Rust.)
//
// Over there, a handler that commits to SQLite does NOT get to answer. The
// commit emits `Event::Wrote { request, position }`, the core files a
// `GatedWrite` and emits `Effect::AwaitDurable`, and the response sits in
// `gated_writes` until the replicator proves a position in the bucket. Only
// then does `durable_reached` compare — and it acknowledges *only* when
// `durable >= gate.position` (`logic/lib.rs:3711`). A shorter proof, from a
// lagging or lying replicator, FAILS the write rather than acknowledging one
// the node could not actually restore. Acknowledgement and durability are made
// one act, so losing a node cannot lose an acked write.
//
// The discipline is: hold the response, and never let it out on your own
// judgement. That is a rule about a value's lifetime, which is what Koru's
// obligations are.
//
// Here `cell.write` mints `<pending!>` — an UNANSWERED RESPONSE — and the only
// consumers of `<!pending>` are `gate.ack` and `gate.fail`. `gate.ack`
// additionally consumes `<!durable>`, a proof minted only by `replica.sync`,
// so one proof covers one gated write exactly as one `await_durable` ticket
// does. An answer cannot be invented (843) and a request cannot be quietly
// dropped (844).
//
// LANGUAGE CORNER: the same mechanism, read by ARITY OF DISCHARGE. A lease has
// exactly one way back (`cas.release`), so auto-discharge inserts it and you
// cannot leak a lease — that is 838, and you can see it fire on the last line
// of this trace. A response has TWO ways out, so auto-discharge refuses to
// choose and the program must put every request's fate in writing. celld
// reaches the same place by hand: its fence explicitly drains every gated write
// to a FAILED response (`logic/lib.rs:3838`), because silently dropping and
// silently acking are both lies. Here neither can be the default.
import std/io
import std/store
// The owner record from 838, plus the cell's two positions. celld takes its
// commit position from SQLite's `total_changes`; `synced` is what the
// replicator has actually put in the bucket.
std/store:new(own, capacity: 1) { epoch: 0[i64], etag: 0[i64] }
std/store:new(st, capacity: 1) { pos: 0[i64], synced: 0[i64] }
pub tor cas.claim { node: string, guard: i64 }
| applied string<lease!>
| rejected
cas.claim = if(guard == own.etag)
| then |> std/store:stored { own.etag: own.etag + 1, own.epoch: own.epoch + 1 } => applied node
| else => rejected
pub tor cas.release { lease: string<!lease> }
cas.release = std/io:print.ln(" release {{ lease:s }}")
// A commit advances the position and mints the unanswered response. celld's
// `wrote()` refuses the gate outright when the request is not resident at the
// epoch that committed: durability cannot be proven for it, so it fails rather
// than falsely acknowledging.
pub tor cell.write { lease: string<lease>, data: string }
| committed string<pending!>
| not-resident
cell.write = if(own.epoch > 0)
| then |> std/store:stored { st.pos: st.pos + 1 } => committed data
| else => not-resident
// The replicator proves only what it has actually put in the bucket, so a
// demand to cover a position past the commit point comes back `behind`.
pub tor replica.sync { lease: string<lease>, upto: i64 }
| reached i64<durable!>
| behind
replica.sync = if(upto <= st.pos)
| then |> std/store:stored { st.synced: upto } => reached upto
| else => behind
// THE GATE. An acknowledgement consumes the response AND the proof.
pub tor gate.ack { r: string<!pending>, proof: i64<!durable> }
gate.ack = std/io:print.ln(" ack {{ r:s }} — durable through {{ proof:d }}")
// The other way out. It needs no proof, because it acknowledges nothing.
pub tor gate.fail { r: string<!pending>, why: string }
gate.fail = std/io:print.ln(" fail {{ r:s }} — {{ why:s }}")
// One request, end to end. `ahead` is how far past the commit the caller
// insists the proof must reach — 0 is an honest replicator, 5 is a demand it
// cannot meet.
pub tor handle { lease: string<lease>, data: string, ahead: i64 }
handle = cell.write(lease, data)
| committed r |> std/io:print.ln(" held {{ data:s }} at pos {{ st.pos:d }} — response gated") |> replica.sync(lease, upto: st.pos + ahead)
| reached p |> gate.ack(r, proof: p)
| behind |> gate.fail(r, why: "durability unproven")
| not-resident |> std/io:print.ln(" not resident at commit epoch")
std/io:print.ln("node-a claims the cell")
cas.claim(node: "node-a", guard: 0)
| applied l |> handle(lease: l, data: "msg-1", ahead: 0) |> handle(lease: l, data: "msg-2", ahead: 5)
| rejected |> std/io:print.ln(" rejected")
Actual
node-a claims the cell
held msg-1 at pos 1 — response gated
ack msg-1 — durable through 1
held msg-2 at pos 2 — response gated
fail msg-2 — durability unproven
release node-a
Expected output
✓ Zig✓ C#node-a claims the cell
held msg-1 at pos 1 — response gated
ack msg-1 — durable through 1
held msg-2 at pos 2 — response gated
fail msg-2 — durability unproven
release node-a
Emitted Zig source
// Access compiler flags via the per-user compiler_env module
const CompilerEnv = @import("compiler_env").CompilerEnv;
pub const panic = if (@import("builtin").mode == .Debug)
@import("std").debug.FullPanic(@import("std").debug.defaultPanic)
else
@import("std").debug.simple_panic;
const __koru_bare = struct {
extern fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
extern fn free(ptr: ?*anyopaque) void;
fn bareAlloc(_: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, _: usize) ?[*]u8 {
var p: ?*anyopaque = null;
const a = @max(alignment.toByteUnits(), @sizeOf(usize));
if (posix_memalign(&p, a, len) != 0) return null;
return @ptrCast(p);
}
fn bareResize(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) bool { return false; }
fn bareRemap(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) ?[*]u8 { return null; }
fn bareFree(_: *anyopaque, memory: []u8, _: @import("std").mem.Alignment, _: usize) void { free(@ptrCast(memory.ptr)); }
const vtable = @import("std").mem.Allocator.VTable{ .alloc = bareAlloc, .resize = bareResize, .remap = bareRemap, .free = bareFree };
const allocator = @import("std").mem.Allocator{ .ptr = undefined, .vtable = &vtable };
};
const __koru_backing = if (@import("builtin").link_libc) @import("std").heap.c_allocator else if (@import("builtin").os.tag == .freestanding) __koru_bare.allocator else @import("std").heap.page_allocator;
var __koru_leak_count: @import("std").atomic.Value(usize) = .init(0);
fn __koru_alloc(ctx: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ctx;
const r = __koru_backing.rawAlloc(len, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
if (r != null) _ = __koru_leak_count.fetchAdd(1, .monotonic);
}
return r;
}
fn __koru_resize(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) bool {
_ = ctx;
return __koru_backing.rawResize(memory, alignment, new_len, ret_addr);
}
fn __koru_remap(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
_ = ctx;
return __koru_backing.rawRemap(memory, alignment, new_len, ret_addr);
}
fn __koru_free(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, ret_addr: usize) void {
_ = ctx;
__koru_backing.rawFree(memory, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
_ = __koru_leak_count.fetchSub(1, .monotonic);
}
}
const __koru_vtable = @import("std").mem.Allocator.VTable{ .alloc = __koru_alloc, .resize = __koru_resize, .remap = __koru_remap, .free = __koru_free };
pub fn koru_allocator() @import("std").mem.Allocator {
return .{ .ptr = undefined, .vtable = &__koru_vtable };
}
pub inline fn __koru_intcast(comptime T: type, x: anytype) T {
if (comptime (@import("builtin").mode == .Debug or @import("builtin").mode == .ReleaseSafe))
return @as(T, @intCast(x));
const dst = @typeInfo(T);
const src = @typeInfo(@TypeOf(x));
if (comptime (dst == .int and src == .int and dst.int.bits == src.int.bits and dst.int.signedness != src.int.signedness))
return @as(T, @bitCast(x));
return @as(T, @intCast(x));
}
pub fn koru_leak_check() void {
if (comptime @import("builtin").mode != .Debug) return;
if (__koru_leak_count.load(.acquire) == 0) return;
if (comptime @import("builtin").target.os.tag == .freestanding) {
if (comptime @import("builtin").cpu.arch == .wasm32 or @import("builtin").cpu.arch == .wasm64) {
@panic("KORU LEAK CHECK FAILED: the produced program leaked");
} else {
const __klc = struct { extern var stdout: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; };
var __lb: [128]u8 = undefined;
const __lm = "KORU LEAK CHECK FAILED: allocations still outstanding at end of run: ";
@memcpy(__lb[0..__lm.len], __lm);
var __ln: usize = __lm.len;
var __lv = __koru_leak_count.load(.acquire);
var __ld: [20]u8 = undefined;
var __lk: usize = 0;
while (__lv > 0) : (__lk += 1) { __ld[__lk] = @intCast('0' + __lv % 10); __lv /= 10; }
for (0..__lk) |__li| { __lb[__ln] = __ld[__lk - 1 - __li]; __ln += 1; }
__lb[__ln] = '\n'; __ln += 1; __lb[__ln] = 0;
_ = __klc.fputs(@as([*:0]const u8, @ptrCast(&__lb)), __klc.stdout);
@trap();
}
} else {
@import("std").debug.print("KORU LEAK CHECK FAILED: the produced program leaked (trace above)\n", .{});
@import("std").process.exit(1);
}
}
pub const main_module = struct {
// std/store: 'own' created (cell + write-subflow at module scope); fields epoch: i64, etag: i64
// std/store: 'st' created (cell + write-subflow at module scope); fields pos: i64, synced: i64
pub const cas_claim_event = struct {
pub const Input = struct {
node: []const u8,
guard: i64,
};
pub const Output = union(enum(u8)) {
applied: []const u8,
rejected: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:47
const node = __koru_event_input.node;
const guard = __koru_event_input.guard;
_ = &node;
_ = &guard;
_ = &__koru_event_input;
if ((blk: { const __koru_l = guard; const __koru_r = __koru_store_own.etag; const __koru_lo = @typeInfo(@TypeOf(__koru_l)) == .optional; const __koru_ro = @typeInfo(@TypeOf(__koru_r)) == .optional; const __koru_lp = if (__koru_lo) @typeInfo(@TypeOf(__koru_l)).optional.child else @TypeOf(__koru_l); const __koru_rp = if (__koru_ro) @typeInfo(@TypeOf(__koru_r)).optional.child else @TypeOf(__koru_r); const __koru_ls = __koru_str: { const __koru_ty = @typeInfo(__koru_lp); if (__koru_ty == .array) break :__koru_str __koru_ty.array.child == u8; if (__koru_ty == .pointer) { const __koru_ptr = __koru_ty.pointer; if (__koru_ptr.size == .slice and __koru_ptr.child == u8) break :__koru_str true; break :__koru_str __koru_ptr.size == .one and @typeInfo(__koru_ptr.child) == .array and @typeInfo(__koru_ptr.child).array.child == u8; } break :__koru_str false; }; const __koru_rs = __koru_str: { const __koru_ty = @typeInfo(__koru_rp); if (__koru_ty == .array) break :__koru_str __koru_ty.array.child == u8; if (__koru_ty == .pointer) { const __koru_ptr = __koru_ty.pointer; if (__koru_ptr.size == .slice and __koru_ptr.child == u8) break :__koru_str true; break :__koru_str __koru_ptr.size == .one and @typeInfo(__koru_ptr.child) == .array and @typeInfo(__koru_ptr.child).array.child == u8; } break :__koru_str false; }; if (__koru_ls and __koru_rs) { const __koru_ln = if (__koru_lo) __koru_l == null else false; const __koru_rn = if (__koru_ro) __koru_r == null else false; break :blk (__koru_ln == __koru_rn) and (__koru_ln or @import("std").mem.eql(u8, if (__koru_lo) __koru_l.? else __koru_l, if (__koru_ro) __koru_r.? else __koru_r)); } else break :blk __koru_l == __koru_r; })) {
{ const result_c0_0 = main_module.__store_envwrite_own_event.handler(.{ .field = @as(i64, @bitCast(@as(u64, 1) << 1)), .value = __koru_store_own.etag + 1 });
_ = &result_c0_0;
const result_c0_1 = main_module.__store_envwrite_own_event.handler(.{ .field = @as(i64, @bitCast(@as(u64, 1) << 0)), .value = __koru_store_own.epoch + 1 });
_ = &result_c0_1;
const result_c0_2 = main_module.__store_announce_own_event.handler(.{ .field = 1 });
_ = &result_c0_2;
const result_c0_3 = main_module.__store_announce_own_event.handler(.{ .field = 0 });
_ = &result_c0_3;
return .{ .applied = node };
}
} else {
{ return .{ .rejected = .{ } };
}
}
}
};
pub const cas_release_event = struct {
pub const Input = struct {
lease: []const u8,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:52
const lease = __koru_event_input.lease;
_ = &lease;
_ = &__koru_event_input;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" release {s}\n", .{lease});
}
};
pub const cell_write_event = struct {
pub const Input = struct {
lease: []const u8,
data: []const u8,
};
pub const Output = union(enum(u8)) {
committed: []const u8,
not_resident: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:61
const lease = __koru_event_input.lease;
const data = __koru_event_input.data;
_ = &lease;
_ = &data;
_ = &__koru_event_input;
if (__koru_store_own.epoch > 0) {
{ const result_c0_0 = main_module.__store_write_st_event.handler(.{ .field = 0, .value = __koru_store_st.pos + 1 });
_ = &result_c0_0;
return .{ .committed = data };
}
} else {
{ return .{ .not_resident = .{ } };
}
}
}
};
pub const replica_sync_event = struct {
pub const Input = struct {
lease: []const u8,
upto: i64,
};
pub const Output = union(enum(u8)) {
reached: i64,
behind: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:70
const lease = __koru_event_input.lease;
const upto = __koru_event_input.upto;
_ = &lease;
_ = &upto;
_ = &__koru_event_input;
if (upto <= __koru_store_st.pos) {
{ const result_c0_0 = main_module.__store_write_st_event.handler(.{ .field = 1, .value = upto });
_ = &result_c0_0;
return .{ .reached = upto };
}
} else {
{ return .{ .behind = .{ } };
}
}
}
};
pub const gate_ack_event = struct {
pub const Input = struct {
r: []const u8,
proof: i64,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:76
const r = __koru_event_input.r;
const proof = __koru_event_input.proof;
_ = &r;
_ = &proof;
_ = &__koru_event_input;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" ack {s} — durable through {d}\n", .{r, proof});
}
};
pub const gate_fail_event = struct {
pub const Input = struct {
r: []const u8,
why: []const u8,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:80
const r = __koru_event_input.r;
const why = __koru_event_input.why;
_ = &r;
_ = &why;
_ = &__koru_event_input;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" fail {s} — {s}\n", .{r, why});
}
};
pub const handle_event = struct {
pub const Input = struct {
lease: []const u8,
data: []const u8,
ahead: i64,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:86
const lease = __koru_event_input.lease;
const data = __koru_event_input.data;
const ahead = __koru_event_input.ahead;
_ = &lease;
_ = &data;
_ = &ahead;
_ = &__koru_event_input;
const result = main_module.cell_write_event.handler(.{ .lease = lease, .data = data });
switch (result) {
// >>> BRANCH: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:90 | not-resident |>
.not_resident => {
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" not resident at commit epoch\n", .{});
},
// >>> BRANCH: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:87 | committed r |>
.committed => |r| {
_ = &r;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw(" held {s} at pos {d} — response gated\n", .{data, __koru_store_st.pos});
const result_0 = main_module.replica_sync_event.handler(.{ .lease = lease, .upto = __koru_store_st.pos + ahead });
_ = &result_0;
switch (result_0) {
// >>> BRANCH: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:88 | reached p |>
.reached => |p| {
_ = main_module.gate_ack_event.handler(.{ .r = r, .proof = p });
},
// >>> BRANCH: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:89 | behind |>
.behind => {
_ = main_module.gate_fail_event.handler(.{ .r = r, .why = "durability unproven" });
},
}
},
}
}
};
// >>> FLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:92 ~std.io:print.impl()
pub fn flow0() void {
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw("node-a claims the cell\n", .{});
}
// >>> FLOW: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:93 ~input:cas.claim()
pub fn flow1() void {
const result_0 = main_module.cas_claim_event.handler(.{ .node = "node-a", .guard = 0 });
switch (result_0) {
// >>> BRANCH: tests/regression/830_THE_WORLD/842_celld_durability_gate/input.k:94 | applied l |>
.applied => |l| {
const result_1 = main_module.handle_event.handler(.{ .lease = l, .data = "msg-1", .ahead = 0 });
_ = &result_
... [truncated - 28KB total]Emitted C# source
static class main_module {
public static void __koru_stdout_write(dynamic s) => global::System.Console.Out.Write(s);
public static void __koru_stderr_write(dynamic s) => global::System.Console.Error.Write(s);
// The mutable handle carrier — `*String`-style resources are
// `new __KoruBox { data = … }` because C# anonymous types are
// read-only: `s.data = …` (std/string append/clear, handle
// mutation generally) needs a settable member.
public class __KoruBox { public dynamic data; }
// Textification for `{{ … }}` operands: C# bool ToStrings as
// `True` where Koru prints `true`, and the operand's static type
// is unknown at this boundary — a `(x) is bool` inline test would
// be a compile error on statically-typed operands instead. Generic
// on purpose: `dynamic` boxed every value-type operand — measured
// ~1s/10M elements on 012_threat_scanner — while T specializes to
// the operand's own ToString() with no box.
public static string __koru_str<T>(T v) => v is bool b ? (b ? "true" : "false") : v?.ToString();
// std/store: 'own' created (cell + write-subflow at module scope); fields epoch: i64, etag: i64
// std/store: 'st' created (cell + write-subflow at module scope); fields pos: i64, synced: i64
static class __koru_store_own {
public static long epoch = 0;
public static long etag = 0;
public static long __koru_brand = -1;
}
static class __koru_store_st {
public static long pos = 0;
public static long synced = 0;
public static long __koru_brand = -1;
}
public static class cas_claim_event {
public struct Input {
public string node;
public long guard;
}
public struct Output {
public string tag;
public string applied;
public dynamic rejected;
}
public static Output handler(Input __koru_input) {
var node = __koru_input.node;
var guard = __koru_input.guard;
if (guard == __koru_store_own.etag) {
{ {
var __koru_p_field = (long)((long)((long)((ulong)((ulong)(1) << 1))));
var __koru_p_value = (long)(__koru_store_own.etag + 1);
var __koru_mask = (ulong)__koru_p_field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_own.epoch = __koru_p_value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_own.etag = __koru_p_value;
}
{
var __koru_p_field = (long)((long)((long)((ulong)((ulong)(1) << 0))));
var __koru_p_value = (long)(__koru_store_own.epoch + 1);
var __koru_mask = (ulong)__koru_p_field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_own.epoch = __koru_p_value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_own.etag = __koru_p_value;
}
{
var __koru_p_field = (long)(1);
}
{
var __koru_p_field = (long)(0);
}
return new Output { tag = "applied", applied = node };
}
} else {
{ return new Output { tag = "rejected" };
}
}
return default;
}
}
public static class cas_release_event {
public struct Input {
public string lease;
}
public static dynamic handler(Input __koru_input) {
var lease = __koru_input.lease;
__koru_stdout_write(" release " + __koru_str(lease) + "\n");
return default;
}
}
public static class cell_write_event {
public struct Input {
public string lease;
public string data;
}
public struct Output {
public string tag;
public string committed;
public dynamic not_resident;
}
public static Output handler(Input __koru_input) {
var lease = __koru_input.lease;
var data = __koru_input.data;
if (__koru_store_own.epoch > 0) {
{ main_module.__store_write_st_event.handler(new __store_write_st_event.Input { field = (long)(0), value = (long)(__koru_store_st.pos + 1)});
return new Output { tag = "committed", committed = data };
}
} else {
{ return new Output { tag = "not-resident" };
}
}
return default;
}
}
public static class replica_sync_event {
public struct Input {
public string lease;
public long upto;
}
public struct Output {
public string tag;
public long reached;
public dynamic behind;
}
public static Output handler(Input __koru_input) {
var lease = __koru_input.lease;
var upto = __koru_input.upto;
if (upto <= __koru_store_st.pos) {
{ main_module.__store_write_st_event.handler(new __store_write_st_event.Input { field = (long)(1), value = (long)(upto)});
return new Output { tag = "reached", reached = upto };
}
} else {
{ return new Output { tag = "behind" };
}
}
return default;
}
}
public static class gate_ack_event {
public struct Input {
public string r;
public long proof;
}
public static dynamic handler(Input __koru_input) {
var r = __koru_input.r;
var proof = __koru_input.proof;
__koru_stdout_write(" ack " + __koru_str(r) + " — durable through " + __koru_str(proof) + "\n");
return default;
}
}
public static class gate_fail_event {
public struct Input {
public string r;
public string why;
}
public static dynamic handler(Input __koru_input) {
var r = __koru_input.r;
var why = __koru_input.why;
__koru_stdout_write(" fail " + __koru_str(r) + " — " + __koru_str(why) + "\n");
return default;
}
}
public static class handle_event {
public struct Input {
public string lease;
public string data;
public long ahead;
}
public static dynamic handler(Input __koru_input) {
var lease = __koru_input.lease;
var data = __koru_input.data;
var ahead = __koru_input.ahead;
var result_0 = main_module.cell_write_event.handler(new cell_write_event.Input { lease = (string)(lease), data = (string)(data)});
if (result_0.tag == "committed") {
var r = result_0.committed;
__koru_stdout_write(" held " + __koru_str(data) + " at pos " + __koru_str(__koru_store_st.pos) + " — response gated" + "\n");
var result_1 = main_module.replica_sync_event.handler(new replica_sync_event.Input { lease = (string)(lease), upto = (long)(__koru_store_st.pos + ahead)});
if (result_1.tag == "reached") {
var p = result_1.reached;
main_module.gate_ack_event.handler(new gate_ack_event.Input { r = (string)(r), proof = (long)(p)});
}
if (result_1.tag == "behind") {
main_module.gate_fail_event.handler(new gate_fail_event.Input { r = (string)(r), why = (string)("durability unproven")});
}
}
if (result_0.tag == "not-resident") {
__koru_stdout_write(" not resident at commit epoch" + "\n");
}
return default;
}
}
public static class __store_apply_own_event {
public struct Input {
public long field;
public long value;
}
public struct Output {
public string tag;
public long epoch;
public long etag;
}
public static Output handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
switch (field) {
case 0: { __koru_store_own.epoch = value; return new Output { tag = "epoch", epoch = value }; }
case 1: { __koru_store_own.etag = value; return new Output { tag = "etag", etag = value }; }
}
throw new global::System.Exception("__store_apply_own: field index " + field + " is not a column of store 'own'");
return default;
}
}
public static class __store_announce_own_event {
public struct Input {
public long field;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
return default;
}
}
public static class __store_envwrite_own_event {
public struct Input {
public long field;
public long value;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
var __koru_mask = (ulong)field;
if ((__koru_mask & (1UL << 0)) != 0) __koru_store_own.epoch = value;
if ((__koru_mask & (1UL << 1)) != 0) __koru_store_own.etag = value;
return default;
}
}
public static class __store_apply_st_event {
public struct Input {
public long field;
public long value;
}
public struct Output {
public string tag;
public long pos;
public long synced;
}
public static Output handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
switch (field) {
case 0: { __koru_store_st.pos = value; return new Output { tag = "pos", pos = value }; }
case 1: { __koru_store_st.synced = value; return new Output { tag = "synced", synced = value }; }
}
throw new global::System.Exception("__store_apply_st: field index " + field + " is not a column of store 'st'");
return default;
}
}
public static class __store_write_st_event {
public struct Input {
public long field;
public long value;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
var value = __koru_input.value;
var result_2 = main_module.__store_apply_st_event.handler(new __store_apply_st_event.Input { field = (long)(field), value = (long)(value)});
if (result_2.tag == "pos") {
var _auto_8 = result_2.pos;
}
if (result_2.tag == "synced") {
var _auto_9 = result_2.synced;
}
return default;
}
}
public static class __store_announce_st_event {
public struct Input {
public long field;
}
public static dynamic handler(Input __koru_input) {
var field = __koru_input.field;
return default;
}
}
public static class std_io_print_impl_event {
public struct Input {
public dynamic expr;
}
public static dynamic handler(dynamic __koru_input) {
throw new global::System.Exception("std.io:impl has no C# implementation — its body resolves only to a non-cs target (|zig / [comptime]), directly or through a callee");
}
}
public static class std_control_if_event {
public struct Input {
public dynamic expr;
}
public struct Output {
public string tag;
public dynamic then;
public dynamic @else;
}
public static Output handler(Input __koru_input) {
var expr = __koru_input.expr;
throw new global::System.Exception("if: |template| proc is inlined at call sites and must never be called");
}
}
public static class koru_start_event {
public struct Input {
}
public struct Output {
public string tag;
public dynamic done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static class koru_end_event {
public struct Input {
}
public struct Output {
public string tag;
public dynamic done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static void flow0() {
__koru_stdout_write("node-a claims the cell" + "\n");
}
public static void flow1() {
var result_3 = main_module.cas_claim_event.handler(new cas_claim_event.Input { node = (string)("node-a"), guard = (long)(0)});
if (result_3.tag == "applied") {
var l = result_3.applied;
main_module.handle_event.handler(new handle_event.Input { lease = (string)(l), data = (string)("msg-1"), ahead = (long)(0)});
main_module.handle_event.handler(new handle_event.Input { lease = (string)(l), data = (string)("msg-2"), ahead = (long)(5)});
main_module.cas_release_event.handler(new cas_release_event.Input { lease = (string)(l)});
}
if (result_3.tag == "rejected") {
__koru_stdout_write(" rejected" + "\n");
}
}
public static void flow2() {
main_module.koru_start_event.handler(new koru_start_event.Input { });
}
public static void flow3() {
main_module.koru_end_event.handler(new koru_end_event.Input { });
}
}
static class Program {
static void Main() {
main_module.flow2();
main_module.flow0();
main_module.flow1();
main_module.flow3();
}
}
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (own, capacity: 1, source: epoch: 0[i64], etag: 0[i64])
flow ~new click a branch to expand · @labels scroll to their anchor
new (st, capacity: 1, source: pos: 0[i64], synced: 0[i64])
subflow ~cas.claim click a branch to expand · @labels scroll to their anchor
if (guard == own.etag)
subflow ~cas.release click a branch to expand · @labels scroll to their anchor
print.ln (expr: " release {{ lease:s }}")
subflow ~cell.write click a branch to expand · @labels scroll to their anchor
if (own.epoch > 0)
subflow ~replica.sync click a branch to expand · @labels scroll to their anchor
if (upto <= st.pos)
subflow ~gate.ack click a branch to expand · @labels scroll to their anchor
print.ln (expr: " ack {{ r:s }} — durable through {{ proof:d }}")
subflow ~gate.fail click a branch to expand · @labels scroll to their anchor
print.ln (expr: " fail {{ r:s }} — {{ why:s }}")
subflow ~handle click a branch to expand · @labels scroll to their anchor
cell.write (lease, data)
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "node-a claims the cell")
flow ~cas.claim click a branch to expand · @labels scroll to their anchor
cas.claim (node: "node-a", guard: 0)
Test Configuration
MUST_RUN LANGUAGES: zig cs