fix: always update state on agent_end, archive previous state

The agent_end hook was skipping state extraction when CURRENT_STATE.md
already contained meaningful content. This meant once a state was written,
subsequent conversations never updated it — the next /new would always
recover stale data.

Now: every session end overwrites CURRENT_STATE.md with the latest
conversation state. Previous state is archived to STATE_ARCHIVE_<ts>.md.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-16 17:10:29 +10:00
co-authored by Claude Opus 4.6
parent ddc472b63f
commit 2fc42661cf
+12 -9
View File
@@ -238,20 +238,23 @@ const plugin = {
const statePath = resolveStatePath(ws);
if (!statePath) return;
// If there's already a meaningful state file, don't overwrite with
// auto-extracted content (agent or user wrote it explicitly)
const existing = readFile(statePath);
if (existing && buildSnapshot(existing)) {
log.info?.("[memory-continuity] Existing state is meaningful, skipping auto-extract");
// Always extract from the latest conversation — the whole point is to
// capture what happened *this* session so the next session can recover.
const newState = extractStateFromMessages(event?.messages);
if (!newState) {
log.info?.("[memory-continuity] No extractable state from conversation");
return;
}
// Extract from conversation messages
const newState = extractStateFromMessages(event?.messages);
if (!newState) return;
// Archive previous state if it exists
const existing = readFile(statePath);
if (existing) {
const archivePath = statePath.replace(/CURRENT_STATE\.md$/, `STATE_ARCHIVE_${Date.now()}.md`);
writeFile(archivePath, existing);
}
writeFile(statePath, newState);
log.info?.("[memory-continuity] Auto-extracted state from conversation");
log.info?.("[memory-continuity] Updated state from conversation");
}, { priority: 90 }); // low priority, run after other hooks
// ------------------------------------------------------------------