{"record":{"id":"0bfb48a7a8536929","repo":"github/copilot-sdk","slug":"failed-to-set-foreground-session-0bfb48","errorCode":null,"errorMessage":"Failed to set foreground session","messagePattern":"Failed to set foreground session","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"java/sdk/src/main/java/com/github/copilot/CopilotClient.java","lineNumber":1707,"sourceCode":"    /**\n     * Requests the TUI to switch to displaying the specified session.\n     * <p>\n     * This is only available when connecting to a server running in TUI+server mode\n     * (--ui-server).\n     *\n     * @param sessionId\n     *            the ID of the session to display in the TUI\n     * @return a future that completes when the operation is done\n     * @throws RuntimeException\n     *             if the operation fails\n     */\n    public CompletableFuture<Void> setForegroundSessionId(String sessionId) {\n        return ensureConnected().thenCompose(connection -> connection.rpc\n                .invoke(\"session.setForeground\", new com.github.copilot.rpc.SetForegroundSessionRequest(sessionId),\n                        com.github.copilot.rpc.SetForegroundSessionResponse.class)\n                .thenAccept(response -> {\n                    if (!response.success()) {\n                        throw new RuntimeException(\n                                response.error() != null ? response.error() : \"Failed to set foreground session\");\n                    }\n                }));\n    }\n\n    /**\n     * Subscribes to all session lifecycle events.\n     * <p>\n     * Lifecycle events are emitted when sessions are created, deleted, updated, or\n     * change foreground/background state (in TUI+server mode).\n     *\n     * @param handler\n     *            a callback that receives lifecycle events\n     * @return an AutoCloseable that, when closed, unsubscribes the handler\n     */\n    public AutoCloseable onLifecycle(SessionLifecycleHandler handler) {\n        return lifecycleManager.subscribe(handler);\n    }","sourceCodeStart":1689,"sourceCodeEnd":1725,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/java/sdk/src/main/java/com/github/copilot/CopilotClient.java#L1689-L1725","documentation":"setForegroundSessionId invokes session.setForeground; if the server reports success=false, the client throws RuntimeException with the server's error message, or the generic \"Failed to set foreground session\" when no error detail was provided.","triggerScenarios":"Calling setForegroundSessionId with a sessionId the server does not recognize or cannot make foreground; response.success() false in SetForegroundSessionResponse.","commonSituations":"Setting foreground on a session that was already deleted/expired; id from a different client instance; race with session termination.","solutions":["Verify the sessionId belongs to a live session in this client (client.sessions map) before calling","Handle the failed future and inspect response.error() for the server's reason","Re-create the session if its id is no longer valid, then set foreground on the new id"],"exampleFix":"// before\nclient.setForegroundSessionId(staleId).join();\n// after\nif (client.getSession(staleId) != null) {\n    client.setForegroundSessionId(staleId).join();\n}","handlingStrategy":"try-catch","validationCode":"if (!isLiveSession(client, sessionId)) { return; }","typeGuard":null,"tryCatchPattern":"client.setForegroundSessionId(id)\n    .exceptionally(ex -> {\n        log.warn(\"setForeground failed for {}: {}\", id, ex.getMessage());\n        return null;\n    });","preventionTips":["Only set foreground on sessions owned by this client instance","Re-check session liveness after reconnects","Surface response.error() to users rather than the generic message"],"tags":["rpc","session","foreground"],"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-15T23:17:13.987Z"}