Skip to content

Commit 9e60e14

Browse files
committed
fix: improve ID stability and add diff reset notification
1 parent 4d66519 commit 9e60e14

5 files changed

Lines changed: 13 additions & 4 deletions

File tree

scripts/post-build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function main(): void {
2626

2727
// Create i18n mock
2828
const i18nDir = path.join(BUILD_DIR, devtoolsFrontEndCorePath, 'i18n');
29+
fs.mkdirSync(i18nDir, {recursive: true});
2930
const localesFile = path.join(i18nDir, 'locales.js');
3031
const localesContent = `
3132
export const LOCALES = [

src/McpContext.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,9 @@ export class McpContext implements Context {
757757
const nodeAny = node as unknown as Record<string, unknown>;
758758
// StaticText nodes often have unstable backendNodeIds in some contexts,
759759
// or we might want to group them by their parent.
760-
const uniqueBackendId =
761-
nodeAny.backendNodeId && node.role !== 'StaticText'
762-
? `${nodeAny.loaderId}_${nodeAny.backendNodeId}`
763-
: `${nodeAny.loaderId}_${node.role}_${parentId}_${index}`;
760+
const uniqueBackendId = nodeAny.backendNodeId
761+
? `${nodeAny.loaderId}_${nodeAny.backendNodeId}`
762+
: `${nodeAny.loaderId}_${nodeAny.role}_${parentId}_${index}`;
764763

765764
if (uniqueBackendNodeIdToMcpId.has(uniqueBackendId)) {
766765
// Re-use MCP exposed ID if the uniqueId is the same.
@@ -800,6 +799,7 @@ export class McpContext implements Context {
800799
idToNode,
801800
hasSelectedElement: false,
802801
verbose,
802+
diffReset: !!(options.diff && !page.lastSnapshot),
803803
};
804804

805805
if (options.diff && page.lastSnapshot) {

src/formatters/SnapshotFormatter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export class SnapshotFormatter {
2727
Get a verbose snapshot to include all elements if you are interested in the selected element.\n\n`);
2828
}
2929

30+
if (this.#snapshot.diffReset) {
31+
chunks.push(
32+
'Note: no previous snapshot found for this page (cache may have been reset due to navigation). Returning full snapshot.\n\n',
33+
);
34+
}
35+
3036
if (this.#snapshot.diff) {
3137
const {added, changed, removed} = this.#snapshot.diff;
3238
if (added.length === 0 && changed.length === 0 && removed.length === 0) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface TextSnapshot {
3333
// snapshot. This flag indicates if there is any selected element.
3434
hasSelectedElement: boolean;
3535
verbose: boolean;
36+
diffReset?: boolean;
3637
diff?: {
3738
added: string[];
3839
removed: string[];

tests/McpContext.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ describe('McpContext', () => {
180180
await page.pptrPage.goto('about:blank');
181181
await context.createTextSnapshot(page, false, undefined, {diff: true});
182182
assert.strictEqual(page.textSnapshot?.diff, undefined); // Should be reset
183+
assert.strictEqual(page.textSnapshot?.diffReset, true);
183184
});
184185
});
185186
});

0 commit comments

Comments
 (0)