✓
Passing This code compiles and runs correctly.
Code
// Pins: the test block std/compiler's own test lowering generates must reach
// the emitted program.
//
// processTestFlow (koru_std/compiler.kz:1423) parses a test body, substitutes
// each mocked event by writing `const __mock_result_N = <mocked branch>` plus
// the binding extraction for the branch the mock forces
// (compiler.kz:1722-1773), and hands the finished `test "..." { ... }` back as
// an inline_code item that replaces the test flow.
//
// That pass only ever sees a live test flow when the coordinator puts
// test-generation ahead of elaborate: std/testing's test transform
// (koru_std/testing.kz:225) runs inside elaborate's run-pre-transforms
// (compiler.kz:1249) and consumes every test invocation, so under the default
// order (compiler.kz:757) test-generation finds nothing to do. The coordinator
// override below is the shape 430_COORDINATION/8401_custom_coordinator_bug and
// 430_008_pipeline_profiling already pin.
//
// post.sh reads output_emitted.zig with the variant-coverage checker's own
// bare-token predicate and requires a __mock_result_N token in bare code.
// See BUG.md.
import std/compiler
import std/optimizer
import std/testing
import std/io
std/compiler:coordinate = std/compiler:context-create(program_ast, allocator): c0 |> std/compiler:test-generation(ctx: c0): c1 |> std/compiler:elaborate(ctx: c1): c2 |> std/compiler:transform-taps(ctx: c2): c3 |> std/compiler:analysis(ctx: c3)
| ctx c4 |> std/optimizer:optimize(ctx: c4): c5 |> std/compiler:emission(ctx: c5): c6 => coordinated {
c6.ctx.ast,
c6.code,
metrics: "test-generation ran before elaborate"
}
| failed f => error f.message
tor withdraw { amount: u32 }
| success u32
| insufficient-funds
test(mock substitution binds the mocked branch) {
withdraw => success 50
withdraw(amount: 100)
| success s |> _
}
std/io:print.ln("test-generation pass ran")
Supporting Files
# Aspirational: test-generation's output is discarded before it reaches the AST
Red on purpose. It flips green when `koru_std/compiler.kz:1361-1371` is fixed,
and at that moment it also becomes the corpus witness for `__mock_result_{}`,
which the variant-coverage wall reports UNCOVERED today.
## The defect
`test-generation` (compiler.kz:1309) collects the flows it lowered into
`test_flows_to_remove`, then rebuilds the item list deciding replacement by
pointer identity:
```zig
for (mutable_ctx.ast.items) |item| { // 1361 — by-value capture
if (item == .flow) {
const flow = &item.flow; // 1363 — address of the copy
for (test_flows_to_remove.items) |test_flow| {
if (test_flow == flow) { // 1367
```
`test_flows_to_remove` was filled from a *different* by-value `for` at 1322,
so the two `&item.flow` addresses are addresses of two separate per-loop
temporaries. `should_replace` is never true: the `~test` flow survives
untouched and the generated `test "..." { ... }` inline_code is dropped.
Consequences: every block this pass produces is thrown away, and
`__mock_result_{}` — real emitted Zig, written bare through
`code_emitter.write` at compiler.kz:1727-1732 — can appear in no artifact.
## Evidence (2026-08-06, this test's own program shape)
The arm provably executes:
- A mock naming a branch the event does not declare panics at
compiler.kz:1504; the stack trace reads
`processTestFlow` ← `test_generation_event.handler` ← `coordinate_event.handler`.
- The same program with an impure `withdraw` and **no** mock panics at
compiler.kz:1542, `Test '...' has impure events without mocks`. That proves
both that the `~test` body parsed into a flow and that the mock map hits for
the mocked variant — the same map and key `emitFlowWithMocks` tests at
compiler.kz:1722.
The product provably does not land:
- `std/compiler:ast-dump` inserted immediately after
`std/compiler:test-generation.default(ctx)` shows the `~test` invocation
still present as a `flow` item and no `inline_code` item anywhere in the AST.
## Neighbouring findings, not fixed here
- `koru_std/testing.kz:685` defines a second `emitFlowWithMocks` with no caller
anywhere in the repo.
- The `~test` body is parsed under the synthetic filename `__test__.kz`
(testing.kz:280, compiler.kz:1460), so a body written without `~` prefixes
parses as Zig host lines — zero mocks, zero flows. This test's body is
`~`-prefixed, matching 395_008.
Flows
subflow ~coordinate click a branch to expand · @labels scroll to their anchor
context-create (program_ast, allocator)
flow ~test click a branch to expand · @labels scroll to their anchor
test (mock substitution binds the mocked branch, source: withdraw => success 50
withdraw(amount: 100)
| success s |> _)
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "test-generation pass ran")
Test Configuration
Post-validation Script:
#!/bin/bash
# The generated test block must reach output_emitted.zig. Decided with the
# variant-coverage checker's own bare-token predicate, not a plain grep: the
# whole point is bare code versus a string literal, which grep cannot tell
# apart.
cd "$(dirname "$0")"
ROOT="$(cd ../../../../.. && pwd)"
python3 - "$ROOT" <<'PY'
import sys
from pathlib import Path
root = Path(sys.argv[1])
sys.path.insert(0, str(root / "invariants" / "checks"))
from check_variant_coverage import bare_tokens, marker_pattern
artifact = Path("output_emitted.zig")
if not artifact.exists():
print("FAIL: no output_emitted.zig — the backend never emitted the program")
sys.exit(1)
pattern = marker_pattern("__mock_result_{}")
hits = sorted(t for t in bare_tokens(artifact) if pattern.match(t))
if hits:
print("PASS: emitted program carries the mock substitution:", ", ".join(hits))
sys.exit(0)
print("FAIL: no bare __mock_result_N token in output_emitted.zig.")
print(" processTestFlow generated the test block (it panics from inside itself")
print(" on a bad mock, and on an unmocked impure event), but the block never")
print(" reached the AST — see BUG.md.")
sys.exit(1)
PY