Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

perf(a2a): defer gateway service imports on agent startup

de4d9a5a06a1654
diff --git a/jiuwenswarm/gateway/a2a_manager/__init__.py b/jiuwenswarm/gateway/a2a_manager/__init__.py
index 3c1ad2c03d..7fa4e530a3 100644
--- a/jiuwenswarm/gateway/a2a_manager/__init__.py
+++ b/jiuwenswarm/gateway/a2a_manager/__init__.py
@@ -1,16 +1,49 @@
"""A2A ingress lifecycle, configuration persistence, and management API types."""
-from .config import (
- A2AIngressConfigRepository,
- A2AOutboundSettingsRepository,
- load_a2a_ingress_config,
- load_a2a_ingress_config_safely,
-)
-from .manager import A2AManager
-from .models import A2AIngressConfig, A2AIngressError, A2AIngressSnapshot, A2AIngressState
+from __future__ import annotations
+
+from importlib import import_module
+from typing import TYPE_CHECKING, Any
+
+if TYPE_CHECKING:
+ from .config import (
+ A2AIngressConfigRepository,
+ A2AOutboundSettingsRepository,
+ load_a2a_ingress_config,
+ load_a2a_ingress_config_safely,
+ )
+ from .manager import A2AManager
+ from .models import A2AIngressConfig, A2AIngressError, A2AIngressSnapshot, A2AIngressState
+
+# AgentServer imports tool_rpc and outbound errors through this package. Keep
+# Gateway service implementations unloaded until a caller requests their exports.
+_EXPORTS = {
+ "A2AIngressConfig": ".models",
+ "A2AIngressConfigRepository": ".config",
+ "A2AIngressError": ".models",
+ "A2AIngressSnapshot": ".models",
+ "A2AIngressState": ".models",
+ "A2AManager": ".manager",
+ "A2AOutboundSettingsRepository": ".config",
+ "load_a2a_ingress_config": ".config",
+ "load_a2a_ingress_config_safely": ".config",
+}
__all__ = [
"A2AIngressConfig", "A2AIngressConfigRepository", "A2AIngressError", "A2AIngressSnapshot",
"A2AIngressState", "A2AManager", "A2AOutboundSettingsRepository",
"load_a2a_ingress_config", "load_a2a_ingress_config_safely",
]
+
+
+def __getattr__(name: str) -> Any:
+ module_name = _EXPORTS.get(name)
+ if module_name is None:
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+ value = getattr(import_module(module_name, __name__), name)
+ globals()[name] = value
+ return value
+
+
+def __dir__() -> list[str]:
+ return sorted(set(globals()) | set(__all__))
diff --git a/jiuwenswarm/gateway/a2a_manager/outbound/__init__.py b/jiuwenswarm/gateway/a2a_manager/outbound/__init__.py
index fd91e02d8a..a1404ccc8c 100644
--- a/jiuwenswarm/gateway/a2a_manager/outbound/__init__.py
+++ b/jiuwenswarm/gateway/a2a_manager/outbound/__init__.py
@@ -1,28 +1,62 @@
"""A2A outbound domain models and persistence contracts."""
-from .credentials import A2AOutboundCredentialStore
-from .discovery import A2AOutboundDiscoveryService, DiscoveredCard
-from .dispatcher import A2AOutboundDispatcher
-from .enterprise import EnterpriseA2AAgentView, EnterpriseA2AProjection
-from .errors import A2AOutboundError, A2AOutboundErrorCode, safe_error_summary
-from .models import (
- A2ACompatibleInterface,
- A2ADiscoveredAgent,
- A2AOutboundAgent,
- A2AOutboundAvailability,
- A2AOutboundDiscovery,
- A2AOutboundDispatch,
- A2AOutboundDispatchMode,
- A2AOutboundDispatchStatus,
- TERMINAL_DISPATCH_STATUSES,
-)
-from .repository import (
- A2A_OUTBOUND_AGENT_STORE_NAME,
- A2A_OUTBOUND_DISPATCH_STORE_NAME,
- A2AOutboundRepository,
- JsonA2AOutboundRecordCodec,
-)
-from .registry import A2AOutboundRegistry
+from __future__ import annotations
+
+from importlib import import_module
+from typing import TYPE_CHECKING, Any
+
+if TYPE_CHECKING:
+ from .credentials import A2AOutboundCredentialStore
+ from .discovery import A2AOutboundDiscoveryService, DiscoveredCard
+ from .dispatcher import A2AOutboundDispatcher
+ from .enterprise import EnterpriseA2AAgentView, EnterpriseA2AProjection
+ from .errors import A2AOutboundError, A2AOutboundErrorCode, safe_error_summary
+ from .models import (
+ A2ACompatibleInterface,
+ A2ADiscoveredAgent,
+ A2AOutboundAgent,
+ A2AOutboundAvailability,
+ A2AOutboundDiscovery,
+ A2AOutboundDispatch,
+ A2AOutboundDispatchMode,
+ A2AOutboundDispatchStatus,
+ TERMINAL_DISPATCH_STATUSES,
+ )
+ from .repository import (
+ A2A_OUTBOUND_AGENT_STORE_NAME,
+ A2A_OUTBOUND_DISPATCH_STORE_NAME,
+ A2AOutboundRepository,
+ JsonA2AOutboundRecordCodec,
+ )
+ from .registry import A2AOutboundRegistry
+
+# Error handling and tool metadata must not eagerly load discovery/dispatch SDKs.
+# Preserve the package API while resolving each implementation on first access.
+_EXPORTS = {
+ "A2ACompatibleInterface": ".models",
+ "A2ADiscoveredAgent": ".models",
+ "A2AOutboundAgent": ".models",
+ "A2AOutboundAvailability": ".models",
+ "A2AOutboundCredentialStore": ".credentials",
+ "A2AOutboundDiscoveryService": ".discovery",
+ "A2AOutboundDispatcher": ".dispatcher",
+ "EnterpriseA2AAgentView": ".enterprise",
+ "EnterpriseA2AProjection": ".enterprise",
+ "A2AOutboundRegistry": ".registry",
+ "A2AOutboundDiscovery": ".models",
+ "A2AOutboundDispatch": ".models",
+ "A2AOutboundDispatchMode": ".models",
+ "A2AOutboundDispatchStatus": ".models",
+ "A2AOutboundError": ".errors",
+ "A2AOutboundErrorCode": ".errors",
+ "DiscoveredCard": ".discovery",
+ "A2AOutboundRepository": ".repository",
+ "A2A_OUTBOUND_AGENT_STORE_NAME": ".repository",
+ "A2A_OUTBOUND_DISPATCH_STORE_NAME": ".repository",
+ "JsonA2AOutboundRecordCodec": ".repository",
+ "TERMINAL_DISPATCH_STATUSES": ".models",
+ "safe_error_summary": ".errors",
+}
__all__ = [
"A2ACompatibleInterface",
@@ -49,3 +83,16 @@ __all__ = [
"TERMINAL_DISPATCH_STATUSES",
"safe_error_summary",
]
+
+
+def __getattr__(name: str) -> Any:
+ module_name = _EXPORTS.get(name)
+ if module_name is None:
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+ value = getattr(import_module(module_name, __name__), name)
+ globals()[name] = value
+ return value
+
+
+def __dir__() -> list[str]:
+ return sorted(set(globals()) | set(__all__))
diff --git a/tests/unit_tests/gateway/test_a2a_import_boundaries.py b/tests/unit_tests/gateway/test_a2a_import_boundaries.py
new file mode 100644
index 0000000000..6a7e9af529
--- /dev/null
+++ b/tests/unit_tests/gateway/test_a2a_import_boundaries.py
@@ -0,0 +1,188 @@
+"""Keep Agent-side A2A contracts independent of Gateway service startup."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+import subprocess
+import sys
+import textwrap
+
+import pytest
+
+
+_REPO_ROOT = Path(__file__).resolve().parents[3]
+
+
+def _run_fresh_python(tmp_path: Path, code: str) -> None:
+ """Use a fresh interpreter so pytest collection cannot hide eager imports."""
+ runtime = tmp_path / "runtime"
+ config = runtime / "config"
+ config.mkdir(parents=True)
+ (config / "config.yaml").write_text("{}\n", encoding="utf-8")
+ (config / ".env").write_text("", encoding="utf-8")
+ env = os.environ.copy()
+ env.update(
+ PYTHONIOENCODING="utf-8",
+ PYTHONDONTWRITEBYTECODE="1",
+ JIUWENSWARM_DATA_DIR=str(runtime),
+ JIUWENCLAW_DATA_DIR=str(runtime),
+ JIUWENSWARM_CONFIG_DIR=str(config),
+ JIUWENSWARM_HOME=str(runtime / "home"),
+ JIUWENSWARM_EDITION="personal",
+ JIUWENSWARM_LOG_CONSOLE_ENABLED="false",
+ JIUWENSWARM_LOG_FILE_ENABLED="false",
+ OTEL_SDK_DISABLED="true",
+ )
+ script = (
+ f"import sys\nsys.path.insert(0, {str(_REPO_ROOT)!r})\n"
+ + textwrap.dedent(code)
+ )
+ result = subprocess.run(
+ [sys.executable, "-c", script],
+ cwd=tmp_path,
+ env=env,
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ timeout=90,
+ check=False,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+ "import_code",
+ [
+ "import jiuwenswarm.gateway.a2a_manager",
+ "import jiuwenswarm.gateway.a2a_manager.outbound",
+ """
+from jiuwenswarm.gateway.a2a_manager.outbound import (
+ A2AOutboundErrorCode, safe_error_summary,
+)
+from jiuwenswarm.gateway.a2a_manager.tool_rpc import A2A_TOOL_FIND_AGENTS
+assert A2A_TOOL_FIND_AGENTS == "a2a.outbound.tool.find_agents"
+assert safe_error_summary(A2AOutboundErrorCode.MANAGER_UNAVAILABLE)
+""",
+ """
+from jiuwenswarm.agents.harness.common.rails.a2a_outbound_toolkit_rail import (
+ A2AOutboundToolkitRail,
+)
+from jiuwenswarm.agents.harness.common.tools.a2a_outbound_tools import (
+ A2AOutboundToolkit,
+)
+class OfflineBackend:
+ ready = False
+tools = A2AOutboundToolkit(OfflineBackend()).get_tools()
+assert [tool.card.name for tool in tools] == [
+ "a2a_find_agents", "a2a_dispatch_task", "a2a_get_dispatch",
+]
+A2AOutboundToolkitRail()
+""",
+ ],
+ ids=["ingress-package", "outbound-package", "agent-contracts", "agent-tool-cards"],
+)
+def test_agent_imports_do_not_load_gateway_a2a_services(
+ tmp_path: Path, import_code: str,
+) -> None:
+ _run_fresh_python(
+ tmp_path,
+ import_code + """
+heavy_prefixes = (
+ "jiuwenswarm.gateway.a2a_manager.manager",
+ "jiuwenswarm.gateway.a2a_manager.outbound.discovery",
+ "jiuwenswarm.gateway.a2a_manager.outbound.dispatcher",
+ "jiuwenswarm.gateway.channel_manager.protocol.a2a.a2a_connect",
+ "a2a.server",
+)
+loaded = sorted(
+ name for name in sys.modules
+ if any(name == prefix or name.startswith(prefix + ".") for prefix in heavy_prefixes)
+)
+assert not loaded, f"Agent-side import loaded Gateway A2A services: {loaded}"
+""",
+ )
+
+
+ ("package_name", "expected_exports"),
+ [
+ (
+ "jiuwenswarm.gateway.a2a_manager",
+ {
+ "config": (
+ "A2AIngressConfigRepository", "A2AOutboundSettingsRepository",
+ "load_a2a_ingress_config", "load_a2a_ingress_config_safely",
+ ),
+ "manager": ("A2AManager",),
+ "models": (
+ "A2AIngressConfig", "A2AIngressError", "A2AIngressSnapshot", "A2AIngressState",
+ ),
+ },
+ ),
+ (
+ "jiuwenswarm.gateway.a2a_manager.outbound",
+ {
+ "credentials": ("A2AOutboundCredentialStore",),
+ "discovery": ("A2AOutboundDiscoveryService", "DiscoveredCard"),
+ "dispatcher": ("A2AOutboundDispatcher",),
+ "enterprise": ("EnterpriseA2AAgentView", "EnterpriseA2AProjection"),
+ "errors": ("A2AOutboundError", "A2AOutboundErrorCode", "safe_error_summary"),
+ "models": (
+ "A2ACompatibleInterface", "A2ADiscoveredAgent", "A2AOutboundAgent",
+ "A2AOutboundAvailability", "A2AOutboundDiscovery", "A2AOutboundDispatch",
+ "A2AOutboundDispatchMode", "A2AOutboundDispatchStatus", "TERMINAL_DISPATCH_STATUSES",
+ ),
+ "repository": (
+ "A2A_OUTBOUND_AGENT_STORE_NAME", "A2A_OUTBOUND_DISPATCH_STORE_NAME",
+ "A2AOutboundRepository", "JsonA2AOutboundRecordCodec",
+ ),
+ "registry": ("A2AOutboundRegistry",),
+ },
+ ),
+ ],
+ ids=["ingress-exports", "outbound-exports"],
+)
+def test_public_exports_keep_identity_and_star_import_compatibility(
+ tmp_path: Path, package_name: str, expected_exports: dict[str, tuple[str, ...]],
+) -> None:
+ _run_fresh_python(
+ tmp_path,
+ f"""
+from importlib import import_module
+package_name = {package_name!r}
+expected_exports = {expected_exports!r}
+package = import_module(package_name)
+expected_names = {{name for names in expected_exports.values() for name in names}}
+assert set(package.__all__) == expected_names
+assert expected_names <= set(dir(package))
+assert not hasattr(package, "not_an_a2a_export")
+namespace = {{}}
+exec(f"from {{package_name}} import *", namespace)
+for module_name, names in expected_exports.items():
+ module = import_module(f"{{package_name}}.{{module_name}}")
+ for name in names:
+ original = getattr(module, name)
+ assert getattr(package, name) is original, name
+ assert namespace[name] is original, name
+""",
+ )
+
+
+def test_concurrent_first_manager_access_keeps_one_class_identity(tmp_path: Path) -> None:
+ _run_fresh_python(
+ tmp_path,
+ """
+from concurrent.futures import ThreadPoolExecutor
+from threading import Barrier
+import jiuwenswarm.gateway.a2a_manager as package
+barrier = Barrier(4)
+def resolve(_):
+ barrier.wait(timeout=30)
+ return package.A2AManager
+with ThreadPoolExecutor(max_workers=4) as pool:
+ managers = list(pool.map(resolve, range(4)))
+from jiuwenswarm.gateway.a2a_manager.manager import A2AManager
+assert all(manager is A2AManager for manager in managers)
+""",
+ )