fix: restore pre-existing content on ENOENT during late-block retraction

When a slug collision exists (preExistingContent !== null) and the file
is externally deleted before the post-hook drain, the ENOENT handler
now restores the prior session's content instead of returning early.
The inline overwrite already destroyed the original file, so early
return would permanently lose the prior session's memory.
This commit is contained in:
zeroaltitude 2026-03-10 00:19:54 -07:00
parent 28f44b1736
commit c0792e5376
No known key found for this signature in database
GPG Key ID: 77592FB1C703882E

View File

@ -476,8 +476,23 @@ const saveSessionToMemory: HookHandler = async (event) => {
"code" in err &&
(err as NodeJS.ErrnoException).code === "ENOENT"
) {
// File was externally deleted — nothing to retract.
log.debug("Session save retraction skipped — file already removed");
if (preExistingContent !== null) {
// Our inline write overwrote a pre-existing entry (slug collision),
// and the file was subsequently deleted externally. Restore the
// prior session's content — it was lost to our inline overwrite.
await writeFileWithinRoot({
rootDir: memoryDir,
relativePath: filename,
data: preExistingContent,
encoding: "utf-8",
});
log.debug(
"Session save retracted by post-hook — pre-existing file restored after external deletion",
);
} else {
// No prior content existed — file was externally deleted, nothing to restore.
log.debug("Session save retraction skipped — file already removed");
}
return;
}
throw err;