Repository navigation
Expand file tree
/
Copy pathwatcher_linux.py
More file actions
executable file
·1506 lines (1419 loc) · 67.4 KB
/
Copy pathwatcher_linux.py
File metadata and controls
executable file
·1506 lines (1419 loc) · 67.4 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/python3
"""Yes, Dev engine for Linux: watch for Chrome's "Allow remote debugging?"
consent dialog through AT-SPI.
Status: v0.8.14. Detection via AT-SPI is proven live (titled dialogs by
title, untitled Wayland bubbles by window-geometry child totals).
Auto-approval works via a visual pipeline, proven end-to-end on
2026-09-22: xdg-desktop-portal screenshot (no prompt) -> PIL finds the
rightmost blue button = Allow -> click through a dedicated ABSOLUTE
uinput pointer (1:1 with the logical desktop; ydotool's relative device
is accel-warped and misses). The hung CDP client then completes its
handshake. Verification is visual (a fresh screenshot no longer shows
the button); child totals are a second signal only because queued
attaches leak nodes. If no button is visible the engine raises the host
window with one uncovered-corner click (another app covering Chrome
hides the bubble and swallows every click - observed live) and stands
down after 3 empty looks. Gates: --enable-click (off by default),
--observe always wins, burst guard, 3 attempts per cycle with a
--cool-off-s pause (default 30s) and a fresh pointer before the next
cycle, and [ACTION] is logged only after verification. The AT-SPI
Action path (below) still serves stacks that expose the button (X11).
Approval is verified two ways (fresh screenshot or child total), the
normalize ladder runs 3 rounds, stand-downs are reason-coded, and a
state.json feeds tools/doctor.py; the edge-case matrix lives in
TESTING.md. Clicks pause while the session is locked, and an opt-in
--restart-chrome-on-stuck turns the stuck-queue hint into a restart, and
--selftest proves the whole stack without waiting for a real prompt.
Off-workspace bubbles are hunted (up to 3 workspaces, switched back),
and multi-monitor setups are detected and warned about. An idle
visual backstop catches prompts whose child-count bump was missed
(leak aliasing - observed live after a reboot).
Boot-safe: the systemd user service starts before the desktop exists
(linger + default.target), so every dependency is re-acquired lazily and
retried - the absolute pointer is rebuilt every 15s until Mutter/portal
answer (and rebuilt on RDP resolution changes), and AT-SPI is
re-initialised after 20 consecutive scan failures. A reboot recovers
without a manual restart.
Why no plain Invoke like Windows/macOS: on GNOME + Wayland (Chrome 153,
verified Sep 2026, real profile, remote-debugging on port 9222 in
DevToolsActivePort mode) every Chrome frame reports child_count 0 over
AT-SPI and the bubble exposes no objects, so the button is found
visually instead. Run:
/usr/bin/python3 watcher_linux.py --observe # watch only
/usr/bin/python3 watcher_linux.py --observe --enable-click # arm (observe wins)
/usr/bin/python3 watcher_linux.py --enable-click # auto-approve
/usr/bin/python3 watcher_linux.py --once # one sweep
/usr/bin/python3 watcher_linux.py --probe # dump AT-SPI tree
Auto-click deps (all distro packages):
sudo apt install python3-gi gir1.2-atspi-2.0 python3-pil python3-evdev
# dbus-python for the portal screenshot; user must be in the input
# group (or otherwise have rw on /dev/uinput) to create the pointer.
Contract with the tray (identical on all platforms): append a line
containing `[ACTION]` per approval, in the existing format
2026-08-27 16:11:28.644 [ACTION] APPROVED via AtspiAction:press
and the counter / clouds / burst guard work unchanged. `[ACTION]` is only
written after the dialog is verified gone.
Requires the distro python (it ships python3-gi):
sudo apt install python3-gi gir1.2-atspi-2.0
/usr/bin/python3 watcher_linux.py --observe
"""
from __future__ import annotations
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import time
from collections import deque
from datetime import datetime
from pathlib import Path
try:
from platform_linux import (DATA_DIR, LOG_PATH, acquire_single_instance,
atspi_available, ensure_data_dir)
except ImportError:
sys.path.insert(0, str(Path(__file__).resolve().parent))
from platform_linux import (DATA_DIR, LOG_PATH, acquire_single_instance,
atspi_available, ensure_data_dir)
try:
import auto_click
except Exception:
auto_click = None
try:
import gi
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi
except Exception:
sys.exit(
"Yes, Dev Linux engine needs AT-SPI via GObject Introspection:\n"
" sudo apt install python3-gi gir1.2-atspi-2.0\n"
"and run with the distro python: /usr/bin/python3 watcher_linux.py"
)
VERSION = "0.8.14"
DIALOG_PATTERN = re.compile(r"^allow remote debugging\??$", re.I)
APPROVE_PATTERN = re.compile(r"^(allow|approve)$", re.I)
CHROME_NAMES = ("Google Chrome", "Chromium", "Chrome")
EDGE_NAMES = ("Microsoft Edge", "Edge")
TEST_NAMES = ("Google Chrome for Testing",)
POLL_MS_DEFAULT = 250
DEDUPE_SECONDS = 2.0
DEDUPE_MAX = 400
VERIFY_WAIT_S = 0.8
# Minimal burst guard (the tray owns the real one upstream; this keeps an
# unsupervised engine from approving a runaway loop forever).
BURST_LIMIT_DEFAULT = 60 # approvals per window before pausing clicks
BURST_WINDOW_S = 60.0
BURST_PAUSE_S = 60.0
CLICK_MAX_ATTEMPTS = 3 # per cycle, then cool off and start a fresh one
CLICK_RETRY_S = 1.0 # minimum gap between click attempts on one bubble
CLICK_COOL_OFF_S = 30.0 # pause between attempt cycles (0 = wait for clear)
NO_BUTTON_STOP = 3 # consecutive no-button looks before standing down
NORMALIZE_ROUNDS = 3 # focus/maximize rounds per bubble before giving up
LOCK_CACHE_S = 5 # how long a lock-state reading is trusted
WORKSPACE_HUNT_MAX = 3 # off-workspace bubbles: hunt up to N workspaces over
VISUAL_BACKSTOP_S = 5.0 # idle visual glance for bubbles the count missed (0=off)
DROPCHECK_EVERY_S = 3600 # how often to check the Pictures dir for backlog
DROPCHECK_WARN_COUNT = 500 # portal drops above this = cleanup regressed
DROPCHECK_WARN_BYTES = 512 * 1024 * 1024
CHROME_RESTART_COOLDOWN_S = 1800 # opt-in restart: at most once per 30 min
CLICKER_RETRY_S = 15.0 # re-try clicker creation (boot order, RDP resize)
ATSPI_REINIT_AFTER = 20 # consecutive failed scans before re-initialising
_DIALOG_ROLES = {"dialog", "alert", "window", "frame"}
def _safe(fn, default=None):
try:
return fn()
except Exception:
return default
def _role_name(node) -> str:
return _safe(lambda: node.get_role_name() or "", "") or ""
def _node_name(node) -> str:
return _safe(lambda: node.get_name() or "", "") or ""
def _children(node) -> list:
out = []
try:
n = node.get_child_count()
except Exception:
return out
for i in range(n):
try:
ch = node.get_child_at_index(i)
except Exception:
continue
if ch is not None:
out.append(ch)
return out
def _bounds(node):
"""(x, y, w, h) via Component, or None if not exposed."""
try:
comp = node.get_component_iface()
if comp is None:
return None
# extents are in window or screen coords; screen is stable for dedupe.
ext = comp.get_extents(Atspi.CoordType.SCREEN)
return (int(ext.x), int(ext.y), int(ext.width), int(ext.height))
except Exception:
return None
def _is_defunct(node) -> bool | None:
"""True if the node is gone, False if alive, None if no answer."""
try:
# A torn-down node raises or reports a nonsensical role.
node.get_role()
_ = node.get_name()
return False
except Exception:
return True
def _do_action(node, preferred=("press", "click", "activate")) -> str | None:
try:
act = node.get_action_iface()
if act is None:
return None
try:
available = [(i, act.get_action_name(i)) for i in range(act.get_n_actions())]
except Exception:
return None
ordered = [a for want in preferred for a in available if a[1].lower() == want]
ordered += [a for a in available if a not in ordered]
for idx, name in ordered:
try:
if act.do_action(idx):
return f"AtspiAction:{name}"
except Exception:
continue
except Exception:
pass
return None
class Engine:
def __init__(self, observe=False, poll_ms=POLL_MS_DEFAULT,
include_edge=False, log_path=LOG_PATH,
exit_with_parent=False, diagnostics=False,
dialog_pattern=DIALOG_PATTERN, approve_pattern=APPROVE_PATTERN,
burst_limit=BURST_LIMIT_DEFAULT, enable_click=False,
cool_off_s=CLICK_COOL_OFF_S, restart_chrome_on_stuck=False,
workspace_hunt=True, visual_backstop_s=VISUAL_BACKSTOP_S):
self.observe = observe
self.poll_s = max(0.05, poll_ms / 1000.0)
self.include_edge = include_edge
self.log_path = Path(log_path)
self.exit_with_parent = exit_with_parent
self.diagnostics = diagnostics
self.dialog_pattern = dialog_pattern
self.approve_pattern = approve_pattern
self.burst_limit = burst_limit
self.cool_off_s = cool_off_s
self._cycle_next: dict[str, float] = {}
self._nobutton: dict[str, int] = {}
self._raised: set[str] = set()
self._normalized: dict[str, int] = {}
self.enable_click = enable_click and not observe
self._action_times: deque[float] = deque()
self._burst_paused_until = 0.0
self._rects: dict[tuple, tuple] = {}
self._pending: dict[str, int] = {}
self._click_attempts: dict[str, int] = {}
self._click_last: dict[str, float] = {}
self._clicker = None
self._clicker_created = 0.0
self._clicker_retry_at = 0.0
self._clicker_warned = False
self._scan_errors = 0
self._norm_reason = ""
self._standdowns: deque[float] = deque()
self._last_hint = 0.0
self._started = time.time()
self._last_error = None
self._last_action = None
self._last_scan_ms = 0
self._last_state_write = 0.0
self._locked_checked = 0.0
self._locked_state = False
self._last_locked_log = 0.0
self.restart_chrome_on_stuck = restart_chrome_on_stuck
self._last_chrome_restart = 0.0
self.workspace_hunt = workspace_hunt
self._ws_shift: dict[str, int] = {}
self._ws_restore_on_start = 0
self._monitors_warned = False
self.visual_backstop_s = visual_backstop_s
self._last_backstop = 0.0
self._last_observe_backstop_log = 0.0
self._last_dropcheck = 0.0
try:
import json as _json
st = _json.loads((Path(DATA_DIR) / "state.json").read_text())
self._ws_restore_on_start = int(st.get("ws_shift_total", 0) or 0)
except Exception:
pass
self.approved = 0
self._parent_pid = os.getppid()
self._seen: dict[str, float] = {}
self._next_diagnostic_at = 0.0
try:
Atspi.init()
except Exception:
pass
def log(self, message, level="INFO"):
now = datetime.now()
stamp = now.strftime("%Y-%m-%d %H:%M:%S.") + f"{now.microsecond // 1000:03d}"
line = f"{stamp} [{level}] {message}"
if level == "ERROR":
self._last_error = f"{stamp} {message[:160]}"
elif level == "ACTION":
self._last_action = f"{stamp} {message[:160]}"
print(line, flush=True)
try:
ensure_data_dir()
if self.log_path.exists() and self.log_path.stat().st_size > 1_048_576:
self.log_path.replace(self.log_path.with_name(self.log_path.name + ".1"))
with self.log_path.open("a", encoding="utf-8") as fh:
fh.write(line + "\n")
except Exception:
pass
def _desktop(self):
try:
return Atspi.get_desktop(0)
except Exception:
return None
def _is_browser_app(self, node) -> str | None:
name = _node_name(node)
if any(k.lower() in name.lower() for k in CHROME_NAMES) or name in TEST_NAMES:
return "chrome"
if self.include_edge and any(k.lower() in name.lower() for k in EDGE_NAMES):
return "edge"
return None
def _browser_apps(self):
desk = self._desktop()
if desk is None:
return []
out = []
for child in _children(desk):
if _role_name(child) != "application":
continue
kind = self._is_browser_app(child)
if kind:
out.append((kind, child))
return out
def find_dialog_hosts(self, diag=None):
"""Frames/dialogs whose title matches. Checks app children and, where
the server exposes them, one level deeper - but never a deep recursive
walk (perf: the page DOM must stay out of scope)."""
hits = []
seen = set()
for kind, app in self._browser_apps():
app_name = _node_name(app)
frames = _children(app)
if diag is not None:
diag.append(f"{app_name}:{len(frames)}")
for frame in frames:
title = _node_name(frame).strip()
role = _role_name(frame).lower()
if self.dialog_pattern.match(title) and role in _DIALOG_ROLES:
sig = self._host_signature(frame)
if sig not in seen:
seen.add(sig)
hits.append((kind, frame))
continue
# Bubble case: dialog nested one level inside the frame, if
# the server exposes children at all (Wayland today: 0).
for sub in _children(frame)[:20]:
stitle = (_node_name(sub) or _safe(lambda: sub.get_description() or "", "")).strip()
srole = _role_name(sub).lower()
if self.dialog_pattern.match(stitle) and srole in _DIALOG_ROLES:
sig = self._host_signature(sub)
if sig not in seen:
seen.add(sig)
hits.append((kind, sub))
return hits
def find_approve_button(self, host, depth=0):
if depth > 6:
return None
if _role_name(host).lower() in ("push button", "button"):
label = (_node_name(host) or "").strip()
if self.approve_pattern.match(label):
return host
for child in _children(host):
found = self.find_approve_button(child, depth + 1)
if found is not None:
return found
return None
def _button_labels(self, host, depth=0, out=None):
if out is None:
out = []
if depth > 6:
return out
if _role_name(host).lower() in ("push button", "button"):
out.append(_node_name(host))
for child in _children(host):
self._button_labels(child, depth + 1, out)
return out
def _host_signature(self, host) -> str:
b = _bounds(host)
if b is not None:
return f"rect:{b[0]},{b[1]},{b[2]}x{b[3]}"
title = _node_name(host).strip()
if title:
return f"title:{title.lower()}"
return f"obj:{id(host)}"
_dedupe_key = _host_signature
def _dialog_still_up(self, host, button) -> bool:
"""Identity, not geometry: a queued successor renders at the same
coordinates, so geometry alone would call a dismissed dialog alive."""
if _is_defunct(host) or _is_defunct(button):
return False
b = _bounds(host)
if b is not None and (b[2] <= 0 or b[3] <= 0):
return False
return True
def _approve(self, host, button) -> str | None:
how = _do_action(button)
if how is None:
return None
time.sleep(VERIFY_WAIT_S)
if not self._dialog_still_up(host, button):
return how
# One retry before giving up; never synthesize pointer input here.
how2 = _do_action(button)
time.sleep(VERIFY_WAIT_S)
if how2 and not self._dialog_still_up(host, button):
return how2 + "+retry"
return None
def _burst_ok(self, now: float) -> bool:
"""Minimal burst guard. True when clicking is allowed right now."""
if self.burst_limit <= 0:
return True
if now < self._burst_paused_until:
return False
cutoff = now - BURST_WINDOW_S
while self._action_times and self._action_times[0] < cutoff:
self._action_times.popleft()
if len(self._action_times) >= self.burst_limit:
self._burst_paused_until = now + BURST_PAUSE_S
self.log(f"burst guard: {len(self._action_times)} approvals in "
f"{int(BURST_WINDOW_S)}s >= limit {self.burst_limit} - "
f"pausing clicks for {int(BURST_PAUSE_S)}s", "ERROR")
return False
return True
def _note_action(self) -> None:
self._action_times.append(time.time())
def _get_clicker(self, size=None):
"""Absolute-pointer device, created on demand and retried until the
desktop is ready (boot order: the service starts before the a11y
bus, portal and Mutter exist, so a one-shot create would boot dead).
Passing `size` (from a fresh screenshot) also rebuilds on RDP
resolution changes."""
if self._clicker is not None and (size is None or size == self._clicker.size):
return self._clicker
now = time.time()
if self._clicker is None and now < self._clicker_retry_at:
return None
if auto_click is None:
if not self._clicker_warned:
self.log("auto-click unavailable: auto_click module missing", "ERROR")
self._clicker_warned = True
self._clicker_retry_at = now + CLICKER_RETRY_S
return None
try:
clicker = auto_click.AbsoluteClicker(size)
if not clicker.available(size):
raise RuntimeError("evdev/uinput/screen-size not ready")
except Exception as exc:
if not self._clicker_warned:
self.log(f"auto-click not ready yet ({exc}); retrying every "
f"{int(CLICKER_RETRY_S)}s - normal in the first seconds "
f"after boot", "WARN")
self._clicker_warned = True
self._clicker_retry_at = now + CLICKER_RETRY_S
return None
self._clicker = clicker
self._clicker_created = time.time()
self._clicker_warned = False
self._clicker_retry_at = 0.0
self.log(f"auto-click ready: absolute pointer {clicker.size}", "INFO")
return clicker
def _drop_clicker(self, why: str) -> None:
"""Release the pointer device so the next cycle builds a fresh one.
Empirically a long-lived device stops delivering clicks in some
session states (seat/RDP churn) while a freshly created identical
device delivers immediately - observed live 2026-09-22: the
engine's device failed every click for minutes while a fresh CLI
click approved the same bubble first try. Recreating is cheap.
"""
if self._clicker is None:
return
try:
self._clicker.close()
except Exception:
pass
self._clicker = None
self.log(f" released the pointer device ({why}) - the next cycle "
f"creates a fresh one", "WARN")
def _host_raise_point(self, bounds) -> tuple[int, int] | None:
"""A point inside the host window not covered by another app.
Other apps' top-level windows are read from AT-SPI (Chrome's own
windows are ignored - clicking another Chrome window still raises
Chrome). Preference order is window edges and bottom corners,
away from tabs, toolbars and window controls.
"""
x, y, w, h = bounds
if w < 120 or h < 120:
return None
covered = []
try:
desk = Atspi.get_desktop(0)
except Exception:
return None
for app in _children(desk):
try:
aname = (app.get_name() or '').lower()
except Exception:
continue
if 'chrome' in aname or 'chromium' in aname or 'edge' in aname:
continue
for fr in _children(app):
b = _bounds(fr)
if b and b[2] >= 200 and b[3] >= 200:
covered.append(b)
cands = [
(x + w - 14, y + h - 14), # bottom-right
(x + w // 2, y + h - 14), # bottom-centre
(x + 20, y + h - 14), # bottom-left
(x + w - 14, y + h // 2), # mid-right
(x + w - 14, y + 60), # upper-right, below the tab strip
]
for px, py in cands:
if all(not (cx <= px < cx + cw and cy <= py < cy + ch)
for cx, cy, cw, ch in covered):
return (px, py)
return None
def _raise_host(self, dkey: str) -> bool:
"""Click an uncovered point of the bubble's host window to raise it.
On Wayland clicks only reach the active window: when another app
covers the host, the bubble is invisible to screenshots and every
click vanishes into the covering window (observed live 2026-09-22:
a second app's window hid the bubble for an hour; a corner click
raised Chrome and the bubble reappeared). Once per bubble.
"""
if dkey in self._raised:
return False
self._raised.add(dkey)
try:
_, _, rect = dkey.split(":", 2)
xs, ys, wh = rect.split(",")
w, h = wh.split("x")
bounds = (int(xs), int(ys), int(w), int(h))
except Exception:
return False
pt = self._host_raise_point(bounds)
if pt is None:
return False
if auto_click is None:
return False
with auto_click.screenshot() as shot:
size = auto_click.image_size(shot) if shot else None
clicker = self._get_clicker(size)
if clicker is None or not clicker.click(*pt):
return False
self.log(f" clicked an uncovered corner {pt} to raise the host window "
f"(it was covered or inactive)", "AUDIT")
return True
def _active_chrome_frame(self):
"""(kind, (x, y, w, h)) of an ACTIVE Chrome frame, or None.
Keyboard window-management (Super+Up, Alt+Tab) acts on whatever
has focus, so it is only ever sent after this check proves a
Chrome window is active - never blind.
"""
if Atspi is None:
return None
for kind, app in self._browser_apps():
for fr in _children(app):
try:
if not fr.get_state_set().contains(Atspi.StateType.ACTIVE):
continue
b = _bounds(fr)
if b and b[2] > 200:
return kind, b
except Exception:
continue
return None
def _normalize_host(self, dkey: str):
"""Bring Chrome to the front and maximize it, so the bubble has a
deterministic position. Returns (kind, rect) of the active Chrome
frame after maximizing, or None if Chrome could not be focused.
Focus path: uncovered-corner click (partial cover), then Alt+Tab
probing (fully covered); a second normalize attempt also cycles
same-app windows with Super+` (bubble may be on another Chrome
window). Maximize is GNOME's Super+Up, idempotent when already
maximized. Focus is verified before any key is sent.
"""
if auto_click is None:
return None
n = self._normalized.get(dkey, 0)
if n >= NORMALIZE_ROUNDS:
return None
self._normalized[dkey] = n + 1
self._norm_reason = "no-visible-button"
act = self._active_chrome_frame()
if act is None:
self._raise_host(dkey) # corner click, if any is uncovered
time.sleep(0.4)
act = self._active_chrome_frame()
clicker = self._get_clicker(None)
if clicker is None:
self._norm_reason = "pointer-unavailable"
return None
k = 1
while act is None and k <= 3:
auto_click.alttab_held(clicker, k) # walk k windows down the MRU
time.sleep(0.5)
act = self._active_chrome_frame()
k += 1
if act is None:
self._norm_reason = "no-focus"
self.log(" could not bring Chrome to the front (no focus within "
"reach); leaving this bubble alone", "WARN")
return None
if n >= 1:
# Cycle same-app windows until the ACTIVE one is the bubble's
# host (its rect matches the tracked dkey) - maximizing the
# wrong Chrome window was a real failure mode. Bounded.
try:
_, _, want = dkey.split(":", 2)
except Exception:
want = None
for _ in range(4):
act = self._active_chrome_frame() or act
have = None
try:
b0 = act[1]
have = f"{b0[0]},{b0[1]},{b0[2]}x{b0[3]}"
except Exception:
pass
if want is None or have == want:
break
auto_click.combo(clicker, "nextwindow")
time.sleep(0.5)
kind, b = act
# Ubuntu-style GNOME maps Super+Up to TOGGLE maximize: only send it
# when the window clearly does not already fill the screen, or we
# would restore it to windowed mid-bubble.
sw, sh = auto_click._screen_size() or (0, 0)
fills = sw and b[2] >= sw - 64 and b[3] >= sh - 24
if fills:
self.log(" Chrome already fills the screen - maximize not needed", "AUDIT")
elif not auto_click.combo(clicker, "maximize"):
return None
else:
before_area = b[2] * b[3]
time.sleep(0.5)
act2 = self._active_chrome_frame() or act
if act2[1][2] * act2[1][3] < before_area:
# Toggle was mid-state (e.g. half-tiled): first press
# restored it. One more press maximizes from windowed.
auto_click.combo(clicker, "maximize")
time.sleep(0.5)
act2 = self._active_chrome_frame() or act2
self.log(" Chrome focused - maximized; the bubble now has a "
"deterministic position", "AUDIT")
time.sleep(0.4)
act = self._active_chrome_frame() or act
kind, b = act
rect = f"{b[0]},{b[1]},{b[2]}x{b[3]}"
return kind, rect
def _migrate(self, old: str, new: str) -> None:
"""Move bubble tracking to a new geometry key (window moved or was
maximized while the bubble was pending)."""
if old == new or old not in self._pending:
return
self._pending[new] = self._pending.pop(old)
self._click_attempts[new] = 0
self._click_last.pop(old, None)
self._cycle_next.pop(old, None)
self._nobutton.pop(old, None)
self._raised.discard(old)
self._normalized[new] = self._normalized.pop(old, 0)
shift = self._ws_shift.pop(old, 0)
if shift:
self._ws_shift[new] = shift
def _ws_cleanup(self, dkey: str) -> None:
self._pending.pop(dkey, None)
self._click_attempts.pop(dkey, None)
self._cycle_next.pop(dkey, None)
self._click_last.pop(dkey, None)
self._nobutton.pop(dkey, None)
self._raised.discard(dkey)
self._normalized.pop(dkey, None)
def _ws_restore(self, dkey: str) -> None:
"""Switch back any workspaces the hunt advanced for this bubble."""
shift = self._ws_shift.pop(dkey, 0)
if not shift:
return
clicker = self._get_clicker(None)
if clicker is None:
self.log(f" WARNING: could not restore {shift} workspace switch(es) - "
f"press Super+PageUp {shift} time(s) yourself", "ERROR")
return
for _ in range(shift):
auto_click.combo(clicker, "wsup")
time.sleep(0.4)
self.log(f" restored the workspace ({shift} switch(es) back)", "INFO")
def _portal_dropcheck(self, now: float) -> None:
"""Hourly guard against portal-drop backlog in ~/Pictures.
The engine deletes every portal drop right after capture; an
accumulation here (150k files / 53GB filled the root disk live in
Sep 2026) means the cleanup regressed. Never auto-deletes (the
dir also holds the user's own screenshots) - just warns with the
exact cleanup command."""
if now - self._last_dropcheck < DROPCHECK_EVERY_S:
return
self._last_dropcheck = now
if auto_click is None:
return
try:
n, total = auto_click.portal_drop_backlog()
except Exception:
return
if n >= DROPCHECK_WARN_COUNT or total >= DROPCHECK_WARN_BYTES:
mb = total / (1024 * 1024)
self.log(f"portal drops accumulating in Pictures: {n} files "
f"({mb:.0f} MB) - cleanup may have regressed; inspect then "
f"run: find ~/Pictures -maxdepth 1 -name 'Screenshot*.png' "
f"-mtime +1 -delete", "WARN")
def _backstop_scan(self, now: float) -> None:
"""Idle visual glance: the child-count bump can be missed when a
leaked node clears at the same time a new attach bumps (the count
returns to the value we already recorded - observed live after a
reboot). While armed and idle, look at the screen; if the Allow
button sits in the dialog region twice in a row at the same spot,
treat it as a candidate and let the normal click flow handle it."""
if auto_click is None:
return
with auto_click.screenshot() as shot:
if not shot:
return
size = auto_click.image_size(shot)
pt = auto_click.find_allow_button(shot)
if pt is None or size is None:
return
# position prior: the consent bubble sits centre-screen, not in
# page flow; this keeps a stray page button pair from firing.
if not (0.15 * size[0] <= pt[0] <= 0.85 * size[0]
and 0.20 * size[1] <= pt[1] <= 0.80 * size[1]):
return
time.sleep(0.8)
with auto_click.screenshot() as shot2:
pt2 = auto_click.find_allow_button(shot2) if shot2 else None
if pt2 is None or abs(pt2[0] - pt[0]) > 10 or abs(pt2[1] - pt[1]) > 10:
return
if self.observe:
if now - self._last_observe_backstop_log > 60:
self._last_observe_backstop_log = now
self.log(f"visual backstop (observe): Allow button on screen at "
f"{pt2} - would click", "OBSERVE")
return
dkey = "bubble:chrome:visual"
if dkey in self._pending:
return
self._pending[dkey] = 0 # totals unknown; visual verify decides
self._click_attempts[dkey] = 0
self.log(f"visual backstop: Allow button on screen at {pt2} with no child "
f"bump (detection missed) - approving", "WARN")
def _locked(self) -> bool:
"""True while the session is locked (org.gnome.ScreenSaver), cached
~5s. Clicks are pointless and risky on a lock screen, so the serve
loop waits instead of burning attempts."""
now = time.time()
if now - self._locked_checked < LOCK_CACHE_S:
return self._locked_state
self._locked_checked = now
try:
out = subprocess.run(
["gdbus", "call", "--session", "--dest", "org.gnome.ScreenSaver",
"--object-path", "/org/gnome/ScreenSaver",
"--method", "org.gnome.ScreenSaver.GetActive"],
capture_output=True, text=True, timeout=5).stdout
self._locked_state = "true" in out
except Exception:
self._locked_state = False
return self._locked_state
def _port_open(self) -> bool:
try:
return ":9222" in subprocess.run(
["ss", "-tlnp"], capture_output=True, text=True,
timeout=10).stdout
except Exception:
return False
def _restart_chrome(self, now: float) -> None:
"""Opt-in (--restart-chrome-on-stuck): restart the Chrome that
holds the debug port so a stuck consent queue clears. Tabs
restore; rate-limited to once per 30 minutes."""
if now - self._last_chrome_restart < CHROME_RESTART_COOLDOWN_S:
self.log(" chrome restart already attempted recently - not repeating", "WARN")
return
self._last_chrome_restart = now
try:
ss = subprocess.run(["ss", "-tlnp"], capture_output=True, text=True,
timeout=10).stdout
pid = None
for line in ss.splitlines():
if ":9222" in line and "pid=" in line:
m = re.search(r"pid=(\d+)", line)
if m:
pid = int(m.group(1))
break
if pid is None:
self.log(" chrome restart skipped: no process holds :9222", "WARN")
return
self.log(f" restart-chrome-on-stuck: terminating Chrome pid {pid} "
f"(tabs will restore)", "WARN")
subprocess.run(["kill", "-TERM", str(pid)], timeout=10)
for _ in range(30):
time.sleep(1)
if not self._port_open():
break
uid = os.getuid()
wl = "wayland-0"
try:
cands = sorted(p.name for p in Path(f"/run/user/{uid}").glob("wayland-*")
if not p.name.endswith(".lock"))
if cands:
wl = cands[0]
except Exception:
pass
binary = (shutil.which("google-chrome") or shutil.which("google-chrome-stable")
or "/usr/bin/google-chrome")
subprocess.Popen(
["setsid", "-f", "env",
f"XDG_RUNTIME_DIR=/run/user/{uid}", f"WAYLAND_DISPLAY={wl}",
f"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/{uid}/bus",
binary, "--ozone-platform=wayland", "--restore-last-session"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
for _ in range(30):
time.sleep(1)
if self._port_open():
self.log(" chrome restart: debug port is back; the next attach "
"will prompt", "INFO")
return
self.log(" chrome restart: debug port did not return within 30s", "ERROR")
except Exception as exc:
self.log(f" chrome restart failed: {exc!r}", "ERROR")
def _stuck_hint_due(self, now: float) -> bool:
"""True at most once per 10 min, and only when stand-downs are
clustering - the known stuck-consent-queue signature."""
self._standdowns.append(now)
while self._standdowns and now - self._standdowns[0] > 600:
self._standdowns.popleft()
if len(self._standdowns) >= 3 and now - self._last_hint > 600:
self._last_hint = now
return True
return False
def _write_state(self, now: float) -> None:
"""Machine-readable health snapshot for tools/doctor.py (throttled
to ~5s, atomic replace)."""
if now - self._last_state_write < 5.0:
return
self._last_state_write = now
try:
clicker = None
if self._clicker is not None:
clicker = {"size": list(self._clicker.size),
"age_s": int(now - self._clicker_created)}
state = {
"pid": os.getpid(),
"started": datetime.fromtimestamp(self._started).isoformat(timespec="seconds"),
"updated": datetime.now().isoformat(timespec="seconds"),
"observe": self.observe,
"enable_click": self.enable_click,
"approved_session": self.approved,
"pointer": clicker,
"pending": [{"key": k,
"attempts": self._click_attempts.get(k, 0),
"normalize_rounds": self._normalized.get(k, 0),
"no_button_looks": self._nobutton.get(k, 0),
"ws_shift": self._ws_shift.get(k, 0)}
for k in self._pending],
"workspace_hunt": self.workspace_hunt,
"ws_shift_total": sum(self._ws_shift.values()),
"last_scan_ms": self._last_scan_ms,
"last_error": self._last_error,
"last_action": self._last_action,
}
path = Path(DATA_DIR) / "state.json"
tmp = path.with_name(path.name + ".tmp")
tmp.write_text(json.dumps(state, indent=1) + "\n", encoding="utf-8")
tmp.replace(path)
except Exception:
pass
def _note_scan(self, ok: bool) -> None:
"""Re-init AT-SPI after a run of failures: at boot the a11y bus can
start after us, and a connection made too early never heals."""
if ok:
self._scan_errors = 0
return
self._scan_errors += 1
if self._scan_errors == ATSPI_REINIT_AFTER:
self.log(f"{self._scan_errors} consecutive scan failures - re-initialising "
f"AT-SPI", "WARN")
try:
Atspi.init()
except Exception as exc:
self.log(f"AT-SPI re-init failed: {exc!r}", "ERROR")
self._scan_errors = 0
def _approve_visual(self, dkey: str, baseline: int) -> tuple[str, str | None]:
"""Screenshot -> find Allow -> click it -> verify visually.
Returns (status, detail):
- ("approved", how) the click was emitted and a fresh screenshot
no longer shows the button;
- ("no-button", None) the button is not in the screenshot at all
(bubble already gone, or not renderable);
- ("failed", None) the button is still visible after the click.
Child totals are a second signal only. Queued/orphaned attaches
leak nodes: a bubble that is gone can leave the window's child
total elevated indefinitely (observed live 2026-09-22), so the
totals alone cannot decide whether a click landed.
"""
if auto_click is None:
return "failed", None
with auto_click.screenshot() as shot:
if not shot:
self.log(" screenshot failed (xdg-desktop-portal?)", "WARN")
return "failed", None
size = auto_click.image_size(shot)
clicker = self._get_clicker(size)
if clicker is None:
return "failed", None
pt = auto_click.find_allow_button(shot)
if pt is None and self._raise_host(dkey):
time.sleep(VERIFY_WAIT_S)
with auto_click.screenshot() as shot2:
pt = auto_click.find_allow_button(shot2) if shot2 else None
if pt is None:
return "no-button", None
where = f"{pt} on {size[0]}x{size[1]}" if size else str(pt)
self.log(f" visual approve: clicking Allow at {where}", "AUDIT")
if not clicker.click(*pt):
self.log(" click emit failed", "WARN")
return "failed", None
time.sleep(VERIFY_WAIT_S)
try:
with auto_click.screenshot() as after:
vis_gone = (after is not None
and auto_click.find_allow_button(after) is None)
except Exception:
vis_gone = False
tot_gone = False
if baseline: # base 0 = visual backstop bubble, totals unknown
try:
key = tuple(dkey.split(":", 2)[1:]) # "bubble:<app>:<rect>"
e = self._rect_snapshot().get((key[0], key[1]))
tot_gone = e is None or e["total"] <= baseline
except Exception:
pass
if vis_gone or tot_gone:
# Either signal is enough: screenshots can lag a frame, and
# queued attaches can keep the child total elevated after the
# bubble is gone (both observed live). Disagreements are noted
# instead of stalling the loop.
if vis_gone and not tot_gone:
self.log(" (child total still elevated - queued attaches leak "
"nodes; visual verify says gone)", "INFO")
if tot_gone and not vis_gone:
self.log(" (screenshot still shows the button but the child "
"total dropped - screenshot lag; accepting)", "INFO")
return "approved", f"abs-pointer click {pt}"
return "failed", None