Repository navigation
Expand file tree
/
Copy pathlauncher.py
More file actions
1600 lines (1405 loc) · 79.2 KB
/
Copy pathlauncher.py
File metadata and controls
1600 lines (1405 loc) · 79.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
"""Bootstrap the managed source, supervise server.py, and optionally host its UI."""
from __future__ import annotations
import base64
import html
import json
import logging
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
import webbrowser
from contextlib import suppress
from logging.handlers import RotatingFileHandler
from typing import Optional
def _rehome_legacy_handoff() -> None:
"""Before package adoption can wait: repair a recognized legacy GUI handoff once."""
marked, args = os.environ.pop("OUROBOROS_LAUNCHER_HANDOFF", ""), sys.argv[1:]
if sys.platform == "win32" and not getattr(sys, "frozen", False) and not marked and "--no-ui" not in args \
and any(arg.split("=", 1)[0] == "--seed-bundle" for arg in args):
# This existing Windows leaf is stdlib-only; importing its package would run adoption first.
import runpy
runpy.run_path(str(pathlib.Path(__file__).parent / "ouroboros" / "launcher_windows_runtime.py"))[
"rehome_legacy_handoff"](args)
if __name__ == "__main__":
_rehome_legacy_handoff()
# Seal-safe imports: the current interpreter needs the flag before project imports;
# PYTHONDONTWRITEBYTECODE alone only affects later child interpreters. Bundle writes
# would invalidate a macOS signature and trigger AppTranslocation.
sys.dont_write_bytecode = True
os.environ.setdefault("PYTHONDONTWRITEBYTECODE", "1")
from ouroboros.config import (
AGENT_SERVER_PORT,
DATA_DIR,
LAUNCHER_STOP_GRACE_SEC,
PANIC_EXIT_CODE,
PID_FILE, PORT_FILE,
REPO_DIR,
RESTART_EXIT_CODE,
SETTINGS_PATH,
acquire_pid_lock,
apply_settings_to_env as _apply_settings_to_env,
load_settings,
get_runtime_mode,
normalize_runtime_mode,
read_version,
release_pid_lock,
save_settings,
)
from ouroboros.launcher_bootstrap import (
BootstrapContext,
bootstrap_repo as _bootstrap_repo,
check_git as _check_git, launcher_sources_changed, remember_loaded_checkout,
install_deps,
embedded_python_env, update_external_host,
exec_source_launcher, find_embedded_python, macos_source_entry, parse_launch_options, shell_runtime_facts,
automatic_launch_allowed,
sync_existing_repo_from_bundle,
)
from ouroboros.desktop_media import attach_media_permissions, display_ready as _display_ready, host_environment, route_gtk_browser
from ouroboros.runtime_venv import PREPARATION_DEPS_TIMEOUT_SEC, RuntimeVenvRefused, mark_ready, prepare_runtime_venv
from ouroboros.launcher_onboarding import (
prepare_first_run_settings as _prepare_first_run_settings,
present_first_run_onboarding as _present_first_run_onboarding,
setup_recovery_menu,
)
from ouroboros.launcher_background import (Background, DesktopApi, activate_running_instance, background_env, other_copy_line,
request_tray_cleanup, restore_tray_after_handoff, stop_tray_before_exit)
from ouroboros.launcher_server_reaper import QuitEngineStop, reap_same_install_strays as _reap_same_install_strays_impl
from ouroboros.launcher_context_menu import attach_every_window, enable_quote_menu
from ouroboros.launcher_readiness import wait_for_server as _wait_for_server
from ouroboros.launcher_window_chrome import paint_room, transfer_when_windowed
from ouroboros.standalone_page import themed_page
from ouroboros.launcher_windows_runtime import ( # noqa: F401 (re-exported: same objects, prior launcher surface)
_prepare_windows_webview_runtime,
_show_windows_message,
)
from ouroboros.platform_layer import (
BUNDLE_DIR_ENV,
IS_LINUX,
IS_MACOS,
IS_WINDOWS,
assign_pid_to_job,
close_job,
create_kill_on_close_job,
current_process_group_id,
force_kill_pid,
git_install_hint,
install_shutdown_signal_handlers,
kill_pid_tree,
kill_process_group_id,
kill_process_tree,
merge_hidden_kwargs,
open_path_external,
pid_provably_gone,
process_command,
process_group_id,
resume_process,
subprocess_new_group_kwargs,
terminate_job,
terminate_process_group_id,
)
from ouroboros.unindexed import bytecode_prefix
from ouroboros.utils import atomic_write_json, utc_now_iso
MAX_CRASH_RESTARTS = 5
CRASH_WINDOW_SEC = 120
# Entry protocol 1: --seed-bundle selects plain Python for the body and its
# installs; --runtime-venv is retained separately while the shell keeps app identity.
MACOS_SOURCE_ENTRY_PROTOCOL = 1
# One bounded visible retry when dependency installation fails.
_DEPS_RETRY_DELAY_SEC = 5
# WA6: globally suppress bytecode writes for the launcher process itself and any
# naive os.environ.copy() child it spawns. A signed+notarized macOS .app must not
# write __pycache__/*.pyc into its own bundle at runtime — that breaks the codesign
# seal and triggers AppTranslocation. Caches land in the data dir's unindexed prefix
# (as in launcher_bootstrap.embedded_python_env), renamed from an older launcher's
# before any child starts; any other explicit caller prefix is kept.
os.environ["PYTHONPYCACHEPREFIX"] = bytecode_prefix(DATA_DIR, os.environ.get("PYTHONPYCACHEPREFIX", ""))
_LOG_FORMAT = "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
_log_dir = DATA_DIR / "logs"
_log_dir.mkdir(parents=True, exist_ok=True)
_file_handler = RotatingFileHandler(
_log_dir / "launcher.log",
maxBytes=2 * 1024 * 1024,
backupCount=2,
encoding="utf-8",
)
_handlers: list[logging.Handler] = [_file_handler]
if not getattr(sys, "frozen", False):
_handlers.append(logging.StreamHandler())
logging.basicConfig(level=logging.INFO, format=_LOG_FORMAT, handlers=_handlers)
# Shared SSOTs: redaction filter, uncaught exceptions into these handlers; quiet request-URL lines (credentials).
try:
from ouroboros.process_logging import install_exception_hooks, utc_log_formatter
from ouroboros.observability import SecretRedactingLogFilter as _RedactFilter
for _handler in _handlers:
_handler.setFormatter(utc_log_formatter()) # the JSON logs' clock (UTC, Z) over basicConfig's local fallback
_handler.addFilter(_RedactFilter())
install_exception_hooks()
except Exception:
pass # defensive: a broken observability import must not kill the launcher
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
log = logging.getLogger("launcher")
APP_VERSION = read_version()
def _server_process_record_path() -> pathlib.Path:
return pathlib.Path(DATA_DIR) / "state" / "server_process.json"
def _hidden_run(command, **kwargs):
"""Hidden bootstrap commands keep their own Job/lifeline until cleanup."""
from ouroboros.launcher_bootstrap import run_bootstrap_command
if IS_LINUX:
kwargs.setdefault("env", host_environment())
return run_bootstrap_command(command, _shutdown_event, **merge_hidden_kwargs(kwargs))
def _hidden_popen(command, *, bootstrap=False, **kwargs):
"""subprocess.Popen() with platform-appropriate hidden-window flags."""
if IS_LINUX:
kwargs.setdefault("env", host_environment())
if bootstrap:
from ouroboros.launcher_bootstrap import spawn_bootstrap_command
return spawn_bootstrap_command(command, _shutdown_event, interactive=True, **merge_hidden_kwargs(kwargs))["proc"]
return subprocess.Popen(command, **merge_hidden_kwargs(kwargs))
def _bundle_dir() -> pathlib.Path:
if getattr(sys, "frozen", False):
return pathlib.Path(sys._MEIPASS)
return _external_seed_bundle or pathlib.Path(__file__).parent
# The layout of a window shown before the server, on the interface's tokens (standalone_page).
_WINDOW_CSS = """
body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center;
background: var(--bg-primary, Canvas); color: var(--text-primary, CanvasText); font-family: var(--font-sans, system-ui); }
.window { box-sizing: border-box; max-width: 460px; padding: 24px; text-align: center; }
.window h2 { margin: 0 0 12px; font-size: var(--type-page, 24px); font-weight: 600; line-height: var(--line-title); }
.window p { margin: 0; font-size: var(--type-body); line-height: var(--line-body); overflow-wrap: anywhere; }
.window .muted { margin-top: 10px; font-size: var(--type-meta); line-height: var(--line-meta); }
.window .btn { margin: 16px auto 0; }
.window .ui-status { margin-top: 12px; }
"""
def _window_page(title: str, body: str) -> str:
"""A window shown before the server: no saved choice can reach it, so it follows the device (macOS),
in the web/ shipped with this launcher's own code (the app's when frozen, the checkout's otherwise)."""
code_dir = pathlib.Path(sys._MEIPASS) if getattr(sys, "frozen", False) else pathlib.Path(__file__).parent
return themed_page(code_dir / "web", title, f'<div class="window">{body}</div>', _WINDOW_CSS)
def _already_running_window(line: str) -> None:
"""A second launch's pre-server notice (``webview`` is imported by then)."""
import webview
paint_room(webview.create_window("Ouroboros", html=_window_page(
"Ouroboros", f"<h2>Ouroboros is already running</h2><p>{html.escape(line)}</p>"), width=440, height=200))
webview.start(private_mode=False)
def _exec_source_launcher(windowed=None) -> None:
exec_source_launcher(_bootstrap_context(), _launch_argv, release_lock=release_pid_lock,
acquire_lock=acquire_pid_lock, before_release=lambda: stop_tray_before_exit(lambda: None),
on_failure=restore_tray_after_handoff, desktop=not _external_ui, macos=IS_MACOS,
cancelled=_shutdown_event.is_set, windowed=windowed)
def _shell_facts() -> dict:
seed = _bundle_dir() if getattr(sys, "frozen", False) or _external_seed_bundle else None
return shell_runtime_facts(REPO_DIR, seed, APP_VERSION, macos=IS_MACOS)
def _bootstrap_context() -> BootstrapContext:
return BootstrapContext(
bundle_dir=_bundle_dir(),
repo_dir=REPO_DIR,
data_dir=DATA_DIR,
settings_path=SETTINGS_PATH,
embedded_python=EMBEDDED_PYTHON,
app_version=(_external_seed_bundle / "VERSION").read_text().strip() if _external_seed_bundle else APP_VERSION,
hidden_run=_hidden_run,
# Launcher is owner-process boundary; first-launch migration may set runtime mode.
save_settings=lambda settings: save_settings(settings, allow_elevation=True),
log=log,
**({"deps_timeout_sec": PREPARATION_DEPS_TIMEOUT_SEC} if _runtime_venv and not _runtime_venv.ready else {}),
)
def check_git() -> bool:
return _check_git(IS_WINDOWS)
def bootstrap_repo() -> bool:
return _bootstrap_repo(_bootstrap_context())
_agent_proc: Optional[subprocess.Popen] = None
_agent_job: Optional[object] = None
_agent_lock = threading.Condition() # also tells a waiting stop that a start was published
_SPAWNING = object()
_agent_start: object = None # _SPAWNING inside Popen; then, on Windows, the published child no resume was committed for
_agent_stopping: object = None # the published child a stop already asked to end; a later stop only waits for it
_server_record_retire_lock = threading.Lock() # the two retirers of state/server_process.json
_shutdown_event = threading.Event()
_quit = QuitEngineStop(LAUNCHER_STOP_GRACE_SEC) # only an explicit Quit ends the owned engine (ARCHITECTURE §9)
# Set by _request_agent_restart(): the next agent exit is a REQUESTED recycle
# (first-run configuration adopted), not a crash — same shape as the exit-code-42
# branch, for the case where the launcher, not the agent, decided the restart.
_agent_restart_requested = threading.Event()
_agent_exit_before_ready = threading.Event() # the child exited before it served: the UI wait ends now
_webview_window = None
# Linux-only browser-fallback flag (#56): set by _detect_headless() when no
# pywebview GUI backend can initialize. Stays False on macOS/Windows — the
# probe never runs there, so every `if _headless:` branch is dead code on
# those platforms and their behavior is unchanged.
_headless = False
_external_ui = False
_external_host_update: Optional[pathlib.Path] = None
_external_host_result: dict = {}
_external_seed_bundle: Optional[pathlib.Path] = None
EMBEDDED_PYTHON = find_embedded_python(_bundle_dir(), macos=IS_MACOS)
_launch_argv: list[str] = []
_launch_intent = "owner"
_runtime_venv = None # runtime_venv.RuntimeVenv selected by --runtime-venv
_generation_port: Optional[int] = None # the endpoint the current generation proved healthy
def _recorded_server_identity(record: dict) -> str:
"""``"ours"``, ``"foreign"`` (gone, reused, another process) or ``"unknown"`` (unmeasurable, so no
signal and no retirement). A measured fingerprint decides alone; older records keep their path/group/command proof."""
try:
pid = int(record.get("pid") or 0)
except (TypeError, ValueError):
return "foreign"
if pid <= 0 or pid == os.getpid() or pid_provably_gone(pid):
return "foreign"
if isinstance(record.get("fingerprint"), dict):
from ouroboros.process_custody import process_fingerprint_matches
if process_fingerprint_matches(record, require_measured=True):
return "ours"
return "unknown" if process_fingerprint_matches(record) else "foreign"
expected_server = str((REPO_DIR / "server.py").resolve())
expected_repo = str(REPO_DIR.resolve())
if record.get("server_path") not in (None, "", expected_server) or record.get("repo_dir") not in (None, "", expected_repo):
return "foreign"
try:
recorded_pgid = int(record.get("pgid") or 0)
except (TypeError, ValueError):
recorded_pgid = 0
live_pgid = process_group_id(pid)
if not IS_WINDOWS and (recorded_pgid <= 0 or live_pgid != recorded_pgid):
return "unknown" if recorded_pgid > 0 and live_pgid <= 0 else "foreign"
command = process_command(pid)
if not command:
return "unknown"
return "ours" if expected_server in command or ("server.py" in command and expected_repo in command) else "foreign"
def _write_server_process_record(proc: subprocess.Popen, *, port: int, server_py: pathlib.Path,
server_host_source: str) -> None:
try:
record = {
"pid": int(proc.pid),
"pgid": process_group_id(proc.pid),
"server_path": str(server_py.resolve()),
"server_host_source": server_host_source,
"repo_dir": str(REPO_DIR.resolve()),
"requested_port": int(port),
"port": int(port),
"argv": [str(EMBEDDED_PYTHON), str(server_py.resolve())],
"created_at": utc_now_iso(),
}
from ouroboros.process_custody import process_fingerprint
record["fingerprint"] = process_fingerprint(proc.pid, record["argv"]) # the identity cleanup proves
atomic_write_json(_server_process_record_path(), record, trailing_newline=True)
except Exception:
log.warning("Failed to write server process record", exc_info=True)
def _update_server_process_record_port(pid: int, actual_port: int) -> None:
try:
record_path = _server_process_record_path()
if not record_path.exists():
return
record = json.loads(record_path.read_text(encoding="utf-8"))
if not isinstance(record, dict) or int(record.get("pid") or 0) != int(pid):
return
record["port"] = int(actual_port)
record.setdefault("requested_port", record["port"])
record["port_updated_at"] = utc_now_iso()
atomic_write_json(record_path, record, trailing_newline=True)
except Exception:
log.debug("Failed to update server process record port", exc_info=True)
def _retained_shared_daemon_pids() -> set[int]:
"""Read this installation's shared daemon custody; never start or adopt it."""
from ouroboros.claudexor_daemon import CUSTODY_PURPOSE
from ouroboros.process_custody import live_daemon_root_pids
return live_daemon_root_pids(
DATA_DIR, purposes={CUSTODY_PURPOSE}, retained_purposes={CUSTODY_PURPOSE}, strict=True,
)
def _confirm_gone(pid: int, timeout: float = 5.0) -> bool:
from ouroboros.process_containment import pid_is_zombie
deadline = time.monotonic() + timeout
while not (pid_provably_gone(pid) or pid_is_zombie(pid)):
if time.monotonic() >= deadline:
return False
time.sleep(0.1)
return True
def _cleanup_recorded_server_process(reason: str = "preflight") -> list[int]:
"""Stop the generation recorded in server_process.json; return the PIDs still unresolved.
Only a positively identified server is signalled, and its record retires only after
confirmed death or a refuted identity. An unmeasurable identity or an unconfirmed
stop keeps the record and is returned, so no next generation starts beside it."""
record_path = _server_process_record_path()
try:
record = json.loads(record_path.read_text(encoding="utf-8"))
except FileNotFoundError:
return []
except (OSError, ValueError):
record = None
if not isinstance(record, dict):
record_path.unlink(missing_ok=True) # no identity to act on or to keep
return []
identity, pid = _recorded_server_identity(record), int(record.get("pid") or 0)
if identity == "foreign":
log.info("Retiring the server process record pid=%d: gone or another process (%s)", pid, reason)
record_path.unlink(missing_ok=True)
return []
if identity == "unknown":
log.warning("Recorded server pid=%d cannot be verified (%s): no signal, record retained", pid, reason)
return [pid]
pgid = int(record.get("pgid") or 0)
log.info("Stopping recorded server process pid=%d pgid=%d (%s)", pid, pgid, reason)
try:
retained = _retained_shared_daemon_pids()
if not IS_WINDOWS and pgid > 0 and pgid != current_process_group_id():
terminate_process_group_id(pgid, exclude_pids=retained)
time.sleep(0.5)
kill_process_group_id(pgid, exclude_pids=retained)
kill_pid_tree(pid, exclude_pids=retained)
except Exception:
log.warning("Shared daemon custody unavailable; stopping only the recorded server (%s)", reason, exc_info=True)
force_kill_pid(pid)
if not _confirm_gone(pid):
log.error("Recorded server pid=%d survived its stop (%s); record retained", pid, reason)
return [pid]
record_path.unlink(missing_ok=True)
return []
def _cleanup_recorded_server_group_for_pid(pid: int, reason: str = "agent_exit") -> bool:
"""Retire the record of a captured child this launcher has already reaped (both callers wait first).
Nothing is signalled: once the root is reaped, its PID and group id are names a later process may
hold, so a group that still has members is reported, never killed. Workers end through their parent
lifeline, ledgered children through the next generation's custody sweep."""
with _server_record_retire_lock: # stop_agent and the lifecycle thread: Windows refuses a delete beside a read
try:
record_path = _server_process_record_path()
if not record_path.exists():
return True # the other caller already retired it
record = json.loads(record_path.read_text(encoding="utf-8"))
if not isinstance(record, dict) or int(record.get("pid") or 0) != int(pid):
return True
if not IS_WINDOWS and (pgid := int(record.get("pgid") or 0)) > 0:
from ouroboros.process_containment import process_group_has_live_members
if process_group_has_live_members(pgid):
log.warning("Server pid=%d exited while its former process group %d still has members; "
"not signalled after the reap (%s)", pid, pgid, reason)
record_path.unlink(missing_ok=True)
except Exception:
log.warning("Failed to retire the recorded server process (%s)", reason, exc_info=True)
return False
return True
def start_agent(port: int = AGENT_SERVER_PORT) -> Optional[subprocess.Popen]:
"""Start server.py as the managed agent subprocess; None once the shutdown latch refused the start."""
global _agent_proc, _agent_job, _agent_start
# Stdlib-only; import before spawning so broken server imports cannot leave its pipe undrained.
from ouroboros.process_logging import copy_capped_output
settings = _load_settings()
_apply_settings_to_env(settings)
env = embedded_python_env(DATA_DIR, host_environment() if IS_LINUX else os.environ.copy())
env["PYTHONPATH"] = str(REPO_DIR)
# Environment owns the bind host (config.SETTINGS_KEYS_NOT_EXPORTED_TO_ENV).
# Merged settings include the unauthored 127.0.0.1 default: setdefault supplies
# it only when absent, preserving the operator override the export exclusion protects.
saved_host = str(settings.get("OUROBOROS_SERVER_HOST") or "").strip()
host_source = "environment" if str(env.get("OUROBOROS_SERVER_HOST") or "").strip() else "settings"
if saved_host:
env.setdefault("OUROBOROS_SERVER_HOST", saved_host)
env["OUROBOROS_SERVER_PORT"] = str(port)
env["OUROBOROS_DATA_DIR"] = str(DATA_DIR)
env["OUROBOROS_REPO_DIR"] = str(REPO_DIR)
env["OUROBOROS_APP_VERSION"] = _shell_facts()["bundle_version"] or str(APP_VERSION)
env["OUROBOROS_MANAGED_BY_LAUNCHER"] = "1"
env["OUROBOROS_MANAGED_REPO_DIR"] = str(REPO_DIR.resolve())
env["OUROBOROS_LAUNCH_INTENT"] = _launch_intent # this launcher's parsed intent, never an inherited one
# Re-stamp presentation each generation (other hosts: web); a killed launcher's value stays until reap.
# Env-only: SETTINGS_DEFAULTS would pop an injected value absent on disk.
env["OUROBOROS_PRESENTATION"] = (
str(os.environ.get("OUROBOROS_PRESENTATION") or "web")
if _external_ui else "browser_fallback" if _headless else "desktop_window"
)
env.update(background_env(env["OUROBOROS_PRESENTATION"])) # can this launcher keep running with its window hidden
if _external_host_update is not None:
env["OUROBOROS_EXTERNAL_HOST_UPDATE"] = str(_external_host_update)
env["OUROBOROS_EXTERNAL_HOST_RESULT"] = json.dumps(_external_host_result)
else:
env.pop("OUROBOROS_EXTERNAL_HOST_UPDATE", None)
env.pop("OUROBOROS_EXTERNAL_HOST_RESULT", None)
# The managed server still resolves payloads from this bundle.
env[BUNDLE_DIR_ENV] = str(_bundle_dir())
server_py = REPO_DIR / "server.py"
log.info("Starting agent: %s %s (port=%d)", EMBEDDED_PYTHON, server_py, port)
env["OUROBOROS_LAUNCHER_STOP_STDIN"] = "1" # every platform: quit stops, EOF (launcher gone) too
popen_kwargs: dict = {
"cwd": str(REPO_DIR),
"env": env,
"stdin": subprocess.PIPE,
"stdout": subprocess.PIPE,
"stderr": subprocess.STDOUT,
}
popen_kwargs.update(subprocess_new_group_kwargs(suspended=IS_WINDOWS)) # Windows resumes it once its Job owns it
with _agent_lock: # begin; a stop that finds this start inside Popen waits for it within its own grace
if _shutdown_event.is_set():
return None
_agent_start, proc = _SPAWNING, None
try: # hidden-window flags: the server starts without a console window (CREATE_NO_WINDOW on Windows)
proc = _hidden_popen([EMBEDDED_PYTHON, str(server_py)], **popen_kwargs)
finally:
with _agent_lock: # publish it (on Windows not yet resumed), or settle the failed spawn
_agent_proc, _agent_start = proc, proc if IS_WINDOWS else None # none is published while one starts
_agent_lock.notify_all()
if IS_WINDOWS:
job = create_kill_on_close_job(allow_breakaway=True)
owned = job is not None and assign_pid_to_job(job, proc.pid)
with _agent_lock: # Job before resume, decided against a stop: one that came first keeps it suspended
resume = owned and _agent_start is proc and not _shutdown_event.is_set()
if resume:
_agent_job, _agent_start = job, None
if not (resume and resume_process(proc.pid)): # one end for a child that must never run
first = owned and not resume
log.log(logging.INFO if first else logging.ERROR, "Killing agent pid %d before it runs: %s", proc.pid,
"its stop came first" if first else "resume failed" if owned else "it has no Windows Job Object")
terminate_job(job)
if not resume: # unpublished: this starter owns it; a published Job closes when its generation retires
close_job(job)
proc.kill()
return proc
log.info("Agent pid %d assigned to Windows Job Object", proc.pid)
_write_server_process_record(proc, port=port, server_py=server_py, server_host_source=host_source)
# The server blocks once its output pipe fills, so the copy drains until the pipe ends.
threading.Thread(target=copy_capped_output, args=(proc.stdout, DATA_DIR / "logs" / "agent_stdout.log"),
kwargs={"report": log}, name="agent-output-copy", daemon=True).start()
return proc
def stop_agent() -> bool:
"""Ordinarily stop the captured server; True once its exit is confirmed. An unconfirmed stop keeps
the Popen, Job and record, so no next generation starts beside a server that may still be live. A
child another stop already asked to end (its pipe closed) is only awaited, never signalled again."""
global _agent_start, _agent_stopping
grace, deadline = LAUNCHER_STOP_GRACE_SEC, time.monotonic() + LAUNCHER_STOP_GRACE_SEC
with _agent_lock:
if _agent_start is _SPAWNING: # its publication and this stop share one grace, never a second
_agent_lock.wait_for(lambda: _agent_start is not _SPAWNING, grace)
grace = max(0.0, deadline - time.monotonic())
proc, job, unpublished = _agent_proc, _agent_job, _agent_start is _SPAWNING
if unresumed := proc is not None and _agent_start is proc:
_agent_start = None # this stop decided first: its starter never resumes it
asked, _agent_stopping = proc is not None and _agent_stopping is proc, proc # one request per child
if proc is None:
if unpublished:
log.error("A server start was still inside Popen at the stop's grace; its child is unconfirmed")
return not unpublished
log.info("Stopping agent (pid=%s)...", proc.pid)
confirmed = False
try:
if unresumed: # nothing to drain: straight to the forced stop and its confirmed exit
raise subprocess.TimeoutExpired("server", 0)
stdin = None if asked else getattr(proc, "stdin", None)
if stdin is not None: # the owned pipe asks the server to drain first; Windows terminate is forced
try:
stdin.write(b"quit\n")
stdin.flush()
except (OSError, ValueError):
log.warning("Launcher stop pipe unavailable; retaining the exit wait and fallback")
if not IS_WINDOWS:
proc.terminate()
finally:
with suppress(OSError, ValueError): # a failed pipe must still reach the wait/forced cleanup
stdin.close()
elif not asked:
proc.terminate()
proc.wait(timeout=grace)
confirmed = True
except subprocess.TimeoutExpired:
if IS_WINDOWS and job is not None:
with _agent_lock: # its members held first; a generation retired meanwhile has a closed Job: no call
_quit.force(job if _agent_proc is proc else None, terminate=terminate_job)
else:
try:
kill_process_tree(proc, exclude_pids=_retained_shared_daemon_pids())
except Exception:
log.warning("Shared daemon custody unavailable; stopping only the captured server", exc_info=True)
proc.kill()
with suppress(subprocess.TimeoutExpired):
proc.wait(timeout=5)
confirmed = True
except Exception:
log.warning("Ordinary agent stop failed", exc_info=True)
if not (confirmed or (callable(getattr(proc, "poll", None)) and proc.poll() is not None)):
log.error("Agent pid=%s did not confirm its exit; its handle and record stay for the next stop", proc.pid)
return False
return _retire_agent(proc, "stop_agent")
def _retire_agent(proc, reason: str) -> bool:
"""A reaped generation: retire its publication, the Job it owned and its record; a later one's stay."""
global _agent_proc, _agent_job, _agent_start, _agent_stopping
with _agent_lock: # its Job ends under the lock: a stop waiting here meets no starter of this generation
job = _agent_job if _agent_proc is proc else None
if _agent_proc is proc: # no start begins before the loop retired its generation
_agent_proc = _agent_job = _agent_start = _agent_stopping = None
_quit.retire(job, terminate=terminate_job, close=close_job,
panic=getattr(proc, "returncode", None) == PANIC_EXIT_CODE) # Panic's exit never waits
return _cleanup_recorded_server_group_for_pid(proc.pid, reason)
def _startup_teardown(reason: str, stopped: bool) -> None:
"""A startup exit after ``stop_agent``: stop identity-proven servers, never stale companion rows; then Quit's."""
_quit.stop_engine(server_stopped=not _cleanup_recorded_server_process(reason) and stopped)
def _read_port_file(default: Optional[int] = AGENT_SERVER_PORT) -> Optional[int]:
"""Read the active server port from PORT_FILE."""
try:
if PORT_FILE.exists():
return int(PORT_FILE.read_text(encoding="utf-8").strip())
except (ValueError, OSError):
pass
return default
def _requested_server_port() -> int:
"""``OUROBOROS_SERVER_PORT`` as requested, else the historical default; the server keeps its fallback."""
raw = str(os.environ.get("OUROBOROS_SERVER_PORT") or "").strip()
return int(raw) if raw else AGENT_SERVER_PORT
def _reap_same_install_strays(reason: str) -> list[int]:
"""Kill leftover generations of THIS install's server; return proven survivors.
Only ever called while this process holds the single-instance pid lock, which is what makes the
identity rule sound: with the lock held, another process running this install's server.py under
this launcher's stamped environment cannot be a live peer's generation. Never called from a
panic or window-close path — Emergency Stop tears down what it owns and adds no new killing.
"""
try:
return _reap_same_install_strays_impl(
REPO_DIR, DATA_DIR, reason, retained_descendant_roots=_retained_shared_daemon_pids(),
)
except Exception:
# A sweep that cannot run must not stop the launcher booting.
log.warning("Same-install stray sweep failed (%s)", reason, exc_info=True)
return []
def _pre_generation_cleanup() -> list[int]:
"""Resolve the previous generation before starting a new one; returns PIDs still unresolved.
Per GENERATION, not once per launcher: exit-42 and crash restarts are where the observed
double-boot collisions began. Recorded cleanup first, then the stray sweep while PPID links
are still live. No port sweep: a listener proves no ownership, and the data-root admission
lock refuses a second updated server whatever port it would take."""
unresolved = _cleanup_recorded_server_process("startup")
return unresolved + [pid for pid in _reap_same_install_strays("startup") if pid not in unresolved]
def _clear_port_file() -> bool:
"""Remove the previous generation's port publication; True only once it is confirmed gone."""
try:
PORT_FILE.unlink(missing_ok=True)
except OSError:
log.warning("Could not remove %s; only this generation's binding can prove readiness", PORT_FILE)
return not PORT_FILE.exists()
def _await_generation_endpoint(proc, *, port_file_cleared: bool, abort_event=None) -> Optional[int]:
"""The port THIS child published, or None when it exited or the launcher stopped. The wait lasts
as long as the child lives: no clock decides that a live server failed (its boot is long on a
cold disk; the server answers ``/api/health`` as soon as its socket serves). The main binding
counts only with the child's PID and measured birth identity (a stale row of an earlier server
that held the same PID fails); the plain port file only after its pre-start removal was confirmed."""
from ouroboros.server_process import read_service_bindings, service_binding_is_live
while not (abort_event is not None and abort_event.is_set()):
if proc.poll() is not None:
return None
try:
binding = read_service_bindings(DATA_DIR).get("main")
if isinstance(binding, dict) and binding.get("pid") == proc.pid and service_binding_is_live(binding):
return int(binding["port"])
except (OSError, ValueError, TypeError):
pass # an informational snapshot; the confirmed-fresh port file remains
if port_file_cleared and (published := _read_port_file(default=None)):
return published
time.sleep(0.25)
return None
def _kill_orphaned_children(reason: str = "window_close") -> None:
"""Shared close/Panic final cleanup (not the startup stray sweep)."""
from ouroboros.launcher_server_reaper import kill_runtime_orphans
kill_runtime_orphans(DATA_DIR, _cleanup_recorded_server_process, reason=reason, log=log)
def agent_lifecycle_loop(port: int = AGENT_SERVER_PORT) -> None:
"""Start/monitor agent; restart on code 42 or bounded crashes."""
global _external_host_result, _generation_port
crash_times: list[float] = []
while not _shutdown_event.is_set():
unresolved = _pre_generation_cleanup()
if unresolved:
# Starting now could put a second generation on the same data directory: a known
# predecessor is unconfirmed or unverifiable. The next iteration tries again.
log.error("Not starting the agent: previous server process(es) %s are not confirmed "
"stopped. Retrying in 3s.", unresolved)
time.sleep(3)
continue
if not automatic_launch_allowed(_launch_intent, DATA_DIR, log):
# Panic landed after main()'s check (a long bootstrap, a restart): stay stopped.
_shutdown_event.set()
break
port_file_cleared = _clear_port_file()
_agent_exit_before_ready.clear() # this generation proves itself
_external_host_result = update_external_host(_external_host_update, EMBEDDED_PYTHON, log, _shutdown_event)
if _shutdown_event.is_set():
break # Native preparation has reaped its owned processes before returning.
proc = start_agent(port) # None when the latch came during its preparation: begin refuses then
if _shutdown_event.is_set():
stop_agent()
break
actual_port = _await_generation_endpoint(proc, port_file_cleared=port_file_cleared, abort_event=_shutdown_event)
if actual_port is None:
log.warning("Agent server exited (or the launcher stopped) before it published an endpoint")
else:
_update_server_process_record_port(proc.pid, actual_port)
if _wait_for_server(actual_port, abort_event=_shutdown_event, proc=proc):
_generation_port = actual_port
else:
log.warning("Agent server exited before it answered /api/health on port %d", actual_port)
proc.wait()
proved = _generation_port is not None
_generation_port = None
exit_code = proc.returncode
log.info("Agent exited with code %d", exit_code)
if not proved and exit_code not in (RESTART_EXIT_CODE, PANIC_EXIT_CODE) and not _agent_restart_requested.is_set():
_agent_exit_before_ready.set() # a child that died before serving: the UI wait fails now, not at a deadline
if exit_code == RESTART_EXIT_CODE:
try:
from ouroboros.delegate_recovery import acknowledge_observed_restart_exit
if not acknowledge_observed_restart_exit(
DATA_DIR, supervisor_pid=proc.pid, exit_code=exit_code,
):
log.info("No prepared delegated-restart transaction required acknowledgement.")
except Exception:
log.warning("Could not acknowledge delegated restart transaction", exc_info=True)
_retire_agent(proc, "agent_exit")
if _shutdown_event.is_set() and exit_code != PANIC_EXIT_CODE:
break
if exit_code == PANIC_EXIT_CODE:
log.info("Panic stop (exit code %d) — shutting down completely.", PANIC_EXIT_CODE)
_shutdown_event.set()
request_tray_cleanup() # Panic removes the indicator without waiting for it
# The agent (server child) already exited; tear down any orphans and
# force-exit the whole process. _webview_window.destroy() from this
# supervisor thread cannot end the main-thread Cocoa webview loop on
# macOS (it leaves a black frozen window), so exit with parity to the
# window-close path: kill orphans, release the pid lock, os._exit(0).
try: # the server's census may not have finished before its hard exit: finish it by identity, bounded
from ouroboros.process_scope import complete_pending_panic_sweep
completion = threading.Thread(
target=complete_pending_panic_sweep, args=(DATA_DIR,),
kwargs={"exclude": {os.getpid(), os.getppid(), 1}, "installation": True}, name="panic-scope-completion", daemon=True)
completion.start()
completion.join(timeout=3.0) # later starts retry saved identities only; the exit never waits longer
except Exception:
log.warning("Pending Panic scope sweep failed; later starts retry saved identities only", exc_info=True)
_kill_orphaned_children(reason="panic_stop")
stop_tray_before_exit(release_pid_lock, wait=0)
os._exit(0)
time.sleep(2)
if _shutdown_event.is_set():
break
if _agent_restart_requested.is_set():
# Launcher-requested recycle (first-run configuration): deliberate,
# so it skips crash accounting exactly like the agent's own code-42
# restart. No dependency sync — nothing about the checkout changed.
_agent_restart_requested.clear()
log.info("Restarting the agent to adopt the saved first-run configuration.")
continue
if exit_code == RESTART_EXIT_CODE:
log.info("Agent requested restart (exit code 42). Restarting...")
sync_existing_repo_from_bundle(_bootstrap_context()) # a deployment policy's packages are its administrator's: none synced
if not os.environ.get("OUROBOROS_DEPLOYMENT_POLICY", "").strip() and not install_deps(_bootstrap_context()):
# Retry dependency sync once before letting imports fail under the crash fuse.
log.error(
"Dependency install failed after the restart request; "
"retrying once in %ds.", _DEPS_RETRY_DELAY_SEC,
)
time.sleep(_DEPS_RETRY_DELAY_SEC)
if not install_deps(_bootstrap_context()):
log.error(
"Dependency install failed twice; starting the agent anyway "
"under the crash fuse (%d crashes in %ds stops the launcher). "
"If the new commit added packages, the server will fail to "
"import them — see the pip output above for the cause.",
MAX_CRASH_RESTARTS, CRASH_WINDOW_SEC,
)
if launcher_sources_changed(REPO_DIR, bundle_dir=_bundle_dir(), source_root=pathlib.Path(__file__).parent):
_exec_source_launcher(transfer_when_windowed) # refused: this shell starts the next generation
continue
now = time.time()
crash_times.append(now)
crash_times[:] = [stamp for stamp in crash_times if (now - stamp) < CRASH_WINDOW_SEC]
if len(crash_times) >= MAX_CRASH_RESTARTS:
log.error("Agent crashed %d times in %ds. Stopping.", MAX_CRASH_RESTARTS, CRASH_WINDOW_SEC)
break
log.info("Agent crashed. Restarting in 3s...")
time.sleep(3)
def _request_agent_restart() -> None:
"""Recycle the managed server so it adopts freshly saved settings.
The lifecycle loop owns the restart; this flags intent and stops the child.
"""
global _generation_port
if _agent_proc is None: # one published reference: read without the lock, as Panic reads it
# Leaving the flag set would make the NEXT ordinary agent exit look like
# a requested restart and skip the crash fuse.
log.info("No managed agent to recycle; the next start reads the saved settings.")
return
_agent_restart_requested.set()
_generation_port = None # the next generation proves its own endpoint
stop_agent()
def _await_server_ready(port: int, abort_event=None, lifecycle_thread=None) -> tuple[bool, int]:
"""Wait until the lifecycle thread proves the current generation serving; its port.
No clock: the wait lasts while the generation lives and the lifecycle thread runs. It ends
False at once when the child exits before it served (``_agent_exit_before_ready``), when the
lifecycle thread ended (the crash fuse) or on abort. The server may rebind on conflict; every
later consumer (UI URL, onboarding window) uses the endpoint that generation published, never
the requested port or a stale file.
"""
if _external_host_update is not None and lifecycle_thread is not None:
# Native preparation has its own bounded operation before server.py exists.
# Do not spend the HTTP readiness window waiting for a compiler. On shutdown
# the hook observes the event and reaps before this thread can finish.
while lifecycle_thread.is_alive() and _agent_proc is None: # one published reference, read lock-free
lifecycle_thread.join(timeout=0.1)
if _agent_proc is None:
return False, port
while not (abort_event is not None and abort_event.is_set()):
if _generation_port:
return True, _generation_port
if lifecycle_thread is not None and not lifecycle_thread.is_alive():
break
if _agent_exit_before_ready.is_set():
break
time.sleep(0.1)
return False, _generation_port or port
def _load_settings() -> dict:
return load_settings()
def _save_settings(changes: dict) -> None:
# The desktop launcher is the owner-controlled writer. The generic
# config.save_settings ratchet deliberately makes allow_elevation inert
# after boot, which is correct for agent-reachable callers but also made a
# confirmed Cyber Pro selection fall back to Advanced. Reuse the existing
# owner writer so the confirmation is honored while its document lock,
# persistence normalization and other owner-only checks remain in force.
# Only ``changes`` land, on the document as it is under that lock: a merged
# read's defaults, environment values and proposals are no choice.
from ouroboros.gateway.owner_settings import _owner_update_settings, fields_on_saved_document
_owner_update_settings(fields_on_saved_document(changes))
def _request_runtime_mode_change(mode: str, confirm_fn) -> dict:
new_mode = normalize_runtime_mode(mode)
settings = _load_settings()
pending_mode = normalize_runtime_mode(settings.get("OUROBOROS_RUNTIME_MODE"))
active_mode = get_runtime_mode()
restart_required = new_mode != active_mode
if new_mode == pending_mode:
return {"ok": True, "runtime_mode": new_mode, "restart_required": restart_required}
message = (
f"Change Ouroboros runtime mode from {pending_mode} to {new_mode}?\n\n"
f"Current boot is still running in {active_mode} mode. "
"This is an owner-only operation. The new mode is saved by the "
"desktop launcher and takes effect after restart."
)
if not confirm_fn("Confirm Runtime Mode Change", message):
return {"ok": False, "error": "Runtime mode change cancelled."}
_save_settings({"OUROBOROS_RUNTIME_MODE": new_mode})
return {"ok": True, "runtime_mode": new_mode, "restart_required": restart_required}
def _request_auto_grant_reviewed_skills_change(enabled: bool, confirm_fn) -> dict:
settings = _load_settings()
old_enabled = str(settings.get("OUROBOROS_AUTO_GRANT_REVIEWED_SKILLS") or "false").strip().lower() in {"1", "true", "yes", "on"}
new_enabled = bool(enabled)
if new_enabled == old_enabled:
return {"ok": True, "enabled": new_enabled}
if new_enabled:
message = (
"Enable auto-grant for reviewed skills?\n\n"
"After this, any fresh executable skill review will grant the "
"skill's manifest-declared settings keys and host permissions for "
"that exact content hash. Only enable this for trusted closed-loop "
"skill development."
)
else:
message = "Disable auto-grant for reviewed skills?"
if not confirm_fn("Confirm Reviewed Skill Auto-Grant", message):
return {"ok": False, "error": "Auto-grant setting change cancelled."}
_save_settings({"OUROBOROS_AUTO_GRANT_REVIEWED_SKILLS": "true" if new_enabled else "false"})
return {"ok": True, "enabled": new_enabled}
def _request_skill_key_grant(skill: str, keys: list, confirm_fn, *, enable_after_grant: bool = False,
allow_pending: bool = False) -> dict:
from ouroboros.config import runtime_setting, SETTINGS_DEFAULTS
from ouroboros.skill_loader import find_skill
from ouroboros.skill_lifecycle_actions import prepare_skill_grant
skill_name = str(skill or "").strip()
items = [str(key or "").strip() for key in (keys or []) if str(key or "").strip()]
repo_path = str(_load_settings().get("OUROBOROS_SKILLS_REPO_PATH") or "")
loaded = find_skill(DATA_DIR, skill_name, repo_path=repo_path)
if loaded is None:
return {"ok": False, "error": f"Skill {skill_name!r} not found"}
preview = prepare_skill_grant(loaded, DATA_DIR, items)
if preview.get("error"):
return preview
message = (
f"Grant skill {loaded.name!r} access to these settings keys / host permissions?\n\n"
+ "\n".join([*preview["keys"], *preview["permissions"]])
+ ("\n\nThen turn this skill on." if enable_after_grant else "")
+ "\n\nOnly grant keys and permissions to reviewed skills you trust."
)
if not confirm_fn("Confirm Skill Grant", message):
return {"ok": False, "error": "Skill grant cancelled."}
# The immutable launcher never imports, registers or installs packages for a skill: after its
# native confirm the server's grant owner revalidates the revision confirmed above, saves the
# grant, turns the skill on when ready and readies its packages (the same POST the browser sends).
actual_port = _read_port_file() or AGENT_SERVER_PORT
req = urllib.request.Request(
f"http://127.0.0.1:{actual_port}/api/skills/{urllib.parse.quote(loaded.name)}/grants", method="POST",
data=json.dumps({"items": items, "expected_content_hash": preview["content_hash"],
**({"enable_after_grant": True} if enable_after_grant else {})}).encode("utf-8"),
headers={"Content-Type": "application/json", **({"Prefer": "respond-async"} if allow_pending else {})},
)
try:
timeout = 20 if allow_pending else float(runtime_setting(
"OUROBOROS_SKILL_LIFECYCLE_TIMEOUT_SEC", SETTINGS_DEFAULTS["OUROBOROS_SKILL_LIFECYCLE_TIMEOUT_SEC"]))
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8") or "{}")
except urllib.error.HTTPError as exc: # a typed refusal is the server's answer, not a transport failure
try:
return json.loads(exc.read().decode("utf-8") or "{}") or {"ok": False, "error": f"HTTP {exc.code}"}
except ValueError:
return {"ok": False, "error": f"HTTP {exc.code}"}
except (OSError, ValueError) as exc:
return {"ok": False, "error": f"The server did not answer the grant: {exc}"}
def _webview_ready() -> tuple[bool, str]:
"""Probe whether pywebview can actually run a GUI window here.
Lazy backend selection lets import succeed without GTK/QT (#56). initialize()
uses PYWEBVIEW_GUI/session selection and is safe to repeat at start(). Linux
also checks `_display_ready`; macOS/Windows stay import-only and `_detect_headless` skips them.
"""
if IS_LINUX and not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
# BEFORE initialize(): even SELECTING a Qt backend constructs a
# QGuiApplication, which can ABORT on a display-less box.
return False, "no DISPLAY or WAYLAND_DISPLAY in the environment"
try:
if IS_LINUX:
from ouroboros.desktop_media import prepare_linux_bundle
prepare_linux_bundle(_bundle_dir())
import webview # noqa: F401 (can itself fail on bare source checkouts)
from webview.guilib import initialize