Fix the rollback replay in client prediction

Review the AI's codeEasy10 min

Your task

Your multiplayer game predicts the player's movement on the client and corrects it when the server answers. An agent wrote the reconciliation step. It feels smooth in a local playtest, but bugs here only show under real latency, so you test the step on its own.

An assistant wrote reconcile_positions(input_deltas, acked_sequence, server_position). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • input_deltas: the client's buffered movement inputs, in order. input_deltas[i] is the input with sequence number i + 1, so the first input is number 1.
  • acked_sequence: the server has applied every input up to and including this sequence number.
  • server_position: the position the server computed after those inputs.
  • Return the timeline: the server position, then the position after each replayed input.

Rules the tests check:

  • An acked_sequence below 0 counts as 0. One above the number of inputs counts as that number.
  • Start from server_position. Replay, in order, every input whose sequence number is greater than acked_sequence, adding its delta to the position.
  • The result starts with server_position and then holds the position after each replayed input.

Examples

reconcile_positions(input_deltas: list[int], acked_sequence: int, server_position: int) -> list[int]
Example 1
Inputreconcile_positions([1, 2, 3, 4], 2, 10)
Output[10, 13, 17]
Example 2
Inputreconcile_positions([1, 2, 3, 4], 4, 10)
Output[10]
Example 3
Inputreconcile_positions([5, -2, 7], 0, 100)
Output[100, 105, 103, 110]