Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,9 @@ func LoadConfig(path string) (*Config, error) {
Version int `json:"version"`
}
if e := json.Unmarshal(data, &versionInfo); e != nil {
return nil, fmt.Errorf("failed to detect config version: %w", e)
e = wrapJSONError(data, e, "config.json")
logger.ErrorCF("config", formatDiagnosticLogMessage("Malformed config file", e), map[string]any{"path": path})
return nil, e
}
if len(data) <= 10 {
logger.Warn(fmt.Sprintf("content is [%s]", string(data)))
Expand All @@ -1030,6 +1032,11 @@ func LoadConfig(path string) (*Config, error) {
// Legacy config (no version field)
v, e := loadConfigV0(data)
if e != nil {
logger.ErrorCF(
"config",
formatDiagnosticLogMessage("Failed to load legacy config", e),
map[string]any{"path": path},
)
return nil, e
}
cfg, e = v.Migrate()
Expand Down Expand Up @@ -1068,6 +1075,11 @@ func LoadConfig(path string) (*Config, error) {
)
cfg, err = loadConfig(data)
if err != nil {
logger.ErrorCF(
"config",
formatDiagnosticLogMessage("Failed to load config", err),
map[string]any{"path": path},
)
return nil, err
}
secPath := securityPath(path)
Expand Down Expand Up @@ -1102,6 +1114,11 @@ func LoadConfig(path string) (*Config, error) {
// Current version
cfg, err = loadConfig(data)
if err != nil {
logger.ErrorCF(
"config",
formatDiagnosticLogMessage("Failed to load config", err),
map[string]any{"path": path},
)
return nil, err
}
// Load security configuration
Expand Down
98 changes: 71 additions & 27 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,72 @@ func TestLoadConfig_WebPreferNativeCanBeDisabled(t *testing.T) {
}
}

func TestLoadConfig_SyntaxErrorReportsLineAndColumn(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.json")
raw := "{\n \"version\": 2,\n \"tools\": {\n \"web\": {\n \"enabled\": true,,\n \"format\": \"markdown\"\n }\n }\n}\n"
if err := os.WriteFile(configPath, []byte(raw), 0o600); err != nil {
t.Fatalf("WriteFile() error: %v", err)
}

_, err := LoadConfig(configPath)
if err == nil {
t.Fatal("expected syntax error, got nil")
}
if !strings.Contains(err.Error(), "syntax error at line 5, column 23") {
t.Fatalf("expected line/column diagnostic, got %q", err.Error())
}
if !strings.Contains(err.Error(), "\"enabled\": true,,") {
t.Fatalf("expected source snippet in diagnostic, got %q", err.Error())
}
if !strings.Contains(err.Error(), "^") {
t.Fatalf("expected caret marker in diagnostic, got %q", err.Error())
}
}

func TestLoadConfig_TypeErrorReportsFieldPath(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.json")
raw := "{\n \"version\": 2,\n \"tools\": {\n \"web\": {\n \"fetch_limit_bytes\": \"oops\"\n }\n }\n}\n"
if err := os.WriteFile(configPath, []byte(raw), 0o600); err != nil {
t.Fatalf("WriteFile() error: %v", err)
}

_, err := LoadConfig(configPath)
if err == nil {
t.Fatal("expected type error, got nil")
}
if !strings.Contains(err.Error(), "type error at line 5, column 33") {
t.Fatalf("expected line/column diagnostic, got %q", err.Error())
}
if !strings.Contains(err.Error(), "fetch_limit_bytes") {
t.Fatalf("expected field name in diagnostic, got %q", err.Error())
}
if !strings.Contains(err.Error(), "\"fetch_limit_bytes\": \"oops\"") {
t.Fatalf("expected source snippet in diagnostic, got %q", err.Error())
}
if !strings.Contains(err.Error(), "^") {
t.Fatalf("expected caret marker in diagnostic, got %q", err.Error())
}
}

func TestLoadConfig_UnknownFieldsReportsExactPaths(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.json")
raw := "{\n \"version\": 2,\n \"tools\": {\n \"weeb\": {\n \"enabled\": true\n },\n \"web\": {\n \"fatch_limit_bytes\": 123\n }\n }\n}\n"
if err := os.WriteFile(configPath, []byte(raw), 0o600); err != nil {
t.Fatalf("WriteFile() error: %v", err)
}

_, err := LoadConfig(configPath)
if err == nil {
t.Fatal("expected unknown field error, got nil")
}
if !strings.Contains(err.Error(), "tools.weeb") || !strings.Contains(err.Error(), "tools.web.fatch_limit_bytes") {
t.Fatalf("expected exact unknown field paths, got %q", err.Error())
}
}

func TestDefaultConfig_ExecAllowRemoteEnabled(t *testing.T) {
cfg := DefaultConfig()
if !cfg.Tools.Exec.AllowRemote {
Expand Down Expand Up @@ -1049,27 +1115,18 @@ func TestLoadConfig_TelegramPlaceholderTextAcceptsSingleString(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.json")
data := `{
"version": 1,
"agents": { "defaults": { "workspace": "", "model": "", "max_tokens": 0, "max_tool_iterations": 0 } },
"bindings": [],
"session": {},
"version": 2,
"channels": {
"telegram": {
"enabled": true,
"bot_token": "",
"token": "",
"allow_from": [],
"placeholder": {
"enabled": true,
"text": "Thinking..."
}
}
},
"model_list": [],
"gateway": {},
"tools": {},
"heartbeat": {},
"devices": {},
"voice": {}
}
}`
if err := os.WriteFile(cfgPath, []byte(data), 0o600); err != nil {
t.Fatalf("setup: %v", err)
Expand All @@ -1085,25 +1142,12 @@ func TestLoadConfig_TelegramPlaceholderTextAcceptsSingleString(t *testing.T) {
}

// TestLoadConfig_WarnsForPlaintextAPIKey verifies that LoadConfig resolves a plaintext
// api_key into memory but does NOT rewrite the config file. File writes are the sole
// api_keys entry into memory but does NOT rewrite the config file. File writes are the sole
// responsibility of SaveConfig.
func TestLoadConfig_WarnsForPlaintextAPIKey(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.json")
const original = `{"version":1,"model_list":[{"model_name":"test","model":"openai/gpt-4","api_key":"sk-plaintext"}]}`
if err := os.WriteFile(cfgPath, []byte(original), 0o600); err != nil {
t.Fatalf("setup: %v", err)
}
secPath := filepath.Join(dir, SecurityConfigFile)
const securityConfig = `
model_list:
test:0:
api_keys:
- "sk-plaintext"
`
if err := os.WriteFile(secPath, []byte(securityConfig), 0o600); err != nil {
t.Fatalf("setup: %v", err)
}
const original = `{"version":2,"model_list":[{"model_name":"test","model":"openai/gpt-4","api_keys":["sk-plaintext"]}]}`
if err := os.WriteFile(cfgPath, []byte(original), 0o600); err != nil {
t.Fatalf("setup: %v", err)
}
Expand Down
Loading