Day 65 — Persistence & Storage: The Complete AppendOnlyCSVWriter
This workspace delivers a trade-log writer that survives the two failure modes the free post named but didn’t fix: concurrent coroutines racing for the same file, and a crash leaving a corrupted trailing row that the loader must detect rather than trust.
What you’re building
AppendOnlyCSVWriterand its companionCorruptionQuarantineboundary. The writer serializes every append behind anasyncio.Lock, builds each row as one complete string (fields plus newline) before writing it, and callsos.fsync()after every write so a row is durable on disk before the coroutine returns. On the read side,load_fills()walks the file once, and any trailing line that doesn’t end in\n— the signature of a crash mid-write — is pulled out into a quarantine list instead of being handed to the caller as valid data. This sits directly downstream of Day 64’sPathStorage, which resolves and creates thefills/directory this writer appends into, and directly upstream ofFillAuditLog, which now only ever sees rows that passed the newline check.The free post showed the shape of a safe append — build the row as one string, write it, flush it. This workspace is what that shape grows into once it has to survive the conditions a live engine actually runs under: multiple coroutines appending fills from different symbols’ WebSocket streams in the same event-loop tick, a process that gets killed without warning, and a loader that has to assume every file it opens might contain exactly one bad row from the last time that happened.



