fix(cli): gracefully handle --resume when no sessions exist (#21429)

This commit is contained in:
Sandy Tao
2026-03-06 11:02:33 -08:00
committed by GitHub
parent 6f579934db
commit 42d367d72f
4 changed files with 109 additions and 15 deletions

View File

@@ -341,6 +341,29 @@ describe('SessionSelector', () => {
);
});
it('should throw SessionError with NO_SESSIONS_FOUND when resolving latest with no sessions', async () => {
// Empty chats directory — no session files
const chatsDir = path.join(tmpDir, 'chats');
await fs.mkdir(chatsDir, { recursive: true });
const emptyConfig = {
storage: {
getProjectTempDir: () => tmpDir,
},
getSessionId: () => 'current-session-id',
} as Partial<Config> as Config;
const sessionSelector = new SessionSelector(emptyConfig);
await expect(sessionSelector.resolveSession('latest')).rejects.toSatisfy(
(error) => {
expect(error).toBeInstanceOf(SessionError);
expect((error as SessionError).code).toBe('NO_SESSIONS_FOUND');
return true;
},
);
});
it('should not list sessions with only system messages', async () => {
const sessionIdWithUser = randomUUID();
const sessionIdSystemOnly = randomUUID();

View File

@@ -463,7 +463,7 @@ export class SessionSelector {
const sessions = await this.listSessions();
if (sessions.length === 0) {
throw new Error('No previous sessions found for this project.');
throw SessionError.noSessionsFound();
}
// Sort by startTime (oldest first, so newest sessions get highest numbers)