Skip to content

ADR 0098 D1: Clock/Entropy seam — time and randomness become injected world inputs - #697

Merged
nikhilunni merged 1 commit into
mainfrom
d1-clock-entropy-seam
Jul 16, 2026
Merged

nikhilunni merged 1 commit into
mainfrom
d1-clock-entropy-seam

Conversation

@nikhilunni

Copy link
Copy Markdown
Contributor

First DST-harness PR of ADR 0098 (#694). Follows #695/#696.

What

The seam — engram_core::traits::{Clock, Entropy} (crates/engram-core/src/traits/clock.rs):

  • Clock::now_utc() / now_mono() (Duration since a process epoch — replaces Instant pairs, since a fake can't mint opaque Instants) / sleep().
  • Entropy::uuid() / u64(range) for jitter. Prod impls SystemClock / OsEntropy; Services gains clock + entropy fields.

The sweep — ~70 files: every decision-feeding Utc::now(), Instant::now(), Uuid::new_v4() in the coordinator reads through the seam. Highlights:

  • idle_detector::classify takes a hoisted now (the event-age TTL decision the DST program targets); placement's ranking fns take a trailing now from callers; dead_host's ProbeMemory.last_rescue becomes a now_mono() Duration mark; Reconciler and CowStateCache carry injected clock fields (SystemClock default, with_clock() for the sim harness).
  • Event/DB timestamps (at:) convert too — trace reproducibility under simulation needs them on the sim clock.

Enforcement — crates/engram-coordinator/clippy.toml disallowed-methods + workspace clippy::disallowed_methods = "deny": a direct call is a hard error. SystemClock/OsEntropy are the one honest caller of each; test modules that drive a live system carry one scoped allow with a standard justification comment.

Semantic fix the lane caught

The three GC sweeps (chunk / bundle / snapshot-blob) mark candidates — PG stamps first_seen_at DEFAULT now() — then promote in the same call. A now captured at sweep start never sees a same-sweep candidate as expired under zero grace (two admin_chunk_gc_live_pg tests failed). Fixed by threading the clock and reading it fresh at promote time, preserving pre-D1 ordering. The host-vs-PG cross-clock comparison is pre-existing (the f97a1c2 class) and is what D3's bind-param now() unifies.

Verification

  • just check green (1680 passed); coordinator clippy clean across all targets.
  • Full 143-test live-PG lane green 2× at --test-threads=16.
  • Negative probe: a planted Utc::now() in src fails clippy with the ADR-0098 message, then reverted.

Next

D3 (bind-param now() in PostgresStore, 126 sites) and the engram-sim crate (D4) build on this seam.

🤖 Generated with Claude Code

… world inputs

New engram-core traits Clock (now_utc / now_mono / sleep) and Entropy
(uuid / u64-jitter) with SystemClock/OsEntropy prod impls; Services
gains clock + entropy fields. Every decision-feeding Utc::now(),
Instant::now(), and Uuid::new_v4() in the coordinator now reads
through the seam — idle-detector event-age TTLs (classify takes a
hoisted now param), queue-scanner timeouts, dead_host heartbeat
staleness (ProbeMemory.last_rescue: Instant -> now_mono Duration
marks), placement (all ranking fns take a trailing now), reconciler
and CowStateCache (injected clock fields, SystemClock default),
event/DB timestamps included for trace reproducibility.

Enforcement: crates/engram-coordinator/clippy.toml disallowed-methods
+ workspace lint clippy::disallowed_methods=deny make a direct call a
hard error (verified by negative probe). SystemClock/OsEntropy are
the one honest caller of each; test modules that drive a live system
carry a single scoped allow with a standard justification comment.

Semantic fix caught by the live-PG lane: the three GC sweeps
(chunk/bundle/snapshot-blob) mark candidates (PG stamps first_seen_at
DEFAULT now()) then promote in the same call — a now captured at
sweep start never sees a same-sweep candidate as expired under zero
grace (admin_chunk_gc tests failed). The sweeps take the clock and
read it fresh at promote time, preserving the pre-D1 ordering; the
host-vs-PG cross-clock comparison itself is pre-existing and is
unified by D3's bind-param now().

Verified: just check green (1680 passed); coordinator clippy clean
across all targets; full 143-test live-PG lane green 2x at 16
threads.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@nikhilunni
nikhilunni merged commit e647d22 into main Jul 16, 2026
19 checks passed
@nikhilunni
nikhilunni deleted the d1-clock-entropy-seam branch July 16, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant