Return (and lazily create) the process-global VisualizerRuntime singleton. The runtime is enabled when `MASFACTORY_VISUALIZER_PORT` is set. Host and mode are read from environment variables. When unavailable, this returns None.
()
| 803 | |
| 804 | |
| 805 | def get_visualizer_runtime() -> VisualizerRuntime | None: |
| 806 | """Return (and lazily create) the process-global VisualizerRuntime singleton. |
| 807 | |
| 808 | The runtime is enabled when `MASFACTORY_VISUALIZER_PORT` is set. Host and mode are read |
| 809 | from environment variables. When unavailable, this returns None. |
| 810 | """ |
| 811 | global _RUNTIME_SINGLETON |
| 812 | if not VISUALIZER_ENABLED: |
| 813 | return None |
| 814 | if _RUNTIME_SINGLETON is not None: |
| 815 | return _RUNTIME_SINGLETON |
| 816 | |
| 817 | host = os.environ.get("MASFACTORY_VISUALIZER_HOST") or "127.0.0.1" |
| 818 | port = _as_int(os.environ.get("MASFACTORY_VISUALIZER_PORT")) |
| 819 | if port is None: |
| 820 | return None |
| 821 | mode = _as_str(os.environ.get("MASFACTORY_VISUALIZER_MODE")) |
| 822 | if not mode: |
| 823 | # Best-effort: if we're running under VS Code Python debugging (debugpy), |
| 824 | # treat this as debug mode even when env injection is unavailable. |
| 825 | try: |
| 826 | import sys |
| 827 | |
| 828 | trace = sys.gettrace() |
| 829 | mod = getattr(trace, "__module__", "") if trace is not None else "" |
| 830 | if trace is not None and isinstance(mod, str): |
| 831 | lowered = mod.lower() |
| 832 | if "debugpy" in lowered or "pydevd" in lowered or "ptvsd" in lowered: |
| 833 | mode = "debug" |
| 834 | except Exception: |
| 835 | mode = None |
| 836 | mode = mode or "run" |
| 837 | _RUNTIME_SINGLETON = VisualizerRuntime(host=host, port=port, mode=mode) |
| 838 | return _RUNTIME_SINGLETON |
no test coverage detected