fix(media-use): escape control characters in the remaining resolve and cube-validate lines (#8059)

#8013 escaped resolve's results, recipes, stats and error lines, and
#8087 the adopt skip line. A few resolve lines still printed their
values raw: argument errors that repeat --type, --provider, --from,
--reuse and the --for path, the measured-grade note and the
similar-asset hint that repeats the intent. cube-validate's error can
quote a value from the LUT file. They now go through the same
terminalText copy, so a control character shows as \xNN. --json output
is unchanged.
This commit is contained in:
Ular Kimsanov
2026-11-18 04:15:50 +00:00
committed by GitHub
parent a9b4745f1f
commit 9a096d72f3
4 changed files with 94 additions and 13 deletions
@@ -6,6 +6,7 @@
import { readFileSync } from "node:fs";
import { isMainModule } from "./main-module.mjs";
import { terminalText } from "./terminal-text.mjs";
export const DEFAULT_MAX_CUBE_LUT_SIZE = 65;
@@ -186,7 +187,7 @@ function main(argv) {
}
const result = validateCubeFile(file);
if (!result.ok) {
console.error(`error: ${result.error}`);
console.error(terminalText(`error: ${result.error}`));
process.exit(1);
}
console.log(`ok: LUT_3D_SIZE ${result.size}`);
@@ -128,3 +128,30 @@ test("CLI exits zero for valid files and non-zero for invalid files", () => {
rmSync(dir, { recursive: true, force: true });
}
});
test("the command prints a bad value from the file with control characters as visible escapes", () => {
const dir = mkdtempSync(join(tmpdir(), "cube-validate-escape-"));
try {
const file = join(dir, "bad.cube");
writeFileSync(file, "LUT_3D_SIZE 2\n0 0 1\x1b]52;c;aGk=\x07\n");
assert.throws(
() =>
execFileSync(
process.execPath,
[new URL("./cube-validate.mjs", import.meta.url).pathname, file],
{
encoding: "utf8",
stdio: "pipe",
},
),
(err) => {
const stderr = String(err.stderr);
assert.ok(stderr.includes("1\\x1b]52;c;aGk=\\x07"), stderr);
for (const raw of ["\x07", "\x1b"]) assert.ok(!stderr.includes(raw), stderr);
return err.status === 1;
},
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
+26 -12
View File
@@ -250,7 +250,7 @@ if (args.analyze) {
}
const mediaPath = resolve(args.for);
if (!existsSync(mediaPath)) {
console.error(`error: --for file not found: ${mediaPath}`);
console.error(terminalText(`error: --for file not found: ${mediaPath}`));
process.exit(2);
}
let analysis;
@@ -265,7 +265,7 @@ if (args.analyze) {
if (args.json) {
console.log(JSON.stringify({ ok: true, type: "grade-analysis", ...analysis }));
} else {
console.log(formatMeasuredNote(mediaPath, analysis.measured));
console.log(terminalText(formatMeasuredNote(mediaPath, analysis.measured)));
console.log(`suggested adjust: ${JSON.stringify(analysis.adjust)}`);
}
process.exit(0);
@@ -320,7 +320,9 @@ if (!args.type || !args.intent || !args.intent.trim()) {
}
if (!listTypes().includes(args.type)) {
console.error(`error: unknown media type: ${args.type} (known: ${listTypes().join(", ")})`);
console.error(
terminalText(`error: unknown media type: ${args.type} (known: ${listTypes().join(", ")})`),
);
process.exit(2);
}
@@ -329,7 +331,9 @@ if (!listTypes().includes(args.type)) {
// resolve`). Match rule mirrors runProviders (full name or dotted prefix).
if (args.provider && !providerMatches(args.type, args.provider)) {
console.error(
`error: unknown provider "${args.provider}" for type ${args.type} (available: ${providerNamesFor(args.type).join(", ")})`,
terminalText(
`error: unknown provider "${args.provider}" for type ${args.type} (available: ${providerNamesFor(args.type).join(", ")})`,
),
);
process.exit(2);
}
@@ -452,7 +456,9 @@ async function run() {
const { similar } = listCandidates({ projectDir, type, intent, cap: CANDIDATE_CAP });
if (similar > 0) {
console.error(
`media-use: ${similar} similar cached asset${similar === 1 ? "" : "s"} already ${similar === 1 ? "exists" : "exist"} — run \`resolve --candidates --type ${type} --intent "${intent}"\` to review and reuse instead of fetching.`,
terminalText(
`media-use: ${similar} similar cached asset${similar === 1 ? "" : "s"} already ${similar === 1 ? "exists" : "exist"} — run \`resolve --candidates --type ${type} --intent "${intent}"\` to review and reuse instead of fetching.`,
),
);
}
} catch {
@@ -627,7 +633,7 @@ function mergeSmartAdjust(block) {
ffmpegPath: ffmpegBinary(),
ffprobePath: ffprobeBinary(),
});
console.error(formatMeasuredNote(mediaPath, analysis.measured));
console.error(terminalText(formatMeasuredNote(mediaPath, analysis.measured)));
return {
...block,
adjust: {
@@ -904,18 +910,20 @@ async function ingest(src) {
const isUrl = /^https?:\/\//i.test(src);
if (isUrl && !isDirectMediaUrl(src)) {
console.error(
`error: --from takes a direct public media URL or a local file; "${src}" is not a direct media link (no platform pages / yt-dlp)`,
terminalText(
`error: --from takes a direct public media URL or a local file; "${src}" is not a direct media link (no platform pages / yt-dlp)`,
),
);
process.exit(2);
}
if (!isUrl && !existsSync(resolve(src))) {
console.error(`error: file not found: ${src}`);
console.error(terminalText(`error: file not found: ${src}`));
process.exit(2);
}
// Refuse 0-byte input: an empty asset would register clean but fail at render
// (freezeUrl already rejects empty responses; this covers local files).
if (!isUrl && statSync(resolve(src)).size === 0) {
console.error(`error: refusing to ingest a 0-byte file: ${src}`);
console.error(terminalText(`error: refusing to ingest a 0-byte file: ${src}`));
process.exit(2);
}
if (args.source && !AGENT_SOURCES.includes(args.source)) {
@@ -1275,13 +1283,17 @@ async function reuseGlobal(shaArg) {
const rec = findGlobalBySha(shaArg);
if (rec && rec.ambiguous) {
console.error(
`error: sha prefix "${shaArg}" is ambiguous (${rec.count} matches) — use more characters`,
terminalText(
`error: sha prefix "${shaArg}" is ambiguous (${rec.count} matches) — use more characters`,
),
);
process.exit(2);
}
if (!rec) {
console.error(
`error: no reusable global asset matches sha "${shaArg}". --reuse takes a sha from \`resolve --candidates\`; to skip the caches, pass --provider <name> instead`,
terminalText(
`error: no reusable global asset matches sha "${shaArg}". --reuse takes a sha from \`resolve --candidates\`; to skip the caches, pass --provider <name> instead`,
),
);
process.exit(1);
}
@@ -1295,7 +1307,9 @@ async function reuseGlobal(shaArg) {
localizeImportedRecord(importFromCache(rec, projectDir, id, localPath), localPath),
);
if (!imported) {
console.error(`error: cache entry for "${shaArg}" is incomplete or missing on disk`);
console.error(
terminalText(`error: cache entry for "${shaArg}" is incomplete or missing on disk`),
);
process.exit(1);
}
// Distinguish an explicit agent reuse from an automatic normalize-exact hit.
@@ -585,6 +585,45 @@ test("recipe and stats output print control characters as visible escapes", () =
}
});
test("argument errors and the similar-asset hint print control characters as visible escapes", () => {
setup();
try {
const record = makeRecord({ provenance: { prompt: "cached query", provider: "test" } });
appendRecord(tmp, record);
const filePath = join(tmp, record.path);
mkdirSync(join(filePath, ".."), { recursive: true });
writeFileSync(filePath, "cached audio");
const evil = "\x1b]52;c;aGk=\x07";
const shown = "\\x1b]52;c;aGk=\\x07";
const runs = [
["unknown media type", "--type", `bgm${evil}`, "--intent", "x"],
["unknown provider", "--type", "bgm", "--intent", "x", "--provider", `p${evil}`],
["file not found", "--type", "bgm", "--from", join(tmp, `missing${evil}.wav`)],
["no reusable global asset", "--type", "bgm", "--reuse", `ab${evil}`],
[
"similar cached asset",
"--type",
"bgm",
"--intent",
`cached query song${evil}`,
"--local-only",
],
];
for (const [line, ...args] of runs) {
const { stderr } = spawnResolve([...args, "--project", tmp]);
const printed = stderr.split("\n").find((l) => l.includes(line)) ?? "";
assert.ok(printed.includes(shown), `${line}\n${stderr}`);
for (const raw of ["\x07", "\x1b"])
assert.ok(
!stderr.includes(raw),
`no raw control character reaches the terminal: ${stderr}`,
);
}
} finally {
cleanup();
}
});
test("entity hit matches across icon/image (figma-imported brand marks)", () => {
setup();
const record = makeRecord({