Files
reverse-engineering-anything/src/hopper/LinuxPrivateDisplayDiagnostic.ts
T
郭立 Guo Li 7590b50801 perf(schema): reduce retained integer validator allocations (#4360)
Construct integer formats directly while preserving bounds, refinements, metadata and current shared schema owners. Keep Incur-dependent CLI numeric coercion declarations unchanged.

Verify canonical advertisements, 126 numeric boundary cases, 974 actual CLI parser cases, broad source tests and real public-artifact SDK workflows. Record 72 normal tasks and 12 separate owned-inspector heap diagnostics with whole-family resource bounds; distinguish SDK spans and asymmetric audit overhead. Generate numerical report sections from receipts and reject stale tables through documentation conformance checks.

Closes #6236. Current-head CI and Greptile pass; actionable review findings are addressed. Codex review was unavailable due repository usage limits.
2026-10-22 05:11:08 +00:00

82 lines
2.7 KiB
TypeScript

import { z } from "zod";
import {
isHopperStartupFailureCode,
type HopperStartupDiagnostic,
} from "../domain/hopperStartupFailure.js";
import { safeParseJson } from "../domain/safeJson.js";
export const LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX = "REA_X11_DIAGNOSTIC=";
const diagnosticContextSchema = z.object({
component: z.literal("hopper_private_display"),
operation: z.enum(["probe", "launch"]),
reason: z.string().regex(/^[a-z0-9_]+$/u),
socket_directory: z.literal("/tmp/.X11-unix"),
socket_directory_mode: z
.string()
.regex(/^[0-7]{4}$/u)
.nullable(),
mount_read_only: z.boolean().nullable(),
effective_socket_directory_mode: z
.string()
.regex(/^[0-7]{4}$/u)
.nullable(),
effective_mount_read_only: z.boolean().nullable(),
wsl: z.boolean(),
strategy: z.enum(["direct", "user-mount-namespace", "unavailable"]),
fallback_reason: z
.string()
.regex(/^[a-z0-9_]+$/u)
.nullable(),
xvfb_stderr_bytes: z.int().nonnegative().safe(),
});
const failureCodeSchema = z.string().transform((value, context) => {
if (isHopperStartupFailureCode(value)) return value;
context.addIssue({
code: "custom",
message: "Unknown startup failure code",
});
return z.NEVER;
});
const diagnosticSchema = z.discriminatedUnion("status", [
diagnosticContextSchema
.extend({ status: z.literal("ready"), failure_code: z.null() })
.strict(),
diagnosticContextSchema
.extend({ status: z.literal("error"), failure_code: failureCodeSchema })
.strict(),
]);
export type LinuxPrivateDisplayDiagnosticParse =
| { readonly ok: true; readonly value: HopperStartupDiagnostic }
| {
readonly ok: false;
readonly reason:
| "diagnostic_missing"
| "diagnostic_multiple"
| "diagnostic_malformed";
};
/** Parse exactly one helper diagnostic from stderr. */
export const parseLinuxPrivateDisplayDiagnostic = (
stderr: string,
): LinuxPrivateDisplayDiagnosticParse => {
const records = stderr
.split("\n")
.filter((line) => line.startsWith(LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX));
if (records.length === 0) return { ok: false, reason: "diagnostic_missing" };
if (records.length !== 1) return { ok: false, reason: "diagnostic_multiple" };
const record = records[0];
if (record === undefined) return { ok: false, reason: "diagnostic_missing" };
const decoded = safeParseJson(
record.slice(LINUX_PRIVATE_DISPLAY_DIAGNOSTIC_PREFIX.length),
);
if (!decoded.ok) return { ok: false, reason: "diagnostic_malformed" };
const parsed = diagnosticSchema.safeParse(decoded.value);
if (!parsed.success) return { ok: false, reason: "diagnostic_malformed" };
return { ok: true, value: parsed.data };
};