fix(loader): disable config file rewrite during startup
Some checks failed
CI / build-and-test (push) Failing after -32m45s

* migrate legacy schemas in memory only
* log hint to use amcs-migrate-config for persistence
This commit is contained in:
2026-04-21 21:31:05 +02:00
parent 7f9c6f122e
commit 55859811be
4 changed files with 19 additions and 22 deletions

View File

@@ -40,16 +40,14 @@ func LoadWithLogger(explicitPath string, log *slog.Logger) (*Config, string, err
}
if len(applied) > 0 {
if err := rewriteConfigFile(path, data, raw); err != nil {
return nil, path, err
}
if log != nil {
for _, step := range applied {
log.Warn("config migrated",
log.Warn("config migrated in memory",
slog.String("path", path),
slog.Int("from_version", step.From),
slog.Int("to_version", step.To),
slog.String("describe", step.Describe),
slog.String("hint", "persist with amcs-migrate-config"),
)
}
}
@@ -81,22 +79,6 @@ func decodeTyped(raw map[string]any) (Config, error) {
return cfg, nil
}
func rewriteConfigFile(path string, original []byte, migrated map[string]any) error {
backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())
if err := os.WriteFile(backupPath, original, 0o600); err != nil {
return fmt.Errorf("write backup %q: %w", backupPath, err)
}
out, err := yaml.Marshal(migrated)
if err != nil {
return fmt.Errorf("marshal migrated config: %w", err)
}
if err := os.WriteFile(path, out, 0o600); err != nil {
return fmt.Errorf("write migrated config %q: %w", path, err)
}
return nil
}
func ResolvePath(explicitPath string) string {
if path := strings.TrimSpace(explicitPath); path != "" {
if path != ".yaml" && path != ".yml" {

View File

@@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
@@ -215,7 +216,15 @@ logging:
if err != nil {
t.Fatalf("glob backups: %v", err)
}
if len(entries) != 1 {
t.Fatalf("backup files = %d, want 1", len(entries))
if len(entries) != 0 {
t.Fatalf("backup files = %d, want 0 (load should not rewrite config)", len(entries))
}
originalOnDisk, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("read original config: %v", err)
}
if !strings.Contains(string(originalOnDisk), "provider: \"litellm\"") {
t.Fatalf("expected source config to remain unchanged on disk")
}
}