From 50d4a8e77093ef32a00ffdd578ec960ad715b13d Mon Sep 17 00:00:00 2001 From: Nanda Pranesh Date: Fri, 27 Nov 2026 14:50:46 +0530 Subject: [PATCH] Route memory-fix in the discovery binary to the agent's keyless file fixer (WEB-6954) Co-Authored-By: Claude Fable 5.2 --- binary/tests/test_frozen_contract.py | 71 ++++++++++++++++++++++++++++ packaging/unbound_discovery_entry.py | 23 +++++++++ 2 files changed, 94 insertions(+) diff --git a/binary/tests/test_frozen_contract.py b/binary/tests/test_frozen_contract.py index 0de2f89bb..4584eccef 100644 --- a/binary/tests/test_frozen_contract.py +++ b/binary/tests/test_frozen_contract.py @@ -14,6 +14,8 @@ import importlib.util import json +import sys +import types import pytest @@ -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 == "" diff --git a/packaging/unbound_discovery_entry.py b/packaging/unbound_discovery_entry.py index d116eed3e..71fd49648 100644 --- a/packaging/unbound_discovery_entry.py +++ b/packaging/unbound_discovery_entry.py @@ -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. """ @@ -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