Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(cli): keep text only for string config keys, infer types for the rest
Maohao4f1b78ac32f9
diff --git a/cli/src/opensandbox_cli/commands/config_cmd.py b/cli/src/opensandbox_cli/commands/config_cmd.pyindex 0958759f0..0843f13be 100644--- a/cli/src/opensandbox_cli/commands/config_cmd.py+++ b/cli/src/opensandbox_cli/commands/config_cmd.py@@ -26,7 +26,7 @@ from tomlkit.exceptions import ParseErrorfrom tomlkit.items import InlineTable, Tablefrom opensandbox_cli.client import ClientContext-from opensandbox_cli.config import init_config_file, resolve_config+from opensandbox_cli.config import STRING_KEYS, init_config_file, resolve_configfrom opensandbox_cli.utils import handle_errors, output_option, prepare_output@@ -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.- 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.+ 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"- for parse in (int, float):- try:- number = parse(raw)- except ValueError:- continue- return number if str(number) == raw else raw+ try:+ return int(raw)+ except ValueError:+ pass+ try:+ return float(raw)+ except ValueError:+ passreturn raw@@ -166,7 +166,7 @@ def config_set(if not isinstance(table, (Table, InlineTable, OutOfOrderTableProxy)):raise click.ClickException(f"Cannot set {key}: '{section}' is not a TOML table.")- table[field] = _parse_toml_value(value)+ table[field] = value if key in STRING_KEYS else _parse_toml_value(value)path.write_text(tomlkit.dumps(document), encoding="utf-8")obj.output.success(f"Set {key} = {value}")diff --git a/cli/src/opensandbox_cli/config.py b/cli/src/opensandbox_cli/config.pyindex 9d386b9f1..000368346 100644--- a/cli/src/opensandbox_cli/config.py+++ b/cli/src/opensandbox_cli/config.py@@ -74,6 +74,20 @@ def load_config_file(config_path: Path | None = None) -> dict[str, Any]:return tomllib.load(f)+# Config-file keys resolve_config reads as text. `config set` stores them+# as typed: inferring a TOML number would rewrite values such as an API key+# "007123" (written back as 7123).+STRING_KEYS = frozenset(+ {+ "connection.api_key",+ "connection.domain",+ "connection.protocol",+ "defaults.image",+ "defaults.timeout",+ }+)++def resolve_config(*,cli_api_key: str | None = None,diff --git a/cli/tests/test_commands.py b/cli/tests/test_commands.pyindex f5eeab424..428f6d4b8 100644--- a/cli/tests/test_commands.py+++ b/cli/tests/test_commands.py@@ -295,6 +295,29 @@ class TestConfigSet:assert load_config_file(cfg_path)["connection"]["api_key"] == valueassert resolve_config(config_path=cfg_path)["api_key"] == value+ @pytest.mark.parametrize(+ ("key", "value", "resolved_key", "expected"),+ [+ ("connection.request_timeout", "1e2", "request_timeout", 100),+ ("connection.use_server_proxy", "1.00", "use_server_proxy", True),+ ],+ )+ def test_set_still_infers_numbers_for_typed_keys(+ self,+ runner: CliRunner,+ tmp_path: Path,+ key: str,+ value: str,+ resolved_key: str,+ expected: object,+ ) -> None:+ cfg_path = tmp_path / "config.toml"+ cfg_path.write_text("[connection]\n")++ runner.invoke(cli, ["--config", str(cfg_path), "config", "set", key, value])++ assert resolve_config(config_path=cfg_path)[resolved_key] == expected+def test_set_reports_invalid_existing_toml(self, runner: CliRunner, tmp_path: Path) -> None: