Skip to content

fix(workspaces): bound R2 page reads to avoid connection limit#663

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-workspace-page-read-connection-limit
Draft

fix(workspaces): bound R2 page reads to avoid connection limit#663
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-workspace-page-read-connection-limit

Conversation

@posthog

@posthog posthog Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

The AI agent's read_workspace_items tool threw "Response closed due to connection limit" whenever it read more than ~6 pages of an extracted file, which severed the agent conversation.

readWorkspacePageProjection fetched every selected page's R2 object in one Promise.all pass and only read the bodies in a second pass, so all response bodies were held open as live connections simultaneously. Since a read can request up to maxWorkspacePageReadCount (20) pages, this exceeded Cloudflare Workers' ~6 concurrent open-connection limit.

The fix collapses the two passes into bounded batches that get() and text() each page in the same task, capped at pageReadConcurrency = 6 — mirroring the existing pageWriteConcurrency guard already used on the write path. This keeps simultaneously-open R2 connections under the limit regardless of how many pages are requested.

A regression test models the connection limit (a body counts as open between get() and text()) and asserts a 20-page read never opens more than 6 connections; it fails on the old code and passes with the fix.

Why

A production error on the posthog-edge worker deterministically broke the core AI file-read flow for any multi-page read, severing the agent conversation.

Test plan

  • vp test --run src/features/workspaces/extraction/workspace-page-projection.test.ts — 5 passing
  • vp check — format, lint, and typecheck clean

Created with PostHog Code from this inbox report.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Review in cubic

readWorkspacePageProjection opened every selected page's R2 object in one
Promise.all pass and only read the bodies in a second pass, so all response
bodies were held open as live connections at once. A read can request up to
maxWorkspacePageReadCount (20) pages, which exceeds the Cloudflare Workers
~6 concurrent open-connection limit and makes object.text() throw "Response
closed due to connection limit", severing the agent conversation.

Collapse the two passes into bounded batches that get() and text() each page
in the same task, capped at pageReadConcurrency = 6, mirroring the existing
pageWriteConcurrency guard on the write path.

Generated-By: PostHog Code
Task-Id: 3344d727-3576-48b1-a601-c6adc7ab7f02
@github-actions

Copy link
Copy Markdown

React Doctor found no new issues. 🎉

Reviewed by React Doctor for commit 30014ed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

0 participants