forked from arcahyadi/odysseus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac_wrapper.py
More file actions
1759 lines (1580 loc) · 83.2 KB
/
Copy pathmac_wrapper.py
File metadata and controls
1759 lines (1580 loc) · 83.2 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
import json
import os
import re as _re
import sys
import time as _time
# ==============================================================================
# CRITICAL: Logging setup must happen BEFORE any PyQt6/QtWebEngine imports.
#
# sys.stdout/stderr alone is not enough; Chromium renderer subprocesses inherit
# OS-level file descriptors (fd 1, fd 2), not Python's sys.stdout/stderr.
# os.dup2 replaces the OS fds so all child process output lands in our log.
# ==============================================================================
LOG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
os.makedirs(LOG_DIR, exist_ok=True)
# Rotate logs at startup rather than mid-run: once os.dup2 binds the Chromium
# renderer's inherited fds to the log inode, the file cannot be swapped while
# the process lives. Renaming before the open+dup2 avoids that constraint.
# Constants match src/constants.py (LOG_MAX_BYTES, LOG_BACKUP_COUNT) so all
# three log files follow the same retention policy.
_LOG_MAX_BYTES = 10 * 1024 * 1024 # 10 MB, matches app RotatingFileHandler
_LOG_BACKUP_COUNT = 5 # matches app LOG_BACKUP_COUNT
def _rotate_log(path: str) -> None:
"""Shift existing backups and rename path → path.1 if over _LOG_MAX_BYTES.
Mirrors the behaviour of logging.handlers.RotatingFileHandler with
backupCount=_LOG_BACKUP_COUNT. Silent on any error.
"""
try:
if os.path.getsize(path) <= _LOG_MAX_BYTES:
return
# Shift: path.4 → path.5, path.3 → path.4, ..., path.1 → path.2
for n in range(_LOG_BACKUP_COUNT, 1, -1):
src = f'{path}.{n - 1}'
dst = f'{path}.{n}'
if os.path.exists(src):
if os.path.exists(dst):
os.remove(dst)
os.rename(src, dst)
# Move current log to path.1
backup = path + '.1'
if os.path.exists(backup):
os.remove(backup)
os.rename(path, backup)
except OSError:
pass
_rotate_log(os.path.join(LOG_DIR, "wrapper_system.log"))
_log_file = open(os.path.join(LOG_DIR, "wrapper_system.log"), "a", buffering=1)
sys.stdout.flush()
sys.stderr.flush()
os.dup2(_log_file.fileno(), 1) # redirect fd 1: Chromium renderer stdout → our log
os.dup2(_log_file.fileno(), 2) # redirect fd 2: Chromium renderer stderr → our log
sys.stdout = _log_file
sys.stderr = _log_file
print(f'[LOG] wrapper_system.log opened at {_time.strftime("%Y-%m-%dT%H:%M:%S")}',
flush=True)
# macOS: Qt WebEngine rides Metal (via ANGLE) on Qt 6.5+, on both arm64 and
# x86_64. None of the Linux wrapper's GBM/zero-copy/NVIDIA branching applies
# here, but GPU-less machines (VMs — macOS guests get no paravirtual GPU) DO
# need detection: forcing GPU rasterization onto the SwiftShader fallback
# churns a CPU-emulated GPU per frame and shows up as flicker/lag and glitched
# repaints (observed live on the Tahoe bench). Every real Mac GPU (Intel, AMD,
# Apple silicon) registers an IOAccelerator subclass in the IO registry, so an
# empty `ioreg -rc IOAccelerator` (~15 ms, once at startup) is the reliable
# no-Metal-device signal. Fail-safe: any probe error reads as "has GPU" so a
# glitch never degrades a real machine.
import subprocess
import tempfile
def _macos_software_render() -> bool:
"""True when no Metal-capable accelerator exists (SwiftShader fallback)."""
try:
r = subprocess.run(["ioreg", "-rc", "IOAccelerator"],
capture_output=True, timeout=5)
return not r.stdout.strip()
except Exception:
return False
_software_render = _macos_software_render()
_gpu_flags = [] if _software_render else [
"--ignore-gpu-blocklist",
"--enable-gpu-rasterization",
]
# WebGPU is pointless on SwiftShader and adds feature surface; keep it only
# where a real GPU backs it.
_features = "SharedArrayBuffer,PartitionAllocMemoryReclaimer,BlinkHeapCompaction"
if not _software_render:
_features = "WebGPU," + _features
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = " ".join([
"--no-sandbox",
*_gpu_flags,
f"--enable-features={_features}",
"--enable-logging=stderr --log-level=1", # captured via os.dup2 into wrapper_system.log
"--remote-debugging-port=9222", # Chrome DevTools at http://localhost:9222
# --minor-ms, not --minor-mc: V8 renamed the minor-GC flag (MinorMC → MinorMS)
# in the V8 shipped with Qt 6.11's Chromium; the old name logs "Error:
# unrecognized flag" at renderer start (non-fatal, but the flag is dropped).
"--js-flags=--expose-gc,--initial-old-space-size=128,--max-old-space-size=512,--optimize-for-size,--minor-ms",
"--renderer-process-limit=1",
"--disable-extensions",
# NB: low-end-device-mode (the Chromium flag) is deliberately NOT set. It
# caused a lighter-rectangle raster tint on dark themes and did not bound the
# actual OOM — Oilpan detached-DOM churn, a separate pool from the raster
# tile budget. See jdmanring/odysseus#96.
])
import concurrent.futures as _futures
import ctypes
import signal
import socket as _cdp_sock
import struct as _cdp_struct
import base64 as _cdp_b64
import urllib.request as _cdp_req
import threading as _threading
import time
from PyQt6.QtWidgets import (QApplication, QMainWindow, QColorDialog,
QDialog, QLabel, QVBoxLayout, QHBoxLayout, QPushButton,
QMessageBox)
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import (
QWebEngineProfile, QWebEnginePage, QWebEngineScript, QWebEngineSettings,
)
from PyQt6.QtWebChannel import QWebChannel
from PyQt6.QtCore import Qt, QUrl, QObject, QFile, QIODevice, QTimer, QThread, QSettings, QEvent, pyqtSlot, pyqtSignal, QSocketNotifier
from PyQt6.QtGui import QDesktopServices, QColor, QIcon, QAction, QKeySequence, QPixmap
from PyQt6.QtNetwork import QLocalServer, QLocalSocket
INSTALL_DIR = os.path.dirname(os.path.abspath(__file__))
VENV_PYTHON = os.path.join(INSTALL_DIR, "venv", "bin", "python")
PORT = os.environ.get("APP_PORT", "7000")
WINDOW_TITLE = "Odysseus"
PROFILE_NAME = "odysseus"
def _theme_bg_color() -> QColor:
"""Read the saved theme background from data/user_prefs.json.
setBackgroundColor() sets the QWebEnginePage compositor base-background
colour, what shows in any brief gap before content paints. Hardcoding
#282c34 (the default theme) would flash lighter than the actual background
on a custom dark theme (e.g. Catppuccin #1e1e2e); reading the persisted bg
at startup keeps the base colour in sync with the user's real theme.
"""
try:
prefs_path = os.path.join(INSTALL_DIR, 'data', 'user_prefs.json')
with open(prefs_path, encoding='utf-8') as f:
prefs = json.load(f)
for user_data in prefs.get('_users', {}).values():
bg = user_data.get('theme', {}).get('colors', {}).get('bg', '')
if bg and bg.startswith('#') and len(bg) == 7:
r, g, b = int(bg[1:3], 16), int(bg[3:5], 16), int(bg[5:7], 16)
return QColor(r, g, b)
except Exception:
pass
return QColor(0x28, 0x2c, 0x34) # default Odysseus dark theme
# macOS conventions: Application Support for data, Caches for cache.
DATA_DIR = os.path.expanduser("~/Library/Application Support/odysseus/webengine")
CACHE_DIR = os.path.expanduser("~/Library/Caches/odysseus/webengine")
_UVICORN_PATTERN = "uvicorn app:app"
_server_proc = None
# Bind host the server is currently launched on. 127.0.0.1 = local only;
# 0.0.0.0 = reachable from the LAN (the tray "Expose to Network" toggle).
_bind_host = "127.0.0.1"
def _desired_host():
"""Bind host implied by the persisted Expose-to-Network preference."""
try:
return "0.0.0.0" if QSettings("odysseus", "odysseus").value(
"exposeToNetwork", False, type=bool) else "127.0.0.1"
except Exception:
return "127.0.0.1"
def _reachable_host():
"""Address a client should actually connect to. When bound to all
interfaces, 0.0.0.0 is not connectable — resolve the primary LAN IP so
'Open in Browser' / 'Copy Server URL' / the status line are useful."""
if _bind_host != "0.0.0.0":
return "localhost"
try:
s = _cdp_sock.socket(_cdp_sock.AF_INET, _cdp_sock.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # no packets sent; just picks the egress iface
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "localhost"
def _server_url():
return f"http://{_reachable_host()}:{PORT}"
def _server_running():
return _server_proc is not None and _server_proc.poll() is None
def kill_zombies():
result = subprocess.run(["pkill", "-f", _UVICORN_PATTERN], check=False)
if result.returncode == 0:
print("Killed stale uvicorn process(es), waiting for port to release...")
time.sleep(1)
def start_server():
global _server_proc, _bind_host
_bind_host = _desired_host()
print(f"Starting Odysseus server on {_bind_host}:{PORT}...")
# --no-access-log: uvicorn's access log defaults to ON, emitting one log line
# per HTTP request. For this embedded, localhost, single-user deployment the
# always-on UI polls (email/tasks/calendar) would churn that log forever with
# no operator reading it; errors still surface via server.log. Startup banners
# and tracebacks still reach server_access.log via the subprocess stdout/stderr.
cmd = [VENV_PYTHON, "-m", "uvicorn", "app:app",
"--host", _bind_host, "--port", PORT, "--no-access-log"]
env = os.environ.copy()
env["ODYSSEUS_LOG_FILE"] = os.path.join(LOG_DIR, "server.log")
_rotate_log(os.path.join(LOG_DIR, "server_access.log"))
_access_log = open(os.path.join(LOG_DIR, "server_access.log"), "a", buffering=1)
_server_proc = subprocess.Popen(
cmd,
cwd=INSTALL_DIR,
env=env,
stdout=_access_log,
stderr=_access_log,
start_new_session=True,
)
for _ in range(30):
try:
import urllib.request
urllib.request.urlopen(f"http://localhost:{PORT}", timeout=1)
print("Server ready.")
return True
except Exception:
time.sleep(0.5)
print("Server slow to start, proceeding anyway.")
return False
def stop_server():
global _server_proc
print("Stopping server...")
if _server_proc is not None:
try:
_server_proc.terminate()
_server_proc.wait(timeout=5)
except Exception:
try:
_server_proc.kill()
except Exception:
pass
_server_proc = None
_cdp_executor.shutdown(wait=False, cancel_futures=True)
subprocess.run(["pkill", "-f", _UVICORN_PATTERN], check=False)
print("Server stopped.")
def restart_server():
"""Stop just the uvicorn process and start it again (re-reading the bind
host). Unlike stop_server() this leaves the CDP executor intact — a restart
must not permanently tear down debugging infrastructure the wrapper reuses."""
global _server_proc
if _server_proc is not None:
try:
_server_proc.terminate()
_server_proc.wait(timeout=5)
except Exception:
try:
_server_proc.kill()
except Exception:
pass
_server_proc = None
subprocess.run(["pkill", "-f", _UVICORN_PATTERN], check=False)
return start_server()
class _ServerRestartThread(QThread):
"""start_server() blocks up to ~15s polling for readiness; running it on the
GUI thread would freeze the window. Do it off-thread and signal back."""
finished_ok = pyqtSignal(bool)
def run(self):
try:
ok = restart_server()
except Exception:
ok = False
self.finished_ok.emit(bool(ok))
def _signal_handler(sig, frame):
# SIGTERM/SIGINT (e.g. a relaunch killing the old instance). Stop the server,
# then HARD-EXIT rather than sys.exit: letting the interpreter unwind runs
# Qt's QWebEngineView teardown, which intermittently null-derefs in
# setVisible on CrBrowserMain (Qt 6.11) and logs a spurious crash. Nothing
# meaningful is lost — the persistent profile flushes as it changes.
stop_server()
sys.stdout.flush()
sys.stderr.flush()
os._exit(0)
def _cdp_ws_call(ws_url, method, params=None):
"""Send one CDP command to an already-resolved ws:// URL and return the result.
Shared by _cdp_call (page target) and _cdp_browser_call (browser target).
Returns the CDP result dict or None on any error.
"""
try:
hostpath = ws_url[len('ws://'):]
host_port, path = hostpath.split('/', 1)
host_name, port_s = host_port.split(':')
s = _cdp_sock.create_connection((host_name, int(port_s)), timeout=2)
try:
key = _cdp_b64.b64encode(os.urandom(16)).decode()
s.sendall((
f'GET /{path} HTTP/1.1\r\nHost: {host_port}\r\n'
'Upgrade: websocket\r\nConnection: Upgrade\r\n'
f'Sec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n'
).encode())
buf = b''
while b'\r\n\r\n' not in buf:
buf += s.recv(4096)
if b' 101 ' not in buf.split(b'\r\n')[0]:
return None
payload_obj = {'id': 1, 'method': method}
if params:
payload_obj['params'] = params
msg = json.dumps(payload_obj).encode()
mask = os.urandom(4)
masked = bytes(b ^ mask[i % 4] for i, b in enumerate(msg))
frame_len = len(msg)
length_byte = 0x7E if frame_len > 125 else frame_len
header = bytes([0x81, 0x80 | length_byte])
if frame_len > 125:
header += _cdp_struct.pack('!H', frame_len)
s.sendall(header + mask + masked)
hdr = b''
while len(hdr) < 2:
hdr += s.recv(2 - len(hdr))
dlen = hdr[1] & 0x7F
if dlen == 126:
lb = b''
while len(lb) < 2:
lb += s.recv(2 - len(lb))
dlen = _cdp_struct.unpack('!H', lb)[0]
data = b''
while len(data) < dlen:
data += s.recv(dlen - len(data))
return json.loads(data.decode()).get('result')
finally:
s.close()
except Exception:
return None
def _cdp_call(method, params=None):
"""One-shot CDP call on the page target via stdlib WebSocket.
Embedded Chromium builds (PyQt, Electron, native wrappers) do not receive OS
memory-pressure signals that would trigger Oilpan's automatic GC, so Python-side
CDP calls are the reliable way to invoke collection without --expose-gc.
Returns the CDP result dict or None on any error.
"""
try:
raw = _cdp_req.urlopen('http://localhost:9222/json', timeout=1).read()
pages = json.loads(raw)
ws_url = next(
(p['webSocketDebuggerUrl'] for p in pages if p.get('type') == 'page'),
None,
)
if not ws_url:
return None
return _cdp_ws_call(ws_url, method, params)
except Exception:
return None
# ── macOS memory reads (pure ctypes; NO ps/vm_stat subprocesses) ──────────────────
# ps and vm_stat cost a process spawn per sample and need text parsing. The kernel
# already exposes the same numbers in-process: proc_pid_rusage() gives per-pid
# resident size (the VmRSS analogue) and lifetime max phys footprint (the VmPeak
# analogue), and sysctl hw.memsize / kern.memorystatus_vm_pressure_level give
# system totals and the OS pressure level. All verified live on macOS 26 (Tahoe):
# resident_size matched `ps -o rss` within 0.1%.
_libc = ctypes.CDLL("/usr/lib/libSystem.B.dylib", use_errno=True)
# rusage_info_v4 layout: 16-byte uuid then packed uint64 fields (resource.h).
# Indices below are uint64 offsets after the uuid.
_RUSAGE_INFO_V4 = 4
_RI_RESIDENT_SIZE = 6 # bytes
_RI_PHYS_FOOTPRINT = 7 # bytes (what Activity Monitor's "Memory" shows)
_RI_LIFETIME_MAX_PHYS_FOOTPRINT = 28 # bytes, v4+ only
def _sysctl_u64(name: str):
"""Read a 64-bit sysctl by name, or None on failure."""
try:
val = ctypes.c_uint64(0)
size = ctypes.c_size_t(8)
if _libc.sysctlbyname(name.encode(), ctypes.byref(val),
ctypes.byref(size), None, 0) == 0:
return val.value
except Exception:
pass
return None
def _sysctl_u32(name: str):
"""Read a 32-bit sysctl by name, or None on failure."""
try:
val = ctypes.c_uint32(0)
size = ctypes.c_size_t(4)
if _libc.sysctlbyname(name.encode(), ctypes.byref(val),
ctypes.byref(size), None, 0) == 0:
return val.value
except Exception:
pass
return None
def _mac_process_mem_kb(pid):
"""(resident kB, phys_footprint kB, lifetime_max_footprint kB) for pid,
or (0, 0, 0) on failure.
proc_pid_rusage works cross-pid for same-uid processes, which covers the
renderer (our child). No task_for_pid entitlement needed.
"""
try:
buf = (ctypes.c_uint64 * 64)() # 512 B, > sizeof(rusage_info_v4)
if _libc.proc_pid_rusage(int(pid), _RUSAGE_INFO_V4, buf) == 0:
u64 = ctypes.cast(buf, ctypes.POINTER(ctypes.c_uint64))
base = 2 # skip the 16-byte uuid
return (u64[base + _RI_RESIDENT_SIZE] // 1024,
u64[base + _RI_PHYS_FOOTPRINT] // 1024,
u64[base + _RI_LIFETIME_MAX_PHYS_FOOTPRINT] // 1024)
except Exception:
pass
return (0, 0, 0)
# Purge reasons driven by genuine memory pressure. These bypass the busy-page
# gate below: when the host is nearly out of memory, a possible renderer crash
# (which auto-reloads) beats a host OOM kill (which takes the whole app down).
_PURGE_PRESSURE_REASONS = frozenset({'psi-critical', 'low-memory', 'node-threshold'})
# Busy-page probe: input-idle is NOT page-idle. The renderer segfaulted three
# times on 2026-07-19 (exit=11), each immediately after a forcible purge fired
# while a model download was repainting the cookbook card — no mouse/keyboard
# input, so every idle timer considered the page quiescent. Ask the page itself:
# cookbookRunning.js persists its task list in localStorage ('cookbook-tasks'),
# so an active download or a serve that is still starting up is visible here
# without any new JS-side wiring.
_BUSY_TASKS_JS = (
"(()=>{try{const t=JSON.parse(localStorage.getItem('cookbook-tasks'))||[];"
"return t.some(x=>x&&((x.type==='download'&&(x.status==='running'||x.status==='queued'))"
"||(x.type==='serve'&&x.status==='running')))}catch(e){return false}})()"
)
def _renderer_busy():
"""True when the page reports an active download/starting serve.
Runs on the CDP executor thread (socket I/O). Fails open (False) so a CDP
hiccup can never permanently disable memory reclaim.
"""
res = _cdp_call('Runtime.evaluate',
{'expression': _BUSY_TASKS_JS, 'returnByValue': True})
try:
return bool(res['result']['value'])
except (TypeError, KeyError):
return False
# Pattern that chatHistory.js emits at the end of each Phase 2 eviction batch.
_RE_EVICT = _re.compile(r'\[chatHistory\] Phase 2 evict: removed (\d+) live nodes')
# Bounds CDP background work to eviction audit threads (5 s sleep each).
_cdp_executor = _futures.ThreadPoolExecutor(max_workers=2, thread_name_prefix='cdp')
# Renderer reclaim policy (issue #106). simulatePressureNotification is a no-op on
# QtWebEngine; Memory.forciblyPurgeJavaScriptMemory actually reclaims the renderer
# cache (measured 5.2 GB and 3.7 GB freed in single calls). The purge causes a ~1s
# stutter, so it is only ever fired off the interaction path (mouse-idle,
# focus-loss), gated by an RSS ceiling so light use never stutters, and rate-limited
# so it cannot repeat back to back.
# ── Capability detection: Rung 1 (docs/fork/low-resource-profile-design.md) ────────
# Pick the DEFAULT reclaim profile from device capability. MACOS-ONLY signals; this
# is the macOS wrapper; qt_wrapper.py / windows_wrapper.py carry their own platform
# equivalents. An explicit ODYSSEUS_* env var always overrides this (Rung 0). FAIL-
# SAFE: any read error yields the STANDARD (capable) profile, so a glitch never
# degrades a good machine; a misread low-RAM box is covered by the env override.
_LOW_RAM_GB = 2.0 # ≤ this ⇒ constrained (aligns with Android isLowRamDevice / IsLowEndDevice)
def _classify_resources(mem_total_gb, software_render):
"""Pure mapping: (mem_total_gb|None, software_render) -> (is_low_resource, reason)."""
reasons = []
if mem_total_gb is not None and mem_total_gb <= _LOW_RAM_GB:
reasons.append(f'RAM {mem_total_gb:.1f} GB')
if software_render:
reasons.append('software render')
return (bool(reasons), ', '.join(reasons) or 'capable')
def _macos_total_ram_gb():
mem = _sysctl_u64('hw.memsize')
if mem is not None:
return mem / (1024 ** 3)
return None
# Software-render signal comes from the pre-flag IOAccelerator probe at the top
# of this file (no Metal device ⇒ SwiftShader): the same probe that strips the
# GPU-rasterization flags also selects the constrained reclaim profile here.
_low_resource, _profile_reason = _classify_resources(_macos_total_ram_gb(), _software_render)
# RSS ceiling: the renderer is only purged above this. Measured working set after a
# purge is ~430 MB, so the off-interaction reclaim sawtooth stays ~0.43 GB → ceiling.
# Default ~1.2 GB (a safety net; with producers eliminated the renderer rarely
# approaches it). Tunable via ODYSSEUS_PURGE_CEILING_MB: lower it on RAM-constrained
# machines for a tighter cap (purges fire sooner/more often, the right trade when
# system swap/compressor churn is worse than an occasional off-interaction stutter;
# this is the "adaptive loading" response to a low-resource device; see docs/fork/
# low-resource-profile-design.md). Floored at 512 MB (just above the working set, so
# the ceiling can never sit below it and cause constant purging).
try:
_PURGE_RSS_CEILING_KB = max(512, int(float(os.environ.get(
'ODYSSEUS_PURGE_CEILING_MB', '700' if _low_resource else '1200')))) * 1024
except ValueError:
_PURGE_RSS_CEILING_KB = 1_200_000
_PURGE_MIN_INTERVAL_S = 15
# Seconds of no input (mouse OR keyboard) before the *sustained-idle* reclaim may
# fire. The purge blocks the renderer ~1s and there is NO lazy/async purge on
# QtWebEngine (the only CDP reclaim is the synchronous OOM-intervention; the
# memory-pressure eviction is a no-op; see research). So this must only fire on a
# genuine away-from-keyboard gap: a short reading/thinking pause must NOT trigger
# it. At 3 s it fired constantly during normal use, and a ~1s freeze landing on a
# click, or dropping a mid-drag mouseup, left Chromium's left-button state stuck
# ("can't left-click, right-click works"). The prompt-reclaim-on-leave cases are
# handled separately and without this delay by the focus-loss and minimize purges.
# Default = 60 s, the established standard: the W3C/WICG Idle Detection API
# restricts its idle threshold to a MINIMUM of 60 s; below that you are measuring
# a pause, not idle (short thresholds are unreliable for "idle" and even leak
# typing cadence, hence the spec floor). Best-practice range is 30–120 s; 60 s is
# the principled safe choice for a *disruptive* (blocking) reclaim.
# Tunable via ODYSSEUS_IDLE_RECLAIM_S for users who deliberately want more
# aggressive reclaim (lower) and accept the stutter risk. Floored at 2 s.
try:
_IDLE_RECLAIM_AFTER_S = max(2.0, float(os.environ.get(
'ODYSSEUS_IDLE_RECLAIM_S', '20' if _low_resource else '60')))
except ValueError:
_IDLE_RECLAIM_AFTER_S = 60.0
# Renderer-hang detection core (issue #137). The Qt-free bookkeeping lives in
# qt_watchdog.py so it is unit-testable under the server venv's stub PyQt6;
# this file only wires pings/pongs and performs the recovery. (qt_psi is NOT
# imported here: PSI is a Linux kernel interface; the macOS pressure signal
# is the memorystatus sysctl below.)
import qt_watchdog
import qt_about
# Log the selected profile once (diagnosable; notes when an env var overrode it).
_profile_overridden = bool(
{'ODYSSEUS_IDLE_RECLAIM_S', 'ODYSSEUS_PURGE_CEILING_MB'} & os.environ.keys())
print(f"[PROFILE] {'low-resource' if _low_resource else 'standard'} ({_profile_reason}): "
f"idle={_IDLE_RECLAIM_AFTER_S:.0f}s ceiling={_PURGE_RSS_CEILING_KB // 1024}MB"
f"{' [env override]' if _profile_overridden else ''}", flush=True)
# Pressure event cell, written by the memorystatus monitor thread and drained on
# the Qt main thread by the 250 ms drain timer (the same cross-thread dispatch
# pattern qt_wrapper.py uses for PSI events: QTimer.singleShot from a daemon
# thread has no event loop to fire on, so a polled cell is the correct hand-off).
# Single-element list assignment is GIL-atomic so no lock is needed. Holds the
# pressure level (2=warning, 4=critical) or None when no event is pending.
_pressure_event_pending: list = [None]
# kern.memorystatus_vm_pressure_level values (xnu kern_memorystatus.h).
_MACOS_PRESSURE_NORMAL = 1
_MACOS_PRESSURE_WARNING = 2
_MACOS_PRESSURE_CRITICAL = 4
def _start_macos_memory_monitor():
"""Background thread watching macOS memory pressure via the memorystatus sysctl.
kern.memorystatus_vm_pressure_level is the same graduated signal (normal /
warning / critical) that dispatch_source memory-pressure sources deliver —
readable unprivileged with one cheap in-process sysctl, no libdispatch
machinery and no vm_stat subprocess per sample (the previous design here
spawned vm_stat every 5 s and parsed its text output). This is the macOS
equivalent of the OS memory-pressure signal that Chromium's browser process
receives in a normal installation but that does not reach the renderer
sandbox in embedded QtWebEngine builds.
The thread only records the elevated level into _pressure_event_pending; the
Qt-side drain timer maps warning → async JS GC and critical → the shared
_purge_renderer gate, so sustained pressure cannot repeat-purge every poll —
the RSS ceiling and rate limit apply exactly as they do to every other
reclaim trigger. After signalling, the thread backs off a full 30 s (the
level stays elevated while pressure persists, and the gated decision already
happened on the Qt side).
"""
_POLL_S = 5
_BACKOFF_S = 30
def _loop():
if _sysctl_u32('kern.memorystatus_vm_pressure_level') is None:
print('[MEM] macOS: memorystatus pressure sysctl unavailable', flush=True)
return
print('[MEM] macOS memory pressure monitor active', flush=True)
while True:
level = _sysctl_u32('kern.memorystatus_vm_pressure_level')
if level is not None and level >= _MACOS_PRESSURE_WARNING:
_pressure_event_pending[0] = level
_time.sleep(_BACKOFF_S)
else:
_time.sleep(_POLL_S)
_threading.Thread(target=_loop, daemon=True, name='macos-mem-monitor').start()
def _cdp_audit_listeners(n_evicted: int) -> None:
"""Measure jsEventListeners delta 5 s after a Phase 2 eviction batch.
Runs in a background thread. Captures the listener count immediately before
sleeping (pre-GC baseline), forces a collection, then reads again. The forced
GC is what makes the delta meaningful: without it V8 may not have collected
the evicted nodes yet, and a delta of 0 is ambiguous between "listeners
retained" and "garbage not collected yet" (measured live 2026-07-19: delta
stayed 0 for 12+ s after evicting 61 nodes, then dropped 430 listeners the
moment a major GC ran). After the forced GC, a delta ≈ 0 with interactive
nodes evicted indicates listener retention; a proportional drop confirms
the WeakRef closures released cleanly.
"""
pre = _cdp_call('Memory.getDOMCounters')
pre_listeners = pre.get('jsEventListeners', 0) if pre else None
_time.sleep(5)
# Force a collection so the post-read reflects reachability, not GC timing.
_cdp_call('HeapProfiler.collectGarbage')
post = _cdp_call('Memory.getDOMCounters')
if post and pre_listeners is not None:
post_listeners = post.get('jsEventListeners', 0)
delta = pre_listeners - post_listeners
print(
f'[CDP] post-evict listeners:'
f' before={pre_listeners} after={post_listeners}'
f' delta={delta} nodes-evicted={n_evicted}',
flush=True,
)
class NativeBridge(QObject):
"""Python-to-JS bridge exposed via QWebChannel.
On macOS, QColorDialog.getColor() delegates to NSColorPanel — the native
macOS color picker. No DBus/XDG portal needed.
EXCEPT on software-render machines (no Metal device — VMs, remote sessions):
NSColorPanel's eyedropper spawns the ColorSampler XPC service, whose window
capture drives WindowServer through SkyLight's SOFTWARE capture path
(CaptureSurfaceSW::Populate), which aborts — killing WindowServer and the
whole login session (crash reports 2026-07-20: ColorSampler SIGILL →
WindowServer SIGABRT in WSCompositeDestinationCreateWithIOSurface). There,
eyedropperInPage tells the page to sample itself: JS overlays a crosshair
and calls samplePagePixel(x, y); we read the pixel from view.grab() — Qt's
own offscreen widget render, zero WindowServer capture involvement.
"""
colorPicked = pyqtSignal(str)
eyedropperInPage = pyqtSignal()
def __init__(self, view=None, parent=None):
super().__init__(parent)
self._view = view
@pyqtSlot()
def openColorPicker(self):
if _software_render and self._view is not None:
self.eyedropperInPage.emit()
return
color = QColorDialog.getColor()
self.colorPicked.emit(color.name() if color.isValid() else '')
@pyqtSlot(float, float)
def samplePagePixel(self, x, y):
"""Resolve an in-page eyedropper click: (x, y) are CSS px in the viewport,
which map 1:1 to widget-logical px; view.grab() returns a device-pixel
pixmap, so scale by its devicePixelRatio. (-1, -1) = user cancelled."""
if x < 0 or y < 0 or self._view is None:
self.colorPicked.emit('')
return
try:
pixmap = self._view.grab()
dpr = pixmap.devicePixelRatio() or 1.0
img = pixmap.toImage()
px, py = int(x * dpr), int(y * dpr)
if 0 <= px < img.width() and 0 <= py < img.height():
self.colorPicked.emit(img.pixelColor(px, py).name())
else:
self.colorPicked.emit('')
except Exception as e:
print(f'[EYEDROPPER] in-page sample failed: {e}', flush=True)
self.colorPicked.emit('')
class OdysseusPage(QWebEnginePage):
"""QWebEnginePage subclass that routes external links to the system browser."""
def javaScriptConsoleMessage(self, level, message, line_number, source_id):
# Chromium's --enable-logging=stderr captures the renderer's internal log but
# NOT JavaScript console.log(); those only reach Python via this override.
# Print without a prefix so structured [tag] messages sort cleanly in the log.
label = level.name if hasattr(level, 'name') else str(level)
if label in ('WARNING', 'ERROR', 'CRITICAL'):
print(f'[JS:{label}] {message}', flush=True)
else:
print(message, flush=True)
# When chatHistory.js evicts a Phase 2 batch, audit whether jsEventListeners
# drops proportionally; confirms that WeakRef fixes released the closures.
m = _RE_EVICT.match(message)
if m:
_cdp_executor.submit(_cdp_audit_listeners, int(m.group(1)))
def acceptNavigationRequest(self, url, nav_type, is_main_frame):
if is_main_frame and url.host() not in ('localhost', '127.0.0.1'):
QDesktopServices.openUrl(url)
return False
return super().acceptNavigationRequest(url, nav_type, is_main_frame)
def createWindow(self, win_type):
page = QWebEnginePage(self.profile(), self)
page.urlChanged.connect(lambda url: (QDesktopServices.openUrl(url), page.deleteLater()))
return page
class _InputIdleFilter(QObject):
"""App-level event filter that records the last user-input time and restarts the
post-interaction idle timer.
Qt WebEngine handles input internally, but Qt delivers these events at the
QApplication level before Chromium consumes them, so installing this filter on
QApplication.instance() catches all interaction over any widget or the web
content area. It tracks keyboard as well as mouse so that typing defers the
reclaim purge: a forcible purge causes a ~1s stutter, so it must never fire
mid-typing.
"""
_INPUT_EVENTS = frozenset((
QEvent.Type.MouseMove,
QEvent.Type.MouseButtonPress,
QEvent.Type.KeyPress,
QEvent.Type.Wheel,
))
def __init__(self, on_input, timer: QTimer, parent=None):
super().__init__(parent)
self._on_input = on_input
self._timer = timer
def eventFilter(self, obj, event):
if event.type() in self._INPUT_EVENTS:
self._on_input()
self._timer.start()
return False
class _QuitFilter(QObject):
"""App-level event filter that flags a genuine quit before any window's
closeEvent runs.
macOS delivers ⌘Q, the application/Dock menu's Quit, and the quit Apple
Event as a QEvent.Quit posted to the QApplication, which Qt then turns into
closeAllWindows(). OdysseusWindow.closeEvent hides-and-vetoes by default (so
the red button keeps the app in the Dock); on a real quit it must accept
instead. Catching QEvent.Quit here — before the derived closeEvents — sets
the window's _quitting flag so it accepts. We do not consume the event
(return False): Qt still runs its normal termination.
"""
def __init__(self, window, parent=None):
super().__init__(parent)
self._window = window
def eventFilter(self, obj, event):
if event.type() == QEvent.Type.Quit:
print('[LIFECYCLE] _QuitFilter: QEvent.Quit -> arm quit', flush=True)
self._window._quitting = True
return False
class OdysseusWindow(QMainWindow):
def __init__(self, profile: QWebEngineProfile):
super().__init__()
self.setWindowTitle(WINDOW_TITLE)
self.browser = QWebEngineView()
page = OdysseusPage(profile, self.browser)
self._page = page # held for lifecycle management in changeEvent
# Allow JS clipboard WRITES (copy buttons). Off by default in
# QtWebEngine, which makes navigator.clipboard.writeText and the
# execCommand('copy') fallback both silently no-op. Deliberately NOT
# enabling JavascriptCanPaste, which would let pages READ the
# system clipboard.
page.settings().setAttribute(
QWebEngineSettings.WebAttribute.JavascriptCanAccessClipboard, True)
self._last_purge = 0.0 # monotonic ts of last forcible renderer purge
self._last_input = time.monotonic() # ts of last mouse/keyboard activity
self._quitting = False # set by the app-level Quit filter; see closeEvent
self._pending_hide = False # closeEvent-from-fullscreen defers the hide
self._last_hide_ts = 0.0 # guards the reopen against the post-hide bounce
self._fs_prev_maximized = False # was the window zoomed BEFORE it went FS?
self._restore_maximized = False # re-zoom on the next Dock-reopen?
self._fs_observer_ok = False # native FS-exit observer installed?
self._hide_on_fs_observer = False # this close waits on the FS-exit event
# Inject synchronous flag so JS knows it's running inside the Qt wrapper
flag_script = QWebEngineScript()
flag_script.setSourceCode("window.__QT_WRAPPER__ = true;")
flag_script.setName("qt-wrapper-flag")
flag_script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentCreation)
flag_script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
page.scripts().insert(flag_script)
# Inject Qt's qwebchannel.js from Qt's internal resources
_f = QFile(":/qtwebchannel/qwebchannel.js")
_f.open(QIODevice.OpenModeFlag.ReadOnly)
_qwc_js = bytes(_f.readAll()).decode()
_f.close()
qwc_script = QWebEngineScript()
qwc_script.setSourceCode(_qwc_js)
qwc_script.setName("qwebchannel.js")
qwc_script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentCreation)
qwc_script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
page.scripts().insert(qwc_script)
# Native bridge, held as instance attrs to prevent GC
self._bridge = NativeBridge(view=self.browser)
self._channel = QWebChannel(page)
self._channel.registerObject("bridge", self._bridge)
page.setWebChannel(self._channel)
# Renderer crash recovery: auto-reload on OOM or hard crash
self._crash_times = []
def _on_renderer_crash(status, exit_code):
label = {0: 'Normal', 1: 'Abnormal', 2: 'Crashed', 3: 'Killed(OOM)'}.get(
status.value, f'Unknown({status.value})')
print(f'[RENDERER] {label} exit={exit_code} at {_time.strftime("%H:%M:%S")}',
flush=True)
if status.value == 0:
return
now = _time.monotonic()
self._crash_times = [t for t in self._crash_times if now - t < 10]
if self._crash_times:
print('[RENDERER] Crash loop, not reloading', flush=True)
return
self._crash_times.append(now)
print('[RENDERER] Scheduling reload in 1s', flush=True)
QTimer.singleShot(1000, lambda: self.browser.setUrl(
QUrl(f"http://localhost:{PORT}")))
page.renderProcessTerminated.connect(_on_renderer_crash)
# Periodic renderer memory snapshot (every 30s).
# Reads renderer + host memory via proc_pid_rusage (cheap, in-process, no
# ps subprocess per sample) and dispatches ONE CDP DOMCounters read to the
# executor so the GUI thread never blocks on the 9222 socket. When node
# accumulation is high, the drain timer triggers reclaim so collection
# doesn't wait for the next focus-loss.
_last_peak: list[int] = [0] # renderer lifetime-max footprint, high-water
_last_host_rss: list[int] = [0] # host-process resident, previous sample
_node_threshold_pending: list = [False] # set by executor, drained on main thread
def _log_dom_counters():
# Executor thread: CDP socket I/O + log only. The node-threshold GC is
# handed back to the Qt main thread via the drained flag because
# runJavaScript must not be called from a worker thread.
counts = _cdp_call('Memory.getDOMCounters')
if not counts:
return
nodes = counts.get('nodes', 0)
listeners = counts.get('jsEventListeners', 0)
# listeners/node ratio should be roughly constant (~3–5× for a
# typical chat session). A rising ratio indicates a listener leak
# either removeEventListener is being skipped or setInterval
# closures are preventing GC of elements that still hold listeners.
ratio = f'{listeners / nodes:.1f}' if nodes else 'n/a'
print(
f'[CDP] nodes={nodes} '
f'documents={counts.get("documents")} '
f'listeners={listeners} (listeners/node={ratio})',
flush=True,
)
# Detached node accumulation above this threshold means Oilpan is not
# keeping up.
if nodes > 50_000:
print(
f'[GC] node-count threshold ({nodes} > 50000)'
f', async JS GC',
flush=True,
)
_node_threshold_pending[0] = True
def _log_renderer_memory():
pid = page.renderProcessPid()
if pid:
rss_kb, footprint_kb, peak_kb = _mac_process_mem_kb(pid)
if rss_kb:
print(f'[MEM] pid={pid} VmRSS:\t{rss_kb} kB '
f'(footprint={footprint_kb} kB)', flush=True)
if peak_kb > _last_peak[0]:
_last_peak[0] = peak_kb
print(f'[MEM] pid={pid} VmPeak:\t{peak_kb} kB (new peak)',
flush=True)
# Host-process memory. This (mac_wrapper.py) embeds Chromium's browser
# process plus the in-process GPU thread and the in-process network/
# tracing services, so it is the largest single consumer in the stack
# and is NOT covered by the renderer-pid reading above. We track it to
# answer whether that footprint is a fixed baseline or climbs with use
# (issue #112). The per-sample delta makes growth visible.
host_rss, _host_fp, _host_peak = _mac_process_mem_kb(os.getpid())
if host_rss:
delta = host_rss - _last_host_rss[0] if _last_host_rss[0] else 0
_last_host_rss[0] = host_rss
print(f'[MEM] host pid={os.getpid()} VmRSS: '
f'{host_rss} kB (delta={delta:+d} kB)', flush=True)
# At most one CDP call per sample, off the GUI thread.
_cdp_executor.submit(_log_dom_counters)
# This periodic timer is telemetry only. The renderer purge is NOT
# fired here: the timer runs regardless of interaction and a forcible
# purge causes a ~1s stutter. Reclaim happens strictly off the
# interaction path (mouse-idle and focus-loss) via _purge_renderer.
# The previous call here, simulatePressureNotification('critical'), was
# a no-op on QtWebEngine (measured: no RSS change); issue #106.
self._mem_timer = QTimer()
self._mem_timer.timeout.connect(_log_renderer_memory)
self._mem_timer.start(30_000)
_start_macos_memory_monitor()
# Focus-loss GC timer: 500 ms single-shot debounce started on WindowDeactivate,
# cancelled on WindowActivate. Skips transient focus shifts (notifications,
# dropdowns) that would otherwise trigger unnecessary GC mid-typing.
def _on_focus_loss_gc():
print('[GC] focus-loss: async JS GC', flush=True)
page.runJavaScript(
"if(typeof gc==='function')"
"gc({type:'major',execution:'async'});"
)
# Window is not focused: a reclaim stutter is invisible. Gated and
# rate-limited inside _purge_renderer.
self._purge_renderer('focus-loss')
self._gc_focus_timer = QTimer()
self._gc_focus_timer.setSingleShot(True)
self._gc_focus_timer.timeout.connect(_on_focus_loss_gc)
# Drain timer: polls the cross-thread event cells every 250 ms on the main
# thread. QTimer.singleShot from a daemon Python thread (the memorystatus
# monitor) or an executor worker has no event loop to fire on; this polling
# pattern is the correct cross-thread dispatch (same as qt_wrapper's PSI
# drain). Two cells: the pressure level (monitor thread) and the
# node-threshold flag (DOMCounters executor task).
def _drain_events():
level = _pressure_event_pending[0]
if level is not None:
_pressure_event_pending[0] = None
if level >= _MACOS_PRESSURE_CRITICAL:
# Routed through the shared gate: the RSS ceiling and 15 s rate
# limit stop sustained pressure from purging on every wakeup.
action = self._purge_renderer('low-memory')
else:
# Warning: non-disruptive async GC only; escalate to the
# blocking purge only at critical.
page.runJavaScript(
"if(typeof gc==='function')"
"gc({type:'major',execution:'async'});"
)
action = 'async-gc'
print(
f'[MEM] macOS pressure level={level} action={action}',
flush=True,
)
if _node_threshold_pending[0]:
_node_threshold_pending[0] = False
page.runJavaScript(
"if(typeof gc==='function')"
"gc({type:'major',execution:'async'});"
)
self._purge_renderer('node-threshold')
self._gc_drain_timer = QTimer()
self._gc_drain_timer.timeout.connect(_drain_events)
self._gc_drain_timer.start(250)
# Post-interaction reclaim: when input has been still for 2 seconds the user