From d67980b8a088fc5f89da82245f5a18b3425e70c7 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 21 Feb 2026 15:08:25 -0500 Subject: [PATCH] save scripts --- MainContext.md | 2 ++ config.toml | 1 + shell_runner.py | 26 +++++++++++++++++++------- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/MainContext.md b/MainContext.md index 1a6a908..770a63f 100644 --- a/MainContext.md +++ b/MainContext.md @@ -32,6 +32,7 @@ - Before any script runs, `gui.py` shows a modal `ConfirmDialog` on the main thread; the background send thread blocks on a `threading.Event` until the user clicks Approve or Reject - The dialog displays `base_dir`, shows the script in an editable text box (allowing last-second tweaks), and has Approve & Run / Reject buttons - On approval the (possibly edited) script is passed to `shell_runner.run_powershell()` which prepends `Set-Location -LiteralPath ''` and runs it via `powershell -NoProfile -NonInteractive -Command` +- Every script (original, before Set-Location is prepended) is saved to ./scripts/generated/ai_.ps1 before execution; the saved path appears in the tool result - stdout, stderr, and exit code are returned to the AI as the tool result - Rejections return `"USER REJECTED: command was not executed"` to the AI - All tool calls (script + result/rejection) are appended to `_tool_log` and displayed in the Tool Calls panel @@ -61,3 +62,4 @@ - System prompt support could be added as a field in `config.toml` and passed in `ai_client.send()` - Discussion history excerpts could be individually toggleable for inclusion in the generated md - `MAX_TOOL_ROUNDS` in `ai_client.py` caps agentic loops at 5 rounds; adjustable + diff --git a/config.toml b/config.toml index 26b01c2..70c151a 100644 --- a/config.toml +++ b/config.toml @@ -12,6 +12,7 @@ paths = [ "gui.py", "pyproject.toml", "MainContext.md", + "C:/projects/manual_slop/shell_runner.py", ] [screenshots] diff --git a/shell_runner.py b/shell_runner.py index 99ee506..68b41d9 100644 --- a/shell_runner.py +++ b/shell_runner.py @@ -1,15 +1,27 @@ -import subprocess -import shlex +import subprocess from pathlib import Path +from datetime import datetime TIMEOUT_SECONDS = 60 +SCRIPTS_DIR = Path("./scripts/generated") + +def _save_script(script: str) -> Path: + """Save the original (pre-Set-Location) script to ./scripts/generated/ with a timestamp name.""" + SCRIPTS_DIR.mkdir(parents=True, exist_ok=True) + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f") + path = SCRIPTS_DIR / f"ai_{timestamp}.ps1" + path.write_text(script, encoding="utf-8") + return path def run_powershell(script: str, base_dir: str) -> str: """ Run a PowerShell script with working directory set to base_dir. + Saves the script to ./scripts/generated/ before running. Returns a string combining stdout, stderr, and exit code. Raises nothing - all errors are captured into the return string. """ + saved_path = _save_script(script) + # Prepend Set-Location so the AI doesn't need to worry about cwd full_script = f"Set-Location -LiteralPath '{base_dir}'\n{script}" @@ -21,16 +33,16 @@ def run_powershell(script: str, base_dir: str) -> str: timeout=TIMEOUT_SECONDS, cwd=base_dir ) - parts = [] + parts = [f"SAVED: {saved_path}"] if result.stdout.strip(): parts.append(f"STDOUT:\n{result.stdout.strip()}") if result.stderr.strip(): parts.append(f"STDERR:\n{result.stderr.strip()}") parts.append(f"EXIT CODE: {result.returncode}") - return "\n".join(parts) if parts else f"EXIT CODE: {result.returncode}" + return "\n".join(parts) except subprocess.TimeoutExpired: - return f"ERROR: command timed out after {TIMEOUT_SECONDS}s" + return f"SAVED: {saved_path}\nERROR: command timed out after {TIMEOUT_SECONDS}s" except FileNotFoundError: - return "ERROR: powershell executable not found" + return f"SAVED: {saved_path}\nERROR: powershell executable not found" except Exception as e: - return f"ERROR: {e}" + return f"SAVED: {saved_path}\nERROR: {e}"