mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-21 10:34:35 -07:00
fix(core): dynamic session ID injection to resolve resume bugs (#24972)
This commit is contained in:
committed by
GitHub
parent
80764c8bb5
commit
d06dba3538
@@ -444,7 +444,7 @@ export const AppContainer = (props: AppContainerProps) => {
|
||||
|
||||
const [isConfigInitialized, setConfigInitialized] = useState(false);
|
||||
|
||||
const logger = useLogger(config.storage);
|
||||
const logger = useLogger(config);
|
||||
const { inputHistory, addInput, initializeFromLogger } =
|
||||
useInputHistoryStore();
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import open from 'open';
|
||||
import path from 'node:path';
|
||||
import { bugCommand } from './bugCommand.js';
|
||||
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
||||
import { getVersion } from '@google/gemini-cli-core';
|
||||
import { getVersion, type Config } from '@google/gemini-cli-core';
|
||||
import { GIT_COMMIT_INFO } from '../../generated/git-commit.js';
|
||||
import { formatBytes } from '../utils/formatters.js';
|
||||
|
||||
@@ -89,7 +89,8 @@ describe('bugCommand', () => {
|
||||
getBugCommand: () => undefined,
|
||||
getIdeMode: () => true,
|
||||
getContentGeneratorConfig: () => ({ authType: 'oauth-personal' }),
|
||||
},
|
||||
getSessionId: vi.fn().mockReturnValue('test-session-id'),
|
||||
} as unknown as Config,
|
||||
geminiClient: {
|
||||
getChat: () => ({
|
||||
getHistory: () => [],
|
||||
@@ -137,7 +138,8 @@ describe('bugCommand', () => {
|
||||
storage: {
|
||||
getProjectTempDir: () => '/tmp/gemini',
|
||||
},
|
||||
},
|
||||
getSessionId: vi.fn().mockReturnValue('test-session-id'),
|
||||
} as unknown as Config,
|
||||
geminiClient: {
|
||||
getChat: () => ({
|
||||
getHistory: () => history,
|
||||
@@ -182,7 +184,8 @@ describe('bugCommand', () => {
|
||||
getBugCommand: () => ({ urlTemplate: customTemplate }),
|
||||
getIdeMode: () => true,
|
||||
getContentGeneratorConfig: () => ({ authType: 'vertex-ai' }),
|
||||
},
|
||||
getSessionId: vi.fn().mockReturnValue('test-session-id'),
|
||||
} as unknown as Config,
|
||||
geminiClient: {
|
||||
getChat: () => ({
|
||||
getHistory: () => [],
|
||||
|
||||
@@ -16,7 +16,6 @@ import { GIT_COMMIT_INFO } from '../../generated/git-commit.js';
|
||||
import { formatBytes } from '../utils/formatters.js';
|
||||
import {
|
||||
IdeClient,
|
||||
sessionId,
|
||||
getVersion,
|
||||
INITIAL_HISTORY_LENGTH,
|
||||
debugLogger,
|
||||
@@ -59,7 +58,7 @@ export const bugCommand: SlashCommand = {
|
||||
let info = `
|
||||
* **CLI Version:** ${cliVersion}
|
||||
* **Git Commit:** ${GIT_COMMIT_INFO}
|
||||
* **Session ID:** ${sessionId}
|
||||
* **Session ID:** ${config?.getSessionId() || 'Unknown'}
|
||||
* **Operating System:** ${osVersion}
|
||||
* **Sandbox Environment:** ${sandboxEnv}
|
||||
* **Model Version:** ${modelVersion}
|
||||
|
||||
@@ -158,6 +158,7 @@ Implement a comprehensive authentication system with multiple providers.
|
||||
getIdeMode: () => false,
|
||||
isTrustedFolder: () => true,
|
||||
getPreferredEditor: () => undefined,
|
||||
getSessionId: () => 'test-session-id',
|
||||
storage: {
|
||||
getPlansDir: () => mockPlansDir,
|
||||
},
|
||||
@@ -464,6 +465,7 @@ Implement a comprehensive authentication system with multiple providers.
|
||||
getTargetDir: () => mockTargetDir,
|
||||
getIdeMode: () => false,
|
||||
isTrustedFolder: () => true,
|
||||
getSessionId: () => 'test-session-id',
|
||||
storage: {
|
||||
getPlansDir: () => mockPlansDir,
|
||||
},
|
||||
|
||||
@@ -82,6 +82,7 @@ const mockConfigPlain = {
|
||||
getExtensionRegistryURI: () => undefined,
|
||||
getContentGeneratorConfig: () => ({ authType: undefined }),
|
||||
getSandboxEnabled: () => false,
|
||||
getSessionId: () => 'test-session-id',
|
||||
};
|
||||
|
||||
const mockConfig = mockConfigPlain as unknown as Config;
|
||||
|
||||
@@ -124,7 +124,7 @@ describe('<HistoryItemDisplay />', () => {
|
||||
duration: '1s',
|
||||
};
|
||||
const { lastFrame, unmount } = await renderWithProviders(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<HistoryItemDisplay {...baseItem} item={item} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -157,7 +157,7 @@ describe('<HistoryItemDisplay />', () => {
|
||||
type: 'model_stats',
|
||||
};
|
||||
const { lastFrame, unmount } = await renderWithProviders(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<HistoryItemDisplay {...baseItem} item={item} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -173,7 +173,7 @@ describe('<HistoryItemDisplay />', () => {
|
||||
type: 'tool_stats',
|
||||
};
|
||||
const { lastFrame, unmount } = await renderWithProviders(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<HistoryItemDisplay {...baseItem} item={item} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -190,7 +190,7 @@ describe('<HistoryItemDisplay />', () => {
|
||||
duration: '1s',
|
||||
};
|
||||
const { lastFrame, unmount } = await renderWithProviders(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<HistoryItemDisplay {...baseItem} item={item} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
|
||||
@@ -86,6 +86,7 @@ describe('<ModelDialog />', () => {
|
||||
getProModelNoAccess: mockGetProModelNoAccess,
|
||||
getProModelNoAccessSync: mockGetProModelNoAccessSync,
|
||||
getLastRetrievedQuota: () => ({ buckets: [] }),
|
||||
getSessionId: () => 'test-session-id',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -55,6 +55,7 @@ describe('ToolConfirmationQueue', () => {
|
||||
getFileSystemService: () => ({
|
||||
readFile: vi.fn().mockResolvedValue('Plan content'),
|
||||
}),
|
||||
getSessionId: () => 'test-session-id',
|
||||
storage: {
|
||||
getPlansDir: () => '/mock/temp/plans',
|
||||
},
|
||||
|
||||
@@ -60,7 +60,7 @@ describe('SessionStatsContext', () => {
|
||||
> = { current: undefined };
|
||||
|
||||
const { unmount } = await render(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<TestHarness contextRef={contextRef} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -79,7 +79,7 @@ describe('SessionStatsContext', () => {
|
||||
> = { current: undefined };
|
||||
|
||||
const { unmount } = await render(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<TestHarness contextRef={contextRef} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -162,7 +162,7 @@ describe('SessionStatsContext', () => {
|
||||
};
|
||||
|
||||
const { unmount } = await render(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<CountingTestHarness />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
@@ -245,7 +245,7 @@ describe('SessionStatsContext', () => {
|
||||
> = { current: undefined };
|
||||
|
||||
const { unmount } = await render(
|
||||
<SessionStatsProvider>
|
||||
<SessionStatsProvider sessionId="test-session-id">
|
||||
<TestHarness contextRef={contextRef} />
|
||||
</SessionStatsProvider>,
|
||||
);
|
||||
|
||||
@@ -13,14 +13,13 @@ import {
|
||||
useMemo,
|
||||
useEffect,
|
||||
} from 'react';
|
||||
|
||||
import type {
|
||||
SessionMetrics,
|
||||
ModelMetrics,
|
||||
RoleMetrics,
|
||||
ToolCallStats,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { uiTelemetryService, sessionId } from '@google/gemini-cli-core';
|
||||
import { uiTelemetryService } from '@google/gemini-cli-core';
|
||||
|
||||
export enum ToolCallDecision {
|
||||
ACCEPT = 'accept',
|
||||
@@ -183,9 +182,10 @@ const SessionStatsContext = createContext<SessionStatsContextValue | undefined>(
|
||||
|
||||
// --- Provider Component ---
|
||||
|
||||
export const SessionStatsProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
children,
|
||||
}) => {
|
||||
export const SessionStatsProvider: React.FC<{
|
||||
children: React.ReactNode;
|
||||
sessionId: string;
|
||||
}> = ({ children, sessionId }) => {
|
||||
const [stats, setStats] = useState<SessionStatsState>({
|
||||
sessionId,
|
||||
sessionStartTime: new Date(),
|
||||
|
||||
@@ -262,14 +262,13 @@ export const useGeminiStream = (
|
||||
useStateAndRef<boolean>(true);
|
||||
const processedMemoryToolsRef = useRef<Set<string>>(new Set());
|
||||
const { startNewPrompt, getPromptCount } = useSessionStats();
|
||||
const storage = config.storage;
|
||||
const logger = useLogger(storage);
|
||||
const logger = useLogger(config);
|
||||
const gitService = useMemo(() => {
|
||||
if (!config.getProjectRoot()) {
|
||||
return;
|
||||
}
|
||||
return new GitService(config.getProjectRoot(), storage);
|
||||
}, [config, storage]);
|
||||
return new GitService(config.getProjectRoot(), config.storage);
|
||||
}, [config]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleRetryAttempt = (payload: RetryAttemptPayload) => {
|
||||
@@ -1580,6 +1579,7 @@ export const useGeminiStream = (
|
||||
operation: options?.isContinuation
|
||||
? GeminiCliOperation.SystemPrompt
|
||||
: GeminiCliOperation.UserPrompt,
|
||||
sessionId: config.getSessionId(),
|
||||
},
|
||||
async ({ metadata: spanMetadata }) => {
|
||||
spanMetadata.input = query;
|
||||
@@ -2105,7 +2105,7 @@ export const useGeminiStream = (
|
||||
}
|
||||
|
||||
if (checkpointsToWrite.size > 0) {
|
||||
const checkpointDir = storage.getProjectTempCheckpointsDir();
|
||||
const checkpointDir = config.storage.getProjectTempCheckpointsDir();
|
||||
try {
|
||||
await fs.mkdir(checkpointDir, { recursive: true });
|
||||
for (const [fileName, content] of checkpointsToWrite) {
|
||||
@@ -2122,15 +2122,7 @@ export const useGeminiStream = (
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
saveRestorableToolCalls();
|
||||
}, [
|
||||
toolCalls,
|
||||
config,
|
||||
onDebugMessage,
|
||||
gitService,
|
||||
history,
|
||||
geminiClient,
|
||||
storage,
|
||||
]);
|
||||
}, [toolCalls, config, onDebugMessage, gitService, history, geminiClient]);
|
||||
|
||||
const lastOutputTime = Math.max(
|
||||
lastToolOutputTime,
|
||||
|
||||
@@ -8,14 +8,7 @@ import { act } from 'react';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook } from '../../test-utils/render.js';
|
||||
import { useLogger } from './useLogger.js';
|
||||
import {
|
||||
sessionId as globalSessionId,
|
||||
Logger,
|
||||
type Storage,
|
||||
type Config,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { ConfigContext } from '../contexts/ConfigContext.js';
|
||||
import type React from 'react';
|
||||
import { Logger, type Storage, type Config } from '@google/gemini-cli-core';
|
||||
|
||||
let deferredInit: { resolve: (val?: unknown) => void };
|
||||
|
||||
@@ -41,35 +34,15 @@ describe('useLogger', () => {
|
||||
const mockStorage = {} as Storage;
|
||||
const mockConfig = {
|
||||
getSessionId: vi.fn().mockReturnValue('active-session-id'),
|
||||
storage: mockStorage,
|
||||
} as unknown as Config;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should initialize with the global sessionId by default', async () => {
|
||||
const { result } = await renderHook(() => useLogger(mockStorage));
|
||||
|
||||
expect(result.current).toBeNull();
|
||||
|
||||
await act(async () => {
|
||||
deferredInit.resolve();
|
||||
});
|
||||
|
||||
expect(result.current).not.toBeNull();
|
||||
expect(Logger).toHaveBeenCalledWith(globalSessionId, mockStorage);
|
||||
});
|
||||
|
||||
it('should initialize with the active sessionId from ConfigContext when available', async () => {
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<ConfigContext.Provider value={mockConfig}>
|
||||
{children}
|
||||
</ConfigContext.Provider>
|
||||
);
|
||||
|
||||
const { result } = await renderHook(() => useLogger(mockStorage), {
|
||||
wrapper,
|
||||
});
|
||||
it('should initialize with the sessionId from config', async () => {
|
||||
const { result } = await renderHook(() => useLogger(mockConfig));
|
||||
|
||||
expect(result.current).toBeNull();
|
||||
|
||||
|
||||
@@ -4,24 +4,17 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useContext } from 'react';
|
||||
import {
|
||||
sessionId as globalSessionId,
|
||||
Logger,
|
||||
type Storage,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { ConfigContext } from '../contexts/ConfigContext.js';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Logger, type Config } from '@google/gemini-cli-core';
|
||||
|
||||
/**
|
||||
* Hook to manage the logger instance.
|
||||
*/
|
||||
export const useLogger = (storage: Storage): Logger | null => {
|
||||
export const useLogger = (config: Config): Logger | null => {
|
||||
const [logger, setLogger] = useState<Logger | null>(null);
|
||||
const config = useContext(ConfigContext);
|
||||
|
||||
useEffect(() => {
|
||||
const activeSessionId = config?.getSessionId() ?? globalSessionId;
|
||||
const newLogger = new Logger(activeSessionId, storage);
|
||||
const newLogger = new Logger(config.getSessionId(), config.storage);
|
||||
|
||||
/**
|
||||
* Start async initialization, no need to await. Using await slows down the
|
||||
@@ -30,11 +23,9 @@ export const useLogger = (storage: Storage): Logger | null => {
|
||||
*/
|
||||
newLogger
|
||||
.initialize()
|
||||
.then(() => {
|
||||
setLogger(newLogger);
|
||||
})
|
||||
.then(() => setLogger(newLogger))
|
||||
.catch(() => {});
|
||||
}, [storage, config]);
|
||||
}, [config]);
|
||||
|
||||
return logger;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user