feat: first commit

This commit is contained in:
Neeraj Nigam
2026-03-18 21:23:14 -05:00
commit 0b1ec4a818
25 changed files with 3068 additions and 0 deletions

38
shell.py Normal file
View File

@@ -0,0 +1,38 @@
"""Persistent shell session manager."""
import subprocess
class ShellSession:
"""Wraps a persistent bash subprocess."""
_SENTINEL = "__END_OF_CMD_OUTPUT__"
def __init__(self, cwd: str) -> None:
self._proc = subprocess.Popen(
["bash"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=cwd,
text=True,
)
def run(self, cmd: str) -> str:
"""Send a command and return its output."""
full_cmd = f"{cmd}\necho {self._SENTINEL}\n"
self._proc.stdin.write(full_cmd)
self._proc.stdin.flush()
lines = []
for line in self._proc.stdout:
if self._SENTINEL in line:
break
lines.append(line)
return "".join(lines)
def close(self) -> None:
"""Terminate the shell process."""
if self._proc.poll() is None:
self._proc.stdin.close()
self._proc.terminate()
self._proc.wait()