| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204 |
- import { beforeEach, describe, expect, it, vi } from "vitest";
- const sendCustom = vi.fn();
- const setConfig = vi.fn();
- const registerCrashEventMonitor = vi.fn();
- const fromWebContents = vi.fn(() => null as unknown);
- const getResourceManagerWindowId = vi.fn(() => null as number | null);
- // Bugfix: 这些断言覆盖稳定性上报的纯映射与阈值逻辑,不需要真实 Electron/main 依赖。
- // 全量单测里真实 Electron 入口首次加载可能超过 5s,导致无关运行时成本压垮纯函数测试。
- vi.mock("electron", () => ({
- BrowserWindow: {
- fromWebContents,
- getFocusedWindow: vi.fn(() => null),
- },
- }));
- vi.mock("@arms/rum-electron", () => ({
- default: {
- sendCustom,
- setConfig,
- },
- }));
- vi.mock("@zcode/shared", () => ({
- mapZCodeEnvToArmsRumEnv: vi.fn(() => "local"),
- }));
- vi.mock("../src/main/desktopCrashCapture.js", () => ({
- registerCrashEventMonitor,
- }));
- vi.mock("../src/main/resourceManagerWindow.js", () => ({
- getResourceManagerWindowId,
- }));
- describe("desktopStabilityTelemetry", () => {
- beforeEach(() => {
- sendCustom.mockClear();
- setConfig.mockClear();
- registerCrashEventMonitor.mockClear();
- fromWebContents.mockReset();
- fromWebContents.mockReturnValue(null);
- getResourceManagerWindowId.mockReset();
- getResourceManagerWindowId.mockReturnValue(null);
- vi.resetModules();
- });
- describe("classifyRenderProcessCrash", () => {
- it("separates the crashed surface from the crash cause", async () => {
- const { classifyRenderProcessCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- classifyRenderProcessCrash({
- reason: "oom",
- exitCode: -1,
- webContentsType: "webview",
- }),
- ).toEqual({
- crashKind: "oom",
- crashScope: "embedded_webview",
- crashCause: "oom",
- });
- expect(
- classifyRenderProcessCrash({
- reason: "crashed",
- exitCode: 1,
- webContentsType: "window",
- }),
- ).toEqual({
- crashKind: "native",
- crashScope: "main_window_renderer",
- crashCause: "process_crashed",
- });
- });
- it("returns null for killed and clean-exit", async () => {
- const { classifyRenderProcessCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- classifyRenderProcessCrash({
- reason: "killed",
- exitCode: 0,
- webContentsType: "renderer",
- }),
- ).toBeNull();
- expect(
- classifyRenderProcessCrash({
- reason: "clean-exit",
- exitCode: 0,
- webContentsType: "renderer",
- }),
- ).toBeNull();
- });
- it("maps all non-OOM process crashes to native instead of calling them JS errors", async () => {
- const { classifyRenderProcessCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- classifyRenderProcessCrash({
- reason: "abnormal-exit",
- exitCode: 1,
- webContentsType: "renderer",
- }),
- ).toEqual({
- crashKind: "native",
- crashScope: "main_window_renderer",
- crashCause: "abnormal_exit",
- });
- });
- it("classifies memory eviction as an OOM crash", async () => {
- const { classifyRenderProcessCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- classifyRenderProcessCrash({
- reason: "memory-eviction",
- exitCode: 137,
- webContentsType: "window",
- }),
- ).toEqual({
- crashKind: "oom",
- crashScope: "main_window_renderer",
- crashCause: "oom",
- });
- });
- });
- it("reports each crash callback once with count semantics and schema-v2 dimensions", async () => {
- const {
- configureDesktopStabilityTelemetry,
- registerDesktopStabilityMonitors,
- registerStabilityMainWindow,
- } = await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- debug: vi.fn(),
- };
- const mainWindow = {
- id: 101,
- isDestroyed: () => false,
- once: vi.fn(),
- };
- const auxiliaryWindow = {
- id: 102,
- isDestroyed: () => false,
- once: vi.fn(),
- };
- registerStabilityMainWindow(mainWindow as never);
- getResourceManagerWindowId.mockReturnValue(auxiliaryWindow.id);
- fromWebContents.mockImplementation((webContents: unknown) =>
- (webContents as { id: number }).id === 11 ? mainWindow : auxiliaryWindow,
- );
- registerDesktopStabilityMonitors(logger, {} as never);
- const hooks = registerCrashEventMonitor.mock.calls[0]?.[2] as {
- onRenderProcessGone: (
- webContents: {
- id: number;
- getType: () => string;
- },
- details: { reason: string; exitCode: number },
- ) => void;
- };
- hooks.onRenderProcessGone(
- { id: 11, getType: () => "window" },
- { reason: "crashed", exitCode: 139 },
- );
- hooks.onRenderProcessGone(
- { id: 12, getType: () => "window" },
- { reason: "crashed", exitCode: 139 },
- );
- const reports = sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_crash");
- expect(reports).toHaveLength(2);
- expect(reports[0]?.[0]).toMatchObject({
- value: 1,
- properties: expect.objectContaining({
- telemetry_schema_version: "2",
- crash_kind: "native",
- crash_scope: "main_window_renderer",
- crash_cause: "process_crashed",
- crash_source: "electron_callback",
- web_contents_id: "11",
- metric_value: "1",
- }),
- });
- expect(reports[0]?.[0].properties.crash_id).toMatch(
- /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
- );
- expect(reports[1]?.[0].properties).toMatchObject({
- crash_scope: "auxiliary_window_renderer",
- scene_window: "process_monitor",
- });
- });
- it("logs controlled process exits as info instead of console error", async () => {
- const { configureDesktopStabilityTelemetry, registerDesktopStabilityMonitors } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- debug: vi.fn(),
- };
- registerDesktopStabilityMonitors(logger, {} as never);
- const hooks = registerCrashEventMonitor.mock.calls[0]?.[2] as {
- onRenderProcessGone: (
- webContents: { id: number; getType: () => string },
- details: { reason: string; exitCode: number },
- ) => void;
- onChildProcessGone: (details: {
- type: string;
- reason: string;
- exitCode: number;
- serviceName?: string;
- name?: string;
- }) => void;
- };
- hooks.onRenderProcessGone(
- { id: 21, getType: () => "window" },
- { reason: "clean-exit", exitCode: 0 },
- );
- hooks.onChildProcessGone({
- type: "Utility",
- reason: "killed",
- exitCode: 0,
- name: "Network Service",
- });
- expect(logger.info).toHaveBeenCalledTimes(2);
- expect(logger.info).toHaveBeenCalledWith(
- "[stability] perf_process_exit reported",
- expect.objectContaining({ exit_kind: "normal" }),
- );
- expect(logger.error).not.toHaveBeenCalled();
- hooks.onChildProcessGone({
- type: "Utility",
- reason: "crashed",
- exitCode: 34,
- name: "Network Service",
- });
- expect(logger.warn).toHaveBeenCalledWith(
- "[stability] perf_process_exit reported",
- expect.objectContaining({ exit_kind: "recoverable_child_crash" }),
- );
- expect(logger.error).not.toHaveBeenCalled();
- });
- it("uses Electron serviceName to attribute Host crashes", async () => {
- const { configureDesktopStabilityTelemetry, registerDesktopStabilityMonitors } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- debug: vi.fn(),
- };
- registerDesktopStabilityMonitors(logger, {} as never);
- const hooks = registerCrashEventMonitor.mock.calls[0]?.[2] as {
- onChildProcessGone: (details: {
- type: string;
- reason: string;
- exitCode: number;
- serviceName?: string;
- name?: string;
- }) => void;
- };
- hooks.onChildProcessGone({
- type: "Utility",
- reason: "abnormal-exit",
- exitCode: 1,
- serviceName: "zcode-host-local-1",
- name: "Node Utility Process",
- });
- const report = sendCustom.mock.calls.find((call) => call[0]?.name === "perf_crash")?.[0];
- expect(report).toMatchObject({
- name: "perf_crash",
- properties: expect.objectContaining({
- crash_scope: "host",
- process_role: "host",
- process_name: "zcode-host-local-1",
- }),
- });
- });
- it.each(["oom", "memory-eviction"])(
- "classifies Host %s exits as OOM crashes",
- async (reason) => {
- const { configureDesktopStabilityTelemetry, registerDesktopStabilityMonitors } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- debug: vi.fn(),
- };
- registerDesktopStabilityMonitors(logger, {} as never);
- const hooks = registerCrashEventMonitor.mock.calls[0]?.[2] as {
- onChildProcessGone: (details: {
- type: string;
- reason: string;
- exitCode: number;
- serviceName?: string;
- name?: string;
- }) => void;
- };
- hooks.onChildProcessGone({
- type: "Utility",
- reason,
- exitCode: 137,
- serviceName: "zcode-host-local-1",
- name: "Node Utility Process",
- });
- const report = sendCustom.mock.calls.find((call) => call[0]?.name === "perf_crash")?.[0];
- expect(report).toMatchObject({
- name: "perf_crash",
- properties: expect.objectContaining({
- crash_kind: "oom",
- crash_cause: "oom",
- crash_scope: "host",
- process_role: "host",
- }),
- });
- },
- );
- describe("child process crash reporting", () => {
- it("does not report recoverable Chromium GPU and utility process exits as crash", async () => {
- const { shouldReportChildProcessGoneAsCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- shouldReportChildProcessGoneAsCrash({
- type: "GPU",
- reason: "crashed",
- exitCode: 34,
- name: "",
- }),
- ).toBe(false);
- expect(
- shouldReportChildProcessGoneAsCrash({
- type: "Utility",
- reason: "crashed",
- exitCode: -1073741205,
- name: "Video Capture",
- }),
- ).toBe(false);
- expect(
- shouldReportChildProcessGoneAsCrash({
- type: "Utility",
- reason: "crashed",
- exitCode: -1073741205,
- name: "Network Service",
- }),
- ).toBe(false);
- });
- it("reports zcode host child process exits as crash", async () => {
- const { mapChildProcessGoneToProcessRole, shouldReportChildProcessGoneAsCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(mapChildProcessGoneToProcessRole("Utility", "zcode-host-local-1")).toBe("host");
- expect(
- shouldReportChildProcessGoneAsCrash({
- type: "Utility",
- reason: "abnormal-exit",
- exitCode: 256,
- name: "zcode-host-local-1",
- }),
- ).toBe(true);
- });
- it("does not report clean child exits as crash", async () => {
- const { shouldReportChildProcessGoneAsCrash } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(
- shouldReportChildProcessGoneAsCrash({
- type: "Utility",
- reason: "clean-exit",
- exitCode: 0,
- name: "zcode-host-local-1",
- }),
- ).toBe(false);
- });
- });
- describe("shouldReportAnr", () => {
- it("does not report below 5s threshold", async () => {
- const { shouldReportAnr, STABILITY_ANR_THRESHOLD_MS } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(shouldReportAnr(STABILITY_ANR_THRESHOLD_MS - 100, false)).toBe(false);
- expect(shouldReportAnr(4_900, false)).toBe(false);
- });
- it("reports at 5s and only once", async () => {
- const { shouldReportAnr, STABILITY_ANR_THRESHOLD_MS } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(shouldReportAnr(STABILITY_ANR_THRESHOLD_MS, false)).toBe(true);
- expect(shouldReportAnr(STABILITY_ANR_THRESHOLD_MS, true)).toBe(false);
- });
- });
- describe("shouldReportFreeze", () => {
- it("does not report freeze below 30s", async () => {
- const { shouldReportFreeze } = await import("../src/main/desktopStabilityTelemetry.js");
- expect(shouldReportFreeze(29_900, false, false)).toBe(false);
- });
- it("reports freeze at 30s when no crash", async () => {
- const { shouldReportFreeze, STABILITY_FREEZE_THRESHOLD_MS } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(shouldReportFreeze(STABILITY_FREEZE_THRESHOLD_MS, false, false)).toBe(true);
- });
- it("does not report freeze after crash", async () => {
- const { shouldReportFreeze, STABILITY_FREEZE_THRESHOLD_MS } =
- await import("../src/main/desktopStabilityTelemetry.js");
- expect(shouldReportFreeze(STABILITY_FREEZE_THRESHOLD_MS, false, true)).toBe(false);
- });
- });
- it("attributes ANR to an auxiliary BrowserWindow instead of main", async () => {
- vi.useFakeTimers();
- try {
- const {
- configureDesktopStabilityTelemetry,
- registerDesktopStabilityMonitors,
- STABILITY_ANR_THRESHOLD_MS,
- } = await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- debug: vi.fn(),
- };
- const webContentsHandlers = new Map<string, () => void>();
- const webContents = {
- id: 401,
- getURL: () => "file:///about.html",
- on: vi.fn((event: string, handler: () => void) => {
- webContentsHandlers.set(event, handler);
- }),
- };
- const auxiliaryWindow = {
- id: 402,
- isDestroyed: () => false,
- webContents,
- };
- registerDesktopStabilityMonitors(logger, {} as never);
- const hooks = registerCrashEventMonitor.mock.calls[0]?.[2] as {
- onBrowserWindowCreated: (win: typeof auxiliaryWindow) => void;
- };
- hooks.onBrowserWindowCreated(auxiliaryWindow);
- webContentsHandlers.get("unresponsive")?.();
- vi.advanceTimersByTime(STABILITY_ANR_THRESHOLD_MS);
- const report = sendCustom.mock.calls.find((call) => call[0]?.name === "perf_anr")?.[0];
- expect(report).toMatchObject({
- name: "perf_anr",
- properties: expect.objectContaining({
- scene_window: "other",
- window_id: "402",
- web_contents_id: "401",
- }),
- });
- } finally {
- vi.useRealTimers();
- }
- });
- describe("Agent process ARMS reporting", () => {
- it("reports every unexpected Agent exit with count and sanitized error detail", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- const workspacePath = "/Users/alice/private-project";
- const event = {
- type: "agent-process-exited" as const,
- pid: 12345,
- provider: "glm" as const,
- workspacePath,
- runtimeInstanceId: "runtime-test-1",
- exitCode: 1,
- signal: null,
- endedAt: 200,
- terminationKind: "unexpected" as const,
- runtimeReady: true,
- runtimeGeneration: 3,
- uptimeMs: 100,
- stderrLineCount: 7,
- stderrTail: [
- `Error: write EPIPE token=super-secret at ${workspacePath}`,
- "request URL contains sk-1234567890abcdefghijklmnopqrstuv",
- ` at Socket._write (${workspacePath}/child.js:10:20)`,
- " at open (/Users/bob/.zcode/state.db:30:40)",
- " at load (/opt/zcode-private/tool.js:50:60)",
- " code: 'EPIPE',",
- "x".repeat(1_000),
- "x".repeat(1_000),
- "x".repeat(1_000),
- "x".repeat(1_000),
- ],
- };
- reportAgentProcessExitToArms(event, logger);
- reportAgentProcessExitToArms({ ...event, pid: 12346 }, logger);
- const reports = sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash");
- expect(reports).toHaveLength(2);
- expect(reports[0][0]).toMatchObject({
- name: "perf_agent_crash",
- type: "custom",
- group: "stability",
- value: 1,
- properties: expect.objectContaining({
- process_role: "agent",
- incident_kind: "unexpected_exit",
- exit_code: "1",
- error_name: "Error",
- error_code: "EPIPE",
- error_message: "write EPIPE token=<redacted> at <workspace>",
- runtime_instance_id: "runtime-test-1",
- runtime_generation: "3",
- stderr_line_count: "7",
- crash_phase: "runtime",
- termination_class: "exit_nonzero",
- diagnostic_class: "errno",
- }),
- });
- const properties = reports[0][0].properties as Record<string, string>;
- expect(properties.error_stack).toContain("<workspace>");
- expect(properties.error_stack).not.toContain(workspacePath);
- expect(properties.error_stack).not.toContain("/Users/bob");
- expect(properties.error_stack).not.toContain("/opt/zcode-private");
- expect(properties.error_stack).toContain("<home>/.zcode/state.db");
- expect(properties.error_stack).toContain("<path>");
- expect(properties.error_stack).not.toContain("super-secret");
- expect(properties.error_stack).not.toContain("sk-1234567890abcdefghijklmnopqrstuv");
- expect(properties.error_stack).toContain("sk-<redacted>");
- expect(properties.error_stack).toHaveLength(4_000);
- expect(properties.error_fingerprint).toMatch(/^[0-9a-f]{16}$/);
- });
- it.each([
- {
- name: "OOM",
- runtimeReady: false,
- stderrTail: [
- "<--- Last few GCs --->",
- "[189964:0x18a8001c8000] Mark-Compact 3691 MB",
- "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory",
- "----- Native stack trace -----",
- " 1: 0x00007ff6deadbeef",
- ],
- expected: {
- crash_phase: "startup",
- diagnostic_class: "oom",
- error_name: "AgentOutOfMemory",
- error_code: "ERR_OUT_OF_MEMORY",
- error_message: "CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory",
- },
- },
- {
- name: "SQLite",
- runtimeReady: true,
- stderrTail: [
- "Error: SQLite migration initialization failed for C:\\Users\\alice\\.zcode\\cli\\db\\db.sqlite",
- ],
- expected: {
- crash_phase: "runtime",
- diagnostic_class: "sqlite",
- error_name: "Error",
- error_code: "",
- error_message:
- "SQLite migration initialization failed for <home>/.zcode/cli/db/db.sqlite",
- },
- },
- {
- name: "errno",
- runtimeReady: false,
- stderrTail: [
- "[Object]",
- "node:events:486",
- "EPERM: process.cwd failed with error operation not permitted, uv_cwd",
- "1: 0x00007ff6deadbeef",
- ],
- expected: {
- crash_phase: "startup",
- diagnostic_class: "errno",
- error_name: "AgentProcessExit",
- error_code: "EPERM",
- error_message: "EPERM: process.cwd failed with error operation not permitted, uv_cwd",
- },
- },
- {
- name: "noise only",
- runtimeReady: false,
- stderrTail: [
- "[Object], [Object], [Object],",
- "<--- Last few GCs --->",
- "----- Native stack trace -----",
- "1: 0x00007ff6deadbeef",
- "(node:1234) ExperimentalWarning: SQLite is an experimental feature and might change at any time",
- "(Use `ZCode --trace-warnings ...` to show where the warning was created)",
- "},",
- ],
- expected: {
- crash_phase: "startup",
- diagnostic_class: "none",
- error_name: "AgentProcessExit",
- error_code: "",
- error_message: "Agent process exited unexpectedly (code=1, signal=null)",
- },
- },
- ])(
- "extracts a stable $name diagnostic summary",
- async ({ runtimeReady, stderrTail, expected }) => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "win32",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: "C:\\repo",
- runtimeInstanceId: `runtime-${expected.diagnostic_class}`,
- exitCode: 1,
- signal: null,
- endedAt: 200,
- terminationKind: "unexpected",
- runtimeReady,
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: stderrTail.length,
- stderrTail,
- },
- logger,
- );
- const properties = sendCustom.mock.calls.find(
- (call) => call[0]?.name === "perf_agent_crash",
- )?.[0].properties;
- expect(properties).toMatchObject({
- ...expected,
- termination_class: "exit_nonzero",
- });
- expect(properties.error_message).not.toMatch(/^\[Object\]|^\d+:\s*0x|^<---|^-----/);
- },
- );
- it.each(["app_quit", "update_install"] as const)(
- "keeps %s lifecycle active and suppresses late Agent exits",
- async (scene) => {
- const {
- configureDesktopStabilityTelemetry,
- getStabilityLifecycleScene,
- notifyStabilityAppExit,
- reportAgentProcessExitToArms,
- } = await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "win32",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
- notifyStabilityAppExit(scene, logger, { exitCode: 0, exitKind: "normal" });
- sendCustom.mockClear();
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: "C:\\repo",
- runtimeInstanceId: "runtime-shutdown",
- exitCode: 1073807364,
- signal: null,
- endedAt: 200,
- terminationKind: "unexpected",
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- },
- logger,
- );
- expect(getStabilityLifecycleScene()).toBe(scene);
- expect(
- sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash"),
- ).toHaveLength(0);
- },
- );
- it.each([
- {
- name: "Windows 0x40010004",
- platform: "win32" as const,
- exitCode: 0x40010004,
- signal: null,
- },
- {
- name: "POSIX SIGTERM",
- platform: "linux" as const,
- exitCode: null,
- signal: "SIGTERM",
- },
- ])("does not report controlled termination signature: $name", async (input) => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: input.platform,
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: input.platform === "win32" ? "C:\\repo" : "/repo",
- runtimeInstanceId: "runtime-controlled-termination",
- exitCode: input.exitCode,
- signal: input.signal,
- endedAt: 200,
- terminationKind: "unexpected",
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- },
- logger,
- );
- expect(
- sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash"),
- ).toHaveLength(0);
- });
- it.each([
- {
- name: "Windows 0x40010004",
- platform: "win32" as const,
- workspacePath: "C:\\repo",
- exitCode: 0x40010004,
- signal: null,
- terminationClass: "exit_nonzero",
- },
- {
- name: "POSIX SIGTERM",
- platform: "linux" as const,
- workspacePath: "/repo",
- exitCode: null,
- signal: "SIGTERM",
- terminationClass: "signal",
- },
- ])("reports protocol failure after controlled cleanup: $name", async (input) => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: input.platform,
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: input.workspacePath,
- runtimeInstanceId: "runtime-protocol-failure",
- exitCode: input.exitCode,
- signal: input.signal,
- endedAt: 200,
- terminationKind: "unexpected",
- terminationReason: "protocol-close",
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- },
- logger,
- );
- const reports = sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash");
- expect(reports).toHaveLength(1);
- expect(reports[0]?.[0].properties).toMatchObject({
- termination_class: input.terminationClass,
- termination_reason: "protocol-close",
- });
- });
- it("still reports an unexpected SIGKILL during runtime", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "linux",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: "/repo",
- runtimeInstanceId: "runtime-sigkill",
- exitCode: null,
- signal: "SIGKILL",
- endedAt: 200,
- terminationKind: "unexpected",
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- },
- logger,
- );
- const reports = sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash");
- expect(reports).toHaveLength(1);
- expect(reports[0]?.[0].properties).toMatchObject({
- crash_phase: "unknown",
- termination_class: "signal",
- });
- });
- it("classifies an unexpected clean code as exit_zero crash", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "linux",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- reportAgentProcessExitToArms(
- {
- type: "agent-process-exited",
- pid: 12345,
- provider: "glm",
- workspacePath: "/repo",
- runtimeInstanceId: "runtime-exit-zero",
- exitCode: 0,
- signal: null,
- endedAt: 200,
- terminationKind: "unexpected",
- runtimeReady: true,
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- },
- logger,
- );
- expect(
- sendCustom.mock.calls.find((call) => call[0]?.name === "perf_agent_crash")?.[0].properties,
- ).toMatchObject({
- crash_phase: "runtime",
- termination_class: "exit_zero",
- });
- });
- it("does not report expected or watchdog Agent exits", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "win32",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- const base = {
- type: "agent-process-exited" as const,
- pid: 12345,
- provider: "glm" as const,
- workspacePath: "C:\\repo",
- runtimeInstanceId: "runtime-test-2",
- exitCode: 1,
- signal: null,
- endedAt: 200,
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 0,
- };
- reportAgentProcessExitToArms(
- {
- ...base,
- terminationKind: "expected",
- terminationReason: "workspace-dispose",
- },
- logger,
- );
- reportAgentProcessExitToArms(
- {
- ...base,
- terminationKind: "watchdog_recycle",
- terminationReason: "request-timeout",
- },
- logger,
- );
- expect(
- sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_agent_crash"),
- ).toHaveLength(0);
- });
- it("normalizes home paths and uses the meaningful errno line for Agent fingerprints", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessExitToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- const base = {
- type: "agent-process-exited" as const,
- pid: 12345,
- provider: "glm" as const,
- workspacePath: "/repo",
- exitCode: 1,
- signal: null,
- endedAt: 200,
- terminationKind: "unexpected" as const,
- runtimeGeneration: 1,
- uptimeMs: 100,
- stderrLineCount: 3,
- };
- reportAgentProcessExitToArms(
- {
- ...base,
- runtimeInstanceId: "runtime-path-a",
- stderrTail: [
- "[Object]",
- "node:events:486",
- "EPERM: process.cwd failed at /Users/alice/.zcode/state.db:10:20",
- ],
- },
- logger,
- );
- reportAgentProcessExitToArms(
- {
- ...base,
- runtimeInstanceId: "runtime-path-b",
- stderrTail: [
- "[Object]",
- "node:events:999",
- "EPERM: process.cwd failed at /home/bob/.zcode/state.db:30:40",
- ],
- },
- logger,
- );
- const reports = sendCustom.mock.calls
- .filter((call) => call[0]?.name === "perf_agent_crash")
- .map((call) => call[0].properties as Record<string, string>);
- expect(reports.map((report) => report.error_message)).toEqual([
- "EPERM: process.cwd failed at <home>/.zcode/state.db:10:20",
- "EPERM: process.cwd failed at <home>/.zcode/state.db:30:40",
- ]);
- expect(reports.map((report) => report.error_code)).toEqual(["EPERM", "EPERM"]);
- expect(reports[0]?.error_fingerprint).toBe(reports[1]?.error_fingerprint);
- });
- it("reports Agent spawn errors without command or workspace paths", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessSpawnErrorToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "linux",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- const workspacePath = "/home/alice/private-project";
- reportAgentProcessSpawnErrorToArms(
- {
- type: "agent-process-error",
- pid: null,
- provider: "glm",
- workspacePath,
- runtimeInstanceId: "runtime-test-3",
- command: `${workspacePath}/missing-agent`,
- args: ["app-server", "--stdio", "--token=must-not-upload"],
- errorName: "Error",
- errorCode: "ENOENT",
- errorMessage: `spawn ${workspacePath}/missing-agent ENOENT sk-abcdefghijklmnopqrstuv0123456789`,
- errorStack: `Error: spawn ${workspacePath}/missing-agent ENOENT sk-abcdefghijklmnopqrstuv0123456789`,
- runtimeGeneration: 1,
- occurredAt: 100,
- },
- logger,
- );
- const report = sendCustom.mock.calls.find(
- (call) => call[0]?.name === "perf_agent_spawn_error",
- )?.[0];
- expect(report).toMatchObject({
- name: "perf_agent_spawn_error",
- type: "custom",
- group: "stability",
- value: 1,
- properties: expect.objectContaining({
- process_role: "agent",
- incident_kind: "spawn_error",
- error_code: "ENOENT",
- error_message: "spawn <workspace>/missing-agent ENOENT sk-<redacted>",
- diagnostic_class: "errno",
- runtime_instance_id: "runtime-test-3",
- }),
- });
- expect(JSON.stringify(report)).not.toContain(workspacePath);
- expect(JSON.stringify(report)).not.toContain("must-not-upload");
- expect(JSON.stringify(report)).not.toContain("sk-abcdefghijklmnopqrstuv0123456789");
- expect(JSON.stringify(report)).toContain("sk-<redacted>");
- });
- it("reports one Agent start with the runtime instance used by its later crash", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessStartToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "linux",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- reportAgentProcessStartToArms(
- {
- type: "agent-process-spawned",
- pid: 12345,
- provider: "glm",
- workspacePath: "/repo/demo",
- command: "/tmp/zcode-agent",
- args: ["app-server", "--stdio"],
- startedAt: 100,
- runtimeGeneration: 4,
- runtimeInstanceId: "runtime-test-4",
- },
- logger,
- );
- expect(
- sendCustom.mock.calls.find((call) => call[0]?.name === "perf_agent_start")?.[0],
- ).toMatchObject({
- name: "perf_agent_start",
- value: 1,
- properties: expect.objectContaining({
- process_role: "agent",
- runtime_generation: "4",
- runtime_instance_id: "runtime-test-4",
- }),
- });
- });
- it("reports Agent ready with the same runtime instance and startup duration", async () => {
- const { configureDesktopStabilityTelemetry, reportAgentProcessReadyToArms } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "linux",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- reportAgentProcessReadyToArms(
- {
- type: "agent-process-ready",
- pid: 12345,
- provider: "glm",
- workspacePath: "/repo/demo",
- readyAt: 250,
- startupDurationMs: 150,
- runtimeGeneration: 4,
- runtimeInstanceId: "runtime-test-4",
- },
- logger,
- );
- expect(
- sendCustom.mock.calls.find((call) => call[0]?.name === "perf_agent_ready")?.[0],
- ).toMatchObject({
- name: "perf_agent_ready",
- value: 1,
- properties: expect.objectContaining({
- process_role: "agent",
- runtime_generation: "4",
- runtime_instance_id: "runtime-test-4",
- startup_duration_ms: "150",
- }),
- });
- });
- });
- describe("reportPerfAppStart", () => {
- it("sends perf_app_start only once per process", async () => {
- const { configureDesktopStabilityTelemetry, reportPerfAppStart } =
- await import("../src/main/desktopStabilityTelemetry.js");
- configureDesktopStabilityTelemetry({
- deviceMid: "test-device",
- platform: "darwin",
- appVersion: "0.0.0-test",
- armsEnv: "local",
- });
- const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
- reportPerfAppStart(logger);
- reportPerfAppStart(logger);
- const perfStarts = sendCustom.mock.calls.filter((call) => call[0]?.name === "perf_app_start");
- expect(perfStarts).toHaveLength(1);
- expect(perfStarts[0][0]).toMatchObject({
- name: "perf_app_start",
- type: "custom",
- group: "stability",
- value: 1,
- });
- expect(perfStarts[0][0].properties).toMatchObject({
- event_name: "perf_app_start",
- scene_lifecycle: "cold_start",
- scene_window: "main",
- device_mid: "test-device",
- telemetry_schema_version: "2",
- });
- });
- });
- });
|