Skip to content

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

fix: three different session-unavailable states all answer with a bare 404, so the CLI prescribes a config edit that cannot work on a bridge (PIN-150)

Luigi Agostid6230e0b859c
diff --git a/cmd/pinchtab/cmd_bridge.go b/cmd/pinchtab/cmd_bridge.go
index 09f87379..cc209a7a 100644
--- a/cmd/pinchtab/cmd_bridge.go
+++ b/cmd/pinchtab/cmd_bridge.go
@@ -28,6 +28,9 @@ By default, the bridge launches Chrome itself. Use --cdp-attach to attach the
bridge to an already-running browser process (Chrome or CloakBrowser) via
its remote debugging URL — the external process is never killed by PinchTab.
+Agent sessions are unavailable in bridge mode: the /sessions family is not served
+here and no config value mounts it. Run "pinchtab server" if you need them.
+
Examples:
pinchtab bridge
pinchtab bridge --cdp-attach ws://127.0.0.1:9222/devtools/browser/<id>
diff --git a/cmd/pinchtab/cmd_session.go b/cmd/pinchtab/cmd_session.go
index cbf34fa0..333d30cd 100644
--- a/cmd/pinchtab/cmd_session.go
+++ b/cmd/pinchtab/cmd_session.go
@@ -8,6 +8,7 @@ import (
"github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
"github.com/pinchtab/pinchtab/internal/cli/output"
+ "github.com/pinchtab/pinchtab/internal/server"
"github.com/spf13/cobra"
)
@@ -86,12 +87,8 @@ func init() {
return
}
statusCode, rawBody, _ := apiclient.DoPostQuietWithStatus(rt.client, rt.base, rt.token, "/sessions", body)
- if statusCode == 404 {
- fmt.Fprintln(os.Stderr, "Error: agent sessions are not enabled on this server")
- output.Hint("enable with: sessions.agent.enabled = true in config.json, then restart the server")
- os.Exit(1)
- }
if statusCode >= 400 {
+ exitSessionUnavailable(rawBody)
apiclient.ExitWithAPIError(statusCode, rawBody)
}
var result sessionCreateResult
@@ -126,3 +123,42 @@ func init() {
sessionCmd.AddCommand(infoCmd, listCmd, createCmd, revokeCmd)
rootCmd.AddCommand(sessionCmd)
}
+
+// exitSessionUnavailable renders the two states in which the session family exists but is
+// not mounted, each with the remedy that can actually succeed there. It decides on the
+// CODE: the three states used to share a bare 404, so keying off the status printed a
+// config edit at bridge users, for whom no config value mounts the route.
+//
+// Anything else — including an unrecognised code and a genuinely unknown path, which
+// carries no code at all — falls through to the generic API error. A permissive default
+// here would silently reuse the config remedy for the next mode, which is the conflation
+// this replaced.
+func sessionUnavailableAdvice(rawBody []byte) (message, remedy string, ok bool) {
+ var resp struct {
+ Code string `json:"code"`
+ Error string `json:"error"`
+ Details struct {
+ Remedy string `json:"remedy"`
+ } `json:"details"`
+ }
+ if json.Unmarshal(rawBody, &resp) != nil {
+ return "", "", false
+ }
+ switch resp.Code {
+ case server.CodeSessionsUnavailableInBridgeMode, server.CodeSessionsDisabled:
+ return resp.Error, resp.Details.Remedy, true
+ }
+ return "", "", false
+}
+
+func exitSessionUnavailable(rawBody []byte) {
+ message, remedy, ok := sessionUnavailableAdvice(rawBody)
+ if !ok {
+ return
+ }
+ fmt.Fprintf(os.Stderr, "Error: %s\n", message)
+ if remedy != "" {
+ output.Hint(remedy)
+ }
+ os.Exit(1)
+}
diff --git a/cmd/pinchtab/cmd_session_unavailable_test.go b/cmd/pinchtab/cmd_session_unavailable_test.go
new file mode 100644
index 00000000..73878672
--- /dev/null
+++ b/cmd/pinchtab/cmd_session_unavailable_test.go
@@ -0,0 +1,107 @@
+package main
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/server"
+)
+
+// The three states are produced by the REAL registrars rather than hand-written JSON, so
+// this is a producer-to-consumer test: if a code, a message or a remedy changes on the
+// server side, the assertion the CLI makes about it moves with it instead of drifting.
+func sessionCreateResponse(t *testing.T, register func(*http.ServeMux), path string) []byte {
+ t.Helper()
+
+ mux := http.NewServeMux()
+ if register != nil {
+ register(mux)
+ }
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, httptest.NewRequest(http.MethodPost, path, nil))
+ return w.Body.Bytes()
+}
+
+// This is the criterion the card exists for. All THREE states, because a test covering
+// only the bridge leaves the conflation in place: what was wrong was not one bad message
+// but one message serving three states, and only asserting each separately can show that
+// each remedy is reachable and correct where it is printed.
+func TestSessionCreateAdviceDiffersPerUnavailableState(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ // register is nil for the unknown-path state: nothing mounts the family, which
+ // is precisely what a typo or an old server looks like.
+ register func(*http.ServeMux)
+ path string
+ wantAdvice bool
+ wantRemedy string
+ banned string
+ why string
+ }{
+ {
+ name: "bridge mode",
+ register: server.RegisterSessionsUnavailableInBridgeMode,
+ path: "/sessions",
+ wantAdvice: true,
+ wantRemedy: "pinchtab server",
+ banned: "sessions.agent.enabled",
+ why: "no config value mounts the family on a bridge, so a config remedy is the unescapable loop this card fixes",
+ },
+ {
+ name: "server with sessions disabled",
+ register: server.RegisterSessionsDisabled,
+ path: "/sessions",
+ wantAdvice: true,
+ wantRemedy: "sessions.agent.enabled",
+ banned: "",
+ why: "this is the one state the config remedy fits",
+ },
+ {
+ name: "unknown path",
+ register: nil,
+ path: "/sessions",
+ wantAdvice: false,
+ why: "an unrecognised body must fall through to the generic API error rather than reusing either session remedy",
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ body := sessionCreateResponse(t, tc.register, tc.path)
+
+ message, remedy, ok := sessionUnavailableAdvice(body)
+ if ok != tc.wantAdvice {
+ t.Fatalf("advice recognised = %v, want %v; %s (body=%s)", ok, tc.wantAdvice, tc.why, body)
+ }
+ if !tc.wantAdvice {
+ return
+ }
+ if message == "" {
+ t.Errorf("no message, so the CLI prints an empty Error: line")
+ }
+ if !strings.Contains(remedy, tc.wantRemedy) {
+ t.Errorf("remedy = %q, want it to name %q; %s", remedy, tc.wantRemedy, tc.why)
+ }
+ if tc.banned != "" && strings.Contains(remedy, tc.banned) {
+ t.Errorf("remedy = %q, which prescribes %q; %s", remedy, tc.banned, tc.why)
+ }
+ })
+ }
+}
+
+// The mapping must be exhaustive rather than permissive. An unrecognised code — a future
+// mode, or an older server — must NOT inherit either remedy, because keying off a
+// permissive default is how 404 came to serve three states.
+func TestAnUnrecognisedCodeGetsNoSessionAdvice(t *testing.T) {
+ for _, body := range []string{
+ `{"code":"sessions_unavailable_in_some_future_mode","error":"nope","details":{"remedy":"do something"}}`,
+ `{"code":"session_not_found","error":"session not found"}`,
+ `{"error":"agent sessions are not enabled on this server"}`,
+ `404 page not found`,
+ ``,
+ } {
+ if _, _, ok := sessionUnavailableAdvice([]byte(body)); ok {
+ t.Errorf("body %q was treated as a known unavailable state; an unrecognised code must fall through to the generic error", body)
+ }
+ }
+}
diff --git a/docs/endpoints.md b/docs/endpoints.md
index 9cae32af..46182efe 100644
--- a/docs/endpoints.md
+++ b/docs/endpoints.md
@@ -778,6 +778,8 @@ 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`, whose remedy is the config change. No config value mounts the family in bridge mode.
+
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.
## Feature Gates
diff --git a/internal/cli/hints.go b/internal/cli/hints.go
index c0bb8546..772856e6 100644
--- a/internal/cli/hints.go
+++ b/internal/cli/hints.go
@@ -43,12 +43,15 @@ const SessionCreateCommand = "export PINCHTAB_SESSION=$(pinchtab session create
// end-of-line yields exactly the command — it is the only place that command is
// published, and its readers are agents.
//
-// A THIRD state stays wrong and this does not fix it: a bridge has no agent
-// sessions at all, so the otherwise-clause cannot help there. The honest enabler
-// is exposing the capability in the health payload, which reports nothing about
-// it today.
+// The THIRD state is a bridge, where no config value mounts the family at all, so the
+// fallback must not name a config edit as the universal enabler: it is one of two
+// enablers, and the other is running a different process. The clause therefore attributes
+// each remedy to the mode it works in rather than prescribing one for both. The command
+// itself is unconditional — creating a session is what the caller wanted in every state,
+// and the server now answers each with its own code and remedy if it cannot.
const NoSessionHint = "this tab is shared — no agent session is set. If agent sessions are enabled on this server, create one now; " +
- "otherwise set sessions.agent.enabled = true in config.json and restart first. Either way the command is: " +
+ "otherwise a full server needs sessions.agent.enabled = true in config.json and a restart, and a bridge has no agent sessions at all. " +
+ "Either way the command is: " +
SessionCreateCommand
// NextStepsRunningHints is the "Next steps" group shown when the server is up;
diff --git a/internal/dashboard/agent_session_api.go b/internal/dashboard/agent_session_api.go
index c5bd63ea..58395f13 100644
--- a/internal/dashboard/agent_session_api.go
+++ b/internal/dashboard/agent_session_api.go
@@ -32,16 +32,41 @@ func NewSessionAPI(store *session.Store, browsersAvailable []string) *SessionAPI
return &SessionAPI{store: store, browsersAvailable: browsersAvailable}
}
-// RegisterHandlers registers session API routes.
+// RegisterHandlers registers session API routes. It walks session.RoutePatterns() rather
+// than naming the patterns here, so this registration and the unavailable-mode ones in the
+// server package cannot disagree about what the family contains. A pattern with no handler
+// panics: leaving it unrouted here is exactly the bare-404 state the shared list prevents.
func (a *SessionAPI) RegisterHandlers(mux *http.ServeMux) {
if a == nil || a.store == nil || !a.store.Enabled() {
return
}
- mux.HandleFunc("POST /sessions", a.handleCreate)
- mux.HandleFunc("GET /sessions", a.handleList)
- mux.HandleFunc("GET /sessions/me", a.handleMe)
- mux.HandleFunc("GET /sessions/{id}", a.handleGet)
- mux.HandleFunc("POST /sessions/{id}/revoke", a.handleRevoke)
+ a.registerPatterns(mux, session.RoutePatterns())
+}
+
+func (a *SessionAPI) registerPatterns(mux *http.ServeMux, patterns []string) {
+ for _, pattern := range patterns {
+ handler := a.handlerFor(pattern)
+ if handler == nil {
+ panic("dashboard: no session handler bound for " + pattern)
+ }
+ mux.HandleFunc(pattern, handler)
+ }
+}
+
+func (a *SessionAPI) handlerFor(pattern string) http.HandlerFunc {
+ switch pattern {
+ case "POST /sessions":
+ return a.handleCreate
+ case "GET /sessions":
+ return a.handleList
+ case "GET /sessions/me":
+ return a.handleMe
+ case "GET /sessions/{id}":
+ return a.handleGet
+ case "POST /sessions/{id}/revoke":
+ return a.handleRevoke
+ }
+ return nil
}
func (a *SessionAPI) handleCreate(w http.ResponseWriter, r *http.Request) {
diff --git a/internal/dashboard/agent_session_routes_test.go b/internal/dashboard/agent_session_routes_test.go
new file mode 100644
index 00000000..36acd338
--- /dev/null
+++ b/internal/dashboard/agent_session_routes_test.go
@@ -0,0 +1,79 @@
+package dashboard
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/session"
+)
+
+// This is the enforcement criterion 10 asks about, and it is what stops the defect
+// regenerating one route at a time. The live registration walks session.RoutePatterns()
+// and panics on a pattern it cannot bind, so a route added to the shared list without a
+// handler fails loudly here instead of being mounted in server mode and answering a bare
+// mux 404 in the other two.
+//
+// Driving handlerFor over the real list is the browserless half: it needs no store and no
+// server, so it runs everywhere and names the unbound pattern.
+func TestEverySharedSessionRouteHasAHandler(t *testing.T) {
+ patterns := session.RoutePatterns()
+ if len(patterns) == 0 {
+ t.Fatal("the shared route list is empty, so this census would pass over nothing")
+ }
+
+ api := NewSessionAPI(nil, nil)
+ for _, pattern := range patterns {
+ if api.handlerFor(pattern) == nil {
+ t.Errorf("%s is in session.RoutePatterns() but binds no handler; RegisterHandlers panics on it, and the unavailable-mode registrars would answer it while server mode 404s", pattern)
+ }
+ }
+}
+
+// The reverse direction: a handler this package can serve but which is NOT in the shared
+// list would be mounted in server mode only, which is the asymmetry the list exists to
+// prevent. Enumerating the patterns the switch accepts is the only way to see it.
+func TestNoSessionHandlerIsReachableOutsideTheSharedList(t *testing.T) {
+ // The switch's own arms, spelled once here. A pattern added to handlerFor without
+ // being added to the shared list fails this.
+ served := []string{
+ "POST /sessions",
+ "GET /sessions",
+ "GET /sessions/me",
+ "GET /sessions/{id}",
+ "POST /sessions/{id}/revoke",
+ }
+
+ shared := map[string]bool{}
+ for _, pattern := range session.RoutePatterns() {
+ shared[pattern] = true
+ }
+
+ api := NewSessionAPI(nil, nil)
+ for _, pattern := range served {
+ if api.handlerFor(pattern) == nil {
+ t.Errorf("%s no longer binds a handler; if the route was removed, remove it from this list and from session.RoutePatterns()", pattern)
+ continue
+ }
+ if !shared[pattern] {
+ t.Errorf("%s binds a handler but is absent from session.RoutePatterns(), so it is mounted in server mode and a bare 404 in bridge mode", pattern)
+ }
+ }
+ if len(served) != len(shared) {
+ t.Errorf("handlerFor serves %d patterns but the shared list holds %d; the two definitions have diverged", len(served), len(shared))
+ }
+}
+
+// RegisterHandlers must refuse to leave a listed route unrouted rather than skipping it,
+// because a silently skipped pattern is exactly the bare-404 state.
+func TestRegisterHandlersPanicsOnAListedRouteItCannotBind(t *testing.T) {
+ defer func() {
+ if recover() == nil {
+ t.Error("an unbindable pattern was skipped silently; it would answer a bare 404 while its siblings work")
+ }
+ }()
+
+ api := NewSessionAPI(newTestSessionStore(), nil)
+ mux := http.NewServeMux()
+ // Stand in for a route added to the shared list with no handler.
+ api.registerPatterns(mux, []string{"GET /sessions/unbound"})
+}
diff --git a/internal/server/bridge.go b/internal/server/bridge.go
index be313950..80064177 100644
--- a/internal/server/bridge.go
+++ b/internal/server/bridge.go
@@ -80,6 +80,7 @@ func RunBridgeServer(cfg *config.RuntimeConfig, version string) {
}
h.RegisterRoutes(mux, doShutdown)
activity.RegisterHandlers(mux, actStore)
+ RegisterSessionsUnavailableInBridgeMode(mux)
cli.LogSecurityWarnings(cfg)
server = &http.Server{
diff --git a/internal/server/server.go b/internal/server/server.go
index 0bd404d7..eb04accd 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -171,6 +171,12 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
ServerMetrics: handlers.SnapshotMetrics,
})
profMgr.RegisterHandlers(mux)
+ if !sessionStore.Enabled() {
+ // 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)
+ }
syncCtx, syncCancel := context.WithCancel(context.Background())
go func() {
diff --git a/internal/server/sessions_unavailable.go b/internal/server/sessions_unavailable.go
new file mode 100644
index 00000000..68b16d45
--- /dev/null
+++ b/internal/server/sessions_unavailable.go
@@ -0,0 +1,60 @@
+package server
+
+import (
+ "net/http"
+
+ "github.com/pinchtab/pinchtab/internal/httpx"
+ "github.com/pinchtab/pinchtab/internal/session"
+)
+
+// Three states used to be one bare mux 404, so the CLI mapped all of them onto the only
+// remedy that fits one: a config edit. On a bridge that edit can never work, and the user
+// re-ran it forever. These codes exist so the caller can tell the states apart; the CLI
+// branches on them rather than on the status, because keying off 404 is what conflated
+// them and would conflate the next mode too.
+const (
+ // CodeSessionsUnavailableInBridgeMode: the family is structurally absent. No config
+ // mounts it, so the remedy is a different process, not a setting.
+ CodeSessionsUnavailableInBridgeMode = "sessions_unavailable_bridge_mode"
+ // CodeSessionsDisabled: mounted by config and switched off, where the config remedy
+ // this family always printed is the correct one.
+ CodeSessionsDisabled = "sessions_disabled"
+)
+
+const (
+ msgSessionsUnavailableInBridgeMode = "agent sessions are unavailable in bridge mode"
+ remedySessionsUnavailableInBridge = "run the full server instead: pinchtab server"
+
+ msgSessionsDisabled = "agent sessions are not enabled on this server"
+ remedySessionsDisabled = "pinchtab config set sessions.agent.enabled true && pinchtab server restart"
+)
+
+// registerSessionsUnavailable answers the whole session family with one coded refusal.
+// Both modes that do not mount the family call it, and it ranges over the same
+// session.RoutePatterns() the live registration uses — that shared list is what stops a
+// route added later being reachable in server mode and a bare 404 everywhere else.
+//
+// The status stays 404: the family genuinely is not here. What changes is that the body is
+// the product's error envelope with a code and a remedy instead of net/http's bare
+// "404 page not found", which escaped the envelope entirely because no route was
+// registered at all.
+func registerSessionsUnavailable(mux *http.ServeMux, code, message, remedy string) {
+ handler := func(w http.ResponseWriter, r *http.Request) {
+ httpx.ErrorCode(w, http.StatusNotFound, code, message, false, map[string]any{"remedy": remedy})
+ }
+ for _, pattern := range session.RoutePatterns() {
+ mux.HandleFunc(pattern, handler)
+ }
+}
+
+// RegisterSessionsUnavailableInBridgeMode is the bridge's answer for the family.
+func RegisterSessionsUnavailableInBridgeMode(mux *http.ServeMux) {
+ registerSessionsUnavailable(mux, CodeSessionsUnavailableInBridgeMode,
+ msgSessionsUnavailableInBridgeMode, remedySessionsUnavailableInBridge)
+}
+
+// RegisterSessionsDisabled is the full server's answer when sessions.agent.enabled is off.
+func RegisterSessionsDisabled(mux *http.ServeMux) {
+ registerSessionsUnavailable(mux, CodeSessionsDisabled,
+ msgSessionsDisabled, remedySessionsDisabled)
+}
diff --git a/internal/server/sessions_unavailable_test.go b/internal/server/sessions_unavailable_test.go
new file mode 100644
index 00000000..2bbdd024
--- /dev/null
+++ b/internal/server/sessions_unavailable_test.go
@@ -0,0 +1,137 @@
+package server
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/session"
+)
+
+func decodeSessionRefusal(t *testing.T, w *httptest.ResponseRecorder) (code, message, remedy string) {
+ t.Helper()
+
+ var resp struct {
+ Code string `json:"code"`
+ Error string `json:"error"`
+ Details struct {
+ Remedy string `json:"remedy"`
+ } `json:"details"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatalf("body is not the product's error envelope (%v): %s", err, w.Body.String())
+ }
+ return resp.Code, resp.Error, resp.Details.Remedy
+}
+
+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.
+ return httptest.NewRequest(method, strings.ReplaceAll(path, "{id}", "abc123"), nil)
+}
+
+// EVERY pattern must answer, not just POST /sessions: the defect was that an unmounted
+// family fell through to net/http's bare "404 page not found", which carries no code at
+// all and so cannot be told apart from a typo.
+func TestBothUnavailableModesAnswerEveryRouteInTheFamilyWithACode(t *testing.T) {
+ for _, mode := range []struct {
+ name string
+ register func(*http.ServeMux)
+ wantCode string
+ wantRemedy string
+ }{
+ {"bridge", RegisterSessionsUnavailableInBridgeMode, CodeSessionsUnavailableInBridgeMode, "pinchtab server"},
+ {"sessions disabled", RegisterSessionsDisabled, CodeSessionsDisabled, "sessions.agent.enabled"},
+ } {
+ t.Run(mode.name, func(t *testing.T) {
+ mux := http.NewServeMux()
+ mode.register(mux)
+
+ patterns := session.RoutePatterns()
+ if len(patterns) == 0 {
+ t.Fatal("the shared route list is empty, so this test would pass over nothing")
+ }
+ for _, pattern := range patterns {
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, requestFor(pattern))
+
+ if w.Code != http.StatusNotFound {
+ t.Errorf("%s: status = %d, want 404", pattern, w.Code)
+ }
+ if strings.Contains(w.Body.String(), "404 page not found") {
+ t.Errorf("%s: still the bare mux 404, which carries no code: %s", pattern, w.Body.String())
+ }
+ code, message, remedy := decodeSessionRefusal(t, w)
+ if code != mode.wantCode {
+ t.Errorf("%s: code = %q, want %q", pattern, code, mode.wantCode)
+ }
+ if message == "" {
+ t.Errorf("%s: refusal carries no message", pattern)
+ }
+ if !strings.Contains(remedy, mode.wantRemedy) {
+ t.Errorf("%s: remedy = %q, want it to name %q — the remedy that can actually succeed in this mode", pattern, remedy, mode.wantRemedy)
+ }
+ }
+ })
+ }
+}
+
+// The two modes must not converge: a bridge told to edit config is the original defect,
+// and it would be invisible to a test that only checked both answered SOMETHING.
+func TestTheTwoUnavailableModesDoNotShareARemedy(t *testing.T) {
+ answer := func(register func(*http.ServeMux)) (string, string) {
+ mux := http.NewServeMux()
+ register(mux)
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/sessions", nil))
+ code, _, remedy := decodeSessionRefusal(t, w)
+ return code, remedy
+ }
+
+ bridgeCode, bridgeRemedy := answer(RegisterSessionsUnavailableInBridgeMode)
+ disabledCode, disabledRemedy := answer(RegisterSessionsDisabled)
+
+ if bridgeCode == disabledCode {
+ t.Errorf("both modes answer with %q, so the caller cannot tell them apart", bridgeCode)
+ }
+ if bridgeRemedy == disabledRemedy {
+ t.Errorf("both modes prescribe %q; one of them cannot work", bridgeRemedy)
+ }
+ if strings.Contains(bridgeRemedy, "sessions.agent.enabled") {
+ t.Errorf("the bridge remedy prescribes a config edit, which is the unescapable loop this fixes: %q", bridgeRemedy)
+ }
+}
+
+// An unknown path must stay a bare 404 saying nothing about sessions — in both modes. This
+// is the assertion that stops the fix over-firing onto typos, and the one the review
+// predicted nobody would write.
+func TestAnUnknownPathIsUntouchedAndSaysNothingAboutSessions(t *testing.T) {
+ for _, mode := range []struct {
+ name string
+ register func(*http.ServeMux)
+ }{
+ {"bridge", RegisterSessionsUnavailableInBridgeMode},
+ {"sessions disabled", RegisterSessionsDisabled},
+ } {
+ t.Run(mode.name, func(t *testing.T) {
+ mux := http.NewServeMux()
+ mode.register(mux)
+
+ // Session-ISH on purpose: a plausible typo is the case that would be
+ // swallowed by a prefix-matched registration.
+ for _, path := range []string{"/session", "/sessionz", "/sessions/me/extra", "/nope"} {
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
+
+ if w.Code != http.StatusNotFound {
+ t.Errorf("%s: status = %d, want 404", path, w.Code)
+ }
+ if strings.Contains(strings.ToLower(w.Body.String()), "session") {
+ t.Errorf("%s: an unknown path is diagnosed as a session problem it never asked about: %s", path, w.Body.String())
+ }
+ }
+ })
+ }
+}
diff --git a/internal/server/sessions_unavailable_wiring_test.go b/internal/server/sessions_unavailable_wiring_test.go
new file mode 100644
index 00000000..48d050fe
--- /dev/null
+++ b/internal/server/sessions_unavailable_wiring_test.go
@@ -0,0 +1,48 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/srccensus"
+)
+
+// The registrars are unit-tested against a bare mux, but that cannot show either MODE
+// calls one — and a mode that stops calling it is silently back to the bare mux 404 this
+// card exists to remove. Neither entry point is drivable from a unit test: RunBridgeServer
+// and RunDashboard bind a listener, launch a browser and block on signals.
+//
+// So the wiring is pinned at the source, inside the enclosing function rather than merely
+// somewhere in the package: a call that drifted out of the mode's own body would otherwise
+// still satisfy it.
+func TestBothModesWireTheSessionFamilyToACodedRefusal(t *testing.T) {
+ pkg := srccensus.Load(t, ".", 4)
+
+ for _, wiring := range []struct {
+ mode string
+ registrar string
+ why string
+ }{
+ {"RunBridgeServer", "RegisterSessionsUnavailableInBridgeMode",
+ "a bridge never mounts the family, so without this POST /sessions is a bare 404 and the CLI cannot tell it from a typo"},
+ {"RunDashboard", "RegisterSessionsDisabled",
+ "a full server with sessions.agent.enabled false does not mount the family either, and that is the one state whose remedy is the config edit"},
+ } {
+ t.Run(wiring.mode, func(t *testing.T) {
+ fn, ok := pkg.Func(wiring.mode)
+ if !ok {
+ t.Fatalf("%s not found in %s; if it was renamed, re-point this guard at the new entry point rather than deleting it", wiring.mode, pkg.Dir())
+ }
+
+ inside := 0
+ for _, site := range pkg.Calls(t, wiring.registrar) {
+ if pkg.Contains(fn, site) {
+ inside++
+ }
+ }
+ if inside == 0 {
+ t.Errorf("%s never calls %s, so that mode answers the session family with net/http's bare 404: %s",
+ wiring.mode, wiring.registrar, wiring.why)
+ }
+ })
+ }
+}
diff --git a/internal/session/routes.go b/internal/session/routes.go
new file mode 100644
index 00000000..e83ede57
--- /dev/null
+++ b/internal/session/routes.go
@@ -0,0 +1,23 @@
+package session
+
+import "slices"
+
+// routePatterns is the ONE definition of the agent-session family's mux patterns. Three
+// call sites range over it: the live registration in the dashboard package, and the two
+// unavailable-mode registrations in the server package. Keeping it here — beside the store
+// rather than beside any one registrar — is what makes it impossible for a route to be
+// mounted in server mode and answer a bare mux 404 in bridge mode, which is the defect
+// this list exists to prevent regenerating one route at a time.
+var routePatterns = []string{
+ "POST /sessions",
+ "GET /sessions",
+ "GET /sessions/me",
+ "GET /sessions/{id}",
+ "POST /sessions/{id}/revoke",
+}
+
+// RoutePatterns returns the family's mux patterns. The copy is deliberate: a caller that
+// mutated the slice would silently unmount a route in one mode only.
+func RoutePatterns() []string {
+ return slices.Clone(routePatterns)
+}