fix: guard against undefined .slice() calls in search actions#1033
Open
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
fix: guard against undefined .slice() calls in search actions#1033MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
When LLM tool call arguments are malformed or SearXNG returns an unexpected response shape, calling .slice() on undefined crashes the search pipeline with 'TypeError: Cannot read properties of undefined (reading "slice")'. Changes: - webSearch, academicSearch, socialSearch, uploadsSearch: guard input.queries with ?? [] before .slice(0, 3) - scrapeURL: guard params.urls with ?? [] before .slice(0, 3) - searxng.ts: guard data.results and data.suggestions with ?? [] to prevent downstream .map() on undefined Fixes ItzCrazyKns#964
Contributor
There was a problem hiding this comment.
1 issue found across 6 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/lib/agents/search/researcher/actions/webSearch.ts">
<violation number="1" location="src/lib/agents/search/researcher/actions/webSearch.ts:88">
P2: `?? []` only guards null/undefined; non-array `input.queries` can still break `.map()` and crash action execution.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| config.classification.classification.skipSearch === false, | ||
| execute: async (input, additionalConfig) => { | ||
| input.queries = input.queries.slice(0, 3); | ||
| const queries = (input.queries ?? []).slice(0, 3); |
Contributor
There was a problem hiding this comment.
P2: ?? [] only guards null/undefined; non-array input.queries can still break .map() and crash action execution.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/agents/search/researcher/actions/webSearch.ts, line 88:
<comment>`?? []` only guards null/undefined; non-array `input.queries` can still break `.map()` and crash action execution.</comment>
<file context>
@@ -85,7 +85,7 @@ const webSearchAction: ResearchAction<typeof actionSchema> = {
config.classification.classification.skipSearch === false,
execute: async (input, additionalConfig) => {
- input.queries = input.queries.slice(0, 3);
+ const queries = (input.queries ?? []).slice(0, 3);
const researchBlock = additionalConfig.session.getBlock(
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #964
Adds defensive null checks (
?? []) before.slice()calls in the search execution pipeline to preventTypeError: Cannot read properties of undefined (reading 'slice').The crash occurs when LLM tool call arguments arrive with
queriesorurlsasundefined(common with LiteLLM proxies or models that don't perfectly conform to tool calling format), or when SearXNG returns an unexpected response shape without aresultsarray.Changes
webSearch.ts: Guardinput.querieswith?? []before.slice(0, 3); guardres.resultswith?? []before.map()academicSearch.ts: Same guards forinput.queriesandres.resultssocialSearch.ts: Same guards forinput.queriesandres.resultsuploadsSearch.ts: Guardinput.querieswith?? []before.slice(0, 3)scrapeURL.ts: Guardparams.urlswith?? []before.slice(0, 3)searxng.ts: Guarddata.resultsanddata.suggestionswith?? []at the source, preventing downstream crashes in all consumersRoot cause
The error trace from the issue:
This maps to:
Object.execute= action'sexecute()method calling.slice()oninput.queries/params.urlsd.execute/d.executeAll=ActionRegistry.execute()/executeAll()i.research=Researcher.research()When LLM arguments are parsed from tool calls, the
queries/urlsfields can beundefinedif the model response is malformed. Similarly, SearXNG can return responses without aresultsfield.Test plan
Note: This PR was authored by Claude (AI), operated by @MaxwellCalkin.
Summary by cubic
Prevents search crashes by guarding
.slice()/.map()calls whenqueries,urls, or SearXNG response fields are undefined. Fixes #964 so malformed tool-call arguments or empty SearXNG responses return empty results instead of throwing.input.queriesandparams.urlswith?? []before.slice(0, 3)in web, academic, social, and uploads search actions and URL scraping (use localqueries/urlsvars).resultsandsuggestionsto[], and use(res.results ?? [])in actions before.map().Written for commit 3452334. Summary will update on new commits.