Primary finding
State file rollback uses non-existent .bak and can cause data loss on write failure
- skills/fleet-state/SKILL.md:291-306
- skills/fleet-state/SKILL.md:308
The write flow moves the new file into place before validation, then attempts to restore from a .bak that is never created in this step (and not created anywhere else in the document). If jq empty fails (e.g., interrupted write, disk error), the restore path will also fail due to missing backup, leaving a corrupt or empty state file and losing prior history.
Recommendation
- Create a backup before overwriting: if [ -f memory/topics/fleet-state.json ]; then cp memory/topics/fleet-state.json memory/topics/fleet-state.json.bak; fi - Prefer atomic update: write to $TMP, validate ($ jq empty "$TMP"), then move into place (mv "$TMP" memory/topics/fleet-state.json). Only update the on-disk file after successful validation. - If validation after move is kept, ensure the backup is created before mv and the error path logs FLEET_STATE_STATE_CORRUPT and restores from the created .bak.