fix: report AgentExecutionBlocked in non-interactive programmatic modes (#26262)

This commit is contained in:
Coco Sheng
2026-04-30 16:41:35 -04:00
committed by GitHub
parent a03ec92436
commit 2f0c7518ad
11 changed files with 488 additions and 24 deletions
@@ -59,6 +59,13 @@ interface RunNonInteractiveParams {
resumedSessionData?: ResumedSessionData;
}
/**
* Runs the non-interactive CLI using the LegacyAgentSession.
*
* Programmatic output formats (JSON, STREAM_JSON) use lenient sanitization
* by stripping ANSI escape sequences from messages to ensure clean,
* parseable output for downstream consumers.
*/
export async function runNonInteractive({
config,
settings,
@@ -339,7 +346,13 @@ export async function runNonInteractive({
const formatter = new JsonFormatter();
const stats = uiTelemetryService.getMetrics();
textOutput.write(
formatter.format(config.getSessionId(), responseText, stats),
formatter.format(
config.getSessionId(),
responseText,
stats,
undefined,
warnings,
),
);
} else {
textOutput.ensureTrailingNewline();
@@ -420,6 +433,7 @@ export async function runNonInteractive({
let responseText = '';
let preToolResponseText: string | undefined;
let streamEnded = false;
const warnings: string[] = [];
for await (const event of session.stream({ streamId })) {
if (streamEnded) break;
switch (event.type) {
@@ -538,9 +552,18 @@ export async function runNonInteractive({
const errorCode = event._meta?.['code'];
if (errorCode === 'AGENT_EXECUTION_BLOCKED') {
const blockMessage = `Agent execution blocked: ${event.message.trim()}`;
if (config.getOutputFormat() === OutputFormat.TEXT) {
process.stderr.write(`[WARNING] ${event.message}\n`);
process.stderr.write(`[WARNING] ${blockMessage}\n`);
} else if (streamFormatter) {
streamFormatter.emitEvent({
type: JsonStreamEventType.ERROR,
timestamp: new Date().toISOString(),
severity: 'warning',
message: stripAnsi(blockMessage),
});
}
warnings.push(blockMessage);
break;
}
@@ -554,9 +577,10 @@ export async function runNonInteractive({
type: JsonStreamEventType.ERROR,
timestamp: new Date().toISOString(),
severity,
message: event.message,
message: stripAnsi(event.message),
});
}
warnings.push(event.message);
break;
}
case 'agent_end': {