-
Notifications
You must be signed in to change notification settings - Fork 48
Drain broker stdout after SDK startup #838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "id": "traj_mz5m5ysjj31e", | ||
| "version": 1, | ||
| "task": { | ||
| "title": "Fix Relay SDK broker stdout drain" | ||
| }, | ||
| "status": "completed", | ||
| "startedAt": "2026-05-10T20:24:43.831Z", | ||
| "completedAt": "2026-05-10T20:35:47.359Z", | ||
| "agents": [ | ||
| { | ||
| "name": "default", | ||
| "role": "lead", | ||
| "joinedAt": "2026-05-10T20:35:41.197Z" | ||
| } | ||
| ], | ||
| "chapters": [ | ||
| { | ||
| "id": "chap_a1rg3jdl8b1w", | ||
| "title": "Work", | ||
| "agentName": "default", | ||
| "startedAt": "2026-05-10T20:35:41.197Z", | ||
| "endedAt": "2026-05-10T20:35:47.359Z", | ||
| "events": [ | ||
| { | ||
| "ts": 1778445341198, | ||
| "type": "decision", | ||
| "content": "Drain spawned broker stdout in SDK clients: Drain spawned broker stdout in SDK clients", | ||
| "raw": { | ||
| "question": "Drain spawned broker stdout in SDK clients", | ||
| "chosen": "Drain spawned broker stdout in SDK clients", | ||
| "alternatives": [], | ||
| "reasoning": "agent-relay run and direct SDK users both reach AgentRelayClient.spawn; after startup URL parsing stdout was left paused/undrained, so the root fix belongs in the TypeScript and Python SDK clients rather than only in Ricky's loader workaround." | ||
| }, | ||
| "significance": "high" | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "retrospective": { | ||
| "summary": "Fixed Relay SDK broker stdout drain at the source. TypeScript AgentRelayClient.spawn now resumes and drains broker stdout after startup URL parsing; Python SDK spawn now starts a stdout drain task and cancels it during shutdown. Added a TypeScript regression that floods fake broker stdout after startup and a Python unit test for stdout draining. Verified SDK typecheck/build, focused Vitest, and focused pytest suites.", | ||
| "approach": "Standard approach", | ||
| "confidence": 0.9 | ||
| }, | ||
| "commits": [], | ||
| "filesChanged": [], | ||
| "projectId": "/Users/khaliqgant/Projects/AgentWorkforce/relay-broker-stdout-drain", | ||
| "tags": [], | ||
| "_trace": { | ||
| "startRef": "bffd6b21275090f2648701d2005e875f2ca882c6", | ||
| "endRef": "bffd6b21275090f2648701d2005e875f2ca882c6" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Trajectory: Fix Relay SDK broker stdout drain | ||
|
|
||
| > **Status:** ✅ Completed | ||
| > **Confidence:** 90% | ||
| > **Started:** May 10, 2026 at 10:24 PM | ||
| > **Completed:** May 10, 2026 at 10:35 PM | ||
|
|
||
| --- | ||
|
|
||
| ## Summary | ||
|
|
||
| Fixed Relay SDK broker stdout drain at the source. TypeScript AgentRelayClient.spawn now resumes and drains broker stdout after startup URL parsing; Python SDK spawn now starts a stdout drain task and cancels it during shutdown. Added a TypeScript regression that floods fake broker stdout after startup and a Python unit test for stdout draining. Verified SDK typecheck/build, focused Vitest, and focused pytest suites. | ||
|
|
||
| **Approach:** Standard approach | ||
|
|
||
| --- | ||
|
|
||
| ## Key Decisions | ||
|
|
||
| ### Drain spawned broker stdout in SDK clients | ||
| - **Chose:** Drain spawned broker stdout in SDK clients | ||
| - **Reasoning:** agent-relay run and direct SDK users both reach AgentRelayClient.spawn; after startup URL parsing stdout was left paused/undrained, so the root fix belongs in the TypeScript and Python SDK clients rather than only in Ricky's loader workaround. | ||
|
|
||
| --- | ||
|
|
||
| ## Chapters | ||
|
|
||
| ### 1. Work | ||
| *Agent: default* | ||
|
|
||
| - Drain spawned broker stdout in SDK clients: Drain spawned broker stdout in SDK clients |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { setTimeout as sleep } from 'node:timers/promises'; | ||
| import { test } from 'vitest'; | ||
|
|
||
| import { AgentRelayClient } from '../client.js'; | ||
|
|
||
| const BROKER_STDOUT_DRAIN_TIMEOUT_MS = 5_000; | ||
|
|
||
| test('spawn drains broker stdout after startup so event floods cannot wedge the broker', async () => { | ||
| const cwd = await mkdtemp(join(tmpdir(), 'agent-relay-sdk-stdout-drain-')); | ||
|
|
||
| try { | ||
| await mkdir(cwd, { recursive: true }); | ||
| await writeFile( | ||
| join(cwd, 'init'), | ||
| [ | ||
| "const http = require('node:http');", | ||
| 'const server = http.createServer((req, res) => {', | ||
| " if (req.url === '/api/session') {", | ||
| " res.writeHead(200, { 'content-type': 'application/json' });", | ||
| " res.end(JSON.stringify({ workspace_key: 'wk_fake', mode: 'test', uptime_secs: 0 }));", | ||
| ' return;', | ||
| ' }', | ||
| " if (req.url === '/api/session/renew' || req.url === '/api/shutdown') {", | ||
| " res.writeHead(200, { 'content-type': 'application/json' });", | ||
| ' res.end(JSON.stringify({ ok: true }));', | ||
| ' return;', | ||
| ' }', | ||
| " res.writeHead(404, { 'content-type': 'application/json' });", | ||
| " res.end(JSON.stringify({ error: 'not found' }));", | ||
| '});', | ||
| "server.listen(0, '127.0.0.1', () => {", | ||
| ' const address = server.address();', | ||
| ' console.log(`[agent-relay] API listening on http://127.0.0.1:${address.port}`);', | ||
| ' let index = 0;', | ||
| " const chunk = 'x'.repeat(1024);", | ||
| ' const writeMore = () => {', | ||
| ' let ok = true;', | ||
| ' while (index < 20000 && ok) {', | ||
| ' ok = process.stdout.write(`event-${index}:${chunk}\\n`);', | ||
| ' index += 1;', | ||
| ' }', | ||
| ' if (index >= 20000) {', | ||
| ' setTimeout(() => process.exit(0), 25);', | ||
| ' return;', | ||
| ' }', | ||
| " process.stdout.once('drain', writeMore);", | ||
| ' };', | ||
| ' writeMore();', | ||
| '});', | ||
| '', | ||
| ].join('\n'), | ||
| 'utf8' | ||
| ); | ||
|
|
||
| const client = await AgentRelayClient.spawn({ | ||
| binaryPath: process.execPath, | ||
| cwd, | ||
| startupTimeoutMs: 3_000, | ||
| requestTimeoutMs: 3_000, | ||
| }); | ||
|
|
||
| const child = client.child; | ||
| assert.ok(child, 'spawned client should retain broker child process'); | ||
| const outcome = | ||
| child.exitCode !== null | ||
| ? 'exited' | ||
| : await Promise.race([ | ||
| new Promise<'exited'>((resolve) => child.once('exit', () => resolve('exited'))), | ||
| sleep(BROKER_STDOUT_DRAIN_TIMEOUT_MS).then(() => 'blocked' as const), | ||
| ]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| client.disconnect(); | ||
| if (outcome !== 'exited') { | ||
| child.kill('SIGKILL'); | ||
| } | ||
|
|
||
| assert.equal(outcome, 'exited'); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.