mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-04 18:31:36 -07:00
fix(cli): use active sessionId in useLogger and improve resume robustness (#22606)
This commit is contained in:
@@ -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)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user