roadmap BUILT

139. Drive the unsafe count down, and cinch the ratchet behind it

In brief. Reduce the hand-written unsafe this tree carries outside kernel/src/arch/, and lower the ceiling after each reduction so the ground gained cannot be given back quietly. The instrument is milestone 134 (the register of measures)'s census and the ceiling relation in script/lint.

Where it stands

The ceiling is <!--count-at-most:unsafe-density-outside-arch-->, in blocks per 10,000 lines outside kernel/src/arch/. It sits at the tree's own value with no headroom, a trade calef ruled for. Its current number, and the reasoning for each step, are in notes/unsafe-obligations/ceilings.md. The unsafe impls other than Send/Sync are held at the tree's exact value as unsafe-trait-claims (name provisional). unsafe fn has no ceiling: every call of one is already a block the density counts, because unsafe_op_in_unsafe_fn is on, and the # Safety contracts check binds each one.

The shared primitives this milestone's reductions rest on, each holding one invariant once:

Primitive What it holds
user_mode_runtime::mapped_window::MappedWindow a mapped page or frame, bounds-checked at every access; as_slice/as_mut_slice hand the whole window to a parser
the crate-root invoke wrappers and virtio::read_register and its siblings one capability method each, safe to call with any slot naming that method's object type (see BUGS)
initial_ramdisk::bytes(len) the initrd mapping every program starts with
crate::memory::alloc_zeroed, alloc_contiguous_zeroed a fresh frame, zeroed, exclusively the caller's
crate::device_tree the firmware's device-tree pointer
revoke::chain the revocation log's page walk, with SPACES held by a borrow
AddressSpace::while_installed installing a space for the length of a closure
intrusive_fifo::Unqueued<T> a thread's right to be on one queue (name provisional)

Rules in force

The scheduler's queue handoff (calef, #1842): an ownership token, Unqueued<T>, which is not Clone, replaces NonNull<T> in intrusive_fifo and inter_process_communication. Only a pop, a removal or thread creation mints one. The running thread's token lives on Thread::own_token, not in its core's current slot, because a slot written by one core and read by another would need a new unsafe impl Sync, or about 5% of the fastpath's band. Asked "confirm the running thread's queue token stays on Thread::own_token, not the per-core slot your #1842 ruling named", calef answered "a" (2026-10-20 UTC).

Names (calef, #1842): the foreign spelling stops at the boundary. initrd survives only as the device tree's linux,initrd-start and QEMU's -initrd, and the program side says initial_ramdisk. "IRQ" becomes "interrupt", per §154 (the acronym test is whether the phrase is spoken). Each ratified name carries its Name: ratified line at its definition, and design/naming.md states the boundary rule.

MappedWindow::as_slice and as_mut_slice stay, though they cost raw blocks. calef: "Keep. We don't want to penalize reuse." One invariant at six declarations beats eighteen call sites.

Exit criterion

calef, #1842: this block is BUILT when every open item and every Follow-on entry marked Outstanding is done, split out with a recorded owner, or refused with a reason. No item is open and no Follow-on entry is Outstanding.

kernel/src/arch/ is not a target and this block will not accept a reduction there. Rule 1 says architecture-specific code lives under arch/, and unsafe is what architecture is made of. Driving that number down means either writing the assembly wrong or moving it somewhere it does not belong, and both are worse than the number.

What counts as a reduction, which is the whole design of this block

The metric is gameable and the obvious way to game it is invisible. Moving three unsafe blocks into one helper function reduces the count by two and reduces the risk by nothing: the same invariants are asserted by the same code in a different place. A milestone that rewarded that would make the tree worse while the graph went the right way, and the graph would be the reason.

So the test is not the token count, it is the number of distinct invariants asserted by hand.

§94 (what may live in a userspace library, and what must be per-binary) is the worked example and it is what a real reduction looks like. The trap instruction was inlined at 48 sites in 7 variants across 58 panic handlers, each one a hand-written assertion of the same invariant. Lifting it into user_mode_runtime::trap() left one. That is 47 chances to write it differently, removed. §94 states the general form: a per-binary item whose body is copied verbatim into every binary is not per-binary; only its declaration is. Copying it is asserting the same invariant N times by hand.

And §94 found the cost of the copy by counting the copies: one of the 58 handlers was different. terminal_sink_caretaker called exit() and never trapped, so a panic there reported EVENT_EXIT where every other program reports EVENT_FAULT. A supervisor would have been told a panicking program finished cleanly. The duplication was not only ugly, it was already wrong in one place and nobody knew.

So a reduction qualifies when it does one of these:

It does not qualify when it merely relocates unsafe, wraps it in a function whose safety argument is the same argument, or hides it behind a macro.

The ratchet

After each reduction, lower the ceiling to the new count in the same commit. A ceiling left above the true number is not a ratchet, it is a budget, and a budget gets spent by whoever arrives next without knowing it was won.

Why it matters

DECISIONS §14 (the project's direction) calls this a verified-Rust capability microkernel; unsafe is where that claim is suspended. Every block outside the architecture layer is a place where the proofs and the type system are standing aside and a person's comment is the whole argument.

BUGS

Follow-on

Index row

Collapse hand-written unsafe outside kernel/src/arch/ into one declaration per invariant, and hold the unsafe-density-outside-arch ceiling at the tree's own value.