Skip to content

fix: guard against undefined .slice() calls in search actions#1033

Open
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin:fix/search-slice-typeerror-964
Open

fix: guard against undefined .slice() calls in search actions#1033
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin:fix/search-slice-typeerror-964

Conversation

@MaxwellCalkin
Copy link

@MaxwellCalkin MaxwellCalkin commented Mar 8, 2026

Summary

Fixes #964

Adds defensive null checks (?? []) before .slice() calls in the search execution pipeline to prevent TypeError: Cannot read properties of undefined (reading 'slice').

The crash occurs when LLM tool call arguments arrive with queries or urls as undefined (common with LiteLLM proxies or models that don't perfectly conform to tool calling format), or when SearXNG returns an unexpected response shape without a results array.

Changes

  • webSearch.ts: Guard input.queries with ?? [] before .slice(0, 3); guard res.results with ?? [] before .map()
  • academicSearch.ts: Same guards for input.queries and res.results
  • socialSearch.ts: Same guards for input.queries and res.results
  • uploadsSearch.ts: Guard input.queries with ?? [] before .slice(0, 3)
  • scrapeURL.ts: Guard params.urls with ?? [] before .slice(0, 3)
  • searxng.ts: Guard data.results and data.suggestions with ?? [] at the source, preventing downstream crashes in all consumers

Root cause

The error trace from the issue:

TypeError: Cannot read properties of undefined (reading 'slice')
    at Object.execute (.next/server/chunks/641.js:41:596)
    at d.execute (.next/server/chunks/641.js:123:140)
    at d.executeAll (.next/server/chunks/641.js:123:218)
    at i.research (.next/server/chunks/641.js:541:988)

This maps to:

  1. Object.execute = action's execute() method calling .slice() on input.queries/params.urls
  2. d.execute / d.executeAll = ActionRegistry.execute()/executeAll()
  3. i.research = Researcher.research()

When LLM arguments are parsed from tool calls, the queries/urls fields can be undefined if the model response is malformed. Similarly, SearXNG can return responses without a results field.

Test plan

  • Search with a working SearXNG + standard LLM should still work normally
  • Search with a model that returns malformed tool call arguments (e.g. via LiteLLM proxy) should gracefully return empty results instead of crashing
  • Academic and social search modes should also be protected
  • URL scraping with undefined urls should not crash

Note: This PR was authored by Claude (AI), operated by @MaxwellCalkin.


Summary by cubic

Prevents search crashes by guarding .slice()/.map() calls when queries, urls, or SearXNG response fields are undefined. Fixes #964 so malformed tool-call arguments or empty SearXNG responses return empty results instead of throwing.

  • Bug Fixes
    • Guard input.queries and params.urls with ?? [] before .slice(0, 3) in web, academic, social, and uploads search actions and URL scraping (use local queries/urls vars).
    • Default SearXNG results and suggestions to [], and use (res.results ?? []) in actions before .map().

Written for commit 3452334. Summary will update on new commits.

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
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Cannot read properties of undefined (reading 'slice')

1 participant