forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomi-harness
More file actions
executable file
·1522 lines (1319 loc) · 63.1 KB
/
Copy pathomi-harness
File metadata and controls
executable file
·1522 lines (1319 loc) · 63.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Run Omi desktop experience flows through the local automation bridge."""
from __future__ import annotations
import argparse
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError
from decimal import Decimal, InvalidOperation
import json
import os
import re
import shutil
import statistics
import subprocess
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
import typing
SCHEMA_VERSION = 2
MIN_COMPATIBLE_FLOW_VERSION = 1
SCRIPT_DIR = Path(__file__).resolve().parent
DESKTOP_DIR = SCRIPT_DIR.parent
DEFAULT_PORT = int(os.environ.get("OMI_AUTOMATION_PORT", "47777"))
DEFAULT_RUN_ROOT = DESKTOP_DIR / ".harness/runs"
POLL_INTERVAL_SECONDS = 0.02
NAMED_NON_PRODUCTION_BUNDLE_PREFIX = "com.omi.omi-"
STABLE_ACCESSIBILITY_IDENTIFIER = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]*")
AUTOMATION_UI_PRESENTATION_ACTION = "set_automation_ui_presentation"
AUTOMATION_UI_PRESENTATION_MODES = {"quiet", "interactive", "normal"}
if str(SCRIPT_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPT_DIR))
from automation_token_lib import ( # noqa: E402
automation_token,
automation_token_missing_message,
)
@dataclass
class HarnessContext:
base_url: str
flow_path: Path
run_dir: Path
steps_dir: Path
lane: str
log_path: Path
log_start: int
bundle_id: str | None
process_match: str | None
presentation_restore_mode: str | None = None
presentation_control_available: bool = False
def now_iso() -> str:
return datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
def slug(value: str) -> str:
cleaned = re.sub(r"[^A-Za-z0-9_.-]+", "-", value.strip().lower()).strip("-")
return cleaned or "flow"
def read_yaml(path: Path) -> dict[str, typing.Any]:
try:
import yaml
except ImportError as exc:
print(
"omi-harness: PyYAML is required to run flow files; install it with `python3 -m pip install PyYAML`",
file=sys.stderr,
)
raise SystemExit(2) from exc
with path.open("r", encoding="utf-8") as handle:
return yaml.safe_load(handle) or {}
def env_flag(name: str) -> bool:
return os.environ.get(name, "").strip().lower() in {"1", "true", "yes", "on"}
def validate_flow_schema(flow: dict[str, typing.Any], args: argparse.Namespace) -> int:
raw_version = flow.get("version")
if isinstance(raw_version, bool) or not isinstance(raw_version, int):
raise SystemExit("omi-harness: flow version must be an integer")
if raw_version > SCHEMA_VERSION:
raise SystemExit(
f"omi-harness: flow schema version {raw_version} is newer than supported version {SCHEMA_VERSION}"
)
if raw_version < MIN_COMPATIBLE_FLOW_VERSION:
raise SystemExit(
f"omi-harness: flow schema version {raw_version} is older than compatible version "
f"{MIN_COMPATIBLE_FLOW_VERSION}"
)
if raw_version < SCHEMA_VERSION and not getattr(args, "allow_legacy_flow_version", False):
raise SystemExit(
f"omi-harness: flow schema version {raw_version} requires explicit compatibility; "
"pass --allow-legacy-flow-version or set OMI_HARNESS_ALLOW_LEGACY_FLOW_VERSION=1"
)
return raw_version
def write_json(path: Path, data: typing.Any) -> None:
path.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n", encoding="utf-8")
def automation_port_from_base_url(base_url: str) -> int:
return int(base_url.rsplit(":", 1)[1])
def request_json(
base_url: str,
method: str,
route: str,
body: dict[str, typing.Any] | None = None,
authenticate: bool = True,
) -> dict[str, typing.Any]:
data = None
headers = {"Accept": "application/json"}
if authenticate:
port = automation_port_from_base_url(base_url)
token = automation_token(port)
if not token:
# Fail loud instead of omitting Authorization and surfacing a generic 401.
return {"ok": False, "error": automation_token_missing_message(port)}
headers["Authorization"] = f"Bearer {token}"
if body is not None:
data = json.dumps(body).encode("utf-8")
headers["Content-Type"] = "application/json"
request = urllib.request.Request(f"{base_url}{route}", data=data, method=method, headers=headers)
try:
with urllib.request.urlopen(request, timeout=20) as response:
return json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as exc:
payload = exc.read().decode("utf-8", errors="replace")
try:
parsed = json.loads(payload)
except json.JSONDecodeError:
parsed = {"ok": False, "error": payload}
parsed["http_status"] = exc.code
return parsed
except urllib.error.URLError as exc:
return {"ok": False, "error": f"connection_failed: {exc.reason}"}
except TimeoutError as exc:
return {"ok": False, "error": f"connection_timeout: {exc}"}
class AutomationUIPresentationUnavailable(RuntimeError):
"""The bundle predates the optional automation UI presentation action."""
def automation_ui_presentation_unavailable(response: dict[str, typing.Any]) -> bool:
error = str(response.get("error") or "")
return response.get("http_status") in {404, 501} or error.startswith("unknown_action:")
def automation_ui_presentation_detail(response: dict[str, typing.Any]) -> dict[str, typing.Any]:
if automation_ui_presentation_unavailable(response):
raise AutomationUIPresentationUnavailable(
f"{AUTOMATION_UI_PRESENTATION_ACTION} is unavailable on this bundle"
)
if not response.get("ok"):
raise RuntimeError(response.get("error") or "automation UI presentation action failed")
result = response.get("result")
detail = result.get("detail") if isinstance(result, dict) else None
if not isinstance(detail, dict):
raise RuntimeError("automation UI presentation action returned no detail")
return detail
def automation_ui_presentation(
ctx: HarnessContext, mode: str | None = None, activate: bool = False
) -> dict[str, typing.Any]:
payload: dict[str, typing.Any] = {"name": AUTOMATION_UI_PRESENTATION_ACTION}
if mode is not None:
if mode not in AUTOMATION_UI_PRESENTATION_MODES:
raise ValueError(f"unsupported automation UI presentation mode: {mode!r}")
payload["params"] = {"mode": mode, "activate": activate}
return automation_ui_presentation_detail(
request_json(ctx.base_url, "POST", "/action", payload)
)
def validate_automation_ui_presentation_mode(detail: dict[str, typing.Any], key: str) -> str:
mode = detail.get(key)
if mode not in AUTOMATION_UI_PRESENTATION_MODES:
raise RuntimeError(f"automation UI presentation detail missing valid {key}: {detail!r}")
return str(mode)
def prepare_automation_ui_presentation(ctx: HarnessContext, warnings: list[str]) -> None:
"""Take quiet-mode ownership for the run, when the bundle supports it."""
try:
query_detail = automation_ui_presentation(ctx)
except AutomationUIPresentationUnavailable as exc:
warnings.append(f"{exc}; continuing without quiet UI presentation")
return
# The query is intentional: it exercises the action's read path before any
# harness-owned presentation change. The setter's previous_mode remains the
# authoritative restore target in case a user changes the mode between calls.
prior_mode = validate_automation_ui_presentation_mode(query_detail, "mode")
# Record the queried mode before attempting the setter. If the setter
# changes the app and then loses its response, teardown still has a target.
ctx.presentation_restore_mode = prior_mode
ctx.presentation_control_available = True
try:
quiet_detail = automation_ui_presentation(ctx, "quiet", activate=False)
except AutomationUIPresentationUnavailable as exc:
ctx.presentation_control_available = False
warnings.append(f"{exc}; continuing without quiet UI presentation")
return
restore_mode = validate_automation_ui_presentation_mode(quiet_detail, "previous_mode")
ctx.presentation_restore_mode = restore_mode
# Mark ownership before validating the returned current mode so a malformed
# response after a successful setter still gets a best-effort restore.
ctx.presentation_control_available = True
if validate_automation_ui_presentation_mode(quiet_detail, "mode") != "quiet":
raise RuntimeError(f"automation UI presentation did not enter quiet mode: {quiet_detail!r}")
def restore_automation_ui_presentation(
ctx: HarnessContext, errors: list[str]
) -> None:
if not ctx.presentation_control_available or ctx.presentation_restore_mode is None:
return
restore_mode = ctx.presentation_restore_mode
try:
detail = automation_ui_presentation(ctx, restore_mode, activate=False)
if validate_automation_ui_presentation_mode(detail, "mode") != restore_mode:
raise RuntimeError(f"restore returned unexpected detail: {detail!r}")
except AutomationUIPresentationUnavailable as exc:
errors.append(f"failed to restore automation UI presentation to {restore_mode!r}: {exc}")
except Exception as exc: # noqa: BLE001 - teardown must be reflected in run artifacts.
errors.append(f"failed to restore automation UI presentation to {restore_mode!r}: {exc}")
finally:
ctx.presentation_control_available = False
def run_agent_swift_in_interactive_mode(
ctx: HarnessContext, args: list[str]
) -> subprocess.CompletedProcess[str]:
if not ctx.presentation_control_available:
return run_agent_swift(ctx, args)
try:
automation_ui_presentation(ctx, "interactive", activate=True)
return run_agent_swift(ctx, args)
finally:
# AX needs real pixels and activation only for the duration of this
# command; quiet mode is restored even when agent-swift fails.
automation_ui_presentation(ctx, "quiet", activate=False)
def log_path_from_health(health: dict[str, typing.Any], explicit_log: str | None = None) -> Path:
if explicit_log:
return Path(explicit_log)
raw_path = health.get("logFilePath")
if not isinstance(raw_path, str) or not raw_path or not Path(raw_path).is_absolute():
raise RuntimeError(
"automation health did not provide an absolute logFilePath; use a current named bundle "
"or pass --log explicitly"
)
return Path(raw_path)
def resolve_log_path(base_url: str, explicit_log: str | None = None) -> Path:
health = request_json(base_url, "GET", "/health", authenticate=False)
if not health.get("ok"):
raise RuntimeError(f"unable to resolve bundle log path: {health.get('error', health)}")
return log_path_from_health(health, explicit_log)
def git_sha() -> str | None:
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
return result.stdout.strip()
except (subprocess.CalledProcessError, FileNotFoundError):
return None
def tool_version(command: list[str]) -> str | None:
try:
result = subprocess.run(
command,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=5,
)
except (subprocess.SubprocessError, FileNotFoundError):
return None
return result.stdout.strip().splitlines()[0] if result.stdout.strip() else None
def nested_get(data: dict[str, typing.Any], dotted: str) -> typing.Any:
current: typing.Any = data
for part in dotted.split("."):
if isinstance(current, dict):
current = current.get(part)
else:
return None
return current
def normalize_state_response(response: dict[str, typing.Any]) -> dict[str, typing.Any]:
result = response.get("result")
return result if isinstance(result, dict) else {}
def state_snapshot(ctx: HarnessContext) -> dict[str, typing.Any]:
return normalize_state_response(request_json(ctx.base_url, "GET", "/state"))
def recent_traces(ctx: HarnessContext) -> list[dict[str, typing.Any]]:
response = request_json(ctx.base_url, "GET", "/traces/recent")
result = response.get("result")
return result if isinstance(result, list) else []
EXPECTATION_OPERATORS = {"min", "max", "exists", "contains"}
def _decimal(value: typing.Any) -> Decimal | None:
"""Coerce only assertion-operator operands; literal equality remains type-strict."""
if isinstance(value, bool) or not isinstance(value, (int, float, str)):
return None
try:
parsed = Decimal(str(value).strip())
except (InvalidOperation, ValueError):
return None
return parsed if parsed.is_finite() else None
def expectation_mismatch(actual: typing.Any, expected: typing.Any) -> str | None:
if not isinstance(expected, dict):
return None if actual == expected else f"expected strict equality with {expected!r}, got {actual!r}"
# Dictionaries remain literal values when the actual value is also a dictionary.
# A dictionary against a scalar is an explicit operator expression, so reject
# misspellings and mixed operators instead of silently treating them as equality.
if isinstance(actual, dict):
return None if actual == expected else f"expected strict equality with {expected!r}, got {actual!r}"
if not expected:
return "numeric operator expression must not be empty"
unsupported = sorted(set(expected) - EXPECTATION_OPERATORS)
if unsupported:
return f"unsupported expectation operator(s): {', '.join(unsupported)}"
if "exists" in expected:
if len(expected) != 1:
return "exists cannot be combined with numeric expectation operators"
operand = expected["exists"]
if not isinstance(operand, bool):
return f"exists requires a boolean operand, got {operand!r}"
exists = actual is not None
return None if exists == operand else f"expected value existence to be {operand!r}, got {exists!r}"
if "contains" in expected:
if len(expected) != 1:
return "contains cannot be combined with other expectation operators"
operand = expected["contains"]
if not isinstance(operand, str):
return f"contains requires a string operand, got {operand!r}"
if not isinstance(actual, str):
return f"contains requires a string actual value, got {actual!r}"
return None if operand in actual else f"expected {actual!r} to contain {operand!r}"
actual_number = _decimal(actual)
if actual_number is None:
return f"numeric operator requires a finite numeric actual value, got {actual!r}"
for operator, operand in expected.items():
operand_number = _decimal(operand)
if operand_number is None:
return f"{operator} requires a finite numeric operand, got {operand!r}"
if operator == "min" and actual_number < operand_number:
return f"expected {actual!r} to be >= {operand!r}"
if operator == "max" and actual_number > operand_number:
return f"expected {actual!r} to be <= {operand!r}"
return None
def expectation_matches(data: dict[str, typing.Any], expectations: dict[str, typing.Any]) -> bool:
return all(expectation_mismatch(nested_get(data, key), value) is None for key, value in expectations.items())
def expectation_mismatches(data: dict[str, typing.Any], expectations: dict[str, typing.Any]) -> dict[str, typing.Any]:
mismatches = {}
for key, expected in expectations.items():
actual = nested_get(data, key)
reason = expectation_mismatch(actual, expected)
if reason is not None:
mismatches[key] = {"expected": expected, "actual": actual, "reason": reason}
return mismatches
def wait_for_state(
ctx: HarnessContext,
expectations: dict[str, typing.Any],
timeout: float = 5.0,
stability_window_seconds: float = 0.0,
) -> tuple[bool, dict[str, typing.Any]]:
deadline = time.monotonic() + timeout
stability_window_seconds = max(0.0, stability_window_seconds)
stable_since: float | None = None
latest: dict[str, typing.Any] = {}
while time.monotonic() < deadline:
latest = state_snapshot(ctx)
observed_at = time.monotonic()
if expectation_matches({"state": latest}, expectations):
if stable_since is None:
stable_since = observed_at
if observed_at - stable_since >= stability_window_seconds:
return True, latest
else:
stable_since = None
time.sleep(POLL_INTERVAL_SECONDS)
return False, latest
def wait_for_trace(
ctx: HarnessContext, expectations: dict[str, typing.Any], timeout: float = 5.0
) -> tuple[bool, list[dict[str, typing.Any]]]:
deadline = time.monotonic() + timeout
traces: list[dict[str, typing.Any]] = []
while time.monotonic() < deadline:
traces = recent_traces(ctx)
if any(expectation_matches({"trace": trace}, expectations) for trace in traces):
return True, traces
time.sleep(POLL_INTERVAL_SECONDS)
return False, traces
def log_cursor(path: Path) -> int:
try:
return path.stat().st_size
except FileNotFoundError:
return 0
def read_log_tail(ctx: HarnessContext) -> str:
if not ctx.log_path.exists():
return ""
with ctx.log_path.open("rb") as handle:
handle.seek(ctx.log_start)
raw = handle.read(256_000)
return raw.decode("utf-8", errors="replace")
def collect_logs(ctx: HarnessContext) -> dict[str, typing.Any]:
text = read_log_tail(ctx)
out = ctx.run_dir / "logs.txt"
out.write_text(text, encoding="utf-8")
error_count = len(re.findall(r"\b(error|failed|exception|crash)\b", text, flags=re.IGNORECASE))
return {
"path": str(out),
"available": ctx.log_path.exists(),
"bytes": len(text.encode("utf-8")),
"error_count": error_count,
}
def _ancestor_pids() -> set[int]:
"""Return PIDs of this process and all its ancestors (parent chain).
macOS ``pgrep`` has no ``-A`` (exclude-ancestors) flag, so when the harness
is launched from a scripted shell/CI command whose command line contains the
process-match string (e.g. ``--process-match omi-harness-test``), ``pgrep``
returns that ancestor as well as the app. Filtering the ancestor chain
here keeps ``resolve_sample_pid`` from failing the uniqueness check.
"""
pids: set[int] = {os.getpid()}
try:
current = os.getppid()
while current > 1 and current not in pids:
pids.add(current)
# Walk up the parent chain via ps (portable; no /proc on macOS).
result = subprocess.run(
["ps", "-o", "ppid=", "-p", str(current)],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=3,
)
parent = result.stdout.strip()
if not parent.isdigit():
break
current = int(parent)
except (OSError, subprocess.SubprocessError):
pass
return pids
def process_matches(match: str) -> list[dict[str, typing.Any]]:
completed = subprocess.run(
["pgrep", "-fl", match],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=5,
)
skip_pids = _ancestor_pids()
matches: list[dict[str, typing.Any]] = []
for line in completed.stdout.splitlines():
parts = line.strip().split(maxsplit=1)
if not parts or not parts[0].isdigit():
continue
command = parts[1] if len(parts) > 1 else ""
pid = int(parts[0])
# Only skip the harness runner itself and its ancestor shells (whose
# command lines may contain the match string). Do NOT filter by the
# substring "omi-harness" because the target app may be launched as a
# named bundle like "omi-harness-test" that legitimately matches.
if pid in skip_pids:
continue
matches.append({"pid": pid, "command": command})
return matches
def resolve_sample_pid(ctx: HarnessContext, spec: dict[str, typing.Any]) -> tuple[int, str]:
if spec.get("pid") is not None:
return int(spec["pid"]), f"pid:{spec['pid']}"
match = str(spec.get("process_match") or ctx.process_match or "").strip()
if not match:
raise RuntimeError("power.sample requires pid, process_match, or --process-match")
matches = process_matches(match)
if not matches:
raise RuntimeError(f"no process matched {match!r}")
if len(matches) > 1:
commands = ", ".join(f"{item['pid']}:{item['command']}" for item in matches[:5])
raise RuntimeError(f"process_match {match!r} matched {len(matches)} processes: {commands}")
return int(matches[0]["pid"]), match
def read_process_sample(pid: int) -> dict[str, typing.Any] | None:
completed = subprocess.run(
["ps", "-o", "%cpu=", "-o", "rss=", "-p", str(pid)],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=5,
)
if completed.returncode != 0:
return None
parts = completed.stdout.strip().split()
if len(parts) < 2:
return None
return {
"at": now_iso(),
"cpu_percent": float(parts[0]),
"rss_mb": round(float(parts[1]) / 1024, 2),
}
def sample_process_power(
ctx: HarnessContext, spec: dict[str, typing.Any], path: Path
) -> tuple[bool, str | None, dict[str, typing.Any]]:
pid, match = resolve_sample_pid(ctx, spec)
duration_ms = max(250, int(spec.get("duration_ms", 5000)))
interval_ms = max(100, int(spec.get("interval_ms", 250)))
warmup_ms = max(0, int(spec.get("warmup_ms", 0)))
if warmup_ms:
time.sleep(warmup_ms / 1000)
deadline = time.monotonic() + duration_ms / 1000
samples: list[dict[str, typing.Any]] = []
while time.monotonic() < deadline:
sample = read_process_sample(pid)
if sample is None:
raise RuntimeError(f"process {pid} died or became unreadable while sampling")
samples.append(sample)
time.sleep(interval_ms / 1000)
if not samples:
raise RuntimeError(f"no samples collected for pid {pid}")
cpu_values = [float(sample["cpu_percent"]) for sample in samples]
rss_values = [float(sample["rss_mb"]) for sample in samples]
summary = {
"pid": pid,
"process_match": match,
"duration_ms": duration_ms,
"interval_ms": interval_ms,
"sample_count": len(samples),
"avg_cpu_percent": round(statistics.fmean(cpu_values), 2),
"peak_cpu_percent": round(max(cpu_values), 2),
"avg_rss_mb": round(statistics.fmean(rss_values), 2),
"peak_rss_mb": round(max(rss_values), 2),
}
errors: list[str] = []
max_avg_cpu = spec.get("max_avg_cpu_percent")
if max_avg_cpu is not None and summary["avg_cpu_percent"] > float(max_avg_cpu):
errors.append(f"avg_cpu_percent {summary['avg_cpu_percent']} exceeded {max_avg_cpu}")
max_peak_cpu = spec.get("max_peak_cpu_percent")
if max_peak_cpu is not None and summary["peak_cpu_percent"] > float(max_peak_cpu):
errors.append(f"peak_cpu_percent {summary['peak_cpu_percent']} exceeded {max_peak_cpu}")
max_avg_rss = spec.get("max_avg_rss_mb")
if max_avg_rss is not None and summary["avg_rss_mb"] > float(max_avg_rss):
errors.append(f"avg_rss_mb {summary['avg_rss_mb']} exceeded {max_avg_rss}")
write_json(path, {"summary": summary, "samples": samples})
return not errors, "; ".join(errors) if errors else None, summary
def step_prefix(index: int, step: dict[str, typing.Any]) -> str:
return f"{index:03d}-{slug(str(step.get('id') or step.get('name') or 'step'))}"
def run_agent_swift(ctx: HarnessContext, args: list[str]) -> subprocess.CompletedProcess[str]:
if not ctx.bundle_id:
raise RuntimeError("AX steps require --bundle-id")
if not ctx.bundle_id.startswith(NAMED_NON_PRODUCTION_BUNDLE_PREFIX):
raise RuntimeError(
"AX steps require a named non-production bundle id beginning with "
f"{NAMED_NON_PRODUCTION_BUNDLE_PREFIX!r}; refusing {ctx.bundle_id!r}"
)
connect = subprocess.run(
["agent-swift", "connect", "--bundle-id", ctx.bundle_id],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=10,
)
if connect.returncode != 0:
return connect
return subprocess.run(
["agent-swift", *args], check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=20
)
def action_payload(spec: dict[str, typing.Any]) -> dict[str, typing.Any]:
payload = dict(spec)
params = dict(payload.get("params") or {})
for key in list(payload.keys()):
if key not in {"name", "params"}:
params[key] = payload.pop(key)
if params:
payload["params"] = params
return payload
def assert_state(ctx: HarnessContext, spec: dict[str, typing.Any], path: Path) -> tuple[bool, str | None]:
state = state_snapshot(ctx)
write_json(path, state)
expectations = spec.get("equals") or spec
if not isinstance(expectations, dict):
return False, "state.expect requires a mapping"
if expectation_matches({"state": state}, expectations):
return True, None
return False, f"state assertions failed: {expectation_mismatches({'state': state}, expectations)}"
def assert_log(ctx: HarnessContext, spec: dict[str, typing.Any], path: Path) -> tuple[bool, str | None]:
text = read_log_tail(ctx)
path.write_text(text, encoding="utf-8")
contains = spec.get("contains", [])
absent = spec.get("absent", [])
if isinstance(contains, str):
contains = [contains]
if isinstance(absent, str):
absent = [absent]
missing = [item for item in contains if item not in text]
present = [item for item in absent if item in text]
if missing:
return False, f"log missing expected text: {missing}"
if present:
return False, f"log contained forbidden text: {present}"
return True, None
def assert_trace(ctx: HarnessContext, spec: dict[str, typing.Any], path: Path) -> tuple[bool, str | None]:
traces = recent_traces(ctx)
write_json(path, traces)
# A responsiveness assertion must apply to the route just exercised, not
# a faster matching route left over from an earlier loop iteration.
# `latest` is intentionally a harness-only selector, never a trace field.
latest = spec.get("latest", False)
if not isinstance(latest, bool):
return False, "trace.expect latest must be a boolean"
expectations = spec.get("equals") or {key: value for key, value in spec.items() if key != "latest"}
if not isinstance(expectations, dict):
return False, "trace.expect requires a mapping"
if latest:
# State waits commonly issue a follow-up /state request. Select the
# newest trace for the exercised route rather than accidentally
# asserting on that bookkeeping request. Select by route identity only
# (path + method); status and duration remain assertion predicates so a
# failed latest response can never be hidden by an earlier success.
selector = {
key: value
for key, value in expectations.items()
if key in {"trace.path", "trace.method"}
and not isinstance(value, dict)
}
matching = [trace for trace in traces if expectation_matches({"trace": trace}, selector)]
candidates = matching[-1:] if matching else traces[-1:]
else:
candidates = traces
if any(expectation_matches({"trace": trace}, expectations) for trace in candidates):
return True, None
reasons = [expectation_mismatches({"trace": trace}, expectations) for trace in candidates]
return False, f"no trace matched: expectations={expectations}, mismatches={reasons}"
def ax_snapshot_elements(snapshot: typing.Any) -> list[dict[str, typing.Any]]:
"""Return AX element dictionaries in the traversal order reported by agent-swift."""
elements: list[dict[str, typing.Any]] = []
def visit(value: typing.Any) -> None:
if isinstance(value, dict):
if isinstance(value.get("identifier"), str):
elements.append(value)
for child in value.values():
visit(child)
elif isinstance(value, list):
for child in value:
visit(child)
visit(snapshot)
return elements
def ax_accessibility_label(element: dict[str, typing.Any]) -> str | None:
"""Read the label agent-swift exposes for a VoiceOver-visible element."""
for key in ("label", "title", "text", "name"):
value = element.get(key)
if isinstance(value, str) and value:
return value
attributes = element.get("attrs")
if isinstance(attributes, dict):
for key in ("AXLabel", "AXTitle", "AXDescription"):
value = attributes.get(key)
if isinstance(value, str) and value:
return value
return None
def ax_elements_by_identifier(snapshot: typing.Any) -> dict[str, dict[str, typing.Any]]:
"""Index the first element for each stable AX identifier without changing its order."""
elements: dict[str, dict[str, typing.Any]] = {}
for element in ax_snapshot_elements(snapshot):
identifier = element["identifier"]
if identifier not in elements:
elements[identifier] = element
return elements
def is_stable_accessibility_identifier(value: typing.Any) -> bool:
return isinstance(value, str) and bool(STABLE_ACCESSIBILITY_IDENTIFIER.fullmatch(value))
def _string_list(spec: typing.Any, field: str) -> tuple[list[str] | None, str | None]:
if isinstance(spec, str):
spec = [spec]
if not isinstance(spec, list) or not all(is_stable_accessibility_identifier(item) for item in spec):
return None, f"ax.expect {field} requires a stable identifier or list of stable identifiers"
return spec, None
def assert_ax(ctx: HarnessContext, spec: dict[str, typing.Any], path: Path) -> tuple[bool, str | None]:
if ctx.lane != "ui":
return True, "skipped outside ui lane"
# AX snapshots address the app by bundle id and do not need visible pixels,
# activation, or the user's cursor. Keep the window parked in quiet mode.
result = run_agent_swift(ctx, ["snapshot", "-i", "--json"])
path.write_text(result.stdout, encoding="utf-8")
if result.returncode != 0:
return False, "agent-swift snapshot failed"
text = result.stdout
try:
snapshot = json.loads(text)
except json.JSONDecodeError:
return False, "agent-swift snapshot returned invalid JSON"
visible = spec.get("text_visible", [])
if isinstance(visible, str):
visible = [visible]
missing = [item for item in visible if item not in text]
if missing:
return False, f"AX snapshot missing text: {missing}"
identifiers = ax_elements_by_identifier(snapshot)
visible_ids, error = _string_list(spec.get("identifiers_visible", []), "identifiers_visible")
if error:
return False, error
missing_ids = [identifier for identifier in visible_ids or [] if identifier not in identifiers]
if missing_ids:
return False, f"AX snapshot missing stable identifier(s): {missing_ids}"
focus_order, error = _string_list(spec.get("focus_order", []), "focus_order")
if error:
return False, error
if focus_order:
if len(set(focus_order)) != len(focus_order):
return False, "ax.expect focus_order must not repeat an identifier"
actual_order = [str(element["identifier"]) for element in ax_snapshot_elements(snapshot)]
expected_index = 0
for identifier in actual_order:
if identifier == focus_order[expected_index]:
expected_index += 1
if expected_index == len(focus_order):
break
if expected_index != len(focus_order):
missing_or_reordered = focus_order[expected_index:]
return False, (
"AX keyboard focus order did not contain the expected stable-id subsequence: "
f"missing or reordered {missing_or_reordered}; actual={actual_order}"
)
voiceover_labels = spec.get("voiceover_labels", {})
if not isinstance(voiceover_labels, dict) or not all(
is_stable_accessibility_identifier(identifier) and isinstance(label, str) and label
for identifier, label in voiceover_labels.items()
):
return False, "ax.expect voiceover_labels requires a mapping of stable identifiers to non-empty labels"
# AX labels can contain user data. Keep terminal/run-summary failures to
# stable identifiers; the local artifact remains available for intentional
# inspection under the harness's non-production boundary.
label_mismatches = [
identifier
for identifier, expected in voiceover_labels.items()
if identifier not in identifiers or ax_accessibility_label(identifiers[identifier]) != expected
]
if label_mismatches:
return False, f"AX VoiceOver label assertions failed for stable identifier(s): {label_mismatches}"
return True, None
def activate_ax(ctx: HarnessContext, spec: dict[str, typing.Any], path: Path) -> tuple[bool, str | None]:
"""Activate one AX element through its stable accessibility identifier, never coordinates."""
if ctx.lane != "ui":
return True, "skipped outside ui lane"
identifier = spec.get("identifier")
if not is_stable_accessibility_identifier(identifier):
return False, "ax.activate requires a stable identifier"
action = spec.get("action", "press")
if action not in {"click", "press"}:
return False, "ax.activate action must be click or press"
runner = run_agent_swift_in_interactive_mode if action == "click" else run_agent_swift
result = runner(ctx, ["find", "identifier", identifier, action])
path.write_text(result.stdout, encoding="utf-8")
if result.returncode != 0:
return False, f"agent-swift failed to {action} identifier {identifier!r}"
return True, None
def export_visual(ctx: HarnessContext, path: Path, target: str | None = None) -> dict[str, typing.Any]:
payload = {"path": str(path)}
if target:
payload["target"] = target
return request_json(ctx.base_url, "POST", "/visual/export", payload)
def apply_wait(ctx: HarnessContext, step: dict[str, typing.Any], prefix: str, result: dict[str, typing.Any]) -> None:
wait_spec = step.get("wait") or {}
if not result["ok"] or not isinstance(wait_spec, dict) or not wait_spec:
return
timeout = float(step.get("timeout_seconds", 5))
if "trace" in wait_spec:
waited_ok, traces = wait_for_trace(ctx, wait_spec["trace"], timeout)
trace_path = ctx.steps_dir / f"{prefix}-traces.json"
write_json(trace_path, traces)
result["artifacts"]["traces"] = str(trace_path)
if not waited_ok:
result["ok"] = False
mismatches = [expectation_mismatches({"trace": trace}, wait_spec["trace"]) for trace in traces]
result["error"] = f"trace wait timed out after {timeout}s; mismatches={mismatches}"
else:
stability_window_seconds = float(step.get("stability_window_seconds", 0))
waited_ok, state = wait_for_state(ctx, wait_spec, timeout, stability_window_seconds)
state_path = ctx.steps_dir / f"{prefix}-state.json"
write_json(state_path, state)
result["artifacts"]["state"] = str(state_path)
if not waited_ok:
result["ok"] = False
mismatches = expectation_mismatches({"state": state}, wait_spec)
result["error"] = f"state wait timed out after {timeout}s; mismatches={mismatches}"
def execute_step(ctx: HarnessContext, index: int, step: dict[str, typing.Any]) -> dict[str, typing.Any]:
prefix = step_prefix(index, step)
started = time.perf_counter()
result: dict[str, typing.Any] = {
"id": step.get("id", f"S{index}"),
"name": step.get("name", f"Step {index}"),
"started_at": now_iso(),
"ok": True,
"warnings": [],
"artifacts": {},
}
try:
if "bridge.navigate" in step:
payload = dict(step["bridge.navigate"] or {})
payload.setdefault("activateApp", False)
response = request_json(ctx.base_url, "POST", "/navigate", payload)
response_path = ctx.steps_dir / f"{prefix}-navigate.json"
write_json(response_path, response)
result.update(operation="bridge.navigate", response_path=str(response_path), ok=bool(response.get("ok")))
if not result["ok"]:
result["error"] = response.get("error")
elif "bridge.action" in step:
payload = action_payload(dict(step["bridge.action"] or {}))
response = request_json(ctx.base_url, "POST", "/action", payload)
response_path = ctx.steps_dir / f"{prefix}-action.json"
write_json(response_path, response)
result.update(operation="bridge.action", response_path=str(response_path), ok=bool(response.get("ok")))
if not result["ok"]:
result["error"] = response.get("error")
expectations = step.get("expect")
if result["ok"] and expectations is not None:
if not isinstance(expectations, dict):
result.update(ok=False, error="bridge.action expect requires a mapping")
else:
mismatches = expectation_mismatches(response, expectations)
if mismatches:
result.update(ok=False, error=f"bridge.action expectations failed: {mismatches}")
elif "visual.export" in step:
if ctx.lane not in {"visual", "ui"}:
result["operation"] = "visual.export"
result["warnings"].append(f"skipped in {ctx.lane} lane")
else:
options = dict(step["visual.export"] or {})
name = slug(str(options.get("name") or step.get("id") or f"step-{index}"))
png_path = ctx.steps_dir / f"{prefix}-{name}.png"
response = export_visual(ctx, png_path, str(options["target"]) if "target" in options else None)
response_path = ctx.steps_dir / f"{prefix}-visual.json"
write_json(response_path, response)
result.update(operation="visual.export", response_path=str(response_path), ok=bool(response.get("ok")))
result["artifacts"]["screenshot"] = str(png_path)
if not result["ok"]:
result["error"] = response.get("error")
elif "visual.action_sequence" in step:
if ctx.lane not in {"visual", "ui"}:
result["operation"] = "visual.action_sequence"
result["warnings"].append(f"skipped in {ctx.lane} lane")
else:
options = dict(step["visual.action_sequence"] or {})
action_name = str(options.get("action") or "")
if not action_name:
result.update(
operation="visual.action_sequence", ok=False, error="visual.action_sequence requires action"
)
else:
frames = max(1, int(options.get("frames", 8)))
interval_ms = max(1, int(options.get("interval_ms", 16)))
target = str(options.get("target") or "floating")
params = dict(options.get("params") or {})
action_path = ctx.steps_dir / f"{prefix}-action.json"
frame_records = []
with ThreadPoolExecutor(max_workers=1) as executor:
action_future = executor.submit(
request_json,
ctx.base_url,
"POST",
"/action",
{"name": action_name, "params": params},
)
ok = True
for frame_index in range(frames):
frame_path = ctx.steps_dir / f"{prefix}-frame-{frame_index:02d}.png"
captured_at = now_iso()
response = export_visual(ctx, frame_path, target)
frame_records.append(
{
"index": frame_index,
"captured_at": captured_at,
"path": str(frame_path),
"ok": bool(response.get("ok")),
"response": response,
}
)
ok = ok and bool(response.get("ok"))
if frame_index < frames - 1:
time.sleep(interval_ms / 1000)
try:
action_response = action_future.result(timeout=25)
except FutureTimeoutError:
action_response = {"ok": False, "error": "action request timed out after capture"}
write_json(action_path, action_response)
ok = ok and bool(action_response.get("ok"))
manifest_path = ctx.steps_dir / f"{prefix}-sequence.json"