Two truths and pure functions
Keeping desired and observed state apart, and computing behavior as pure functions of facts.
- Core equation
actual != wanted → send the difference- Wanted function
wanted = f(config, events, t)- Relation to HAIG
- Extracted from the HAIG thesis as a separate runtime concern
- Audience
- Software engineers building systems that act on the real world
HAIG governs how an AI configures software and how people watch it. This note governs how the runtime itself works, regardless of whether configuration comes from an AI, a UI, or hand-edited files. The two ideas pair well because pure functions and explicit desired versus actual state make previews real, explanations provable, and recovery automatic, though neither requires the other.
1Two truths: wanted and actual
actual != wanted -> send the difference
A system that acts on the real world keeps two sources of truth apart on purpose:
- Wanted (ASoT, the assertive source of truth) is what the owner says the system should be at time
t. It is computed aswanted = f(t)from two explicit inputs: configuration (schedules, rules, settings) and append-only events (presses, requests, sensor readings, and recognized human changes). - Actual (ESoT, the emergent source of truth) is what the world actually shows right now, as last observed from devices, sensors, or external services. It is read, never written directly. Unknown is an explicit state (a source is down, a device is offline, or a reading omits a field). The system never invents what it was not told.
- The gap is the difference between
wanted(now)andactual(now), compared field by field. Unknown is a gap too.
The runtime runs one reconcile loop between them:
- Observe: read actual state as it is, read-only.
- Compute: evaluate
wanted = f(now)from configuration and recorded events. - Decide: a pure function
decide(wanted, actual, recent_sends, now)returnssend,wait, ornothing. - Act: pace writes with a cooldown and backoff. Slightly wrong for a moment is fine; flapping is not.
- Repeat on a timer and on every hint (a config change, a new reading, a device reconnecting).
Because the loop is level-triggered and recomputes from scratch on every pass, a lost write, a device returning from offline, a hub restart, and a schedule edit are all just a gap. None of them needs a special case.
2Some events are new ASoT
The reconcile loop always brings actual toward wanted, never the other way. Actual state is never copied directly into wanted. If f(t) copied whatever a device reported, the two truths would collapse into one and the system would agree with every glitch, reboot, or drift.
Instead, some observed events become new ASoT.
When a person acts outside the system (flips a wall switch, turns a thermostat dial, presses a TV remote), the system asks one question of the incoming reading: do my own recent sends explain it?
- If nothing sent explains a major move, the system classifies it as a person's change and records it as a timestamped event (when it happened, where it came from, and what changed).
- That event enters
f(t)as a bounded layer over the base configuration, setting only the fields that moved (for example, switching a light off or dimming it without overwriting its scheduled warmth). It stands until the next overriding moment, rule, or intent. - Everything else that disagrees with wanted (a dropped command, a minor echo, a device reboot, a power cut returning many devices together) is not a person's change. It stays a gap and the loop drives it back to wanted.
Modeling human overrides as events inside f(t) rather than silent mutations of desired state solves three problems at once: the system knows why the value changed, the layer carries its own rule for when it ends, and the underlying plan remains untouched underneath.
2.1Run versus ask
Not every thing in the real world should be held continuously (Table 1). Which mode applies comes from the configuration, not from hardcoded device types.
| Mode | Examples | How wanted behaves |
|---|---|---|
| Run | Lights in a routine, a thermostat band, a shade position | Kept as wanted over a stretch. The loop closes gaps continuously; a person's change becomes a new wanted layer until overridden. |
| Ask | Turning a TV on once at wake-up, a button press, a device that never reports | Wanted only until a reading confirms it (or for an explicit hold window, such as holding audio volume for 30 s while a TV starts). After that, nothing is wanted and whatever anyone does stands. |
3Rules for keeping two truths honest
- Show both, and the gap. People can see what is (actual), what is wanted, which layer is on top, and why a gap is not closing ("device offline", "source unreachable"). Never retry forever in silence.
- Fail safe, guess nothing. You do not know what you do not know. When a reading is missing or ambiguous, defer to
f(t)rather than guessing what a device probably means. - Pause is an event with an end. Pausing leaves what it covers as it is until a stated end (a time, a next moment, or resume). Recording pause as a layer means a forgotten pause stays visible in health checks instead of disappearing into hidden state.
- One precedence rule over data. Layers (base schedule, rules, pauses, presses, hand changes) resolve from explicit precedence data recorded on each layer, not ad-hoc branches scattered through the code.
- Explain from facts. "Why is the garage light on?" is answered by reading the active layer in
f(t)and the timestamped event that created it, not by guesswork.
4Pure functions of facts: same input, same output
Design behavior as a pure function of explicit inputs wherever possible: what the system wants now depends only on its configuration, its append-only events, and the timestamp t, never on hidden history or the order passes happened to run.
Why this property pays off across the whole system:
- Previews and dry runs are real. Running
f(t)on a virtual clock executes the exact same logic as production, not an approximation. - Explanations can be recomputed. "Why was the heating off at 7:00 AM?" is answered by evaluating
f(7:00 AM)on the recorded configuration and events. - Restarts change nothing. Folding the same inputs after a reboot yields the exact same wanted state.
- Retries are safe. Level-triggered reconciliation and idempotent edge effects mean a retry after a timeout cannot double-charge, double-book, or double-send.
- Tests are cheap and deterministic. Property tests can draw random configurations and run them on a virtual clock without real hardware. A test that fails intermittently is always a real bug, never something to rerun.
How to hold to it:
- Time is an input. Pass the clock in; never read system time inside core logic. Past, now, and tomorrow morning are the same function call.
- Randomness is seeded from inputs, so the same inputs always give the same result and can be reproduced.
- Derived, not stored. Anything computable from configuration and events is computed, never cached in mutable state where it can drift. Keep the event instead.
- One code path; effects at the edges. A new wish finds its place in the one model rather than adding a side branch. Side effects (sending a device command, charging a card, sending an email) stay few, thin, recorded, and idempotent.
Where the real world is impure (a sensor misreads, an external service is down), record what was observed as an input so even impure situations can be replayed afterward.
5Litmus tests
- Given the recorded configuration and events, can the system recompute what it wanted at any timestamp, and does a preview run the exact same code?
- Does actual state enter wanted only as a classified, recorded event, never by silently copying what a device shows?
- For any difference between wanted and actual, can a person see both, the layer on top, and why a gap is open or closing?
- Are a restart, a lost write, a returning offline device, and a config edit all handled by the same gap-closing path with no special cases?
- Do tests run on a virtual clock with deterministic inputs, without attaching real hardware?