Skip to content

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

fix(session): name the setting that is off in the disabled refusal, so its remedy works (PIN-290)

Luigi Agosti8f43d2746909
diff --git a/cmd/pinchtab/cmd_session_unavailable_test.go b/cmd/pinchtab/cmd_session_unavailable_test.go
index dd99288a..751bd6b1 100644
--- a/cmd/pinchtab/cmd_session_unavailable_test.go
+++ b/cmd/pinchtab/cmd_session_unavailable_test.go
@@ -7,6 +7,7 @@ import (
"testing"
"github.com/pinchtab/pinchtab/internal/server"
+ "github.com/pinchtab/pinchtab/internal/session"
)
// The three states are produced by the REAL registrars rather than hand-written JSON, so
@@ -54,7 +55,7 @@ func TestSessionCreateAdviceDiffersPerUnavailableState(t *testing.T) {
},
{
name: "server with sessions disabled",
- register: server.RegisterSessionsDisabled,
+ register: func(mux *http.ServeMux) { server.RegisterSessionsDisabled(mux, []string{session.SettingEnabled}) },
path: "/sessions",
wantAdvice: true,
wantGuidance: "sessions.agent.enabled",
diff --git a/docs/endpoints.md b/docs/endpoints.md
index 95d5fe24..7c289892 100644
--- a/docs/endpoints.md
+++ b/docs/endpoints.md
@@ -894,9 +894,11 @@ Scheduler routes are only present when `scheduler.enabled` is true.
Create returns `sessionToken` — the plaintext token shown only once.
-Agent session routes are only present in full server mode with `sessions.agent.enabled` true. The family always answers, so the state is readable from the error code rather than from a bare 404: a bridge returns `sessions_unavailable_bridge_mode`, whose remedy is to run `pinchtab server`, and a full server with the setting off returns `sessions_disabled`. No config value mounts the family in bridge mode.
+Agent session routes are only present in full server mode with agent sessions on — `sessions.agent.enabled` true and `sessions.agent.mode` not `off`. The family always answers, so the state is readable from the error code rather than from a bare 404: a bridge returns `sessions_unavailable_bridge_mode`, whose remedy is to run `pinchtab server`, and a full server with them switched off returns `sessions_disabled`. No config value mounts the family in bridge mode.
-`sessions_disabled` covers two states, and its `details.hint` says which one. A server that BOOTED with the setting off never mounted the family, so enabling it needs `pinchtab config set sessions.agent.enabled true` and a restart, and the refusal carries no `details.remedy` because that is not one command. A server that booted with it on and was switched off by a config save already mounted the family: `pinchtab config set sessions.agent.enabled true` applies live, needs no restart, and is carried as the refusal's `details.remedy`.
+`sessions_disabled` covers two states, and its `details.hint` says which one. A server that BOOTED with agent sessions off never mounted the family, so switching them on needs a config edit *and* a restart, and the refusal carries no `details.remedy` because that is not one command. A server that booted with them on and was switched off by a config save already mounted the family: the edit applies live, needs no restart, and is carried as the refusal's `details.remedy`.
+
+Two settings switch agent sessions off, so the guidance names the one that actually is off — `sessions.agent.enabled` false, `sessions.agent.mode` `off`, or both, in which case the remedy sets both. Following the prescribed command restores the service on a save-disabled server; there is no state in which running it leaves you on the same refusal.
Session-authenticated callers cannot reach dashboard/admin endpoint families such as config, dashboard agent listings, dashboard event streams, session management, profile management, instance management, or cache controls. They are intended for trusted automation in controlled environments, not for untrusted multi-tenant isolation.
diff --git a/internal/dashboard/agent_session_api.go b/internal/dashboard/agent_session_api.go
index 9cd45563..b1b2c935 100644
--- a/internal/dashboard/agent_session_api.go
+++ b/internal/dashboard/agent_session_api.go
@@ -61,8 +61,9 @@ func (a *SessionAPI) registerPatterns(mux *http.ServeMux, patterns []string) {
func (a *SessionAPI) whileEnabled(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if a.store == nil || !a.store.Enabled() {
+ hint, r := session.DisabledGuidance(a.store.DisabledBy(), true)
httpx.ErrorCode(w, http.StatusNotFound, session.CodeDisabled, session.MsgDisabled, false,
- remedy.Details(session.HintDisabledBySave, session.RemedyEnable()))
+ remedy.Details(hint, r))
return
}
next(w, r)
diff --git a/internal/server/agent_session_live_config_test.go b/internal/server/agent_session_live_config_test.go
index 60c71881..05f83a41 100644
--- a/internal/server/agent_session_live_config_test.go
+++ b/internal/server/agent_session_live_config_test.go
@@ -12,6 +12,7 @@ import (
"testing"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/remedy"
)
func (f frontDoor) sessionRequest(t *testing.T, token string) *httptest.ResponseRecorder {
@@ -105,19 +106,48 @@ func TestEnablingAgentSessionsReportsARestartReason(t *testing.T) {
}
}
+// applyRemedy runs the refusal's own remedy against a file config, so the test
+// follows the prescription instead of restoring by hand. A remedy that names the
+// wrong setting therefore does not merely read wrong — the service stays refused.
+func applyRemedy(t *testing.T, fc *config.FileConfig, line string) {
+ t.Helper()
+
+ segments := remedy.Segments(line)
+ if len(segments) == 0 {
+ t.Fatalf("the refusal carried no runnable remedy (%q)", line)
+ }
+ for _, words := range segments {
+ if len(words) != 5 || words[1] != "config" || words[2] != "set" {
+ t.Fatalf("remedy segment %v is not a `pinchtab config set <field> <value>` this test can apply", words)
+ }
+ if err := config.SetConfigValue(fc, words[3], words[4]); err != nil {
+ t.Fatalf("the refusal prescribes `%s` and the config editor answers %v", strings.Join(words, " "), err)
+ }
+ }
+}
+
// mode "off" is the other half of one question, and the docs promise it reduces
// the auth surface. It must reach exactly the state enabled false reaches — the
// same refusals, through the same assertions — or the two fields drift again.
-// mode and the two timeouts have no boot-time consumer, so they apply live in
-// both directions and never produce a restart reason.
+//
+// Both fields reach one refusal, so the refusal has to say WHICH is off: an
+// operator handed a command for the setting they did not touch runs it and lands
+// on the identical refusal with nothing new. The restore here is the refusal's own
+// remedy rather than a hand-written inverse, so a wrong prescription reds twice —
+// once on the setting it names, once on the service that stays refused.
func TestModeOffDisablesAgentSessionsExactlyAsEnabledFalseDoes(t *testing.T) {
for _, tc := range []struct {
name string
disable func(*config.FileConfig)
- restore func(*config.FileConfig)
+ names []string
+ banned string
}{
- {"enabled false", func(fc *config.FileConfig) { agentSessionsEnabled(fc, false) }, func(fc *config.FileConfig) { agentSessionsEnabled(fc, true) }},
- {"mode off", func(fc *config.FileConfig) { fc.Sessions.Agent.Mode = "off" }, func(fc *config.FileConfig) { fc.Sessions.Agent.Mode = "preferred" }},
+ {"enabled false", func(fc *config.FileConfig) { agentSessionsEnabled(fc, false) },
+ []string{"sessions.agent.enabled"}, "sessions.agent.mode"},
+ {"mode off", func(fc *config.FileConfig) { fc.Sessions.Agent.Mode = "off" },
+ []string{"sessions.agent.mode"}, "sessions.agent.enabled"},
+ {"both off", func(fc *config.FileConfig) { agentSessionsEnabled(fc, false); fc.Sessions.Agent.Mode = "off" },
+ []string{"sessions.agent.enabled", "sessions.agent.mode"}, ""},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFrontDoor(t, nil)
@@ -140,22 +170,60 @@ func TestModeOffDisablesAgentSessionsExactlyAsEnabledFalseDoes(t *testing.T) {
t.Fatalf("the existing session token still authenticated (%d: %s)", after.Code, after.Body.String())
}
create := f.createSessionOverHTTP(t)
- if create.Code != http.StatusNotFound || errorCode(t, create) != CodeSessionsDisabled {
+ if create.Code != http.StatusNotFound {
t.Fatalf("POST /sessions answered %d (%s), want the disabled refusal", create.Code, create.Body.String())
}
+ code, _, hint, prescribed := decodeSessionRefusal(t, create)
+ if code != CodeSessionsDisabled {
+ t.Fatalf("refusal code = %q, want %q", code, CodeSessionsDisabled)
+ }
+ for _, setting := range tc.names {
+ if !strings.Contains(hint, setting) {
+ t.Errorf("hint = %q, want it to name %q — the setting that is actually off", hint, setting)
+ }
+ if !strings.Contains(prescribed, setting) {
+ t.Errorf("remedy = %q, want it to set %q", prescribed, setting)
+ }
+ }
+ if tc.banned != "" {
+ if strings.Contains(hint, tc.banned) || strings.Contains(prescribed, tc.banned) {
+ t.Errorf("the refusal prescribes %q, which is not the setting that was switched off: hint %q, remedy %q",
+ tc.banned, hint, prescribed)
+ }
+ }
- // Back again, live: the family was mounted at boot, so nothing here is frozen.
- f.save(t, tc.restore)
+ // Follow the prescription exactly. The family was mounted at boot, so it
+ // must restore the service live, with no restart owed.
+ f.save(t, func(fc *config.FileConfig) { applyRemedy(t, fc, prescribed) })
if !f.agentSessions.Enabled() {
- t.Fatal("the store did not come back; the restoring save reported applied and moved nothing")
+ t.Fatal("the refusal's own remedy did not re-enable the store")
}
if back := f.createSessionOverHTTP(t); back.Code != http.StatusCreated {
- t.Fatalf("POST /sessions answered %d (%s) after restoring", back.Code, back.Body.String())
+ t.Fatalf("POST /sessions answered %d (%s) after running the prescribed remedy", back.Code, back.Body.String())
}
})
}
}
+// The boot-frozen direction, through mode rather than through enabled: a process
+// that booted with mode off never mounted the family, so a save turning it back on
+// must report a restart reason instead of claiming it applied. Without the mode
+// fold in SessionsFileConfig.AgentEnabled this save reports applied and moves
+// nothing — the defect one field over, which no unit table can see.
+func TestEnablingThroughModeOnABootDisabledServerReportsARestartReason(t *testing.T) {
+ f := newFrontDoor(t, func(fc *config.FileConfig) { fc.Sessions.Agent.Mode = "off" })
+
+ reasons := f.saveReportingReasons(t, func(fc *config.FileConfig) { fc.Sessions.Agent.Mode = "preferred" })
+ if !slices.Contains(reasons, "Agent sessions") {
+ t.Fatalf("restartReasons = %v, want one naming the agent sessions block", reasons)
+ }
+ create := f.createSessionOverHTTP(t)
+ if create.Code != http.StatusNotFound || errorCode(t, create) != CodeSessionsDisabled {
+ t.Fatalf("POST /sessions answered %d (%s) after the save; if the family were live the restart reason would be manufactured",
+ create.Code, create.Body.String())
+ }
+}
+
// The timeouts have no boot-time consumer either: they reach the store and never
// produce a restart reason. f.save asserts the second half.
func TestAgentSessionTimeoutsApplyLive(t *testing.T) {
diff --git a/internal/server/front_door_live_config_test.go b/internal/server/front_door_live_config_test.go
index 62c52415..681428db 100644
--- a/internal/server/front_door_live_config_test.go
+++ b/internal/server/front_door_live_config_test.go
@@ -73,7 +73,7 @@ func newFrontDoor(t *testing.T, boot func(*config.FileConfig)) frontDoor {
if agentSessions.Enabled() {
dashboard.NewSessionAPI(agentSessions, cfg.BrowsersAvailable).RegisterHandlers(mux)
} else {
- RegisterSessionsDisabled(mux)
+ RegisterSessionsDisabled(mux, agentSessions.DisabledBy())
}
return frontDoor{
diff --git a/internal/server/server.go b/internal/server/server.go
index 2955bad1..bc106470 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -161,7 +161,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
// Without this the family is a bare mux 404, indistinguishable from a typo and
// from bridge mode — which is what made the CLI print a config remedy at users
// for whom no config could work.
- RegisterSessionsDisabled(mux)
+ RegisterSessionsDisabled(mux, sessionStore.DisabledBy())
}
syncCtx, syncCancel := context.WithCancel(context.Background())
diff --git a/internal/server/sessions_unavailable.go b/internal/server/sessions_unavailable.go
index ae4aade4..2e3a3e9b 100644
--- a/internal/server/sessions_unavailable.go
+++ b/internal/server/sessions_unavailable.go
@@ -26,8 +26,7 @@ const (
msgSessionsUnavailableInBridgeMode = "agent sessions are unavailable in bridge mode"
hintSessionsUnavailableInBridge = "no config value mounts the session family on a bridge; the full server is what serves it."
- msgSessionsDisabled = session.MsgDisabled
- hintSessionsDisabled = session.HintDisabledAtBoot
+ msgSessionsDisabled = session.MsgDisabled
)
// Running the full server is the bridge's whole remedy. The verb it used to lead with —
@@ -58,8 +57,12 @@ func RegisterSessionsUnavailableInBridgeMode(mux *http.ServeMux) {
msgSessionsUnavailableInBridgeMode, hintSessionsUnavailableInBridge, runFullServer.Remedy())
}
-// RegisterSessionsDisabled is the full server's answer when sessions.agent.enabled is off.
-func RegisterSessionsDisabled(mux *http.ServeMux) {
- registerSessionsUnavailable(mux, CodeSessionsDisabled,
- msgSessionsDisabled, hintSessionsDisabled, remedy.None)
+// RegisterSessionsDisabled is the full server's answer when this process booted
+// with agent sessions off. off names the settings that switched them off, so the
+// refusal prescribes the one the operator actually set; the guidance carries no
+// remedy, because reaching a family this process never mounted needs a restart as
+// well as an edit and that is not one command.
+func RegisterSessionsDisabled(mux *http.ServeMux, off []string) {
+ hint, r := session.DisabledGuidance(off, false)
+ registerSessionsUnavailable(mux, CodeSessionsDisabled, msgSessionsDisabled, hint, r)
}
diff --git a/internal/server/sessions_unavailable_test.go b/internal/server/sessions_unavailable_test.go
index 3938e5af..82a427c8 100644
--- a/internal/server/sessions_unavailable_test.go
+++ b/internal/server/sessions_unavailable_test.go
@@ -32,6 +32,13 @@ func decodeSessionRefusal(t *testing.T, w *httptest.ResponseRecorder) (code, mes
return resp.Code, resp.Error, resp.Details.Hint, resp.Details.Remedy
}
+// registerDisabledByEnabled is the boot-disabled registrar in its commonest state,
+// bound so the tables below keep their func(*http.ServeMux) shape. Which setting is
+// off is the subject of the front-door guidance test, not of these.
+func registerDisabledByEnabled(mux *http.ServeMux) {
+ RegisterSessionsDisabled(mux, []string{session.SettingEnabled})
+}
+
func requestFor(pattern string) *http.Request {
method, path, _ := strings.Cut(pattern, " ")
// {id} is a mux wildcard, not a literal; a real caller sends a value.
@@ -52,7 +59,7 @@ func TestBothUnavailableModesAnswerEveryRouteInTheFamilyWithACode(t *testing.T)
{"bridge", RegisterSessionsUnavailableInBridgeMode, CodeSessionsUnavailableInBridgeMode, "pinchtab server", "bridge"},
// No remedy: enabling the family is a file edit plus a restart, which is not one
// command a caller can run, so the guidance is a hint and the field is absent.
- {"sessions disabled", RegisterSessionsDisabled, CodeSessionsDisabled, "", "sessions.agent.enabled"},
+ {"sessions disabled", registerDisabledByEnabled, CodeSessionsDisabled, "", "sessions.agent.enabled"},
} {
t.Run(mode.name, func(t *testing.T) {
mux := http.NewServeMux()
@@ -105,7 +112,7 @@ func TestTheTwoUnavailableModesDoNotShareARemedy(t *testing.T) {
}
bridgeCode, bridgeGuidance := answer(RegisterSessionsUnavailableInBridgeMode)
- disabledCode, disabledGuidance := answer(RegisterSessionsDisabled)
+ disabledCode, disabledGuidance := answer(registerDisabledByEnabled)
if bridgeCode == disabledCode {
t.Errorf("both modes answer with %q, so the caller cannot tell them apart", bridgeCode)
@@ -153,7 +160,7 @@ func TestAnUnknownPathIsUntouchedAndSaysNothingAboutSessions(t *testing.T) {
register func(*http.ServeMux)
}{
{"bridge", RegisterSessionsUnavailableInBridgeMode},
- {"sessions disabled", RegisterSessionsDisabled},
+ {"sessions disabled", registerDisabledByEnabled},
} {
t.Run(mode.name, func(t *testing.T) {
mux := http.NewServeMux()
diff --git a/internal/session/config_access_census_test.go b/internal/session/config_access_census_test.go
index 9a6ba144..a82f2efc 100644
--- a/internal/session/config_access_census_test.go
+++ b/internal/session/config_access_census_test.go
@@ -25,6 +25,7 @@ func TestEveryConfigReadHoldsTheStoreLock(t *testing.T) {
"applyConfig", // called under the lock by UpdateConfig, and from NewStore before sharing
"registerSession", // takes the lock, stamps the lifetime, installs the session
"Enabled", // takes the lock for the read of both fields the predicate folds
+ "DisabledBy", // takes the lock for the read of the same two fields
"PersistPath", // takes the lock for the read
"isExpired", // only ever called with the lock held
"loadPersisted", // runs in NewStore, before the store is reachable
diff --git a/internal/session/routes.go b/internal/session/routes.go
index ea0f5086..2ee64b30 100644
--- a/internal/session/routes.go
+++ b/internal/session/routes.go
@@ -1,6 +1,7 @@
package session
import (
+ "fmt"
"slices"
"github.com/pinchtab/pinchtab/internal/remedy"
@@ -32,22 +33,45 @@ func RoutePatterns() []string {
// it off in a process that did mount it.
//
// The code and the message are shared because both states genuinely are disabled.
-// The HINT is not, and must not be: whether a restart is owed is exactly what
-// separates them. A boot-disabled process never mounted the family, so enabling it
-// cannot reach the running process; a process disabled by a save already mounted
-// the family, so switching it back on applies immediately. One hint serving both
-// tells an operator to restart a server that does not need it.
+// The GUIDANCE is not, and must not be, because it varies in two independent ways.
+// Whether a RESTART is owed separates the two registrars: a boot-disabled process
+// never mounted the family, so enabling it cannot reach the running process, while
+// a save-disabled one already mounted it and re-enabling applies immediately. WHICH
+// SETTING is off separates two ways of being disabled: enabled false and mode off
+// both reach this refusal, and a refusal naming the wrong one hands the operator a
+// command that leaves them on the identical refusal with nothing new.
const (
CodeDisabled = "sessions_disabled"
MsgDisabled = "agent sessions are not enabled on this server"
- HintDisabledAtBoot = "set sessions.agent.enabled = true and restart the server; the route family is mounted at startup, so enabling it cannot take effect in the running process."
- HintDisabledBySave = "agent sessions were switched off by a config save; set sessions.agent.enabled = true to switch them back on — this server already mounted the route family, so it applies without a restart."
+ SettingEnabled = "sessions.agent.enabled"
+ SettingMode = "sessions.agent.mode"
)
-// enableAgentSessions is the remedy the save-disabled state has and the boot-disabled
-// state does not: one command, applying live, with no restart to pair it with.
-var enableAgentSessions = remedy.Declare("pinchtab config set sessions.agent.enabled true")
+const (
+ hintAtBoot = "set %s and restart the server; the route family is mounted at startup, so enabling it cannot take effect in the running process."
+ hintBySave = "agent sessions were switched off by a config save; set %s to switch them back on — this server already mounted the route family, so it applies without a restart."
+)
-// RemedyEnable is the save-disabled state's remedy line.
-func RemedyEnable() remedy.Remedy { return enableAgentSessions.Remedy() }
+var (
+ enableAgentSessions = remedy.Declare("pinchtab config set " + SettingEnabled + " true")
+ preferAgentSessions = remedy.Declare("pinchtab config set " + SettingMode + " preferred")
+ restoreAgentSessions = remedy.Declare("pinchtab config set " + SettingEnabled + " true && pinchtab config set " + SettingMode + " preferred")
+)
+
+// DisabledGuidance is the hint and remedy for a disabled refusal. off is what
+// Store.DisabledBy answered; mounted says whether this process mounted the family,
+// which is what decides whether any single command can reach it.
+func DisabledGuidance(off []string, mounted bool) (string, remedy.Remedy) {
+ setting, r := SettingEnabled+" = true", enableAgentSessions.Remedy()
+ switch {
+ case slices.Contains(off, SettingEnabled) && slices.Contains(off, SettingMode):
+ setting, r = SettingEnabled+" = true and "+SettingMode+" = preferred", restoreAgentSessions.Remedy()
+ case slices.Contains(off, SettingMode):
+ setting, r = SettingMode+" = preferred", preferAgentSessions.Remedy()
+ }
+ if !mounted {
+ return fmt.Sprintf(hintAtBoot, setting), remedy.None
+ }
+ return fmt.Sprintf(hintBySave, setting), r
+}
diff --git a/internal/session/store.go b/internal/session/store.go
index ce9bef24..992418fc 100644
--- a/internal/session/store.go
+++ b/internal/session/store.go
@@ -44,9 +44,27 @@ const (
ModeRequired = "required"
)
-// ModeServes reports whether a mode value leaves agent sessions serving. An
-// empty value is the default, ModePreferred.
-func ModeServes(mode string) bool { return mode != ModeOff }
+// NormalizeMode is the canonical reading of a sessions.agent.mode value: case
+// folded and trimmed, the way this codebase reads every other config enum. The
+// validator and the predicate both go through it so the accepted set is spelled
+// once, and so "Off" means off rather than being a typo that leaves an auth
+// mechanism on.
+func NormalizeMode(mode string) string {
+ return strings.ToLower(strings.TrimSpace(mode))
+}
+
+// ModeServes reports whether a mode value leaves agent sessions serving. It is an
+// allowlist, not a denylist: an empty value is the default (ModePreferred) and
+// nothing else serves, so a value the validator refuses can never be read as
+// "preferred" by a process that started anyway.
+func ModeServes(mode string) bool {
+ switch NormalizeMode(mode) {
+ case "", ModePreferred:
+ return true
+ default:
+ return false
+ }
+}
// Config controls store behavior.
type Config struct {
@@ -498,6 +516,27 @@ func (s *Store) Enabled() bool {
return s.cfg.Enabled && ModeServes(s.cfg.Mode)
}
+// DisabledBy names the settings switching agent sessions off, so a refusal can
+// prescribe a command that works. Two fields reach the one predicate, and an
+// operator who disabled through one of them is not helped by being told to set
+// the other. Empty when the store serves.
+func (s *Store) DisabledBy() []string {
+ if s == nil {
+ return []string{SettingEnabled}
+ }
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ var off []string
+ if !s.cfg.Enabled {
+ off = append(off, SettingEnabled)
+ }
+ if !ModeServes(s.cfg.Mode) {
+ off = append(off, SettingMode)
+ }
+ return off
+}
+
// PersistPath returns the file the store persists to, so a caller rebuilding
// the config from elsewhere can carry it over instead of recomputing it.
func (s *Store) PersistPath() string {