subagent transcript
session 18e25685 · workflow wf_23df5b1c
delegated task
You are an adversarial verifier for a FreeCAD-wasm code review. A reviewer claims: SEVERITY: high CATEGORY: context-detection FILE: freecad-port/FreeC
▸ Magik
You are an adversarial verifier for a FreeCAD-wasm code review. A reviewer claims:
SEVERITY: high
CATEGORY: context-detection
FILE: freecad-port/FreeCAD/src/3rdParty/coin/src/glue/gl.cpp
SUMMARY: Fix Coin's current-context detection on wasm — this is the ROOT of the reported 'cc_glglue_instance: Error setting up GL context' warning. `coin_gl_current_context()` has no __EMSCRIPTEN__ branch and queries EGL/GLX, which are blind to Qt-wasm's emscripten GL context.
DETAIL/EVIDENCE: The reported warning is gl.cpp:2374-2385 in `cc_glglue_instance` (postwarning text 'Error when setting up the GL context. This can happen if there is no current context...'), draining `glGetError()` at glue-instance setup, followed by the assert at 2391 on `glGetString(GL_VERSION)`. The underlying cause: `coin_gl_current_context()` (gl.cpp:5281-5314) resolves the current context ONLY via `eglGetCurrentContext()` (HAVE_EGL, gl.cpp:5286) or `glXGetCurrentContext()` — there is no `__EMSCRIPTEN__` case. Qt-wasm's QOpenGLWidget makes its WebGL2 context current through emscripten's GL module (`GL.makeContextCurrent`), NOT via `eglMakeCurrent`, so emscripten's EGL emulation returns EGL_NO_CONTEXT. Consequently `check_egl()` (gl.cpp:2249-2290) mis-detects the backend and the assert at gl.cpp:2346 ('Must have a current GL context') sees NULL. The existing `fcWasmSyncGLContext()` (WasmGLFixedFunc.cpp:213-224, called at QuarterWidget.cpp:1006) only syncs the emscripten `GLctx` global so glGetString/glGetError operate on Qt's context during paint — it does NOT make `coin_gl_current_context()` return non-null. Fix: add an `#ifdef __EMSCRIPTEN__` branch to `coin_gl_current_context()` that returns a non-null token when `GL.currentContext`/`GLctx` is set (reuse the same JS source as ffSyncContext via an EM_JS helper), and short-circuit `check_egl()` on wasm. That removes the warning at its root and makes glue setup deterministic.
RECOMMENDATION: Add a wasm branch to coin_gl_current_context() (return non-null when GL.currentContext.GLctx exists) and force COIN_USE_EGL handling in check_egl() on __EMSCRIPTEN__. Verify the 'Error setting up GL context' warning disappears once the context is reported current at glglue-instance time.
Independently verify by reading the actual file(s) and running git/grep as needed (repo at /home/magik6k/lcad-wasm/freecad-port/FreeCAD, toolchain at /opt/toolchains, deploy at /home/magik6k/lcad-wasm/freecad-port/deploy-gui). Default to REFUTED if you cannot confirm the evidence. Adjust severity to what the evidence actually supports.
Fable
thinking
I need to examine the actual source files to verify the claims about these functions and trace where the warnings are coming from.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && sed -n '2240,2400p' src/3rdParty/coin/src/glue/gl.cpp", "description": "Read gl.cpp check_egl and cc_glglue_instance region"}
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && sed -n '5270,5330p' src/3rdParty/coin/src/glue/gl.cpp", "description": "Read coin_gl_current_context region"}
Fable
thinking
I need to verify the reviewer's claims about the code structure and line numbers in gl.cpp, then check whether HAVE_EGL is actually defined in the wasm build since that's crucial to understanding if emscripten's implementation would be affected.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -n \"coin_gl_current_context\\|static void check_egl\\|Must have a current GL context\\|Error when setting up the GL context\\|could not call glGetString\" src/3rdParty/co…
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -rn \"HAVE_EGL\\|HAVE_GLX\\|HAVE_WGL\" src/3rdParty/coin/include/Inventor/C/glue/*.h src/3rdParty/coin/*.h 2>/dev/null | head; echo \"---config---\"; find src/3rdParty/coi…
Fable
thinking
The line numbers are checking out across the different functions, so that's a good sign. Now I need to figure out whether HAVE_EGL is actually defined when building for WebAssembly.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -rn \"HAVE_EGL\\|HAVE_GLX\\|HAVE_WGL\" src/3rdParty/coin/ --include=\"*.h.in\" --include=\"*.h\" --include=\"*.cmake\" --include=\"CMakeLists.txt\" 2>/dev/null | grep -i \…
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -rn \"fcWasmSyncGLContext\\|makeContextCurrent\\|GLctx\\|currentContext\" src/ --include=\"*.cpp\" --include=\"*.h\" 2>/dev/null | grep -i \"wasm\\|sync\\|GLctx\\|makeCont…
Fable
thinking
I'm checking the CMakeLists.txt file around line 236 to understand what condition triggers HAVE_EGL and whether it's related to WebAssembly or Emscripten.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && sed -n '215,245p' src/3rdParty/coin/CMakeLists.txt", "description": "Read HAVE_EGL condition context"}
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && sed -n '200,240p' src/Gui/WasmGLFixedFunc.cpp; echo \"===QuarterWidget 995-1015===\"; sed -n '995,1015p' src/Gui/Quarter/QuarterWidget.cpp", "description": "Read ffSyncContext …
Fable
thinking
I'm checking whether HAVE_EGL is defined in the wasm build by looking at the conditional logic that sets COIN_USE_EGL based on whether HAVE_EGL and HAVE_GLX are present, and then examining how coin_gl_current_context uses these definitions.
The key insight is that the reviewer claims coin_gl_current_context returns null on wasm because there's no __EMSCRIPTEN__ branch, but if Emscripten provides EGL emulation and HAVE_EGL is defined during the build, then eglGetCurrentContext() would be called instead. I need to find the actual config.h generated for the wasm build to determine which preprocessor flags are set.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && find . -name \"config.h\" -path \"*coin*\" 2>/dev/null; find . -name \"setup.h\" -path \"*coin*\" 2>/dev/null; echo \"---search build dirs for HAVE_EGL define---\"; grep -rln \"define …
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && ls deploy-gui 2>/dev/null | head; echo \"---build dirs---\"; find . -maxdepth 2 -type d -name \"build*\" 2>/dev/null; find . -maxdepth 3 -name \"*.cmake\" -path \"*coin*\" 2>/dev/null …
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && ls -la; echo \"===deploy-gui===\"; ls deploy-gui 2>/dev/null | head -30", "description": "List project dir and deploy-gui"}
Fable
⚙ Bash {"command": "find . -name \"config.h\" 2>/dev/null | grep -i coin | head; echo \"---all coin config---\"; find . -path \"*coin*\" -name \"*.h\" 2>/dev/null | xargs grep -l \"HAVE_EGL\" 2>/dev/null | head; echo \"---any build tree---\"; find…
Fable
thinking
I need to figure out which graphics APIs are enabled in the emscripten build by examining the CMake configuration and toolchain settings, since there's no preserved build tree with config.h to reference directly.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -rn \"COIN_BUILD_EGL\\|COIN_BUILD_GLX\\|HAVE_EGL\\|HAVE_GLX\" src/3rdParty/coin/config.h.cmake.in src/3rdParty/coin/*.in 2>/dev/null | head; echo \"===find config template…
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && grep -rn \"COIN_BUILD_EGL\\|COIN_BUILD_GLX\\|EGL\\|GLX\" toolchain/ research/ *.md 2>/dev/null | grep -i \"coin\\|egl\\|glx\" | head -20", "description": "Search docs/toolchain for coi…
Fable
thinking
I'm seeing that the build configuration explicitly disables both GLX and EGL support, which means the HAVE_EGL flag never gets set during the CMake configuration phase. This is a key constraint that affects what graphics capabilities are available.
Looking at the coin_gl_current_context() function, with all the graphics API flags undefined, it simply returns NULL every time on WebAssembly. There's no attempt to query EGL or GLX at all—the function just hits the final return statement unconditionally.
The check_egl() function then sets COIN_USE_EGL to 0 since HAVE_EGL isn't defined, which is expected. But the critical issue is that assertion at line 2346: it expects a valid GL context to ex …
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && cat toolchain/configure-stage2.sh 2>/dev/null | head -60", "description": "Read stage2 configure script"}
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port && grep -rn \"Error setting up\\|Error when setting up\\|Must have a current\\|glglue\\|cc_glglue\\|GL context\" STATUS.md stage-4-webgl-coin-backend.md research/R9*.md 2>/dev/null | head…
Fable
thinking
So the Release build configuration automatically defines NDEBUG, which means those asserts at lines 2346 and 2391 are effectively disabled in the actual compiled binary. This is a critical detail—those assertions won't catch anything at runtime since they're compiled out entirely.
The real source of the warning is the glGetError() loop at lines 2374-2385, not coin_gl_current_context(). The function only feeds into a disabled assert, so adding a wasm-specific branch there wouldn't suppress the warning. The existing fcWasmSyncGLContext call that makes the GL context current is what actually addresses the glGetError and glGetString issues.
Let me search for all the places coin_gl_current_cont …
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -rn \"coin_gl_current_context\" src/ --include=\"*.cpp\" --include=\"*.h\" --include=\"*.c\"", "description": "Find all callers of coin_gl_current_context"}
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && sed -n '2335,2360p' src/3rdParty/coin/src/glue/gl.cpp", "description": "Re-read assert context at 2345"}
Fable
thinking
I'm tracing through the callers of coin_gl_current_context to understand how they handle shader support, but this seems tangential to the actual warning. Let me step back and focus on the core issue: the claim is that coin_gl_current_context lacks an __EMSCRIPTEN__ branch, which is supposedly the root cause of the GL context error. I've established some facts about the build, but I need to verify whether this missing branch is actually what's triggering the warning. The reviewer's proposed fix wouldn't actually eliminate the warning since it's driven by glGetError state, not coin_gl_current_context. The real issue is context currency at glglue setup time, which fcWasmSyncGLContext already ad …
Fable
I have gathered enough evidence. Let me verify one final point about the build's NDEBUG/assert status implied by Release mode.
Fable
⚙ Bash {"command": "cd /home/magik6k/lcad-wasm/freecad-port/FreeCAD && grep -n \"isSupported\\|coin_gl_current_context\" src/3rdParty/coin/src/shaders/SoVertexShader.cpp | head; echo \"---confirm Release flags meaning---\"; grep -rn \"COIN_BUILD_E…
Fable
thinking
I've confirmed the key findings: the build configuration disables EGL and GLX support, which means the context-querying functions are compiled out entirely. The `coin_gl_current_context()` function returns NULL unconditionally rather than actually checking the graphics API state as the documentation suggests.
The warning at line 2377 comes from the `glGetError()` loop itself, not from `coin_gl_current_context()`, so fixing that function wouldn't eliminate the warning. There's also an internal inconsistency—if the assert at 2346 were triggered, execution would abort before reaching 2377, so both can't be observed together.
The claim is refuted: while the function does lack WebAssembly suppo …
Fable
Verification complete. My findings:
**Build configuration (confirmed):** `toolchain/configure-stage2.sh` builds with `-DCMAKE_BUILD_TYPE=Release` and `-DCOIN_BUILD_GLX=OFF -DCOIN_BUILD_EGL=OFF`. In `src/3rdParty/coin/CMakeLists.txt` (line ~234), `HAVE_EGL` is only set when `COIN_BUILD_EGL` is on; likewise `HAVE_GLX` requires `COIN_BUILD_GLX`. So in the wasm build **HAVE_EGL, HAVE_GLX, HAVE_WGL, HAVE_AGL/CGL are all undefined.**
**Consequence for coin_gl_current_context (gl.cpp:5279-5314):** With every backend `#ifdef` compiled out, `ctx` stays `NULL` and the function returns `NULL` unconditionally. It does **not** call `eglGetCurrentContext()` or `glXGetCurrentContext()` — those lines are dead code in this build. The reviewer's evidence that it "queries EGL/GLX blind to emscripten's context" is factually wrong for the actual build.
**The warning is not produced via coin_gl_current_context.** The reported text "Error when setting up the GL context" (gl.cpp:2377) is emitted by the `glGetError()` while-loop at 2374-2385, which never calls `coin_gl_current_context()`. `coin_gl_current_context()` is used only at gl.cpp:2345 (the assert) and in shader `isSupported()` gating (SoVertexShader.cpp:108, etc.). Making it return non-null would **not** touch the glGetError-driven warning path — so the proposed fix would not achieve its stated goal.
**The assert path is compiled out.** Release mode defines `NDEBUG`, so the asserts at gl.cpp:2346 and 2391 (the only place the function's null-ness matters for setup) are no-ops. Additionally the claim is internally inconsistent: if the 2346 assert actually fired on NULL, execution would `abort()` there and never reach the postwarning at 2377 — you cannot observe both a "reported warning" and an assert-on-null as the same symptom.
**What actually addresses the warning** is context currency via `fcWasmSyncGLContext()`/`ffSyncContext()` (WasmGLFixedFunc.cpp:207-224, called from QuarterWidget.cpp:1006), which makes emscripten's `GLctx` current so `glGetError`/`glGetString` operate on Qt's WebGL2 context — exactly the mechanism the reviewer dismisses.
The only true kernel: `coin_gl_current_context()` does return NULL on wasm, which could gate Coin shader support off. But that is unrelated to the reported warning, unverified as an actual problem, and minor.
The central causal claim (root of the warning, fix removes it, EGL/GLX misdetection) is contradicted by the code and build config.
Fable
⚙ StructuredOutput {"verdict": "REFUTED", "reason": "The build (toolchain/configure-stage2.sh) is Release with COIN_BUILD_EGL=OFF and COIN_BUILD_GLX=OFF, so HAVE_EGL/HAVE_GLX/HAVE_WGL are all undefined. coin_gl_current_context() (gl.cpp:5279) therefore return…