server init adds the guardrails to a server.yaml from before them

An upgrade replaces the binary and leaves server.yaml alone, so a control
plane installed before guardrails stayed unguarded. Run again, init now
appends the baseline guard section (and so fetches the judge) when the file
has no guard key at all, keeping the old file as server.yaml.bak-<time>.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-05 12:39:28 +02:00
1 parent 3558dba485
commit dc65ea6899
5 files changed
+103 -3

No files matched your search

+11
View File
@@ -21,6 +21,17 @@ is only released once its **exit criterion** is met, not when its code lands.
Until v1.0.0 the minor version is the breaking one: configuration formats and Go
APIs change between minors without a deprecation period.
## Unreleased
### Fixed
- **An upgraded control plane gets the guardrails**: `ctl upgrade` replaces the
binary but leaves server.yaml as it is, and one written before guardrails has no
`guard:` section, so nothing was checked. `llmgw server init`, run again on such
a machine, now adds the baseline policy (and fetches the judge model), keeping
the old file beside it as `server.yaml.bak-<time>`. A file that already says
`guard:`, even empty, is left alone.
## v0.11.4 — 2026-10-05
Two control planes, run in anger on the reference cluster.
+13 -1
View File
@@ -144,7 +144,9 @@ func newServerInitCmd() *cobra.Command {
"SQLite or PostgreSQL, is moved in. Run again, it changes nothing.\n\n" +
"Each step is skipped when its result already exists, so it is safe to run\n" +
"again: to finish an install that stopped, or with another --host to\n" +
"reissue the certificate. It never overwrites server.yaml or the authority.\n\n" +
"reissue the certificate. It never overwrites server.yaml or the authority;\n" +
"to a server.yaml from before guardrails it adds the guard section, the old\n" +
"file kept beside it.\n\n" +
"The authority's key stays in " + defaultPKIDir + " on this machine. Move it\n" +
"somewhere safer once the nodes are onboarded if this machine is exposed.",
Args: cobra.NoArgs,
@@ -548,6 +550,16 @@ func (in *serverInit) ensureConfig(context.Context) error {
path := in.configPath()
if _, err := os.Stat(path); err == nil {
in.kept("%s (never overwritten)", path)
// A file from before guardrails has no guard: at all, and an
// upgrade alone leaves it unguarded. One that says guard:, even
// empty, was decided on and stays as it is.
added, err := appendGuardIfMissing(path, in.judgeWanted())
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
if added {
in.done("server.yaml: guardrails added (baseline policy), the old file kept beside it")
}
} else {
contact := in.admin
if contact == "" {
+38
View File
@@ -13,6 +13,8 @@ import (
"strings"
"time"
"go.yaml.in/yaml/v3"
"gitea.mynodes.xyz/ilker/llmgw/internal/agent/discovery"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
@@ -665,3 +667,39 @@ func waitFor(ctx context.Context, limit time.Duration, what string, done func()
}
}
}
// appendGuardIfMissing adds init's guard section to a server.yaml that has
// no guard key, keeping the file as it was beside it.
func appendGuardIfMissing(path string, judge bool) (bool, error) {
b, err := os.ReadFile(path) //nolint:gosec // server.yaml
if err != nil {
return false, err
}
var top map[string]any
if err := yaml.Unmarshal(b, &top); err != nil {
return false, err
}
if _, ok := top["guard"]; ok {
return false, nil
}
if err := appendBlock(path, b, guardSection(judge)); err != nil {
return false, err
}
return true, nil
}
// appendBlock writes b plus block to path, with b kept in a dated backup.
func appendBlock(path string, b []byte, block string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
backup := fmt.Sprintf("%s.bak-%s", path, time.Now().Format("20060102-150405"))
if err := os.WriteFile(backup, b, 0o640); err != nil {
return err
}
if len(b) > 0 && b[len(b)-1] != '\n' {
b = append(b, '\n')
}
return os.WriteFile(path, append(append(b, '\n'), block...), info.Mode().Perm())
}
+37
View File
@@ -207,3 +207,40 @@ func TestInitSaysAtOnceWhenAModelFitsNowhere(t *testing.T) {
t.Errorf("took %s to say so", time.Since(start))
}
}
func TestAnOldConfigurationGetsGuardrailsOnce(t *testing.T) {
body := fmt.Sprintf(serverConfigTemplate, "/etc/llmgw", "/etc/llmgw", "/etc/llmgw",
"/var/lib/llmgw/llmgw.db", "ops@example.com", "")
path := filepath.Join(t.TempDir(), "server.yaml")
if err := os.WriteFile(path, []byte(body), 0o640); err != nil {
t.Fatal(err)
}
added, err := appendGuardIfMissing(path, true)
if err != nil || !added {
t.Fatalf("added=%v err=%v, want the guard section added", added, err)
}
cfg, err := config.Load(path)
if err != nil {
t.Fatal(err)
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
if len(cfg.Guard.Default) != 1 || cfg.Guard.Policies["baseline"].Judge == nil {
t.Errorf("guard = %+v, want the baseline with its judge", cfg.Guard)
}
if backups, _ := filepath.Glob(path + ".bak-*"); len(backups) != 1 {
t.Errorf("backups = %v, want the old file kept", backups)
}
if added, err := appendGuardIfMissing(path, true); err != nil || added {
t.Errorf("second run: added=%v err=%v, want nothing changed", added, err)
}
// guard: written by hand, even empty, is a decision and stays.
if err := os.WriteFile(path, []byte(body+"guard: {}\n"), 0o640); err != nil {
t.Fatal(err)
}
if added, err := appendGuardIfMissing(path, true); err != nil || added {
t.Errorf("empty guard: added=%v err=%v, want it left alone", added, err)
}
}
+4 -2
View File
@@ -126,8 +126,10 @@ func TestInitNeverOverwritesTheConfiguration(t *testing.T) {
if err := in.run(context.Background()); err != nil {
t.Fatalf("%v\n%s", err, out)
}
if got, _ := os.ReadFile(in.configPath()); string(got) != mine {
t.Error("an existing server.yaml was rewritten")
// Kept as written; only the guardrails it predates are added after it.
got, _ := os.ReadFile(in.configPath())
if !strings.HasPrefix(string(got), mine) || !strings.Contains(string(got[len(mine):]), "\nguard:\n") {
t.Errorf("server.yaml is not the old file with guard: after it:\n%s", got)
}
// And the store it names is the one used.
if _, err := os.Stat(filepath.Join(in.stateDir, "mine.db")); err != nil {