|
16 | 16 | * here, not through `@agent-device/ad-replay`'s façade (#1555 review). |
17 | 17 | */ |
18 | 18 |
|
19 | | -import type { TargetAncestryEntry, TargetAnnotationV1 } from '@agent-device/contracts/replay'; |
| 19 | +import type { RawSnapshotNode } from '@agent-device/kernel/snapshot'; |
| 20 | +import { normalizeType } from '@agent-device/contracts/snapshot'; |
| 21 | +import type { |
| 22 | + LocalIdentity, |
| 23 | + NodeStructuralDenotation, |
| 24 | + TargetAncestryEntry, |
| 25 | + TargetAnnotationV1, |
| 26 | +} from '@agent-device/contracts/replay'; |
| 27 | +import { |
| 28 | + normalizeIdentifierField, |
| 29 | + normalizeLabelField, |
| 30 | + normalizeRoleField, |
| 31 | + truncateToUtf8Bytes, |
| 32 | + TARGET_ANNOTATION_MAX_FIELD_BYTES, |
| 33 | +} from './target-annotation-serde.ts'; |
20 | 34 |
|
21 | 35 | // --------------------------------------------------------------------------- |
22 | 36 | // Local identity + ancestry-prefix matching (decision 3 "Local identity" / |
23 | 37 | // "Ancestry"). Pure over the small structural shapes above — no tree |
24 | 38 | // dependency, so both the writer (over `SnapshotNode`-derived values) and a |
25 | | -// replay verifier can share it verbatim. |
| 39 | +// replay verifier can share it verbatim. The type vocabulary (`LocalIdentity`, |
| 40 | +// `NodeStructuralDenotation`) lives in `@agent-device/contracts/replay` so the |
| 41 | +// guard shapes there reference it nominally; this module re-exports it beside |
| 42 | +// the readers that produce it. |
26 | 43 | // --------------------------------------------------------------------------- |
27 | 44 |
|
28 | | -export type LocalIdentity = { id?: string; role: string; label?: string }; |
| 45 | +export type { LocalIdentity, NodeStructuralDenotation }; |
| 46 | + |
| 47 | +type IdentityTreeNode = Pick<RawSnapshotNode, 'type' | 'identifier' | 'label'>; |
| 48 | + |
| 49 | +/** |
| 50 | + * ADR 0012 decision 3: the ONE snapshot-node local-identity reader — |
| 51 | + * normalized (NFC, label whitespace collapse, `normalizeType` role) AND |
| 52 | + * 256-byte field-capped, on every path. Shared by the record-time writer |
| 53 | + * (`src/daemon/session-target-evidence.ts`), replay-time verification |
| 54 | + * (`src/daemon/handlers/session-replay-target-verification.ts`), and the |
| 55 | + * dispatch-side post-resolution guard |
| 56 | + * (`src/commands/interaction/runtime/resolution.ts`), so all three compute |
| 57 | + * a node's identity with byte-identical semantics. |
| 58 | + */ |
| 59 | +export function readNodeLocalIdentity( |
| 60 | + node: Pick<RawSnapshotNode, 'type' | 'identifier' | 'label'>, |
| 61 | +): LocalIdentity { |
| 62 | + const role = normalizeRoleField(normalizeType(node.type ?? '')); |
| 63 | + const id = normalizeIdentifierField(node.identifier); |
| 64 | + const label = normalizeLabelField(node.label); |
| 65 | + return { |
| 66 | + ...(id !== undefined ? { id: truncateToUtf8Bytes(id, TARGET_ANNOTATION_MAX_FIELD_BYTES) } : {}), |
| 67 | + role: truncateToUtf8Bytes(role, TARGET_ANNOTATION_MAX_FIELD_BYTES), |
| 68 | + ...(label !== undefined |
| 69 | + ? { label: truncateToUtf8Bytes(label, TARGET_ANNOTATION_MAX_FIELD_BYTES) } |
| 70 | + : {}), |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** Exact-equality comparison of two normalized local identities. */ |
| 75 | +export function localIdentitiesEqual(a: LocalIdentity, b: LocalIdentity): boolean { |
| 76 | + return a.id === b.id && a.role === b.role && a.label === b.label; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * ADR 0012 decision 3 amendment (#1269): how many nodes in `nodes` carry the |
| 81 | + * canonical identity `id`. THE single uniqueness predicate behind both |
| 82 | + * id-demotion sites — `computeTargetEvidence`'s `target-v1` identity tuple |
| 83 | + * and `buildSelectorChainForNode`'s selector chain. Sharing it is the |
| 84 | + * correctness guarantee: a non-unique id is dropped from BOTH or NEITHER, |
| 85 | + * never half-demoted (dropped from identity while still leading the chain, or |
| 86 | + * the reverse — the exact split #1269's fix exists to close). |
| 87 | + * |
| 88 | + * `id` is a canonical identity id (`readNodeLocalIdentity(node).id`: NFC + |
| 89 | + * 256-byte field cap, no trimming), and every candidate is measured through |
| 90 | + * the SAME reader, so the count is over exactly the identities the replay |
| 91 | + * verifier keys on. There is deliberately NO ancestry / parent-walk |
| 92 | + * exclusion: an id is non-selective the moment two nodes anywhere in the tree |
| 93 | + * carry it, independent of structural context or a broken parent linkage. |
| 94 | + */ |
| 95 | +export function idMatchCountInTree(nodes: readonly IdentityTreeNode[], id: string): number { |
| 96 | + let count = 0; |
| 97 | + for (const node of nodes) { |
| 98 | + if (readNodeLocalIdentity(node).id === id) count += 1; |
| 99 | + } |
| 100 | + return count; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * ADR 0012 decision 3 amendment (#1269): `identity` with its `id` demoted |
| 105 | + * whenever the id is non-unique in `nodes`, built on the SAME |
| 106 | + * `idMatchCountInTree` predicate `buildSelectorChainForNode`'s |
| 107 | + * `selectableId` keys off directly. `computeTargetEvidence` uses this |
| 108 | + * whole-identity form; extracted so a third call site (#1280's |
| 109 | + * press-retarget identity-empty check, `src/core/press-retarget.ts`) |
| 110 | + * shares it rather than re-deriving the rule a third way. A demoted id |
| 111 | + * falls back to role+label, the same shape an unrecorded id already |
| 112 | + * computes. |
| 113 | + */ |
| 114 | +export function demoteNonUniqueLocalIdentity( |
| 115 | + identity: LocalIdentity, |
| 116 | + nodes: readonly IdentityTreeNode[], |
| 117 | +): LocalIdentity { |
| 118 | + if (identity.id === undefined) return identity; |
| 119 | + if (idMatchCountInTree(nodes, identity.id) <= 1) return identity; |
| 120 | + const { role, label } = identity; |
| 121 | + return { role, ...(label !== undefined ? { label } : {}) }; |
| 122 | +} |
| 123 | + |
| 124 | +type StructuralNode = Pick<RawSnapshotNode, 'index' | 'parentIndex'>; |
| 125 | + |
| 126 | +/** Zero-based ordinal among the node's own parent's children, in document order. */ |
| 127 | +export function siblingOrdinal(nodes: readonly StructuralNode[], node: StructuralNode): number { |
| 128 | + let ordinal = 0; |
| 129 | + for (const candidate of nodes) { |
| 130 | + if (candidate.parentIndex !== node.parentIndex) continue; |
| 131 | + if (candidate.index === node.index) return ordinal; |
| 132 | + ordinal += 1; |
| 133 | + } |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +export function readNodeStructuralDenotation( |
| 138 | + node: StructuralNode, |
| 139 | + nodes: readonly StructuralNode[], |
| 140 | +): NodeStructuralDenotation { |
| 141 | + return { documentOrder: node.index, sibling: siblingOrdinal(nodes, node) }; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * The guard passes only when BOTH structural discriminators agree — a |
| 146 | + * fail-closed comparison: two same-local-identity duplicates differ in |
| 147 | + * `documentOrder` (always) and usually `sibling`, so the guard refuses |
| 148 | + * whenever dispatch resolved a different member than verification isolated. |
| 149 | + */ |
| 150 | +export function structuralDenotationsEqual( |
| 151 | + a: NodeStructuralDenotation, |
| 152 | + b: NodeStructuralDenotation, |
| 153 | +): boolean { |
| 154 | + return a.documentOrder === b.documentOrder && a.sibling === b.sibling; |
| 155 | +} |
29 | 156 |
|
30 | 157 | /** The recorded annotation's identity tier as a bare `LocalIdentity` (drop-empty-keys form). */ |
31 | 158 | export function annotationLocalIdentity( |
|
0 commit comments