fix(data): 四處情緒/sync 狀態的讀改寫還沒進鎖,等於繞過了上次的修正

B4 加了 updateJson/updateEmotion,但這幾個呼叫點還是「loadEmotion → 改 →
writeJson」的裸讀改寫,鎖形同不存在:

- sleep 的 emotion-decay、remember --emotion(每輪對話的熱路徑)、
  Stop hook 與 SessionEnd hook 的時間衰減,四處都改走 pl.updateEmotion()。
  衰減與 delta 的算法一個字都沒動,只是把讀與寫收進同一把鎖裡。
- sync.json 同理:背景 sync push 跟前景指令會同時寫它,兩邊撞上時後寫的會把
  pushed_at/overwrites 整段蓋掉——覆蓋紀錄就這樣安靜地消失。新增
  updateSyncState()(帶鎖),六處 loadSyncState + saveSyncState 成對使用全部
  改過去;補預設欄位的邏輯抽成 normalizeSyncState() 給兩邊共用。
  saveSyncState 沒有呼叫端了,直接移掉,免得下次又有人拿它裸寫。

sync.json 的欄位格式與 overwrites 留 10 筆的行為都沒變。

selftest 448 項(+1):新增「並行 8 次 remember --emotion 跟循序結果一樣」,
把對話熱路徑的情緒寫入也蓋進競態測試——原本只蓋到 emotion --apply。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 14:35:06 +00:00
co-authored by Claude Opus 5
parent fa4aa8b787
commit bc571cf37f
5 changed files with 79 additions and 49 deletions
+2 -4
View File
@@ -541,8 +541,7 @@ commands.sleep = async ({ flags }) => {
await run("prune-short-term", () => pl.pruneShortTermDetail(slug));
await run("archive-threads", () => pl.archiveStaleThreads(slug));
await run("emotion-decay", () => {
const state = pl.decayEmotionBy(pl.loadEmotion(slug), pl.SLEEP_DECAY_MINUTES);
pl.writeJson(pl.emotionPath(slug), state);
pl.updateEmotion(slug, (s) => pl.decayEmotionBy(s, pl.SLEEP_DECAY_MINUTES));
});
await run("reindex", () => pl.rebuildIndex(slug));
await run("trim-said", () => pl.trimSaid(slug));
@@ -744,8 +743,7 @@ commands.remember = ({ flags }) => {
pl.rememberShort(slug, entry);
const kept = pl.pruneShortTerm(slug);
if (Object.keys(entry.emotion_deltas).length) {
const state = pl.applyEmotion(pl.loadEmotion(slug), entry.emotion_deltas, text.slice(0, 80));
pl.writeJson(pl.emotionPath(slug), state);
const state = pl.updateEmotion(slug, (s) => pl.applyEmotion(s, entry.emotion_deltas, text.slice(0, 80)));
pl.appendJsonl(pl.journalPath(slug), {
ts: pl.nowIso(), kind: "emotion", trigger: text.slice(0, 120),
deltas: entry.emotion_deltas, levels: state.levels, mood: pl.mood(state),