garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/adr/036-input-sequenced-client-prediction.md

raw

ADR-036: Input-Sequenced Client Prediction and Reconciliation


Metadata:


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:

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:

  1. Wire (cartopolis_core) — a new revision appended to PROTOCOL_HISTORY (v12; the number is derived from that log, never written by hand):

    • ClientIntent gains sequence: u32 — monotonic per connection, and dt_ms: u16, the client frame duration the intent covers.
    • WorldSnapshot gains last_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)].
  2. Sim (cartopolis_simulator) — SimWorld records the highest sequence applied per avatar and integrates each intent over its own dt_ms (clamped to a sane ceiling so a lying client can't buy speed), instead of holding one velocity until the next tick. net.rs stamps last_input_sequence per connection when encoding the snapshot — so the broadcast becomes a per-connection encode for avatars, or the field is carried in the local-avatar AvatarStateDto.

  3. Client (cartopolis) — a ring buffer of (sequence, velocity, dt) for inputs sent and not yet acknowledged. On snapshot: drop everything at or below last_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.

  4. Rate limits — MIN_INTENT_INTERVAL must coalesce (keep the newest pending intent and apply it when the window opens) rather than continue past it: with sequence numbers a dropped intent leaves a permanent gap the client will keep replaying.

Rationale

Primary Reasoning:

  1. 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.
  2. The pieces are already in place — the envelope carries a request_id, the client already mirrors the sim's apply_intent scaling to predict, and the snapshot already goes out per tick. Sequencing formalises what the code is informally assuming.
  3. 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:

AlternativeProsConsRejected Because
Age-extrapolated reconcile target only (shipped)No protocol change; kills the 20 Hz sawtoothGuesses with the current velocity; error grows with RTT; can't distinguish drift from correctionNecessary but not sufficient — keep it as the smoothing layer under replay
Client-authoritative positionTrivially smooth, zero perceived latencyAbandons ADR-016's authority model; unfixable cheatingContradicts the server-authoritative decision
Interpolate in the past (render the avatar behind the snapshot stream)Simple, no prediction at allAdds a full RTT + buffer of input lag to the local avatarAcceptable for remote avatars (what smooth_remote_avatars does), not for the one the player drives
Raise tick_hz to hide itNo design workLinear cost in CPU and bandwidth for every connected client; doesn't fix RTTTreats the symptom, and the sawtooth was never really about tick rate

Consequences

Positive:

Negative:

Related