{"record":{"id":"7a43adf53d0488f0","repo":"github/copilot-sdk","slug":"failed-to-unmarshal-getforeground-response-w","errorCode":null,"errorMessage":"failed to unmarshal getForeground response: %w","messagePattern":"failed to unmarshal getForeground response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/client.go","lineNumber":1701,"sourceCode":"//\tif err != nil {\n//\t    log.Fatal(err)\n//\t}\n//\tif sessionID != nil {\n//\t    fmt.Printf(\"TUI is displaying session: %s\\n\", *sessionID)\n//\t}\nfunc (c *Client) GetForegroundSessionID(ctx context.Context) (*string, error) {\n\tif err := c.ensureConnected(ctx); err != nil {\n\t\treturn nil, err\n\t}\n\n\tresult, err := c.client.Request(ctx, \"session.getForeground\", getForegroundSessionRequest{})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar response getForegroundSessionResponse\n\tif err := json.Unmarshal(result, &response); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal getForeground response: %w\", err)\n\t}\n\n\treturn response.SessionID, nil\n}\n\n// SetForegroundSessionID requests the TUI to switch to displaying the specified session.\n//\n// This is only available when connecting to a server running in TUI+server mode\n// (--ui-server).\n//\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","sourceCodeStart":1683,"sourceCodeEnd":1719,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/client.go#L1683-L1719","documentation":"GetForegroundSessionID returns this error when the 'session.getForeground' result cannot be unmarshalled into getForegroundSessionResponse. The backend responded but the payload shape did not match the expected struct.","triggerScenarios":"Calling GetForegroundSessionID while the TUI/backend returns a foreground-session payload that does not decode into getForegroundSessionResponse.","commonSituations":"Calling before the TUI is fully started; backend version mismatch; TUI running in a mode that does not track foreground sessions.","solutions":["Ensure the TUI/backend is fully started before polling for the foreground session","Dump the raw result and compare with getForegroundSessionResponse","Update client and backend to matching versions","Treat this as 'no foreground session' and fall back to GetLastSessionID or a new session"],"exampleFix":"// before\nid, err := client.GetForegroundSessionID(ctx)\n// after\nid, err := client.GetForegroundSessionID(ctx)\nif err != nil {\n    if strings.Contains(err.Error(), \"getForeground\") { id = \"\" } // no foreground session\n    else { return err }\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":"func isForegroundUnmarshalError(err error) bool {\n    return strings.Contains(err.Error(), \"getForeground response\")\n}","tryCatchPattern":"id, err := client.GetForegroundSessionID(ctx)\nif err != nil {\n    if isForegroundUnmarshalError(err) {\n        id = \"\" // no decodable foreground session\n    } else {\n        return err\n    }\n}","preventionTips":["Only poll foreground session after the TUI is ready","Keep client/backend versions aligned","Fall back to GetLastSessionID when the foreground query is undecodable"],"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"}