fix(openclaw): dream verification, failed-turn cleanup, atomic lock

1. Write-only verification: only count memory_store, memory_update,
   memory_forget, memory_delete_all as proof of dream execution.
   Removed memory_list (read-only, orient-only pass should not count).
   Scan only the last assistant message (this turn), not the full
   session snapshot, to avoid matching earlier turns' tool calls.

2. Failed-turn cleanup: agent_end now handles dreamSessionId cleanup
   BEFORE the !event.success early return. A failed/aborted turn
   after lock acquisition releases the lock immediately instead of
   waiting for the 1-hour stale timeout.

3. Atomic lock: replaced read-then-write with exclusive create flag
   (wx). Two racing processes: only one succeeds, other gets EEXIST.
   Stale locks are unlinked before the exclusive create attempt.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
chaithanyak42
2026-04-02 13:14:41 +05:30
co-authored by Claude Opus 4.6
parent 67afcd90d9
commit 7e9824e394
2 changed files with 42 additions and 25 deletions
+11 -6
View File
@@ -150,18 +150,23 @@ export function acquireDreamLock(stateDir: string): boolean {
const lock = JSON.parse(raw) as DreamLock;
const age = Date.now() - lock.startedAt;
if (age < LOCK_STALE_MS) {
// Lock is held and not stale
return false;
return false; // Held and not stale
}
// Stale lock, reclaim
// Stale lock — remove it before attempting exclusive create
try { fs.unlinkSync(lp); } catch { /* race ok */ }
} catch {
// No lock file, proceed
}
// Write lock
// Atomic create with exclusive flag (wx). If two processes race,
// only one succeeds. The other gets EEXIST.
const lock: DreamLock = { pid: process.pid, startedAt: Date.now() };
fs.writeFileSync(lp, JSON.stringify(lock));
return true;
try {
fs.writeFileSync(lp, JSON.stringify(lock), { flag: "wx" });
return true;
} catch {
return false; // Lost race
}
}
/**