Skip to content

Commit 99dcd29

Browse files
committed
refactor(replay): extract shared target-evidence tree helpers into src/replay
Move buildIndexMap/buildAncestryChain/filterIdentitySet out of the daemon's session-target-evidence into the shared replay zone so the commands runtime (wait's polling loop, #1349) can consume them without importing the daemon; press-retarget drops its private buildIndexMap duplicate.
1 parent 968db8b commit 99dcd29

9 files changed

Lines changed: 94 additions & 77 deletions

File tree

9.09 KB
Binary file not shown.

‎src/core/press-retarget.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
demoteNonUniqueLocalIdentity,
2020
readNodeLocalIdentity,
2121
} from '../replay/target-identity-node.ts';
22+
import { buildIndexMap } from '../replay/target-evidence-tree.ts';
2223
import { normalizeSelectorText } from '../selectors/build.ts';
2324
import { isSemanticTouchTarget } from './interaction-targeting.ts';
2425

@@ -47,12 +48,6 @@ function isIdentityEmpty(node: SnapshotNode, nodes: readonly SnapshotNode[]): bo
4748
return normalizeSelectorText(node.value) === null;
4849
}
4950

50-
function buildIndexMap(nodes: readonly SnapshotNode[]): Map<number, SnapshotNode> {
51-
const map = new Map<number, SnapshotNode>();
52-
for (const node of nodes) map.set(node.index, node);
53-
return map;
54-
}
55-
5651
/** True when `node` is a proper descendant of `root` — a cycle-safe parent walk, matching `buildAncestryChain`'s guard. */
5752
function isDescendantOf(
5853
node: SnapshotNode,

‎src/daemon/handlers/session-replay-repair-hint.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ import {
2828
type TargetAnnotationV1,
2929
type TargetScrollRegion,
3030
} from '../../replay/target-identity.ts';
31-
import {
32-
buildAncestryChain,
33-
buildIndexMap,
34-
computeScrollRegionKey,
35-
scrollRegionKeysEqual,
36-
} from '../session-target-evidence.ts';
31+
import { buildAncestryChain, buildIndexMap } from '../../replay/target-evidence-tree.ts';
32+
import { computeScrollRegionKey, scrollRegionKeysEqual } from '../session-target-evidence.ts';
3733

3834
export type ReplayRepairHintCapture =
3935
| { state: 'available'; nodes: SnapshotNode[] }

‎src/daemon/handlers/session-replay-target-classification.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ import {
4141
tryParseSelectorChain,
4242
} from '../../selectors/index.ts';
4343
import {
44+
buildAncestryChain,
4445
buildIndexMap,
46+
filterIdentitySet,
47+
} from '../../replay/target-evidence-tree.ts';
48+
import {
4549
boundedLocalIdentity,
46-
buildAncestryChain,
4750
computeSiblingOrdinal,
4851
computeScrollRegionKey,
4952
scrollRegionKeysEqual,
5053
orderByViewportPosition,
51-
filterIdentitySet,
5254
} from '../session-target-evidence.ts';
5355
import {
5456
classifyTargetBindingMatch,

‎src/daemon/session-target-evidence.ts‎

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ import {
2222
readNodeLocalIdentity,
2323
siblingOrdinal,
2424
} from '../replay/target-identity-node.ts';
25+
import {
26+
buildAncestryChain,
27+
buildIndexMap,
28+
filterIdentitySet,
29+
} from '../replay/target-evidence-tree.ts';
2530
import {
2631
classifyTargetBindingMatch,
27-
matchesAncestryPrefix,
2832
matchesLocalIdentity,
2933
serializeTargetAnnotationV1,
3034
utf8ByteLength,
@@ -118,49 +122,9 @@ export function computeTargetEvidence(
118122
// primitives, computed once per candidate node against the record-time tree.
119123
// ---------------------------------------------------------------------------
120124

121-
export function buildIndexMap(nodes: readonly SnapshotNode[]): Map<number, SnapshotNode> {
122-
const map = new Map<number, SnapshotNode>();
123-
for (const node of nodes) map.set(node.index, node);
124-
return map;
125-
}
126-
127125
/** The one identity reader (normalized AND field-capped, on every path): shared with dispatch's post-resolution guard. */
128126
export const boundedLocalIdentity = readNodeLocalIdentity;
129127

130-
export type AncestryWalk = {
131-
chain: TargetAncestryEntry[];
132-
/**
133-
* Decision 3 capture anomaly: a `parentIndex` that resolves to no node, or
134-
* a parent cycle. A broken walk fails the annotation closed to
135-
* `unverifiable` — the structural signals cannot be trusted.
136-
*/
137-
broken: boolean;
138-
};
139-
140-
/** Decision 3 "Ancestry": nearest K ancestors, leaf→root, {role,label?}. */
141-
export function buildAncestryChain(
142-
node: SnapshotNode,
143-
byIndex: Map<number, SnapshotNode>,
144-
limit: number,
145-
): AncestryWalk {
146-
const chain: TargetAncestryEntry[] = [];
147-
const visited = new Set<number>([node.index]);
148-
let current = node;
149-
while (chain.length < limit) {
150-
if (typeof current.parentIndex !== 'number') return { chain, broken: false };
151-
const parent = byIndex.get(current.parentIndex);
152-
if (!parent || visited.has(parent.index)) return { chain, broken: true };
153-
visited.add(parent.index);
154-
const identity = boundedLocalIdentity(parent);
155-
chain.push({
156-
role: identity.role,
157-
...(identity.label !== undefined ? { label: identity.label } : {}),
158-
});
159-
current = parent;
160-
}
161-
return { chain, broken: false };
162-
}
163-
164128
/**
165129
* Decision 3 record-time write step 3: the winner's zero-based index among
166130
* its OWN parent's children, in document order. Root-level nodes (no parent)
@@ -219,28 +183,6 @@ type DisambiguationDomain = {
219183
viewportOrder: number;
220184
};
221185

222-
/**
223-
* Decision 3 record-time write step 2 / replay-time verification's identity
224-
* set I: candidates sharing the recorded local identity with a matching
225-
* leaf-anchored ancestry prefix. Shared by the writer's self-check (over the
226-
* full record-time tree) and replay-time verification (over the recorded
227-
* selector/ref's matched-node domain) — "record and replay compute this
228-
* ordinal identically by definition" applies to this filter too.
229-
*/
230-
export function filterIdentitySet(
231-
candidates: readonly SnapshotNode[],
232-
byIndex: Map<number, SnapshotNode>,
233-
identity: LocalIdentity,
234-
ancestry: readonly TargetAncestryEntry[],
235-
): SnapshotNode[] {
236-
return candidates.filter((candidate) => {
237-
if (!matchesLocalIdentity(boundedLocalIdentity(candidate), identity)) return false;
238-
const observed = buildAncestryChain(candidate, byIndex, Math.max(ancestry.length, 1));
239-
// A candidate with a broken parent walk cannot prove the prefix.
240-
return !observed.broken && matchesAncestryPrefix(observed.chain, ancestry);
241-
});
242-
}
243-
244186
/**
245187
* ADR 0012 decision 3 amendment (#1269): an id is identity only when it
246188
* uniquely denotes the target in the record-time tree. `boundedLocalIdentity`

‎src/replay/target-evidence-tree.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* ADR 0012 decision 3: the tree-structural evidence primitives shared by the
3+
* record-time writer (`src/daemon/session-target-evidence.ts`), replay-time
4+
* verification (`src/daemon/handlers/session-replay-target-classification.ts`),
5+
* and the command-resolution landmark check `wait` runs inside its polling
6+
* loop (#1349, `src/commands/interaction/runtime/selector-read.ts`). They are
7+
* pure functions over `SnapshotNode` trees; keeping them in the shared
8+
* `replay/` zone lets the commands runtime consume them without importing the
9+
* daemon.
10+
*/
11+
12+
import type { SnapshotNode } from '../kernel/snapshot.ts';
13+
import { readNodeLocalIdentity } from './target-identity-node.ts';
14+
import {
15+
matchesAncestryPrefix,
16+
matchesLocalIdentity,
17+
type LocalIdentity,
18+
type TargetAncestryEntry,
19+
} from './target-identity.ts';
20+
21+
export function buildIndexMap(nodes: readonly SnapshotNode[]): Map<number, SnapshotNode> {
22+
const map = new Map<number, SnapshotNode>();
23+
for (const node of nodes) map.set(node.index, node);
24+
return map;
25+
}
26+
27+
export type AncestryWalk = {
28+
chain: TargetAncestryEntry[];
29+
/**
30+
* Decision 3 capture anomaly: a `parentIndex` that resolves to no node, or
31+
* a parent cycle. A broken walk fails the annotation closed to
32+
* `unverifiable` — the structural signals cannot be trusted.
33+
*/
34+
broken: boolean;
35+
};
36+
37+
/** Decision 3 "Ancestry": nearest K ancestors, leaf→root, {role,label?}. */
38+
export function buildAncestryChain(
39+
node: SnapshotNode,
40+
byIndex: Map<number, SnapshotNode>,
41+
limit: number,
42+
): AncestryWalk {
43+
const chain: TargetAncestryEntry[] = [];
44+
const visited = new Set<number>([node.index]);
45+
let current = node;
46+
while (chain.length < limit) {
47+
if (typeof current.parentIndex !== 'number') return { chain, broken: false };
48+
const parent = byIndex.get(current.parentIndex);
49+
if (!parent || visited.has(parent.index)) return { chain, broken: true };
50+
visited.add(parent.index);
51+
const identity = readNodeLocalIdentity(parent);
52+
chain.push({
53+
role: identity.role,
54+
...(identity.label !== undefined ? { label: identity.label } : {}),
55+
});
56+
current = parent;
57+
}
58+
return { chain, broken: false };
59+
}
60+
61+
/**
62+
* Decision 3 record-time write step 2 / replay-time verification's identity
63+
* set I: candidates sharing the recorded local identity with a matching
64+
* leaf-anchored ancestry prefix. Shared by the writer's self-check (over the
65+
* full record-time tree), replay-time verification (over the recorded
66+
* selector/ref's matched-node domain), and wait's in-loop landmark check —
67+
* "record and replay compute this ordinal identically by definition" applies
68+
* to this filter too.
69+
*/
70+
export function filterIdentitySet(
71+
candidates: readonly SnapshotNode[],
72+
byIndex: Map<number, SnapshotNode>,
73+
identity: LocalIdentity,
74+
ancestry: readonly TargetAncestryEntry[],
75+
): SnapshotNode[] {
76+
return candidates.filter((candidate) => {
77+
if (!matchesLocalIdentity(readNodeLocalIdentity(candidate), identity)) return false;
78+
const observed = buildAncestryChain(candidate, byIndex, Math.max(ancestry.length, 1));
79+
// A candidate with a broken parent walk cannot prove the prefix.
80+
return !observed.broken && matchesAncestryPrefix(observed.chain, ancestry);
81+
});
82+
}

0 commit comments

Comments
 (0)