[bug] MemoryRetriever removeLockIfToken leaves the owner's rebuild lock behind when…
What happened
MemoryRetriever removeLockIfToken leaves the owner's rebuild lock behind when file mtime is a fraction of a millisecond ahead of Date.now()
Summary
Component: Cortex retrieval index, LIFEOS/TOOLS/MemoryRetriever.ts (removeLockIfToken, line 1195 on current main)
What happens: the owner of a rebuild lock often cannot release it. releaseRebuildLock() calls removeLockIfToken(lock.path, lock.token) with the default minAgeMs = 0, but the age check still runs: Date.now() - current.mtimeMs < minAgeMs. fs.statSync().mtimeMs is fractional and on APFS is often a fraction of a millisecond ahead of the integer Date.now(), so the difference is negative, < 0 is true, and the function returns false without unlinking. The lock is left behind until it goes stale.
Repro: acquire and immediately release a lock through the real functions 1000 times. On one install (macOS, APFS): 979 of 1000 locks were left on disk; the mtime was 0.46–0.92 ms ahead of Date.now().
Observed effect: after a rebuild, the next search ran against the previous index generation (generation 0, a leftover lock, and 1 retrieved entry in the hook instead of 3) until the stale lock expired.
Suggested fix: apply the age check only when a positive minimum is given, keeping the token check as it is:
if (!current || current.token !== token || (minAgeMs > 0 && Date.now() - current.mtimeMs < minAgeMs)) return false;
With this change the same 1000-run probe leaves 0 locks; stale takeover (REBUILD_LOCK_STALE_MS, LOCK_BREAKER_STALE_MS) and the foreign-token guard behave as before.
Details
- Kind: bug
- LifeOS version: 8.761.0
Filed with the LifeOS /feedback command. The reporter approved this exact text before it was sent; paths, secrets and personal details were stripped locally.
[!NOTE] 🤖 Kai (AI assistant) responding on behalf of Daniel Miessler
Thanks for the report. This is fixed in our tree today (41b5f8887b), and you're credited at the fix site and as a co-author on the commit. It ships in the next public release. Closing.