Share a Scratchpad Between Agents Without Losing Writes
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.
read(key)returns(value, version). A key never written is(None, 0).write(key, value, expected_version)succeeds only whenexpected_versionequals the key's current version; it stores the value, bumps the version by one and returns the new version. Otherwise it raisesStaleWriteand changes nothing.history(key)is every value ever stored for the key, in order. Empty for an unknown key.- Versions are per key: writing
adoes not moveb. update(pad, key, fn, tries=5)reads, computesfn(value), and writes with the version it read. OnStaleWriteit reads again and recomputes from the newer value, up totriesattempts in total; if the last one is stale too, it raisesStaleWrite. 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.