@@ -134,7 +134,9 @@ export function parseCommand(command: string): { executable: string; args: strin
134134 const trimmed = command . trim ( )
135135 const shellType = detectCommandShell ( trimmed )
136136
137- // PowerShell commands: extract PowerShell executable and arguments
137+ // PowerShell commands: MUST use shell wrapper for proper argument parsing
138+ // Issue #27 fix: PowerShell -Command "..." requires cmd.exe to parse correctly
139+ // Without shell wrapping, arguments are split incorrectly and commands fail
138140 if ( shellType === 'powershell' || shellType === 'pwsh' ) {
139141 const parts = trimmed . split ( / \s + / )
140142 const executable = shellType === 'pwsh' ? 'pwsh' : 'powershell.exe'
@@ -143,7 +145,7 @@ export function parseCommand(command: string): { executable: string; args: strin
143145 return {
144146 executable,
145147 args,
146- shouldBypassShell : true // Direct execution, no shell wrapping
148+ shouldBypassShell : false // Use shell wrapper for proper parsing
147149 }
148150 }
149151
@@ -332,17 +334,12 @@ export const BashTool = Tool.define("bash", async () => {
332334 if ( ! reader ) return
333335 try {
334336 while ( true ) {
335- const { done, value } = await Promise . race ( [
336- reader . read ( ) ,
337- new Promise < { done : true } > ( ( _ , reject ) => {
338- ctx . abort . addEventListener ( "abort" , ( ) => reject ( new Error ( "Aborted" ) ) , { once : true } )
339- } )
340- ] )
337+ const { done, value } = await reader . read ( )
341338 if ( done ) break
342339 append ( value )
343340 }
344- } catch ( _e ) {
345- // Stream reading ended or was aborted
341+ } catch {
342+ // Stream reading ended (abort or natural completion)
346343 }
347344 }
348345
@@ -352,6 +349,7 @@ export const BashTool = Tool.define("bash", async () => {
352349
353350 const kill = ( ) => Shell . killTree ( proc , { exited : ( ) => exited } )
354351
352+ // Handle abort before starting
355353 if ( ctx . abort . aborted ) {
356354 aborted = true
357355 await kill ( )
@@ -369,14 +367,18 @@ export const BashTool = Tool.define("bash", async () => {
369367 void kill ( )
370368 } , timeout + 100 )
371369
372- // Concurrent stream reading and process monitoring
373- await Promise . race ( [
374- proc . exited ,
375- Promise . all ( [
376- readOutput ( stdoutReader ) ,
377- readOutput ( stderrReader ) ,
378- ] ) . then ( ( ) => proc . exited ) ,
379- ] )
370+ // Start reading streams
371+ const stdoutPromise = readOutput ( stdoutReader )
372+ const stderrPromise = readOutput ( stderrReader )
373+
374+ // Wait for process exit
375+ await proc . exited
376+
377+ // Guarantee streams drain before returning (Issue #17 fix)
378+ // This prevents data loss when proc.exited resolves before streams finish
379+ await Promise . all ( [ stdoutPromise , stderrPromise ] ) . catch ( ( ) => { } )
380+
381+ exited = true
380382
381383 // Cleanup
382384 clearTimeout ( timeoutTimer )
0 commit comments