Skip to content

< CollectionsA multi-agent research system · 5 of 9 ·93 · Level 05, Agents

Share a Scratchpad Between Agents Without Losing Writes

medium · implement · Agents

Two agents share a scratchpad. Both read count, both add one, both write, and the pad says 1. Implement Scratchpad with versions, so a stale write is refused instead of silently winning, and update, which retries until its write lands.

  1. read(key) returns (value, version). A key never written is (None, 0).
  2. write(key, value, expected_version) succeeds only when expected_version equals the key's current version; it stores the value, bumps the version by one and returns the new version. Otherwise it raises StaleWrite and changes nothing.
  3. history(key) is every value ever stored for the key, in order. Empty for an unknown key.
  4. Versions are per key: writing a does not move b.
  5. update(pad, key, fn, tries=5) reads, computes fn(value), and writes with the version it read. On StaleWrite it reads again and recomputes from the newer value, up to tries attempts in total; if the last one is stale too, it raises StaleWrite. It returns the value it stored.

The tests include a pad that lets another agent sneak a write in between your read and your write. A correct update applies fn to that agent's value, not to the one it first read.