{"record":{"id":"53e52aaf80b53189","repo":"github/copilot-sdk","slug":"failed-to-set-foreground-session-s","errorCode":null,"errorMessage":"failed to set foreground session: %s","messagePattern":"failed to set foreground session: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/client.go","lineNumber":1737,"sourceCode":"\t\treturn err\n\t}\n\n\tresult, err := c.client.Request(ctx, \"session.setForeground\", setForegroundSessionRequest{SessionID: sessionID})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar response setForegroundSessionResponse\n\tif err := json.Unmarshal(result, &response); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal setForeground response: %w\", err)\n\t}\n\n\tif !response.Success {\n\t\terrorMsg := \"unknown error\"\n\t\tif response.Error != nil {\n\t\t\terrorMsg = *response.Error\n\t\t}\n\t\treturn fmt.Errorf(\"failed to set foreground session: %s\", errorMsg)\n\t}\n\n\treturn nil\n}\n\n// On subscribes to all session lifecycle events.\n//\n// Lifecycle events are emitted when sessions are created, deleted, updated,\n// or change foreground/background state (in TUI+server mode).\n//\n// Returns a function that, when called, unsubscribes the handler.\n//\n// Example:\n//\n//\tunsubscribe := client.On(func(event copilot.SessionLifecycleEvent) {\n//\t    fmt.Printf(\"Session %s: %s\\n\", event.SessionID, event.Type)\n//\t})\n//\tdefer unsubscribe()","sourceCodeStart":1719,"sourceCodeEnd":1755,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/client.go#L1719-L1755","documentation":"SetForegroundSessionID returns this error when the backend explicitly reports the switch failed: setForegroundSessionResponse.Success was false. The backend's error string (or 'unknown error') is included, making this a reported backend-side rejection rather than a decode failure.","triggerScenarios":"Calling SetForegroundSessionID and receiving response.Success == false — e.g. the target session no longer exists or the TUI refuses the switch.","commonSituations":"Switching to a session that was deleted or never existed; TUI running headless; stale session ID cached in the application.","solutions":["Read the backend error text embedded after the colon","Verify the target session exists via ListSessions before switching","Refresh the cached session ID (e.g. via GetLastSessionID) and retry","Handle the case where no TUI is attached — the switch cannot be performed"],"exampleFix":"// before\nerr := client.SetForegroundSessionID(ctx, targetID)\nif err != nil { panic(err) }\n// after\nif err := client.SetForegroundSessionID(ctx, targetID); err != nil {\n    log.Printf(\"failed to focus session %s: %v\", targetID, err)\n    // fall back to listing sessions and picking a valid one\n}","handlingStrategy":"try-catch","validationCode":"sessions, _ := client.ListSessions(ctx)\nif !slices.ContainsFunc(sessions, func(s *SessionInfo) bool { return s.ID == targetSessionID }) {\n    return fmt.Errorf(\"cannot focus: session %s does not exist\", targetSessionID)\n}","typeGuard":null,"tryCatchPattern":"if err := client.SetForegroundSessionID(ctx, id); err != nil {\n    if strings.Contains(err.Error(), \"failed to set foreground session\") {\n        // backend refused: read reason, refresh session list and pick a valid ID\n        return fmt.Errorf(\"focus refused: %w\", err)\n    }\n    return err\n}","preventionTips":["Verify the target session exists before switching","Refresh cached session IDs after deletes","Handle headless/no-TUI operation explicitly"],"tags":["foreground-session","backend-error","session"],"backgroundTag":"api-error-response","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}