Skip to content

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

fix: report state directory restart requirements (PIN-310)

Luigi Agostia9205c64a026
diff --git a/internal/activity/state_dir_restart_test.go b/internal/activity/state_dir_restart_test.go
new file mode 100644
index 00000000..c436211d
--- /dev/null
+++ b/internal/activity/state_dir_restart_test.go
@@ -0,0 +1,48 @@
+package activity_test
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+// Loading again models the process restart promised by the config-set refusal:
+// the new runtime must derive the activity sink from the saved server.stateDir.
+func TestRestartMovesActivityRecordsWithServerStateDir(t *testing.T) {
+ stateDir := filepath.Join(t.TempDir(), "state-moved")
+ fc := config.DefaultFileConfig()
+ fc.Server.StateDir = stateDir
+ configPath := filepath.Join(t.TempDir(), "config.json")
+ t.Setenv("PINCHTAB_CONFIG", configPath)
+ data, err := json.Marshal(fc)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(configPath, data, 0o600); err != nil {
+ t.Fatal(err)
+ }
+
+ restarted := config.Load()
+ wantDir := filepath.Join(stateDir, "activity")
+ if got := restarted.ActivityLogDir(); got != wantDir {
+ t.Fatalf("ActivityLogDir() after restart = %q, want %q", got, wantDir)
+ }
+ store, err := activity.NewStore(restarted.ActivityLogDir(), 1)
+ if err != nil {
+ t.Fatalf("NewStore() after restart: %v", err)
+ }
+ if err := store.Record(activity.Event{Source: "server", Path: "/after-restart"}); err != nil {
+ t.Fatalf("Record() after restart: %v", err)
+ }
+ entries, err := os.ReadDir(wantDir)
+ if err != nil {
+ t.Fatalf("ReadDir(%q): %v", wantDir, err)
+ }
+ if len(entries) == 0 {
+ t.Fatalf("activity record was not written under restarted state dir %q", wantDir)
+ }
+}
diff --git a/internal/browsersession/session.go b/internal/browsersession/session.go
index cd6f0094..5d814559 100644
--- a/internal/browsersession/session.go
+++ b/internal/browsersession/session.go
@@ -253,6 +253,17 @@ func (m *Manager) ElevationWindow() time.Duration {
return m.elevationWindow
}
+// PersistPath returns the file selected when the manager was constructed. Config
+// saves use it to avoid moving live dashboard sessions between state directories.
+func (m *Manager) PersistPath() string {
+ if m == nil {
+ return ""
+ }
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+ return m.persistPath
+}
+
func (m *Manager) UpdateConfig(cfg Config) {
if m == nil {
return
diff --git a/internal/browsersession/session_test.go b/internal/browsersession/session_test.go
index c5d10c00..680cda08 100644
--- a/internal/browsersession/session_test.go
+++ b/internal/browsersession/session_test.go
@@ -131,6 +131,14 @@ func TestIsElevatedPersistsExpiryDeletion(t *testing.T) {
}
}
+func TestPersistPathReportsConfiguredPath(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "sessions.json")
+ mgr := NewManager(Config{Persist: true, PersistPath: path})
+ if got := mgr.PersistPath(); got != path {
+ t.Fatalf("PersistPath() = %q, want %q", got, path)
+ }
+}
+
func TestValidateDebouncesLastSeenPersistence(t *testing.T) {
path := filepath.Join(t.TempDir(), "sessions.json")
base := time.Date(2026, 3, 18, 12, 0, 0, 0, time.UTC)
diff --git a/internal/dashboard/config_api.go b/internal/dashboard/config_api.go
index 199257c6..719e1f48 100644
--- a/internal/dashboard/config_api.go
+++ b/internal/dashboard/config_api.go
@@ -245,7 +245,7 @@ func (c *ConfigAPI) persistAndApply(w http.ResponseWriter, normalized *config.Fi
next := config.NextRuntimeConfig(c.cfg(), normalized)
c.live.Publish(next)
if c.sessions != nil {
- c.sessions.UpdateConfig(BrowserSessionConfig(next))
+ c.sessions.UpdateConfig(browserSessionConfig(next, c.sessions.PersistPath()))
}
if c.agentSessions != nil {
c.agentSessions.UpdateConfig(AgentSessionConfig(next, c.agentSessions.PersistPath()))
diff --git a/internal/dashboard/config_changes.go b/internal/dashboard/config_changes.go
index 0312e0af..5207d7b8 100644
--- a/internal/dashboard/config_changes.go
+++ b/internal/dashboard/config_changes.go
@@ -2,6 +2,8 @@ package dashboard
import (
"encoding/json"
+ "path/filepath"
+ "strings"
"github.com/pinchtab/pinchtab/internal/config"
)
@@ -86,7 +88,7 @@ func changedTargetProxyNames(current, next config.BrowserTargetsConfig) []string
}
func (c *ConfigAPI) restartReasonsFor(next config.FileConfig) []string {
- reasons := make([]string, 0, 6)
+ reasons := make([]string, 0, 8)
// The IDPI guard, allowlist, and sensitive-endpoint policy are snapshotted
// from the boot config when the server starts and are not rebuilt on a config
@@ -99,7 +101,10 @@ func (c *ConfigAPI) restartReasonsFor(next config.FileConfig) []string {
if c.boot.Server.Port != next.Server.Port || c.boot.Server.Bind != next.Server.Bind {
reasons = append(reasons, "Server address")
}
- if c.boot.Profiles.BaseDir != next.Profiles.BaseDir {
+ if c.boot.Server.StateDir != next.Server.StateDir {
+ reasons = append(reasons, "Server state directory (server.stateDir)")
+ }
+ if effectiveProfilesDir(c.boot) != effectiveProfilesDir(next) {
reasons = append(reasons, "Profiles directory")
}
if c.boot.MultiInstance.Strategy != next.MultiInstance.Strategy {
@@ -127,6 +132,13 @@ func (c *ConfigAPI) restartReasonsFor(next config.FileConfig) []string {
return reasons
}
+func effectiveProfilesDir(fc config.FileConfig) string {
+ if baseDir := strings.TrimSpace(fc.Profiles.BaseDir); baseDir != "" {
+ return filepath.Clean(baseDir)
+ }
+ return filepath.Join(strings.TrimSpace(fc.Server.StateDir), "profiles")
+}
+
func sameIntPtr(a, b *int) bool {
if a == nil || b == nil {
return a == b
diff --git a/internal/dashboard/config_changes_test.go b/internal/dashboard/config_changes_test.go
index 532309df..876c1d7f 100644
--- a/internal/dashboard/config_changes_test.go
+++ b/internal/dashboard/config_changes_test.go
@@ -2,6 +2,7 @@ package dashboard
import (
"encoding/json"
+ "path/filepath"
"reflect"
"testing"
"time"
@@ -28,6 +29,43 @@ func TestSensitiveConfigChangesIgnoresAbsentVersusEmptyContainers(t *testing.T)
}
}
+func TestRestartReasonsIncludeStateDirAndDerivedProfilesDir(t *testing.T) {
+ boot := config.DefaultFileConfig()
+ boot.Server.StateDir = filepath.Join(t.TempDir(), "old")
+ boot.Profiles.BaseDir = ""
+ api := newConfigAPIForTest(config.Load(), nil, nil, nil, nil, "test", time.Now())
+ api.boot = boot
+
+ next := boot
+ next.Server.StateDir = filepath.Join(t.TempDir(), "new")
+ reasons := api.restartReasonsFor(next)
+ for _, want := range []string{"Server state directory (server.stateDir)", "Profiles directory"} {
+ if !containsString(reasons, want) {
+ t.Errorf("restartReasonsFor() = %v, want %q", reasons, want)
+ }
+ }
+}
+
+func TestRestartReasonsOmitStateDirWhenUnchanged(t *testing.T) {
+ boot := config.DefaultFileConfig()
+ boot.Server.StateDir = filepath.Join(t.TempDir(), "state")
+ api := newConfigAPIForTest(config.Load(), nil, nil, nil, nil, "test", time.Now())
+ api.boot = boot
+
+ if reasons := api.restartReasonsFor(boot); containsString(reasons, "Server state directory (server.stateDir)") {
+ t.Fatalf("restartReasonsFor() = %v, unexpectedly reports unchanged state dir", reasons)
+ }
+}
+
+func containsString(values []string, want string) bool {
+ for _, value := range values {
+ if value == want {
+ return true
+ }
+ }
+ return false
+}
+
func TestSensitiveConfigChangesStillReportsARealSecurityEdit(t *testing.T) {
current := config.DefaultFileConfig()
next := current
diff --git a/internal/dashboard/session_config.go b/internal/dashboard/session_config.go
index ddfefc9f..0b555451 100644
--- a/internal/dashboard/session_config.go
+++ b/internal/dashboard/session_config.go
@@ -16,12 +16,16 @@ func BrowserSessionConfig(runtime *config.RuntimeConfig) browsersession.Config {
if runtime == nil {
return browsersession.Config{}
}
+ return browserSessionConfig(runtime, filepath.Join(runtime.StateDir, dashboardSessionStateFile))
+}
+
+func browserSessionConfig(runtime *config.RuntimeConfig, persistPath string) browsersession.Config {
return browsersession.Config{
IdleTimeout: runtime.Sessions.Dashboard.IdleTimeout,
MaxLifetime: runtime.Sessions.Dashboard.MaxLifetime,
ElevationWindow: runtime.Sessions.Dashboard.ElevationWindow,
Persist: runtime.Sessions.Dashboard.Persist,
- PersistPath: filepath.Join(runtime.StateDir, dashboardSessionStateFile),
+ PersistPath: persistPath,
PersistElevationAcrossRestart: runtime.Sessions.Dashboard.PersistElevationAcrossRestart,
}
}
diff --git a/internal/server/agent_session_live_config_test.go b/internal/server/agent_session_live_config_test.go
index 5dec2bcd..c1a6f601 100644
--- a/internal/server/agent_session_live_config_test.go
+++ b/internal/server/agent_session_live_config_test.go
@@ -257,7 +257,10 @@ func TestASaveKeepsTheAgentSessionStoreWritingToItsOriginalPath(t *testing.T) {
}
moved := t.TempDir()
- f.save(t, func(fc *config.FileConfig) { fc.Server.StateDir = moved })
+ reasons := f.saveReportingReasons(t, func(fc *config.FileConfig) { fc.Server.StateDir = moved })
+ if !slices.Contains(reasons, "Server state directory (server.stateDir)") {
+ t.Fatalf("restartReasons = %v, want server.stateDir named", reasons)
+ }
if got := f.agentSessions.PersistPath(); got != original {
t.Fatalf("persist path after the save = %q, want %q", got, original)
@@ -278,6 +281,25 @@ func TestASaveKeepsTheAgentSessionStoreWritingToItsOriginalPath(t *testing.T) {
}
}
+func TestStateDirSaveReportsRestartAndDoesNotCreateTheDestination(t *testing.T) {
+ f := newFrontDoor(t, nil)
+ originalDashboardSessions := f.sessions.PersistPath()
+ moved := filepath.Join(t.TempDir(), "state-moved")
+
+ reasons := f.saveReportingReasons(t, func(fc *config.FileConfig) { fc.Server.StateDir = moved })
+ for _, want := range []string{"Server state directory (server.stateDir)", "Profiles directory"} {
+ if !slices.Contains(reasons, want) {
+ t.Errorf("restartReasons = %v, want %q", reasons, want)
+ }
+ }
+ if got := f.sessions.PersistPath(); got != originalDashboardSessions {
+ t.Fatalf("dashboard session persist path after save = %q, want original %q", got, originalDashboardSessions)
+ }
+ if _, err := os.Stat(moved); !os.IsNotExist(err) {
+ t.Fatalf("stateDir save created unapplied destination %q: err=%v", moved, err)
+ }
+}
+
// Both states answer sessions_disabled and both are genuinely disabled, so the code
// and the message are shared. The GUIDANCE cannot be: only the boot-disabled state
// owes a restart. A shared hint is this card's own defect one direction over — the