From 4fd5ae9aac91125cea271a90b8a14605a3b86ffe Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 14:42:47 +0000 Subject: [PATCH] fix(nuitka): point the hook's sys.executable at its own binary Nuitka standalone leaves sys.executable at /python, so every detached re-launch the hooks start (sync-skills, mcp-diagnostic, refresh-claude-ai-connectors) failed and org skills never synced. The smoke test now fails a build whose SessionStart cannot re-launch sync-skills. --- binary/tests/test_frozen_contract.py | 16 ++++++++++++++++ packaging/nuitka/unbound_hook_entry.py | 10 ++++++++-- packaging/scripts/smoke-test.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/binary/tests/test_frozen_contract.py b/binary/tests/test_frozen_contract.py index 3e13c1bcd..ed955f26b 100644 --- a/binary/tests/test_frozen_contract.py +++ b/binary/tests/test_frozen_contract.py @@ -204,6 +204,22 @@ def test_entry_in_a_nuitka_build_is_frozen_and_points_at_its_own_binary(monkeypa assert sys.executable == os.path.join(os.path.abspath(REPO / "packaging"), "unbound-discovery") +def test_hook_entry_in_a_nuitka_build_points_at_its_own_binary(monkeypatch): + monkeypatch.setattr(sys, "frozen", False, raising=False) + monkeypatch.setattr(sys, "_MEIPASS", None, raising=False) + monkeypatch.setattr(sys, "executable", sys.executable) + spec = importlib.util.spec_from_file_location( + "unbound_hook_entry", REPO / "packaging" / "nuitka" / "unbound_hook_entry.py" + ) + entry = importlib.util.module_from_spec(spec) + entry.__compiled__ = object() # what Nuitka defines in a compiled module + spec.loader.exec_module(entry) + bundle = os.path.abspath(REPO / "packaging" / "nuitka") + assert sys.frozen is True + assert sys._MEIPASS == bundle + assert sys.executable == os.path.join(bundle, "unbound-hook") + + @pytest.mark.parametrize("supported, exit_code", [(True, 0), (False, 1)]) def test_entry_memory_fix_selftest_exits_by_the_agents_answer(monkeypatch, supported, exit_code): seen_users = [] diff --git a/packaging/nuitka/unbound_hook_entry.py b/packaging/nuitka/unbound_hook_entry.py index 5d872e976..8baa04a26 100644 --- a/packaging/nuitka/unbound_hook_entry.py +++ b/packaging/nuitka/unbound_hook_entry.py @@ -8,14 +8,18 @@ PyInstaller's bootloader sets sys.frozen and sys._MEIPASS, which unbound_hook._resources.resource_root() uses to locate the vendored hook / -MDM setup sources. Nuitka 2.8 sets neither, so this shim provides both -before unbound_hook is imported: +MDM setup sources, and points sys.executable at the bundle's own binary. +Nuitka 2.8 does none of this, so this shim provides all three before +unbound_hook is imported: * sys.frozen = True -> the vendored modules' frozen-mode gates engage (discovery via the local binary), identical to the PyInstaller artifact. * sys._MEIPASS = -> resource_root() resolves to /vendored/, where build-nuitka.sh places the same data files the .spec's VENDORED list places under PyInstaller's _MEIPASS. + * sys.executable = /unbound-hook -> the hooks' detached re-launches + (sync-skills, mcp-diagnostic, refresh-claude-ai-connectors) start this + binary. Nuitka leaves it at /python, which does not exist. Under Nuitka --standalone the main module's __file__ lives in the dist folder, so its dirname IS the bundle root. Run directly with python for a @@ -29,6 +33,8 @@ if "__compiled__" in globals(): # Nuitka build only; inert under plain python sys.frozen = True sys._MEIPASS = os.path.dirname(os.path.abspath(__file__)) + # Nuitka leaves sys.executable at /python; the hooks re-launch this binary through it. + sys.executable = os.path.join(sys._MEIPASS, "unbound-hook") else: # Dev fallback mirrors binary/src/entry.py: make unbound_hook importable # from a plain repo checkout. diff --git a/packaging/scripts/smoke-test.sh b/packaging/scripts/smoke-test.sh index 2457410f5..6f0034dcd 100755 --- a/packaging/scripts/smoke-test.sh +++ b/packaging/scripts/smoke-test.sh @@ -2,6 +2,7 @@ # Smoke-run both freshly built bundles on the runner (WEB-4789): # * --version on each binary # * a REAL tool+event hook dispatch that proves a vendored module loaded +# * SessionStart re-launches the binary itself (sync-skills) through sys.executable # * if Rosetta is available, repeat --version under forced x86_64 so the # Intel slice is proven to EXECUTE, not just exist in lipo output. # Usage: smoke-test.sh @@ -103,6 +104,31 @@ if grep -qv '^https://127\.0\.0\.1:9/' "$smoke_tmp/curl.log"; then exit 1 fi +# Self re-launch: SessionStart starts a detached ` sync-skills` +# through sys.executable. A bundle whose sys.executable is not runnable +# (Nuitka points it at /python) fails open, so only the child's own +# request proves the re-launch worked: wait for its skills-sync URL in the log. +echo "--- unbound-hook hook claude-code SessionStart (sync-skills re-launch)" +: > "$smoke_tmp/curl.log" +session_start='{"hook_event_name":"SessionStart","source":"startup","session_id":"ci-smoke"}' +out="$(printf '%s' "$session_start" | env -u UNBOUND_GATEWAY_URL -u UNBOUND_DEVICE_TOKEN -u UNBOUND_CLAUDE_API_KEY \ + HOME="$smoke_tmp/home" PATH="$smoke_tmp/bin:/usr/bin:/bin" \ + SMOKE_CURL_LOG="$smoke_tmp/curl.log" "$hook" hook claude-code SessionStart)" +echo "$out" +synced=false +for _ in $(seq 1 30); do + if grep -qxF 'https://127.0.0.1:9/v1/hooks/skills/sync' "$smoke_tmp/curl.log"; then + synced=true + break + fi + sleep 1 +done +cat "$smoke_tmp/curl.log" +if [[ "$synced" != true ]]; then + echo "FAIL: SessionStart did not re-launch '$hook sync-skills', so org skills would never sync on devices." >&2 + exit 1 +fi + if arch -x86_64 /usr/bin/true 2>/dev/null; then echo "--- Rosetta present: re-running --version under x86_64" arch -x86_64 "$hook" --version