Skip to content
Prev Previous commit
Next Next commit
fix(integrations): initialize OpenClaw gateway mode
  • Loading branch information
XnLemon committed Aug 22, 2026
commit ede87c755e9b080eb5e1fc6cf9f0213f8c4a1b0c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def enable(executable: str, endpoint: str, scope_mode: str) -> None:
{"path": "plugins.entries.memory-powercontext.hooks.allowConversationAccess", "value": True},
{"path": "plugins.slots.memory", "value": "memory-powercontext"},
]
if read_config_value(executable, "gateway.mode") is None:
settings.insert(0, {"path": "gateway.mode", "value": "local"})
run_command(
[executable, "config", "set", "--batch-json", json.dumps(settings, separators=(",", ":"))],
timeout=60,
Expand Down Expand Up @@ -187,6 +189,19 @@ def read_tools_allowlist(executable: str) -> list[object]:
return value


def read_config_value(executable: str, path: str) -> object | None:
"""Read a config value, returning ``None`` when the path is absent."""

command = [executable, "config", "get", path, "--json"]
completed = run_command(command, timeout=60, check=False)
if completed.returncode != 0:
return None
try:
return json.loads(completed.stdout or "null")
except json.JSONDecodeError as error:
raise ConfigurationError(f"OpenClaw returned invalid JSON for {path}") from error


def set_tools_allowlist(executable: str, *, add: bool) -> None:
current = read_tools_allowlist(executable)
if add:
Expand Down
15 changes: 15 additions & 0 deletions src/powercontext/cli/openclaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def configure_openclaw(*, executable: str, server_url: str, scope_mode: str) ->
{"path": "plugins.entries.memory-powercontext.hooks.allowConversationAccess", "value": True},
{"path": "plugins.slots.memory", "value": OPENCLAW_PLUGIN_NAME},
]
if read_config_value(executable, "gateway.mode") is None:
settings.insert(0, {"path": "gateway.mode", "value": "local"})
run_openclaw(executable, "config", "set", "--batch-json", json.dumps(settings, separators=(",", ":")))
current = read_tools_allowlist(executable)
merged = list(current)
Expand Down Expand Up @@ -278,6 +280,19 @@ def read_tools_allowlist(executable: str) -> list[object]:
return value


def read_config_value(executable: str, path: str) -> object | None:
"""Read a config value, returning ``None`` when the path is absent."""

command = [executable, "config", "get", path, "--json"]
completed = run_process(command, timeout=60, check=False)
if completed.returncode != 0:
return None
try:
return json.loads(completed.stdout or "null")
except json.JSONDecodeError as error:
raise SetupError.invalid_command_output(command, "invalid JSON") from error


def run_openclaw(executable: str, *arguments: str) -> subprocess.CompletedProcess[str]:
"""Run an OpenClaw command and convert failures to SetupError."""

Expand Down
25 changes: 25 additions & 0 deletions tests/test_openclaw_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ def test_configure_openclaw_preserves_existing_tools_and_adds_missing_tools(monk
assert run_openclaw.call_args_list[-1].args == ("openclaw", "gateway", "restart")


def test_configure_openclaw_initializes_local_gateway_when_mode_is_missing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def read_config(command: list[str], *, timeout: int, check: bool = True) -> CompletedProcess[str]:
del timeout, check
if command[1:4] == ["config", "get", "gateway.mode"]:
return CompletedProcess(command, 1, "", "Config path not found")
if command[1:4] == ["config", "get", "tools.alsoAllow"]:
return CompletedProcess(command, 0, "[]", "")
raise AssertionError(command)

run_openclaw = Mock()
monkeypatch.setattr(openclaw_cli, "run_process", read_config)
monkeypatch.setattr(openclaw_cli, "run_openclaw", run_openclaw)

openclaw_cli.configure_openclaw(
executable="openclaw",
server_url="http://127.0.0.1:8765",
scope_mode="agent",
)

settings = json.loads(run_openclaw.call_args_list[0].args[4])
assert settings[0] == {"path": "gateway.mode", "value": "local"}


def test_install_openclaw_plugin_builds_installs_and_configures(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_openclaw_configure_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ def fake_run(command: list[str], *, timeout: int, check: bool = True) -> Complet
assert state == ["custom_tool"]


def test_enable_initializes_local_gateway_when_mode_is_missing(monkeypatch: pytest.MonkeyPatch) -> None:
commands: list[list[str]] = []

def fake_run(command: list[str], *, timeout: int, check: bool = True) -> CompletedProcess[str]:
del timeout, check
commands.append(command)
if command[1:4] == ["config", "get", "gateway.mode"]:
return CompletedProcess(command, 1, "", "Config path not found")
if command[1:4] == ["config", "get", "tools.alsoAllow"]:
return CompletedProcess(command, 0, "[]", "")
if command[1:3] == ["config", "set"] and command[3] == "tools.alsoAllow":
return CompletedProcess(command, 0, "", "")
if command[1:4] == ["config", "set", "--batch-json"]:
settings = json.loads(command[4])
assert settings[0] == {"path": "gateway.mode", "value": "local"}
return CompletedProcess(command, 0, "", "")
if command[1:3] == ["gateway", "restart"]:
return CompletedProcess(command, 0, "", "")
raise AssertionError(command)

monkeypatch.setattr(configure_openclaw, "run_command", fake_run)
monkeypatch.setenv("OPENCLAW_RESTART", "1")

configure_openclaw.enable("openclaw", "http://127.0.0.1:8765", "agent")

assert commands[0][1:4] == ["config", "get", "gateway.mode"]


@pytest.mark.parametrize(
"value",
[
Expand Down