From 11aceb8ca2cfd6341ee99ec1e1b97190201b91bf Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Sat, 10 Oct 2026 12:50:11 -0400 Subject: [PATCH] config: return credentials-merged config directly as a native term --- src/albedo/daemon/albedo_daemon.erl | 11 +++--- src/albedo/daemon/configuration.gleam | 34 +++++++++++------ test/e2e/settings_test.py | 54 +++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/albedo/daemon/albedo_daemon.erl b/src/albedo/daemon/albedo_daemon.erl index 10425d4b3..059e668c1 100644 --- a/src/albedo/daemon/albedo_daemon.erl +++ b/src/albedo/daemon/albedo_daemon.erl @@ -32,13 +32,14 @@ free_port() -> {ok, Port} = inet:port(Socket), ok = gen_tcp:close(Socket), Port. -%% config.json with each profile's saved apiKey filled in from creds.json. A -%% file that is not a JSON object is passed on as it is, to fail decoding. +%% config.json with each profile's saved apiKey filled in from creds.json. +%% Returns {error, invalid} when config.json is not a valid JSON object, +%% and {error, missing} when it is absent or unreadable. read_config(Home) -> case albedo_credentials:config(Home) of - {ok,Config} -> {ok,iolist_to_binary(json:encode(Config))}; - {error,invalid} -> file:read_file(filename:join(Home,<<"config.json">>)); - {error,_} -> {error,nil} + {ok,Config} -> {ok,Config}; + {error,invalid} -> {error,invalid}; + {error,_} -> {error,missing} end. directory(Path) -> filename:pathtype(Path) =:= absolute andalso filelib:is_dir(Path). %% The content digest of this daemon's own build: sha256 over the sorted diff --git a/src/albedo/daemon/configuration.gleam b/src/albedo/daemon/configuration.gleam index 311ff6a1b..09fb81d3f 100644 --- a/src/albedo/daemon/configuration.gleam +++ b/src/albedo/daemon/configuration.gleam @@ -2,7 +2,6 @@ import albedo/openai_api/types import gleam/dict.{type Dict} import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode -import gleam/json import gleam/list import gleam/option.{type Option, None, Some} import gleam/result @@ -89,11 +88,16 @@ fn configured(config: Configuration, name: String) -> Result(Provider, String) { } fn load(home: String) -> Result(Configuration, String) { - use bytes <- result.try( + use raw <- result.try( read_config(home) - |> result.replace_error(not_configured), + |> result.map_error(fn(error) { + case error { + Missing -> not_configured + Invalid -> invalid_config + } + }), ) - json.parse_bits(bytes, configuration_decoder()) + decode.run(raw, configuration_decoder()) |> result.replace_error(invalid_config) } @@ -164,12 +168,17 @@ pub fn settings( name: String, decoder: decode.Decoder(a), ) -> Result(a, String) { - use bytes <- result.try( + use raw <- result.try( read_config(home) - |> result.replace_error(not_configured), + |> result.map_error(fn(error) { + case error { + Missing -> not_configured + Invalid -> invalid_config + } + }), ) use root <- result.try( - json.parse_bits(bytes, decode.dict(decode.string, decode.dynamic)) + decode.run(raw, decode.dict(decode.string, decode.dynamic)) |> result.replace_error(invalid_config), ) let value = case dict.get(root, "providers") { @@ -180,9 +189,7 @@ pub fn settings( dict.get(providers, name) |> result.replace_error(not_configured) }) - Error(_) if name == "default" -> - json.parse_bits(bytes, decode.dynamic) - |> result.replace_error(invalid_config) + Error(_) if name == "default" -> Ok(raw) Error(_) -> Error(not_configured) } use value <- result.try(value |> result.replace_error(not_configured)) @@ -197,8 +204,13 @@ pub fn select_default( model: String, ) -> Result(Nil, String) +type ConfigFile { + Missing + Invalid +} + @external(erlang, "albedo_daemon", "read_config") -fn read_config(home: String) -> Result(BitArray, Nil) +fn read_config(home: String) -> Result(Dynamic, ConfigFile) /// Validate profiles at the daemon boundary without reporting secret values. pub fn validate_profile( diff --git a/test/e2e/settings_test.py b/test/e2e/settings_test.py index 03670f12a..e54c12aca 100644 --- a/test/e2e/settings_test.py +++ b/test/e2e/settings_test.py @@ -832,6 +832,60 @@ class SettingsTest(unittest.TestCase): caught.exception.headers["WWW-Authenticate"], "Bearer" ) + # exclusive: corrupt vs missing config.json errors + @exclusive + def test_corrupt_and_missing_config_file_errors(self): + config_path = self.app.home / "config.json" + prior = config_path.read_bytes() + route = f"/sessions/{operation_id()}" + body = { + "kind": "new", + "workspace": str(self.app.workspace), + "provider_profile": self.app.profile, + } + try: + # 1. Non-JSON corrupt content + config_path.write_bytes(b"not valid json") + with self.assertRaises(urllib.error.HTTPError) as caught: + self.app.api(route, body, method="PUT", headers={"If-None-Match": "*"}) + self.assertEqual(caught.exception.code, 409) + self.assertIn( + "provider configuration is invalid; run /login", + caught.exception.read().decode(), + ) + + # 2. Valid JSON but not a JSON object + config_path.write_bytes(b"[1, 2]") + with self.assertRaises(urllib.error.HTTPError) as caught: + self.app.api( + f"/sessions/{operation_id()}", + body, + method="PUT", + headers={"If-None-Match": "*"}, + ) + self.assertEqual(caught.exception.code, 409) + self.assertIn( + "provider configuration is invalid; run /login", + caught.exception.read().decode(), + ) + + # 3. Missing config.json + config_path.unlink() + with self.assertRaises(urllib.error.HTTPError) as caught: + self.app.api( + f"/sessions/{operation_id()}", + body, + method="PUT", + headers={"If-None-Match": "*"}, + ) + self.assertEqual(caught.exception.code, 409) + self.assertIn( + "provider is not configured; run /login", + caught.exception.read().decode(), + ) + finally: + config_path.write_bytes(prior) + def test_busy_session_rejects_settings_before_persistence(self): entered, release = threading.Event(), threading.Event() -- knotmirror 0.0.1