Skip to content

feat: add guard concurrency limits - #1162

Open
Th-Shivam wants to merge 1 commit into
superagent-ai:mainfrom
Th-Shivam:feat/guard-concurrency-limit
Open

Th-Shivam wants to merge 1 commit into
superagent-ai:mainfrom
Th-Shivam:feat/guard-concurrency-limit

Conversation

@Th-Shivam

@Th-Shivam Th-Shivam commented Sep 2, 2026 •

Copy link
Copy Markdown

Description

Closes #1161.

guard() previously used unbounded Promise.all() in the TypeScript SDK and unbounded asyncio.gather() in the Python SDK for text chunks and PDF pages. This adds optional per-call concurrency controls: maxConcurrency for TypeScript and max_concurrency for Python.

Both SDKs now support bounded execution for chunk and PDF-page analysis while preserving TypeScript/Python parity, input/result ordering, existing aggregation semantics, token usage accounting, and error behavior. Omitting the option preserves the existing legacy full-fan-out behavior. No default concurrency value was introduced.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

  • TypeScript focused tests: 55 passed
  • TypeScript full suite: 169 passed
  • Python focused guard tests: 40 passed
  • Python full suite: 158 passed
  • TypeScript type-check passed
  • npm run build passed
  • Python compileall passed
  • git diff --check passed

The Python suite reported the existing Daytona SDK deprecation warning.

Checklist

  • Code follows project style guidelines
  • Tests pass locally
  • Documentation updated (if needed)

Note

Low Risk
Backward-compatible optional API on guard parallelization only; classification semantics unchanged and covered by new unit tests.

Overview
Adds optional concurrency caps to guard() in the Python and TypeScript SDKs so large text chunking and multi-page PDF analysis no longer always fan out with unbounded parallelism.

Callers can pass max_concurrency (Python) or maxConcurrency (TypeScript) on guard() / GuardOptions. When set, parallel _guard_single_text / guardSingleText work is routed through new helpers (_map_with_concurrency / mapWithConcurrency) instead of asyncio.gather / Promise.all. Omitting the option keeps the previous behavior (all chunks/pages at once). Invalid values (non–positive integers, including booleans) raise before any provider calls.

Aggregation (OR block logic), token usage totals, and result ordering are unchanged; tests assert peak in-flight calls, ordering of aggregated reasoning, and PDF page paths.

Reviewed by Cursor Bugbot for commit a77785a. Configure here.

Copilot AI lite review requested due to automatic review settings September 2, 2026 07:09
@vercel

vercel Bot commented Sep 2, 2026

Copy link
Copy Markdown

@Th-Shivam is attempting to deploy a commit to the Superagent Team on Vercel.

A member of the Team first needs to authorize it.

@open-cla

open-cla Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Contributor License Agreement

All contributors are covered by a CLA.

@superagent-security

Copy link
Copy Markdown

🚨 Contributor flagged. Click here for more info: Superagent Dashboard

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The Python concurrency helper still schedules one task per item via asyncio.gather, which can keep large numbers of pending tasks in memory and undermine the intended backpressure for very large inputs.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds optional per-call concurrency limits to guard() in both the TypeScript and Python SDKs to prevent unbounded fan-out when analyzing many text chunks or PDF pages, while preserving existing ordering and aggregation behavior when the option is omitted.

Changes:

  • TypeScript: add maxConcurrency to GuardOptions, validate it, and execute chunk/page analysis via a bounded worker pool.
  • Python: add max_concurrency to GuardOptions and guard(), validate it, and apply concurrency limiting for chunk/page analysis.
  • Add targeted tests in both SDKs covering concurrency limiting, ordering, and invalid option values.
File summaries
File Description
sdk/typescript/tests/guard.test.ts Adds tests for chunk concurrency limiting, ordering, and invalid maxConcurrency.
sdk/typescript/tests/guard-file.test.ts Adds test for PDF-page concurrency limiting.
sdk/typescript/src/types.ts Extends GuardOptions with maxConcurrency.
sdk/typescript/src/client.ts Implements bounded concurrency mapping and wires maxConcurrency into chunk/PDF execution.
sdk/python/tests/test_guard.py Adds tests for chunk/PDF concurrency limiting, ordering, and invalid max_concurrency.
sdk/python/src/safety_agent/types.py Extends GuardOptions with max_concurrency.
sdk/python/src/safety_agent/client.py Adds _map_with_concurrency, validates max_concurrency, and uses it for chunk/PDF analysis.
Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +121 to +127
semaphore = asyncio.Semaphore(max_concurrency or len(items))

async def run(item: T) -> R:
async with semaphore:
return await mapper(item)

return list(await asyncio.gather(*(run(item) for item in items)))
@homanp

homanp commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

@Th-Shivam thanks for PR will have a look at it later today/tomorrow.

@Th-Shivam

Copy link
Copy Markdown
Author

@homanp Thanks, really appreciate it! Looking forward to your feedback. Happy to make any changes needed based on your review.

@Th-Shivam

Copy link
Copy Markdown
Author

@homanp Hello Sir, I hope you are doing well , I just wanted to have a quick follow up, is there any update on this pr ?

@Th-Shivam

Copy link
Copy Markdown
Author

Hello @homanp sir , I hope you are doing well , Is there any update or any changes required on this pr ?

This branch has not been deployed

No deployments
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.

[Feature]: Add configurable concurrency limits for guard chunk and PDF-page analysis

3 participants