{"record":{"id":"3eca63eb8804de59","repo":"charmbracelet/crush","slug":"invalid-response-format-missing-search-field","errorCode":null,"errorMessage":"invalid response format: missing search field","messagePattern":"invalid response format: missing search field","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/sourcegraph.go","lineNumber":216,"sourceCode":"\t\t\tcontinue\n\t\t}\n\t\tmessage, ok := errMap[\"message\"].(string)\n\t\tif ok {\n\t\t\tfmt.Fprintf(buffer, \"- %s\\n\", message)\n\t\t}\n\t}\n\treturn true\n}\n\nfunc sourcegraphSearchResults(result map[string]any) (map[string]any, error) {\n\tdata, ok := result[\"data\"].(map[string]any)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"invalid response format: missing data field\")\n\t}\n\n\tsearch, ok := data[\"search\"].(map[string]any)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"invalid response format: missing search field\")\n\t}\n\n\tsearchResults, ok := search[\"results\"].(map[string]any)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"invalid response format: missing results field\")\n\t}\n\treturn searchResults, nil\n}\n\nfunc writeSourcegraphHeader(buffer *strings.Builder, searchResults map[string]any) {\n\tmatchCount, _ := searchResults[\"matchCount\"].(float64)\n\tresultCount, _ := searchResults[\"resultCount\"].(float64)\n\tlimitHit, _ := searchResults[\"limitHit\"].(bool)\n\n\tbuffer.WriteString(\"# Sourcegraph Search Results\\n\\n\")\n\tfmt.Fprintf(buffer, \"Found %d matches across %d results\\n\", int(matchCount), int(resultCount))\n\n\tif limitHit {","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/tools/sourcegraph.go#L198-L234","documentation":"Raised by sourcegraphSearchResults when data[\"search\"] is missing or not a map (internal/agent/tools/sourcegraph.go:214-217). The expected successful envelope is data.search.results; a missing search key means the GraphQL query resolved but the Search connection was absent — typically because the query failed validation server-side and returned errors/null alongside a partially shaped data object.","triggerScenarios":"formatSourcegraphResults -> sourcegraphSearchResults sees data without a \"search\" map: invalid or unsupported query syntax causing Sourcegraph to omit the search field, GraphQL returning {\"data\":{}} with a separate errors array, or an API schema change renaming/moving the search connection.","commonSituations":"Malformed Sourcegraph query strings (bad patternType combos, unbalanced quotes) that yield errors plus a degenerate data object; deprecated query syntax after a Sourcegraph upgrade; self-hosted instances running versions whose schema differs from sourcegraph.com.","solutions":["Print the raw response to check for a top-level \"errors\" array explaining why data.search is absent.","Simplify and re-validate the query (start with a plain term search) to rule out query syntax problems.","Check the Sourcegraph GraphQL schema for changes to the search field and update the hardcoded query string in the tool if it drifted.","For self-hosted endpoints, confirm the instance version still supports the V2 search API used in the query."],"exampleFix":"null","handlingStrategy":"type-guard","validationCode":"search, ok := data[\"search\"].(map[string]any)\nif !ok {\n    if errs, ok := result[\"errors\"].([]any); ok && len(errs) > 0 {\n        return fmt.Errorf(\"search failed: %v\", errs)\n    }\n}","typeGuard":"func extractSearch(data map[string]any) (map[string]any, bool) {\n    search, ok := data[\"search\"].(map[string]any)\n    return search, ok\n}","tryCatchPattern":"formatted, err := formatSourcegraphResults(result, ctxWindow, count)\nif err != nil {\n    // surface err text to the model; suggest simplifying the query\n    return fantasy.NewTextErrorResponse(\"Failed to format results: \" + err.Error()), nil\n}","preventionTips":["Check for a top-level errors array whenever data.search is missing.","Validate query syntax with simple term searches before complex patterns.","Track Sourcegraph schema changes if self-hosting; update the query string accordingly.","Surface the error text to the calling agent so it can rephrase the query."],"tags":["graphql","schema","query","sourcegraph"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}