Terminate a background shell process. Args: bash_id: The unique identifier of the background shell to terminate Returns: BashOutputResult with termination status and remaining output
(self, bash_id: str)
| 566 | } |
| 567 | |
| 568 | async def execute(self, bash_id: str) -> BashOutputResult: |
| 569 | """Terminate a background shell process. |
| 570 | |
| 571 | Args: |
| 572 | bash_id: The unique identifier of the background shell to terminate |
| 573 | |
| 574 | Returns: |
| 575 | BashOutputResult with termination status and remaining output |
| 576 | """ |
| 577 | |
| 578 | try: |
| 579 | # Get remaining output before termination |
| 580 | bg_shell = BackgroundShellManager.get(bash_id) |
| 581 | if bg_shell: |
| 582 | remaining_lines = bg_shell.get_new_output() |
| 583 | else: |
| 584 | remaining_lines = [] |
| 585 | |
| 586 | # Terminate through manager (handles all cleanup) |
| 587 | bg_shell = await BackgroundShellManager.terminate(bash_id) |
| 588 | |
| 589 | # Get remaining output |
| 590 | stdout = "\n".join(remaining_lines) if remaining_lines else "" |
| 591 | |
| 592 | return BashOutputResult( |
| 593 | success=True, |
| 594 | stdout=stdout, |
| 595 | stderr="", |
| 596 | exit_code=bg_shell.exit_code if bg_shell.exit_code is not None else 0, |
| 597 | bash_id=bash_id, |
| 598 | ) |
| 599 | |
| 600 | except ValueError as e: |
| 601 | # Shell not found |
| 602 | available_ids = BackgroundShellManager.get_available_ids() |
| 603 | return BashOutputResult( |
| 604 | success=False, |
| 605 | error=f"{str(e)}. Available: {available_ids or 'none'}", |
| 606 | stdout="", |
| 607 | stderr=str(e), |
| 608 | exit_code=-1, |
| 609 | ) |
| 610 | except Exception as e: |
| 611 | return BashOutputResult( |
| 612 | success=False, |
| 613 | error=f"Failed to terminate bash shell: {str(e)}", |
| 614 | stdout="", |
| 615 | stderr=str(e), |
| 616 | exit_code=-1, |
| 617 | ) |