claude-memory/graph/fixes/fix-race-condition-in-cognitive-memory-save-state-loses-last-59268a.md
2026-02-28 00:03:28 -06:00

42 lines
1.9 KiB
Markdown

---
id: 59268a95-4712-48b4-bccb-6171c7885301
type: fix
title: "Fix: Race condition in cognitive-memory _save_state loses last_reflection timestamp when daily+weekly timers collide"
tags: [cognitive-memory, fix, race-condition, systemd, state-management, atomicity]
importance: 0.8
confidence: 0.8
created: "2026-02-20T06:47:07.701221+00:00"
updated: "2026-02-28T06:03:28.384589+00:00"
relations:
- target: b33e2316-6313-4ed9-b95a-3a469b820101
type: CAUSES
direction: outgoing
strength: 0.9
edge_id: 6f7d12e4-b494-4aea-9be9-f43a5492719c
- target: 89f32ef3-2d87-4b8c-aa6e-ce5a7f60566c
type: RELATED_TO
direction: incoming
strength: 0.7
edge_id: 28b233ae-7bb5-441d-8a95-44617fb82618
---
# Race Condition in _save_state Corrupts last_reflection Timestamp
## Symptom
REFLECTION.md showed "Last reflection: never" despite the weekly reflect timer having run successfully.
## Root Cause
Two concurrent issues:
1. **Timer collision**: The daily timer (decay) and weekly timer (reflect) could fire simultaneously. `OnCalendar=weekly` resolves to Mon midnight, which overlaps with `OnCalendar=daily` on Mondays. Both processes load and save `_state.json` concurrently.
2. **Non-atomic writes with silent failure**: `_save_state()` would overwrite `_state.json` without first merging existing state, so decay's write could clobber the `last_reflection` key written by reflect. Additionally, `_load_state()` silently caught `JSONDecodeError` and returned an empty default dict, permanently losing the key on any write collision.
## Fix
1. `_save_state()` now merges with existing on-disk state before writing (read-modify-write pattern).
2. Writes use atomic `tempfile + os.replace()` to prevent partial writes.
3. Weekly timer changed from `OnCalendar=weekly` to `OnCalendar=Sun *-*-* 02:00:00` to avoid midnight collision with the daily timer.
## Commit
Commit `7f120f8` in the cognitive-memory repo.