fix(cli): use active sessionId in useLogger and improve resume robustness (#22606)

This commit is contained in:
matt korwel
2026-03-17 14:42:40 -07:00
committed by GitHub
parent 5d4e4c2814
commit e0be1b2afd
6 changed files with 136 additions and 17 deletions

View File

@@ -239,6 +239,44 @@ describe('SessionSelector', () => {
expect(result.sessionData.messages[0].content).toBe('Latest session');
});
it('should resolve session by UUID with whitespace (trimming)', async () => {
const sessionId = randomUUID();
// Create test session files
const chatsDir = path.join(tmpDir, 'chats');
await fs.mkdir(chatsDir, { recursive: true });
const session = {
sessionId,
projectHash: 'test-hash',
startTime: '2024-01-01T10:00:00.000Z',
lastUpdated: '2024-01-01T10:30:00.000Z',
messages: [
{
type: 'user',
content: 'Test message',
id: 'msg1',
timestamp: '2024-01-01T10:00:00.000Z',
},
],
};
await fs.writeFile(
path.join(
chatsDir,
`${SESSION_FILE_PREFIX}2024-01-01T10-00-${sessionId.slice(0, 8)}.json`,
),
JSON.stringify(session, null, 2),
);
const sessionSelector = new SessionSelector(config);
// Test resolving by UUID with leading/trailing spaces
const result = await sessionSelector.resolveSession(` ${sessionId} `);
expect(result.sessionData.sessionId).toBe(sessionId);
expect(result.sessionData.messages[0].content).toBe('Test message');
});
it('should deduplicate sessions by ID', async () => {
const sessionId = randomUUID();

View File

@@ -57,10 +57,14 @@ export class SessionError extends Error {
/**
* Creates an error for when a session identifier is invalid.
*/
static invalidSessionIdentifier(identifier: string): SessionError {
static invalidSessionIdentifier(
identifier: string,
chatsDir?: string,
): SessionError {
const dirInfo = chatsDir ? ` in ${chatsDir}` : '';
return new SessionError(
'INVALID_SESSION_IDENTIFIER',
`Invalid session identifier "${identifier}".\n Use --list-sessions to see available sessions, then use --resume {number}, --resume {uuid}, or --resume latest.`,
`Invalid session identifier "${identifier}".\n Searched for sessions${dirInfo}.\n Use --list-sessions to see available sessions, then use --resume {number}, --resume {uuid}, or --resume latest.`,
);
}
}
@@ -416,6 +420,7 @@ export class SessionSelector {
* @throws Error if the session is not found or identifier is invalid
*/
async findSession(identifier: string): Promise<SessionInfo> {
const trimmedIdentifier = identifier.trim();
const sessions = await this.listSessions();
if (sessions.length === 0) {
@@ -430,24 +435,28 @@ export class SessionSelector {
// Try to find by UUID first
const sessionByUuid = sortedSessions.find(
(session) => session.id === identifier,
(session) => session.id === trimmedIdentifier,
);
if (sessionByUuid) {
return sessionByUuid;
}
// Parse as index number (1-based) - only allow numeric indexes
const index = parseInt(identifier, 10);
const index = parseInt(trimmedIdentifier, 10);
if (
!isNaN(index) &&
index.toString() === identifier &&
index.toString() === trimmedIdentifier &&
index > 0 &&
index <= sortedSessions.length
) {
return sortedSessions[index - 1];
}
throw SessionError.invalidSessionIdentifier(identifier);
const chatsDir = path.join(
this.config.storage.getProjectTempDir(),
'chats',
);
throw SessionError.invalidSessionIdentifier(trimmedIdentifier, chatsDir);
}
/**
@@ -458,8 +467,9 @@ export class SessionSelector {
*/
async resolveSession(resumeArg: string): Promise<SessionSelectionResult> {
let selectedSession: SessionInfo;
const trimmedResumeArg = resumeArg.trim();
if (resumeArg === RESUME_LATEST) {
if (trimmedResumeArg === RESUME_LATEST) {
const sessions = await this.listSessions();
if (sessions.length === 0) {
@@ -475,7 +485,7 @@ export class SessionSelector {
selectedSession = sessions[sessions.length - 1];
} else {
try {
selectedSession = await this.findSession(resumeArg);
selectedSession = await this.findSession(trimmedResumeArg);
} catch (error) {
// SessionError already has detailed messages - just rethrow
if (error instanceof SessionError) {
@@ -483,7 +493,7 @@ export class SessionSelector {
}
// Wrap unexpected errors with context
throw new Error(
`Failed to find session "${resumeArg}": ${error instanceof Error ? error.message : String(error)}`,
`Failed to find session "${trimmedResumeArg}": ${error instanceof Error ? error.message : String(error)}`,
);
}
}