Chrome DevTools for coding agents
fix: ensure aggregate IDs in heap snapshots are deterministic (#2923)
Dominik Inführe918c4734d45
diff --git a/src/processors/HeapSnapshotManager.ts b/src/processors/HeapSnapshotManager.tsindex 7c706dff..e35d614b 100644--- a/src/processors/HeapSnapshotManager.ts+++ b/src/processors/HeapSnapshotManager.ts@@ -89,6 +89,15 @@ export class HeapSnapshotManager {idToClassKey: [''],classKeyToId: new Map<string, number>(),});+ // Assign ids to all aggregates of this snapshot. This ensures ids are available+ // even without calling getAggregates() and deterministic (do not depend on+ // the filter used on the getAggregates() call).+ const aggregates = await snapshot.aggregatesWithFilter(+ new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter(),+ );+ for (const key of Object.keys(aggregates)) {+ this.getOrCreateIdForClassKey(filePath, key);+ }return snapshot;}@@ -130,7 +139,7 @@ export class HeapSnapshotManager {let totalSelfSize = 0;for (const [key, aggregate] of Object.entries(aggregates)) {- const id = await this.getOrCreateIdForClassKey(filePath, key);+ const id = this.getOrCreateIdForClassKey(filePath, key);aggregate[stableIdSymbol] = id;objectCount += aggregate.count;totalSelfSize += aggregate.self;@@ -171,10 +180,7 @@ export class HeapSnapshotManager {return await snapshot.getRetainedByContextSummary();}- async getOrCreateIdForClassKey(- filePath: string,- classKey: string,- ): Promise<number> {+ getOrCreateIdForClassKey(filePath: string, classKey: string): number {const cached = this.#getCachedSnapshot(filePath);let id = cached.classKeyToId.get(classKey);if (!id) {@@ -195,7 +201,7 @@ export class HeapSnapshotManager {const filter =new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();await this.#applyNodeFilter(snapshot, filter, filterName, objectId);- const className = await this.resolveClassKeyFromId(filePath, id);+ const className = this.resolveClassKeyFromId(filePath, id);if (!className) {throw new Error(`Class with ID ${id} not found in heap snapshot`);}@@ -369,10 +375,7 @@ export class HeapSnapshotManager {.sort((a, b) => b.sizeDelta - a.sizeDelta);}- async resolveClassKeyFromId(- filePath: string,- id: number,- ): Promise<string | undefined> {+ resolveClassKeyFromId(filePath: string, id: number): string | undefined {const cached = this.#getCachedSnapshot(filePath);return cached.idToClassKey[id];}diff --git a/tests/processors/HeapSnapshotManager.test.ts b/tests/processors/HeapSnapshotManager.test.tsindex ee730488..dd62a013 100644--- a/tests/processors/HeapSnapshotManager.test.ts+++ b/tests/processors/HeapSnapshotManager.test.ts@@ -11,6 +11,7 @@ import sinon from 'sinon';import {HeapSnapshotManager} from '../../src/processors/HeapSnapshotManager.js';import {DevTools} from '../../src/third_party/index.js';+import {stableIdSymbol} from '../../src/utils/id.js';describe('HeapSnapshotManager', () => {afterEach(() => {@@ -89,6 +90,72 @@ describe('HeapSnapshotManager', () => {}});+ it('resolves class IDs in getNodesById without calling getAggregates first', async () => {+ const manager1 = new HeapSnapshotManager();+ const manager2 = new HeapSnapshotManager();+ try {+ const filePath = 'tests/fixtures/example.heapsnapshot';+ const {aggregates} = await manager1.getAggregates(filePath);+ const functionAggregate = Object.values(aggregates).find(+ a => a.name === 'Function',+ );+ assert.ok(functionAggregate);+ const classId = functionAggregate[stableIdSymbol];+ assert.ok(classId !== undefined);++ const expectedNodes = await manager1.getNodesById(filePath, classId);+ const nodesWithoutPriorAggregates = await manager2.getNodesById(+ filePath,+ classId,+ );++ assert.ok(nodesWithoutPriorAggregates.items.length > 0);+ assert.deepStrictEqual(+ nodesWithoutPriorAggregates.items,+ expectedNodes.items,+ );+ } finally {+ manager1.dispose();+ manager2.dispose();+ }+ });++ it('assigns deterministic class IDs even when getAggregates is called with a filter first', async () => {+ const unfilteredFirstManager = new HeapSnapshotManager();+ const filteredFirstManager = new HeapSnapshotManager();+ try {+ const filePath = 'tests/fixtures/example.heapsnapshot';+ const unfilteredFirst =+ await unfilteredFirstManager.getAggregates(filePath);++ const filteredFirst = await filteredFirstManager.getAggregates(+ filePath,+ 'sharedNativeContext',+ );+ const unfilteredSecond =+ await filteredFirstManager.getAggregates(filePath);++ for (const [key, aggregate] of Object.entries(filteredFirst.aggregates)) {+ assert.strictEqual(+ aggregate[stableIdSymbol],+ unfilteredFirst.aggregates[key]?.[stableIdSymbol],+ );+ }++ for (const [key, aggregate] of Object.entries(+ unfilteredSecond.aggregates,+ )) {+ assert.strictEqual(+ aggregate[stableIdSymbol],+ unfilteredFirst.aggregates[key]?.[stableIdSymbol],+ );+ }+ } finally {+ unfilteredFirstManager.dispose();+ filteredFirstManager.dispose();+ }+ });+it('throws when getDetailedClassDiff is called with an invalid classIndex', async () => {const manager = new HeapSnapshotManager();try {