fix(logging): Ensure sandbox startup messages are routed to stderr (#5725)

This commit is contained in:
Allen Hutchison 2025-08-06 17:19:10 -07:00 committed by GitHub
parent 99f88851fb
commit d6a7334279
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 551 additions and 534 deletions

View File

@ -224,7 +224,7 @@ export type Message =
}; };
export interface ConsoleMessageItem { export interface ConsoleMessageItem {
type: 'log' | 'warn' | 'error' | 'debug'; type: 'log' | 'warn' | 'error' | 'debug' | 'info';
content: string; content: string;
count: number; count: number;
} }

View File

@ -18,6 +18,7 @@ export class ConsolePatcher {
private originalConsoleWarn = console.warn; private originalConsoleWarn = console.warn;
private originalConsoleError = console.error; private originalConsoleError = console.error;
private originalConsoleDebug = console.debug; private originalConsoleDebug = console.debug;
private originalConsoleInfo = console.info;
private params: ConsolePatcherParams; private params: ConsolePatcherParams;
@ -30,6 +31,7 @@ export class ConsolePatcher {
console.warn = this.patchConsoleMethod('warn', this.originalConsoleWarn); console.warn = this.patchConsoleMethod('warn', this.originalConsoleWarn);
console.error = this.patchConsoleMethod('error', this.originalConsoleError); console.error = this.patchConsoleMethod('error', this.originalConsoleError);
console.debug = this.patchConsoleMethod('debug', this.originalConsoleDebug); console.debug = this.patchConsoleMethod('debug', this.originalConsoleDebug);
console.info = this.patchConsoleMethod('info', this.originalConsoleInfo);
} }
cleanup = () => { cleanup = () => {
@ -37,13 +39,14 @@ export class ConsolePatcher {
console.warn = this.originalConsoleWarn; console.warn = this.originalConsoleWarn;
console.error = this.originalConsoleError; console.error = this.originalConsoleError;
console.debug = this.originalConsoleDebug; console.debug = this.originalConsoleDebug;
console.info = this.originalConsoleInfo;
}; };
private formatArgs = (args: unknown[]): string => util.format(...args); private formatArgs = (args: unknown[]): string => util.format(...args);
private patchConsoleMethod = private patchConsoleMethod =
( (
type: 'log' | 'warn' | 'error' | 'debug', type: 'log' | 'warn' | 'error' | 'debug' | 'info',
originalMethod: (...args: unknown[]) => void, originalMethod: (...args: unknown[]) => void,
) => ) =>
(...args: unknown[]) => { (...args: unknown[]) => {

View File

@ -16,6 +16,7 @@ import {
} from '../config/settings.js'; } from '../config/settings.js';
import { promisify } from 'util'; import { promisify } from 'util';
import { Config, SandboxConfig } from '@google/gemini-cli-core'; import { Config, SandboxConfig } from '@google/gemini-cli-core';
import { ConsolePatcher } from '../ui/utils/ConsolePatcher.js';
const execAsync = promisify(exec); const execAsync = promisify(exec);
@ -185,6 +186,13 @@ export async function start_sandbox(
nodeArgs: string[] = [], nodeArgs: string[] = [],
cliConfig?: Config, cliConfig?: Config,
) { ) {
const patcher = new ConsolePatcher({
debugMode: cliConfig?.getDebugMode() || !!process.env.DEBUG,
stderr: true,
});
patcher.patch();
try {
if (config.command === 'sandbox-exec') { if (config.command === 'sandbox-exec') {
// disallow BUILD_SANDBOX // disallow BUILD_SANDBOX
if (process.env.BUILD_SANDBOX) { if (process.env.BUILD_SANDBOX) {
@ -424,7 +432,10 @@ export async function start_sandbox(
if (!fs.existsSync(userSettingsDirOnHost)) { if (!fs.existsSync(userSettingsDirOnHost)) {
fs.mkdirSync(userSettingsDirOnHost); fs.mkdirSync(userSettingsDirOnHost);
} }
args.push('--volume', `${userSettingsDirOnHost}:${userSettingsDirInSandbox}`); args.push(
'--volume',
`${userSettingsDirOnHost}:${userSettingsDirInSandbox}`,
);
if (userSettingsDirInSandbox !== userSettingsDirOnHost) { if (userSettingsDirInSandbox !== userSettingsDirOnHost) {
args.push( args.push(
'--volume', '--volume',
@ -784,6 +795,9 @@ export async function start_sandbox(
resolve(); resolve();
}); });
}); });
} finally {
patcher.cleanup();
}
} }
// Helper functions to ensure sandbox image is present // Helper functions to ensure sandbox image is present