{"id":30137599,"url":"https://github.com/runablehq/agents-e2b-connection-issue","last_synced_at":"2025-09-08T13:38:41.735Z","repository":{"id":307288802,"uuid":"1029009221","full_name":"runablehq/agents-e2b-connection-issue","owner":"runablehq","description":"Reproduction of agents-e2b connections issue","archived":false,"fork":false,"pushed_at":"2025-07-30T12:39:46.000Z","size":628,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-30T14:08:41.120Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/runablehq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-30T11:41:39.000Z","updated_at":"2025-07-30T12:39:50.000Z","dependencies_parsed_at":"2025-07-30T14:08:58.892Z","dependency_job_id":"32149368-698d-4e93-a4e8-bfff7a92a1a6","html_url":"https://github.com/runablehq/agents-e2b-connection-issue","commit_stats":null,"previous_names":["runablehq/agents-e2b-connection-issue"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/runablehq/agents-e2b-connection-issue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runablehq%2Fagents-e2b-connection-issue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runablehq%2Fagents-e2b-connection-issue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runablehq%2Fagents-e2b-connection-issue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runablehq%2Fagents-e2b-connection-issue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/runablehq","download_url":"https://codeload.github.com/runablehq/agents-e2b-connection-issue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/runablehq%2Fagents-e2b-connection-issue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269814278,"owners_count":24479359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-11T00:52:55.585Z","updated_at":"2025-08-11T00:53:03.801Z","avatar_url":"https://github.com/runablehq.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🐛 Agents SDK - Minimal Reproduction Issue\n\n![agents-header](https://github.com/user-attachments/assets/f6d99eeb-1803-4495-9c5e-3cf07a37b402)\n\n\u003ca href=\"https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents-starter\"\u003e\u003cimg src=\"https://deploy.workers.cloudflare.com/button\" alt=\"Deploy to Cloudflare\"/\u003e\u003c/a\u003e\n\n**⚠️ ISSUE REPRODUCTION**: This repository demonstrates a critical issue with the [`agents`](https://www.npmjs.com/package/agents) SDK when deployed to Cloudflare Workers. Subsequent requests in the same chat room stall unpredictably at random steps, while the same code works perfectly locally. Added a fetch call to verify if network calls are broken once the agent breaks.\n\n## 🚨 The Problem\n\n**Environment**: Cloudflare Workers (production deployment)  \n**Affected**: Agents SDK chat flow with multiple requests per session  \n**Status**: ❌ Broken in Workers, ✅ Works locally  \n\n### Issue Description\n\nWe're migrating a Node.js project using the Agents SDK to Cloudflare Workers. The application uses:\n- One agent per user serving multiple chat sessions\n- SQLite for internal session mapping\n- E2B sandbox integration\n- Similar flow to the official chat bot example\n\n### Reproduction Steps\n\n1. Deploy this starter to Cloudflare Workers\n2. Send the first chat message → ✅ **Works perfectly**\n3. Send multiple messages in the same chat room but different sessions → ❌ **Hangs indefinitely** after some requests (~4 in this minimal reproduction, but about 1-2 requests in our production environment)\n4. Subsequent requests stall at random steps in production:\n   - Authentication\n   - E2B sandbox loading\n   - Tool initialization\n   - Network calls never resolve or timeout\n\n### Expected vs Actual Behavior\n\n| Environment | First Request | Subsequent Requests |\n|-------------|---------------|--------------------|\n| **Local Development** | ✅ Works | ✅ Works |\n| **Cloudflare Workers** | ✅ Works | ❌ Hangs randomly |\n\n### Current Flow (onChatMessage)\n\n```typescript\n// This flow works locally but fails on Workers after first request\n1. Authentication\n2. Load E2B sandbox\n3. Billing service check\n4. Initialize tools\n5. Return stream from Agents SDK  // ← Hangs here or earlier steps\n```\n\n## 🔍 How to Reproduce\n\n### Prerequisites\n- Cloudflare account\n- OpenAI API key\n- This exact starter template\n\n### Setup Instructions\n\n1. **Clone and Install**:\n   ```bash\n   npx create-cloudflare@latest --template cloudflare/agents-starter\n   cd agents-starter\n   npm install\n   ```\n\n2. **Configure Environment**:\n   ```bash\n   # Create .dev.vars file\n   echo \"OPENAI_API_KEY=your_openai_api_key\" \u003e .dev.vars\n   ```\n\n3. **Test Locally** (this works):\n   ```bash\n   npm start\n   # Open browser, send multiple messages → All work fine\n   ```\n\n4. **Deploy to Workers** (this breaks):\n   ```bash\n   npm run deploy\n   # Visit deployed URL, send first message → Works\n   # Send second message → Hangs indefinitely\n   ```\n\n### 🔧 Technical Details\n\n**Architecture**:\n- One agent instance per user\n- Multiple chat sessions mapped via SQLite\n- Hyperdrive connection to PostgreSQL (GCP)\n- E2B sandbox integration\n- Streaming responses via Agents SDK\n\n**Failure Pattern**:\n- ✅ First request after deployment: Always succeeds\n- ❌ Subsequent requests: Hang at unpredictable steps\n- 🔄 No timeout or error - requests just never resolve\n- 🏠 Local development: No issues whatsoever\n\n**Affected Components**:\n- Authentication flow\n- E2B sandbox initialization\n- Tool system setup\n- Agents SDK streaming\n- Network calls in general\n\n## 📊 Issue Analysis\n\n### What We Know\n\n- **Timing**: Issue started recently (wasn't happening before)\n- **Scope**: Only affects Cloudflare Workers deployment\n- **Pattern**: First request always works, subsequent ones fail\n- **Randomness**: Failure occurs at different steps unpredictably\n- **No Errors**: Requests don't timeout or throw errors, they just hang\n\n### Suspected Causes\n\n1. **Workers Runtime Differences**: \n   - Different event loop behavior\n   - Request/response lifecycle differences\n   - Memory or state management issues\n   - Max allowed connections\n\n2. **Agents SDK Integration**:\n   - Potential Workers-specific compatibility issue\n   - State persistence between requests\n   - Streaming response handling\n\n3. **External Dependencies**:\n   - E2B sandbox connection pooling\n   - Hyperdrive connection management\n   - SQLite state between requests\n\n### 🧪 Debugging Steps Taken\n\n- [x] Confirmed local development works perfectly\n- [x] Verified first request always succeeds in Workers\n- [x] Identified random failure points in subsequent requests\n- [x] Ruled out API key or authentication issues\n- [ ] Need investigation into Workers-specific behavior\n- [ ] Need Agents SDK team input on Workers compatibility\n\n## 📁 Project Structure\n\n```\n├── src/\n│   ├── app.tsx        # Chat UI implementation\n│   ├── server.ts      # ⚠️ Main agent logic (where issues occur)\n│   ├── tools.ts       # Tool definitions (hangs during init)\n│   ├── utils.ts       # Helper functions\n│   └── styles.css     # UI styling\n├── wrangler.jsonc     # Workers configuration\n└── .dev.vars.example  # Environment template\n```\n\n### 🔍 Key Files for Investigation\n\n- **`src/server.ts`**: Contains the main chat flow that hangs\n- **`src/tools.ts`**: Tool initialization that sometimes fails\n- **`wrangler.jsonc`**: Workers configuration that might affect behavior\n- **Network calls**: Any external API calls that hang in Workers\n\n## 🛠️ Help Needed\n\n### For Cloudflare Team\n\n1. **Workers Runtime Investigation**:\n   - Are there known issues with persistent connections in Workers?\n   - How should long-running agent sessions be handled?\n   - Any Workers-specific considerations for the Agents SDK?\n\n2. **Debugging Assistance**:\n   - Best practices for debugging hanging requests in Workers\n   - Logging/monitoring recommendations for this type of issue\n   - Workers-specific profiling tools\n\n### For Agents SDK Team\n\n1. **Workers Compatibility**:\n   - Is the Agents SDK fully tested on Cloudflare Workers?\n   - Any known limitations or required configurations?\n   - Recommended patterns for multi-request agent sessions?\n\n2. **State Management**:\n   - How should agent state persist between requests in Workers?\n   - Are there Workers-specific initialization patterns?\n   - Connection pooling best practices?\n\n### For Community\n\n1. **Similar Issues**:\n   - Has anyone experienced similar hanging request issues?\n   - Any workarounds or solutions found?\n   - Alternative deployment patterns that work?\n\n2. **Testing Help**:\n   - Can others reproduce this issue with the same setup?\n   - Different Workers configurations to try?\n   - Alternative agent architectures that work reliably?\n\n---\n\n## 📋 Original Template Information\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand original starter template documentation\u003c/summary\u003e\n\n### Features\n\n- 💬 Interactive chat interface with AI\n- 🛠️ Built-in tool system with human-in-the-loop confirmation\n- 📅 Advanced task scheduling (one-time, delayed, and recurring via cron)\n- 🌓 Dark/Light theme support\n- ⚡️ Real-time streaming responses\n- 🔄 State management and chat history\n- 🎨 Modern, responsive UI\n\n### Customization Guide\n\n#### Adding New Tools\n\nAdd new tools in `tools.ts` using the tool builder:\n\n```typescript\n// Example of a tool that requires confirmation\nconst searchDatabase = tool({\n  description: \"Search the database for user records\",\n  parameters: z.object({\n    query: z.string(),\n    limit: z.number().optional(),\n  }),\n  // No execute function = requires confirmation\n});\n\n// Example of an auto-executing tool\nconst getCurrentTime = tool({\n  description: \"Get current server time\",\n  parameters: z.object({}),\n  execute: async () =\u003e new Date().toISOString(),\n});\n```\n\n#### Use a different AI model provider\n\nThe starting implementation uses the [`ai-sdk`](https://sdk.vercel.ai/docs/introduction) and [OpenAI provider](https://sdk.vercel.ai/providers/ai-sdk-providers/openai), but you can use alternatives like [`workers-ai-provider`](https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai) or [`anthropic`](https://sdk.vercel.ai/providers/ai-sdk-providers/anthropic).\n\n\u003c/details\u003e\n\n## Learn More\n\n- [`agents`](https://github.com/cloudflare/agents/blob/main/packages/agents/README.md)\n- [Cloudflare Agents Documentation](https://developers.cloudflare.com/agents/)\n- [Cloudflare Workers Documentation](https://developers.cloudflare.com/workers/)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frunablehq%2Fagents-e2b-connection-issue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frunablehq%2Fagents-e2b-connection-issue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frunablehq%2Fagents-e2b-connection-issue/lists"}