Skip to content

Commit 4309168

Browse files
committed
fix(#1990): gate gemini + qwen exit-0 runs on terminal result event
Mirror the codex/claude completion gate for gemini-cli and qwen-code, whose stream-json output ends with a single terminal `result` event. An exit-0 run that did work but never emitted that event was cut off mid-run (e.g. the docker container ran out of disk) and is now registered as success:false, preserving the session (argv.resume) for a context-preserving restart and keeping the docker container filesystem for inspection. New shared helper src/tool-run-health.lib.mjs (getTerminalEventCompletionHealth) treats disk-exhaustion strings as diagnostics only — never an independent gate — to avoid the #1955 echo false positive. opencode is intentionally excluded (upstream step_finish flush bug anomalyco/opencode#26855).
1 parent f2551b7 commit 4309168

3 files changed

Lines changed: 151 additions & 0 deletions

File tree

‎src/gemini.lib.mjs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { defaultModels, geminiModels } from './models/index.mjs';
2121
import { checkPlaywrightMcpPackageAvailability } from './playwright-mcp.lib.mjs';
2222
import { classifyRetryableError, getRetryDelayMs, maybeSwitchToFallbackModel, waitWithCountdown } from './tool-retry.lib.mjs';
2323
import { getCumulativeContextInputTokens, toTokenCount } from './context-fill.lib.mjs';
24+
import { getTerminalEventCompletionHealth } from './tool-run-health.lib.mjs'; // Issue #1990
2425

2526
const shellQuote = value => `"${String(value).replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`;
2627

@@ -581,6 +582,52 @@ export const executeGeminiCommand = async params => {
581582
};
582583
}
583584

585+
// Issue #1990: exit 0 and a non-empty stream are necessary but NOT
586+
// sufficient. gemini-cli's stream-json ends with a terminal `result` event;
587+
// a run that did work but never emitted it was cut off mid-run (e.g. the
588+
// docker container ran out of disk) and must be registered as a failure so
589+
// the session is preserved for a context-preserving restart and — under
590+
// docker isolation — the container filesystem is kept for inspection.
591+
const completionHealth = getTerminalEventCompletionHealth({
592+
eventCounts: geminiJsonState.eventCounts,
593+
terminalEventTypes: ['result'],
594+
hadActivity: (geminiJsonState.messageCount || 0) > 0 || (geminiJsonState.toolUseCount || 0) > 0,
595+
diskEvidenceTexts: [
596+
{ source: 'output', text: allOutput },
597+
{ source: 'result-summary', text: geminiJsonState.resultSummary },
598+
],
599+
});
600+
if (!completionHealth.healthy) {
601+
await log('\n\n❌ Gemini exited 0 but the run did not complete — treating as failure', { level: 'error' });
602+
for (const reason of completionHealth.reasons) {
603+
await log(` • ${reason}`, { level: 'error' });
604+
}
605+
if (completionHealth.diskPressureDetected) {
606+
await log(' 💽 Disk-exhaustion evidence (diagnostic):', { level: 'error' });
607+
for (const evidence of completionHealth.diskEvidence.slice(0, 5)) {
608+
await log(` ↳ [${evidence.source}] ${evidence.text}`, { level: 'error' });
609+
}
610+
await log(' 💡 Free disk space before retrying. Under docker isolation the container is preserved on failure for inspection.', { level: 'error' });
611+
}
612+
if (sessionId && !argv.resume) argv.resume = sessionId;
613+
return {
614+
success: false,
615+
sessionId,
616+
limitReached,
617+
limitResetTime,
618+
messageCount: geminiJsonState.messageCount || 0,
619+
toolUseCount: geminiJsonState.toolUseCount || 0,
620+
resultModelUsage: geminiJsonState.resultModelUsage || buildGeminiResultModelUsage(mappedModel),
621+
pricingInfo: { modelId: mappedModel, modelName: mappedModel, provider: 'Google', totalCostUSD: null },
622+
publicPricingEstimate: null,
623+
resultSummary: geminiJsonState.resultSummary || null,
624+
completionHealth,
625+
incompleteSession: completionHealth.incompleteSession,
626+
diskPressureDetected: completionHealth.diskPressureDetected,
627+
errorInfo: { message: completionHealth.reasons.join(' ') },
628+
};
629+
}
630+
584631
await log('\n\n✅ Gemini command completed');
585632
await log(`📊 Total messages: ${geminiJsonState.messageCount || 0}, Tool uses: ${geminiJsonState.toolUseCount || 0}`);
586633
if (geminiJsonState.resultSummary) {

‎src/qwen.lib.mjs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { qwenModels, defaultModels } from './models/index.mjs';
2222
import { checkPlaywrightMcpPackageAvailability } from './playwright-mcp.lib.mjs';
2323
import { classifyRetryableError, getRetryDelayMs, maybeSwitchToFallbackModel, waitWithCountdown } from './tool-retry.lib.mjs';
2424
import { getCumulativeContextInputTokens, getRestoredContextInputTokens, toTokenCount } from './context-fill.lib.mjs';
25+
import { getTerminalEventCompletionHealth } from './tool-run-health.lib.mjs'; // Issue #1990
2526

2627
export const mapModelToId = model => qwenModels[model] || model;
2728

@@ -638,6 +639,48 @@ export const executeQwenCommand = async params => {
638639
};
639640
}
640641

642+
// Issue #1990: exit 0 with no error event is necessary but NOT sufficient.
643+
// qwen-code's stream-json ends with a terminal `result` event; a run that
644+
// did work but never emitted it was cut off mid-run (e.g. the docker
645+
// container ran out of disk) and must be registered as a failure so the
646+
// session is preserved for a context-preserving restart and — under docker
647+
// isolation — the container filesystem is kept for inspection.
648+
const completionHealth = getTerminalEventCompletionHealth({
649+
eventCounts: qwenState.eventCounts,
650+
terminalEventTypes: ['result'],
651+
hadActivity: (qwenState.parsedEvents?.length || 0) > 0,
652+
diskEvidenceTexts: [
653+
{ source: 'output', text: allOutput },
654+
{ source: 'result-summary', text: resultSummary },
655+
],
656+
});
657+
if (!completionHealth.healthy) {
658+
await log('\n\n❌ Qwen Code exited 0 but the run did not complete — treating as failure', { level: 'error' });
659+
for (const reason of completionHealth.reasons) {
660+
await log(` • ${reason}`, { level: 'error' });
661+
}
662+
if (completionHealth.diskPressureDetected) {
663+
await log(' 💽 Disk-exhaustion evidence (diagnostic):', { level: 'error' });
664+
for (const evidence of completionHealth.diskEvidence.slice(0, 5)) {
665+
await log(` ↳ [${evidence.source}] ${evidence.text}`, { level: 'error' });
666+
}
667+
await log(' 💡 Free disk space before retrying. Under docker isolation the container is preserved on failure for inspection.', { level: 'error' });
668+
}
669+
if (sessionId && !argv.resume) argv.resume = sessionId;
670+
return {
671+
success: false,
672+
sessionId,
673+
limitReached: false,
674+
limitResetTime: null,
675+
...usageResult,
676+
resultSummary,
677+
completionHealth,
678+
incompleteSession: completionHealth.incompleteSession,
679+
diskPressureDetected: completionHealth.diskPressureDetected,
680+
errorInfo: { message: completionHealth.reasons.join(' ') },
681+
};
682+
}
683+
641684
await log('\n\n✅ Qwen Code command completed');
642685
if (resultSummary) {
643686
await log('📝 Captured result summary from Qwen Code output', { verbose: true });

‎src/tool-run-health.lib.mjs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Tool-agnostic run-health analysis (Issue #1990).
2+
//
3+
// Background: under docker isolation two long-running `solve --tool codex` tasks
4+
// reported SUCCESS (Exit Code: 0) while their containers had run out of disk —
5+
// the AI session was cut off mid-run, no commits were produced, yet the process
6+
// exited 0. Reporting that as success also discarded the container filesystem we
7+
// needed to inspect and retry from.
8+
//
9+
// codex.lib.mjs gets a bespoke gate (paired turn.started/turn.completed lifecycle
10+
// — see codex-health.lib.mjs) and claude.lib.mjs already requires its final
11+
// `result` event (shouldFailClaudeStreamWithoutResult). This module provides the
12+
// equivalent gate for the tools whose stream-json output (adopted from the Claude
13+
// Agent SDK schema) ends with a single terminal `result` event: gemini-cli and
14+
// qwen-code. An exit-0 run that clearly began work but never emitted that
15+
// terminal event was interrupted and must NOT be reported as success.
16+
//
17+
// opencode is deliberately NOT gated here: its `step_finish` terminal event is
18+
// not reliably flushed before a clean exit on some versions (upstream bug
19+
// anomalyco/opencode#26855), so gating on it would convert genuine successes into
20+
// failures. See docs/case-studies/issue-1990 for the analysis.
21+
//
22+
// Disk-exhaustion strings ("No space left on device", ENOSPC) are surfaced only
23+
// as supporting *diagnostics* — never an independent failure gate — to avoid the
24+
// issue #1955 class of false positive where a tool echoes a command's stdout that
25+
// merely mentions the phrase.
26+
27+
import { isENOSPC } from './lib.mjs';
28+
29+
export const getTerminalEventCompletionHealth = ({ eventCounts = {}, terminalEventTypes = ['result'], hadActivity = false, diskEvidenceTexts = [] } = {}) => {
30+
const terminalCount = terminalEventTypes.reduce((sum, type) => sum + (eventCounts[type] || 0), 0);
31+
32+
// Only flag a run that did work but never reached its terminal event. A run
33+
// with no activity at all is handled separately by each tool (e.g. gemini's
34+
// emittedNoEvents check) and must not be double-counted here.
35+
const incompleteSession = hadActivity && terminalCount === 0;
36+
37+
const diskEvidence = [];
38+
for (const { source, text } of diskEvidenceTexts) {
39+
if (text && isENOSPC(text)) {
40+
diskEvidence.push({ source, text: String(text).replace(/\s+/g, ' ').trim().slice(0, 300) });
41+
}
42+
}
43+
const diskPressureDetected = diskEvidence.length > 0;
44+
45+
const reasons = [];
46+
if (incompleteSession) {
47+
reasons.push(`The tool exited 0 but never emitted its terminal completion event (${terminalEventTypes.join('/')}); the session was cut off mid-run.`);
48+
if (diskPressureDetected) {
49+
reasons.push(`Disk-exhaustion signals were present in ${diskEvidence.length} location(s) (e.g. "No space left on device") — the likely cause of the interrupted session.`);
50+
}
51+
}
52+
53+
return {
54+
healthy: !incompleteSession,
55+
incompleteSession,
56+
diskPressureDetected,
57+
diskEvidence,
58+
terminalCount,
59+
reasons,
60+
};
61+
};

0 commit comments

Comments
 (0)