From fc69055a323d74e46634007aecfab9341c876036 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Wed, 16 Sep 2026 20:43:50 +0200 Subject: [PATCH] feat(cpg): add an explicit environment switch for the engine session (M2 11.3) A measured run needs to enable the transport without changing any default. The switch reads as enabled only for the exact value "1"; unset, empty, "0", "true" and anything else leave the disposable path in place, and an explicit constructor argument still wins in both directions. 1,396 tests passed, nine skipped. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01EmM1j46qgKMh3zqD1QMwEo --- src/openultrasast/cpg/backend.py | 6 +++++- tests/test_cpg_session.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/openultrasast/cpg/backend.py b/src/openultrasast/cpg/backend.py index dba40d8..b70c689 100644 --- a/src/openultrasast/cpg/backend.py +++ b/src/openultrasast/cpg/backend.py @@ -58,6 +58,8 @@ QUERY_TIMEOUT_ENV = "OPENULTRASAST_CPG_QUERY_TIMEOUT" # first repository measurements unreproducible: runs were killed under memory pressure at different # points, so the numbers described the host, not the engine. Bound it, and let the operator raise it. CPG_HEAP_MB = 2048 +# Explicit opt-in for the transaction-owned engine session (M2). Unset means the disposable path. +SESSION_ENV = "OUSAST_ENGINE_SESSION" HEAP_ENV = "OPENULTRASAST_CPG_HEAP_MB" # `joern-parse` detects the language and then shells out to a frontend, and on a large tree it can fail where @@ -324,7 +326,9 @@ class JoernBackend: # M2 11.3: opt-in transport. Off by default, so shipped behaviour stays the disposable # invocation this project has measured. A session that cannot answer falls back to it rather # than reporting a zero, and what a session returns is the same payload or nothing at all. - session_transport: bool = False + # The environment switch exists so a measured run can enable it without changing any default; + # anything other than "1" leaves it off. + session_transport: bool = field(default_factory=lambda: os.environ.get(SESSION_ENV, "").strip() == "1") _diagnostics: list[str] = field(default_factory=list, init=False, repr=False) _scratch: list[Path] = field(default_factory=list, init=False, repr=False) _cancel_deadline: float | None = field(default=None, init=False, repr=False) diff --git a/tests/test_cpg_session.py b/tests/test_cpg_session.py index d17ecdd..e758d3f 100644 --- a/tests/test_cpg_session.py +++ b/tests/test_cpg_session.py @@ -263,3 +263,18 @@ def _completed(stdout): import subprocess return subprocess.CompletedProcess(["joern"], 0, stdout, "") + + +def test_the_session_switch_is_explicit_and_defaults_off(monkeypatch): + """M2 11.3: a measured run can enable the transport without changing any default.""" + from openultrasast.cpg.backend import SESSION_ENV, JoernBackend + + monkeypatch.delenv(SESSION_ENV, raising=False) + assert JoernBackend().session_transport is False + for value in ("", "0", "true", "yes", "2"): + monkeypatch.setenv(SESSION_ENV, value) + assert JoernBackend().session_transport is False, value + monkeypatch.setenv(SESSION_ENV, "1") + assert JoernBackend().session_transport is True + # An explicit argument still wins over the environment in both directions. + assert JoernBackend(session_transport=False).session_transport is False