-
Notifications
You must be signed in to change notification settings - Fork 37
feat(code): Enrich renderer crash events for Error Tracking #2514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
charlesvien
wants to merge
4
commits into
06-06-claude-adapter-upgrade
Choose a base branch
from
06-06-add-crash-telemetry
base: 06-06-claude-adapter-upgrade
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| collectMemorySnapshot, | ||
| flattenMemorySnapshot, | ||
| } from "./crash-diagnostics"; | ||
|
|
||
| function metric( | ||
| type: string, | ||
| workingSetSize: number, | ||
| peakWorkingSetSize: number, | ||
| ): Electron.ProcessMetric { | ||
| return { | ||
| type, | ||
| memory: { workingSetSize, peakWorkingSetSize, privateBytes: 0 }, | ||
| } as unknown as Electron.ProcessMetric; | ||
| } | ||
|
|
||
| describe("collectMemorySnapshot", () => { | ||
| it("sums working set, tracks peak, and groups by process type", () => { | ||
| const snapshot = collectMemorySnapshot(() => [ | ||
| metric("Browser", 100, 150), | ||
| metric("Tab", 200, 500), | ||
| metric("Tab", 50, 60), | ||
| metric("GPU", 80, 90), | ||
| ]); | ||
|
|
||
| expect(snapshot).toEqual({ | ||
| totalWorkingSetKb: 430, | ||
| peakWorkingSetKb: 500, | ||
| processCount: 4, | ||
| byType: { Browser: 100, Tab: 250, GPU: 80 }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns a zeroed snapshot for no processes", () => { | ||
| expect(collectMemorySnapshot(() => [])).toEqual({ | ||
| totalWorkingSetKb: 0, | ||
| peakWorkingSetKb: 0, | ||
| processCount: 0, | ||
| byType: {}, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns undefined instead of throwing (crash handler must not fail)", () => { | ||
| expect( | ||
| collectMemorySnapshot(() => { | ||
| throw new Error("getAppMetrics unavailable"); | ||
| }), | ||
| ).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("flattenMemorySnapshot", () => { | ||
| it("flattens scalars and serializes byType for PostHog", () => { | ||
| expect( | ||
| flattenMemorySnapshot({ | ||
| totalWorkingSetKb: 430, | ||
| peakWorkingSetKb: 500, | ||
| processCount: 4, | ||
| byType: { Browser: 100, Tab: 250, GPU: 80 }, | ||
| }), | ||
| ).toEqual({ | ||
| memoryTotalWorkingSetKb: 430, | ||
| memoryPeakWorkingSetKb: 500, | ||
| memoryProcessCount: 4, | ||
| memoryByType: '{"Browser":100,"Tab":250,"GPU":80}', | ||
| }); | ||
| }); | ||
|
|
||
| it("returns an empty object when no snapshot was collected", () => { | ||
| expect(flattenMemorySnapshot(undefined)).toEqual({}); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export interface MemorySnapshot { | ||
| totalWorkingSetKb: number; | ||
| peakWorkingSetKb: number; | ||
| processCount: number; | ||
| byType: Record<string, number>; | ||
| } | ||
|
|
||
| export function collectMemorySnapshot( | ||
| getMetrics: () => Electron.ProcessMetric[], | ||
| ): MemorySnapshot | undefined { | ||
| try { | ||
| const metrics = getMetrics(); | ||
| let totalWorkingSetKb = 0; | ||
| let peakWorkingSetKb = 0; | ||
| const byType: Record<string, number> = {}; | ||
| for (const metric of metrics) { | ||
| const workingSet = metric.memory.workingSetSize; | ||
| totalWorkingSetKb += workingSet; | ||
| peakWorkingSetKb = Math.max( | ||
| peakWorkingSetKb, | ||
| metric.memory.peakWorkingSetSize, | ||
| ); | ||
| byType[metric.type] = (byType[metric.type] ?? 0) + workingSet; | ||
| } | ||
| return { | ||
| totalWorkingSetKb, | ||
| peakWorkingSetKb, | ||
| processCount: metrics.length, | ||
| byType, | ||
| }; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export function flattenMemorySnapshot( | ||
| memory: MemorySnapshot | undefined, | ||
| ): Record<string, number | string> { | ||
| if (!memory) { | ||
| return {}; | ||
| } | ||
| return { | ||
| memoryTotalWorkingSetKb: memory.totalWorkingSetKb, | ||
| memoryPeakWorkingSetKb: memory.peakWorkingSetKb, | ||
| memoryProcessCount: memory.processCount, | ||
| memoryByType: JSON.stringify(memory.byType), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { uuidv7 } from "./uuidv7"; | ||
|
|
||
| const UUID_V7 = | ||
| /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; | ||
|
|
||
| describe("uuidv7", () => { | ||
| it("produces a valid v7 string (version nibble 7, variant 10)", () => { | ||
| for (let i = 0; i < 100; i++) { | ||
| expect(uuidv7()).toMatch(UUID_V7); | ||
| } | ||
| }); | ||
|
|
||
| it("encodes the current time so ids sort in creation order", () => { | ||
| const before = Date.now(); | ||
| const id = uuidv7(); | ||
| const after = Date.now(); | ||
|
|
||
| const stampMs = Number.parseInt(id.slice(0, 8) + id.slice(9, 13), 16); | ||
| expect(stampMs).toBeGreaterThanOrEqual(before); | ||
| expect(stampMs).toBeLessThanOrEqual(after); | ||
| }); | ||
|
|
||
| it("is unique across rapid calls", () => { | ||
| const ids = new Set(Array.from({ length: 1000 }, () => uuidv7())); | ||
| expect(ids.size).toBe(1000); | ||
| }); | ||
|
|
||
| it("writes the 48-bit millisecond timestamp big-endian into the first 6 bytes", () => { | ||
| vi.spyOn(Date, "now").mockReturnValue(0x0123456789ab); | ||
| try { | ||
| const id = uuidv7(); | ||
| expect(id.slice(0, 8)).toBe("01234567"); | ||
| expect(id.slice(9, 13)).toBe("89ab"); | ||
| } finally { | ||
| vi.restoreAllMocks(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { randomBytes } from "node:crypto"; | ||
|
|
||
| export function uuidv7(): string { | ||
| const bytes = randomBytes(16); | ||
| const timestamp = Date.now(); | ||
|
|
||
| bytes[0] = Math.floor(timestamp / 2 ** 40) & 0xff; | ||
| bytes[1] = Math.floor(timestamp / 2 ** 32) & 0xff; | ||
| bytes[2] = Math.floor(timestamp / 2 ** 24) & 0xff; | ||
| bytes[3] = Math.floor(timestamp / 2 ** 16) & 0xff; | ||
| bytes[4] = Math.floor(timestamp / 2 ** 8) & 0xff; | ||
| bytes[5] = timestamp & 0xff; | ||
|
|
||
| bytes[6] = (bytes[6] & 0x0f) | 0x70; // version 7 | ||
| bytes[8] = (bytes[8] & 0x3f) | 0x80; // variant 10 | ||
|
|
||
| const hex = bytes.toString("hex"); | ||
| return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.