Skip to content

Commit 59366f6

Browse files
committed
fix(protocol): guard the v-flag icon regex for browser module loads
ClawSweeper P1: the picker's static import evaluates this module in the browser; pre-Unicode-Sets engines threw at module scope and took down the session menu. Lazy capability-guarded construction; such engines fall back to the grapheme heuristic as client pre-validation while the Gateway keeps exact RGI validation.
1 parent 51dd611 commit 59366f6

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎packages/gateway-protocol/src/session-agent-status.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "vitest";
1+
import { afterEach, describe, expect, it, vi } from "vitest";
22
import {
33
normalizeSessionIconValue,
44
SESSION_AGENT_ATTENTION_ICON_IDS,
@@ -36,3 +36,32 @@ describe("session icon grammar", () => {
3636
expect(SESSION_ICON_GLYPH_IDS.filter((id) => attentionIds.has(id))).toEqual([]);
3737
});
3838
});
39+
40+
describe("session icon grammar without Unicode Sets support", () => {
41+
afterEach(() => {
42+
vi.unstubAllGlobals();
43+
vi.resetModules();
44+
});
45+
46+
it("loads and falls back to the grapheme heuristic when the v flag throws", async () => {
47+
const NativeRegExp = RegExp;
48+
// Mimic a pre-Unicode-Sets browser engine: `v`-flag construction throws.
49+
const ThrowingRegExp = function (this: unknown, pattern: string, flags?: string) {
50+
if (flags?.includes("v")) {
51+
throw new SyntaxError("Invalid flags supplied to RegExp constructor 'v'");
52+
}
53+
return new NativeRegExp(pattern, flags);
54+
} as unknown as RegExpConstructor;
55+
vi.stubGlobal("RegExp", ThrowingRegExp);
56+
vi.resetModules();
57+
58+
const fallback = await import("./session-agent-status.js");
59+
expect(fallback.normalizeSessionIconValue("🦞")).toBe("🦞");
60+
expect(fallback.normalizeSessionIconValue("braces")).toBe("braces");
61+
expect(fallback.normalizeSessionIconValue("hammer")).toBeNull();
62+
expect(fallback.normalizeSessionIconValue("a")).toBeNull();
63+
// Known heuristic looseness on such engines: single non-ASCII text graphemes
64+
// pass client pre-validation; the Gateway still rejects them exactly.
65+
expect(fallback.normalizeSessionIconValue("漢")).toBe("漢");
66+
});
67+
});

‎packages/gateway-protocol/src/session-agent-status.ts‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,44 @@ export type SessionIconGlyphId = (typeof SESSION_ICON_GLYPH_IDS)[number];
2222

2323
const SESSION_ICON_GLYPH_ID_SET = new Set<string>(SESSION_ICON_GLYPH_IDS);
2424
// Anchored RGI_Emoji admits exactly one recommended-for-interchange emoji
25-
// sequence (ZWJ families, flags, keycaps included) and nothing else. Construct
26-
// it dynamically because the repository TypeScript target rejects literal `v` flags.
27-
const SESSION_ICON_RE = new RegExp("^\\p{RGI_Emoji}$", "v");
25+
// sequence (ZWJ families, flags, keycaps included) and nothing else. Constructed
26+
// lazily: the TypeScript target rejects literal `v` flags, and this module also
27+
// loads in the browser (session-menu picker), where a pre-Unicode-Sets engine
28+
// must not throw at module evaluation. Server runtimes (Node 22+) always take
29+
// the exact path; `null` marks an engine without `v`-flag support.
30+
let sessionIconRe: RegExp | null | undefined;
31+
function sessionIconPattern(): RegExp | null {
32+
if (sessionIconRe === undefined) {
33+
try {
34+
sessionIconRe = new RegExp("^\\p{RGI_Emoji}$", "v");
35+
} catch {
36+
sessionIconRe = null;
37+
}
38+
}
39+
return sessionIconRe;
40+
}
41+
42+
function isSingleNonAsciiGrapheme(value: string): boolean {
43+
if (value.length > 16 || /^[!-~]$/u.test(value)) {
44+
return false;
45+
}
46+
const graphemes = [...new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(value)];
47+
return graphemes.length === 1;
48+
}
2849

2950
export function normalizeSessionIconValue(value: string): string | null {
3051
const normalized = value.trim();
31-
return SESSION_ICON_RE.test(normalized) || SESSION_ICON_GLYPH_ID_SET.has(normalized)
32-
? normalized
33-
: null;
52+
if (!normalized) {
53+
return null;
54+
}
55+
if (SESSION_ICON_GLYPH_ID_SET.has(normalized)) {
56+
return normalized;
57+
}
58+
const pattern = sessionIconPattern();
59+
// Pre-Unicode-Sets browsers get the older grapheme heuristic as client-side
60+
// pre-validation only; the Gateway re-validates with the exact RGI pattern.
61+
const accepted = pattern ? pattern.test(normalized) : isSingleNonAsciiGrapheme(normalized);
62+
return accepted ? normalized : null;
3463
}
3564

3665
export type SessionAgentStatus = {

0 commit comments

Comments
 (0)