Pitfall Persistence dakka

Screenshot Fifo Lexicographic Sort

pitfallscreenshotfilesystemRENDER

What Happened

The Dakka screenshot FIFO pruner sorted filenames lexicographically to find the oldest frames to delete. Frame 142 sorted before frame 42 : 1 < 4 at the first differing character. The pruner deleted recent frames and kept ancient ones. Debug sessions lost evidence.

Root Cause

See definitions/root-cause-analysis for the analytical framework. Specific cause: lexicographic (string) comparison works character-by-character from left to right. Numbers embedded in strings don’t compare numerically unless they have equal digit length. "9" > "10" lexicographically because '9' > '1'. Without zero-padding, any frame number crossing a power-of-10 boundary (10, 100, 1000) breaks the sort invariant.

How to Avoid

Always zero-pad frame numbers to at least 5 digits (frame_00042.png). This ensures lexicographic order matches chronological order for up to 99,999 frames. At 1 frame per second, that covers ~27 hours of continuous capture : more than enough for any debug session.

const filename = `frame_${String(frameNumber).padStart(5, '0')}.png`;
// frame_00042.png, frame_00142.png :  sorts correctly

If you inherit un-padded filenames from an older system, sort with a numeric comparator instead of relying on the default string sort:

files.sort((a, b) => {
  const numA = parseInt(a.match(/(\d+)/)[1]);
  const numB = parseInt(b.match(/(\d+)/)[1]);
  return numA - numB;
});