Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(cli): keep config set values that a number would rewrite

Maohao9e5b243d2513
diff --git a/cli/src/opensandbox_cli/commands/config_cmd.py b/cli/src/opensandbox_cli/commands/config_cmd.py
index 24b388169..0958759f0 100644
--- a/cli/src/opensandbox_cli/commands/config_cmd.py
+++ b/cli/src/opensandbox_cli/commands/config_cmd.py
@@ -111,20 +111,20 @@ def _parse_toml_value(raw: str) -> Any:
Booleans, integers, and floats are recognized so values like ``false`` or
``30`` keep their native TOML type; anything else is stored as a string.
- The value is always serialized by tomlkit, so quotes, escapes, and
- whitespace in string values are encoded correctly.
+ A number that would not write back as the same text (``007123``,
+ ``1e10``, ``NaN``) stays a string, so values such as API keys are kept
+ as typed. The value is always serialized by tomlkit, so quotes, escapes,
+ and whitespace in string values are encoded correctly.
"""
normalized = raw.lower()
if normalized in ("true", "false"):
return normalized == "true"
- try:
- return int(raw)
- except ValueError:
- pass
- try:
- return float(raw)
- except ValueError:
- pass
+ for parse in (int, float):
+ try:
+ number = parse(raw)
+ except ValueError:
+ continue
+ return number if str(number) == raw else raw
return raw
diff --git a/cli/tests/test_commands.py b/cli/tests/test_commands.py
index 39f11309c..f5eeab424 100644
--- a/cli/tests/test_commands.py
+++ b/cli/tests/test_commands.py
@@ -52,7 +52,7 @@ from opensandbox.models.templates import (
)
from opensandbox_cli.commands.terminal import _make_stdin_reader, stream_pty
-from opensandbox_cli.config import load_config_file
+from opensandbox_cli.config import load_config_file, resolve_config
from opensandbox_cli.main import cli
from opensandbox_cli.output import OutputFormatter
@@ -274,6 +274,27 @@ class TestConfigSet:
assert parsed["connection"]["request_timeout"] == 45
assert parsed["connection"]["use_server_proxy"] is True
+ @pytest.mark.parametrize("value", ["007123", "1e10", "NaN", "1_000", "1.50"])
+ def test_set_keeps_text_a_number_would_change(
+ self,
+ runner: CliRunner,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ value: str,
+ ) -> None:
+ monkeypatch.delenv("OPEN_SANDBOX_API_KEY", raising=False)
+ cfg_path = tmp_path / "config.toml"
+ cfg_path.write_text("[connection]\n")
+
+ result = runner.invoke(
+ cli,
+ ["--config", str(cfg_path), "config", "set", "connection.api_key", value],
+ )
+
+ assert result.exit_code == 0
+ assert load_config_file(cfg_path)["connection"]["api_key"] == value
+ assert resolve_config(config_path=cfg_path)["api_key"] == value
+
def test_set_reports_invalid_existing_toml(
self, runner: CliRunner, tmp_path: Path
) -> None: