-
-
Notifications
You must be signed in to change notification settings - Fork 503
add new readProcessOutput2 logic for long time task #244
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
Open
weinaike
wants to merge
1
commit into
wonderwhy-er:main
Choose a base branch
from
weinaike:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−0
Open
Changes from all commits
Commits
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
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,128 @@ | ||
import assert from 'assert'; | ||
import { startProcess, readProcessOutput, readProcessOutput2 } from '../dist/tools/improved-process-tools.js'; | ||
|
||
/** | ||
* Test readProcessOutput2 behavior compared to readProcessOutput | ||
*/ | ||
async function testReadProcessOutput2Behavior() { | ||
console.log('🧪 Testing readProcessOutput2 behavior...'); | ||
|
||
try { | ||
// Test 1: Quick command - both should behave similarly | ||
console.log('\n📝 Test 1: Quick command (echo)'); | ||
const echoResult = await startProcess({ | ||
command: 'echo "Hello World"', | ||
timeout_ms: 2000 | ||
}); | ||
|
||
if (echoResult.isError) { | ||
throw new Error('Failed to start echo process'); | ||
} | ||
|
||
const echoPid = extractPid(echoResult.content[0].text); | ||
console.log(`Started echo process with PID: ${echoPid}`); | ||
|
||
// Wait a bit then try both functions | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
|
||
const output1 = await readProcessOutput({ pid: echoPid, timeout_ms: 2000 }); | ||
const output2 = await readProcessOutput2({ pid: echoPid, timeout_ms: 2000 }); | ||
|
||
console.log('📊 readProcessOutput:', output1.content[0].text.substring(0, 100)); | ||
console.log('📊 readProcessOutput2:', output2.content[0].text.substring(0, 100)); | ||
|
||
// Test 2: Sleep command - should show different behavior | ||
console.log('\n📝 Test 2: Sleep command (longer running)'); | ||
const sleepResult = await startProcess({ | ||
command: 'sleep 3 && echo "Sleep finished"', | ||
timeout_ms: 1000 | ||
}); | ||
|
||
if (sleepResult.isError) { | ||
throw new Error('Failed to start sleep process'); | ||
} | ||
|
||
const sleepPid = extractPid(sleepResult.content[0].text); | ||
console.log(`Started sleep process with PID: ${sleepPid}`); | ||
|
||
// Test readProcessOutput with short timeout (should timeout early) | ||
console.log('Testing readProcessOutput with 2s timeout...'); | ||
const startTime1 = Date.now(); | ||
const sleepOutput1 = await readProcessOutput({ pid: sleepPid, timeout_ms: 2000 }); | ||
const elapsed1 = Date.now() - startTime1; | ||
console.log(`📊 readProcessOutput took ${elapsed1}ms`); | ||
console.log('Result:', sleepOutput1.content[0].text.substring(0, 100)); | ||
|
||
// Test readProcessOutput2 with same timeout (should also timeout but collect more) | ||
console.log('Testing readProcessOutput2 with 2s timeout...'); | ||
const startTime2 = Date.now(); | ||
const sleepOutput2 = await readProcessOutput2({ pid: sleepPid, timeout_ms: 2000 }); | ||
const elapsed2 = Date.now() - startTime2; | ||
console.log(`📊 readProcessOutput2 took ${elapsed2}ms`); | ||
console.log('Result:', sleepOutput2.content[0].text.substring(0, 100)); | ||
|
||
// Test 3: Python REPL - should both detect waiting for input immediately | ||
console.log('\n📝 Test 3: Python REPL (should detect waiting for input)'); | ||
const pythonResult = await startProcess({ | ||
command: 'python3 -i', | ||
timeout_ms: 3000 | ||
}); | ||
|
||
if (pythonResult.isError) { | ||
console.log('Python not available, skipping REPL test'); | ||
} else { | ||
const pythonPid = extractPid(pythonResult.content[0].text); | ||
console.log(`Started Python REPL with PID: ${pythonPid}`); | ||
|
||
// Wait a moment for Python to fully start | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
|
||
// Test readProcessOutput2 first this time | ||
const startTime4 = Date.now(); | ||
const pythonOutput2 = await readProcessOutput2({ pid: pythonPid, timeout_ms: 3000 }); | ||
const elapsed4 = Date.now() - startTime4; | ||
console.log(`📊 readProcessOutput2 took ${elapsed4}ms for REPL`); | ||
console.log('Result:', pythonOutput2.content[0].text.substring(0, 200)); | ||
console.log('Full result length:', pythonOutput2.content[0].text.length); | ||
|
||
const startTime3 = Date.now(); | ||
const pythonOutput1 = await readProcessOutput({ pid: pythonPid, timeout_ms: 3000 }); | ||
const elapsed3 = Date.now() - startTime3; | ||
console.log(`📊 readProcessOutput took ${elapsed3}ms for REPL`); | ||
console.log('Result:', pythonOutput1.content[0].text.substring(0, 200)); | ||
console.log('Full result length:', pythonOutput1.content[0].text.length); | ||
|
||
// Check if either one detected the prompt correctly | ||
if (elapsed4 < 2000) { | ||
console.log('✅ readProcessOutput2 detected REPL prompt correctly'); | ||
} else if (elapsed3 < 2000) { | ||
console.log('✅ readProcessOutput detected REPL prompt correctly'); | ||
} else { | ||
console.log('❌ Neither function detected REPL prompt correctly'); | ||
throw new Error('Neither function detected REPL prompt correctly'); | ||
} | ||
} | ||
|
||
console.log('\n✅ All tests passed! readProcessOutput2 is working correctly.'); | ||
return true; | ||
|
||
} catch (error) { | ||
console.error('❌ Test failed:', error); | ||
return false; | ||
} | ||
} | ||
|
||
function extractPid(text) { | ||
const match = text.match(/PID (\d+)/); | ||
return match ? parseInt(match[1]) : null; | ||
} | ||
|
||
// Run the test | ||
testReadProcessOutput2Behavior() | ||
.then(success => { | ||
process.exit(success ? 0 : 1); | ||
}) | ||
.catch(error => { | ||
console.error('Test error:', error); | ||
process.exit(1); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Terminate the Python REPL to avoid leaking processes.
Add cleanup via forceTerminate when REPL is started.
Apply:
Also applies to: 80-104