Skip to content

Commit d36e4bc

Browse files
piotrskiclaude
andcommitted
refactor: deduplicate label assignment and simplify line budget logic
- Hoist label assignment before the skip/emit branch in getTree walk - Precompute lineLimit once instead of recalculating reserve on every addLine call Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fc9eddf commit d36e4bc

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

packages/agent-react-devtools/src/__tests__/component-tree.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ describe('ComponentTree', () => {
489489
// With noHost, div is skipped so Header is at depth 1, Deep at depth 2
490490
const filtered = tree.getTree({ noHost: true, maxDepth: 1 });
491491
expect(filtered.map((n) => n.displayName)).toEqual(['App', 'Header']);
492-
>>>>>>> ff2ef20 (test: add tests for tree truncation and host filtering)
493492
});
494493
});
495494
});

packages/agent-react-devtools/src/component-tree.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,20 @@ export class ComponentTree {
410410
if (!node) return;
411411
if (maxDepth !== undefined && depth > maxDepth) return;
412412

413+
// Assign label for every node (even skipped hosts) so IDs stay stable
414+
const label = `@c${labelCounter++}`;
415+
this.labelToId.set(label, node.id);
416+
this.idToLabel.set(node.id, label);
417+
413418
// Check if this host node should be filtered out
414419
const skipThis = noHost && node.type === 'host' && !isSignificantHost(node);
415420

416-
if (!skipThis) {
417-
const label = `@c${labelCounter++}`;
418-
this.labelToId.set(label, node.id);
419-
this.idToLabel.set(node.id, label);
420-
421+
if (skipThis) {
422+
// Promote children to the effective parent at the same depth
423+
for (const childId of node.children) {
424+
walk(childId, depth, effectiveParentId);
425+
}
426+
} else {
421427
const treeNode: TreeNode = {
422428
id: node.id,
423429
label,
@@ -435,16 +441,6 @@ export class ComponentTree {
435441
for (const childId of node.children) {
436442
walk(childId, depth + 1, node.id);
437443
}
438-
} else {
439-
// Labels are still assigned for skipped host nodes so IDs stay stable
440-
const label = `@c${labelCounter++}`;
441-
this.labelToId.set(label, node.id);
442-
this.idToLabel.set(node.id, label);
443-
444-
// Skip this node: promote children to the effective parent at the same depth
445-
for (const childId of node.children) {
446-
walk(childId, depth, effectiveParentId);
447-
}
448444
}
449445
};
450446

packages/agent-react-devtools/src/formatters.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ export function formatTree(nodes: TreeNode[], hintOrOpts?: string | FormatTreeOp
9393

9494
const lines: string[] = [];
9595
let truncated = false;
96+
// Reserve lines for truncation message (+1) and optional summary footer
97+
const lineLimit = maxLines !== undefined
98+
? maxLines - (totalCount !== undefined ? 1 : 0) - 1
99+
: Infinity;
96100

97101
function addLine(line: string): boolean {
98-
// Reserve lines for truncation message and summary footer
99-
const reserve = (totalCount !== undefined ? 1 : 0) + 1; // +1 for truncation line
100-
if (maxLines !== undefined && lines.length >= maxLines - reserve) {
102+
if (lines.length >= lineLimit) {
101103
truncated = true;
102-
return false; // signal: stop adding
104+
return false;
103105
}
104106
lines.push(line);
105107
return true;

0 commit comments

Comments
 (0)