Skip to content

Chrome DevTools for coding agents

feat: add maxNameLength truncation option to compare_heapsnapshots (#3098)

Dominik Inführ1e1c53e5137d
diff --git a/docs/tool-reference.md b/docs/tool-reference.md
index e0812eb4..40061bc7 100644
--- a/docs/tool-reference.md
+++ b/docs/tool-reference.md
@@ -550,6 +550,7 @@ in the DevTools Elements panel (if any).
- **baseFilePath** (string) **(required)**: A path to the base .heapsnapshot file (earlier snapshot).
- **currentFilePath** (string) **(required)**: A path to the current .heapsnapshot file (later snapshot).
- **classIndex** (number) _(optional)_: Optional 0-based index of the class in the summary list to filter results, showing individual objects.
+- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
---
diff --git a/src/McpResponse.ts b/src/McpResponse.ts
index 26a2e17d..84e3b985 100644
--- a/src/McpResponse.ts
+++ b/src/McpResponse.ts
@@ -456,21 +456,27 @@ export class McpResponse implements Response {
};
}
- setHeapSnapshotClassDiffs(classDiffs: HeapSnapshotClassDiff[]): void {
+ setHeapSnapshotClassDiffs(
+ classDiffs: HeapSnapshotClassDiff[],
+ options?: HeapSnapshotFormatOptions,
+ ): void {
this.#heapSnapshotOptions = {
...this.#heapSnapshotOptions,
include: true,
classDiffs,
+ formatOptions: options,
};
}
setHeapSnapshotDetailedClassDiff(
detailedClassDiff: HeapSnapshotDetailedClassDiff,
+ options?: HeapSnapshotFormatOptions,
): void {
this.#heapSnapshotOptions = {
...this.#heapSnapshotOptions,
include: true,
detailedClassDiff,
+ formatOptions: options,
};
}
@@ -1450,8 +1456,16 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
response.push('### Heap Snapshot Diff');
response.push(
compactEncode
- ? compactEncode(classDiffs)
- : HeapSnapshotFormatter.formatDiffSummary(classDiffs),
+ ? compactEncode(
+ classDiffs.map(diff => ({
+ ...diff,
+ className: formatName(diff.className, formatOptions),
+ })),
+ )
+ : HeapSnapshotFormatter.formatDiffSummary(
+ classDiffs,
+ formatOptions,
+ ),
);
structuredContent.heapSnapshotClassDiffs = classDiffs;
}
@@ -1460,8 +1474,17 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
response.push('### Heap Snapshot Detailed Diff');
response.push(
compactEncode
- ? compactEncode(detailedClassDiff)
- : HeapSnapshotFormatter.formatDiffDetails(detailedClassDiff),
+ ? compactEncode({
+ ...detailedClassDiff,
+ className: formatName(
+ detailedClassDiff.className,
+ formatOptions,
+ ),
+ })
+ : HeapSnapshotFormatter.formatDiffDetails(
+ detailedClassDiff,
+ formatOptions,
+ ),
);
structuredContent.heapSnapshotDetailedClassDiff = detailedClassDiff;
}
diff --git a/src/config/cli-options.ts b/src/config/cli-options.ts
index 02ec9f43..c0a15d19 100644
--- a/src/config/cli-options.ts
+++ b/src/config/cli-options.ts
@@ -197,6 +197,13 @@ export const commands: Commands = {
'Optional 0-based index of the class in the summary list to filter results, showing individual objects.',
required: false,
},
+ maxNameLength: {
+ name: 'maxNameLength',
+ type: 'integer',
+ description:
+ 'Maximum length of names before truncation. Defaults to 100.',
+ required: false,
+ },
},
},
drag: {
diff --git a/src/formatters/HeapSnapshotFormatter.ts b/src/formatters/HeapSnapshotFormatter.ts
index 952d603b..f87b90fc 100644
--- a/src/formatters/HeapSnapshotFormatter.ts
+++ b/src/formatters/HeapSnapshotFormatter.ts
@@ -306,7 +306,10 @@ export class HeapSnapshotFormatter {
return Object.entries(aggregates).sort((a, b) => b[1].maxRet - a[1].maxRet);
}
- static formatDiffSummary(diffs: HeapSnapshotClassDiff[]): string {
+ static formatDiffSummary(
+ diffs: HeapSnapshotClassDiff[],
+ options: HeapSnapshotFormatOptions,
+ ): string {
const lines: string[] = [];
lines.push(
'index,className,addedCount,removedCount,countDelta,addedSize,removedSize,sizeDelta',
@@ -315,7 +318,7 @@ export class HeapSnapshotFormatter {
let index = 0;
for (const diff of diffs) {
lines.push(
- `${index},${diff.className},${diff.addedCount},${diff.removedCount},${diff.countDelta},${formatBytesToKb(diff.addedSize)},${formatBytesToKb(diff.removedSize)},${formatBytesToKb(diff.sizeDelta)}`,
+ `${index},${formatName(diff.className, options)},${diff.addedCount},${diff.removedCount},${diff.countDelta},${formatBytesToKb(diff.addedSize)},${formatBytesToKb(diff.removedSize)},${formatBytesToKb(diff.sizeDelta)}`,
);
index++;
}
@@ -323,10 +326,13 @@ export class HeapSnapshotFormatter {
return lines.join('\n');
}
- static formatDiffDetails(diff: HeapSnapshotDetailedClassDiff): string {
+ static formatDiffDetails(
+ diff: HeapSnapshotDetailedClassDiff,
+ options: HeapSnapshotFormatOptions,
+ ): string {
const lines: string[] = [];
lines.push(
- `${diff.className}: # new: ${diff.addedCount}, # deleted: ${diff.removedCount}, # delta: ${formatSignedCount(diff.countDelta)}, alloc size: ${formatSignedSize(diff.addedSize)}, freed size: ${formatSignedSize(diff.removedSize)}, size delta: ${formatSignedSize(diff.sizeDelta)}`,
+ `${formatName(diff.className, options)}: # new: ${diff.addedCount}, # deleted: ${diff.removedCount}, # delta: ${formatSignedCount(diff.countDelta)}, alloc size: ${formatSignedSize(diff.addedSize)}, freed size: ${formatSignedSize(diff.removedSize)}, size delta: ${formatSignedSize(diff.sizeDelta)}`,
);
const addedIds = diff.addedIds;
diff --git a/src/telemetry/tool_call_metrics.json b/src/telemetry/tool_call_metrics.json
index c7ebb788..43dd1d53 100644
--- a/src/telemetry/tool_call_metrics.json
+++ b/src/telemetry/tool_call_metrics.json
@@ -1083,6 +1083,10 @@
{
"name": "class_index",
"argType": "number"
+ },
+ {
+ "name": "max_name_length",
+ "argType": "number"
}
]
},
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index 684280db..41cf4eb4 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -218,9 +218,13 @@ export interface Response {
dominators: DevTools.HeapSnapshotModel.HeapSnapshotModel.DominatorChain,
options?: HeapSnapshotFormatOptions,
): void;
- setHeapSnapshotClassDiffs(classDiffs: HeapSnapshotClassDiff[]): void;
+ setHeapSnapshotClassDiffs(
+ classDiffs: HeapSnapshotClassDiff[],
+ options?: HeapSnapshotFormatOptions,
+ ): void;
setHeapSnapshotDetailedClassDiff(
detailedClassDiff: HeapSnapshotDetailedClassDiff,
+ options?: HeapSnapshotFormatOptions,
): void;
setHeapSnapshotObjectDetails(
objectInfo: DevTools.HeapSnapshotModel.HeapSnapshotModel.ObjectInfo,
diff --git a/src/tools/memory.ts b/src/tools/memory.ts
index 26314d4a..63952799 100644
--- a/src/tools/memory.ts
+++ b/src/tools/memory.ts
@@ -416,6 +416,7 @@ export const compareHeapSnapshots = defineTool(() => ({
.describe(
'Optional 0-based index of the class in the summary list to filter results, showing individual objects.',
),
+ maxNameLength: maxNameLengthSchema,
},
blockedByDialog: false,
handler: async (request, response, context) => {
@@ -425,13 +426,17 @@ export const compareHeapSnapshots = defineTool(() => ({
request.params.currentFilePath,
request.params.classIndex,
);
- response.setHeapSnapshotDetailedClassDiff(classDiffResult);
+ response.setHeapSnapshotDetailedClassDiff(classDiffResult, {
+ maxNameLength: request.params.maxNameLength,
+ });
} else {
const diff = await context.getHeapSnapshotClassDiffs(
request.params.baseFilePath,
request.params.currentFilePath,
);
- response.setHeapSnapshotClassDiffs(diff);
+ response.setHeapSnapshotClassDiffs(diff, {
+ maxNameLength: request.params.maxNameLength,
+ });
}
},
}));
diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.ts
index 6dd9b9eb..e50ee4e9 100644
--- a/tests/McpResponse.test.ts
+++ b/tests/McpResponse.test.ts
@@ -1709,6 +1709,29 @@ describe('McpResponse heap snapshot formatting', () => {
assert.ok(detailedText.includes('TestClass: # new: 1, # deleted: 0'));
});
+ it('truncates class diff names in compact format', async () => {
+ const context = createMockMcpContext();
+ const diffs = createMockClassDiffs();
+
+ const toonDiffResponse = new McpResponse(createMockParsedArguments());
+ toonDiffResponse.setHeapSnapshotClassDiffs(diffs, {maxNameLength: 4});
+ const toonDiffResult = await toonDiffResponse.handle(context, 'toon');
+ const toonDiffText = getTextContent(toonDiffResult.content[0]);
+ assert.ok(toonDiffText.includes('Test...'));
+
+ const detailedDiff = createMockDetailedClassDiff();
+ const toonDetailedResponse = new McpResponse(createMockParsedArguments());
+ toonDetailedResponse.setHeapSnapshotDetailedClassDiff(detailedDiff, {
+ maxNameLength: 4,
+ });
+ const toonDetailedResult = await toonDetailedResponse.handle(
+ context,
+ 'toon',
+ );
+ const toonDetailedText = getTextContent(toonDetailedResult.content[0]);
+ assert.ok(toonDetailedText.includes('Test...'));
+ });
+
it('formats duplicate strings with pagination and object details', async () => {
const response = new McpResponse(createMockParsedArguments());
const duplicateStrings = [
diff --git a/tests/formatters/HeapSnapshotFormatter.test.ts b/tests/formatters/HeapSnapshotFormatter.test.ts
index 89305689..568034a4 100644
--- a/tests/formatters/HeapSnapshotFormatter.test.ts
+++ b/tests/formatters/HeapSnapshotFormatter.test.ts
@@ -181,7 +181,7 @@ describe('HeapSnapshotFormatter', () => {
sizeDelta: 0,
},
];
- const result = HeapSnapshotFormatter.formatDiffSummary(summarized);
+ const result = HeapSnapshotFormatter.formatDiffSummary(summarized, {});
const expected = [
'index,className,addedCount,removedCount,countDelta,addedSize,removedSize,sizeDelta',
`0,Balanced,1,1,0,${formatBytesToKb(100)},${formatBytesToKb(100)},${formatBytesToKb(0)}`,
@@ -194,6 +194,29 @@ describe('HeapSnapshotFormatter', () => {
assert.equal(summarizedJson.includes('addedIndexes'), false);
assert.equal(summarizedJson.includes('deletedIndexes'), false);
});
+
+ it('truncates className according to maxNameLength', () => {
+ const summarized = [
+ {
+ className: 'LongClassName',
+ addedCount: 1,
+ removedCount: 0,
+ countDelta: 1,
+ addedSize: 100,
+ removedSize: 0,
+ sizeDelta: 100,
+ },
+ ];
+ const result = HeapSnapshotFormatter.formatDiffSummary(summarized, {
+ maxNameLength: 4,
+ });
+ const expected = [
+ 'index,className,addedCount,removedCount,countDelta,addedSize,removedSize,sizeDelta',
+ `0,Long...,1,0,1,${formatBytesToKb(100)},${formatBytesToKb(0)},${formatBytesToKb(100)}`,
+ ].join('\n');
+
+ assert.strictEqual(result, expected);
+ });
});
describe('formatDiffDetails', () => {
@@ -212,7 +235,7 @@ describe('HeapSnapshotFormatter', () => {
deletedSelfSizes: [60],
};
- const formatted = HeapSnapshotFormatter.formatDiffDetails(details);
+ const formatted = HeapSnapshotFormatter.formatDiffDetails(details, {});
const formatted120 = formatBytesToKb(120);
const formatted60 = formatBytesToKb(60);
@@ -226,6 +249,35 @@ describe('HeapSnapshotFormatter', () => {
assert.strictEqual(formatted, expected);
});
+
+ it('truncates className according to maxNameLength', () => {
+ const details = {
+ className: 'LongClassName',
+ addedCount: 1,
+ removedCount: 0,
+ countDelta: 1,
+ addedSize: 60,
+ removedSize: 0,
+ sizeDelta: 60,
+ addedIds: [101],
+ addedSelfSizes: [60],
+ deletedIds: [],
+ deletedSelfSizes: [],
+ };
+
+ const formatted = HeapSnapshotFormatter.formatDiffDetails(details, {
+ maxNameLength: 4,
+ });
+ const formatted60 = formatBytesToKb(60);
+
+ const expected = [
+ `Long...: # new: 1, # deleted: 0, # delta: +1, alloc size: +${formatted60}, freed size: 0.0\u00A0kB, size delta: +${formatted60}`,
+ 'Objects:',
+ ` + @101 (self_size: ${formatted60})`,
+ ].join('\n');
+
+ assert.strictEqual(formatted, expected);
+ });
});
describe('sort', () => {
@@ -631,12 +683,12 @@ describe('HeapSnapshotFormatter', () => {
describe('formatDiffSummary and formatDiffDetails', () => {
it('formats diff summary comparing heap-1 to heap-2', async t => {
const diffs = await manager.getClassDiffs(heap1Path, heap2Path);
- t.assert.snapshot(HeapSnapshotFormatter.formatDiffSummary(diffs));
+ t.assert.snapshot(HeapSnapshotFormatter.formatDiffSummary(diffs, {}));
});
it('formats diff summary comparing heap-2 to heap-3', async t => {
const diffs = await manager.getClassDiffs(heap2Path, heap3Path);
- t.assert.snapshot(HeapSnapshotFormatter.formatDiffSummary(diffs));
+ t.assert.snapshot(HeapSnapshotFormatter.formatDiffSummary(diffs, {}));
});
it('formats detailed diff comparing heap-1 to heap-2 with classIndex filter', async t => {
@@ -646,7 +698,7 @@ describe('HeapSnapshotFormatter', () => {
2,
);
t.assert.snapshot(
- HeapSnapshotFormatter.formatDiffDetails(detailedDiff),
+ HeapSnapshotFormatter.formatDiffDetails(detailedDiff, {}),
);
});
});
diff --git a/tests/tools/memory.test.ts b/tests/tools/memory.test.ts
index c1ad3e0b..1e2080e0 100644
--- a/tests/tools/memory.test.ts
+++ b/tests/tools/memory.test.ts
@@ -602,6 +602,7 @@ describe('memory', () => {
sinon.assert.calledOnceWithExactly(
response.setHeapSnapshotClassDiffs,
diffs,
+ {maxNameLength: undefined},
);
});
@@ -616,6 +617,7 @@ describe('memory', () => {
baseFilePath: 'heap1.heapsnapshot',
currentFilePath: 'heap2.heapsnapshot',
classIndex: 2,
+ maxNameLength: 500,
},
},
response,
@@ -631,6 +633,7 @@ describe('memory', () => {
sinon.assert.calledOnceWithExactly(
response.setHeapSnapshotDetailedClassDiff,
detailedDiff,
+ {maxNameLength: 500},
);
});
});