Repository navigation
Expand file tree
/
Copy pathselector-recording.ts
More file actions
197 lines (182 loc) · 7.57 KB
/
Copy pathselector-recording.ts
File metadata and controls
197 lines (182 loc) · 7.57 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
196
197
import type { DaemonRequest } from './daemon-request.ts';
import type { SnapshotNode } from '@agent-device/kernel/snapshot';
import type { FindReadResult } from '@agent-device/contracts/interaction';
import { stripAndroidSystemChromeProvenanceFromNode } from '@agent-device/contracts/android-system-chrome';
import { SessionStore } from './session-store.ts';
import type { SessionRef } from './session-state.ts';
import { isInteractiveObservation } from './session-action-recorder.ts';
import { isSessionRecording } from './session-script-publication-capability.ts';
import {
computeTargetEvidence,
type RecordedTargetCapture,
type TargetEvidenceMode,
} from '@agent-device/selectors/target-evidence';
export function buildFindRecordResult(
result: Record<string, unknown>,
action: 'exists' | 'wait' | 'get_text' | 'get_attrs' | 'list',
): Record<string, unknown> {
if (action === 'exists') return { found: true };
if (action === 'wait') {
return { found: true, waitedMs: result.waitedMs };
}
if (action === 'list') {
return {
action: 'list',
matches: Array.isArray(result.matches) ? result.matches.length : 0,
};
}
const ref = typeof result.ref === 'string' ? result.ref : undefined;
if (action === 'get_attrs') return { ref, action: 'get attrs' };
return {
ref,
action: 'get text',
text: typeof result.text === 'string' ? result.text : '',
};
}
// The daemon consumes the engine's find result verbatim; the shape lives in
// contracts (below both zones) per R2.
type DaemonFindResult = FindReadResult;
export function toDaemonFindData(result: DaemonFindResult): Record<string, unknown> {
if (result.kind === 'found') {
return {
found: true,
...(typeof result.waitedMs === 'number' ? { waitedMs: result.waitedMs } : {}),
};
}
if (result.kind === 'list') {
return {
matches: result.matches.map((match) => ({
...daemonRefAddress(match.ref),
node: stripAndroidSystemChromeProvenanceFromNode(match.node),
})),
};
}
return {
...daemonRefAddress(result.ref),
...(result.kind === 'text' ? { text: result.text } : {}),
node: stripAndroidSystemChromeProvenanceFromNode(result.node),
};
}
/**
* A match the capture's producer issued no ref for carries no `ref` own-property. The wire form
* already hid that case — `JSON.stringify` drops an `undefined` value — but the response DATA object
* is what the CLI formatter and a Node consumer read, and an own-property whose value is not a ref
* is exactly what the optional field exists to make impossible. Omitting the key also keeps the two
* consumers of that object reading one rule: an absent address, never an address-shaped `undefined`.
*/
function daemonRefAddress(ref: string | undefined): { ref?: string } {
return ref === undefined ? {} : { ref };
}
export function buildGetRecordResult(
result: Record<string, unknown>,
property: 'text' | 'attrs',
): Record<string, unknown> {
const selectorChain = Array.isArray(result.selectorChain) ? result.selectorChain : undefined;
const resolvedTarget = getResolvedTarget(result);
const ref = resolvedTarget?.kind === 'ref' ? normalizeDaemonRef(resolvedTarget.ref) : undefined;
const selector = resolvedTarget?.kind === 'selector' ? resolvedTarget.selector : undefined;
const recordedTarget = {
...(ref ? { ref } : {}),
...(selector ? { selector } : {}),
...(selectorChain ? { selectorChain } : {}),
};
if (property === 'attrs') return recordedTarget;
const text = typeof result.text === 'string' ? result.text : '';
return {
...recordedTarget,
text,
refLabel: compactRecordedGetRefLabel(text),
};
}
type DaemonGetResult = {
target: { kind: 'ref'; ref: string } | { kind: 'selector'; selector: string };
text?: string;
node: SnapshotNode;
};
export function toDaemonGetData(result: DaemonGetResult): Record<string, unknown> {
const { target } = result;
return {
...(target.kind === 'ref' ? { ref: normalizeDaemonRef(target.ref) } : {}),
...(target.kind === 'selector' ? { selector: target.selector } : {}),
...(typeof result.text === 'string' ? { text: result.text } : {}),
node: stripAndroidSystemChromeProvenanceFromNode(result.node),
};
}
/**
* #1349: a recorded `wait`/`is` entry without the result's resolution
* payload — `node`/`preActionNodes` feed `computeTargetEvidence` once and
* must not be retained on `session.actions` for the session's lifetime.
*/
export function stripResolutionPayload<T extends Record<string, unknown>>(result: T): T {
const { node: _node, preActionNodes: _preActionNodes, ...recordResult } = result;
return recordResult as T;
}
export function toDaemonWaitData(result: Record<string, unknown>): Record<string, unknown> {
return {
waitedMs: result.waitedMs,
...(typeof result.text === 'string' ? { text: result.text } : {}),
...(typeof result.selector === 'string' ? { selector: result.selector } : {}),
...(typeof result.captures === 'number' ? { captures: result.captures } : {}),
...(typeof result.nodeCount === 'number' ? { nodeCount: result.nodeCount } : {}),
...(typeof result.hint === 'string' ? { hint: result.hint } : {}),
};
}
export function stripSelectorChain<T extends Record<string, unknown>>(result: T): T {
const { selectorChain: _selectorChain, ...publicResult } = result;
return publicResult as T;
}
export function recordIfSession(
sessionStore: SessionStore,
ref: SessionRef | undefined,
req: DaemonRequest,
result: Record<string, unknown>,
/** ADR 0012 decision 3: record-time input for the `target-v1` annotation. */
recordedTarget?: RecordedTargetCapture,
/** #1349: `landmark` for wait's existence-semantics evidence; defaults to `action`. */
evidenceMode?: TargetEvidenceMode,
): void {
if (!ref) return;
const session = sessionStore.resolveCurrent(ref);
if (!session) return;
const targetEvidence =
isSessionRecording(session) && recordedTarget
? computeTargetEvidence(recordedTarget, { mode: evidenceMode })
: undefined;
sessionStore.recordAction(ref, {
command: req.command,
positionals: req.positionals ?? [],
flags: req.flags ?? {},
result,
// #1271 stage 2: the shared recorder for `get`, `is`, `wait`, and read-only
// `find`. `isInteractiveObservation` decides both halves — the command
// class AND the provenance — so an authored plan step recorded through
// this same path keeps its place in the healed script.
interactiveObservation: isInteractiveObservation(req),
// #1398: lets session-scoped echo protection apply the SAME
// landmark-vs-action treatment #1349 already established for
// identity-empty evidence.
...(targetEvidence ? { targetEvidence, targetEvidenceMode: evidenceMode ?? 'action' } : {}),
});
}
function compactRecordedGetRefLabel(text: string): string | undefined {
const trimmed = text.trim();
if (!trimmed || trimmed.length > 80 || /[\r\n]/.test(trimmed)) return undefined;
return trimmed;
}
function getResolvedTarget(
result: Record<string, unknown>,
): { kind: 'ref'; ref: string } | { kind: 'selector'; selector: string } | undefined {
const target = result.target;
if (!target || typeof target !== 'object') return undefined;
const record = target as Record<string, unknown>;
if (record.kind === 'ref' && typeof record.ref === 'string') {
return { kind: 'ref', ref: record.ref };
}
if (record.kind === 'selector' && typeof record.selector === 'string') {
return { kind: 'selector', selector: record.selector };
}
return undefined;
}
function normalizeDaemonRef(ref: string): string {
return ref.startsWith('@') ? ref.slice(1) : ref;
}