Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ jobs:
- name: Run localization tests
run: pnpm --filter @browser-skill/i18n test

- name: Run VOM tests
run: pnpm --filter @browser-skill/vom test

- name: Run extension tests
run: pnpm ext:test

Expand Down
25 changes: 25 additions & 0 deletions packages/vom/src/__tests__/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ describe("renderVom single-layer page", () => {
expect(out.text).toContain('@e1 checkbox "Subscribe"' + marker + ' ="on"');
expect(out.refs).toHaveLength(1);
});
it.each([100, 600, 2000])("bounds sibling-context reads for %i repeated actions", (count) => {
let nameReads = 0;
const nodes = [node({ id: 1, role: "RootWebArea" })];
for (let i = 0; i < count; i++) {
const label = node({ id: 2 + i * 2, parentId: 1, role: "StaticText" });
Object.defineProperty(label, "name", {
enumerable: true,
get() {
nameReads++;
return `Record ${i}`;
},
});
nodes.push(
label,
node({ id: 3 + i * 2, parentId: 1, tag: "button", role: "button", name: "Open" }),
);
}
const out = renderVom(scene(nodes));
expect(out.refs).toHaveLength(count);
expect(out.refs.map((ref) => ref.ctx)).toEqual(
Array.from({ length: count }, (_, index) => `Record ${Math.max(0, index - 2)}`),
);
// Count semantic reads rather than asserting a machine-dependent duration.
expect(nameReads).toBeLessThan(count * 10);
});

it("does not derive handle context across frame scopes", () => {
const out = renderVom(
Expand Down
38 changes: 34 additions & 4 deletions packages/vom/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,32 @@ function collectSameContainerContext(
let guard = 0;
while (parentId !== null && guard <= state.parentMap.size) {
const siblings = state.children.get(parentId) ?? [];
for (const sibling of siblings) {
if (sibling.id === childId) break;
collectWeakLabelsFromSubtree(sibling, node, nodeName, state, labels);
while (labels.length > MAX_HANDLE_CONTEXT_ITEMS) labels.shift();
const end = state.siblingIndex.get(childId) ?? siblings.length;
let cache = state.siblingContextCache.get(parentId);
if (!cache) {
cache = new Map();
state.siblingContextCache.set(parentId, cache);
}
// The subtree collector depends on scope, target name and incoming labels.
// Include all three so descendants arriving from different containers retain
// the original context semantics. DFS visits sibling prefixes in order.
const key = JSON.stringify([node.contextScopeId, nodeName, labels]);
let prefix = cache.get(key);
if (!prefix || prefix.nextIndex > end) prefix = { nextIndex: 0, labels: [...labels] };
for (; prefix.nextIndex < end; prefix.nextIndex++) {
collectWeakLabelsFromSubtree(
siblings[prefix.nextIndex],
node,
nodeName,
state,
prefix.labels,
);
while (prefix.labels.length > MAX_HANDLE_CONTEXT_ITEMS) prefix.labels.shift();
}
labels.splice(0, labels.length, ...prefix.labels);
// Bound retained variants on pages with many distinct action names.
if (!cache.has(key) && cache.size >= 64) cache.delete(cache.keys().next().value!);
cache.set(key, prefix);

const parent = state.nodesById.get(parentId);
if (!parent || !sharesContextScope(node, parent) || isContextBoundary(parent)) break;
Expand Down Expand Up @@ -791,6 +812,8 @@ interface RenderState {
redactValues: boolean;
truncated: boolean;
children: Map<number | null, VomNode[]>;
siblingIndex: Map<number, number>;
siblingContextCache: Map<number, Map<string, { nextIndex: number; labels: string[] }>>;
parentMap: Map<number, number | null>;
nodesById: Map<number, VomNode>;
domContextIndex: DomContextIndex;
Expand Down Expand Up @@ -907,8 +930,15 @@ function createRenderState(
activeScopeBlocks: ActiveScopeBlock[],
): RenderState {
const children = buildChildren(nodes);
const siblingIndex = new Map<number, number>();
for (const siblings of children.values()) {
for (let index = 0; index < siblings.length; index++)
siblingIndex.set(siblings[index].id, index);
}
const state: RenderState = {
visualAncestors: new Set(),
siblingIndex,
siblingContextCache: new Map(),
lines: [...initialLines],
refs: [],
nextRef: 1,
Expand Down