From 971c8a988d654b20e828a6b996c4f2a6cfbcde55 Mon Sep 17 00:00:00 2001 From: Levi Neely Date: Mon, 27 Jul 2026 09:49:44 +0200 Subject: [PATCH] elevate: forward caller environment to elevated commands The elevate client now includes the full environment in its request. The adapter wraps the command with 'env -i K=V ... bash -c cmd' so the elevated command runs with the agent's env rather than the adapter's startup env. --- scripts/x/elevate | 2 +- scripts/x/elevate-adapter-superpowerd | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/scripts/x/elevate b/scripts/x/elevate index 3741243..31b809b 100755 --- a/scripts/x/elevate +++ b/scripts/x/elevate @@ -44,7 +44,7 @@ def main(): print("usage: elevate -- ", file=sys.stderr) sys.exit(1) - req = json.dumps({"cmd": sys.argv[2], "cwd": os.getcwd()}).encode() + b"\n" + req = json.dumps({"cmd": sys.argv[2], "cwd": os.getcwd(), "env": dict(os.environ)}).encode() + b"\n" sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: diff --git a/scripts/x/elevate-adapter-superpowerd b/scripts/x/elevate-adapter-superpowerd index b9f0332..bf49209 100755 --- a/scripts/x/elevate-adapter-superpowerd +++ b/scripts/x/elevate-adapter-superpowerd @@ -345,6 +345,20 @@ def handle(conn): pass +def _wrap_with_env(cmd, env): + """Wrap a command with explicit env setup. + + Produces: env -i K1=V1 K2=V2 ... bash -c 'original command' + This ensures the command runs with exactly the caller's environment. + """ + import shlex + parts = ["env", "-i"] + for k, v in sorted(env.items()): + parts.append(f"{k}={v}") + parts.extend(["bash", "-c", cmd]) + return " ".join(shlex.quote(p) for p in parts) + + def _handle_inner(conn): data = b"" while chunk := conn.recv(65536): @@ -360,6 +374,12 @@ def _handle_inner(conn): cmd = req.get("cmd", "") cwd = req.get("cwd", os.getcwd()) + env = req.get("env") + + # If the client sent its environment, wrap the command to reproduce it. + # This ensures the elevated command sees the agent's env, not the adapter's. + if env and isinstance(env, dict): + cmd = _wrap_with_env(cmd, env) token = os.environ.get("SUPERPOWERD_SESSION_TOKEN", "")