fix(cli): conditionally suppress console output in headless and non-interactive modes

This commit is contained in:
Cynthia Long
2026-03-16 23:36:20 +00:00
parent 55778583b9
commit 2e466bd4cb
4 changed files with 29 additions and 3 deletions
+1 -1
View File
@@ -272,7 +272,7 @@ export async function main() {
const isDebugMode = cliConfig.isDebugMode(argv);
const consolePatcher = new ConsolePatcher({
stderr: true,
suppressConsoleOutput: isHeadlessMode() ? true : false,
suppressConsoleOutput: isHeadlessMode() && !isDebugMode,
debugMode: isDebugMode,
onNewMessage: (msg) => {
coreEvents.emitConsoleLog(msg.type, msg.content);
+3 -1
View File
@@ -27,6 +27,7 @@ import {
CoreEvent,
createWorkingStdio,
recordToolCallInteractions,
isHeadlessMode,
ToolErrorType,
Scheduler,
ROOT_SCHEDULER_ID,
@@ -63,9 +64,10 @@ export async function runNonInteractive({
resumedSessionData,
}: RunNonInteractiveParams): Promise<void> {
return promptIdContext.run(prompt_id, async () => {
const suppressConsoleOutput = isHeadlessMode() && !config.getDebugMode();
const consolePatcher = new ConsolePatcher({
stderr: true,
suppressConsoleOutput: true,
suppressConsoleOutput,
debugMode: config.getDebugMode(),
onNewMessage: (msg) => {
coreEvents.emitConsoleLog(msg.type, msg.content);
@@ -43,6 +43,26 @@ describe('ConsolePatcher', () => {
console.error = originalError;
});
it('should NOT suppress console.error even when suppressConsoleOutput is true', () => {
const originalError = console.error;
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
patcher = new ConsolePatcher({
debugMode: false,
suppressConsoleOutput: true,
stderr: true,
});
patcher.patch();
console.error('test error');
expect(errorSpy).toHaveBeenCalled();
patcher.cleanup();
errorSpy.mockRestore();
console.error = originalError;
});
it('should NOT suppress output when suppressConsoleOutput is true but debugMode is true', () => {
const originalError = console.error;
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+5 -1
View File
@@ -50,7 +50,11 @@ export class ConsolePatcher {
private patchConsoleMethod =
(type: 'log' | 'warn' | 'error' | 'debug' | 'info') =>
(...args: unknown[]) => {
if (this.params.suppressConsoleOutput && !this.params.debugMode) {
if (
this.params.suppressConsoleOutput &&
!this.params.debugMode &&
type !== 'error'
) {
return;
}