Fix the replay of a lock-free ring buffer

Review the AI's codeMedium20 minAnthropic style

Your task

You asked an agent for a lock-free ring buffer in Rust: one writer thread and one reader thread walk around a fixed array of slots. Each re-prompt only shuffles its atomic orderings. So you check its index logic first, with a single-threaded replay.

An assistant wrote replay_ring_buffer(capacity, op_lines). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • capacity: how many items the buffer can hold. An integer, and it can be 0 or negative.
  • op_lines: the operations, in order. Each is a string such as "push|a", "pop" or "len".
  • Return a list of strings: one result for each operation that counts, in order.

Rules the tests check:

  • The buffer starts empty and holds at most capacity items. A capacity of 0 or less holds nothing.
  • "push|<value>": the value is everything after the first |. It can be empty and can contain more | characters. If the buffer already holds capacity items, return "full" and change nothing. An item the reader has not taken is never overwritten. Otherwise store the value and return "ok".
  • "pop": if the buffer is empty, return "empty". Otherwise remove the oldest stored value and return it. Items come out in the order they went in.
  • "len": return how many items the buffer holds now, as a decimal string such as "2".
  • Op names are exact and case sensitive. A push must start with exactly push|. Any other line, such as "POP", "push" or "pop ", is skipped and adds nothing to the output.
  • No operations returns an empty list.

Examples

replay_ring_buffer(capacity: int, op_lines: list[str]) -> list[str]
Example 1
Inputreplay_ring_buffer(2, ["push|a", "push|b", "push|c", "pop", "push|c", "pop", "pop", "pop"])
Output["ok", "ok", "full", "a", "ok", "b", "c", "empty"]
Example 2
Inputreplay_ring_buffer(1, ["pop", "push|x", "push|y", "pop", "pop"])
Output["empty", "ok", "full", "x", "empty"]