tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

75 lignes · 8319 octetsBrut · Historique

ADR 0036: clock and random, effects the runtime handles

Status: Proposed (2026-10-13, issue #142); needs engineering review (#44). Extends ADR 0004 (the root capabilities of actors) and ADR 0010.

Context

A program read the time and random numbers through externs: tl_random_int and tl_random_token were plain functions, and there was no clock. So code that used them couldn't be tested deterministically: a session token, a salt or a timestamp changed on every run, and nothing could replace them in a test.

Louise already has the tool for this: effects. proc is handled by the runtime around main, but a test can still handle any other effect itself.

Decision

Two effects

  • std/time: pub effect clock { fn now_unix_ns() -> Int fn monotonic_ns() -> Int }.
  • std/random: pub effect random { fn bytes(count: Int) -> Bytes }.

Both have ordinary fn operations, so any handler can answer them. Everything else is Louise code over these operations: time.now(), Timestamp, Duration, the UTC calendar and RFC 3339; random.int, range, float, shuffle, choice, token.

The runtime handles them, like proc

The runtime gives a root capability for each of proc, clock and random around main, each @test and every actor:

  • now_unix_ns reads the system's wall clock.
  • monotonic_ns reads the scheduler's clock in nanoseconds. It is virtual until the first socket opens (ADR 0004, Revision 3), so a program without sockets measures exactly what process.sleep waited, on every run. Then it is real time, continuing from the virtual value, so it never goes back.
  • bytes(n) reads the kernel's generator (/dev/urandom), good for tokens and salts; a negative n panics.
  • Neither operation is a scheduling point, on either backend.

Actor rows become <proc, clock, random, io, ffi> (process.spawn, spawn_link, spawn_monitor, std/actor, std/supervisor, std/http handlers). A handler installed around spawn does not follow the new actor: it gets the runtime's handlers. That is how proc already behaved.

Built-in effects are definitions, not names

louise_core::runnable used to accept main's effects by name (io, ffi, proc). It now compares definitions (WellKnown: the prelude's io and ffi, std/process.proc, std/time.clock, std/random.random). So a program's own effect called clock is still refused as unhandled.

The capability layout is computed

Before, every root capability was proc: the native main passed one per parameter, and actor bodies got exactly one. Now:

  • Entry functions (main, tests): lowering records, for each function whose capabilities are all root ones, their kinds in the order of its row (Program::root_caps). The native main emits tl_root_cap(ctx, TL_ROOT_<KIND>, n_ops) for each.
  • Actor bodies: lowering derives the order from std/process.spawn's parameter type, fn() -> () / <proc, clock, random, io, ffi>: the labels whose instance callers supply, sorted as the type checker sorts a lambda's capabilities (by effect definition, then index). The result is Program::actor_caps, which is passed to tl_main_program(entry, caps, n). The runtime builds those capabilities for each actor. A test checks that the three spawn operations give a body the same capabilities.
  • Codegen checks each root effect's operations against the runtime's tables (PROC_OPS, CLOCK_OPS, RANDOM_OPS in louise_rt_prims), and the runtime checks the kind and the count at tl_root_cap.

Immortal capabilities

proc's root capability is built fresh for each actor (until Revision 1, below: now it is immortal too). clock's and random's hold no state, so each is built once and is immortal (rc 0), as are its clauses:

  • every actor gets the same block;
  • tl_dup/tl_drop and send leave it alone;
  • it is allocated outside the counted heap, so the leak oracle never sees it.

The operating system only behind the effects

The primitives that read the clock and the kernel's generator are runtime functions that only the root clauses call (louise_rt/src/clock.rs). They are not in std or the extern table, so no Louise code can read the time or randomness without going through the effects. The old externs tl_random_int and tl_random_token are removed.

Seeded randomness in Louise

std/prng is SplitMix64 over U64 (ADR 0035): seeded(seed), next(g). random.with_seed(seed, action) answers bytes(n) from it: each call draws ceil(n / 8) outputs and takes their low bytes first. The values for a seed are part of the interface, the same on both backends and every run. They are not for secrets: SplitMix64 is predictable from a few outputs. time.with_fixed_clock(at, action) answers now() with at and monotonic() with 0.

Runtime ABI 6: tl_root_cap(ctx, kind, ops), tl_main_program(entry, actor_caps, n), and the removal of tl_random_int and tl_random_token.

Consequences

  • Code that reads the time or randomness says so in its type, and a test controls both: the demo's tests run with random.with_seed(1), so their session tokens and salts are the same on every run.
  • random.int uses rejection sampling over the bits it needs, so every value is equally likely, and for a seed the values are the same on both backends.
  • A named function passed as an actor body or step function must declare the full row (<proc, clock, random, io, ffi>), even if it uses neither effect: a function value can only be passed where its declared type is expected (ADR 0010). A lambda (fn(s, m) { step(s, m) }) avoids it, as the demo's store does. Adapting a named function to a larger row automatically would remove this cost; it is left for later.
  • Every actor receives three capabilities instead of one: two more arguments, no allocation (the clock and random ones are shared).
  • A defer cleanup can't read the clock or randomness (effects with operations, ADR 0016).
  • Time zones are out of scope: UTC only.

Alternatives considered

  • Library effects, handled in main by hand: no runtime change, but every program and test would need a handler at the top, and actors would need one each.
  • Externs, as before: simplest, but not testable, which was the point of #142.
  • clock and random only around main and tests, not actors: actors would have to receive handlers through closures, which ADR 0010's escape rules forbid across spawn.
  • Fresh clock and random capabilities per actor, like proc's: two allocations per actor for blocks without state.
  • The OS primitives as externs in std (@extern functions only std/time calls): any module could call them and bypass the effect.

Revision 1 — 2026-11-02: proc's root capability is immortal too

Status: Proposed (#422, PR #410); needs engineering review (#44).

proc's capability holds no state either: its clauses capture nothing, and each reads the running actor from its ctx. Building it for each actor cost 18 allocations at each start (the block and one closure per operation) and their release at each exit, through the actor's drop worklist. The 17th operation (unlink, #422) made that release grow a fresh worklist once more (4, 8, 16, then 32 slots): +9.9% instructions in tests/bench/churn and +7.1% in idle. Reserving the worklist's room when the capability was built removed the growth, but it kept 17 slots for the actor's whole life: +136 bytes per idle actor, past ADR 0028's budget of 2 KiB.

So proc's capability is now built once and is immortal, like clock's (clock::immortal_cap): every actor gets the same block, tl_dup/tl_drop and send leave it alone, and the leak count never sees it. tl_root_cap keeps its ctx parameter (ABI 6 is unchanged).

Measured against be350b9 (stage 1 before #421 to #423), one thread, instructions: churn 980.4M → 636.4M (−35%), idle 913.6M → 613.6M (−33%), and links_normal, links_trap and links_kill 22% to 30% fewer. Resident memory per idle actor (ADR 0028's method, 1,000 against 100,000 actors): 1,908 → 1,279 bytes.

Nothing a program can observe changes: a clause acts on the actor whose ctx runs it, whichever block it came from.