{"record":{"id":"83152271f4ec5203","repo":"github/copilot-sdk","slug":"failed-to-unmarshal-setforeground-response-w","errorCode":null,"errorMessage":"failed to unmarshal setForeground response: %w","messagePattern":"failed to unmarshal setForeground response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/client.go","lineNumber":1729,"sourceCode":"//\n// Example:\n//\n//\tif err := client.SetForegroundSessionID(\"session-123\"); err != nil {\n//\t    log.Fatal(err)\n//\t}\nfunc (c *Client) SetForegroundSessionID(ctx context.Context, sessionID string) error {\n\tif err := c.ensureConnected(ctx); err != nil {\n\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//","sourceCodeStart":1711,"sourceCodeEnd":1747,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/client.go#L1711-L1747","documentation":"Returned by Client.SetForegroundSessionID when the session.setForeground RPC succeeds but its JSON result cannot be decoded into setForegroundSessionResponse; the json.Unmarshal error is wrapped via %w. It signals that the CLI returned an unexpected response shape, typically a client/CLI version mismatch.","triggerScenarios":"Calling SetForegroundSessionID when the backend's response is not the expected {success, error} JSON object.","commonSituations":"Backend version drift; TUI not running so the backend returns an error string instead of the ack object; IPC stream corruption.","solutions":["Log the raw result to see the returned payload","Confirm the TUI is running and supports foreground switching","Update client library and backend to the same version","Retry after confirming the TUI state"],"exampleFix":"// before\nerr := client.SetForegroundSessionID(ctx, id)\n// after\nif err := client.SetForegroundSessionID(ctx, id); err != nil {\n    if strings.Contains(err.Error(), \"setForeground response\") {\n        log.Printf(\"unexpected ack from backend: %v\", err)\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":"if targetSessionID == \"\" {\n    return fmt.Errorf(\"SetForegroundSessionID: empty sessionID\")\n}","typeGuard":"func isSetForegroundUnmarshalError(err error) bool {\n    return strings.Contains(err.Error(), \"setForeground response\")\n}","tryCatchPattern":"if err := client.SetForegroundSessionID(ctx, id); err != nil {\n    if isSetForegroundUnmarshalError(err) {\n        // ack shape unexpected: verify TUI running, retry\n        return fmt.Errorf(\"setForeground ack undecodable: %w\", err)\n    }\n    return err\n}","preventionTips":["Confirm the TUI is running before switching foreground sessions","Keep client/backend versions aligned","Log raw ack payloads in debug mode"],"tags":["json","unmarshal","foreground-session"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}