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(dashboard): keep dashboards responsive and show observed usage (#1276)

Michael D'Angelo3ede902c0901
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index 6f3e01e79..685ced3df 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -442,6 +442,8 @@ def _read_rollout_usage(
continue
if event.get("type") != "event_msg" or payload.get("type") != "token_count":
continue
+ if "info" in payload and payload["info"] is None:
+ continue
timestamp = _timestamp(event.get("timestamp"))
snapshot = _token_snapshot(payload)
if timestamp is None or snapshot is None:
diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.py
index 9133b0f20..dafd687f3 100644
--- a/plugins/codex-security/tests/test_workbench_scan_usage.py
+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py
@@ -255,6 +255,32 @@ def _complete_scan(fixture: ScanFixture) -> dict[str, Any]:
)
+ ("info", "coverage"),
+ [("missing", "partial"), ("null", "complete"), ("invalid", "partial"), ("valid", "complete")],
+)
+def test_completion_preserves_usage_record_warnings(
+ tmp_path: Path, info: str, coverage: str
+) -> None:
+ fixture = _start_scan(tmp_path)
+ counted = fixture.started_at + timedelta(microseconds=1)
+ event = _token_event(counted, 10, 5)
+ if info == "missing":
+ del event["payload"]["info"]
+ elif info == "null":
+ event["payload"]["info"] = None
+ elif info == "invalid":
+ event["payload"]["info"] = {}
+ parent = _rollout(tmp_path, "scan-parent", [event, _token_event(counted, 20, 10)])
+ _state_graph(fixture.environment, {"scan-parent": parent}, [])
+
+ usage = _complete_scan(fixture)["scan"]["usage"]
+
+ assert usage["coverage"] == coverage
+ assert usage["totalTokens"] == 30
+ assert usage.get("warnings", []) == (["token_record_invalid"] if coverage == "partial" else [])
+
+
@pytest.mark.parametrize("mode", ["standard", "diff"])
def test_completion_counts_only_scan_owned_parent_and_descendants(
tmp_path: Path,
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 80895edb6..1bf4f7dc5 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -90,6 +90,7 @@
"semver": "7.8.5",
"smol-toml": "1.9.0",
"string-width": "8.3.0",
+ "strip-ansi": "7.2.0",
"yaml": "2.9.1",
"yauzl": "3.4.0",
"zod": "4.6.5"
diff --git a/sdk/typescript/pnpm-lock.yaml b/sdk/typescript/pnpm-lock.yaml
index b3390221b..fb0e268c0 100644
--- a/sdk/typescript/pnpm-lock.yaml
+++ b/sdk/typescript/pnpm-lock.yaml
@@ -61,6 +61,9 @@ importers:
string-width:
specifier: 8.3.0
version: 8.3.0
+ strip-ansi:
+ specifier: 7.2.0
+ version: 7.2.0
yaml:
specifier: 2.9.1
version: 2.9.1
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index ab1365b8e..b3d804ba4 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -83,6 +83,7 @@ import {
type ScanSessionEvent,
type ScanWorkerEvent,
} from "./cost.js";
+import type { ScanTokenUsage } from "./cost-model.js";
import {
DeepScanProgressTracker,
type DeepScanProgress,
@@ -315,6 +316,8 @@ export interface ScanOptions extends ScanSettings {
parentScanId?: string;
expectedPluginVersion?: string;
onCost?: (cost: Readonly<ScanCost>, maxCostUsd?: number) => void;
+ /** Observed tokens, including models without a price estimate. */
+ onUsage?: (usage: Readonly<ScanTokenUsage>) => void;
onBudgetApproaching?: (
budget: ScanBudget,
) => number | undefined | Promise<number | undefined>;
@@ -426,6 +429,7 @@ export interface ScanBudget {
type ScanObserverName =
| "onAuthentication"
| "onCost"
+ | "onUsage"
| "onOutputArchived"
| "onOutputDirReady"
| "onScanRegistered"
@@ -1580,6 +1584,10 @@ export class CodexSecurity {
repository: repo,
scanDirectory: scanDir,
maxCostUsd: options.maxCostUsd,
+ onUsage:
+ options.onUsage === undefined
+ ? undefined
+ : (usage) => notifyObserver(options, "onUsage")(usage),
onActivity:
options.onActivity === undefined
? undefined
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 42dc6e7be..31a5675aa 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -144,6 +144,7 @@ import {
formatScanCost,
formatScanCostTokens,
formatTokenUsage,
+ type ScanTokenUsage,
} from "./cost-model.js";
import { readRegularInputFile, resolveScanPrompts } from "./prompt-files.js";
import {
@@ -8205,6 +8206,7 @@ async function executeScan(
let deepProgress: DeepScanProgress | null = null;
let deepConsolidating = false;
let runningCost: Readonly<ScanCost> | null = null;
+ let runningUsage: Readonly<ScanTokenUsage> | null = null;
let maxCostUsd = arguments_.maxCostUsd;
const showCost = arguments_.showCost === true || maxCostUsd !== undefined;
let phase: string | null = null;
@@ -8430,9 +8432,13 @@ async function executeScan(
`Files: ${fileProgress.filesCompleted.toLocaleString("en-US")}/${fileProgress.filesTotal.toLocaleString("en-US")}`,
);
}
- if (runningCost !== null) {
- details.push(`Tokens: ${formatScanCostTokens(runningCost)}`);
- if (showCost) details.push(`Cost: ${formatScanCost(runningCost)}`);
+ const tokens =
+ runningCost !== null
+ ? formatScanCostTokens(runningCost)
+ : formatTokenUsage(runningUsage);
+ if (tokens !== null) details.push(`Tokens: ${tokens}`);
+ if (showCost && runningCost !== null) {
+ details.push(`Cost: ${formatScanCost(runningCost)}`);
}
return details.length === 0 ? stage : `${stage} | ${details.join(" | ")}`;
};
@@ -8471,6 +8477,20 @@ async function executeScan(
archiveExisting: arguments_.archiveExisting,
parentScanId: arguments_.parentScanId,
expectedPluginVersion: arguments_.expectedPluginVersion,
+ onUsage: async (usage) => {
+ runningUsage = usage;
+ if (dashboard !== null) {
+ dashboard.setUsage(usage);
+ return;
+ }
+ const priorCost = runningCost;
+ // The tracker queues usage before cost; let the paired cost update render once.
+ await Promise.resolve();
+ if (runningCost !== priorCost) return;
+ progress?.stopTimer();
+ progress?.stage(`Tokens: ${formatTokenUsage(usage)}.`);
+ progress?.startTimer(runningMessage());
+ },
onCost: (cost, limit = maxCostUsd) => {
if (limit !== maxCostUsd && limit !== undefined) {
dashboard?.note(`Total cost limit increased to ${formatUsd(limit)}.`);
@@ -9522,9 +9542,14 @@ function componentScanEventLine(
const progress = event.value;
return `codex-security: ${componentName} ${scanPhase(progress.phase)} | Files: ${progress.filesCompleted.toLocaleString("en-US")}/${progress.filesTotal.toLocaleString("en-US")}\n`;
}
- if (event.type !== "cost") return null;
- const cost = event.value;
- return `codex-security: ${componentName} | Tokens: ${formatScanCostTokens(cost)}${showCost ? ` | Cost: ${formatScanCost(cost)}` : ""}\n`;
+ if (event.type === "usage") {
+ const tokens = formatTokenUsage(event.value);
+ return tokens === null
+ ? null
+ : `codex-security: ${componentName} | Tokens: ${tokens}\n`;
+ }
+ if (event.type !== "cost" || !showCost) return null;
+ return `codex-security: ${componentName} | Cost: ${formatScanCost(event.value)}\n`;
}
function protectedRootErrorMessage(
diff --git a/sdk/typescript/src/component-scan.ts b/sdk/typescript/src/component-scan.ts
index 7c8fe64f3..5a088715e 100644
--- a/sdk/typescript/src/component-scan.ts
+++ b/sdk/typescript/src/component-scan.ts
@@ -22,6 +22,7 @@ import {
type CodexSecurityConfig,
} from "./config.js";
import type { ScanCost, ScanSessionEvent } from "./cost.js";
+import type { ScanTokenUsage } from "./cost-model.js";
import { readThreatModelPath } from "./artifact-export.js";
import { errorMessage } from "./errors.js";
import type { CoverageCompleteness, Finding } from "./models.js";
@@ -84,6 +85,7 @@ type ComponentScanUpdate =
| { type: "activity"; value: ScanActivity }
| { type: "session"; value: ScanSessionEvent }
| { type: "cost"; value: Readonly<ScanCost> }
+ | { type: "usage"; value: Readonly<ScanTokenUsage> }
| { type: "workers"; value: ScanWorkerStatus }
| { type: "warning"; value: string };
@@ -216,6 +218,7 @@ export async function runComponentScans(
onActivity: (value) => emit({ type: "activity", value }),
onSessionEvent: (value) => emit({ type: "session", value }),
onCost: (value) => emit({ type: "cost", value }),
+ onUsage: (value) => emit({ type: "usage", value }),
onWorkerStatus: (value) => emit({ type: "workers", value }),
onWarning: (value) => emit({ type: "warning", value }),
};
diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.ts
index 27554b27f..2e581da08 100644
--- a/sdk/typescript/src/cost.ts
+++ b/sdk/typescript/src/cost.ts
@@ -1,5 +1,5 @@
import { parseJson } from "./value.js";
-import { open, readdir } from "node:fs/promises";
+import { open, readdir, stat } from "node:fs/promises";
import { join } from "node:path";
import { StringDecoder } from "node:string_decoder";
import { isRecord } from "./record.js";
@@ -78,6 +78,7 @@ interface ScanCostTrackerOptions {
maxCostUsd?: number;
expectedFilesTotal?: number;
onCost?: (cost: Readonly<ScanCost>) => void;
+ onUsage?: (usage: Readonly<ScanTokenUsage>) => void;
onActivity?: (activity: ScanActivity) => void;
onProgress?: (progress: ScanProgress) => void;
onSessionEvent?: (event: ScanSessionEvent) => void;
@@ -130,6 +131,7 @@ export class ScanCostTracker {
#pending: Promise<void> = Promise.resolve();
#snapshot: ScanCostSnapshot = { usage: null, cost: null };
#lastCost: string | null = null;
+ #lastUsage: string | null = null;
#highestFilesCompleted = 0;
#expectedFilesTotal: number | undefined;
@@ -155,6 +157,7 @@ export class ScanCostTracker {
if (
this.#options.maxCostUsd === undefined &&
this.#options.onCost === undefined &&
+ this.#options.onUsage === undefined &&
this.#options.onActivity === undefined &&
this.#options.onProgress === undefined &&
this.#options.onSessionEvent === undefined &&
@@ -209,6 +212,7 @@ export class ScanCostTracker {
return this.#snapshot;
const cost = estimateScanCost(this.#options.model, fallbackUsage);
this.#snapshot = { usage: fallbackUsage ?? null, cost };
+ this.#reportUsage(tokenUsage(fallbackUsage));
this.#reportCost(cost);
return this.#snapshot;
}
@@ -345,6 +349,7 @@ export class ScanCostTracker {
if (usage === null) return;
const cost = estimateScanCost(this.#options.model, usage);
this.#snapshot = { usage, cost };
+ this.#reportUsage(usage);
this.#reportCost(cost);
}
@@ -394,6 +399,14 @@ export class ScanCostTracker {
this.#lastCost = signature;
this.#options.onCost?.(cost);
}
+
+ #reportUsage(usage: ScanTokenUsage | null): void {
+ if (usage === null) return;
+ const signature = JSON.stringify(usage);
+ if (signature === this.#lastUsage) return;
+ this.#lastUsage = signature;
+ this.#options.onUsage?.(usage);
+ }
}
export async function* sessionFiles(
@@ -429,6 +442,7 @@ async function readSessionUsage(
if (session.unreadable) return;
let file;
try {
+ if ((await stat(path)).size === session.offset) return;
file = await open(path, "r");
} catch (error) {
if (isMissingFile(error)) return;
diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.ts
index 52bbe9376..f9e8dd83b 100644
--- a/sdk/typescript/src/index.ts
+++ b/sdk/typescript/src/index.ts
@@ -52,6 +52,7 @@ export type {
} from "./component-plan.js";
export { estimateScanCost } from "./cost.js";
export type { ScanCost, ScanSessionEvent, ScanWorkerEvent } from "./cost.js";
+export type { ScanTokenUsage } from "./cost-model.js";
export type { DeepScanProgress } from "./deep-progress.js";
export type { CustomValidationResult } from "./custom-validation.js";
export type { ScanActivity, ScanActivityStatus } from "./scan-activity.js";
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.ts
index b95bd5127..39126b3a9 100644
--- a/sdk/typescript/src/scan-activity.ts
+++ b/sdk/typescript/src/scan-activity.ts
@@ -279,7 +279,7 @@ function proseActivity(
function prose(text: string, limit?: number): string {
const normalized = text
.split(
- /(`{3,}[^\r\n]*(?:\r?\n[\s\S]*?(?:\r?\n[ \t]*`{3,}[ \t]*(?=\r?\n|$)|$)|$)|`[^`\r\n]+`)/gu,
+ /((?<!`)`{3,}[^`\r\n]*(?:\r?\n[\s\S]*?(?:\r?\n[ \t]*`{3,}[ \t]*(?=\r?\n|$)|$)|$)|`[^`\r\n]+`)/gu,
)
.reduce((result, part, index) => {
if (index % 2 === 1) {
diff --git a/sdk/typescript/src/scan-dashboard.ts b/sdk/typescript/src/scan-dashboard.ts
index 4e4a80aea..74be02927 100644
--- a/sdk/typescript/src/scan-dashboard.ts
+++ b/sdk/typescript/src/scan-dashboard.ts
@@ -2,7 +2,8 @@ import { basename, isAbsolute } from "node:path";
import { emitKeypressEvents } from "node:readline";
import { PassThrough } from "node:stream";
import { pathToFileURL } from "node:url";
-import { stripVTControlCharacters } from "node:util";
+import stripAnsi from "strip-ansi";
+import stringWidth from "string-width";
import { isRecord } from "./record.js";
import type { ScanBudget } from "./api.js";
import type { ScanModelConfiguration } from "./config.js";
@@ -17,6 +18,8 @@ import {
formatScanCost,
formatScanCosts,
formatScanCostTokens,
+ formatTokenUsage,
+ type ScanTokenUsage,
} from "./cost-model.js";
import type { ScanActivity } from "./scan-activity.js";
import type { ScanMode } from "./targets.js";
@@ -43,6 +46,7 @@ interface DashboardStream {
interface DashboardClock {
now(): number;
+ queueMicrotask?(callback: () => void): void;
setInterval(callback: () => void, milliseconds: number): NodeJS.Timeout;
clearInterval(timer: NodeJS.Timeout): void;
}
@@ -126,6 +130,10 @@ export class ScanDashboard {
#showComponent = false;
#componentResult: ComponentScanResult | null = null;
readonly #activities: TimedScanActivity[] = [];
+ readonly #activityCache = new WeakMap<
+ TimedScanActivity,
+ { width: number; icon: string | undefined; lines: DashboardActivityLine[] }
+ >();
readonly #details: (ScanSessionEvent & { recordedAt: number })[] = [];
#detailsCache: {
width: number;
@@ -138,6 +146,7 @@ export class ScanDashboard {
#files: ScanProgress | null = null;
#publicationProgress: { completed: number; total: number } | null = null;
#cost: Readonly<ScanCost> | null = null;
+ #tokens: string | null = null;
#budget: {
request: ScanBudget;
input: string;
@@ -145,6 +154,7 @@ export class ScanDashboard {
finish: (limit?: number) => void;
} | null = null;
#timer: NodeJS.Timeout | null = null;
+ #refreshPending = false;
#scrollOffset = 0;
#view: "activity" | "details" = "activity";
#source: "all" | "main" | number = "all";
@@ -481,6 +491,9 @@ export class ScanDashboard {
case "cost":
dashboard.setCost(event.value);
break;
+ case "usage":
+ dashboard.setUsage(event.value);
+ break;
case "workers":
if (event.value.kind === "dispatch")
dashboard.setStage(scanPhaseLabel(event.value.phase));
@@ -489,7 +502,7 @@ export class ScanDashboard {
dashboard.note(event.value);
break;
}
- this.#refresh();
+ this.#scheduleRefresh();
}
public showComponents(stage: string): void {
@@ -523,10 +536,16 @@ export class ScanDashboard {
maxCostUsd = this.#options.maxCostUsd,
): void {
this.#cost = cost;
+ this.#tokens = formatScanCostTokens(cost);
this.#options.maxCostUsd = maxCostUsd;
this.#refresh();
}
+ public setUsage(usage: Readonly<ScanTokenUsage>): void {
+ this.#tokens = formatTokenUsage(usage);
+ this.#refresh();
+ }
+
public requestBudgetIncrease(
request: ScanBudget,
): Promise<number | undefined> {
@@ -593,7 +612,7 @@ export class ScanDashboard {
this.#activityLines(this.#width()).length - previousRows,
);
}
- this.#refresh();
+ this.#scheduleRefresh();
}
public recordDetails(session: ScanSessionEvent): void {
@@ -624,7 +643,7 @@ export class ScanDashboard {
this.#activityLines(this.#width()).length - previousRows,
);
}
- this.#refresh();
+ this.#scheduleRefresh();
}
}
@@ -647,6 +666,15 @@ export class ScanDashboard {
} catch {}
}
+ #scheduleRefresh(): void {
+ if (this.#timer === null || this.#refreshPending) return;
+ this.#refreshPending = true;
+ (this.#options.clock.queueMicrotask ?? queueMicrotask)(() => {
+ this.#refreshPending = false;
+ this.#refresh();
+ });
+ }
+
#frame(): string {
if (this.#options.presentation === "components") {
return this.#showComponent
@@ -835,6 +863,10 @@ export class ScanDashboard {
10,
width - 52 - (this.#showCost ? costWidth + 1 : 0),
);
+ const cell = (value: string, width: number): string => {
+ const text = fitLine(value, width);
+ return text + " ".repeat(width - stringWidth(text));
+ };
const row = (
marker: string,
name: string,
@@ -843,7 +875,7 @@ export class ScanDashboard {
findings: string,
cost: string,
): string =>
- ` ${marker} ${fitLine(name, nameWidth).padEnd(nameWidth)} ${fitLine(status, 24).padEnd(24)} ${files.padStart(11)} ${findings.padStart(8)}${this.#showCost ? ` ${cost.padStart(8)}` : ""}`;
+ ` ${marker} ${cell(name, nameWidth)} ${cell(status, 24)} ${files.padStart(11)} ${findings.padStart(8)}${this.#showCost ? ` ${cost.padStart(8)}` : ""}`;
const table = this.#components
.slice(first, first + rows)
.map(({ receipt, dashboard }, index) => {
@@ -926,7 +958,8 @@ export class ScanDashboard {
this.#options.presentation === "verification"
? 0
: this.#tokenLines().length - 1 + this.#costLines().length - 1) +
- (this.#options.presentation === "publication"
+ (this.#options.presentation === "publication" ||
+ this.#options.presentation === "verification"
? 2
: this.#options.mode === "deep"
? 2
@@ -935,10 +968,7 @@ export class ScanDashboard {
}
#tokenLines(): string[] {
- const tokens =
- this.#cost === null
- ? "waiting for usage"
- : formatScanCostTokens(this.#cost);
+ const tokens = this.#tokens ?? "waiting for usage";
return wrapActivity(" TOKENS ", tokens, this.#width());
}
@@ -1123,6 +1153,12 @@ export class ScanDashboard {
: entry.status === "running"
? runningIcon
: ACTIVITY_MARKERS[kind];
+ const cached = this.#activityCache.get(entry);
+ if (cached?.width === width && cached.icon === icon) {
+ for (const line of cached.lines) lines.push(line);
+ continue;
+ }
+ const first = lines.length;
const timestamp = `[${formatLocalTime(entry.recordedAt)}]`;
const worker =
entry.worker === undefined ? "" : `worker ${entry.worker} · `;
@@ -1131,6 +1167,11 @@ export class ScanDashboard {
for (const path of entry.paths) {
append(" ".repeat(prefix.length), path, "path");
}
+ this.#activityCache.set(entry, {
+ width,
+ icon,
+ lines: lines.slice(first),
+ });
}
return lines;
}
@@ -1200,12 +1241,29 @@ function detailsText(value: unknown): string {
.join("\n");
}
+function replaceVisibleText(
+ value: string,
+ search: string,
+ replacement: string,
+): string {
+ let replaced = false;
+ return value
+ .split(/(\u001B\[[0-?]*[ -/]*[@-~]|\u001B\][\s\S]*?(?:\u0007|\u001B\\))/gu)
+ .map((part, index) => {
+ if (index % 2 === 1 || replaced || !part.includes(search)) return part;
+ replaced = true;
+ return part.replace(search, () => replacement);
+ })
+ .join("");
+}
+
function styleInlineCode(value: string, line: DashboardActivityLine): string {
for (const text of line.bold ?? []) {
- value = value.replace(text, `\u001B[1m${text}\u001B[22m`);
+ value = replaceVisibleText(value, text, `\u001B[1m${text}\u001B[22m`);
}
for (const text of line.code ?? []) {
- value = value.replace(
+ value = replaceVisibleText(
+ value,
text,
`\u001B[2m${text}\u001B[22m${line.kind === "message" ? "\u001B[1m" : ""}`,
);
@@ -1220,7 +1278,8 @@ function linkActivity(
for (const { label, target } of links ?? []) {
const safe = safeHyperlinkTarget(target);
if (safe !== undefined) {
- value = value.replace(
+ value = replaceVisibleText(
+ value,
label,
`\u001B]8;;${safe}\u0007${label}\u001B]8;;\u0007`,
);
@@ -1269,7 +1328,8 @@ function formatLocalTime(timestamp: number): string {
}
function wrapActivity(prefix: string, value: string, width: number): string[] {
- const available = Math.max(1, width - prefix.length);
+ prefix = width <= 2 ? "" : fitLine(prefix, width - 2);
+ const available = Math.max(1, width - stringWidth(prefix));
const continuation = " ".repeat(prefix.length);
const lines: string[] = [];
const append = (text: string): void => {
@@ -1277,35 +1337,51 @@ function wrapActivity(prefix: string, value: string, width: number): string[] {
};
let current = "";
let separator = "";
- for (const word of value.split(/(\s+)/u)) {
+ let currentWidth = 0;
+ const widths = new Map<string, number>();
+ const wrappedWords = new Map<string, ReturnType<typeof columnChunks>>();
+ const text = terminalText(value);
+ const segments = graphemes.segment(text);
+ let offset = 0;
+ for (const word of text.split(/(\s+)/u)) {
+ const start = offset;
+ offset += word.length;
if (/^\s+$/u.test(word)) {
separator = word;
continue;
}
- const characters = Array.from(word);
- if (characters.length > available) {
+ const ascii = /^[\u0020-\u007E]*$/u.test(word);
+ let parts = ascii ? undefined : wrappedWords.get(word);
+ if (!ascii && parts === undefined) {
+ parts = columnChunks(word, available, widths, segments, start);
+ wrappedWords.set(word, parts);
+ }
+ const wordWidth = ascii
+ ? word.length
+ : parts!.length > 1
+ ? available + 1
+ : parts![0]!.columns;
+ if (wordWidth > available) {
if (current !== "") {
append(current);
current = "";
}
- for (let start = 0; start < characters.length; start += available) {
- const part = characters.slice(start, start + available).join("");
- if (start + available < characters.length) {
- append(part);
- } else {
- current = part;
- }
- }
+ const chunks =
+ parts ?? columnChunks(word, available, widths, segments, start);
+ const last = chunks.at(-1)!;
+ current = last.text;
+ currentWidth = last.columns;
+ for (let index = 0; index < chunks.length - 1; index++)
+ append(chunks[index]!.text);
} else if (
current !== "" &&
- Array.from(current).length +
- Array.from(separator).length +
- characters.length >
- available
+ currentWidth + stringWidth(separator) + wordWidth > available
) {
append(current);
current = word;
+ currentWidth = wordWidth;
} else {
+ currentWidth += (current === "" ? 0 : stringWidth(separator)) + wordWidth;
current = current === "" ? word : `${current}${separator}${word}`;
}
separator = "";
@@ -1314,16 +1390,73 @@ function wrapActivity(prefix: string, value: string, width: number): string[] {
return lines;
}
+const graphemes = new Intl.Segmenter();
+
+function columnChunks(
+ value: string,
+ width: number,
+ widths = new Map<string, number>(),
+ segments = graphemes.segment(value),
+ offset = 0,
+): { text: string; columns: number }[] {
+ const parts: { text: string; columns: number }[] = [];
+ let current = "";
+ let columns = 0;
+ const append = (text: string, size: number): void => {
+ if (current !== "" && columns + size > width) {
+ parts.push({ text: current, columns });
+ current = "";
+ columns = 0;
+ }
+ current += text;
+ columns += size;
+ };
+ const appendGraphemes = (start: number, end: number): void => {
+ while (start < end) {
+ const next = segments.containing(offset + start)!;
+ const boundary = Math.min(end, next.index + next.segment.length - offset);
+ const segment = value.slice(start, boundary);
+ let size = widths.get(segment);
+ if (size === undefined) {
+ size = stringWidth(segment);
+ widths.set(segment, size);
+ }
+ append(segment, size);
+ start = boundary;
+ }
+ };
+ let cursor = 0;
+ for (const match of value.matchAll(/[\u0020-\u007E]{2,}/gu)) {
+ // Keep edge characters with Unicode that can attach to their grapheme.
+ appendGraphemes(cursor, match.index + 1);
+ let start = match.index + 1;
+ const end = match.index + match[0].length - 1;
+ while (start < end) {
+ if (columns >= width) {
+ parts.push({ text: current, columns });
+ current = "";
+ columns = 0;
+ }
+ const size = Math.min(width - columns, end - start);
+ append(value.slice(start, start + size), size);
+ start += size;
+ }
+ cursor = end;
+ }
+ appendGraphemes(cursor, value.length);
+ if (current !== "" || parts.length === 0)
+ parts.push({ text: current, columns });
+ return parts;
+}
+
function wrapCode(prefix: string, value: string, width: number): string[] {
- const characters = Array.from(value);
- const available = Math.max(1, width - prefix.length);
- return Array.from(
- { length: Math.max(1, Math.ceil(characters.length / available)) },
- (_, index) =>
- `${index === 0 ? prefix : " ".repeat(prefix.length)}${characters
- .slice(index * available, (index + 1) * available)
- .join("")}`,
- );
+ prefix = width <= 2 ? "" : fitLine(prefix, width - 2);
+ const prefixWidth = stringWidth(prefix);
+ const continuation = " ".repeat(prefixWidth);
+ return columnChunks(
+ terminalText(value),
+ Math.max(1, width - prefixWidth),
+ ).map((part, index) => `${index === 0 ? prefix : continuation}${part.text}`);
}
function styleLine(
@@ -1390,13 +1523,19 @@ function budgetBar(cost: number, limit: number): string {
return `[${"█".repeat(filled)}${"░".repeat(12 - filled)}] ${Math.round(proportion * 100)}%`;
}
+function terminalText(value: string): string {
+ return stripAnsi(value).replaceAll(/[\u0000-\u001F\u007F]/gu, " ");
+}
+
function fitLine(value: string, width: number): string {
- const clean = stripVTControlCharacters(value).replaceAll(
- /[\u0000-\u001F\u007F]/gu,
- " ",
- );
- const characters = Array.from(clean);
- return characters.length <= width
- ? clean
- : `${characters.slice(0, Math.max(0, width - 1)).join("")}…`;
+ const clean = terminalText(value);
+ if (stringWidth(clean) <= width) return clean;
+ let clipped = "";
+ let columns = 0;
+ for (const { segment } of graphemes.segment(clean)) {
+ columns += stringWidth(segment);
+ if (columns > width - 1) break;
+ clipped += segment;
+ }
+ return `${clipped}…`;
}
diff --git a/sdk/typescript/src/scan-history-renderer.ts b/sdk/typescript/src/scan-history-renderer.ts
index 879d373d2..bf28dc03d 100644
--- a/sdk/typescript/src/scan-history-renderer.ts
+++ b/sdk/typescript/src/scan-history-renderer.ts
@@ -121,7 +121,7 @@ export function renderScanHistory(
const after = entry["afterOccurrenceIds"] as string[] | undefined;
const grouped =
before?.length || after?.length
- ? ` ${accent("·")} ${before?.length ?? 1} → ${after?.length ?? 1}`
+ ? ` ${accent("·")} ${before?.length ?? (entry["beforeOccurrenceId"] ? 1 : 0)} → ${after?.length ?? (entry["afterOccurrenceId"] ? 1 : 0)}`
: "";
const matches = entry["matches"] as JsonObject[] | undefined;
const related = entry["related"] as JsonObject[] | undefined;
@@ -286,6 +286,11 @@ export function renderScanHistory(
lines.push(
` ${Object.entries(summary)
.filter(([, count]) => count)
+ .sort(
+ ([left], [right]) =>
+ Object.keys(SEVERITY_COLORS).indexOf(left.toUpperCase()) -
+ Object.keys(SEVERITY_COLORS).indexOf(right.toUpperCase()),
+ )
.map(([severity, count]) => {
const label = severity.toUpperCase();
return paint(
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index f5c262c63..dcf2ab3a9 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -3770,7 +3770,7 @@ describe("CodexSecurity orchestration", () => {
join(sessions, "a-synthetic.jsonl"),
join(sessions, "b-synthetic.jsonl"),
];
- await Promise.all(logs.map((path) => writeFile(path, "")));
+ await Promise.all(logs.map((path) => writeFile(path, "{}\n")));
const denied = new Set([logs[0]!]);
const attempts = new Map<string, number>();
let firstRepeated!: () => void;
@@ -3811,6 +3811,7 @@ describe("CodexSecurity orchestration", () => {
await first;
denied.delete(logs[0]!);
denied.add(logs[1]!);
+ await appendFile(logs[1]!, "{}\n");
await second;
denied.delete(logs[1]!);
for await (const event of completedEvents()) {
@@ -4779,6 +4780,36 @@ describe("CodexSecurity orchestration", () => {
"gpt-daybreak-blue-latest",
"gpt-daybreak-red-latest",
];
+ test("reports unpriced model usage without letting its observer stop the scan", async () => {
+ const { root, repository, codexHome, scanDir } = await scanDirectories();
+ const usages: unknown[] = [];
+ const observerErrors: string[] = [];
+ const client = new TestClient(
+ { codexOverrides: { model: "synthetic-unpriced-model" } },
+ {
+ ...scanRuntimeDependencies(codexHome, scanDir),
+ runWorkbench: recordingWorkbench([]),
+ createCodex: completedCodex(root),
+ },
+ );
+ try {
+ const result = await client.run(repository, {
+ onUsage: async (usage) => {
+ usages.push(usage);
+ await Promise.resolve();
+ throw new Error("synthetic usage observer");
+ },
+ onObserverError: (observer) => observerErrors.push(observer),
+ });
+ expect(result.cost).toBeNull();
+ expect(usages).toEqual([
+ expect.objectContaining({ input_tokens: 10, output_tokens: 3 }),
+ ]);
+ expect(observerErrors).toEqual(["onUsage"]);
+ } finally {
+ await client.close();
+ }
+ });
test.each(pricedModels)("tracks live and saved %s costs", async (model) => {
const { root, repository, codexHome, scanDir } = await scanDirectories();
diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.ts
index af9fe84ff..7b4f523c3 100644
--- a/sdk/typescript/tests-ts/cli-project-config.test.ts
+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts
@@ -2,6 +2,8 @@ import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { join } from "node:path";
import { afterEach, expect, test, mock } from "bun:test";
+import { ScanCostTracker } from "../src/cost.js";
+import type { ScanTokenUsage } from "../src/index.js";
import { CodexSecurity, type ScanOptions } from "../src/api.js";
import { main } from "../src/cli.js";
import type { CodexSecurityConfig, JsonObject } from "../src/config.js";
@@ -1188,3 +1190,65 @@ test.each([
expect(onRun).not.toHaveBeenCalled();
},
);
+
+test.each([
+ { model: "gpt-5.6-sol", showCost: false },
+ { model: "gpt-5.6-sol", showCost: true },
+ { model: "synthetic-unpriced-model", showCost: false },
+ { model: "synthetic-unpriced-model", showCost: true },
+])(
+ "headless component usage is printed once for %p",
+ async ({ model, showCost }) => {
+ const input = await fixture({
+ output: { directory: "../component-results" },
+ });
+ await writeFile(
+ join(input.repository, "lib", "index.ts"),
+ "export const value = 1;\n",
+ );
+ const usage: ScanTokenUsage = {
+ input_tokens: 1250,
+ cached_input_tokens: 200,
+ cache_write_input_tokens: 0,
+ output_tokens: 30,
+ reasoning_output_tokens: 0,
+ total_tokens: 1280,
+ };
+ const { stderr, runCli } = createCliTest(main);
+ const deps = dependencies({ currentDirectory: input.root });
+ deps.createSecurity = () =>
+ fakeSecurity(async (_repository, options) => {
+ const tracker = new ScanCostTracker({
+ codexHome: input.root,
+ model,
+ onUsage: options?.onUsage,
+ onCost: options?.onCost,
+ });
+ tracker.start("synthetic-scan-thread");
+ await tracker.stop(usage);
+ return fakeResult([], "complete", null);
+ });
+ expect(
+ await runCli(
+ [
+ "scan-components",
+ input.repository,
+ "-c",
+ input.config,
+ "--component",
+ "lib",
+ "--headless",
+ "--json",
+ ...(showCost ? ["--show-cost"] : []),
+ ],
+ deps,
+ ),
+ ).toBe(0);
+ expect(stderr.text().match(/Tokens:/g)).toHaveLength(1);
+ expect(stderr.text()).toContain("Tokens: 1,050 uncached input");
+ expect(stderr.text()).toContain("30 output");
+ expect(stderr.text().includes("Cost:")).toBe(
+ showCost && model === "gpt-5.6-sol",
+ );
+ },
+);
diff --git a/sdk/typescript/tests-ts/cli-show-cost.test.ts b/sdk/typescript/tests-ts/cli-show-cost.test.ts
index 4ce8bdb46..457b5b3d0 100644
--- a/sdk/typescript/tests-ts/cli-show-cost.test.ts
+++ b/sdk/typescript/tests-ts/cli-show-cost.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test";
import { main } from "../src/cli.js";
-import { dependencies, fakeResult } from "./cli-fixtures.js";
+import { ScanResult, type ScanTokenUsage } from "../src/index.js";
+import { dependencies, fakeResult, fakeSecurity } from "./cli-fixtures.js";
import { createCliTest, captureCli } from "./support/cli-run.js";
const result = fakeResult([], "complete", {
@@ -85,3 +86,76 @@ test.each([false, true])(
).toBe(showCost);
},
);
+
+const observedUsage: ScanTokenUsage = {
+ input_tokens: 1250,
+ cached_input_tokens: 200,
+ cache_write_input_tokens: 0,
+ cache_write_input_tokens_reported: false,
+ output_tokens: 30,
+ reasoning_output_tokens: 0,
+ total_tokens: 1280,
+};
+
+test.each([
+ { name: "headless", args: ["--headless"], tty: true, environment: {} },
+ { name: "CI", args: [], tty: true, environment: { CI: "true" } },
+ { name: "non-TTY", args: [], tty: false, environment: {} },
+])("shows live unpriced usage in $name scans", async (mode) => {
+ const { stderr, runCli } = createCliTest(main, { stderr: mode.tty });
+ const unpriced = new ScanResult({
+ ...result,
+ turnResult: {
+ ...result.turnResult,
+ model: "synthetic-unpriced-model",
+ usage: observedUsage,
+ },
+ });
+ expect(unpriced.cost).toBeNull();
+ const deps = dependencies({ environment: mode.environment });
+ deps.createSecurity = () =>
+ fakeSecurity(async (_repository, options = {}) => {
+ options.onScanStarted?.();
+ await Promise.resolve().then(() => options.onUsage?.(observedUsage));
+ expect(stderr.text()).toContain("1,280 total");
+ expect(stderr.text()).not.toContain("REPORT");
+ options.onScanStarted?.();
+ expect(stderr.text().trim().split("\n").at(-1)).toContain("1,280 total");
+ return unpriced;
+ });
+ expect(await runCli(["scan", ".", ...mode.args], deps)).toBe(0);
+ expect(stderr.text().split("REPORT")[1]).toContain("1,280 total");
+});
+
+test.each(
+ [[], ["--show-cost"], ["--max-cost", "20"], ["--max-cost", "0.001"]].map(
+ (flags) => ({ flags }),
+ ),
+)("paired usage and cost preserve progress with $flags", async ({ flags }) => {
+ const outputs: string[] = [];
+ for (const delivery of ["none", "direct", "deferred"]) {
+ const { stderr, runCli } = createCliTest(main);
+ const deps = dependencies();
+ deps.createSecurity = () =>
+ fakeSecurity(async (_repository, options = {}) => {
+ options.onScanStarted?.();
+ if (delivery === "direct") {
+ const observed = options.onUsage?.(observedUsage);
+ options.onCost?.(result.cost!);
+ await observed;
+ } else {
+ await Promise.all([
+ Promise.resolve().then(
+ () => delivery === "deferred" && options.onUsage?.(observedUsage),
+ ),
+ Promise.resolve().then(() => options.onCost?.(result.cost!)),
+ ]);
+ }
+ return result;
+ });
+ expect(await runCli(["scan", ".", ...flags], deps)).toBe(0);
+ outputs.push(stderr.text());
+ }
+ expect(outputs[1]).toBe(outputs[0]);
+ expect(outputs[2]).toBe(outputs[0]);
+});
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 52f0f806a..75413ae9f 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -2220,8 +2220,47 @@ describe("CLI", () => {
expect(text).not.toContain("Estimated cost: $0.0248865 of $2.00 limit");
});
+ test("shows known dashboard tokens when the model has no price entry", async () => {
+ const { stderr, runCli } = createCliTest(main, { stderr: true });
+ const deps = dependencies({ environment: { NO_COLOR: "1" } });
+ const createSecurity = deps.createSecurity;
+ deps.createSecurity = (config) => {
+ const client = createSecurity(config);
+ const run = client.run.bind(client);
+ client.run = async (repository, options) => {
+ options?.onUsage?.({
+ input_tokens: 100,
+ cached_input_tokens: 0,
+ cache_write_input_tokens: 0,
+ output_tokens: 10,
+ reasoning_output_tokens: 0,
+ total_tokens: 110,
+ });
+ return run(repository, options);
+ };
+ return client;
+ };
+ expect(
+ await runCli(
+ [
+ "scan",
+ "/synthetic/repository",
+ "--model",
+ "synthetic-unpriced-model",
+ "--show-cost",
+ ],
+ deps,
+ ),
+ ).toBe(0);
+ const text = stripVTControlCharacters(stderr.text()).replace(/\s+/gu, " ");
+ expect(text).toContain(
+ "100 uncached input, 0 cache reads, 0 cache writes, 10 output, 110 total",
+ );
+ expect(text).toContain("unavailable (model pricing or usage missing)");
+ });
+
test.each([false, true])(
- "shows durable Deep progress without changing stdout or TUI layout (interactive=%j)",
+ "shows durable Deep progress without changing stdout or TUI layout (interactive=%p)",
async (interactive) => {
const { stdout, stderr, runCli } = createCliTest(main, {
stderr: interactive,
diff --git a/sdk/typescript/tests-ts/component-scan.test.ts b/sdk/typescript/tests-ts/component-scan.test.ts
index d2d115978..19788a8d7 100644
--- a/sdk/typescript/tests-ts/component-scan.test.ts
+++ b/sdk/typescript/tests-ts/component-scan.test.ts
@@ -493,6 +493,14 @@ test("forwards scan events with their component identity without letting observe
event: {},
});
options.onCost?.(fakeResult([], "complete", { input_tokens: 100 }).cost!);
+ options.onUsage?.({
+ input_tokens: 100,
+ cached_input_tokens: 0,
+ cache_write_input_tokens: 0,
+ output_tokens: 10,
+ reasoning_output_tokens: 0,
+ total_tokens: 110,
+ });
options.onWorkerStatus?.({
kind: "dispatch",
phase: "validation",
@@ -517,6 +525,7 @@ test("forwards scan events with their component identity without letting observe
"activity",
"session",
"cost",
+ "usage",
"workers",
"warning",
]);
@@ -566,6 +575,14 @@ test.each([
filesCompleted: 2,
filesTotal: 2,
});
+ options.onUsage?.({
+ input_tokens: 100,
+ cached_input_tokens: 10,
+ cache_write_input_tokens: 0,
+ output_tokens: 20,
+ reasoning_output_tokens: 0,
+ total_tokens: 120,
+ });
options.onCost?.({
model: "gpt-5.6",
inputTokens: 100,
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index d074e691c..3d654ced2 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -2,6 +2,7 @@ import { jsonLines } from "./support/json.js";
import { runNodePython } from "./support/python-probe.js";
import * as filesystem from "node:fs/promises";
import { appendFile, writeFile } from "node:fs/promises";
+import * as fs from "node:fs/promises";
import { join, parse, sep } from "node:path";
import { Codex } from "@openai/codex-sdk";
import { afterEach, describe, expect, spyOn, test } from "bun:test";
@@ -152,6 +153,107 @@ test.each([
});
describe("scan cost", () => {
+ test("ignores token-count events without a usage snapshot in both readers", async () => {
+ const home = await codexHome();
+ const path = await writeSession(
+ home,
+ childUuid7Thread,
+ {},
+ { parent: scanThreadId },
+ );
+ await writeFile(
+ path,
+ jsonLines([
+ ...ownershipRollout([]),
+ {
+ type: "event_msg",
+ timestamp: "2026-07-26T12:03:00Z",
+ payload: {
+ type: "token_count",
+ info: null,
+ rate_limits: { primary: { used_percent: 5 } },
+ },
+ },
+ ]) + "\n",
+ );
+ const tracker = new ScanCostTracker({
+ codexHome: home,
+ model: "gpt-6-astra",
+ });
+ tracker.start(scanThreadId);
+ expect((await tracker.stop()).usage).toEqual(ownedSdkUsage);
+ expect(readPythonRolloutUsage(BUNDLED_PLUGIN_ROOT, path)).toEqual({
+ usage: ownedPythonUsage,
+ warnings: [],
+ });
+ });
+
+ test("does not reopen unchanged rollouts and reads appended worker usage", async () => {
+ const { home, parent, worker } = await workerSessionFixture();
+ await writeSession(home, "unrelated-thread", {
+ input_tokens: 900,
+ output_tokens: 90,
+ });
+ const tracker = new ScanCostTracker({
+ codexHome: home,
+ model: "gpt-6-astra",
+ });
+ tracker.start("scan-thread");
+ await tracker.refresh();
+ const opened = spyOn(fs, "open");
+ try {
+ await tracker.refresh();
+ expect(opened).not.toHaveBeenCalled();
+ await appendFile(
+ worker,
+ jsonLines([
+ {
+ type: "event_msg",
+ payload: {
+ type: "token_count",
+ info: {
+ total_token_usage: { input_tokens: 200, output_tokens: 20 },
+ },
+ },
+ },
+ ]) + "\n",
+ );
+ const snapshot = await tracker.refresh();
+ expect(opened.mock.calls.map(([path]) => path)).toEqual([worker]);
+ expect(snapshot.usage).toMatchObject({
+ input_tokens: 300,
+ output_tokens: 30,
+ });
+ expect(opened.mock.calls.map(([path]) => path)).not.toContain(parent);
+ } finally {
+ opened.mockRestore();
+ await tracker.stop();
+ }
+ });
+
+ test("reports known usage even when model pricing is unavailable", async () => {
+ const home = await codexHome();
+ await writeSession(home, "scan-thread", {
+ input_tokens: 100,
+ output_tokens: 10,
+ });
+ const usages: unknown[] = [];
+ const tracker = new ScanCostTracker({
+ codexHome: home,
+ model: "synthetic-unpriced-model",
+ onUsage: (usage) => usages.push(usage),
+ });
+ tracker.start("scan-thread");
+ const snapshot = await tracker.stop();
+ expect(snapshot.cost).toBeNull();
+ expect(snapshot.usage).toMatchObject({
+ input_tokens: 100,
+ output_tokens: 10,
+ });
+ expect(usages).toHaveLength(1);
+ expect(usages[0]).toEqual(snapshot.usage);
+ });
+
test("shows distinct token categories without adding cached input twice", () => {
expect(
formatTokenUsage({
@@ -909,6 +1011,7 @@ describe("live scan cost tracking", () => {
parent: "scan-thread",
});
await expect(tracker.refresh()).rejects.toMatchObject({ code });
+ await expect(tracker.refresh()).rejects.toMatchObject({ code });
denied = false;
const snapshot = await tracker.stop();
expect(snapshot.cost?.inputTokens).toBe(1_000_030);
diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.ts
index 99c1f4abb..e3df4d25a 100644
--- a/sdk/typescript/tests-ts/scan-activity.test.ts
+++ b/sdk/typescript/tests-ts/scan-activity.test.ts
@@ -536,6 +536,23 @@ describe("scan activity", () => {
}
});
+ test("preserves a backtick run followed by a bare carriage return", () => {
+ const run = "`".repeat(16_000);
+ expect(
+ scanActivityFromEvent(
+ {
+ type: "item.completed",
+ item: {
+ id: "backtick-run",
+ type: "agent_message",
+ text: `${run}\rAfter the run.`,
+ },
+ },
+ "/synthetic/repository",
+ ),
+ ).toMatchObject({ description: `${run} After the run.` });
+ });
+
test("removes internal progress markers from fenced assistant examples", () => {
expect(
scanActivityFromEvent(
diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.ts
index 3a0487c3c..986266b0a 100644
--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts
@@ -1,3 +1,4 @@
+import stringWidth from "string-width";
import { EventEmitter } from "node:events";
import { Writable } from "node:stream";
import { pathToFileURL } from "node:url";
@@ -14,6 +15,7 @@ const STARTED_AT = new Date(2026, 6, 29, 9, 41, 0).getTime();
function fakeClock(now: () => number = () => STARTED_AT) {
return {
now,
+ queueMicrotask: (callback: () => void) => callback(),
setInterval: () => ({}) as NodeJS.Timeout,
clearInterval: () => {},
};
@@ -55,6 +57,393 @@ class DashboardTestInput extends EventEmitter {
}
describe("live scan dashboard", () => {
+ test.each([
+ ["single line", () => "START" + "x".repeat(4 * 1024 * 1024) + "界END"],
+ [
+ "multiple lines",
+ () => "START\n" + "x".repeat(4 * 1024 * 1024) + "界\nEND",
+ ],
+ [
+ "ASCII prose",
+ () =>
+ "START " +
+ "ordinary tool output with small words ".repeat(50_000) +
+ " END",
+ ],
+ ["CJK", () => "START " + "界".repeat(500_000) + " END"],
+ [
+ "unterminated terminal controls",
+ () => "START" + "\u001B]".repeat(40_000) + " END",
+ ],
+ [
+ "Japanese prose",
+ () =>
+ "START " +
+ "日本語の出力です テスト結果を表示します ".repeat(50_000) +
+ " END",
+ ],
+ [
+ "distinct Japanese words",
+ () =>
+ "START " +
+ Array.from(
+ { length: 40_000 },
+ (_, index) => `日本語出力${index} 結果表示${index} `,
+ ).join("") +
+ " END",
+ ],
+ ] as const)(
+ "retains a large wrapped session result with %s",
+ (_name, output) => {
+ const stderr = capture(true);
+ const input = new DashboardTestInput();
+ const dashboard = createDashboard(
+ { ...stderr.stream, columns: 88, rows: 24 },
+ { input },
+ );
+ dashboard.start();
+ input.emit("data", "d");
+ dashboard.recordDetails({
+ threadId: "synthetic-thread",
+ parentThreadId: null,
+ event: {
+ type: "response_item",
+ payload: {
+ type: "function_call_output",
+ output: output(),
+ },
+ },
+ });
+ expect(lastFrame(stderr)).toContain("END");
+ dashboard.scroll(Number.MAX_SAFE_INTEGER);
+ expect(lastFrame(stderr)).toContain("START");
+ dashboard.scroll(-Number.MAX_SAFE_INTEGER);
+ expect(lastFrame(stderr)).toContain("END");
+ for (const line of lastFrame(stderr).split("\n")) {
+ expect(line.length).toBeLessThanOrEqual(88);
+ }
+ dashboard.stop();
+ },
+ );
+
+ test.each(
+ ["A\u0301", "A\uFE0F", "1\uFE0F\u20E3", "\u0600A", "👩‍💻"].flatMap(
+ (cluster) => [true, false].map((code) => [cluster, code] as const),
+ ),
+ )(
+ "keeps %s intact at ASCII wrapping boundaries with code=%p",
+ (cluster, code) => {
+ for (let padding = 0; padding < 40; padding++) {
+ const stderr = capture(true);
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 40,
+ rows: 24,
+ });
+ dashboard.start();
+ dashboard.record({
+ id: "mixed-code",
+ kind: "message",
+ status: "completed",
+ description:
+ (code ? "```text\n" : "") +
+ "x".repeat(padding) +
+ cluster +
+ "yyy" +
+ (code ? "\n```" : ""),
+ paths: [],
+ });
+ expect(lastFrame(stderr)).toContain(cluster);
+ dashboard.stop();
+ }
+ },
+ );
+
+ test.each([
+ ["CSI", "MARK\u001B[31m界👩🏽‍💻\u001B[0mEND", "MARK界👩🏽‍💻END"],
+ [
+ "OSC with BEL",
+ "MARK\u001B]8;;https://example.com\u0007界👩🏽‍💻\u001B]8;;\u0007END",
+ "MARK界👩🏽‍💻END",
+ ],
+ ["OSC with ST", "MARK\u001B]0;title\u001B\\界👩🏽‍💻END", "MARK界👩🏽‍💻END"],
+ ["OSC with C1 ST", "MARK\u001B]0;title\u009C界👩🏽‍💻END", "MARK界👩🏽‍💻END"],
+ ["incomplete CSI", "MARK\u001B[", "MARK ["],
+ ["incomplete OSC", "MARK\u001B]!!! diagnostic", "MARK ]!!! diagnostic"],
+ [
+ "OSC interrupted by another escape",
+ "MARK\u001B]!!!before\u001B[31mafter\u001B[0m\u0007tailEND",
+ "MARK ]!!!beforeafter tailEND",
+ ],
+ ] as const)(
+ "retains visible text while escaping %s",
+ (_name, value, expected) => {
+ for (const view of ["prose", "code", "details"] as const) {
+ const stderr = capture(true);
+ const input = new DashboardTestInput();
+ const dashboard = createDashboard(
+ { ...stderr.stream, columns: 120, rows: 24 },
+ { input, color: false },
+ );
+ dashboard.start();
+ if (view === "details") {
+ input.emit("data", "d");
+ dashboard.recordDetails({
+ threadId: "synthetic-thread",
+ parentThreadId: null,
+ event: {
+ type: "response_item",
+ payload: { type: "function_call_output", output: value },
+ },
+ });
+ } else {
+ dashboard.record({
+ id: "escaped-text",
+ kind: "message",
+ status: "completed",
+ description:
+ view === "code" ? "```text\n" + value + "\n```" : value,
+ paths: [],
+ });
+ }
+ expect(lastFrame(stderr)).toContain(expected);
+ expect(stderr.text()).not.toContain("\u001B]");
+ expect(stderr.text()).not.toContain("\u001B[31m");
+ dashboard.stop();
+ }
+ },
+ );
+
+ test.each(["\t", "\u0007"])(
+ "wraps code after escaping terminal control %j",
+ (control) => {
+ const stderr = capture(true);
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 40,
+ rows: 24,
+ });
+ dashboard.start();
+ dashboard.record({
+ id: "escaped-code",
+ kind: "message",
+ status: "completed",
+ description:
+ "```ts\n" + control.repeat(2) + "callWithLongName(value123)\n```",
+ paths: [],
+ });
+ const frame = lastFrame(stderr);
+ expect(frame.replace(/\s+/gu, "")).toContain(
+ "callWithLongName(value123)",
+ );
+ dashboard.stop();
+ },
+ );
+
+ test.each(["activity", "details"] as const)(
+ "coalesces %s replay frames while retaining event order and cancellation",
+ (view) => {
+ const input = new DashboardTestInput();
+ const pending: (() => void)[] = [];
+ let frame = "";
+ let frames = 0;
+ let interrupted = false;
+ const dashboard = createDashboard(
+ {
+ columns: 88,
+ rows: 24,
+ write: (text) => {
+ frame = text;
+ frames++;
+ },
+ },
+ {
+ input,
+ clock: {
+ ...fakeClock(),
+ queueMicrotask: (callback: () => void) => pending.push(callback),
+ },
+ onInterrupt: () => {
+ interrupted = true;
+ },
+ },
+ );
+ dashboard.start();
+ if (view === "details") input.emit("data", "d");
+ const initialFrames = frames;
+ for (let i = 0; i < 600; i++) {
+ if (view === "details")
+ dashboard.recordDetails({
+ threadId: "synthetic-thread",
+ parentThreadId: null,
+ event: {
+ type: "event_msg",
+ payload: { type: "agent_message", message: `Replay entry ${i}` },
+ },
+ });
+ else
+ dashboard.record({
+ id: `replay-${i}`,
+ kind: "message",
+ status: "completed",
+ description: `Replay entry ${i}`,
+ paths: [],
+ });
+ }
+ expect(frames).toBe(initialFrames);
+ expect(pending).toHaveLength(1);
+ input.emit("data", "\u0003");
+ expect(interrupted).toBe(true);
+ pending.shift()!();
+ expect(frames).toBe(initialFrames + 1);
+ expect(frame).toContain("Replay entry 599");
+ expect(frame.indexOf("Replay entry 598")).toBeLessThan(
+ frame.indexOf("Replay entry 599"),
+ );
+ dashboard.record({
+ id: "pending-at-stop",
+ kind: "message",
+ status: "completed",
+ description: "Pending at stop",
+ paths: [],
+ });
+ dashboard.stop();
+ const stoppedFrames = frames;
+ pending.shift()!();
+ expect(frames).toBe(stoppedFrames);
+ },
+ );
+
+ test("ignores Delete and function keys when filtering session details", () => {
+ const stderr = capture(true);
+ const input = new DashboardTestInput();
+ const dashboard = createDashboard(stderr.stream, { input });
+ dashboard.start();
+ input.emit("data", "d");
+ for (const key of ["\u001B[3~", "\u001B[15~"]) {
+ input.emit("data", key);
+ expect(lastFrame(stderr)).not.toContain("DETAILS · worker");
+ }
+ input.emit("data", "3");
+ expect(lastFrame(stderr)).toContain("DETAILS · worker 3");
+ dashboard.stop();
+ });
+
+ test("uses the full verification viewport", () => {
+ const stderr = capture(true);
+ const dashboard = createDashboard(
+ { ...stderr.stream, columns: 80, rows: 24 },
+ { presentation: "verification" },
+ );
+ dashboard.start();
+ expect(
+ stripVTControlCharacters(stderr.text().split("\u001B[H").at(-1)!).split(
+ "\n",
+ ),
+ ).toHaveLength(24);
+ dashboard.stop();
+ });
+
+ test("styles inline code without changing generated terminal escapes", () => {
+ const stderr = capture(true);
+ const dashboard = createDashboard(stderr.stream, { color: true });
+ dashboard.start();
+ dashboard.record({
+ id: "inline-ansi",
+ kind: "message",
+ status: "completed",
+ description: "The code is `[2m`.",
+ paths: [],
+ });
+ expect(lastFrame(stderr)).toContain("The code is [2m.");
+ expect(stderr.text()).toContain("The code is \u001B[2m[2m\u001B[22m");
+ expect(lastFrame(stderr)).not.toContain("\u001B");
+ dashboard.stop();
+ });
+
+ test.each([
+ ["activity", 16],
+ ["details", 25],
+ ] as const)("retains wide graphemes in narrow %s frames", (view, columns) => {
+ for (const cluster of ["界", "👩‍💻"]) {
+ for (const code of [false, true]) {
+ const input = new DashboardTestInput();
+ let frame = "";
+ const dashboard = createDashboard(
+ {
+ columns,
+ rows: 50,
+ write: (text) => {
+ frame = text;
+ return true;
+ },
+ },
+ { input },
+ );
+ const text = cluster.repeat(3);
+ dashboard.start();
+ if (view === "details") {
+ input.emit("data", "d");
+ dashboard.recordDetails({
+ threadId: "synthetic-thread",
+ parentThreadId: null,
+ worker: 1,
+ event: {
+ type: "response_item",
+ payload: {
+ type: "function_call_output",
+ output: code ? `\n${text}` : text,
+ },
+ },
+ });
+ } else {
+ dashboard.record({
+ id: "wide-text",
+ kind: "message",
+ status: "completed",
+ description: code ? `\`\`\`text\n${text}\n\`\`\`` : text,
+ paths: [],
+ worker: 1,
+ });
+ }
+ const clean = stripVTControlCharacters(frame);
+ expect(clean.split(cluster).length - 1).toBe(3);
+ for (const line of clean.split("\n")) {
+ expect(stringWidth(line)).toBeLessThanOrEqual(columns);
+ }
+ dashboard.stop();
+ }
+ }
+ });
+
+ test("wraps wide activity text to terminal columns without losing it", () => {
+ const stderr = capture(true);
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 40,
+ rows: 24,
+ });
+ dashboard.start();
+ dashboard.record({
+ id: "wide-text",
+ kind: "message",
+ status: "completed",
+ description: "界".repeat(24),
+ paths: [],
+ });
+ const frame = lastFrame(stderr);
+ expect((frame.match(/界/g) ?? []).length).toBe(24);
+ for (const line of frame.split("\n")) {
+ expect(
+ Array.from(line).reduce(
+ (width, char) => width + (char === "界" ? 2 : 1),
+ 0,
+ ),
+ ).toBeLessThanOrEqual(40);
+ }
+ dashboard.stop();
+ });
+
test.each([
["unknown-model", false],
["gpt-5.6-cyber", true],
@@ -1936,3 +2325,50 @@ function failingDashboardOutput(output: string[]) {
return true;
};
}
+
+test("aligns component columns by terminal width and retains trailing cost", async () => {
+ const stderr = capture(true);
+ const dashboard = createDashboard(
+ { ...stderr.stream, columns: 160, rows: 24 },
+ { presentation: "components", showCost: true },
+ );
+ const receipts: ComponentReceipt[] = [
+ "a".repeat(74),
+ "界".repeat(37),
+ "e\u0301".repeat(37),
+ ].map((name, index) => ({
+ id: String(index),
+ name,
+ paths: ["src"],
+ status: "pending",
+ outputDir: `/synthetic/results/${index}`,
+ }));
+ dashboard.start();
+ dashboard.setComponents(receipts);
+ for (const receipt of receipts)
+ dashboard.recordComponentEvent({
+ componentId: receipt.id,
+ type: "cost",
+ value: {
+ model: "synthetic-model",
+ inputTokens: 1,
+ cachedInputTokens: 0,
+ cacheWriteInputTokens: 0,
+ outputTokens: 1,
+ estimatedUsd: 1,
+ estimatedUsdRange: { min: 1, max: 1, context: "unknown" },
+ },
+ });
+ await Promise.resolve();
+ const frame = stripVTControlCharacters(
+ stderr.text().split("\u001B[H").at(-1)!,
+ );
+ const rows = frame.split("\n").filter((line) => line.includes("Queued"));
+ expect(rows).toHaveLength(3);
+ for (const row of rows) expect(row).toContain("$1.00");
+ expect(
+ new Set(rows.map((row) => stringWidth(row.slice(0, row.indexOf("$1.00")))))
+ .size,
+ ).toBe(1);
+ dashboard.stop();
+});
diff --git a/sdk/typescript/tests-ts/scan-history-renderer.test.ts b/sdk/typescript/tests-ts/scan-history-renderer.test.ts
index 8a2d06750..dab1ad4dd 100644
--- a/sdk/typescript/tests-ts/scan-history-renderer.test.ts
+++ b/sdk/typescript/tests-ts/scan-history-renderer.test.ts
@@ -3,6 +3,95 @@ import { describe, expect, test } from "bun:test";
import { renderScanHistory } from "../src/scan-history-renderer.js";
describe("scan history renderer", () => {
+ test("counts singleton sides when the other side is grouped", () => {
+ const text = renderScanHistory(
+ {
+ repository: "/synthetic/repository",
+ beforeScanId: "before-scan",
+ afterScanId: "after-scan",
+ coverage: { afterCompleteness: "complete" },
+ summary: { persisting: 2 },
+ findings: [
+ {
+ title: "Merged group",
+ severity: "high",
+ status: "persisting",
+ path: "merged.ts",
+ beforeOccurrenceIds: ["before-one", "before-two"],
+ afterOccurrenceId: "after-one",
+ },
+ {
+ title: "Expanded group",
+ severity: "high",
+ status: "persisting",
+ path: "expanded.ts",
+ beforeOccurrenceId: "before-one",
+ afterOccurrenceIds: ["after-one", "after-two"],
+ },
+ ],
+ },
+ "compare",
+ { color: false },
+ );
+ expect(text).toContain("2 → 1");
+ expect(text).toContain("1 → 2");
+ });
+
+ test("shows zero findings on the absent side of grouped comparisons", () => {
+ const text = renderScanHistory(
+ {
+ repository: "/synthetic/repository",
+ beforeScanId: "before-scan",
+ afterScanId: "after-scan",
+ coverage: { afterCompleteness: "complete" },
+ summary: { resolved: 2, new: 2 },
+ findings: [
+ {
+ title: "Resolved group",
+ severity: "high",
+ status: "resolved",
+ path: "before.ts",
+ beforeOccurrenceIds: ["before-one", "before-two"],
+ },
+ {
+ title: "Added group",
+ severity: "medium",
+ status: "new",
+ path: "after.ts",
+ afterOccurrenceIds: ["after-one", "after-two"],
+ },
+ ],
+ },
+ "compare",
+ { color: false },
+ );
+ expect(text).toContain("2 → 0");
+ expect(text).toContain("0 → 2");
+ });
+
+ test("orders severity totals from critical to informational", () => {
+ const text = renderScanHistory(
+ {
+ scanId: "synthetic-scan",
+ targetPath: "/synthetic/repository",
+ progress: { status: "complete" },
+ findings: [],
+ severityCounts: {
+ low: 4,
+ informational: 5,
+ medium: 3,
+ critical: 1,
+ high: 2,
+ },
+ },
+ "show",
+ { color: false },
+ );
+ expect(text).toMatch(
+ /1 CRITICAL.*2 HIGH.*3 MEDIUM.*4 LOW.*5 INFORMATIONAL/,
+ );
+ });
+
test.each([
[
"spaced Chinese",