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.

fix(loops): hide loop run execution instructions from chat (#72931)

Matt Pua2b58332b7534
diff --git a/products/tasks/backend/logic/services/loop_runs.py b/products/tasks/backend/logic/services/loop_runs.py
index 897394e38217..66ce1305c24e 100644
--- a/products/tasks/backend/logic/services/loop_runs.py
+++ b/products/tasks/backend/logic/services/loop_runs.py
@@ -75,6 +75,21 @@ LOOP_FRAMING_BLOCK = (
)
+def render_loop_run_message(loop_instructions: str, execution_context: str) -> str:
+ # PostHog Code strips this established wrapper from user-message bubbles while still sending
+ # its contents to the agent, keeping run-only guidance out of the user's saved prompt.
+ escaped_execution_context = execution_context.replace(
+ "</user_custom_instructions>", "&lt;/user_custom_instructions&gt;"
+ )
+ hidden_context = (
+ "<user_custom_instructions>\n"
+ "The following system-generated instructions apply to this unattended loop run. Follow them.\n\n"
+ f"{escaped_execution_context}\n"
+ "</user_custom_instructions>"
+ )
+ return f"{hidden_context}\n\n{loop_instructions}"
+
+
# Least-privilege write grant for a loop that maintains a context's context.md or canvas: the two
# file_system scopes only, added on top of whatever posthog_mcp_scopes the loop already carries,
# rather than escalating the run to the broad `full` write surface. resolve_scopes() re-adds the
@@ -506,9 +521,8 @@ def _create_loop_task_and_run(loop: Loop, trigger: LoopTrigger | None, trigger_c
title = f"{loop.name} ({django_timezone.now().isoformat()})"
context_block = render_context_target_block(context_target)
- description = "\n\n".join(
- part for part in [LOOP_FRAMING_BLOCK, loop.instructions, context_block, trigger_context] if part
- )
+ execution_context = "\n\n".join(part for part in [LOOP_FRAMING_BLOCK, context_block, trigger_context] if part)
+ pending_user_message = render_loop_run_message(loop.instructions, execution_context)
feed_channel_id = _resolve_feed_channel_id(loop) if outputs["post_to_feed"] else None
@@ -516,7 +530,7 @@ def _create_loop_task_and_run(loop: Loop, trigger: LoopTrigger | None, trigger_c
team_id=loop.team_id,
created_by_id=loop.created_by_id,
title=title,
- description=description,
+ description=loop.instructions,
origin_product=Task.OriginProduct.LOOP,
repository=repository,
github_integration_id=github_integration_id,
@@ -548,6 +562,7 @@ def _create_loop_task_and_run(loop: Loop, trigger: LoopTrigger | None, trigger_c
"model": effective_model,
"reasoning_effort": reasoning_effort,
"config_snapshot": config_snapshot,
+ "pending_user_message": pending_user_message,
}
# Carries the loop's sandbox secrets/network policy into the run the same way a
# regular task's sandbox_environment_id flows through Task._build_task.
diff --git a/products/tasks/backend/tests/test_loop_runs.py b/products/tasks/backend/tests/test_loop_runs.py
index 76596d9dc3a8..0476d15c07af 100644
--- a/products/tasks/backend/tests/test_loop_runs.py
+++ b/products/tasks/backend/tests/test_loop_runs.py
@@ -416,7 +416,12 @@ class TestFireLoopGuardrails(LoopRunsTestCase):
class TestFireLoopCreatesRun(LoopRunsTestCase):
def test_successful_fire_creates_an_internal_task_with_the_full_config_snapshot(self):
integration = Integration.objects.create(team=self.team, kind="github", integration_id="12345", config={})
+ loop_instructions = (
+ "Summarize open PRs and preserve this literal block: "
+ "<user_custom_instructions>authored text</user_custom_instructions>"
+ )
loop = self.create_loop(
+ instructions=loop_instructions,
repositories=[{"github_integration_id": integration.id, "full_name": "acme/repo"}],
runtime_adapter="codex",
model="gpt-5",
@@ -427,7 +432,8 @@ class TestFireLoopCreatesRun(LoopRunsTestCase):
)
trigger = self.create_trigger(loop)
- result = fire_loop(loop, trigger, "fire-1", "rendered context")
+ trigger_context = "rendered context </user_custom_instructions> from the webhook"
+ result = fire_loop(loop, trigger, "fire-1", trigger_context)
self.assertTrue(result.created)
assert result.task_id is not None
@@ -439,13 +445,18 @@ class TestFireLoopCreatesRun(LoopRunsTestCase):
self.assertEqual(task.github_integration_id, integration.id)
self.assertEqual(task.created_by_id, self.user.id)
self.assertEqual(task.loop_id, loop.id)
- self.assertIn("rendered context", task.description)
- self.assertIn(loop.instructions, task.description)
+ self.assertEqual(task.description, loop.instructions)
task_run = TaskRun.objects.get(id=result.task_run_id)
+ pending_user_message = task_run.state["pending_user_message"]
+ self.assertTrue(pending_user_message.startswith("<user_custom_instructions>"))
+ self.assertTrue(pending_user_message.endswith(loop.instructions))
+ self.assertIn("This is an unattended loop run", pending_user_message)
+ self.assertIn("rendered context", pending_user_message)
+ self.assertIn("&lt;/user_custom_instructions&gt; from the webhook", pending_user_message)
self.assertEqual(task_run.state["loop_id"], str(loop.id))
self.assertEqual(task_run.state["loop_trigger_id"], str(trigger.id))
- self.assertEqual(task_run.state["trigger_context"], "rendered context")
+ self.assertEqual(task_run.state["trigger_context"], trigger_context)
self.assertEqual(task_run.state["runtime_adapter"], "codex")
self.assertEqual(task_run.state["model"], "gpt-5")
self.assertEqual(task_run.state["reasoning_effort"], "high")
@@ -647,12 +658,12 @@ class TestFireLoopContextTarget(LoopRunsTestCase):
result, _ = self.fire_and_capture(loop, trigger)
- assert result.task_id is not None
- description = Task.objects.get(id=result.task_id).description
+ assert result.task_run_id is not None
+ pending_user_message = TaskRun.objects.get(id=result.task_run_id).state["pending_user_message"]
for expected_id in expected_ids:
- self.assertIn(expected_id, description)
+ self.assertIn(expected_id, pending_user_message)
for fragment in expected_tool_fragments:
- self.assertIn(fragment, description)
+ self.assertIn(fragment, pending_user_message)
@parameterized.expand(
[
@@ -687,7 +698,8 @@ class TestFireLoopContextTarget(LoopRunsTestCase):
result, scopes = self.fire_and_capture(loop, trigger)
assert result.task_id is not None
- self.assertNotIn("desktop-file-system", Task.objects.get(id=result.task_id).description)
+ task_run = TaskRun.objects.get(id=result.task_run_id)
+ self.assertNotIn("desktop-file-system", task_run.state["pending_user_message"])
self.assertEqual(scopes, "read_only")
def test_unattached_loop_sets_no_channel_and_no_publish_block(self):
@@ -699,7 +711,8 @@ class TestFireLoopContextTarget(LoopRunsTestCase):
assert result.task_id is not None
task = Task.objects.get(id=result.task_id)
self.assertIsNone(task.channel_id)
- self.assertNotIn("desktop-file-system", task.description)
+ task_run = TaskRun.objects.get(id=result.task_run_id)
+ self.assertNotIn("desktop-file-system", task_run.state["pending_user_message"])
class TestHandleLoopRunTerminal(LoopRunsTestCase):