✓
Passing This code compiles and runs correctly.
Code
// The World in Koru — entry 8.
// Doorway: celld's ownership lease — `cas_owner()` + the epoch fence, from
// Deno's self-hosted Durable Objects (github.com/denoland/celld, Rust).
//
// Over there, a cell's owner is one object in an S3 bucket,
// `cells/<cell>/own.json` = `{ node, epoch }`, claimed by a conditional PUT:
// If-None-Match to create, If-Match to take over (`bucket.rs:205-233`). A
// takeover bumps the epoch (`logic/lib.rs:2812`), and the epoch is baked into
// the object PREFIX — `cells/<cell>/ltx/e<epoch>/` — so, in their words,
// "epoch-in-prefix is the data-path fence: a stale owner writes a dead prefix"
// (`replication.rs:6`). No consensus, no membership protocol, no failure
// detector. The bucket is the coordinator, and one atomic write is the whole
// election.
//
// It is a beautiful fence, and it is a fence made of CONSEQUENCE. A node that
// lost the CAS still runs. It still calls its write path. It is merely writing
// somewhere nobody will ever read. Correctness arrives after the fact, as a
// property of where the bytes landed.
//
// Here in Koru the lease is an OBLIGATION, so it arrives before the fact.
// `cas.claim` is the only minter of `<lease!>`; `cell.write` BORROWS `<lease>`
// on an input, so it cannot be entered without proof of ownership; `cas.release`
// consumes `<!lease>`, retiring the proof. The stale owner does not write a dead
// prefix. The stale owner does not compile — see the three negative twins,
// 839 (use after release), 840 (forged lease), 841 (a lease with no way back).
//
// LANGUAGE CORNER: 834/835 exercise obligations as mint-then-consume — a lock
// you take and give back. This entry exercises the third form, the BORROW: an
// input typed `<lease>` reads the token without discharging it, so one lease
// admits many writes and no lease admits none. That is capability-as-type, and
// nothing else in the catalog uses it.
import std/io
import std/store
// ── THE BUCKET ──────────────────────────────────────────────────────────────
// One row: the owner record. `etag` is the version a conditional PUT compares;
// `epoch` is the fence that rides in the object prefix. In-memory and
// deterministic — which is also celld's own posture, since the crate holding
// their entire distributed protocol declares "no async, I/O, clocks,
// randomness, locks, or dependencies."
std/store:new(own, capacity: 1) { epoch: 0[i64], etag: 0[i64] }
// ── THE CAS ─────────────────────────────────────────────────────────────────
// The only minter of <lease!>. On a win the record is rewritten — etag advances,
// epoch bumps — and the winner receives a token. On a loss the caller gets a
// branch with no token in it, and there is nothing downstream of `rejected`
// that can reach the data path.
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
// ── THE DATA PATH ───────────────────────────────────────────────────────────
// BORROWS <lease>: reads the token, does not retire it.
pub tor cell.write { lease: string<lease>, cell: string, data: string }
cell.write = std/io:print.ln(" {{ lease:s }} write e{{ own.epoch:d }} {{ cell:s }} <- {{ data:s }}")
// ── THE RELEASE ─────────────────────────────────────────────────────────────
// CONSUMES <!lease>, the way celld's `release_owner` blanks the record so the
// next node can claim without waiting out a lease TTL.
pub tor cas.release { lease: string<!lease> }
cas.release = std/io:print.ln(" {{ lease:s }} release")
// ── A TWO-NODE RACE ─────────────────────────────────────────────────────────
// node-a claims the fresh record and hands it back cleanly.
std/io:print.ln("node-a cas guard=0")
cas.claim(node: "node-a", guard: 0)
| applied la |> cell.write(lease: la, cell: "room-42", data: "hello from a") |> cas.release(lease: la)
| rejected |> std/io:print.ln(" node-a rejected")
// node-b races on a STALE guard — it read the record before node-a's write.
// This is the whole of compare-and-swap: no lock, no lease timer, no
// coordinator. The etag moved, so the write does not apply.
std/io:print.ln("node-b cas guard=0 (stale)")
cas.claim(node: "node-b", guard: 0)
| applied lb |> cell.write(lease: lb, cell: "room-42", data: "hello from b")
| rejected |> std/io:print.ln(" node-b rejected — etag moved")
// node-b re-reads and takes over at the current etag. NO release is written
// here: auto-discharge inserts it at scope exit, so a lease cannot be leaked
// even by forgetting. celld needs a 10-second node-lease TTL, a renewal timer,
// and a self-fence that halts the process (`logic/lib.rs:1838-1888`) to get the
// same property at runtime.
std/io:print.ln("node-b cas guard={{ own.etag:d }} (takeover)")
cas.claim(node: "node-b", guard: own.etag)
| applied lb2 |> cell.write(lease: lb2, cell: "room-42", data: "hello from b")
| rejected |> std/io:print.ln(" node-b rejected")
Actual
node-a cas guard=0
node-a write e1 room-42 <- hello from a
node-a release
node-b cas guard=0 (stale)
node-b rejected — etag moved
node-b cas guard=1 (takeover)
node-b write e2 room-42 <- hello from b
node-b release
Expected output
✓ Zig✓ C#node-a cas guard=0
node-a write e1 room-42 <- hello from a
node-a release
node-b cas guard=0 (stale)
node-b rejected — etag moved
node-b cas guard=1 (takeover)
node-b write e2 room-42 <- hello from b
node-b release
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
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/838_celld_lease_race/input.k:53
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 cell_write_event = struct {
pub const Input = struct {
lease: []const u8,
cell: []const u8,
data: []const u8,
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> SUBFLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:60
const lease = __koru_event_input.lease;
const cell = __koru_event_input.cell;
const data = __koru_event_input.data;
_ = &lease;
_ = &cell;
_ = &data;
_ = &__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(" {s} write e{d} {s} <- {s}\n", .{lease, __koru_store_own.epoch, cell, data});
}
};
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/838_celld_lease_race/input.k:66
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(" {s} release\n", .{lease});
}
};
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:71 ~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 cas guard=0\n", .{});
}
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:72 ~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/838_celld_lease_race/input.k:73 | applied la |>
.applied => |la| {
const result_1 = main_module.cell_write_event.handler(.{ .lease = la, .cell = "room-42", .data = "hello from a" });
_ = &result_1;
_ = main_module.cas_release_event.handler(.{ .lease = la });
},
// >>> BRANCH: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:74 | rejected |>
.rejected => {
(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 rejected\n", .{});
},
}
}
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:79 ~std.io:print.impl()
pub fn flow2() 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-b cas guard=0 (stale)\n", .{});
}
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:80 ~input:cas.claim()
pub fn flow3() void {
const result_0 = main_module.cas_claim_event.handler(.{ .node = "node-b", .guard = 0 });
switch (result_0) {
// >>> BRANCH: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:81 | applied lb |>
.applied => |lb| {
const result_1 = main_module.cell_write_event.handler(.{ .lease = lb, .cell = "room-42", .data = "hello from b" });
_ = &result_1;
_ = main_module.cas_release_event.handler(.{ .lease = lb });
},
// >>> BRANCH: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:82 | rejected |>
.rejected => {
(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-b rejected — etag moved\n", .{});
},
}
}
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:89 ~std.io:print.impl()
pub fn flow4() 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-b cas guard={d} (takeover)\n", .{__koru_store_own.etag});
}
// >>> FLOW: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:90 ~input:cas.claim()
pub fn flow5() void {
const result_0 = main_module.cas_claim_event.handler(.{ .node = "node-b", .guard = __koru_store_own.etag });
switch (result_0) {
// >>> BRANCH: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:91 | applied lb2 |>
.applied => |lb2| {
const result_1 = main_module.cell_write_event.handler(.{ .lease = lb2, .cell = "room-42", .data = "hello from b" });
_ = &result_1;
_ = main_module.cas_release_event.handler(.{ .lease = lb2 });
},
// >>> BRANCH: tests/regression/830_THE_WORLD/838_celld_lease_race/input.k:92 | rejected |>
.rejected => {
(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-b rejected\n", .{});
},
}
}
pub fn koru_start_flow() void {
const result_0 = koru_kor
... [truncated - 24KB 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
static class __koru_store_own {
public static long epoch = 0;
public static long etag = 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 cell_write_event {
public struct Input {
public string lease;
public string cell;
public string data;
}
public static dynamic handler(Input __koru_input) {
var lease = __koru_input.lease;
var cell = __koru_input.cell;
var data = __koru_input.data;
__koru_stdout_write(" " + __koru_str(lease) + " write e" + __koru_str(__koru_store_own.epoch) + " " + __koru_str(cell) + " <- " + __koru_str(data) + "\n");
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(" " + __koru_str(lease) + " release" + "\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 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 cas guard=0" + "\n");
}
public static void flow1() {
var result_0 = main_module.cas_claim_event.handler(new cas_claim_event.Input { node = (string)("node-a"), guard = (long)(0)});
if (result_0.tag == "applied") {
var la = result_0.applied;
main_module.cell_write_event.handler(new cell_write_event.Input { lease = (string)(la), cell = (string)("room-42"), data = (string)("hello from a")});
main_module.cas_release_event.handler(new cas_release_event.Input { lease = (string)(la)});
}
if (result_0.tag == "rejected") {
__koru_stdout_write(" node-a rejected" + "\n");
}
}
public static void flow2() {
__koru_stdout_write("node-b cas guard=0 (stale)" + "\n");
}
public static void flow3() {
var result_1 = main_module.cas_claim_event.handler(new cas_claim_event.Input { node = (string)("node-b"), guard = (long)(0)});
if (result_1.tag == "applied") {
var lb = result_1.applied;
main_module.cell_write_event.handler(new cell_write_event.Input { lease = (string)(lb), cell = (string)("room-42"), data = (string)("hello from b")});
main_module.cas_release_event.handler(new cas_release_event.Input { lease = (string)(lb)});
}
if (result_1.tag == "rejected") {
__koru_stdout_write(" node-b rejected — etag moved" + "\n");
}
}
public static void flow4() {
__koru_stdout_write("node-b cas guard=" + __koru_str(__koru_store_own.etag) + " (takeover)" + "\n");
}
public static void flow5() {
var result_2 = main_module.cas_claim_event.handler(new cas_claim_event.Input { node = (string)("node-b"), guard = (long)(__koru_store_own.etag)});
if (result_2.tag == "applied") {
var lb2 = result_2.applied;
main_module.cell_write_event.handler(new cell_write_event.Input { lease = (string)(lb2), cell = (string)("room-42"), data = (string)("hello from b")});
main_module.cas_release_event.handler(new cas_release_event.Input { lease = (string)(lb2)});
}
if (result_2.tag == "rejected") {
__koru_stdout_write(" node-b rejected" + "\n");
}
}
public static void flow6() {
main_module.koru_start_event.handler(new koru_start_event.Input { });
}
public static void flow7() {
main_module.koru_end_event.handler(new koru_end_event.Input { });
}
}
static class Program {
static void Main() {
main_module.flow6();
main_module.flow0();
main_module.flow1();
main_module.flow2();
main_module.flow3();
main_module.flow4();
main_module.flow5();
main_module.flow7();
}
}
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (own, capacity: 1, source: epoch: 0[i64], etag: 0[i64])
subflow ~cas.claim click a branch to expand · @labels scroll to their anchor
if (guard == own.etag)
subflow ~cell.write click a branch to expand · @labels scroll to their anchor
print.ln (expr: " {{ lease:s }} write e{{ own.epoch:d }} {{ cell:s }} <- {{ data:s }}")
subflow ~cas.release click a branch to expand · @labels scroll to their anchor
print.ln (expr: " {{ lease:s }} release")
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "node-a cas guard=0")
flow ~cas.claim click a branch to expand · @labels scroll to their anchor
cas.claim (node: "node-a", guard: 0)
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "node-b cas guard=0 (stale)")
flow ~cas.claim click a branch to expand · @labels scroll to their anchor
cas.claim (node: "node-b", guard: 0)
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "node-b cas guard={{ own.etag:d }} (takeover)")
flow ~cas.claim click a branch to expand · @labels scroll to their anchor
cas.claim (node: "node-b", guard: own.etag)
Test Configuration
MUST_RUN LANGUAGES: zig cs