@@ -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();
|
||||
|
||||
@@ -241,6 +241,16 @@ function validateSessionIdFlags(parsed: Args): void {
|
||||
}
|
||||
}
|
||||
|
||||
function openSessionOrExit(path: string, sessionDir?: string): SessionManager {
|
||||
try {
|
||||
return SessionManager.open(path, sessionDir);
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(chalk.red(`Error: ${message}`));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function forkSessionOrExit(sourcePath: string, cwd: string, sessionDir?: string, sessionId?: string): SessionManager {
|
||||
try {
|
||||
return SessionManager.forkFrom(sourcePath, cwd, sessionDir, { id: sessionId });
|
||||
@@ -290,7 +300,7 @@ async function createSessionManager(
|
||||
switch (resolved.type) {
|
||||
case "path":
|
||||
case "local":
|
||||
return SessionManager.open(resolved.path, sessionDir);
|
||||
return openSessionOrExit(resolved.path, sessionDir);
|
||||
|
||||
case "global": {
|
||||
console.log(chalk.yellow(`Session found in different project: ${resolved.cwd}`));
|
||||
|
||||
Reference in New Issue
Block a user