Skip to content

fix(ai-sdk): clamp search limit to 1-50 and add client timeout - #1583

Open
Sravanjangam wants to merge 1 commit into
supermemoryai:mainfrom
Sravanjangam:fix/ai-sdk-tool-bounds
Open

fix(ai-sdk): clamp search limit to 1-50 and add client timeout#1583
Sravanjangam wants to merge 1 commit into
supermemoryai:mainfrom
Sravanjangam:fix/ai-sdk-tool-bounds

Conversation

@Sravanjangam

@Sravanjangam Sravanjangam commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Hi supermemory team 👋 Thanks for building such a great product! While running a security & quality audit of the repo we hit this issue and put together a small, tested fix — details below.

Problem

In packages/ai-sdk, search_memories exposed a limit parameter that was an unconstrained LLM-controlled number flowing into the metered Search API with full documents included — a runaway agent loop could issue arbitrarily large queries. The underlying client also had no timeout or retry bound, so hung requests stalled agent turns indefinitely.

Part of #1578 (finding M8).

Solution

Constrain at both ends: the zod schema rejects out-of-range values up front, a runtime clamp defends against non-schema callers, and the HTTP client gets bounded timeouts/retries.

Changes

  • packages/ai-sdk/src/tools.ts → schema limit: integer().min(1).max(50); clampSearchLimit() applied at the call site; client constructed with 30s timeout / maxRetries: 2
  • packages/ai-sdk/src/limit.test.ts (new) → 4 tests (schema bounds + clamp behavior)

Verification

Fresh from the committed branch: bunx vitest run src/limit.test.ts4/4 pass; bunx tsc --noEmit → exit 0 (package fully clean); Biome clean.


Happy to iterate on any of this — feedback and reworks very welcome! 🙏

Environment

  • macOS 26.1 (arm64) · bun 1.4.0 · node v26.7.0
  • vitest 3.2.4 (workspace-pinned) · Biome lint clean
  • Branch fix/ai-sdk-tool-bounds — all gates re-run fresh at commit 287e8c24054e

- search_memories limit is now an integer constrained to 1-50 in the
  JSON schema AND clamped at execute time (clampSearchLimit): negative,
  fractional, or huge model-supplied values previously flowed straight
  into the metered Search API with full documents included, flooding
  agent context and inflating cost.
- Supermemory client now sets timeout: 30s / maxRetries: 2 so a hung
  connection can't stall an agent's tool loop indefinitely.
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.

1 participant