diff --git a/pkgs/mcpx/internal/settings/tickerinterval_internal_test.go b/pkgs/mcpx/internal/settings/tickerinterval_internal_test.go new file mode 100644 index 0000000000..d01a8f7497 --- /dev/null +++ b/pkgs/mcpx/internal/settings/tickerinterval_internal_test.go @@ -0,0 +1,53 @@ +package settings + +import ( + "strings" + "testing" +) + +// The ticker-interval guard is a class-of-bug guard: it iterates the +// authoritative tickerIntervalSettings table itself, so a future member +// of the table is covered the moment it is added, with no second list +// to keep in sync (#3467). +func TestEveryTickerIntervalSettingRefusesNonPositiveValues(t *testing.T) { + sch, err := New(Registry()) + if err != nil { + t.Fatal(err) + } + if len(tickerIntervalSettings) == 0 { + t.Fatal("tickerIntervalSettings is empty; the guard would cover nothing") + } + // The three settings passed directly to time.NewTicker today must be + // members; dropping one from the table fails here. + for _, path := range []string{"daemon.reapInterval", "daemon.saveInterval", "http.ssePing"} { + if !tickerIntervalSettings[path] { + t.Errorf("%s is passed to time.NewTicker but missing from tickerIntervalSettings", path) + } + } + for path := range tickerIntervalSettings { + s, ok := sch.Lookup(path) + if !ok { + t.Errorf("%s is in tickerIntervalSettings but is not a registered setting", path) + continue + } + if s.Kind != KindDuration { + t.Errorf("%s is in tickerIntervalSettings but has kind %v, not KindDuration", path, s.Kind) + continue + } + for _, raw := range []string{"0s", "-5s", "0", "-1ns"} { + err := Validate(*s, raw) + if err == nil { + t.Errorf("%s=%q: want a refusal for a non-positive ticker interval", path, raw) + continue + } + if !strings.Contains(err.Error(), path) { + t.Errorf("%s=%q: refusal should name the setting, got %v", path, raw, err) + } + } + for _, raw := range []string{"1ms", "30s", s.Default} { + if err := Validate(*s, raw); err != nil { + t.Errorf("%s=%q: want acceptance for a positive interval, got %v", path, raw, err) + } + } + } +} diff --git a/pkgs/mcpx/internal/settings/tickerinterval_test.go b/pkgs/mcpx/internal/settings/tickerinterval_test.go new file mode 100644 index 0000000000..ee4307f01e --- /dev/null +++ b/pkgs/mcpx/internal/settings/tickerinterval_test.go @@ -0,0 +1,49 @@ +package settings_test + +import ( + "testing" + + "github.com/developing-today/code/pkgs/mcpx/internal/settings" +) + +func lookupSetting(t *testing.T, path string) settings.Setting { + t.Helper() + sch, err := settings.New(settings.Registry()) + if err != nil { + t.Fatal(err) + } + s, ok := sch.Lookup(path) + if !ok { + t.Fatalf("%s is not a setting", path) + } + return *s +} + +// The refusal behaviour for the ticker settings themselves is covered +// by TestEveryTickerIntervalSettingRefusesNonPositiveValues in +// tickerinterval_internal_test.go, which iterates the authoritative +// tickerIntervalSettings table (present and future members). The tests +// here pin the other half of the contract: the durations that are NOT +// ticker intervals keep their semantics. + +// Positivity is per-setting, not blanket: daemon.idleExit=0s means +// "never exit" and must stay valid (#3467). +func TestIdleExitZeroStaysValid(t *testing.T) { + s := lookupSetting(t, "daemon.idleExit") + if err := settings.Validate(s, "0s"); err != nil { + t.Errorf("daemon.idleExit=0s: want acceptance (zero means never), got %v", err) + } +} + +// Durations whose readers fall back to a default when non-positive +// (artifacts.gcInterval, transport.sseKeepAlive) are not ticker +// intervals; Validate must keep accepting zero for them so the guarded +// fallback behaviour stays intact (#3467). +func TestGuardedFallbackDurationsStillAcceptZero(t *testing.T) { + for _, path := range []string{"artifacts.gcInterval", "transport.sseKeepAlive"} { + s := settings.Setting{Path: path, Kind: settings.KindDuration, Default: "30s"} + if err := settings.Validate(s, "0s"); err != nil { + t.Errorf("%s=0s: want acceptance (reader defaults it), got %v", path, err) + } + } +} diff --git a/pkgs/mcpx/internal/settings/value.go b/pkgs/mcpx/internal/settings/value.go index 5104590c61..3f0a051f6a 100644 --- a/pkgs/mcpx/internal/settings/value.go +++ b/pkgs/mcpx/internal/settings/value.go @@ -489,6 +489,17 @@ func ParseBytes(v string) (int64, error) { return n, nil } +// tickerIntervalSettings are the KindDuration settings whose values are +// passed directly to time.NewTicker (daemon/server.go: the reap and save +// tickers and the SSE ping ticker). time.NewTicker panics on a +// non-positive period, so Validate refuses zero and negative values for +// exactly these paths (#3467). +var tickerIntervalSettings = map[string]bool{ + "daemon.reapInterval": true, + "daemon.saveInterval": true, + "http.ssePing": true, +} + // Validate checks one raw value against its declaration. func Validate(set Setting, raw string) error { raw = strings.TrimSpace(raw) @@ -513,6 +524,16 @@ func Validate(set Setting, raw string) error { if err != nil { return fmt.Errorf("want a duration like 30s or 5m, got %q", raw) } + if d <= 0 && tickerIntervalSettings[set.Path] { + // These three settings are passed directly to time.NewTicker, + // which panics on a non-positive period, so a zero or negative + // value must be refused here rather than crash the daemon at + // startup or an SSE handler later (#3467). Positivity is + // per-setting, not blanket: daemon.idleExit=0s means "never", + // and artifacts.gcInterval / transport.sseKeepAlive fall back + // to a default when non-positive, so they keep accepting it. + return fmt.Errorf("%s must be a positive duration, got %q", set.Path, raw) + } if set.Path == "protoTasks.maxTTL" && d > 0 && d < time.Millisecond { // Store.Start clamps in whole milliseconds; a positive // sub-millisecond max would truncate to 0 and delete every