feat(cli): enable activity logging for non-interactive mode and evals (#17703)

This commit is contained in:
Sandy Tao
2026-01-28 09:02:41 -08:00
committed by GitHub
parent 25ae1a1b54
commit 9e09db1ddb
4 changed files with 92 additions and 16 deletions

View File

@@ -38,6 +38,11 @@ import type { LoadedSettings } from './config/settings.js';
// Mock core modules
vi.mock('./ui/hooks/atCommandProcessor.js');
const mockRegisterActivityLogger = vi.hoisted(() => vi.fn());
vi.mock('./utils/activityLogger.js', () => ({
registerActivityLogger: mockRegisterActivityLogger,
}));
const mockCoreEvents = vi.hoisted(() => ({
on: vi.fn(),
off: vi.fn(),
@@ -259,6 +264,52 @@ describe('runNonInteractive', () => {
// so we no longer expect shutdownTelemetry to be called directly here
});
it('should register activity logger when GEMINI_CLI_ACTIVITY_LOG_FILE is set', async () => {
vi.stubEnv('GEMINI_CLI_ACTIVITY_LOG_FILE', '/tmp/test.jsonl');
const events: ServerGeminiStreamEvent[] = [
{
type: GeminiEventType.Finished,
value: { reason: undefined, usageMetadata: { totalTokenCount: 0 } },
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
);
await runNonInteractive({
config: mockConfig,
settings: mockSettings,
input: 'test',
prompt_id: 'prompt-id-activity-logger',
});
expect(mockRegisterActivityLogger).toHaveBeenCalledWith(mockConfig);
vi.unstubAllEnvs();
});
it('should not register activity logger when GEMINI_CLI_ACTIVITY_LOG_FILE is not set', async () => {
vi.stubEnv('GEMINI_CLI_ACTIVITY_LOG_FILE', '');
const events: ServerGeminiStreamEvent[] = [
{
type: GeminiEventType.Finished,
value: { reason: undefined, usageMetadata: { totalTokenCount: 0 } },
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
);
await runNonInteractive({
config: mockConfig,
settings: mockSettings,
input: 'test',
prompt_id: 'prompt-id-activity-logger-off',
});
expect(mockRegisterActivityLogger).not.toHaveBeenCalled();
vi.unstubAllEnvs();
});
it('should handle a single tool call and respond', async () => {
const toolCallEvent: ServerGeminiStreamEvent = {
type: GeminiEventType.ToolCallRequest,

View File

@@ -70,6 +70,14 @@ export async function runNonInteractive({
coreEvents.emitConsoleLog(msg.type, msg.content);
},
});
if (config.storage && process.env['GEMINI_CLI_ACTIVITY_LOG_FILE']) {
const { registerActivityLogger } = await import(
'./utils/activityLogger.js'
);
registerActivityLogger(config);
}
const { stdout: workingStdout } = createWorkingStdio();
const textOutput = new TextOutput(workingStdout);

View File

@@ -323,13 +323,17 @@ export class ActivityLogger extends EventEmitter {
}
/**
* Registers the activity logger if debug mode and interactive session are enabled.
* Registers the activity logger.
* Captures network and console logs to a session-specific JSONL file.
*
* The log file location can be overridden via the GEMINI_CLI_ACTIVITY_LOG_FILE
* environment variable. If not set, defaults to logs/session-{sessionId}.jsonl
* in the project's temp directory.
*
* @param config The CLI configuration
*/
export function registerActivityLogger(config: Config) {
if (config.isInteractive() && config.storage && config.getDebugMode()) {
if (config.storage) {
const capture = ActivityLogger.getInstance();
capture.enable();
@@ -338,10 +342,10 @@ export function registerActivityLogger(config: Config) {
fs.mkdirSync(logsDir, { recursive: true });
}
const logFile = path.join(
logsDir,
`session-${config.getSessionId()}.jsonl`,
);
const logFile =
process.env['GEMINI_CLI_ACTIVITY_LOG_FILE'] ||
path.join(logsDir, `session-${config.getSessionId()}.jsonl`);
const writeToLog = (type: 'console' | 'network', payload: unknown) => {
try {
const entry =