Skip to content

Commit 63859f7

Browse files
wings1029steipete
andauthored
fix(codex): preserve Unicode diagnostics boundaries (#103740)
* fix(codex): preserve Unicode diagnostics boundaries Co-authored-by: 陈志强0668000989 <[email protected]> * test(codex): assert diagnostics preview boundary * test(codex): narrow diagnostics result text --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent fef5c61 commit 63859f7

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎extensions/codex/src/command-handlers.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime";
55
import type { PluginCommandContext, PluginCommandResult } from "openclaw/plugin-sdk/plugin-entry";
66
import { parseAgentSessionKey } from "openclaw/plugin-sdk/routing";
77
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
8+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
89
import { resolveCodexAppServerAuthProfileIdForAgent } from "./app-server/auth-bridge.js";
910
import { CODEX_CONTROL_METHODS, type CodexControlMethod } from "./app-server/capabilities.js";
1011
import {
@@ -1669,7 +1670,7 @@ function formatCodexDiagnosticsTargetLine(target: CodexDiagnosticsTarget): strin
16691670

16701671
function normalizeDiagnosticsReason(note: string): string | undefined {
16711672
const normalized = normalizeOptionalString(note);
1672-
return normalized ? normalized.slice(0, CODEX_DIAGNOSTICS_REASON_MAX_CHARS) : undefined;
1673+
return normalized ? truncateUtf16Safe(normalized, CODEX_DIAGNOSTICS_REASON_MAX_CHARS) : undefined;
16731674
}
16741675

16751676
function parseDiagnosticsArgs(args: string): ParsedDiagnosticsArgs {
@@ -2051,7 +2052,10 @@ function pruneCodexDiagnosticsCooldownMap(map: Map<string, number>, now: number)
20512052
}
20522053

20532054
function formatCodexErrorForDisplay(error: string): string {
2054-
const safe = formatCodexTextForDisplay(error).slice(0, CODEX_DIAGNOSTICS_ERROR_MAX_CHARS);
2055+
const safe = truncateUtf16Safe(
2056+
formatCodexTextForDisplay(error),
2057+
CODEX_DIAGNOSTICS_ERROR_MAX_CHARS,
2058+
);
20552059
return escapeCodexChatText(safe) || "unknown error";
20562060
}
20572061

‎extensions/codex/src/commands.test.ts‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,7 +3134,7 @@ describe("codex command", () => {
31343134
});
31353135
});
31363136

3137-
it("bounds diagnostics notes before upload", async () => {
3137+
it("keeps diagnostics notes UTF-16 safe at the upload boundary", async () => {
31383138
const sessionFile = path.join(tempDir, "session.jsonl");
31393139
await writeTestBinding(
31403140
{ kind: "session", agentId: "main", sessionId: "session-1" },
@@ -3146,19 +3146,30 @@ describe("codex command", () => {
31463146
value: { threadId: "thread-789" },
31473147
}),
31483148
);
3149-
const note = "x".repeat(2050);
3149+
// The emoji's surrogate pair straddles the 2048-unit feedback limit.
3150+
const expectedReason = "x".repeat(2047);
3151+
const note = `${expectedReason}😀tail`;
31503152
const deps = createDeps({ safeCodexControlRequest });
31513153

31523154
const request = await handleCodexCommand(createContext(`diagnostics ${note}`, sessionFile), {
31533155
deps,
31543156
});
3157+
expect(requireResultText(request).split("\n")).toContain(`Note: ${expectedReason}`);
31553158
const token = readDiagnosticsConfirmationToken(request);
31563159
await handleCodexCommand(createContext(`diagnostics confirm ${token}`, sessionFile), { deps });
31573160

31583161
expect(mockArg(safeCodexControlRequest, 0, 0)).toBeUndefined();
31593162
expect(mockArg(safeCodexControlRequest, 0, 1)).toBe(CODEX_CONTROL_METHODS.feedback);
3160-
const feedbackParams = requestParams(safeCodexControlRequest);
3161-
expect(feedbackParams.reason).toBe("x".repeat(2048));
3163+
expect(requestParams(safeCodexControlRequest)).toEqual({
3164+
classification: "bug",
3165+
reason: expectedReason,
3166+
threadId: "thread-789",
3167+
includeLogs: true,
3168+
tags: {
3169+
source: "openclaw-diagnostics",
3170+
channel: "test",
3171+
},
3172+
});
31623173
});
31633174

31643175
it("escapes diagnostics notes before showing approval text", async () => {
@@ -3441,6 +3452,35 @@ describe("codex command", () => {
34413452
});
34423453
});
34433454

3455+
it("keeps diagnostics upload errors UTF-16 safe at the display boundary", async () => {
3456+
const sessionFile = path.join(tempDir, "session.jsonl");
3457+
await writeTestBinding(
3458+
{ kind: "session", agentId: "main", sessionId: "session-1" },
3459+
{ threadId: "thread-error-boundary", cwd: "/repo" },
3460+
);
3461+
// The emoji's surrogate pair straddles the 500-unit display limit.
3462+
const expectedError = "x".repeat(499);
3463+
const safeCodexControlRequest = vi.fn(async () => ({
3464+
ok: false as const,
3465+
error: `${expectedError}😀tail`,
3466+
}));
3467+
const deps = createDeps({ safeCodexControlRequest });
3468+
3469+
const request = await handleCodexCommand(createContext("diagnostics", sessionFile), { deps });
3470+
const token = readDiagnosticsConfirmationToken(request);
3471+
3472+
await expect(
3473+
handleCodexCommand(createContext(`diagnostics confirm ${token}`, sessionFile), { deps }),
3474+
).resolves.toEqual({
3475+
text: [
3476+
"Could not send Codex diagnostics:",
3477+
`- channel test, OpenClaw session session-1, Codex thread thread-error-boundary: ${expectedError}`,
3478+
"Inspect locally:",
3479+
"- `codex resume thread-error-boundary`",
3480+
].join("\n"),
3481+
});
3482+
});
3483+
34443484
it("does not throttle diagnostics retries after upload failures", async () => {
34453485
const sessionFile = path.join(tempDir, "session.jsonl");
34463486
await writeTestBinding(

0 commit comments

Comments
 (0)