Repository navigation
Expand file tree
/
Copy pathsnapshot-state.ts
More file actions
195 lines (188 loc) · 7.97 KB
/
Copy pathsnapshot-state.ts
File metadata and controls
195 lines (188 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import type { CommandFlags } from '@agent-device/contracts/command';
import {
readSnapshotOcclusionContextEvidence,
readSnapshotTextInputGeometry,
attachSnapshotTextInputGeometry,
snapshotCaptureAnnotationsFrom,
publicSnapshotCaptureAnnotations,
} from '@agent-device/contracts/capture';
import { isAndroidInputMethodNode } from '@agent-device/contracts/android-input-ownership';
import {
attachRefs,
buildSnapshotPresentationKey,
SNAPSHOT_PRESENTATION_OPTION_KEYS,
snapshotOptionsFromFlags,
type SnapshotPresentation,
type SnapshotPresentationFlagInput,
snapshotRefMinting,
type IosTargetActivation,
type PresentedSurface,
type RawSnapshotNode,
type SnapshotBackend,
type SnapshotCaptureProvenance,
type SnapshotKeyboardBandFact,
snapshotStateProvenance,
type SnapshotState,
type SnapshotViewportSize,
type SnapshotQualityNote,
} from '@agent-device/kernel/snapshot';
import {
annotateCoveredSnapshotNodes,
annotateSnapshotNodesCoveredByPolicy,
} from './snapshot-occlusion.ts';
import { coveredAndroidReplacementNodeIndexes } from './snapshot/android-replacement-surface-occlusion.ts';
import { scopeSnapshotNodes } from './snapshot-desktop-projection.ts';
import { normalizeSnapshotTree, pruneGroupNodes } from './snapshot-tree-ingestion.ts';
import { iosSnapshotComparisonIdentityKey } from './ios-snapshot-planning.ts';
import type { IosSystemSurfaceProvenance } from '@agent-device/contracts/ios-system-surface';
import type { IosSnapshotComparisonIdentity } from '@agent-device/contracts/ios-snapshot';
/** The presentation `flags` capture with; absent flags read as the default full tree. */
export function snapshotPresentationOptionsFromFlags(
flags: SnapshotPresentationFlagInput | undefined,
): SnapshotPresentation {
return snapshotOptionsFromFlags(flags, SNAPSHOT_PRESENTATION_OPTION_KEYS);
}
/**
* The ONE daemon assembly of a captured tree (ADR 0004 / #1797): normalize, group prune,
* post-wire scope for backends that do not scope in their own projection, occlusion annotation,
* refs. Every consumer of a captured tree — the
* snapshot command, selector captures, settle observation, Android blocking-dialog recovery —
* goes through here, so no two call sites can disagree about what a snapshot contains.
*
* The assembly does not present. iOS acquisitions are presented exactly once, by the snapshot
* engine, before they reach here (#2199 / #2188 invariant 2), and a capture arrives with its
* whole provenance pair so no branch here can rediscover who presented it.
*
* Kept below the daemon-server type cycle on purpose: it needs no session state.
*/
export function buildSnapshotState(
data: {
nodes?: RawSnapshotNode[];
truncated?: boolean;
quality?: unknown;
qualityNotes?: SnapshotQualityNote[];
comparisonIdentity?: IosSnapshotComparisonIdentity;
systemSurface?: IosSystemSurfaceProvenance;
/** Foreground repair this capture's own command performed (#2682). */
targetActivation?: IosTargetActivation;
/** The keyboard band the producer measured, carried to the state the tap guards read (#2660). */
keyboard?: SnapshotKeyboardBandFact;
/** The box the producer measured the rects in (#3182), carried to the response that reads this state. */
viewport?: SnapshotViewportSize;
/**
* iOS: the producer's reading of a modal presentation over the app (#3565), decided on the
* acquired tree so every projection of one screen carries the same value.
*/
presentedSurface?: PresentedSurface;
} & SnapshotCaptureProvenance,
flags:
| (Pick<CommandFlags, 'snapshotDepth' | 'snapshotInteractiveOnly' | 'snapshotRaw'> &
Partial<Pick<CommandFlags, 'snapshotScope'>>)
| undefined,
): SnapshotState {
const rawNodes = data?.nodes ?? [];
const snapshotRaw = flags?.snapshotRaw;
const backendAnnotatedNodes = annotateBackendReplacementSurfaces(
data,
rawNodes,
snapshotRaw === true,
);
const normalizedNodes = normalizeSnapshotTree(
snapshotRaw ? backendAnnotatedNodes : pruneGroupNodes(backendAnnotatedNodes),
);
const scopedNodes =
flags?.snapshotScope && backendScopesAfterWire(data?.backend)
? scopeSnapshotNodes(normalizedNodes, flags.snapshotScope)
: normalizedNodes;
const annotations = snapshotCaptureAnnotationsFrom(data);
const presentation = snapshotPresentationOptionsFromFlags(flags);
const nodes = attachRefs(
snapshotRaw
? scopedNodes
: annotateCoveredSnapshotNodes(scopedNodes, overlayOptionsForBackend(data?.backend)),
snapshotRefMinting(data),
);
const textInputGeometry = readSnapshotTextInputGeometry(data.nodes);
if (textInputGeometry) attachSnapshotTextInputGeometry(nodes, textInputGeometry);
return {
nodes,
truncated: data?.truncated,
createdAt: Date.now(),
...snapshotStateProvenance(data),
...publicSnapshotCaptureAnnotations({
quality: annotations.quality,
qualityNotes: annotations.qualityNotes,
}),
...(data.comparisonIdentity
? { comparisonKey: iosSnapshotComparisonIdentityKey(data.comparisonIdentity) }
: {}),
...(data.systemSurface ? { iosSystemSurfaceBundleId: data.systemSurface.bundleId } : {}),
...(data.presentedSurface ? { presentedSurface: data.presentedSurface } : {}),
...(data.targetActivation ? { targetActivation: data.targetActivation } : {}),
...(data.keyboard ? { keyboard: data.keyboard } : {}),
...(data.viewport ? { viewport: data.viewport } : {}),
presentationKey: buildSnapshotPresentationKey(presentation),
presentation,
// Only broad Android snapshots become freshness baselines. If the user asked for a scoped
// or filtered view, preserve that output contract but avoid pretending it is safe for
// route-level comparisons on the next capture.
comparisonSafe: isAndroidComparisonSafeSnapshot(data?.backend, flags),
};
}
function annotateBackendReplacementSurfaces(
owner: object & { backend?: SnapshotBackend },
nodes: RawSnapshotNode[],
raw: boolean,
): RawSnapshotNode[] {
return owner.backend === 'android' && !raw
? annotateAndroidReplacementSurfaces(owner, nodes)
: nodes;
}
function overlayOptionsForBackend(backend: SnapshotBackend | undefined) {
return backend === 'android' ? { isAdditionalOverlayNode: isAndroidInputMethodNode } : {};
}
function annotateAndroidReplacementSurfaces(
owner: object,
nodes: RawSnapshotNode[],
): RawSnapshotNode[] {
const context = readSnapshotOcclusionContextEvidence(owner);
const coveredSourceIndexes = coveredAndroidReplacementNodeIndexes(
context?.nodes ?? nodes,
context?.androidSiblingOrderByNodeIndex,
);
return annotateSnapshotNodesCoveredByPolicy(nodes, (node) => {
const sourceIndex = context?.sourceIndexByNodeIndex.get(node.index) ?? node.index;
return coveredSourceIndexes.has(sourceIndex);
});
}
/**
* Scope resolves once per snapshot, and this names the channels that still need the post-wire
* pass. Every other channel scopes inside its own projection — Android and the macOS helper at
* capture, iOS in the snapshot engine — and a second pass would re-match inside an already-scoped
* tree and hand the two layers different no-match semantics (#1832 C2). Naming the channels that
* need the pass rather than the ones that do not keeps iOS out of post-wire scope planning
* entirely, so the list shrinks as a channel takes ownership instead of growing by exclusion
* (#2199).
*/
function backendScopesAfterWire(backend: SnapshotBackend | undefined): boolean {
return (
backend === undefined ||
backend === 'linux-atspi' ||
backend === 'harmonyos-arkui' ||
backend === 'web'
);
}
function isAndroidComparisonSafeSnapshot(
backend: SnapshotBackend | undefined,
flags:
| (Pick<CommandFlags, 'snapshotDepth' | 'snapshotInteractiveOnly' | 'snapshotRaw'> &
Partial<Pick<CommandFlags, 'snapshotScope'>>)
| undefined,
): boolean {
return (
backend === 'android' &&
flags?.snapshotInteractiveOnly !== true &&
typeof flags?.snapshotDepth !== 'number' &&
!flags?.snapshotScope
);
}