Skip to content

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

fix: an ended agent session closes the tabs it created and nobody else used, on every instance over the trusted-internal hop (PIN-368)

Luigi Agosti38007b192974
diff --git a/docs/core-concepts.md b/docs/core-concepts.md
index d5197c59..54df7b58 100644
--- a/docs/core-concepts.md
+++ b/docs/core-concepts.md
@@ -208,6 +208,11 @@ For managed instances started by the server:
That means the persistent part is the **profile state**, not the tab list.
+A tab opened under an agent session ends with it. When the session is revoked, expires or
+is pruned, each instance closes the tabs that session created, as long as no other caller
+has used them since. It keeps a tab that is paused for human handoff or locked, and logs
+the tab id.
+
## Element references
Snapshots return element references such as `e0`, `e1`, `e2`, and so on.
diff --git a/docs/reference/sessions.md b/docs/reference/sessions.md
index 08ffa07e..f7221afd 100644
--- a/docs/reference/sessions.md
+++ b/docs/reference/sessions.md
@@ -51,6 +51,7 @@ server refuses to load, so the repair does not need a hand edit.
1. **Create** — `pinchtab session create --agent-id <id>` (or `POST /sessions` directly)
2. **Use** — agent sends `Authorization: Session ses_...` with each request, or sets `PINCHTAB_SESSION`
3. **Revoke** — `pinchtab session revoke <session-id>` (or `POST /sessions/{id}/revoke`)
+4. **End** — revoking, expiring or pruning a session closes the tabs it created on every instance, except a tab another caller has used since, a tab paused for human handoff and a locked tab
## Security
diff --git a/internal/bridge/bridge_types.go b/internal/bridge/bridge_types.go
index 69a7dde2..646bf172 100644
--- a/internal/bridge/bridge_types.go
+++ b/internal/bridge/bridge_types.go
@@ -22,6 +22,8 @@ type TabEntry struct {
Policy TabPolicyState
Watching bool
ConsoleCaptureEnabled bool
+ CreatorScope string
+ LastScope string
// Lifecycle auto-close timer. autoCloseGen is bumped on every (re)schedule
// so a fire that races with a reset/cancel can detect itself and bail.
diff --git a/internal/bridge/tab_scope.go b/internal/bridge/tab_scope.go
new file mode 100644
index 00000000..94b8f421
--- /dev/null
+++ b/internal/bridge/tab_scope.go
@@ -0,0 +1,35 @@
+package bridge
+
+import "sort"
+
+func (tm *TabManager) RecordTabScope(tabID, scope string, created bool) {
+ if tm == nil || tabID == "" || scope == "" {
+ return
+ }
+ tm.mu.Lock()
+ defer tm.mu.Unlock()
+ entry, ok := tm.tabs[tabID]
+ if !ok {
+ return
+ }
+ if created && entry.CreatorScope == "" {
+ entry.CreatorScope = scope
+ }
+ entry.LastScope = scope
+}
+
+func (tm *TabManager) TabsOnlyUsedByCreator(scope string) []string {
+ if tm == nil || scope == "" {
+ return nil
+ }
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ var ids []string
+ for id, entry := range tm.tabs {
+ if entry.CreatorScope == scope && entry.LastScope == scope {
+ ids = append(ids, id)
+ }
+ }
+ sort.Strings(ids)
+ return ids
+}
diff --git a/internal/bridge/tab_scope_test.go b/internal/bridge/tab_scope_test.go
new file mode 100644
index 00000000..67422c0f
--- /dev/null
+++ b/internal/bridge/tab_scope_test.go
@@ -0,0 +1,33 @@
+package bridge
+
+import (
+ "context"
+ "testing"
+)
+
+func scopedTabManager(ids ...string) *TabManager {
+ tm := NewTabManager(context.Background(), nil, nil, nil, nil)
+ for _, id := range ids {
+ tm.tabs[id] = &TabEntry{Ctx: context.Background()}
+ }
+ return tm
+}
+
+func TestATabKeepsItsFirstCreatorAndTracksItsLastUser(t *testing.T) {
+ tm := scopedTabManager("own", "shared", "adopted")
+ tm.RecordTabScope("own", "session:s", true)
+ tm.RecordTabScope("shared", "session:s", true)
+ tm.RecordTabScope("shared", "session:t", false)
+ tm.RecordTabScope("adopted", "session:s", true)
+ tm.RecordTabScope("adopted", "session:t", true)
+ tm.RecordTabScope("adopted", "session:s", false)
+ tm.RecordTabScope("missing", "session:s", true)
+
+ got := tm.TabsOnlyUsedByCreator("session:s")
+ if len(got) != 2 || got[0] != "adopted" || got[1] != "own" {
+ t.Fatalf("TabsOnlyUsedByCreator(session:s) = %v, want [adopted own]", got)
+ }
+ if got := tm.TabsOnlyUsedByCreator("session:t"); len(got) != 0 {
+ t.Fatalf("TabsOnlyUsedByCreator(session:t) = %v, want none: t never created a tab", got)
+ }
+}
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index f68f9fb9..5f7ecd61 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -175,7 +175,7 @@ func (h *Handlers) runResolvedActionStep(
} else {
nextCtx = switchedCtx
nextTabID = switchedTabID
- markCreatedTab(w, nextTabID)
+ h.markCreatedTab(w, r, nextTabID)
h.recordResolvedTab(r, nextTabID)
}
}
@@ -677,7 +677,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
}
if switched := switchedTabFromActionResult(result); switched != "" {
h.setCurrentTabForRequest(r, switched)
- markCreatedTab(w, switched)
+ h.markCreatedTab(w, r, switched)
h.recordResolvedTab(r, switched)
}
actionRoute := routeMetadataFor(routing)
diff --git a/internal/handlers/activity_helpers.go b/internal/handlers/activity_helpers.go
index de31809d..130010bc 100644
--- a/internal/handlers/activity_helpers.go
+++ b/internal/handlers/activity_helpers.go
@@ -80,8 +80,27 @@ func (h *Handlers) recordResolvedTab(r *http.Request, tabID string) {
h.recordActivity(r, activity.Update{TabID: tabID})
}
-func markCreatedTab(w http.ResponseWriter, tabID string) {
- if w == nil || strings.TrimSpace(tabID) == "" {
+type tabScopeTracker interface {
+ RecordTabScope(tabID, scope string, created bool)
+ TabsOnlyUsedByCreator(scope string) []string
+}
+
+func (h *Handlers) tabScopes() (tabScopeTracker, bool) {
+ if h == nil {
+ return nil, false
+ }
+ tracker, ok := h.Bridge.(tabScopeTracker)
+ return tracker, ok
+}
+
+func (h *Handlers) markCreatedTab(w http.ResponseWriter, r *http.Request, tabID string) {
+ if strings.TrimSpace(tabID) == "" {
+ return
+ }
+ if tracker, ok := h.tabScopes(); ok {
+ tracker.RecordTabScope(tabID, currentTabScopeFromRequest(r).key, true)
+ }
+ if w == nil {
return
}
w.Header().Set(activity.HeaderPTTabID, tabID)
@@ -89,10 +108,17 @@ func markCreatedTab(w http.ResponseWriter, tabID string) {
}
func (h *Handlers) setCurrentTabForRequest(r *http.Request, tabID string) {
- if h == nil || h.CurrentTabs == nil {
+ if h == nil {
+ return
+ }
+ scope := currentTabScopeFromRequest(r)
+ if tracker, ok := h.tabScopes(); ok {
+ tracker.RecordTabScope(tabID, scope.key, false)
+ }
+ if h.CurrentTabs == nil {
return
}
- h.CurrentTabs.Set(currentTabScopeFromRequest(r), tabID)
+ h.CurrentTabs.Set(scope, tabID)
}
func (h *Handlers) clearCurrentTabReferences(tabID string) {
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 37190ecf..5d09f085 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -472,6 +472,7 @@ var specialCaseRoutes = []string{
"GET /action",
"GET /tabs/{id}/state",
"POST /shutdown",
+ "POST " + SessionTabsClosePath,
}
// registerBridgeRoutes registers the bridge API surface by walking the shared
@@ -521,6 +522,7 @@ func (h *Handlers) registerSpecialRoutes(mux muxRegistrar, doShutdown func()) {
// is the ungated lightweight tab-runtime readiness view, so it is not a
// TabScoped catalog entry and is registered explicitly here.
mux.HandleFunc("GET /tabs/{id}/state", h.HandleTabState)
+ mux.HandleFunc("POST "+SessionTabsClosePath, h.HandleSessionTabsClose)
if doShutdown != nil {
mux.HandleFunc("POST /shutdown", h.HandleShutdown(doShutdown))
}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index f3efa304..c7a917b2 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -332,7 +332,7 @@ func (h *Handlers) tryStaticFirstNavigate(w http.ResponseWriter, r *http.Request
}
h.setCurrentTabForRequest(r, navResult.TabID)
h.recordResolvedURL(r, navResult.URL)
- markCreatedTab(w, navResult.TabID)
+ h.markCreatedTab(w, r, navResult.TabID)
httpx.JSON(w, 200, map[string]any{"tabId": navResult.TabID, "url": navResult.URL, "title": navResult.Title, "route": navRoute})
return staticFirstOutcome{handled: true}
}
@@ -565,7 +565,7 @@ func (h *Handlers) runNavigate(w http.ResponseWriter, r *http.Request, ex navExe
h.setCurrentTabForRequest(r, navResult.TabID)
if ex.isNewTab {
h.recordResolvedTab(r, navResult.TabID)
- markCreatedTab(w, navResult.TabID)
+ h.markCreatedTab(w, r, navResult.TabID)
}
h.recordResolvedURL(r, navResult.URL)
httpx.JSON(w, 200, navResponse(navResult.TabID, navResult.URL, navResult.Title, route, !ex.isNewTab))
@@ -585,7 +585,7 @@ func (h *Handlers) runNavigate(w http.ResponseWriter, r *http.Request, ex navExe
h.setCurrentTabForRequest(r, ex.tabID)
if ex.isNewTab {
h.recordResolvedTab(r, ex.tabID)
- markCreatedTab(w, ex.tabID)
+ h.markCreatedTab(w, r, ex.tabID)
}
h.recordResolvedURL(r, navURL)
diff --git a/internal/handlers/session_tabs.go b/internal/handlers/session_tabs.go
new file mode 100644
index 00000000..897ac161
--- /dev/null
+++ b/internal/handlers/session_tabs.go
@@ -0,0 +1,76 @@
+package handlers
+
+import (
+ "log/slog"
+ "net/http"
+ "strings"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/httpx"
+)
+
+const SessionTabsClosePath = "/internal/session-tabs/close"
+
+type keptSessionTab struct {
+ TabID string `json:"tabId"`
+ Reason string `json:"reason"`
+}
+
+type sessionTabsCloseResult struct {
+ Closed []string `json:"closed"`
+ Kept []keptSessionTab `json:"kept"`
+}
+
+func (h *Handlers) HandleSessionTabsClose(w http.ResponseWriter, r *http.Request) {
+ if !IsTrustedInternalProxy(r) {
+ httpx.NoRoute(w, r, http.StatusNotFound)
+ return
+ }
+ sessionID := strings.TrimSpace(r.Header.Get(activity.HeaderPTSessionID))
+ if sessionID == "" {
+ httpx.ErrorCode(w, http.StatusBadRequest, "session_id_required", activity.HeaderPTSessionID+" is required", false, nil)
+ return
+ }
+ httpx.JSON(w, http.StatusOK, h.closeSessionTabs(sessionID))
+}
+
+func (h *Handlers) closeSessionTabs(sessionID string) sessionTabsCloseResult {
+ scope := scopedCurrentTab(currentTabScopeSession, sessionID)
+ result := sessionTabsCloseResult{Closed: []string{}, Kept: []keptSessionTab{}}
+ if h.CurrentTabs != nil {
+ h.CurrentTabs.Clear(scope)
+ }
+ tracker, ok := h.tabScopes()
+ if !ok {
+ return result
+ }
+ for _, tabID := range tracker.TabsOnlyUsedByCreator(scope.key) {
+ reason := h.sessionTabKeepReason(tabID)
+ if reason == "" {
+ if err := h.Bridge.CloseTab(tabID); err != nil {
+ reason = "close failed: " + err.Error()
+ }
+ }
+ if reason != "" {
+ slog.Info("ended session's tab kept open", "sessionId", sessionID, "tabId", tabID, "reason", reason)
+ result.Kept = append(result.Kept, keptSessionTab{TabID: tabID, Reason: reason})
+ continue
+ }
+ h.clearCurrentTabReferences(tabID)
+ result.Closed = append(result.Closed, tabID)
+ }
+ if len(result.Closed) > 0 {
+ slog.Info("closed ended session's tabs", "sessionId", sessionID, "tabIds", result.Closed)
+ }
+ return result
+}
+
+func (h *Handlers) sessionTabKeepReason(tabID string) string {
+ if err := h.enforceTabNotPausedForHandoff(tabID); err != nil {
+ return "paused for human handoff"
+ }
+ if lock := h.Bridge.TabLockInfo(tabID); lock != nil {
+ return "locked by " + lock.Owner
+ }
+ return ""
+}
diff --git a/internal/handlers/session_tabs_test.go b/internal/handlers/session_tabs_test.go
new file mode 100644
index 00000000..7de789ff
--- /dev/null
+++ b/internal/handlers/session_tabs_test.go
@@ -0,0 +1,230 @@
+package handlers
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "log/slog"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "sort"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/testbrowser"
+)
+
+const sessionTabsSecret = "internal-secret"
+
+type sessionTabsFixture struct {
+ t *testing.T
+ b *bridge.Bridge
+ h *Handlers
+}
+
+func newSessionTabsFixture(t *testing.T) *sessionTabsFixture {
+ t.Helper()
+ chromePath := testbrowser.Path(t)
+ profile := testbrowser.ProfileDir(t)
+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(
+ chromedp.DefaultExecAllocatorOptions[:],
+ chromedp.ExecPath(chromePath),
+ chromedp.UserDataDir(profile),
+ chromedp.Flag("headless", true),
+ chromedp.Flag("no-sandbox", true),
+ )...)
+ ctx, cancelBrowser := chromedp.NewContext(alloc)
+ ctx, cancelTimeout := context.WithTimeout(ctx, 60*time.Second)
+ t.Cleanup(func() {
+ cancelTimeout()
+ cancelBrowser()
+ cancelAlloc()
+ _ = os.RemoveAll(profile)
+ })
+ if err := chromedp.Run(ctx, chromedp.Navigate("about:blank")); err != nil {
+ t.Fatal(err)
+ }
+ cfg := &config.RuntimeConfig{ActionTimeout: 5 * time.Second, DefaultBrowser: config.BrowserChrome, StateDir: t.TempDir()}
+ b := bridge.New(context.Background(), ctx, cfg)
+ b.RegisterTab("seed", ctx)
+ return &sessionTabsFixture{t: t, b: b, h: New(b, cfg, nil, nil, nil)}
+}
+
+func (f *sessionTabsFixture) serve(handler http.HandlerFunc, path, body, sessionID string, trusted bool) *httptest.ResponseRecorder {
+ f.t.Helper()
+ req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ if sessionID != "" {
+ req.Header.Set(activity.HeaderPTSessionID, sessionID)
+ }
+ if trusted {
+ req.Header.Set(InternalTokenHeader, sessionTabsSecret)
+ }
+ rec := httptest.NewRecorder()
+ TrustedInternalProxyStripMiddleware(sessionTabsSecret)(handler).ServeHTTP(rec, req)
+ return rec
+}
+
+func (f *sessionTabsFixture) newTab(sessionID string, trusted bool) string {
+ f.t.Helper()
+ rec := f.serve(f.h.HandleTab, "/tab", `{"action":"new"}`, sessionID, trusted)
+ var out struct {
+ TabID string `json:"tabId"`
+ }
+ if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.TabID == "" {
+ f.t.Fatalf("new tab for %q answered %d: %s", sessionID, rec.Code, rec.Body.String())
+ }
+ return out.TabID
+}
+
+func (f *sessionTabsFixture) closeFor(sessionID string) sessionTabsCloseResult {
+ f.t.Helper()
+ rec := f.serve(f.h.HandleSessionTabsClose, SessionTabsClosePath, "", sessionID, true)
+ if rec.Code != http.StatusOK {
+ f.t.Fatalf("close for %q answered %d: %s", sessionID, rec.Code, rec.Body.String())
+ }
+ var out sessionTabsCloseResult
+ if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
+ f.t.Fatal(err)
+ }
+ return out
+}
+
+func (f *sessionTabsFixture) open(tabID string) bool {
+ _, _, err := f.b.TabContext(tabID)
+ return err == nil
+}
+
+func keptIDs(kept []keptSessionTab) []string {
+ ids := make([]string, 0, len(kept))
+ for _, k := range kept {
+ ids = append(ids, k.TabID)
+ }
+ sort.Strings(ids)
+ return ids
+}
+
+func TestAnEndedSessionClosesOnlyTheTabsItCreatedAndNobodyElseUsed(t *testing.T) {
+ f := newSessionTabsFixture(t)
+ own := f.newTab("sess_s", true)
+ shared := f.newTab("sess_s", true)
+ paused := f.newTab("sess_s", true)
+ locked := f.newTab("sess_s", true)
+ other := f.newTab("sess_t", true)
+ global := f.newTab("", false)
+
+ if rec := f.serve(f.h.HandleTab, "/tab", `{"action":"focus","tabId":"`+shared+`"}`, "sess_t", true); rec.Code != http.StatusOK {
+ t.Fatalf("focus by the second session answered %d: %s", rec.Code, rec.Body.String())
+ }
+ if err := f.b.SetTabHandoff(paused, "captcha", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.b.Lock(locked, "agent-s", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+
+ logs := captureSlog(t)
+ first := f.closeFor("sess_s")
+ if len(first.Closed) != 1 || first.Closed[0] != own {
+ t.Fatalf("closed = %v, want only %s", first.Closed, own)
+ }
+ wantKept := []string{locked, paused}
+ sort.Strings(wantKept)
+ if got := keptIDs(first.Kept); len(got) != 2 || got[0] != wantKept[0] || got[1] != wantKept[1] {
+ t.Fatalf("kept = %+v, want the paused and locked tabs %v", first.Kept, wantKept)
+ }
+ for _, id := range wantKept {
+ if !strings.Contains(logs.String(), "tabId="+id) {
+ t.Errorf("the kept tab %s was not logged: %s", id, logs.String())
+ }
+ }
+ if f.open(own) {
+ t.Fatalf("the ended session's own tab %s is still open", own)
+ }
+ for name, id := range map[string]string{"used by another session": shared, "paused": paused, "locked": locked, "another session's": other, "global": global} {
+ if !f.open(id) {
+ t.Errorf("the %s tab %s was closed", name, id)
+ }
+ }
+
+ second := f.closeFor("sess_s")
+ if len(second.Closed) != 0 {
+ t.Fatalf("a second end of the same session closed %v, want nothing", second.Closed)
+ }
+}
+
+func TestTheSessionTabsCloseRouteIsUnroutedOffTheTrustedHop(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest(http.MethodPost, SessionTabsClosePath, nil)
+ req.Header.Set(activity.HeaderPTSessionID, "sess_s")
+ rec := httptest.NewRecorder()
+ TrustedInternalProxyStripMiddleware(sessionTabsSecret)(http.HandlerFunc(h.HandleSessionTabsClose)).ServeHTTP(rec, req)
+ var body struct {
+ Code string `json:"code"`
+ }
+ _ = json.Unmarshal(rec.Body.Bytes(), &body)
+ if rec.Code != http.StatusNotFound || body.Code != "not_found" {
+ t.Fatalf("an untrusted caller got %d %q: %s, want the unrouted 404", rec.Code, body.Code, rec.Body.String())
+ }
+}
+
+type syncBuffer struct {
+ mu sync.Mutex
+ buf bytes.Buffer
+}
+
+func (b *syncBuffer) Write(p []byte) (int, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.buf.Write(p)
+}
+
+func (b *syncBuffer) String() string {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.buf.String()
+}
+
+func captureSlog(t *testing.T) *syncBuffer {
+ t.Helper()
+ buf := &syncBuffer{}
+ previous := slog.Default()
+ slog.SetDefault(slog.New(slog.NewTextHandler(buf, nil)))
+ t.Cleanup(func() { slog.SetDefault(previous) })
+ return buf
+}
+
+func TestAPopupAClickOfTheSessionOpensIsClosedWithTheSession(t *testing.T) {
+ f := newSessionTabsFixture(t)
+ opener := f.newTab("sess_s", true)
+ ctx, _, err := f.b.TabContext(opener)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := chromedp.Run(ctx, chromedp.Evaluate(`document.body.innerHTML = '<a id="pop" href="about:blank" target="_blank">pop</a>'`, nil)); err != nil {
+ t.Fatal(err)
+ }
+ rec := f.serve(f.h.HandleAction, "/action", `{"kind":"click","selector":"#pop","tabId":"`+opener+`"}`, "sess_s", true)
+ var out struct {
+ Result map[string]any `json:"result"`
+ }
+ _ = json.Unmarshal(rec.Body.Bytes(), &out)
+ popup, _ := out.Result["switchedToTab"].(string)
+ if popup == "" {
+ t.Fatalf("the click opened no adopted popup (%d): %s", rec.Code, rec.Body.String())
+ }
+ closed := f.closeFor("sess_s").Closed
+ sort.Strings(closed)
+ want := []string{opener, popup}
+ sort.Strings(want)
+ if len(closed) != 2 || closed[0] != want[0] || closed[1] != want[1] {
+ t.Fatalf("closed = %v, want the opener and its popup %v", closed, want)
+ }
+}
diff --git a/internal/handlers/tab_lifecycle.go b/internal/handlers/tab_lifecycle.go
index 536487f0..d351d34f 100644
--- a/internal/handlers/tab_lifecycle.go
+++ b/internal/handlers/tab_lifecycle.go
@@ -116,7 +116,7 @@ func (h *Handlers) createBlankTab(w http.ResponseWriter, r *http.Request, browse
h.setCurrentTabForRequest(r, newTabID)
h.recordActivity(r, activity.Update{Action: "tab.new", TabID: newTabID, URL: curURL})
- markCreatedTab(w, newTabID)
+ h.markCreatedTab(w, r, newTabID)
response := map[string]any{"tabId": newTabID, "url": curURL, "title": title}
if browserContextID != "" {
response["browserContextId"] = browserContextID
diff --git a/internal/httpx/httpx.go b/internal/httpx/httpx.go
index ce609adc..c0428678 100644
--- a/internal/httpx/httpx.go
+++ b/internal/httpx/httpx.go
@@ -66,6 +66,14 @@ func Error(w http.ResponseWriter, code int, err error) {
ErrorCode(w, code, "error", message, false, nil)
}
+func NoRoute(w http.ResponseWriter, r *http.Request, status int) {
+ code := "not_found"
+ if status == http.StatusMethodNotAllowed {
+ code = "method_not_allowed"
+ }
+ ErrorCode(w, status, code, "no route for "+r.Method+" "+r.URL.Path, false, nil)
+}
+
func ErrorCode(w http.ResponseWriter, status int, code, message string, retryable bool, details map[string]any) {
logFailureCause(w, status, code, message)
sanitized := SanitizeErrorMessage(message)
diff --git a/internal/orchestrator/health_transport.go b/internal/orchestrator/health_transport.go
index f357294d..7ed84ce9 100644
--- a/internal/orchestrator/health_transport.go
+++ b/internal/orchestrator/health_transport.go
@@ -38,14 +38,21 @@ type memoryMetrics struct {
}
func (o *Orchestrator) instanceGet(ctx context.Context, inst *InstanceInternal, path string) (*http.Response, error) {
+ return o.instanceRequest(ctx, http.MethodGet, inst, path, nil)
+}
+
+func (o *Orchestrator) instanceRequest(ctx context.Context, method string, inst *InstanceInternal, path string, header http.Header) (*http.Response, error) {
target, err := o.instancePathURL(inst, path, "")
if err != nil {
return nil, err
}
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil)
+ req, err := http.NewRequestWithContext(ctx, method, target.String(), nil)
if err != nil {
return nil, err
}
+ for key, values := range header {
+ req.Header[key] = values
+ }
tagOrchestratorMonitoringRequest(req)
o.applyInstanceAuth(req, inst)
return o.client.Do(req)
diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go
index b8122dba..ee413e37 100644
--- a/internal/orchestrator/orchestrator.go
+++ b/internal/orchestrator/orchestrator.go
@@ -41,6 +41,7 @@ type Orchestrator struct {
// shutdown paths) can wait for them instead of leaking goroutines that
// race with stubbed package vars.
detachedStops sync.WaitGroup
+ sessionCloses sync.WaitGroup
// monitors tracks the per-instance startup monitors for the same reason.
// A monitor writes instance state for up to instanceStartupTimeout after
diff --git a/internal/orchestrator/proxy.go b/internal/orchestrator/proxy.go
index 0f077125..f88d947d 100644
--- a/internal/orchestrator/proxy.go
+++ b/internal/orchestrator/proxy.go
@@ -335,11 +335,15 @@ func (o *Orchestrator) applyInstanceAuth(req *http.Request, inst *InstanceIntern
// honors X-PinchTab-* identity headers we propagate. Attached external
// bridges have their own auth domain and won't recognize the token,
// which is the desired behavior.
- if inst.authToken == "" && o.internalToken != "" {
+ if o.hopIsTrusted(inst) {
req.Header.Set(handlers.InternalTokenHeader, o.internalToken)
}
}
+func (o *Orchestrator) hopIsTrusted(inst *InstanceInternal) bool {
+ return inst.authToken == "" && o.internalToken != ""
+}
+
func classifyLaunchError(err error) int {
msg := err.Error()
if strings.Contains(msg, "cannot contain") || strings.Contains(msg, "cannot be empty") {
diff --git a/internal/orchestrator/session_lifecycle.go b/internal/orchestrator/session_lifecycle.go
index 36f27f4d..b31ee556 100644
--- a/internal/orchestrator/session_lifecycle.go
+++ b/internal/orchestrator/session_lifecycle.go
@@ -1,19 +1,19 @@
package orchestrator
import (
+ "context"
+ "log/slog"
+ "net/http"
+ "sync"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/handlers"
"github.com/pinchtab/pinchtab/internal/session"
)
-// SessionLifecycleHook returns a session.LifecycleHook the caller can wire
-// into a session.Store via OnLifecycle. The hook drops the session →
-// instance binding so a future request that reuses the same session id
-// will be re-routed via the normal precedence rules instead of resurrecting
-// the old binding.
-//
-// The instance-side scoped current-tab map is bounded (LRU cap + stale
-// detection on the bridge), so we deliberately do NOT propagate eviction
-// over the network. Dead entries on the instance get pushed out by normal
-// traffic; this avoids a separate trusted internal HTTP surface.
+var sessionTabsCloseBudget = 10 * time.Second
+
func (o *Orchestrator) SessionLifecycleHook() session.LifecycleHook {
if o == nil {
return func(session.LifecycleEvent) {}
@@ -23,5 +23,43 @@ func (o *Orchestrator) SessionLifecycleHook() session.LifecycleHook {
return
}
o.bindings.ClearSession(evt.SessionID)
+ o.sessionCloses.Add(1)
+ go func() {
+ defer o.sessionCloses.Done()
+ o.closeEndedSessionTabs(evt.SessionID)
+ }()
+ }
+}
+
+func (o *Orchestrator) closeEndedSessionTabs(sessionID string) {
+ o.mu.RLock()
+ var targets []*InstanceInternal
+ for _, inst := range o.instances {
+ if inst.Status == "running" && instanceIsActive(inst) && o.hopIsTrusted(inst) {
+ targets = append(targets, inst)
+ }
+ }
+ o.mu.RUnlock()
+
+ ctx, cancel := context.WithTimeout(context.Background(), sessionTabsCloseBudget)
+ defer cancel()
+ header := http.Header{}
+ header.Set(activity.HeaderPTSessionID, sessionID)
+ var wg sync.WaitGroup
+ for _, inst := range targets {
+ wg.Add(1)
+ go func(inst *InstanceInternal) {
+ defer wg.Done()
+ resp, err := o.instanceRequest(ctx, http.MethodPost, inst, handlers.SessionTabsClosePath, header)
+ if err != nil {
+ slog.Warn("could not close an ended session's tabs", "sessionId", sessionID, "instanceId", inst.ID, "err", err)
+ return
+ }
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ slog.Warn("could not close an ended session's tabs", "sessionId", sessionID, "instanceId", inst.ID, "status", resp.StatusCode)
+ }
+ }(inst)
}
+ wg.Wait()
}
diff --git a/internal/orchestrator/session_lifecycle_test.go b/internal/orchestrator/session_lifecycle_test.go
index 46029d42..32c0a21f 100644
--- a/internal/orchestrator/session_lifecycle_test.go
+++ b/internal/orchestrator/session_lifecycle_test.go
@@ -1,8 +1,15 @@
package orchestrator
import (
+ "net/http"
+ "net/http/httptest"
+ "sync"
"testing"
+ "time"
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/handlers"
"github.com/pinchtab/pinchtab/internal/session"
)
@@ -27,3 +34,101 @@ func TestSessionLifecycleHook_NoopOnEmptyID(t *testing.T) {
t.Fatal("unrelated binding should not be touched")
}
}
+
+type recordedClose struct {
+ path, sessionID string
+ trusted bool
+}
+
+type closeRecorder struct {
+ mu sync.Mutex
+ calls []recordedClose
+}
+
+func (c *closeRecorder) handler(hang bool) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ c.mu.Lock()
+ c.calls = append(c.calls, recordedClose{
+ path: r.Method + " " + r.URL.Path,
+ sessionID: r.Header.Get(activity.HeaderPTSessionID),
+ trusted: r.Header.Get(handlers.InternalTokenHeader) != "",
+ })
+ c.mu.Unlock()
+ if hang {
+ <-r.Context().Done()
+ return
+ }
+ _, _ = w.Write([]byte(`{"closed":[],"kept":[]}`))
+ }
+}
+
+func (c *closeRecorder) snapshot() []recordedClose {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return append([]recordedClose(nil), c.calls...)
+}
+
+func addInstanceOver(t *testing.T, o *Orchestrator, id string, handler http.Handler, authToken string) {
+ t.Helper()
+ backend := httptest.NewServer(handler)
+ t.Cleanup(backend.Close)
+ o.client = backend.Client()
+ o.mu.Lock()
+ o.instances[id] = &InstanceInternal{
+ Instance: bridge.Instance{ID: id, ProfileName: id, Status: "running"},
+ URL: backend.URL,
+ authToken: authToken,
+ }
+ o.mu.Unlock()
+}
+
+func TestAnEndedSessionAsksEverySpawnedInstanceToCloseItsTabs(t *testing.T) {
+ for _, reason := range []string{session.LifecycleReasonRevoked, session.LifecycleReasonExpired, session.LifecycleReasonPruned} {
+ t.Run(reason, func(t *testing.T) {
+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})
+ spawnedA, spawnedB, attached := &closeRecorder{}, &closeRecorder{}, &closeRecorder{}
+ addInstanceOver(t, o, "inst_a", spawnedA.handler(false), "")
+ addInstanceOver(t, o, "inst_b", spawnedB.handler(false), "")
+ addInstanceOver(t, o, "inst_attached", attached.handler(false), "bridge-token")
+ o.bindings.BindSession("ses_dead", "inst_a")
+
+ o.SessionLifecycleHook()(session.LifecycleEvent{SessionID: "ses_dead", Reason: reason})
+ o.sessionCloses.Wait()
+
+ want := recordedClose{path: "POST " + handlers.SessionTabsClosePath, sessionID: "ses_dead", trusted: true}
+ for name, rec := range map[string]*closeRecorder{"inst_a": spawnedA, "inst_b": spawnedB} {
+ if calls := rec.snapshot(); len(calls) != 1 || calls[0] != want {
+ t.Errorf("%s received %+v, want one %+v", name, calls, want)
+ }
+ }
+ if calls := attached.snapshot(); len(calls) != 0 {
+ t.Errorf("the attached bridge received %+v: it cannot honour the trusted hop", calls)
+ }
+ if _, ok := o.bindings.ResolveSession("ses_dead"); ok {
+ t.Error("binding should have been cleared")
+ }
+ })
+ }
+}
+
+func TestAHangingInstanceNeitherStallsTheHookNorOutlivesTheCloseBudget(t *testing.T) {
+ previous := sessionTabsCloseBudget
+ sessionTabsCloseBudget = 200 * time.Millisecond
+ t.Cleanup(func() { sessionTabsCloseBudget = previous })
+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})
+ hanging := &closeRecorder{}
+ addInstanceOver(t, o, "inst_hang", hanging.handler(true), "")
+
+ started := time.Now()
+ o.SessionLifecycleHook()(session.LifecycleEvent{SessionID: "ses_dead", Reason: session.LifecycleReasonRevoked})
+ if elapsed := time.Since(started); elapsed > 50*time.Millisecond {
+ t.Fatalf("the hook took %v: the close must run off the dispatch goroutine", elapsed)
+ }
+ o.sessionCloses.Wait()
+ if elapsed := time.Since(started); elapsed < sessionTabsCloseBudget || elapsed > sessionTabsCloseBudget+300*time.Millisecond {
+ t.Fatalf("the close took %v against a hanging instance, want the %v budget", elapsed, sessionTabsCloseBudget)
+ }
+ if len(hanging.snapshot()) != 1 {
+ t.Fatal("the hanging instance was never asked")
+ }
+}
diff --git a/internal/server/notfound.go b/internal/server/notfound.go
index 9a5f5fbf..3bfa4683 100644
--- a/internal/server/notfound.go
+++ b/internal/server/notfound.go
@@ -28,11 +28,7 @@ func notFoundEnvelope(mux *http.ServeMux) http.Handler {
if status == 0 {
status = http.StatusNotFound
}
- code := "not_found"
- if status == http.StatusMethodNotAllowed {
- code = "method_not_allowed"
- }
- httpx.ErrorCode(w, status, code, "no route for "+r.Method+" "+r.URL.Path, false, nil)
+ httpx.NoRoute(w, r, status)
})
}