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(posthog-ai): honor actor targeting for context selection (#121226)

Adam Bowker30b91721fc87
diff --git a/docs/published/handbook/engineering/ai/sandboxed-agents.md b/docs/published/handbook/engineering/ai/sandboxed-agents.md
index 3833303b4fd0..eaf9c466319d 100644
--- a/docs/published/handbook/engineering/ai/sandboxed-agents.md
+++ b/docs/published/handbook/engineering/ai/sandboxed-agents.md
@@ -742,6 +742,7 @@ or stream echo, and never submits the message again.
The `phai-context-selection` feature flag enables hidden organizational context on human prompts in web and Slack cloud runs using Claude or Codex.
The `phai-context-selection` flag selects `control`, `shadow`, or `treatment` using the task ID.
+Person-property conditions use the current actor's email and staff status, so an email-targeted rollout applies to that actor's eligible tasks while percentage and variant bucketing remain task-based.
Other flag values disable selection. Local runs, Pi, and other runtime adapters skip it.
An explicit disabled response stops further preparation requests for that sandbox session, including after conversation history is cleared.
Flag changes can enable selection in a new session. Preparation errors do not disable later attempts.
diff --git a/products/context_layer/backend/selection.py b/products/context_layer/backend/selection.py
index 65072fb58b8a..c6edc6263e31 100644
--- a/products/context_layer/backend/selection.py
+++ b/products/context_layer/backend/selection.py
@@ -133,7 +133,7 @@ def bounded_request[T](team_id: int, operation: Callable[[float], T]) -> T:
raise SelectionUnavailable() from error
-def selection_mode(run: TaskRun) -> str:
+def selection_mode(run: TaskRun, actor: User) -> str:
if (
run.task.runtime != Task.Runtime.ACP
or (run.state or {}).get("runtime_adapter", "claude") not in ("claude", "codex")
@@ -144,6 +144,7 @@ def selection_mode(run: TaskRun) -> str:
value = get_feature_flag_or_none(
"phai-context-selection",
str(run.task_id),
+ person_properties={"email": actor.email, "is_staff": actor.is_staff},
groups={"organization": str(run.team.organization_id), "project": str(run.team_id)},
group_properties={"organization": {"id": str(run.team.organization_id)}, "project": {"id": str(run.team_id)}},
send_feature_flag_events=False,
@@ -370,7 +371,7 @@ class ContextSelector:
def prepare(self, deadline: float) -> PreparedContext:
try:
time_remaining(deadline)
- self.mode = selection_mode(self.run)
+ self.mode = selection_mode(self.run, self.actor)
if self.mode == "disabled":
return PreparedContext()
if not self.selection.prompt.strip() or self.mode == "control":
diff --git a/products/context_layer/backend/test/test_selection.py b/products/context_layer/backend/test/test_selection.py
index c5244029c0f2..b315d654e0ad 100644
--- a/products/context_layer/backend/test/test_selection.py
+++ b/products/context_layer/backend/test/test_selection.py
@@ -234,6 +234,22 @@ class TestSelection(SimpleTestCase):
else:
self.client_factory.assert_not_called()
+ @parameterized.expand([("[email protected]", "shadow"), ("[email protected]", "disabled")])
+ def test_email_targeting_uses_current_actor(self, email: str, expected: str) -> None:
+ self.run_record.task.created_by = User(id=6, email="[email protected]")
+ self.selector.actor.email = email
+ self.flag.side_effect = lambda *args, **kwargs: (
+ "shadow" if kwargs.get("person_properties", {}).get("email") == "[email protected]" else False
+ )
+
+ result = self.selector.prepare(time.monotonic() + 60)
+
+ self.assertEqual(result.mode, expected)
+ self.assertEqual(result.reason, "selected" if expected == "shadow" else "disabled")
+ self.assertEqual(result.context, "")
+ self.assertEqual(bool(self.requests), expected == "shadow")
+ self.assertEqual(self.flag.call_args.args[1], str(self.run_record.task_id))
+
@parameterized.expand([("shadow", False), ("shadow", None), ("treatment", False), ("treatment", None)])
def test_ai_consent_required_before_retrieval_and_model_calls(self, mode: str, consent: bool | None) -> None:
self.flag.return_value = mode
@@ -255,9 +271,9 @@ class TestSelection(SimpleTestCase):
def test_runtime_eligibility(self, name, runtime, adapter, expected) -> None:
self.run_record.task.runtime = runtime
self.run_record.state = {"runtime_adapter": adapter} if adapter else {}
- self.assertEqual(selection_mode(self.run_record), expected)
+ self.assertEqual(selection_mode(self.run_record, self.selector.actor), expected)
self.run_record.environment = TaskRun.Environment.LOCAL
- self.assertEqual(selection_mode(self.run_record), "disabled")
+ self.assertEqual(selection_mode(self.run_record, self.selector.actor), "disabled")
self.assertEqual(self.selector.actor.is_staff, False)
if expected == "treatment":
self.assertEqual(self.flag.call_args.args[1], str(self.run_record.task_id))