Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions binary/tests/test_frozen_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import importlib.util
import json
import sys
import types

import pytest

Expand Down Expand Up @@ -129,3 +131,72 @@ def test_frozen_discovery_missing_binary_skips_without_exec(frozen_module, monke
monkeypatch.setattr(m, "FROZEN_DISCOVERY_BIN", str(REPO / "does-not-exist"))
m._dispatch_discovery()
assert calls == [], "must not exec (or fall back to download) when binary missing"


def _load_discovery_entry():
spec = importlib.util.spec_from_file_location(
"unbound_discovery_entry", REPO / "packaging" / "unbound_discovery_entry.py"
)
entry = importlib.util.module_from_spec(spec)
spec.loader.exec_module(entry)
return entry


def _install_fake_memory_fix_helper(monkeypatch, helper_main):
for name in ("coding_discovery_tools", "coding_discovery_tools.memory_fix_helper"):
monkeypatch.delitem(sys.modules, name, raising=False)
package = types.ModuleType("coding_discovery_tools")
package.__path__ = []
helper = types.ModuleType("coding_discovery_tools.memory_fix_helper")
helper.main = helper_main
monkeypatch.setitem(sys.modules, "coding_discovery_tools", package)
monkeypatch.setitem(sys.modules, "coding_discovery_tools.memory_fix_helper", helper)


def test_entry_memory_fix_runs_helper_and_strips_token(monkeypatch):
seen_argv = []

def helper_main():
seen_argv.append(list(sys.argv))
return 2

_install_fake_memory_fix_helper(monkeypatch, helper_main)
monkeypatch.setattr(sys, "argv", ["unbound-discovery", "memory-fix", "--extra"])
assert _load_discovery_entry().main() == 2
assert seen_argv == [["unbound-discovery", "--extra"]]


def test_entry_memory_fix_runs_without_any_config(monkeypatch, capsys):
monkeypatch.delenv("UNBOUND_API_KEY", raising=False)
calls = []
_install_fake_memory_fix_helper(monkeypatch, lambda: calls.append(1) or 0)
monkeypatch.setattr(sys, "argv", ["unbound-discovery", "memory-fix"])
assert _load_discovery_entry().main() == 0
assert calls == [1], "memory-fix must not fall into the no-config idle branch"
assert capsys.readouterr().out == ""


def test_entry_memory_fix_missing_helper_exits_3_with_empty_stdout(monkeypatch, capsys):
monkeypatch.delitem(sys.modules, "coding_discovery_tools.memory_fix_helper", raising=False)
monkeypatch.setitem(sys.modules, "coding_discovery_tools", types.ModuleType("coding_discovery_tools"))
monkeypatch.setattr(sys, "argv", ["unbound-discovery", "memory-fix"])
assert _load_discovery_entry().main() == 3
assert capsys.readouterr().out == ""


def test_entry_memory_fix_broken_helper_import_exits_1_not_3(monkeypatch, capsys):
_install_fake_memory_fix_helper(monkeypatch, lambda: 0)
monkeypatch.delattr(sys.modules["coding_discovery_tools.memory_fix_helper"], "main")
monkeypatch.setattr(sys, "argv", ["unbound-discovery", "memory-fix"])
assert _load_discovery_entry().main() == 1
assert capsys.readouterr().out == ""


def test_entry_memory_fix_helper_crash_exits_1(monkeypatch, capsys):
def helper_main():
raise RuntimeError("boom")

_install_fake_memory_fix_helper(monkeypatch, helper_main)
monkeypatch.setattr(sys, "argv", ["unbound-discovery", "memory-fix"])
assert _load_discovery_entry().main() == 1
assert capsys.readouterr().out == ""
23 changes: 23 additions & 0 deletions packaging/unbound_discovery_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
Subcommand routing mirrors upstream install.sh at SOURCE_REF: a leading
"mcp-scan" runs the on-demand single-server scan (scan_single_mcp_server,
used by the agent hooks when the gateway reports an unknown MCP server);
a leading "memory-fix" runs the keyless memory-file fixer (memory_fix_helper)
that the agent re-launches itself into as the file's owner, speaking JSON on
stdin/stdout, so that route never writes to stdout itself;
anything else runs the full discovery sweep. Everything else (argument
semantics, detection, reporting via curl) is the upstream script, unchanged.
"""
Expand Down Expand Up @@ -90,6 +93,26 @@ def main():
_log_crash()
return 1

if argv and argv[0] == "memory-fix":
sys.argv = [sys.argv[0]] + argv[1:]
try:
from coding_discovery_tools.memory_fix_helper import main as memory_fix_main
except ModuleNotFoundError as missing:
# Only the helper itself being absent means "this bundle has no helper"; anything else is a crash.
if missing.name not in ("coding_discovery_tools", "coding_discovery_tools.memory_fix_helper"):
_log_crash()
return 1
return 3
except ImportError:
_log_crash()
return 1

try:
return memory_fix_main()
except Exception:
_log_crash()
return 1

if "-h" in argv or "--help" in argv:
from coding_discovery_tools.ai_tools_discovery import main as discovery_main

Expand Down