ADR-036: Input-Sequenced Client Prediction and Reconciliation
Metadata:
- ID: ADR-036
- Status: Proposed
- Date: 2026-07-23
- Tags: [networking, protocol, prediction, avatar, latency, replication]
- Decision Type: architecture_pattern
- Aligns with: [G-01, P-02]
- Roadmap tier: P1
- Doc resolution: R2
- Related: [ADR-008, ADR-009, ADR-012, ADR-016]
- Supersedes: []
- Superseded By: []
Context
Problem: the client is server-authoritative for avatar movement (ADR-016): it
sends ClientIntent, the sim integrates at tick_hz (20 Hz) and broadcasts
WorldSnapshot. The client predicts locally between snapshots, but has no way to
know which of its inputs a given snapshot already accounts for. Reconciliation
therefore compares a prediction that has consumed inputs up to now against an
authoritative position that has consumed inputs up to now − (RTT + tick phase).
The first-order symptom — reconciling against the raw 20 Hz snapshot, so the
correction fought the prediction on every frame between ticks and produced a
20 Hz sawtooth in apparent speed — was fixed without a protocol change by
extrapolating the reconcile target by its own age
(avatar::smooth_online_avatar_display). That removes the pulsing but not the
underlying error: the extrapolation guesses with the current velocity, so every
direction change, wall slide, or server-side correction still resolves as a blend
rather than a replay. On loopback (solo mode) the residual is small; over a real
network it grows linearly with RTT.
Requirements:
- The client's displayed avatar responds to input in the same frame, at any RTT.
- A server correction (collision, teleport, anti-cheat clamp) converges without a visible slide, and without discarding inputs the player has made since.
- No new authority on the client: the sim stays authoritative (ADR-016).
- Per-connection rate limits (ADR-012) must not silently drop an input edge.
Decision
Number client inputs, have the sim echo the last one it applied, and reconcile by replaying the unacknowledged tail from the authoritative state.
Approach:
-
Wire (
cartopolis_core) — a new revision appended toPROTOCOL_HISTORY(v12; the number is derived from that log, never written by hand):ClientIntentgainssequence: u32— monotonic per connection, anddt_ms: u16, the client frame duration the intent covers.WorldSnapshotgainslast_input_sequence: u32(per receiving client; 0 before the first intent). The ADR-009 envelope is unchanged: no new message kinds, two added postcard fields, both#[serde(default)].
-
Sim (
cartopolis_simulator) —SimWorldrecords the highestsequenceapplied per avatar and integrates each intent over its owndt_ms(clamped to a sane ceiling so a lying client can't buy speed), instead of holding one velocity until the next tick.net.rsstampslast_input_sequenceper connection when encoding the snapshot — so the broadcast becomes a per-connection encode for avatars, or the field is carried in the local-avatarAvatarStateDto. -
Client (
cartopolis) — a ring buffer of(sequence, velocity, dt)for inputs sent and not yet acknowledged. On snapshot: drop everything at or belowlast_input_sequence, reset the predicted position to the authoritative one, then re-integrate the remaining tail. The result is the position the sim will report once it has caught up, so the residual error is real divergence only, and the existing smoothing handles it. -
Rate limits —
MIN_INTENT_INTERVALmust coalesce (keep the newest pending intent and apply it when the window opens) rather thancontinuepast it: with sequence numbers a dropped intent leaves a permanent gap the client will keep replaying.
Rationale
Primary Reasoning:
- Replay is the only reconciliation that is correct rather than plausible: it reproduces the sim's own integration over exactly the inputs the sim has not seen yet, so at steady state the error is zero regardless of RTT.
- The pieces are already in place — the envelope carries a
request_id, the client already mirrors the sim'sapply_intentscaling to predict, and the snapshot already goes out per tick. Sequencing formalises what the code is informally assuming. - It makes server corrections expressible. Today a sim-side clamp is indistinguishable from prediction drift; after this, a mismatch that survives replay is by definition a correction and can be snapped and logged.
Alternatives Considered:
| Alternative | Pros | Cons | Rejected Because |
|---|---|---|---|
| Age-extrapolated reconcile target only (shipped) | No protocol change; kills the 20 Hz sawtooth | Guesses with the current velocity; error grows with RTT; can't distinguish drift from correction | Necessary but not sufficient — keep it as the smoothing layer under replay |
| Client-authoritative position | Trivially smooth, zero perceived latency | Abandons ADR-016's authority model; unfixable cheating | Contradicts the server-authoritative decision |
| Interpolate in the past (render the avatar behind the snapshot stream) | Simple, no prediction at all | Adds a full RTT + buffer of input lag to the local avatar | Acceptable for remote avatars (what smooth_remote_avatars does), not for the one the player drives |
Raise tick_hz to hide it | No design work | Linear cost in CPU and bandwidth for every connected client; doesn't fix RTT | Treats the symptom, and the sawtooth was never really about tick rate |
Consequences
Positive:
- Local avatar responds within a frame at any RTT; corrections converge exactly.
- Wall sliding (client-side collision, no building geometry in the sim) stops being a source of permanent predicted/authoritative divergence.
- A measurable divergence metric falls out for free — useful in
--dump-state.
Negative:
- Breaking wire change:
PROTOCOL_VERSION11 → 12, anddecode_app_framerejects mismatches outright, so client and sim must be deployed together. Mitigation: solo mode embeds the sim in-process, so only external--connectdeployments are affected. - The snapshot becomes per-connection for the local-avatar field, weakening the single-encode broadcast. Mitigation: encode the shared body once and stamp the per-client field, or carry the ack in a small separate message.
- The client must keep the replay integration byte-for-byte in step with
SimWorld::apply_intent/step. Mitigation: move the scaling constants intocartopolis_coreso both sides read one definition — they are duplicated today (network::SIM_WALK_SPEEDmirrorsstate::WALK_SPEEDby comment only).