Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(tasks): read the sandbox env file in one pass under agentsh (#120289)

Jonathan Mieloo594f46840c45
diff --git a/products/tasks/backend/logic/services/agentsh.py b/products/tasks/backend/logic/services/agentsh.py
index 0075ad24b0be..874e4efda127 100644
--- a/products/tasks/backend/logic/services/agentsh.py
+++ b/products/tasks/backend/logic/services/agentsh.py
@@ -259,12 +259,14 @@ def generate_env_wrapper(
return f"""\
#!/bin/bash
unset {excluded_names}
-while IFS= read -r -d $'\\0' line; do
+mapfile -d '' -t _agent_env < {quoted_env_file} 2>/dev/null
+for line in "${{_agent_env[@]}}"; do
case "$line" in
{excluded_entries}) ;;
*) export "$line" ;;
esac
-done < {quoted_env_file} 2>/dev/null
+done
+unset _agent_env
while IFS= read -r -d $'\\0' line; do
case "$line" in
diff --git a/products/tasks/backend/tests/test_agentsh.py b/products/tasks/backend/tests/test_agentsh.py
index 24f091c96095..6344c126b07e 100644
--- a/products/tasks/backend/tests/test_agentsh.py
+++ b/products/tasks/backend/tests/test_agentsh.py
@@ -404,6 +404,7 @@ class TestEnvWrapper(SimpleTestCase):
wrapper_file = Path(temp_dir) / "wrapper.sh"
env_file.write_bytes(
b"SAFE_BASE=kept\x00NODE_OPTIONS=--require=/tmp/payload.js\x00GITHUB_TOKEN=ghs_snapshot\x00"
+ b"JWT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----\nabc\n-----END PUBLIC KEY-----\x00"
)
github_env_file.write_bytes(b"GITHUB_TOKEN=ghs_fresh\x00GH_TOKEN=ghs_fresh\x00IGNORED=unsafe\x00")
oauth_env_file.write_bytes(b"POSTHOG_PERSONAL_API_KEY=oauth_fresh\x00IGNORED=unsafe\x00")
@@ -415,8 +416,8 @@ class TestEnvWrapper(SimpleTestCase):
str(wrapper_file),
"bash",
"-c",
- 'printf "%s|%s|%s|%s|%s" "$SAFE_BASE" "$GH_TOKEN" "$GITHUB_TOKEN" '
- '"$POSTHOG_PERSONAL_API_KEY" "${NODE_OPTIONS:-}"',
+ 'printf "%s|%s|%s|%s|%s|%s" "$SAFE_BASE" "$GH_TOKEN" "$GITHUB_TOKEN" '
+ '"$POSTHOG_PERSONAL_API_KEY" "${NODE_OPTIONS:-}" "$JWT_PUBLIC_KEY"',
],
check=True,
capture_output=True,
@@ -424,7 +425,10 @@ class TestEnvWrapper(SimpleTestCase):
env={"PATH": os.environ["PATH"], "NODE_OPTIONS": "--require=/tmp/inherited.js"},
)
- self.assertEqual(result.stdout, "kept|ghs_fresh|ghs_fresh|oauth_fresh|")
+ self.assertEqual(
+ result.stdout,
+ "kept|ghs_fresh|ghs_fresh|oauth_fresh||-----BEGIN PUBLIC KEY-----\nabc\n-----END PUBLIC KEY-----",
+ )
def test_wrapper_does_not_set_proxy_vars(self):
wrapper = generate_env_wrapper()