fix(coding-agent): reject invalid session files

closes #6002
This commit is contained in:
Mario Zechner
2026-06-25 15:16:18 +02:00
parent f14b3594c1
commit 543710f643
5 changed files with 106 additions and 28 deletions
@@ -795,10 +795,13 @@ export class SessionManager {
if (existsSync(this.sessionFile)) {
this.fileEntries = loadEntriesFromFile(this.sessionFile);
// If file was empty or corrupted (no valid header), truncate and start fresh
// to avoid appending messages without a session header (which breaks the session)
// If file was empty, initialize it with a valid session header. If it was
// non-empty but did not parse as a pi session, fail without modifying it.
if (this.fileEntries.length === 0) {
const explicitPath = this.sessionFile;
if (statSync(explicitPath).size > 0) {
throw new Error(`Session file is not a valid pi session and was not modified: ${explicitPath}`);
}
this.newSession();
this.sessionFile = explicitPath;
this._rewriteFile();