Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: fourteen config keys have a real default that config get cannot see, because each default is written inline inside its consumer (PIN-169)

Luigi Agostia7cc99638cae
diff --git a/internal/activity/activity.go b/internal/activity/activity.go
index 83f4e0d0..0ef47028 100644
--- a/internal/activity/activity.go
+++ b/internal/activity/activity.go
@@ -254,15 +254,15 @@ func (tr *TailReader) nextSourceFilePath(cursorPath, todayPath, source string) s
type noopRecorder struct{}
-func NewRecorder(cfg Config, stateDir string) (Recorder, error) {
+func NewRecorder(cfg Config, logDir string) (Recorder, error) {
if !cfg.Enabled {
return noopRecorder{}, nil
}
- return NewStoreWithEvents(stateDir, cfg.RetentionDays, cfg.Events)
+ return NewStoreWithEvents(logDir, cfg.RetentionDays, cfg.Events)
}
-func NewStore(stateDir string, retentionDays int) (*Store, error) {
- return NewStoreWithEvents(stateDir, retentionDays, EventSourceConfig{
+func NewStore(logDir string, retentionDays int) (*Store, error) {
+ return NewStoreWithEvents(logDir, retentionDays, EventSourceConfig{
Dashboard: true,
Server: true,
Bridge: true,
@@ -273,9 +273,11 @@ func NewStore(stateDir string, retentionDays int) (*Store, error) {
})
}
-func NewStoreWithEvents(stateDir string, retentionDays int, events EventSourceConfig) (*Store, error) {
- activityDir := filepath.Join(stateDir, "activity")
- if err := os.MkdirAll(activityDir, 0750); err != nil {
+// NewStoreWithEvents takes the directory it writes into. The state-directory layout
+// belongs to internal/config, which derives this path once, so appending a name here
+// as well would put the same rule in two places.
+func NewStoreWithEvents(logDir string, retentionDays int, events EventSourceConfig) (*Store, error) {
+ if err := os.MkdirAll(logDir, 0750); err != nil {
return nil, fmt.Errorf("create activity dir: %w", err)
}
if retentionDays <= 0 {
@@ -283,7 +285,7 @@ func NewStoreWithEvents(stateDir string, retentionDays int, events EventSourceCo
}
store := &Store{
- dir: activityDir,
+ dir: logDir,
retentionDays: retentionDays,
events: events,
lastPruneTime: time.Now().UTC(),
diff --git a/internal/activity/activity_test.go b/internal/activity/activity_test.go
index b21d7e79..d8db7e30 100644
--- a/internal/activity/activity_test.go
+++ b/internal/activity/activity_test.go
@@ -289,7 +289,7 @@ func TestStoreWritesJSONLFile(t *testing.T) {
t.Fatalf("Record: %v", err)
}
- path := filepath.Join(root, "activity", "events-"+now.Format(time.DateOnly)+".jsonl")
+ path := filepath.Join(root, "events-"+now.Format(time.DateOnly)+".jsonl")
if _, err := os.Stat(path); err != nil {
t.Fatalf("activity log missing: %v", err)
}
@@ -322,7 +322,7 @@ func TestStorePartitionsDashboardEventsOutsidePrimaryLog(t *testing.T) {
t.Fatalf("Record server: %v", err)
}
- mainPath := filepath.Join(root, "activity", "events-"+now.Format(time.DateOnly)+".jsonl")
+ mainPath := filepath.Join(root, "events-"+now.Format(time.DateOnly)+".jsonl")
mainData, err := os.ReadFile(mainPath)
if err != nil {
t.Fatalf("ReadFile main: %v", err)
@@ -334,7 +334,7 @@ func TestStorePartitionsDashboardEventsOutsidePrimaryLog(t *testing.T) {
t.Fatal("primary activity log should include server events")
}
- dashboardPath := filepath.Join(root, "activity", "events-dashboard-"+now.Format(time.DateOnly)+".jsonl")
+ dashboardPath := filepath.Join(root, "events-dashboard-"+now.Format(time.DateOnly)+".jsonl")
dashboardData, err := os.ReadFile(dashboardPath)
if err != nil {
t.Fatalf("ReadFile dashboard: %v", err)
@@ -386,7 +386,7 @@ func TestStoreWritesServerEventsToSourcePartitionToo(t *testing.T) {
t.Fatalf("Record: %v", err)
}
- mainPath := filepath.Join(root, "activity", "events-"+now.Format(time.DateOnly)+".jsonl")
+ mainPath := filepath.Join(root, "events-"+now.Format(time.DateOnly)+".jsonl")
mainData, err := os.ReadFile(mainPath)
if err != nil {
t.Fatalf("ReadFile main: %v", err)
@@ -395,7 +395,7 @@ func TestStoreWritesServerEventsToSourcePartitionToo(t *testing.T) {
t.Fatal("primary activity log should include server events")
}
- serverPath := filepath.Join(root, "activity", "events-server-"+now.Format(time.DateOnly)+".jsonl")
+ serverPath := filepath.Join(root, "events-server-"+now.Format(time.DateOnly)+".jsonl")
serverData, err := os.ReadFile(serverPath)
if err != nil {
t.Fatalf("ReadFile server: %v", err)
@@ -432,7 +432,7 @@ func TestStoreWithEventsRecordsClientOnlyByDefaultPolicy(t *testing.T) {
t.Fatalf("unfiltered query = %#v, want single client event", gotAll)
}
- clientPath := filepath.Join(root, "activity", "events-client-"+now.Format(time.DateOnly)+".jsonl")
+ clientPath := filepath.Join(root, "events-client-"+now.Format(time.DateOnly)+".jsonl")
clientData, err := os.ReadFile(clientPath)
if err != nil {
t.Fatalf("ReadFile client: %v", err)
@@ -447,7 +447,7 @@ func TestStoreWithEventsRecordsClientOnlyByDefaultPolicy(t *testing.T) {
}
for _, source := range []string{"dashboard", "server", "orchestrator"} {
- path := filepath.Join(root, "activity", "events-"+source+"-"+now.Format(time.DateOnly)+".jsonl")
+ path := filepath.Join(root, "events-"+source+"-"+now.Format(time.DateOnly)+".jsonl")
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatalf("%s activity log should not exist, stat err = %v", source, err)
}
@@ -481,7 +481,7 @@ func TestStorePrunesExpiredDailyFiles(t *testing.T) {
t.Fatalf("Record new: %v", err)
}
- oldPath := filepath.Join(root, "activity", "events-"+oldDay.Format(time.DateOnly)+".jsonl")
+ oldPath := filepath.Join(root, "events-"+oldDay.Format(time.DateOnly)+".jsonl")
if _, err := os.Stat(oldPath); !os.IsNotExist(err) {
t.Fatalf("expected old activity file to be pruned, stat err = %v", err)
}
@@ -489,7 +489,7 @@ func TestStorePrunesExpiredDailyFiles(t *testing.T) {
func TestNewStorePrunesExpiredDailyFilesOnStartup(t *testing.T) {
root := t.TempDir()
- activityDir := filepath.Join(root, "activity")
+ activityDir := root
if err := os.MkdirAll(activityDir, 0750); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
@@ -520,7 +520,7 @@ func TestNewStorePrunesExpiredDailyFilesOnStartup(t *testing.T) {
func TestNewStorePrunesExpiredSourceSpecificDailyFilesOnStartup(t *testing.T) {
root := t.TempDir()
- activityDir := filepath.Join(root, "activity")
+ activityDir := root
if err := os.MkdirAll(activityDir, 0750); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
@@ -661,7 +661,7 @@ func TestStoreRecord_SanitizesURLBeforePersisting(t *testing.T) {
t.Fatalf("Record: %v", err)
}
- path := filepath.Join(root, "activity", "events-"+now.Format(time.DateOnly)+".jsonl")
+ path := filepath.Join(root, "events-"+now.Format(time.DateOnly)+".jsonl")
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
diff --git a/internal/config/config_load.go b/internal/config/config_load.go
index 3dc0276c..70c3c661 100644
--- a/internal/config/config_load.go
+++ b/internal/config/config_load.go
@@ -303,6 +303,7 @@ func LoadConfig() (*RuntimeConfig, []LoadDiagnostic, error) {
// server.stateDir could never relocate profiles. A defer is the one shape that
// cannot miss one of this function's several early returns.
defer finalizeProfileConfig(cfg)
+ defer finalizeSchedulerConfig(cfg)
var diags []LoadDiagnostic
res := readAndParseConfigFile()
diff --git a/internal/config/config_methods.go b/internal/config/config_methods.go
index 9c1f5e51..f985d34a 100644
--- a/internal/config/config_methods.go
+++ b/internal/config/config_methods.go
@@ -1,5 +1,7 @@
package config
+import "path/filepath"
+
// EnabledSensitiveEndpoints returns the names of sensitive endpoint families
// that are currently enabled in the runtime configuration.
func (cfg *RuntimeConfig) EnabledSensitiveEndpoints() []string {
@@ -40,3 +42,17 @@ func (cfg *RuntimeConfig) ActivityStateDir() string {
}
return cfg.StateDir
}
+
+// ActivityLogDir is where the activity store keeps its files. This package owns the
+// state-directory layout — the same rule as profiles.baseDir — so the store is handed
+// the directory rather than appending its own name to a parent, and `config get` can
+// report it without a second join.
+func (cfg *RuntimeConfig) ActivityLogDir() string {
+ root := cfg.ActivityStateDir()
+ if root == "" {
+ return ""
+ }
+ return filepath.Join(root, activityLogSubdir)
+}
+
+const activityLogSubdir = "activity"
diff --git a/internal/config/editor_get.go b/internal/config/editor_get.go
index 0d4e6ed5..97f4f063 100644
--- a/internal/config/editor_get.go
+++ b/internal/config/editor_get.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
+ "time"
"github.com/pinchtab/pinchtab/internal/safelog"
)
@@ -569,20 +570,64 @@ func EffectiveConfigValue(path string) (string, error) {
if err != nil {
return "", err
}
- if strings.TrimSpace(value) != "" {
+ if strings.TrimSpace(value) != "" && !runtimeSettlesTheValue(path) {
return value, nil
}
return resolvedConfigValue(path)
}
+// runtimeSettlesTheValue marks the paths whose file entry is an input rather than the
+// answer, so reading the file would report something nothing acts on:
+//
+// scheduler.* a configured zero means "use the default" here
+// observability.activity.stateDir applyFileConfig never copies it; the path derives
+func runtimeSettlesTheValue(path string) bool {
+ return strings.HasPrefix(path, "scheduler.") || path == "observability.activity.stateDir"
+}
+
func resolvedConfigValue(path string) (string, error) {
cfg, _, err := LoadConfig()
if err != nil {
return "", fmt.Errorf("load config: %w", err)
}
resolved := FileConfigFromRuntime(cfg)
+ addSettledDefaults(&resolved, cfg)
+ return GetConfigValue(&resolved, path)
+}
+
+// addSettledDefaults fills the sections FileConfigFromRuntime leaves out on purpose:
+// it serialises configs for writing, where emitting every default would add noise to
+// a file the operator did not ask for. The reader needs the opposite, so it takes the
+// values back off the RuntimeConfig that has already settled them — never from a
+// literal spelled here, which would be a second copy free to drift.
+func addSettledDefaults(resolved *FileConfig, cfg *RuntimeConfig) {
if strings.TrimSpace(resolved.Server.LogLevel) == "" {
resolved.Server.LogLevel = safelog.LevelName(safelog.DefaultLevel)
}
- return GetConfigValue(&resolved, path)
+ resolved.Scheduler = SchedulerFileConfig{
+ Enabled: boolPtrValue(cfg.Scheduler.Enabled),
+ Strategy: cfg.Scheduler.Strategy,
+ MaxQueueSize: intPtrIfPositive(cfg.Scheduler.MaxQueueSize),
+ MaxPerAgent: intPtrIfPositive(cfg.Scheduler.MaxPerAgent),
+ MaxInflight: intPtrIfPositive(cfg.Scheduler.MaxInflight),
+ MaxPerAgentFlight: intPtrIfPositive(cfg.Scheduler.MaxPerAgentFlight),
+ ResultTTLSec: intPtrIfPositive(cfg.Scheduler.ResultTTLSec),
+ WorkerCount: intPtrIfPositive(cfg.Scheduler.WorkerCount),
+ }
+ if resolved.InstanceDefaults.TabPolicy == nil {
+ resolved.InstanceDefaults.TabPolicy = &TabPolicyDefaults{}
+ }
+ tabPolicy := resolved.InstanceDefaults.TabPolicy
+ if tabPolicy.Eviction == "" {
+ tabPolicy.Eviction = cfg.TabEvictionPolicy
+ }
+ if tabPolicy.Lifecycle == "" {
+ tabPolicy.Lifecycle = cfg.TabLifecyclePolicy
+ }
+ if tabPolicy.CloseDelaySec == nil {
+ tabPolicy.CloseDelaySec = intPtrIfPositive(int(cfg.TabCloseDelay / time.Second))
+ }
+ if strings.TrimSpace(resolved.Observability.Activity.StateDir) == "" {
+ resolved.Observability.Activity.StateDir = cfg.ActivityLogDir()
+ }
}
diff --git a/internal/config/editor_get_effective_test.go b/internal/config/editor_get_effective_test.go
index 7be7366f..c325202c 100644
--- a/internal/config/editor_get_effective_test.go
+++ b/internal/config/editor_get_effective_test.go
@@ -96,6 +96,7 @@ var blankIsTheAnswer = map[string]string{
"browser.proxy.geo.locale": "no proxy geo override",
"browser.proxy.geo.webrtcIP": "no proxy geo override",
"browser.proxy.geo.countryISO": "no proxy geo override",
+ "browser.fallbackOrder": "no fallback configured; an implicit launch then tries the primary target only",
"instanceDefaults.timezone": "unset means the host timezone",
"instanceDefaults.userAgent": "unset means the browser's own user agent",
"security.downloadAllowedDomains": "empty list, downloads follow the main allowlist",
@@ -121,20 +122,7 @@ var blankIsTheAnswer = map[string]string{
// out a second time in this package. A key must move out of this table, not gain a
// hardcoded value in the print path.
var defaultLivesAtItsConsumer = map[string]string{
- "server.cookieSecure": "the cookie layer decides from the request scheme",
- "browser.fallbackOrder": "the browser registry orders fallbacks",
- "instanceDefaults.tabPolicy.eviction": "the tab policy layer",
- "instanceDefaults.tabPolicy.lifecycle": "the tab policy layer",
- "instanceDefaults.tabPolicy.closeDelaySec": "the tab policy layer",
- "observability.activity.stateDir": "the activity recorder derives it when unset",
- "scheduler.enabled": "scheduler.New applies its own defaults",
- "scheduler.strategy": "scheduler.New applies its own defaults",
- "scheduler.maxQueueSize": "scheduler.New applies its own defaults",
- "scheduler.maxPerAgent": "scheduler.New applies its own defaults",
- "scheduler.maxInflight": "scheduler.New applies its own defaults",
- "scheduler.maxPerAgentInflight": "scheduler.New applies its own defaults",
- "scheduler.resultTTLSec": "scheduler.New applies its own defaults",
- "scheduler.workerCount": "scheduler.New applies its own defaults",
+ "server.cookieSecure": "there is no single value: the cookie layer decides per request from that request's scheme, so any printed answer would be wrong for half the requests",
}
// TestEveryAddressableConfigKeyAnswersOrIsAccountedFor is the standing census behind
diff --git a/internal/config/scheduler_defaults.go b/internal/config/scheduler_defaults.go
new file mode 100644
index 00000000..90abbaa0
--- /dev/null
+++ b/internal/config/scheduler_defaults.go
@@ -0,0 +1,44 @@
+package config
+
+// DefaultSchedulerConfig is the one place the scheduler's tuning defaults are
+// written. internal/scheduler imports this package, so the values live here and
+// travel to their consumer rather than the other way round — which also makes them
+// visible to `config get`, since the effective-value reader loads a RuntimeConfig.
+func DefaultSchedulerConfig() SchedulerConfig {
+ return SchedulerConfig{
+ Enabled: false,
+ Strategy: "fair-fifo",
+ MaxQueueSize: 1000,
+ MaxPerAgent: 100,
+ MaxInflight: 20,
+ MaxPerAgentFlight: 10,
+ ResultTTLSec: 300,
+ WorkerCount: 4,
+ }
+}
+
+// finalizeSchedulerConfig restores the default for every knob left at or below zero,
+// which is what a configured 0 has always meant here: use the default, never
+// unlimited.
+func finalizeSchedulerConfig(cfg *RuntimeConfig) {
+ defaults := DefaultSchedulerConfig()
+ s := &cfg.Scheduler
+ if s.Strategy == "" {
+ s.Strategy = defaults.Strategy
+ }
+ for _, knob := range []struct {
+ value *int
+ fallback int
+ }{
+ {&s.MaxQueueSize, defaults.MaxQueueSize},
+ {&s.MaxPerAgent, defaults.MaxPerAgent},
+ {&s.MaxInflight, defaults.MaxInflight},
+ {&s.MaxPerAgentFlight, defaults.MaxPerAgentFlight},
+ {&s.ResultTTLSec, defaults.ResultTTLSec},
+ {&s.WorkerCount, defaults.WorkerCount},
+ } {
+ if *knob.value <= 0 {
+ *knob.value = knob.fallback
+ }
+ }
+}
diff --git a/internal/config/scheduler_defaults_test.go b/internal/config/scheduler_defaults_test.go
new file mode 100644
index 00000000..c268aa05
--- /dev/null
+++ b/internal/config/scheduler_defaults_test.go
@@ -0,0 +1,141 @@
+package config
+
+import (
+ "path/filepath"
+ "strconv"
+ "testing"
+)
+
+// Every scheduler knob answers, and it answers the value this package declares —
+// derived from DefaultSchedulerConfig rather than retyped, so a changed default
+// cannot leave a stale expectation passing here.
+func TestSchedulerKeysAnswerTheDeclaredDefaults(t *testing.T) {
+ writeConfigForGet(t, `{}`)
+
+ defaults := DefaultSchedulerConfig()
+ for path, want := range map[string]string{
+ "scheduler.enabled": strconv.FormatBool(defaults.Enabled),
+ "scheduler.strategy": defaults.Strategy,
+ "scheduler.maxQueueSize": strconv.Itoa(defaults.MaxQueueSize),
+ "scheduler.maxPerAgent": strconv.Itoa(defaults.MaxPerAgent),
+ "scheduler.maxInflight": strconv.Itoa(defaults.MaxInflight),
+ "scheduler.maxPerAgentInflight": strconv.Itoa(defaults.MaxPerAgentFlight),
+ "scheduler.resultTTLSec": strconv.Itoa(defaults.ResultTTLSec),
+ "scheduler.workerCount": strconv.Itoa(defaults.WorkerCount),
+ } {
+ if got := effectiveValue(t, path); got != want {
+ t.Errorf("%s = %q, want %q", path, got, want)
+ }
+ }
+}
+
+func TestSchedulerKeysKeepWhatTheFileSets(t *testing.T) {
+ writeConfigForGet(t, `{"scheduler":{"maxInflight":7,"strategy":"strict-fifo","workerCount":9}}`)
+
+ for path, want := range map[string]string{
+ "scheduler.maxInflight": "7",
+ "scheduler.strategy": "strict-fifo",
+ "scheduler.workerCount": "9",
+ } {
+ if got := effectiveValue(t, path); got != want {
+ t.Errorf("%s = %q, want the configured %q", path, got, want)
+ }
+ }
+}
+
+// A configured zero has always meant "use the default" for these knobs — never
+// unlimited. The reader must say so too, or it reports a queue size nothing enforces.
+func TestSchedulerZeroMeansTheDefaultEverywhere(t *testing.T) {
+ writeConfigForGet(t, `{"scheduler":{"maxQueueSize":0,"maxPerAgent":0,"maxInflight":0,"maxPerAgentInflight":0,"resultTTLSec":0,"workerCount":0}}`)
+
+ defaults := DefaultSchedulerConfig()
+ cfg, _, err := LoadConfig()
+ if err != nil {
+ t.Fatalf("LoadConfig() error = %v", err)
+ }
+ if cfg.Scheduler != defaults {
+ t.Errorf("runtime scheduler config = %+v, want the defaults %+v", cfg.Scheduler, defaults)
+ }
+ if got := effectiveValue(t, "scheduler.maxInflight"); got != strconv.Itoa(defaults.MaxInflight) {
+ t.Errorf("scheduler.maxInflight = %q with an explicit 0 in the file, want the default %d", got, defaults.MaxInflight)
+ }
+}
+
+// A negative value is the same case as zero, and it is the one a validator would let
+// through as "set".
+func TestSchedulerNegativeValuesFallBackToTheDefault(t *testing.T) {
+ writeConfigForGet(t, `{"scheduler":{"workerCount":-4}}`)
+
+ defaults := DefaultSchedulerConfig()
+ if got := effectiveValue(t, "scheduler.workerCount"); got != strconv.Itoa(defaults.WorkerCount) {
+ t.Errorf("scheduler.workerCount = %q with -4 in the file, want the default %d", got, defaults.WorkerCount)
+ }
+}
+
+// The tab policy defaults already lived in this package; they were simply invisible to
+// the reader because the writer omits a vanilla block on purpose.
+func TestTabPolicyKeysAnswerTheRuntimeDefaults(t *testing.T) {
+ writeConfigForGet(t, `{}`)
+
+ cfg, _, err := LoadConfig()
+ if err != nil {
+ t.Fatalf("LoadConfig() error = %v", err)
+ }
+ for path, want := range map[string]string{
+ "instanceDefaults.tabPolicy.eviction": cfg.TabEvictionPolicy,
+ "instanceDefaults.tabPolicy.lifecycle": cfg.TabLifecyclePolicy,
+ "instanceDefaults.tabPolicy.closeDelaySec": strconv.Itoa(int(cfg.TabCloseDelay.Seconds())),
+ } {
+ if want == "" || want == "0" {
+ t.Fatalf("%s has no runtime default to compare against (%q), so this test proves nothing", path, want)
+ }
+ if got := effectiveValue(t, path); got != want {
+ t.Errorf("%s = %q, want the runtime's %q", path, got, want)
+ }
+ }
+}
+
+func TestTabPolicyKeysKeepWhatTheFileSets(t *testing.T) {
+ writeConfigForGet(t, `{"instanceDefaults":{"tabPolicy":{"eviction":"reject","lifecycle":"close_idle","closeDelaySec":42}}}`)
+
+ for path, want := range map[string]string{
+ "instanceDefaults.tabPolicy.eviction": "reject",
+ "instanceDefaults.tabPolicy.lifecycle": "close_idle",
+ "instanceDefaults.tabPolicy.closeDelaySec": "42",
+ } {
+ if got := effectiveValue(t, path); got != want {
+ t.Errorf("%s = %q, want the configured %q", path, got, want)
+ }
+ }
+}
+
+// The activity log directory derives from the configured state dir, through the same
+// single owner the store is handed.
+func TestActivityStateDirAnswersTheDerivedLogDir(t *testing.T) {
+ stateDir := filepath.Join(t.TempDir(), "mystate")
+ writeConfigForGet(t, `{"server":{"stateDir":`+quoteJSON(stateDir)+`}}`)
+
+ want := filepath.Join(stateDir, "activity")
+ if got := effectiveValue(t, "observability.activity.stateDir"); got != want {
+ t.Errorf("observability.activity.stateDir = %q, want %q", got, want)
+ }
+}
+
+// applyFileConfig never copies this key into the runtime — activity logs always live
+// under server.stateDir so two instances cannot share one log dir. A reader that
+// echoed the file would name a directory nothing writes to.
+func TestActivityStateDirIgnoresAConfiguredOverride(t *testing.T) {
+ root := t.TempDir()
+ stateDir := filepath.Join(root, "mystate")
+ writeConfigForGet(t, `{"server":{"stateDir":`+quoteJSON(stateDir)+`},
+ "observability":{"activity":{"stateDir":`+quoteJSON(filepath.Join(root, "elsewhere"))+`}}}`)
+
+ want := filepath.Join(stateDir, "activity")
+ got := effectiveValue(t, "observability.activity.stateDir")
+ if got != want {
+ t.Errorf("observability.activity.stateDir = %q, want the directory in effect %q", got, want)
+ }
+ if got == filepath.Join(root, "elsewhere") {
+ t.Error("the reader echoed the configured override, which the runtime discards")
+ }
+}
diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go
index 93423741..c4511ad9 100644
--- a/internal/scheduler/scheduler.go
+++ b/internal/scheduler/scheduler.go
@@ -7,6 +7,8 @@ import (
"net/http"
"sync"
"time"
+
+ "github.com/pinchtab/pinchtab/internal/config"
)
// InstanceResolver finds the localhost port for a given tab ID.
@@ -28,18 +30,59 @@ type Config struct {
WatcherInterval time.Duration `json:"watcherInterval"`
}
-// DefaultConfig returns safe defaults.
+// DefaultConfig returns safe defaults. The knobs an operator can set live in
+// internal/config, which owns their defaults and reports them through `config get`;
+// only the two this package does not expose as config keys are written here.
func DefaultConfig() Config {
+ out := ConfigFromRuntime(config.DefaultSchedulerConfig())
+ out.MaxBatchSize = 50
+ out.WatcherInterval = 30 * time.Second
+ return out
+}
+
+// ConfigFromRuntime maps the operator-facing scheduler settings onto this package's
+// Config. It is the one place that conversion happens, so the seconds-to-duration
+// step cannot drift between callers.
+func ConfigFromRuntime(s config.SchedulerConfig) Config {
return Config{
- Strategy: "fair-fifo",
- MaxQueueSize: 1000,
- MaxPerAgent: 100,
- MaxInflight: 20,
- MaxPerAgentFlight: 10,
- ResultTTL: 5 * time.Minute,
- WorkerCount: 4,
- MaxBatchSize: 50,
- WatcherInterval: 30 * time.Second,
+ Enabled: s.Enabled,
+ Strategy: s.Strategy,
+ MaxQueueSize: s.MaxQueueSize,
+ MaxPerAgent: s.MaxPerAgent,
+ MaxInflight: s.MaxInflight,
+ MaxPerAgentFlight: s.MaxPerAgentFlight,
+ ResultTTL: time.Duration(s.ResultTTLSec) * time.Second,
+ WorkerCount: s.WorkerCount,
+ }
+}
+
+// withDefaults fills every knob left at or below zero. A configured zero has always
+// meant "use the default" here, never unlimited.
+func withDefaults(cfg *Config) {
+ defaults := DefaultConfig()
+ if cfg.Strategy == "" {
+ cfg.Strategy = defaults.Strategy
+ }
+ for _, knob := range []struct {
+ value *int
+ fallback int
+ }{
+ {&cfg.MaxQueueSize, defaults.MaxQueueSize},
+ {&cfg.MaxPerAgent, defaults.MaxPerAgent},
+ {&cfg.MaxInflight, defaults.MaxInflight},
+ {&cfg.MaxPerAgentFlight, defaults.MaxPerAgentFlight},
+ {&cfg.WorkerCount, defaults.WorkerCount},
+ {&cfg.MaxBatchSize, defaults.MaxBatchSize},
+ } {
+ if *knob.value <= 0 {
+ *knob.value = knob.fallback
+ }
+ }
+ if cfg.ResultTTL <= 0 {
+ cfg.ResultTTL = defaults.ResultTTL
+ }
+ if cfg.WatcherInterval <= 0 {
+ cfg.WatcherInterval = defaults.WatcherInterval
}
}
@@ -70,30 +113,7 @@ type Scheduler struct {
// New creates a scheduler with the given config and instance resolver.
func New(cfg Config, resolver InstanceResolver) *Scheduler {
- if cfg.MaxQueueSize <= 0 {
- cfg.MaxQueueSize = 1000
- }
- if cfg.MaxPerAgent <= 0 {
- cfg.MaxPerAgent = 100
- }
- if cfg.MaxInflight <= 0 {
- cfg.MaxInflight = 20
- }
- if cfg.MaxPerAgentFlight <= 0 {
- cfg.MaxPerAgentFlight = 10
- }
- if cfg.ResultTTL <= 0 {
- cfg.ResultTTL = 5 * time.Minute
- }
- if cfg.WorkerCount <= 0 {
- cfg.WorkerCount = 4
- }
- if cfg.MaxBatchSize <= 0 {
- cfg.MaxBatchSize = 50
- }
- if cfg.WatcherInterval <= 0 {
- cfg.WatcherInterval = 30 * time.Second
- }
+ withDefaults(&cfg)
return &Scheduler{
cfg: cfg,
diff --git a/internal/scheduler/scheduler_defaults_test.go b/internal/scheduler/scheduler_defaults_test.go
new file mode 100644
index 00000000..d16a60a5
--- /dev/null
+++ b/internal/scheduler/scheduler_defaults_test.go
@@ -0,0 +1,114 @@
+package scheduler
+
+import (
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+// New with an empty Config must land exactly on DefaultConfig. Anything else means a
+// second set of literals has appeared inside New, which is how the defaults drifted
+// out of reach of `config get` in the first place.
+func TestNewFillsEveryKnobFromDefaultConfig(t *testing.T) {
+ got := New(Config{}, nil).cfg
+ if want := DefaultConfig(); got != want {
+ t.Errorf("New(Config{}).cfg = %+v, want DefaultConfig() %+v", got, want)
+ }
+}
+
+// The operator-facing knobs come from internal/config, so `config get` and the running
+// scheduler cannot disagree. Derived from the config package's own declaration rather
+// than retyped here.
+func TestDefaultConfigTakesTheOperatorKnobsFromTheConfigPackage(t *testing.T) {
+ want := ConfigFromRuntime(config.DefaultSchedulerConfig())
+ got := DefaultConfig()
+
+ got.MaxBatchSize, got.WatcherInterval = want.MaxBatchSize, want.WatcherInterval
+ if got != want {
+ t.Errorf("DefaultConfig() operator knobs = %+v, want %+v from config.DefaultSchedulerConfig()", got, want)
+ }
+ if want.Strategy == "" || want.MaxQueueSize == 0 || want.ResultTTL == 0 {
+ t.Fatalf("config.DefaultSchedulerConfig() looks empty (%+v), so this comparison proves nothing", want)
+ }
+}
+
+// schedulerKnobFields are the settings an operator can address through config get/set.
+// Their default values belong to internal/config/scheduler_defaults.go and nowhere
+// else — a literal beside a second copy is exactly the drift this card paid off.
+// Strategy is absent by name on purpose: internal/config also carries a
+// MultiInstance.Strategy whose default is unrelated, so the scheduler's strategy is
+// censused by its VALUE below instead.
+var schedulerKnobFields = []string{
+ "MaxQueueSize",
+ "MaxPerAgent",
+ "MaxInflight",
+ "MaxPerAgentFlight",
+ "ResultTTL",
+ "ResultTTLSec",
+ "WorkerCount",
+}
+
+var knobLiteralAssignment = regexp.MustCompile(`\b(` + strings.Join(schedulerKnobFields, "|") + `)\s*[:=]\s*("|\d|-\d)`)
+
+func TestSchedulerDefaultsAreSpelledInOnePlace(t *testing.T) {
+ owner := filepath.Join("..", "config", "scheduler_defaults.go")
+ seen := map[string]int{}
+
+ for _, dir := range []string{".", filepath.Join("..", "config"), filepath.Join("..", "server")} {
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ t.Fatalf("ReadDir(%s): %v", dir, err)
+ }
+ files := 0
+ for _, entry := range entries {
+ name := entry.Name()
+ if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
+ continue
+ }
+ files++
+ path := filepath.Join(dir, name)
+ body, err := os.ReadFile(path) // #nosec G304 -- files walked from this repo's own tree.
+ if err != nil {
+ t.Fatalf("ReadFile(%s): %v", path, err)
+ }
+ for i, line := range strings.Split(string(body), "\n") {
+ trimmed := strings.TrimSpace(line)
+ if strings.HasPrefix(trimmed, "//") {
+ continue
+ }
+ field := ""
+ if match := knobLiteralAssignment.FindStringSubmatch(trimmed); match != nil {
+ field = match[1]
+ }
+ if strings.Contains(trimmed, `"`+config.DefaultSchedulerConfig().Strategy+`"`) {
+ field = "Strategy"
+ }
+ if field == "" {
+ continue
+ }
+ seen[field]++
+ if path == owner {
+ continue
+ }
+ t.Errorf("%s:%d gives %s a literal default; the one owner is %s, and a second copy is free to drift from what config get reports:\n\t%s",
+ path, i+1, field, owner, trimmed)
+ }
+ }
+ if files == 0 {
+ t.Fatalf("no non-test Go files found in %s, so this census read nothing", dir)
+ }
+ }
+
+ for _, field := range append(schedulerKnobFields, "Strategy") {
+ if field == "ResultTTL" {
+ continue // the duration form is derived from ResultTTLSec, never written as a literal
+ }
+ if seen[field] == 0 {
+ t.Errorf("found no literal assignment of %s anywhere; the owner has been renamed or the pattern no longer matches, which makes this census vacuous", field)
+ }
+ }
+}
diff --git a/internal/server/bridge.go b/internal/server/bridge.go
index ef2efbdd..be313950 100644
--- a/internal/server/bridge.go
+++ b/internal/server/bridge.go
@@ -46,7 +46,7 @@ func RunBridgeServer(cfg *config.RuntimeConfig, version string) {
MCP: cfg.Observability.Activity.Events.MCP,
Other: cfg.Observability.Activity.Events.Other,
},
- }, cfg.ActivityStateDir())
+ }, cfg.ActivityLogDir())
if err != nil {
fatalStartup("activity store", err)
}
diff --git a/internal/server/server.go b/internal/server/server.go
index ab517d76..a9f796f8 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -138,7 +138,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
MCP: cfg.Observability.Activity.Events.MCP,
Other: cfg.Observability.Activity.Events.Other,
},
- }, cfg.ActivityStateDir())
+ }, cfg.ActivityLogDir())
if err != nil {
fatalStartup("activity store", err)
}
@@ -269,29 +269,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
var sched *scheduler.Scheduler
if cfg.Scheduler.Enabled {
- schedCfg := scheduler.DefaultConfig()
- schedCfg.Enabled = true
- if cfg.Scheduler.Strategy != "" {
- schedCfg.Strategy = cfg.Scheduler.Strategy
- }
- if cfg.Scheduler.MaxQueueSize > 0 {
- schedCfg.MaxQueueSize = cfg.Scheduler.MaxQueueSize
- }
- if cfg.Scheduler.MaxPerAgent > 0 {
- schedCfg.MaxPerAgent = cfg.Scheduler.MaxPerAgent
- }
- if cfg.Scheduler.MaxInflight > 0 {
- schedCfg.MaxInflight = cfg.Scheduler.MaxInflight
- }
- if cfg.Scheduler.MaxPerAgentFlight > 0 {
- schedCfg.MaxPerAgentFlight = cfg.Scheduler.MaxPerAgentFlight
- }
- if cfg.Scheduler.ResultTTLSec > 0 {
- schedCfg.ResultTTL = time.Duration(cfg.Scheduler.ResultTTLSec) * time.Second
- }
- if cfg.Scheduler.WorkerCount > 0 {
- schedCfg.WorkerCount = cfg.Scheduler.WorkerCount
- }
+ schedCfg := scheduler.ConfigFromRuntime(cfg.Scheduler)
resolver := &scheduler.ManagerResolver{Mgr: orch.InstanceManager()}
sched = scheduler.New(schedCfg, resolver)