Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(cli): keep whole characters when shortening publish progress (#2158)

wjf65ac53940d6
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 6215c33bb..cfb35d7cd 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -57,6 +57,7 @@ import {
stripVTControlCharacters,
} from "node:util";
import { Cli, z } from "incur";
+import stringWidth from "string-width";
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
import { formatCliHelp } from "./cli-help.js";
import { formatCoverageScope } from "./coverage-presentation.js";
@@ -625,6 +626,21 @@ function padPublicationColumn(value: string, width: number): string {
return `${value}${" ".repeat(width - publicationDisplayWidth(value))}`;
}
+function fitProgressLine(line: string, width: number): string {
+ const plain = stripVTControlCharacters(line);
+ let ellipsisIndex = 0;
+ let used = 0;
+ for (const { index, segment } of PUBLICATION_GRAPHEME_SEGMENTER.segment(
+ plain,
+ )) {
+ used += stringWidth(segment.toWellFormed());
+ if (used > width)
+ return width > 0 ? `${plain.slice(0, ellipsisIndex)}…` : "";
+ if (used < width) ellipsisIndex = index + segment.length;
+ }
+ return line;
+}
+
function publicationIssueUrl(value: string | undefined): string | undefined {
if (
value === undefined ||
@@ -875,10 +891,7 @@ class PublicationProgressPresenter {
return;
}
const width = Math.max(24, Math.min(this.#stream.columns ?? 120, 160));
- const visible =
- sanitized.length <= width
- ? sanitized
- : `${sanitized.slice(0, width - 1)}…`;
+ const visible = fitProgressLine(sanitized, width);
this.#stream.write(`${visible}\n`);
}
}
@@ -11261,19 +11274,8 @@ export class Progress {
}
#renderTimer(message: string): void {
- let line = this.#line(message);
const width = Math.max(0, (this.#stream.columns ?? 80) - 1);
- if (publicationDisplayWidth(line) > width) {
- let visible = "";
- let used = 0;
- for (const { segment } of PUBLICATION_GRAPHEME_SEGMENTER.segment(line)) {
- const segmentWidth = publicationDisplayWidth(segment);
- if (used + segmentWidth >= width) break;
- visible += segment;
- used += segmentWidth;
- }
- line = width > 0 ? `${visible}…` : "";
- }
+ const line = fitProgressLine(this.#line(message), width);
this.#stream.write(`${this.#timerLineActive ? "\r\u001B[K" : ""}${line}`);
this.#timerLineActive = true;
}
diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.ts
index 90aec1a5d..65828353d 100644
--- a/sdk/typescript/tests-ts/cli-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-publish.test.ts
@@ -2146,6 +2146,58 @@ describe("publish scan", () => {
expect(JSON.parse(stdout.text())).toEqual(publicationResult());
});
+ test.each([
+ ["exact ASCII", "a".repeat(113), "a".repeat(113)],
+ ["exact emoji", `${"a".repeat(111)}😀`, `${"a".repeat(111)}😀`],
+ ["wide text", "界".repeat(60), `${"界".repeat(56)}…`],
+ ["halfwidth text", "カ".repeat(113), "カ".repeat(113)],
+ ["wide vowel mark", "ー".repeat(60), `${"ー".repeat(56)}…`],
+ ["lone surrogate", `${"a".repeat(113)}\uD800`, `${"a".repeat(112)}…`],
+ [
+ "combining marks",
+ `${"a".repeat(111)}e\u0301 tail`,
+ `${"a".repeat(111)}e\u0301…`,
+ ],
+ ["joined emoji", `${"a".repeat(110)}👩‍💻 tail`, `${"a".repeat(110)}👩‍💻…`],
+ ["flag at cutoff", `${"a".repeat(111)}🇺🇸 tail`, `${"a".repeat(111)}…`],
+ ])(
+ "fits noninteractive progress without splitting graphemes: %s",
+ async (_name, text, expected) => {
+ const { stderr, runCli } = createCliTest(main);
+
+ const deps = dependencies();
+ deps.publishScan = async (_scanDirectory, options) => {
+ options.onProgress?.({
+ type: "codex_event",
+ event: {
+ type: "item.completed",
+ item: {
+ id: "reasoning-1",
+ type: "reasoning",
+ text,
+ },
+ },
+ });
+ return publicationResult();
+ };
+
+ expect(
+ await runCli(
+ [
+ "publish",
+ "scan",
+ "completed-scan",
+ ...DESTINATION_OPTIONS,
+ "--json",
+ ],
+ deps,
+ ),
+ ).toBe(0);
+
+ expect(stderr.text()).toBe(`Codex: ${expected}\n`);
+ },
+ );
+
test("hides source-bearing handoff commands in plain and full-screen progress", async () => {
for (const interactive of [false, true]) {
const { stdout, stderr, runCli } = createCliTest(main, {