Skip to content

Commit 5ddb0a3

Browse files
authored
feat: Truncate long names by default to 100 characters (#2893)
1 parent cf2eab9 commit 5ddb0a3

10 files changed

Lines changed: 442 additions & 60 deletions

File tree

‎docs/tool-reference.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ in the DevTools Elements panel (if any).
519519
**Parameters:**
520520

521521
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
522+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
522523
- **pageIdx** (integer) _(optional)_: The zero-based page index. Defaults to 0.
523524
- **pageSize** (integer) _(optional)_: The number of contexts to return per page. Defaults to 20.
524525
- **retainedSize** (string) _(optional)_: Inclusive range for the dead-field score of a context (e.g. "10KB", "1MB-2MB", "-1MB", or "1MB-"). A single value is treated as a minimum.
@@ -557,6 +558,7 @@ in the DevTools Elements panel (if any).
557558
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
558559
- **id** (number) **(required)**: The ID for the class, obtained from details.
559560
- **filterName** (enum: "objectsRetainedByDetachedDomNodes", "objectsRetainedByConsole", "objectsRetainedByEventHandlers", "objectsRetainedByContexts", "sharedNativeContext", "noNativeContext", "attributedToSpecificNativeContext") _(optional)_: An optional filter to apply to the nodes.
561+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
560562
- **objectId** (number) _(optional)_: The object ID (nodeId) of the specific native context to filter by when filterName is attributedToSpecificNativeContext.
561563
- **pageIdx** (number) _(optional)_: The page index for pagination.
562564
- **pageSize** (number) _(optional)_: The page size for pagination.
@@ -585,6 +587,7 @@ in the DevTools Elements panel (if any).
585587

586588
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
587589
- **nodeId** (number) **(required)**: The node ID to get the dominator chain for.
590+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
588591

589592
---
590593

@@ -595,6 +598,7 @@ in the DevTools Elements panel (if any).
595598
**Parameters:**
596599

597600
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
601+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
598602
- **pageIdx** (number) _(optional)_: The page index for pagination.
599603
- **pageSize** (number) _(optional)_: The page size for pagination.
600604

@@ -609,6 +613,7 @@ in the DevTools Elements panel (if any).
609613
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
610614
- **nodeId** (number) **(required)**: The node ID to get outgoing edges for.
611615
- **excludePrimitives** (boolean) _(optional)_: Whether to exclude primitive target nodes. Default is true.
616+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
612617
- **pageIdx** (number) _(optional)_: The page index for pagination.
613618
- **pageSize** (number) _(optional)_: The page size for pagination.
614619
- **retainedSize** (string) _(optional)_: Inclusive retained size range (e.g. "1MB-2MB", "-1MB", or "1MB-") for target nodes. A single value is treated as a minimum. Currently, only the lower bound is applied.
@@ -624,6 +629,7 @@ in the DevTools Elements panel (if any).
624629

625630
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
626631
- **nodeId** (number) **(required)**: The node ID to get object details for.
632+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
627633

628634
---
629635

@@ -635,6 +641,7 @@ in the DevTools Elements panel (if any).
635641

636642
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
637643
- **nodeId** (number) **(required)**: The node ID to get retainers for.
644+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
638645
- **pageIdx** (number) _(optional)_: The page index for pagination.
639646
- **pageSize** (number) _(optional)_: The page size for pagination.
640647

@@ -649,6 +656,7 @@ in the DevTools Elements panel (if any).
649656
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
650657
- **nodeId** (number) **(required)**: The node ID to get retaining paths for.
651658
- **maxDepth** (number) _(optional)_: The maximum depth to search for retaining paths.
659+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
652660
- **maxNodes** (number) _(optional)_: The maximum number of nodes to return.
653661
- **maxSiblings** (number) _(optional)_: The maximum number of siblings to return.
654662

@@ -661,6 +669,7 @@ in the DevTools Elements panel (if any).
661669
**Parameters:**
662670

663671
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
672+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
664673

665674
---
666675

@@ -673,6 +682,7 @@ in the DevTools Elements panel (if any).
673682
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to read.
674683
- **className** (string) _(optional)_: Optional regex or text matching object class name.
675684
- **isDetached** (boolean) _(optional)_: Whether to filter for detached DOM nodes.
685+
- **maxNameLength** (integer) _(optional)_: Maximum length of names before truncation. Defaults to 100.
676686
- **nodeType** (string) _(optional)_: Optional V8 node type filter (e.g. object, closure, string, array, code).
677687
- **pageIdx** (number) _(optional)_: The page index for pagination.
678688
- **pageSize** (number) _(optional)_: The page size for pagination.

‎src/McpResponse.ts‎

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
collectRankedContexts,
2323
type ContextAnalysisReport,
2424
type ContextFilterOptions,
25+
formatName,
2526
HeapSnapshotFormatter,
27+
type HeapSnapshotFormatOptions,
2628
isEdgeLike,
2729
isNodeLike,
2830
} from './formatters/HeapSnapshotFormatter.js';
@@ -81,7 +83,7 @@ interface TraceInsightData {
8183
}
8284

8385
interface ContextAnalysisOptions
84-
extends PaginationOptions, ContextFilterOptions {}
86+
extends PaginationOptions, ContextFilterOptions, HeapSnapshotFormatOptions {}
8587

8688
export class McpResponse implements Response {
8789
#includePages = false;
@@ -116,6 +118,7 @@ export class McpResponse implements Response {
116118
objectInfo?: DevTools.HeapSnapshotModel.HeapSnapshotModel.ObjectInfo;
117119
contextAnalysis?: DevTools.HeapSnapshotModel.HeapSnapshotModel.ContextAnalysisResult;
118120
contextAnalysisOptions?: ContextAnalysisOptions;
121+
formatOptions?: HeapSnapshotFormatOptions;
119122
};
120123
#networkRequestsOptions?: {
121124
include: boolean;
@@ -389,6 +392,7 @@ export class McpResponse implements Response {
389392
staticData: DevTools.HeapSnapshotModel.HeapSnapshotModel.StaticData | null,
390393
nativeContextSizes: DevTools.HeapSnapshotModel.HeapSnapshotModel.NativeContextSizes,
391394
retainedByContextSummary: DevTools.HeapSnapshotModel.HeapSnapshotModel.RetainedByContextSummary,
395+
options?: HeapSnapshotFormatOptions,
392396
) {
393397
this.#heapSnapshotOptions = {
394398
...this.#heapSnapshotOptions,
@@ -397,50 +401,57 @@ export class McpResponse implements Response {
397401
staticData,
398402
nativeContextSizes,
399403
retainedByContextSummary,
404+
formatOptions: options,
400405
};
401406
}
402407

403408
setHeapSnapshotNodes(
404409
nodes: DevTools.HeapSnapshotModel.HeapSnapshotModel.ItemsRange,
405-
options?: PaginationOptions,
410+
options?: PaginationOptions & HeapSnapshotFormatOptions,
406411
) {
407412
this.#heapSnapshotOptions = {
408413
...this.#heapSnapshotOptions,
409414
include: true,
410415
nodes,
411416
pagination: options,
417+
formatOptions: options,
412418
};
413419
}
414420

415421
setHeapSnapshotDuplicateStrings(
416422
duplicateStrings: DuplicateStringGroup[],
417-
options?: PaginationOptions,
423+
options?: PaginationOptions & HeapSnapshotFormatOptions,
418424
) {
419425
this.#heapSnapshotOptions = {
420426
...this.#heapSnapshotOptions,
421427
include: true,
422428
duplicateStrings,
423429
pagination: options,
430+
formatOptions: options,
424431
};
425432
}
426433

427434
setHeapSnapshotRetainingPaths(
428435
retainingPaths: DevTools.HeapSnapshotModel.HeapSnapshotModel.RetainingPaths,
436+
options?: HeapSnapshotFormatOptions,
429437
) {
430438
this.#heapSnapshotOptions = {
431439
...this.#heapSnapshotOptions,
432440
include: true,
433441
retainingPaths,
442+
formatOptions: options,
434443
};
435444
}
436445

437446
setHeapSnapshotDominators(
438447
dominators: DevTools.HeapSnapshotModel.HeapSnapshotModel.DominatorChain,
448+
options?: HeapSnapshotFormatOptions,
439449
) {
440450
this.#heapSnapshotOptions = {
441451
...this.#heapSnapshotOptions,
442452
include: true,
443453
dominators,
454+
formatOptions: options,
444455
};
445456
}
446457

@@ -464,11 +475,13 @@ export class McpResponse implements Response {
464475

465476
setHeapSnapshotObjectDetails(
466477
objectInfo: DevTools.HeapSnapshotModel.HeapSnapshotModel.ObjectInfo,
478+
options?: HeapSnapshotFormatOptions,
467479
) {
468480
this.#heapSnapshotOptions = {
469481
...this.#heapSnapshotOptions,
470482
include: true,
471483
objectInfo,
484+
formatOptions: options,
472485
};
473486
}
474487

@@ -481,6 +494,7 @@ export class McpResponse implements Response {
481494
include: true,
482495
contextAnalysis,
483496
contextAnalysisOptions: options,
497+
formatOptions: options,
484498
};
485499
}
486500

@@ -1191,6 +1205,7 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
11911205

11921206
if (this.#heapSnapshotOptions?.include) {
11931207
response.push('## Heap Snapshot Data');
1208+
const formatOptions = this.#heapSnapshotOptions.formatOptions ?? {};
11941209
const stats = this.#heapSnapshotOptions.stats;
11951210
const staticData = this.#heapSnapshotOptions.staticData;
11961211
if (stats) {
@@ -1207,7 +1222,10 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
12071222
if (nativeContextSizes) {
12081223
response.push('### Native Contexts');
12091224
response.push(
1210-
HeapSnapshotFormatter.formatNativeContextSizes(nativeContextSizes),
1225+
HeapSnapshotFormatter.formatNativeContextSizes(
1226+
nativeContextSizes,
1227+
formatOptions,
1228+
),
12111229
);
12121230
structuredContent.heapSnapshot = structuredContent.heapSnapshot || {};
12131231
structuredContent.heapSnapshot.nativeContextSizes = nativeContextSizes;
@@ -1277,7 +1295,12 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
12771295
this.#heapSnapshotOptions.pagination,
12781296
);
12791297

1280-
response.push(HeapSnapshotFormatter.formatNodes(paginationData.items));
1298+
response.push(
1299+
HeapSnapshotFormatter.formatNodes(
1300+
paginationData.items,
1301+
formatOptions,
1302+
),
1303+
);
12811304

12821305
structuredContent.pagination = paginationData.pagination;
12831306
response.push(...paginationData.info);
@@ -1291,7 +1314,9 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
12911314
if (paths.length === 0) {
12921315
response.push('No retaining paths found.');
12931316
} else {
1294-
response.push(HeapSnapshotFormatter.formatRetainingPaths(paths));
1317+
response.push(
1318+
HeapSnapshotFormatter.formatRetainingPaths(paths, formatOptions),
1319+
);
12951320
}
12961321
const reached = Object.entries(limitsReached)
12971322
.filter(([, hit]) => hit)
@@ -1310,7 +1335,9 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
13101335
if (dominators.length === 0) {
13111336
response.push('No dominators found.');
13121337
} else {
1313-
response.push(HeapSnapshotFormatter.formatDominators(dominators));
1338+
response.push(
1339+
HeapSnapshotFormatter.formatDominators(dominators, formatOptions),
1340+
);
13141341
}
13151342
structuredContent.heapSnapshotDominators = dominators;
13161343
}
@@ -1347,6 +1374,7 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
13471374

13481375
const formatted = HeapSnapshotFormatter.formatDuplicateStrings(
13491376
paginationData.items,
1377+
formatOptions,
13501378
);
13511379
response.push(formatted);
13521380

@@ -1355,11 +1383,18 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
13551383
const objectInfo = this.#heapSnapshotOptions.objectInfo;
13561384
if (objectInfo) {
13571385
response.push('### Object Details');
1358-
response.push(
1359-
compactEncode
1360-
? compactEncode(objectInfo)
1361-
: HeapSnapshotFormatter.formatObjectInfo(objectInfo),
1362-
);
1386+
if (compactEncode) {
1387+
response.push(
1388+
compactEncode({
1389+
...objectInfo,
1390+
name: formatName(objectInfo.name, formatOptions),
1391+
}),
1392+
);
1393+
} else {
1394+
response.push(
1395+
HeapSnapshotFormatter.formatObjectInfo(objectInfo, formatOptions),
1396+
);
1397+
}
13631398
structuredContent.heapSnapshotObjectDetails = objectInfo;
13641399
}
13651400
const contextAnalysis = this.#heapSnapshotOptions.contextAnalysis;
@@ -1385,7 +1420,10 @@ Call ${handleDialog(this.#args).name} to handle it before continuing.`);
13851420
response.push(
13861421
compactEncode
13871422
? compactEncode(report)
1388-
: HeapSnapshotFormatter.formatContextAnalysis(report),
1423+
: HeapSnapshotFormatter.formatContextAnalysis(
1424+
report,
1425+
formatOptions,
1426+
),
13891427
);
13901428
structuredContent.heapSnapshotContextAnalysis = report;
13911429
}

0 commit comments

Comments
 (0)