fix(cli): deliver complete TOON beyond the string limit (#12074)

* fix(cli): deliver complete TOON beyond the string limit

* fix(cli): own cancellation through TOON delivery

* test(cli): await TOON stage acknowledgements

* fix(cli): build TOON cancellations through domain factory

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-18 20:40:21 -05:00
1 parent bd5c985b3f
commit f10d557f06
10 files changed
+1019 -60

No files matched your search

+83 -26
View File
@@ -38,6 +38,12 @@ import { formatCliCallToAction, type CallToAction } from "./outputEnvelope.js";
import { jsonLinesErrorRecord } from "./jsonLines.js";
import type { OutputFilterPath } from "./outputFilter.js";
import { writeCliJsonLines } from "./jsonLinesOutput.js";
import {
ToonOutputCleanupFailure,
toonOutputCancellationStatus,
writeToonOutput,
} from "./toonOutput.js";
import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js";
import {
jsonOutputParts,
isTerminalOutput,
@@ -45,6 +51,8 @@ import {
writeOutputParts,
writeOutputText,
} from "./jsonOutput.js";
export { toonOutputCancellationStatus as cliOutputCancellationStatus } from "./toonOutput.js";
/** Result metadata supplied by the command that actually ran. */
export interface CliResultOutputMetadata {
readonly command: string;
@@ -104,6 +112,7 @@ export const createCliResultOutput = (
| "tsv";
let handled = false;
let failed = false;
let interrupted = false;
let error: AnalysisErrorProjection | undefined;
let completed:
| {
@@ -117,38 +126,68 @@ export const createCliResultOutput = (
const recoverFormattingFailure = async (cause: unknown): Promise<boolean> => {
// A rejected command never reaches write(). Destination failures retain
// handled ownership. Only a completed, unwritten result can be replaced.
const formattingCause =
cause instanceof ToonOutputCleanupFailure
? cause.outputFailure?.cause
: cause;
if (
completed === undefined ||
handled ||
hostRangeErrorKind(cause) !== "host-capacity" ||
!(cause instanceof Error) ||
cause.message !== "Invalid string length"
!(formattingCause instanceof Error)
)
return false;
const stringCapacity =
hostRangeErrorKind(formattingCause) === "host-capacity" &&
formattingCause.message === "Invalid string length";
const systemCode =
"code" in formattingCause ? formattingCause.code : undefined;
const stagingCapacity =
format === "toon" &&
(systemCode === "ENOSPC" ||
systemCode === "EDQUOT" ||
systemCode === "EFBIG")
? systemCode
: undefined;
if (!stringCapacity && stagingCapacity === undefined) return false;
const { originalFailure } = completed;
error = projectAnalysisError(
retainCliPublicationOnError(
new AnalysisResourceConstraintError(
completed.metadata.command,
"memory",
`The ${format} result could not fit in a runtime string. The original result was not written.`,
{
format,
formatted_characters: null,
max_string_characters: constants.MAX_STRING_LENGTH,
...(originalFailure === undefined
? {}
: { original_failure: originalFailure }),
},
{
cause,
retryable: false,
remediationAction:
"Use --format json to stream the complete result, or narrow it with --filter-output. Token limits are applied after formatting and cannot avoid this constraint.",
},
),
completed.receipt,
const refusal = retainCliPublicationOnError(
new AnalysisResourceConstraintError(
completed.metadata.command,
stagingCapacity === undefined ? "memory" : "capacity",
stagingCapacity === undefined
? `The ${format} result could not fit in a runtime string. The original result was not written.`
: "The temporary storage could not stage the TOON result. The original result was not written.",
{
format,
...(stagingCapacity === undefined
? {
formatted_characters: null,
max_string_characters: constants.MAX_STRING_LENGTH,
}
: { system_code: stagingCapacity }),
...(originalFailure === undefined
? {}
: { original_failure: originalFailure }),
},
{
cause: formattingCause,
retryable: false,
remediationAction:
stagingCapacity === undefined
? "Use --format json to stream the complete result, or narrow it with --filter-output. Token limits are applied after formatting and cannot avoid this constraint."
: "Free space in the system temporary directory, use --format json, or narrow the result with --filter-output.",
},
),
completed.receipt,
);
error = projectAnalysisError(
cause instanceof ToonOutputCleanupFailure
? analysisErrorWithCleanupDependencies(
refusal,
cause,
completed.metadata.command,
)
: refusal,
);
const document = options.fullOutput
? {
@@ -231,6 +270,12 @@ export const createCliResultOutput = (
}
return written;
} catch (cause: unknown) {
if (toonOutputCancellationStatus(cause) !== undefined) {
interrupted = true;
handled = true;
failed = true;
throw cause;
}
if (await recoverFormattingFailure(cause)) return true;
if (handled) failed = true;
throw cause;
@@ -251,11 +296,12 @@ export const createCliResultOutput = (
format = options.format as typeof format;
handled = false;
failed = false;
interrupted = false;
error = undefined;
completed = undefined;
},
finish() {
return writeOutputText(destination, "");
return interrupted ? Promise.resolve() : writeOutputText(destination, "");
},
write,
recoverFormattingFailure,
@@ -640,6 +686,17 @@ export const createCliResultOutput = (
}
return true;
}
if (format === "toon") {
await writeToonOutput(
document,
destination,
() => {
handled = true;
},
metadata.command,
);
return true;
}
// Once writing starts, a failed destination must not receive another document.
const formattedText =
format === "json" ? undefined : formatCliOutput(document, format);