# Outbound syncs round: design

Written and committed before any run of the simulation. The frozen protocol is evals/outbound-protocol.json; it pins the
hash of this file and of the code below. No model is called anywhere in this round, and nothing costs money.

## What this round adds

The main page asks what an AI model needs before it answers from company tables. The connectors round asks what goes
wrong when an agent reads business data through an API that pages, rate-limits, times out and fails part-way. Both read.
This round writes: a warehouse pushes computed fields back into the tools people work in (often called reverse ETL), and
people and assistants then read those tools. If the sync leaves a stale health score or a missing risk alert there, every
answer built on it is wrong before any model touches it. No published page measures that.

## Question

When a warehouse pushes computed fields (account health, segment, a risk flag and the assigned account manager) back into
a CRM-style record store and a chat-style message sink, which sync designs leave the destination matching the warehouse,
and how often do retries, duplicate deliveries, partial failures, late arrivals and edits made by people leave it wrong
or stale? Which checks a team can run would show it? No direction is claimed in advance.

## The world (outbound/world.py, explicitly synthetic, from a seed)

- A warehouse table of 1,000 accounts at the start, 3 new accounts each cycle, 48 hourly sync cycles (two synthetic days).
  At the start both destinations are in step: the record store matches the warehouse, every open risk episode already
  has its message, and each design's own state agrees with them.
  Every account has a row version that grows on any change, and each field records the cycle it last changed.
- Fields the warehouse owns: health score (a whole number from 0 to 100), segment (one of four values) and risk flag
  (on when health is below 40, so it follows the score). Starting scores are drawn around 70 (standard deviation 15).
  Each cycle, 8% of accounts get a new health score (the old one plus a normal step with standard deviation 12 and a
  pull a fifth of the way back toward 70, kept within 0 to 100) and 0.5% a new segment.
- From cycle 12, 30% of segment changes go to a fifth value the record store's list of allowed values lacks until cycle 36,
  when an administrator adds it.
- A field both sides edit: the assigned account manager, from a pool of 40. Each cycle 0.5% of accounts are reassigned.
  At cycles 8, 20 and 32 a new manager joins and takes a third of that cycle's reassignments onwards; the record store
  knows a new manager only 6 cycles later.
- People edit the record store directly (a separate seeded stream, the same in every design): each cycle, 0.3% of
  accounts get a new manager from a sales lead, and 0.2% a typed-in health score, at a random second within the hour.

## The two destinations (outbound/destinations.py)

Both are fakes built from behaviour categories that CRM and chat products document. No product is named or imitated
field by field, and no test ran against a real one.

The CRM-style record store:
- upsert by external id, at most 100 rows a request; the reply is 200 with a result per row (partial batch success):
  ok, rejected by validation, or a transient row error (a locked record);
- validation rejects the whole row when the segment is not an allowed value or the account manager is not yet a known
  user;
- batch reads by external id, at most 200 rows a request; each field comes back with its value, when it last changed and
  whether the sync or a person changed it (field history);
- a daily allowance of requests for the sync's user, reset every 24 cycles; past it, every request gets 429 with a
  Retry-After that runs past the cycle, so the sync stops for that cycle;
- idempotency keys: a request whose key the store saw in the last 24 cycles applies nothing and returns the first reply.

The chat-style message sink:
- append only: a message can be posted, never edited or deleted;
- at most one post a second (429 with Retry-After of 1 second);
- a history read returns up to 100 messages a request; a message shows in the history only after a delay drawn from
  0 to 120 seconds;
- idempotency keys, honoured for 24 cycles, as above.

## Faults (outbound/faults.py, a seeded injector)

Every write request (an upsert or a post) draws one outcome, at these base rates:
- times out before it lands: 2% (nothing applied; the client waits 30 seconds and sees a timeout);
- times out after it lands: 2% (applied; the client sees a timeout);
- server error: 2% (nothing applied; 500);
- duplicate delivery: 1% (applied twice at once);
- late copy: 1% (the client sees a timeout; the request is held and applied 1 to 2 cycles later, after whatever was
  written in between, so an older value can land over a newer one);
- otherwise delivered once.

Each row of a delivered upsert also has a 0.5% chance of a transient row error. Reads time out or get a 500 at 2% each
and apply nothing. Four fault levels: none (no transport faults, no edits by people, no allowance; validation stays,
because it comes from the data), low (every rate halved), base and high (every rate doubled). The daily allowance is 300
requests in the base case, with 150 and 600 as the sweep. All rates and the allowance are assumed, not measured.

Every design uses the same transport policy: up to 3 retries after a timeout or a 500, waiting 2, 4 and 8 seconds, and
it honours Retry-After. A request takes 1 second.

## The sync designs (the arms, outbound/designs.py)

Each sync starts 60 seconds into its cycle, after the warehouse has recomputed. A row is always sent whole (all four
fields), as most sync tools send a changed row. Risk alerts go to the message sink: one message per episode, an episode
being a run of cycles with the flag on.

1. **Full overwrite.** Every cycle, upsert every account. Alerts: post when the warehouse flag went from off to on since
   the previous cycle's warehouse snapshot. It keeps no record of what it sent, so an alert whose post failed is not
   tried again.
2. **Diff against the last sent state.** Keep the values last confirmed per row; upsert rows whose warehouse values
   differ from them. A row is marked sent only when its own result is ok; rejected, failed and unknown rows are tried
   again next cycle. Alerts: post every open episode not yet marked sent.
3. **Diff against the destination (read before write).** Every cycle, read every account from the record store and
   upsert the rows that differ from the warehouse. Alerts: read the message history and post every open episode whose
   message is not in it; after a timeout, read the history again before retrying.
4. **Idempotency keys plus a sync ledger.** As design 2, but the ledger keeps the row version confirmed and every write
   carries a key built from the account and the version (or the episode, for alerts), so a retry, a duplicate delivery or
   a late copy of the same write applies once. A rejected row is parked until its warehouse version changes, to save
   requests.
5. **Ledger with keys, plus a read-back every sixth cycle.** Design 4, and at cycles 6, 12, 18 and so on it reads every
   account, re-sends any row that differs from the warehouse (with a fresh key) and un-parks rejected rows. This is design
   3's read used as a repair step a sixth as often; it is declared here, before any run, as the combination the other
   four suggest.

## Who wins the account manager field

The main grid uses "warehouse wins" for every design: the sync writes the warehouse's manager whenever it writes the row.
A separate set of runs, at the base fault level and allowance, crosses designs 3 and 5 with three rules:
- **warehouse wins**: as above;
- **destination wins**: once a person changed the field, the sync never writes it for that account again;
- **last write with versions**: the sync writes the field only when the warehouse changed it after the person's last
  edit, read from the field history.
Designs 1, 2 and 4 never read the destination, so they can only run "warehouse wins". Design 5 sees edits only on its
read-back cycles and remembers them in its ledger.

## Measures, against the known truth (outbound/measure.py)

The simulation knows the warehouse at every moment, every value in each destination and who wrote it. All measures are
taken at the end of each cycle, just before the warehouse recomputes.

1. **Records wrong after a cycle.** An account is wrong when the record store lacks it or its health score, segment or
   risk flag differs from the warehouse. Per run: the mean share over the 48 cycles, and the share after the last cycle.
2. **Stale fields and how long they stay stale.** Each of those three fields, per account, that differs at a cycle end is
   a stale field-cycle; a run of them is an episode, with its length in cycles. Each stale field-cycle gets one cause, in
   this order: rejected by validation (the warehouse row is one the store cannot accept at that moment), a person's edit
   (a person wrote the value the store holds), an older write that landed late (a late copy wrote it), the allowance used
   up (the sync was refused for the allowance that cycle and the row had not landed), otherwise not landed (the write
   failed after its retries, or the design did not send it).
3. **Alerts.** Duplicate messages (messages for an episode beyond the first), missed alerts (episodes with no message by
   the end of the run) and late alerts (first message in a later cycle than the episode's first).
4. **The account manager field.** Edits by people that the sync overwrote while the warehouse had not changed the field
   since before the edit; of those, races (the sync read the account before the edit, in the same cycle); warehouse
   reassignments lost (made after the account's last edit by a person, and not in the store at the end of the run).
5. **Requests spent.** The sync's requests per day (reads, writes and retries, including refused ones), 429 replies and
   cycles cut short by the allowance.

## Checks a team can run (outbound/checks.py)

They read with a separate read-only user whose requests do not count against the sync's allowance; their cost is
reported on its own. Each is scored on every run.

1. **Reconciliation report**, at the end of cycles 6, 12, 18 and so on: read every account and list each field (all four)
   that differs from the warehouse. By construction it lists every difference open when it runs; what it can miss is a
   difference that opens and closes between two reports, and what it costs is reads.
2. **Duplicate detector**, at the end of every cycle: read the message history and flag a message when the same account
   already has a message from the previous 6 cycles. An alert names only the account, as a person would read it, so a
   real second episode within 6 cycles is flagged too; those are counted as false alarms.
3. **Drift alert**, at the end of every cycle: one aggregate request (accounts per segment, flags on, total health
   score), firing when any count differs from the warehouse's by more than 5 accounts or the total by more than 0.5%.
   Small or offsetting differences pass under it.

Scored per design at the base level and allowance, over all 10 seeds: for each cause, the share of stale-field episodes
open while the check fired or listed them; for the duplicate detector, the share of duplicate messages flagged and the
false alarms.

## Runs and analysis (outbound/run.py, outbound/analyze.py)

- Seeds 1 to 10 for every condition. A seed fixes the warehouse's changes and the edits by people, the same in every
  design, so designs are compared on the same history; transport draws come from their own stream, and designs that send
  more requests draw more faults.
- Conditions: the main grid, 5 designs at the low, base and high fault levels with the base allowance (15); the allowance
  sweep, 5 designs at 150 and 600 requests a day with base faults (10); the control, 5 designs with no faults (5); the
  ownership runs, designs 3 and 5 under the other two rules at base (4; their "warehouse wins" runs are the main grid's
  base runs). 34 conditions, 340 runs.
- Each design is reported on its own, never pooled across designs: the median and the range over the 10 seeds. Seeds of
  one condition are pooled only for the checks table and for episode lengths.
- A difference between two designs is called clear when it has the same sign in all 10 seeds; otherwise it is reported
  as one that cannot be told apart at this size.
- The worked example on the page follows one account through designs 2, 3 and 5 at the base level, seed 1: the first
  account whose health score a person types in.
- Outputs: outbound/results/runs.csv (one row per run, every measure), outbound/results/episodes.json (every stale-field
  episode, counted by cause, length and which check saw it), outbound/results/trace.json (the worked example), then
  outbound/results/summary.json (medians, ranges and paired differences) and outbound/results/checks.json (the checks
  table) from `python -B -m outbound.analyze`. The results are recomputed from the seeds by `python -B -m outbound.run`,
  so the tests rebuild them without a key and compare them with the committed files.

## Limits known before running

- A self-test: I wrote the destinations, the fault injector, the five designs and the three checks. The faults are the
  ones the designs were built to face, and the checks are scored on faults written by the same hand.
- Many outcomes follow from a design's mechanism: a design that never reads the destination cannot see an edit made
  there; a sink that honours keys drops a repeated write. The simulation sizes those effects under assumed rates; the
  rates, the allowance and the delays are assumptions, swept where the design says so, not measurements of any product.
- One world size (about 1,000 accounts, 48 cycles), 10 seeds per condition.
- No production sync was operated for this study, and no real product was tested.
