chenhg5/cc-connect · error

session %q not found

Error message

session %q not found

What it means

SwitchSession in the SessionManager throws this when the requested target session cannot be located for the given user key. The manager iterates its known sessions for that user and, if no session ID or name matches `target`, returns this error instead of switching. It is a lookup failure, not a state corruption: the current active session is left untouched.

Source

Thrown at core/session.go:434

	sm.activeSession[userKey] = id
	sm.userSessions[userKey] = append(sm.userSessions[userKey], id)
	return s
}

func (sm *SessionManager) SwitchSession(userKey, target string) (*Session, error) {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	for _, sid := range sm.userSessions[userKey] {
		s := sm.sessions[sid]
		if s != nil && (s.ID == target || s.Name == target) {
			sm.activeSession[userKey] = s.ID
			s.MarkExplicitlyActivated()
			sm.saveLocked()
			return s, nil
		}
	}
	return nil, fmt.Errorf("session %q not found", target)
}

// SwitchToAgentSession finds or creates an internal session that maps to the
// given agent session ID. If an existing session already references agentSID,
// it becomes the active session. Otherwise a new session is created so the
// previous session's AgentSessionID is preserved in KnownAgentSessionIDs.
func (sm *SessionManager) SwitchToAgentSession(userKey, agentSID, agentName, summary string) *Session {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	for _, sid := range sm.userSessions[userKey] {
		s := sm.sessions[sid]
		if s == nil {
			continue
		}
		s.mu.Lock()
		aid := s.AgentSessionID
		s.mu.Unlock()

View on GitHub (pinned to 4000b2338a)

Solutions

  1. List available sessions first (ListSessions) and switch using an exact ID from that list
  2. Check the active session before switching if the intent was just to ensure a session exists — create one via the session manager instead of switching to a guessed ID
  3. If switching by user-facing name, verify the name normalization (case/whitespace) matches how the manager stores it

Example fix

// before
s, err := sm.SwitchSession(userKey, "my-session")
// after
sessions, _ := sm.ListSessions(userKey)
found := false
for _, s := range sessions { if strings.EqualFold(s.Name, "my-session") { found = true } }
if !found { sm.CreateSession(userKey, "my-session") }
s, err := sm.SwitchSession(userKey, "my-session")
Defensive patterns

Strategy: validation

Validate before calling

sessions, _ := sm.ListSessions(userKey)
ids := map[string]bool{}
for _, s := range sessions { ids[s.ID] = true; if s.Name != "" { ids[s.Name] = true } }
if !ids[target] { return fmt.Errorf("no such session %q; available: %v", target, sessions) }

Type guard

func sessionExists(sm *core.SessionManager, userKey, target string) bool {
    sessions, err := sm.ListSessions(userKey)
    if err != nil { return false }
    for _, s := range sessions {
        if s.ID == target || s.Name == target { return true }
    }
    return false
}

Try / catch

s, err := sm.SwitchSession(userKey, target)
if err != nil && strings.Contains(err.Error(), "not found") {
    s, err = sm.CreateSession(userKey, target)
}
if err != nil { slog.Error("switch session failed", "err", err) }

Prevention

When it happens

Trigger: Calling SwitchSession with a session ID that was never created, a stale ID from a deleted/expired session, or a human-typed name that matches no known session (e.g. from a /switch command handled by handleProjectSessionSwitch).

Common situations: Users typing /switch with a typo or a session from a previous process run; IDs cached in UI after the session list was reset; switching before any session has been created for that user.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/8436cc269a21a1ea. Report an issue: GitHub.