Skip to content

Pips buy their own food, and no workplace conjures it - #10

Merged
teceer merged 3 commits into
mainfrom
feat/pips-buy-their-own-food
Aug 6, 2026
Merged

teceer merged 3 commits into
mainfrom
feat/pips-buy-their-own-food

Conversation

@teceer

@teceer teceer commented Aug 6, 2026

Copy link
Copy Markdown
Owner

ADR 0006 step 7 — the point the whole ADR was written for.

Replaces #9, which GitHub auto-closed when its base branch (#8) was deleted on merge; a closed PR cannot be retargeted. Same branch, same commit, now based on main with main merged in.

What changes

Until now a shift handed the worker food out of nothing. farm.go gave +5 a tick, the tavern +3, and both files said in a comment that it was conjured. The economy from #4 and #8 ran alongside that, decorative: money moved, but nothing depended on it.

Now the loop closes:

work → wage → walk to the shop → buy a meal → live

A pip that cannot afford food starves. That is the first death in this simulation caused by economics rather than by a missing building.

The interesting part: a pip decides something

A pip gains an errand — somewhere it is going of its own accord, which outranks the commute. It is the first thing in this simulation a pip decides for itself; everything else about it was decided by a service or by the world.

Two choices worth reviewing:

It lives in the core. Same reason wander does: the browser predicts with this identical code, so a decision taken in a driver or a client would make prediction and truth disagree about what happens next.

It is not a plan. No goal queue, no stored intention. A pip is hungry or it is not, and it re-decides every tick — so a shop that raises its price, or a wage that arrives late, changes its mind with nothing to invalidate. A plan would need cancelling; a standing preference does not.

An errand is also physical, per invariant 5: the pip walks there, goes inside (taking a place, briefly), buys, and leaves in the same tick.

Prices reach the core the way capacity already does (ADR 0004) — the workplace owns the number, the gateway carries it on the registration intent, the core enforces it. A pip deciding inside a tick whether it can afford lunch cannot make a network call to ask.

The proof

One test runs 4,000 ticks of the whole loop: a pip is hired, paid in batches the way the gateway pays it, decides for itself when to stop working and eat, and is alive at the end with money left and total_money == 0 throughout. Two more pin the other side — a penniless pip starves standing next to food it cannot afford, and hunger outranks the commute while employment survives the errand.

need_deltas is deprecated, not reserved

Step 7 asked for reserved. That turns out to be impossible, and the ADR is wrong about it:

Previously present field "2" with name "need_deltas" on message "WorkResponse" was deleted.

make proto-lint runs buf breaking, and that rule is load-bearing — a topic with a week's retention holds messages that must still decode. So the field stays on the wire, marked deprecated, and dies by disuse. Nothing populates it.

The farm and the tavern no longer have any opinion about hunger. That is what makes the tavern's old food_per_tick = -1 — the number that starved everyone it employed — unrepresentable rather than merely fixed.

Test plan

  • cargo test -p sim — 32 pass, 3 new (survival over 4,000 ticks, starving next to unaffordable food, errand outranks commute)
  • mix test in tavern — 32 pass; its shift test now asserts a shift reports only what it produced and paid
  • go test ./... in farm, conformance, bank, world-gateway — pass; farm and conformance re-pinned onto wage and produce
  • cargo clippy --all-targets -- -D warnings clean, cargo fmt --check clean
  • buf lint / buf breaking clean — WorkplaceOffer, RegisterWorkplaceIntent.sells, Workplace.sells all additive
  • make gen regenerated for Go, TS and Elixir
  • Re-verified after merging main: all of the above still green
  • Not exercised: a live world. The loop is proven deterministically in the core, but the numbers (wage 6/tick, meal 50, +200 food) have never faced a running cluster — that is what to watch first after deploy

Balance, and what to watch

A meal restores 200 food; a working pip burns 2 a tick, so one meal buys ~100 ticks of work — 600 in wages against 50 at the till. Employment is comfortably survivable on purpose: if it were marginal, the only failure anyone would ever see is "everyone dies", and the interesting ones (a wage that cannot cover food, a shop nobody can reach) would be invisible underneath it.

Unemployment is now fatal, since nothing else feeds a pip. That is the intended scarcity, but it makes hiring throughput a survival mechanism rather than an optimisation — worth watching on the first live run.

🤖 Generated with Claude Code

teceer added 3 commits August 6, 2026 13:55
ADR 0006 says the core is authoritative for the moment of purchase and the
bank for history and solvency, but the implementation had both gating a
purchase: the bank committed the transfer, then the core decided whether to
apply it. When they disagreed — which happens whenever a wage credit has not
reached the core yet — the pip was charged and received nothing, and the
replica ended up believing a pip was richer than the ledger did. That is the
unsafe direction of error, and it was reachable through the ordinary
best-effort intent path.

Now exactly one side gates each movement, and it is the side that can see
what the other cannot:

- A purchase is gated by sim-core, settled against the pip's replica balance
  inside a tick, which is the only place that decision can be made without a
  network call. The core emits PurchaseMade and the bank books it off
  pipsim.economy.purchases.v1. The bank no longer publishes that event — two
  producers on one fact makes "who decided this" unanswerable from the log.
- Payroll stays gated by the bank, because "can this workplace afford its
  wage bill" is a solvency question and solvency is the ledger's.

Four invariants from the ADR follow from that shape or are enforced directly:

- Buying is physical (invariant 5). A pip must be inside the seller, checked
  in the core against `inside`. ADR 0004 made buildings places rather than
  addresses; money would have quietly reintroduced the model it replaced.
- Idempotency keys are per movement (invariant 3). A booked purchase is keyed
  by the event's own id, so two buys from one seller in a single tick are two
  bookings rather than one.
- A rejection is not a result (invariant 4). Refusals are no longer stored
  under the idempotency key, so a payer funded later in the same tick is
  answered from its balance rather than from a memory of being broke.
- Minting is a privilege carried by the transfer (invariant 6). The treasury's
  licence to go negative now requires TransferKind::Issuance rather than being
  inferred from the payer's identity.

Also: nothing issued money, so every account sat at zero and payroll rejected
for insufficient funds forever — the economy could not start. The gateway now
endows each workplace once, idempotent by balance. `Pip.balance` is exposed on
the snapshot so the gateway can answer "can this pip afford it" before
submitting an intent, and so a client can draw it.
This is ADR 0006 step 7, and the point the whole ADR was written for. Until
now a shift handed the worker food out of nothing: `farm.go` gave +5 a tick,
the tavern +3, and both files said in a comment that it was conjured. The
economy ran alongside that, decorative — money moved but nothing depended
on it.

Now the loop closes. Work pays a wage, the farm sells meals, and a hungry pip
walks to a shop and buys one. A pip that cannot afford food starves, which is
the first death in this simulation caused by economics rather than by a
missing building.

The behaviour lives in the core because every rule about how pips behave
does: the browser predicts with this same code, so a decision taken anywhere
else would make the client and the server disagree about what happens next.
Concretely a pip gains an *errand* — somewhere it is going of its own accord,
which outranks the commute. That is the first thing in this simulation a pip
decides for itself; everything else about it was decided by a service or by
the world.

Deliberately not a plan or a queue of goals. A pip is hungry or it is not, and
it re-decides every tick, so a shop that raises its price or a wage that
arrives late changes its mind with nothing to invalidate.

Prices reach the core the way capacity already does (ADR 0004): the workplace
owns the number, the gateway carries it on the registration intent, the core
enforces it. A pip deciding inside a tick whether it can afford lunch cannot
make a network call to ask.

`need_deltas` is deprecated rather than reserved. Step 7 asked for reserved
and that turns out to be impossible: `buf breaking` refuses the deletion, and
that rule is load-bearing, so the field stays on the wire and dies by disuse.
Nothing populates it. The farm and the tavern no longer have any opinion about
hunger — which is what makes the tavern's old food_per_tick = -1 bug
unrepresentable rather than merely fixed.

Covered by a test that runs 4,000 ticks of the full loop: a pip is hired, paid
in batches the way the gateway pays it, decides for itself when to stop
working and eat, and is still alive at the end with money in the bank and the
supply still closed.
@teceer
teceer merged commit 206b3e8 into main Aug 6, 2026
18 of 21 checks passed
@teceer
teceer deleted the feat/pips-buy-their-own-food branch August 6, 2026 15:43
teceer added a commit that referenced this pull request Aug 7, 2026
#15)

Formatting has landed on main and turned the lint job red three times now
(twice around #5, again in #14). The pattern never changes: rustfmt is
enforced in CI and run nowhere else, so it is found after landing, by
whoever pushes next.

`make hooks` points core.hooksPath at .githooks/, so the hooks are
versioned and change with the repo rather than being copied into .git once
and forgotten.

The hook checks formatting and nothing else. `make lint` runs clippy,
golangci-lint and credo across nine services; a hook people wait thirty
seconds for is a hook people pass --no-verify to, and then it protects
nothing. Each language is checked only when a file of that language is
staged, because the two slow starts are the BEAM and cargo and neither
should be paid for a change that did not touch them.

`make fmt` is the other half: whatever the hook rejects, it fixes.

Two deliberate omissions:

- No `buf format`. It reflows .proto comments onto a single space before
  the slashes, which destroys the column alignment several of those files
  use to keep a field's meaning readable next to its number. That is the
  author's style choice and a hook is the wrong place to overrule it —
  `make proto-lint` already covers the checks that catch real mistakes.
- No `mix format` for a service without a .formatter.exs, since broadcast
  is still an empty shell (ADR 0010).

Also fixes what gofmt immediately found: a stray blank line in driver.go,
from #10. golangci-lint does not have gofmt enabled here, so CI never saw
it.
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