Skip to content

fix: eliminate ~3.9s blocking latency on SDK cold init#2980

Open
majiayu000 wants to merge 3 commits intoComposioHQ:nextfrom
majiayu000:fix/issue-2970-slow-sdk-init
Open

fix: eliminate ~3.9s blocking latency on SDK cold init#2980
majiayu000 wants to merge 3 commits intoComposioHQ:nextfrom
majiayu000:fix/issue-2970-slow-sdk-init

Conversation

@majiayu000
Copy link
Copy Markdown

Summary

Fixes #2970

Three targeted changes to eliminate ~3.9s cold-init blocking caused by synchronous npm registry check and telemetry POST during constructor execution.

Changes

  • ts/packages/core/src/utils/version.ts — Add file-based caching (24h TTL via os.tmpdir()/composio-version-cache.json) and AbortSignal.timeout(5000) to prevent hanging. On cache hit, skip the network request entirely.
  • ts/packages/core/src/telemetry/Telemetry.ts — Route SDK_INITIALIZED event through existing batchProcessor.pushItem() instead of immediate sendMetric(), deferring the HTTP POST via the 200ms batch window.
  • ts/packages/core/src/composio.ts — Wrap checkForLatestVersionFromNPM() in setTimeout(0) to defer to next event loop tick, ensuring zero init-time network I/O.

Type of change

  • Bug fix
  • New feature
  • Refactor/Chore
  • Documentation
  • Breaking change

How Has This Been Tested?

  • Added ts/packages/core/test/core/version-check.test.ts with 7 Vitest tests covering cache write, cache hit, cache stale, fetch timeout, corrupt cache, write error, and npm response validation.
  • Updated ts/packages/core/test/telemetry/TelemetryService.test.ts to verify SDK_INITIALIZED routes through batchProcessor.
  • All 26 tests pass locally. The 15 pre-existing test failures are caused by unbuilt @composio/json-schema-to-zod dependency, unrelated to this change.

Checklist

  • I have read the Code of Conduct and this PR adheres to it
  • I ran linters/tests locally and they passed
  • I updated documentation as needed
  • I added tests or explain why not applicable
  • I added a changeset if this change affects published packages

Additional context

Both the npm version check and telemetry POST were already fire-and-forget (not awaited), but they initiated DNS resolution + TCP connections immediately on cold start, adding ~3.9s blocking latency. The fix uses three complementary strategies: file-based caching (eliminates repeated fetches), batch processing (defers telemetry), and event loop deferral (ensures zero synchronous network I/O during construction).

Three targeted changes to avoid synchronous network I/O during
constructor execution:

1. version.ts: Add file-based cache (24h TTL) for npm version check
   and AbortSignal.timeout(5s) to prevent hanging fetches
2. Telemetry.ts: Route SDK_INITIALIZED event through batchProcessor
   instead of immediate sendMetric(), leveraging existing 200ms batch
   window
3. composio.ts: Defer checkForLatestVersionFromNPM to next event loop
   tick via setTimeout(0)

Signed-off-by: majiayu000 <1835304752@qq.com>
- Add 7 test cases for checkForLatestVersionFromNPM covering cache hits,
  stale cache, missing cache, malformed JSON, write errors, and invalid
  npm responses
- Validate latestVersion is a string and valid semver before writing cache
- Fix misleading comment about setTimeout deferral in constructor

Signed-off-by: majiayu000 <1835304752@qq.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 22, 2026

@majiayu000 is attempting to deploy a commit to the Composio Team on Vercel.

A member of the Team first needs to authorize it.

@majiayu000 majiayu000 marked this pull request as ready for review March 22, 2026 01:18
@majiayu000 majiayu000 requested a review from haxzie as a code owner March 22, 2026 01:18
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

…sion cache

Replace direct Node.js imports (os, path, fs) with the existing #platform
conditional import abstraction so version.ts does not break workerd/edge
runtimes. Cache location moved from os.tmpdir() to ~/.composio/ using
platform.homedir(), guarded by platform.supportsFileSystem.

Addresses review feedback on PR ComposioHQ#2980.

Signed-off-by: majiayu000 <1835304752@qq.com>
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.

[Bug]: Composio SDK Slow Initialization

1 participant