Skip to content

fix: cap search writer context before final answer generation#1053

Open
stablegenius49 wants to merge 1 commit intoItzCrazyKns:masterfrom
stablegenius49:pr-factory/issue-1011-limit-search-context
Open

fix: cap search writer context before final answer generation#1053
stablegenius49 wants to merge 1 commit intoItzCrazyKns:masterfrom
stablegenius49:pr-factory/issue-1011-limit-search-context

Conversation

@stablegenius49
Copy link

@stablegenius49 stablegenius49 commented Mar 11, 2026

Summary

  • cap the search-result context passed into the final writer call so large result sets do not overflow model context windows
  • truncate individual results with a small note while preserving source ordering and citations
  • apply the same capped context builder to both the main chat search flow and the API search flow

Testing

  • npm install
  • npx prettier --write src/lib/utils/splitText.ts src/lib/agents/search/context.ts src/lib/agents/search/index.ts src/lib/agents/search/api.ts
  • npx tsc --noEmit
  • npx eslint src/lib/utils/splitText.ts src/lib/agents/search/context.ts src/lib/agents/search/index.ts src/lib/agents/search/api.ts

Fixes #1011


Summary by cubic

Caps search-result context passed to the final writer to prevent model context window overflow. Truncates long results with a brief note and applies the cap to both chat and API search flows.

  • Bug Fixes
    • Add buildSearchResultsContext with a 20k-token total budget and 2.5k per-result cap.
    • Implement token-based truncation via truncateTextByTokens; export getTokenCount.
    • Escape result titles in attributes; preserve source order and citations.
    • Use the shared context builder in both SearchAgent and APISearchAgent.

Written for commit 0d09a9a. Summary will update on new commits.

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 4 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/context.ts">

<violation number="1" location="src/lib/agents/search/context.ts:48">
P2: Unescaped search result content is inserted into `<result>` blocks, so injected `</result>` or `<result>` text can break prompt boundaries and corrupt citations.</violation>
</file>

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

content = `${truncateTextByTokens(fullContent, noteBudget)}${TRUNCATION_NOTE}`;
}

const entry = `${prefix}${content}${suffix}`;
Copy link
Contributor

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

Choose a reason for hiding this comment

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

P2: Unescaped search result content is inserted into <result> blocks, so injected </result> or <result> text can break prompt boundaries and corrupt citations.

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

<comment>Unescaped search result content is inserted into `<result>` blocks, so injected `</result>` or `<result>` text can break prompt boundaries and corrupt citations.</comment>

<file context>
@@ -0,0 +1,60 @@
+      content = `${truncateTextByTokens(fullContent, noteBudget)}${TRUNCATION_NOTE}`;
+    }
+
+    const entry = `${prefix}${content}${suffix}`;
+    const entryTokens = getTokenCount(entry);
+
</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.

Search sometimes fails with “exceeds the available context size” error

2 participants