Skip to content

fix: guard against undefined slice/map crashes in search actions#1091

Open
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
octo-patch:fix/issue-964-guard-undefined-slice-in-actions
Open

fix: guard against undefined slice/map crashes in search actions#1091
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
octo-patch:fix/issue-964-guard-undefined-slice-in-actions

Conversation

@octo-patch
Copy link
Copy Markdown

@octo-patch octo-patch commented Apr 4, 2026

Fixes #964

Problem

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

  1. LLM tool calls (via LiteLLM proxy or models that don't strictly conform to the JSON schema) return queries/urls as undefined
  2. SearXNG returns a response without a results field (e.g. on error responses or unexpected formats), causing downstream .map() to crash

The webSearch, academicSearch, and socialSearch actions already had defensive Array.isArray() checks, but uploadsSearch and scrapeURL were missing them. The root-level searxng.ts also assumed data.results and data.suggestions were always present.

Solution

  • Default queries to [] before .slice(0, 3) in uploadsSearch.ts and scrapeURL.ts
  • Default SearXNG results and suggestions to [] at the source in searxng.ts, protecting all consumers

Testing

  • Normal searches continue to work as before
  • Malformed tool call arguments (undefined queries/urls) now return empty results instead of crashing
  • SearXNG returning unexpected response shapes no longer causes unhandled rejections

Summary by cubic

Prevents TypeError crashes in search actions by defaulting undefined inputs and SearXNG fields to empty arrays (fixes #964). Malformed tool calls and error responses now return empty results instead of throwing.

  • Bug Fixes
    • Default queries/urls to [] before .slice() in uploadsSearch and scrapeURL.
    • Default SearXNG results/suggestions to [] in searxng.ts.

Written for commit 68105d3. Summary will update on new commits.

When LLM tool calls return malformed arguments (e.g. via LiteLLM
proxies or models that don't strictly follow the tool schema), the
queries/urls fields can be undefined, causing a TypeError at the
.slice() call. Similarly, SearXNG can return responses without a
results field, causing downstream .map() crashes.

- Default queries/urls to [] before .slice() in uploadsSearch and scrapeURL
- Default SearXNG results/suggestions to [] at the source (fixes ItzCrazyKns#964)
Copy link
Copy Markdown
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.

3 issues found across 3 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/searxng.ts">

<violation number="1" location="src/lib/searxng.ts:55">
P2: `?? []` does not validate runtime response shape; non-array `results`/`suggestions` can still propagate and trigger downstream `.forEach`/`.map` crashes.</violation>
</file>

<file name="src/lib/agents/search/researcher/actions/scrapeURL.ts">

<violation number="1" location="src/lib/agents/search/researcher/actions/scrapeURL.ts:28">
P2: `?? []` avoids only null/undefined, but non-array `urls` values can still crash `.slice()`/`.map()` at runtime.</violation>
</file>

<file name="src/lib/agents/search/researcher/actions/uploadsSearch.ts">

<violation number="1" location="src/lib/agents/search/researcher/actions/uploadsSearch.ts:30">
P2: The nullish fallback is incomplete: `queries` is not validated as an array, so malformed non-array inputs can still crash or propagate wrong types.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/lib/searxng.ts

const results: SearxngSearchResult[] = data.results;
const suggestions: string[] = data.suggestions;
const results: SearxngSearchResult[] = data.results ?? [];
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 4, 2026

Choose a reason for hiding this comment

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

P2: ?? [] does not validate runtime response shape; non-array results/suggestions can still propagate and trigger downstream .forEach/.map crashes.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/searxng.ts, line 55:

<comment>`?? []` does not validate runtime response shape; non-array `results`/`suggestions` can still propagate and trigger downstream `.forEach`/`.map` crashes.</comment>

<file context>
@@ -52,8 +52,8 @@ export const searchSearxng = async (
 
-    const results: SearxngSearchResult[] = data.results;
-    const suggestions: string[] = data.suggestions;
+    const results: SearxngSearchResult[] = data.results ?? [];
+    const suggestions: string[] = data.suggestions ?? [];
 
</file context>
Fix with Cubic

enabled: (_) => true,
execute: async (params, additionalConfig) => {
params.urls = params.urls.slice(0, 3);
params.urls = (params.urls ?? []).slice(0, 3);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 4, 2026

Choose a reason for hiding this comment

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

P2: ?? [] avoids only null/undefined, but non-array urls values can still crash .slice()/.map() at runtime.

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/scrapeURL.ts, line 28:

<comment>`?? []` avoids only null/undefined, but non-array `urls` values can still crash `.slice()`/`.map()` at runtime.</comment>

<file context>
@@ -25,7 +25,7 @@ const scrapeURLAction: ResearchAction<typeof schema> = {
   enabled: (_) => true,
   execute: async (params, additionalConfig) => {
-    params.urls = params.urls.slice(0, 3);
+    params.urls = (params.urls ?? []).slice(0, 3);
 
     let readingBlockId = crypto.randomUUID();
</file context>
Suggested change
params.urls = (params.urls ?? []).slice(0, 3);
const urls = Array.isArray(params.urls) ? params.urls : [];
params.urls = urls.slice(0, 3);
Fix with Cubic

`,
execute: async (input, additionalConfig) => {
input.queries = input.queries.slice(0, 3);
input.queries = (input.queries ?? []).slice(0, 3);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 4, 2026

Choose a reason for hiding this comment

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

P2: The nullish fallback is incomplete: queries is not validated as an array, so malformed non-array inputs can still crash or propagate wrong types.

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/uploadsSearch.ts, line 30:

<comment>The nullish fallback is incomplete: `queries` is not validated as an array, so malformed non-array inputs can still crash or propagate wrong types.</comment>

<file context>
@@ -27,7 +27,7 @@ const uploadsSearchAction: ResearchAction<typeof schema> = {
   `,
   execute: async (input, additionalConfig) => {
-    input.queries = input.queries.slice(0, 3);
+    input.queries = (input.queries ?? []).slice(0, 3);
 
     const researchBlock = additionalConfig.session.getBlock(
</file context>
Suggested change
input.queries = (input.queries ?? []).slice(0, 3);
input.queries = Array.isArray(input.queries)
? input.queries.slice(0, 3)
: [];
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