Repository navigation
Expand file tree
/
Copy pathrecord.ts
More file actions
302 lines (270 loc) · 11.6 KB
/
Copy pathrecord.ts
File metadata and controls
302 lines (270 loc) · 11.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { AppError } from '@agent-device/kernel/errors';
import type { DeviceKind, DeviceTarget, PublicPlatform } from '@agent-device/kernel/device';
import type {
Point,
PresentedSurface,
Rect,
SnapshotKeyboardBandFact,
} from '@agent-device/kernel/snapshot';
function readRequired<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
message: string,
): T {
const value = parse(record[key]);
if (value === undefined) {
throw new AppError('COMMAND_FAILED', message, { response: record });
}
return value;
}
function readOptional<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
): T | undefined {
return parse(record[key]);
}
function readNullable<T>(
record: Record<string, unknown>,
key: string,
parse: (value: unknown) => T | undefined,
): T | null | undefined {
const value = record[key];
return value === null ? null : parse(value);
}
export function readRequiredString(record: Record<string, unknown>, key: string): string {
return readRequired(record, key, parseNonEmptyString, `Daemon response is missing "${key}".`);
}
export function readOptionalString(
record: Record<string, unknown>,
key: string,
): string | undefined {
return readOptional(record, key, parseNonEmptyString);
}
export function readNullableString(
record: Record<string, unknown>,
key: string,
): string | null | undefined {
return readNullable(record, key, parseNonEmptyString);
}
export function readRequiredNumber(record: Record<string, unknown>, key: string): number {
return readRequired(
record,
key,
parseFiniteNumber,
`Daemon response is missing numeric "${key}".`,
);
}
export function readRequiredPlatform(record: Record<string, unknown>, key: string): PublicPlatform {
return readRequired(record, key, parsePlatform, `Daemon response has invalid "${key}".`);
}
export function readRequiredDeviceKind(record: Record<string, unknown>, key: string): DeviceKind {
return readRequired(record, key, parseDeviceKind, `Daemon response has invalid "${key}".`);
}
export function readDeviceTarget(record: Record<string, unknown>, key: string): DeviceTarget {
return readOptional(record, key, parseDeviceTarget) ?? 'mobile';
}
export function parseRect(value: unknown): Rect | undefined {
if (!isRecord(value)) return undefined;
const x = readNumberField(value, 'x');
const y = readNumberField(value, 'y');
const width = readNumberField(value, 'width');
const height = readNumberField(value, 'height');
if (x === undefined || y === undefined || width === undefined || height === undefined) {
return undefined;
}
return { x, y, width, height };
}
/**
* Reads a producer-declared keyboard band (#2660) out of an untyped payload, shared by every seam
* that receives one: the Apple runner's wire reader, the daemon's Node client reader. `undefined`
* means the producer published no fact at all, which is how a tier that never reads the keyboard is
* distinguished from one that measured and reported.
*
* A payload that cannot be placed is restated as `unmeasurable` naming where it failed, never dropped
* and never trusted: silence would tell the tap guard the producer never looked. Validation reasons
* identify the rejected shape: `malformed-fact`, `unrecognized-kind`, `unreported-reason`,
* `invalid-visibility-probe`, `unrecognized-derivation`, `invalid-visible-frame`, or
* `invalid-derived-frame`. They describe validation failures rather than levels of trust.
*
* `treeBound` survives only as the literal `true` on the kinds that can carry it; any other value reads
* as unbound, which costs a reader evidence but never claims a binding the producer did not make.
*/
export function readSnapshotKeyboardBandFact(value: unknown): SnapshotKeyboardBandFact | undefined {
if (value === undefined) return undefined;
if (!isRecord(value)) return { kind: 'unmeasurable', reason: 'malformed-fact' };
if (value.kind === 'absent') return { kind: 'absent', ...treeBinding(value) };
if (value.kind === 'unmeasurable') {
return typeof value.reason === 'string' && value.reason.trim().length > 0
? { kind: 'unmeasurable', reason: value.reason }
: { kind: 'unmeasurable', reason: 'unreported-reason' };
}
if (value.kind === 'tree-rejected') return readKeyboardTreeRejection(value);
if (value.kind === 'visibility-only') {
return typeof value.visible === 'boolean'
? { kind: 'visibility-only', visible: value.visible }
: { kind: 'unmeasurable', reason: 'invalid-visibility-probe' };
}
if (value.kind === 'derived' && value.derivation !== 'docked-tree') {
return { kind: 'unmeasurable', reason: 'unrecognized-derivation' };
}
if (value.kind === 'visible' || value.kind === 'derived') {
return readKeyboardFrameFact(value.frame, value.kind, treeBinding(value));
}
return { kind: 'unmeasurable', reason: 'unrecognized-kind' };
}
function treeBinding(value: Record<string, unknown>): { treeBound?: true } {
return value.treeBound === true ? { treeBound: true } : {};
}
function readKeyboardTreeRejection(value: Record<string, unknown>): SnapshotKeyboardBandFact {
if (value.derivation !== 'docked-tree') {
return { kind: 'unmeasurable', reason: 'unrecognized-derivation' };
}
if (value.visible !== undefined && typeof value.visible !== 'boolean') {
return { kind: 'unmeasurable', reason: 'invalid-visibility-probe' };
}
return {
kind: 'tree-rejected',
derivation: 'docked-tree',
...(typeof value.visible === 'boolean' ? { visible: value.visible } : {}),
};
}
function readKeyboardFrameFact(
value: unknown,
kind: 'visible' | 'derived',
binding: { treeBound?: true },
): SnapshotKeyboardBandFact {
const frame = parseRect(value);
if (!isPlottableKeyboardFrame(frame)) {
return { kind: 'unmeasurable', reason: `invalid-${kind}-frame` };
}
return kind === 'derived'
? { kind: 'derived', derivation: 'docked-tree', frame, ...binding }
: { kind: 'visible', frame, ...binding };
}
// Same three refusals as `isPositiveFiniteRect` in kernel/rect, inlined: this module is a leaf that
// many facades evaluate, and a rect import here would land in every one of their closures.
// `record.test.ts` pins the parity on one rect table.
function isPlottableKeyboardFrame(frame: Rect | undefined): frame is Rect {
if (!frame) return false;
const { x, y, width, height } = frame;
if (![x, y, width, height].every(Number.isFinite)) return false;
if (!Number.isFinite(x + width) || !Number.isFinite(y + height)) return false;
if (width <= 0 || height <= 0) return false;
const isCGRectInfinite =
x === -Number.MAX_VALUE / 2 &&
y === -Number.MAX_VALUE / 2 &&
width === Number.MAX_VALUE &&
height === Number.MAX_VALUE;
return !isCGRectInfinite;
}
/**
* The declared states, each keyed by its own name so a payload string is a state only when this table
* has that exact key. Keyed by `PresentedSurface`, so a state added to the vocabulary without a row
* here fails to compile instead of silently reading as `unknown`.
*/
const PRESENTED_SURFACE_BY_NAME: Readonly<Record<PresentedSurface, PresentedSurface>> = {
presented: 'presented',
absent: 'absent',
unknown: 'unknown',
};
/** The key a producer publishes its presented-surface fact under (#3565). */
const PRESENTED_SURFACE_WIRE_KEY = 'presentedSurface';
/**
* Reads a producer's presented-surface fact (#3565) out of a response or runner payload, shared by the
* Apple runner's wire reader and the Node client's. Taking the payload rather than the value is the
* point: reading a wire fact is a property lookup on data the producer controls, so it has to be
* own-property both ways. A missing key is the producer stating nothing and stays silence — and an
* inherited one is not silence's substitute, since a plain lookup walks to `Object.prototype`, where
* anything sharing the process could park `'absent'`, the one state that disarms the confirmation
* #1831 arms. A value under the fact's own key which is not one of the three states is restated as
* `'unknown'` rather than dropped: the reader cannot tell a malformed `'absent'` from a genuine one,
* and silence would leave a consumer arming a dismissal confirmation on nothing — the guessing this
* fact retires. Only a name in this table is trusted, so a payload naming `constructor` is not.
*/
export function readPresentedSurfaceFact(
payload: Record<string, unknown>,
): PresentedSurface | undefined {
if (!Object.hasOwn(payload, PRESENTED_SURFACE_WIRE_KEY)) return undefined;
const value = payload[PRESENTED_SURFACE_WIRE_KEY];
return typeof value === 'string' && Object.hasOwn(PRESENTED_SURFACE_BY_NAME, value)
? PRESENTED_SURFACE_BY_NAME[value as PresentedSurface]
: 'unknown';
}
export function parsePoint(value: unknown): Point | undefined {
if (!isRecord(value)) return undefined;
const x = readNumberField(value, 'x');
const y = readNumberField(value, 'y');
if (x === undefined || y === undefined) {
return undefined;
}
return { x, y };
}
function readNumberField(record: Record<string, unknown>, key: string): number | undefined {
const value = record[key];
return typeof value === 'number' ? value : undefined;
}
function parseNonEmptyString(value: unknown): string | undefined {
return typeof value === 'string' && value.length > 0 ? value : undefined;
}
function parseFiniteNumber(value: unknown): number | undefined {
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
}
// Client-side parser for the PUBLIC leaf platform a daemon response carries. Under
// approach (b) the daemon always emits leaf strings (`ios`/`macos`), never the
// internal `apple`; the extra `apple` acceptance is forward-compat only, and — being
// unable to disambiguate the Apple OS from the bare token — maps to the dominant
// `ios` leaf (unreachable today, since output is never `apple`).
function parsePlatform(value: unknown): PublicPlatform | undefined {
if (value === 'apple') return 'ios';
return value === 'ios' ||
value === 'macos' ||
value === 'android' ||
value === 'harmonyos' ||
value === 'vega' ||
value === 'linux' ||
value === 'web'
? value
: undefined;
}
function parseDeviceKind(value: unknown): DeviceKind | undefined {
return value === 'simulator' || value === 'emulator' || value === 'device' ? value : undefined;
}
function parseDeviceTarget(value: unknown): DeviceTarget | undefined {
return value === 'tv' || value === 'mobile' || value === 'desktop' ? value : undefined;
}
export function asRecord(value: unknown): Record<string, unknown> {
if (!isRecord(value)) {
throw new AppError('COMMAND_FAILED', 'Daemon returned an unexpected response shape.', {
value,
});
}
return value;
}
export function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
export function asOptionalRecord(value: unknown): Record<string, unknown> | undefined {
return isRecord(value) ? value : undefined;
}
type WithoutUndefined<T extends Record<string, unknown>> = {
[K in keyof T as undefined extends T[K] ? never : K]: T[K];
} & {
[K in keyof T as undefined extends T[K] ? K : never]?: Exclude<T[K], undefined>;
};
export function stripUndefined<T extends Record<string, unknown>>(value: T): WithoutUndefined<T> {
const output = {} as WithoutUndefined<T>;
for (const [key, current] of Object.entries(value)) {
if (current !== undefined) {
(output as Record<string, unknown>)[key] = current;
}
}
return output;
}
export function splitNonEmptyTrimmedLines(text: string): string[] {
return text
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0);
}