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(signals): resolve scout acting user by project access (#93973)

Alessandro Pogliaghia48645703a5a
diff --git a/products/signals/backend/scout_harness/runner.py b/products/signals/backend/scout_harness/runner.py
index aba70a61ca4f..90cf71da369d 100644
--- a/products/signals/backend/scout_harness/runner.py
+++ b/products/signals/backend/scout_harness/runner.py
@@ -254,11 +254,11 @@ async def arun_signals_scout(
# the human who authored or enabled the scout instead of pooling on one team-level default
# user. Scouts don't clone a repo on the cadence path, so they don't need a GitHub integration
# — the `resolve_acting_user_id_for_team` fallback prefers the GitHub creator when present but
- # falls back to any active org member, so a team that never connected GitHub still runs (these
- # dominated the fleet failure rate when the run instead crashed ~5s into `_spawn_and_run` and
- # booked a bogus `failed`). The only remaining short-circuit is the genuine "no active user to
- # act as" case; like the withheld / in-flight skips it leaves no row, no lifecycle event, and
- # a `skip_reason` the coordinator can surface — not a failure.
+ # falls back to any other member with access to this project, so a team that never connected
+ # GitHub still runs (these dominated the fleet failure rate when the run instead crashed ~5s
+ # into `_spawn_and_run` and booked a bogus `failed`). The only remaining short-circuit is the
+ # genuine "no member can act" case; like the withheld / in-flight skips it leaves no row, no
+ # lifecycle event, and a `skip_reason` the coordinator can surface — not a failure.
user_id = await database_sync_to_async(resolve_scout_acting_user_id, thread_sensitive=False)(
team, skill.name, config
)
diff --git a/products/signals/backend/temporal/agentic/__init__.py b/products/signals/backend/temporal/agentic/__init__.py
index 9fb4d7565ea2..ca46bbb39785 100644
--- a/products/signals/backend/temporal/agentic/__init__.py
+++ b/products/signals/backend/temporal/agentic/__init__.py
@@ -3,7 +3,9 @@ import structlog
from posthog.models.github_integration_base import GitHubIntegrationBase
from posthog.models.organization import OrganizationMembership
from posthog.models.team.team import Team
+from posthog.models.user import User
from posthog.models.user_integration import UserGitHubIntegration
+from posthog.user_permissions import UserPermissions
from products.signals.backend.report_generation.select_repo import resolve_team_github_integration
from products.tasks.backend.facade import api as tasks_facade
@@ -77,32 +79,47 @@ def resolve_user_id_for_team(team_id: int, github: GitHubIntegrationBase | None
return membership.user_id
+def _can_act_on_team(user_id: int, team: Team) -> bool:
+ """Whether a scout run started as this user survives the workflow dispatcher.
+
+ Mirrors `_user_can_dispatch` in the tasks workflow dispatcher, which is the authority. A run
+ resolved as a user this returns False for is killed at dispatch and booked as failed.
+ """
+ user = User.objects.filter(id=user_id, is_active=True).first()
+ if user is None:
+ return False
+ return UserPermissions(user=user, team=team).current_team.effective_membership_level is not None
+
+
def resolve_acting_user_id_for_team(team_id: int) -> int | None:
- """Resolve the user a Signals scout sandbox acts as, *without* requiring GitHub.
-
- `resolve_user_id_for_team` gates on a GitHub integration because the repo-cloning callers
- (report generation, repo selection, custom agent) need those credentials. The scout cadence
- path never clones a repo — `user_id` only scopes the sandbox connection token / MCP identity —
- so a GitHub integration is the wrong precondition there. Prefer the GitHub-integration creator
- when one exists (stable attribution, matches the other surfaces), otherwise fall back to any
- active org member.
-
- Returns ``None`` only when the org has no active member to act as — a genuine "can't run yet"
- that the scheduled caller short-circuits on, rather than crashing deep in the spawn path and
- booking a bogus `failed` outcome. Genuine errors (missing team, DB failures) still propagate.
+ """Resolve the user a Signals scout sandbox acts as. Does not require a GitHub integration.
+
+ `resolve_user_id_for_team` requires GitHub because its callers clone a repo. The scout cadence
+ path does not clone: `user_id` only scopes the sandbox connection token and MCP identity.
+
+ Every candidate must pass `_can_act_on_team`, because the run mints a sandbox token as this
+ user. Org membership is not enough. A project can deny a member by going private or by one
+ per-member rule, and either way org membership keeps picking a user the dispatcher rejects on
+ every tick. `Team.all_users_with_access()` narrows the pool first, which keeps the walk short
+ on a private project. Prefer the GitHub integration creator when that user qualifies, so
+ attribution matches the other surfaces.
+
+ Return None when the project has no such member. The scheduled caller then skips the tick
+ instead of crashing deep in the spawn path and booking a bogus `failed` run. Genuine errors
+ (missing team, DB failures) still propagate.
"""
team = Team.objects.select_related("organization").get(id=team_id)
github = resolve_team_github_integration(team_id, team=team)
if github is not None:
try:
- return resolve_user_id_for_team(team_id, github=github)
+ github_user_id = resolve_user_id_for_team(team_id, github=github)
except RuntimeError:
- # Integration present but its user is unusable — fall through to any active member.
- pass
- membership = (
- OrganizationMembership.objects.select_related("user")
- .filter(organization=team.organization, user__is_active=True)
+ github_user_id = None
+ if github_user_id is not None and _can_act_on_team(github_user_id, team):
+ return github_user_id
+ candidate_ids = (
+ OrganizationMembership.objects.filter(organization=team.organization, user__in=team.all_users_with_access())
.order_by("id")
- .first()
+ .values_list("user_id", flat=True)
)
- return membership.user_id if membership else None
+ return next((user_id for user_id in candidate_ids.iterator() if _can_act_on_team(user_id, team)), None)
diff --git a/products/signals/backend/test/test_resolve_user_id.py b/products/signals/backend/test/test_resolve_user_id.py
index eb662b03f616..2a2c17a2d297 100644
--- a/products/signals/backend/test/test_resolve_user_id.py
+++ b/products/signals/backend/test/test_resolve_user_id.py
@@ -1,10 +1,12 @@
import pytest
+from posthog.constants import AvailableFeature
from posthog.models import Organization, Team, User
from posthog.models.integration import Integration
from posthog.models.organization import OrganizationMembership
from posthog.models.user_integration import UserIntegration
+from products.access_control.backend.models.access_control import AccessControl
from products.signals.backend.temporal.agentic import resolve_acting_user_id_for_team, resolve_user_id_for_team
@@ -167,3 +169,31 @@ def test_acting_user_prefers_github_creator_when_present(organization, team):
_create_github_integration(team, created_by=creator)
assert resolve_acting_user_id_for_team(team.id) == creator.id
+
+
[email protected]("denial", ["private_project", "per_member_rule"])
[email protected]("with_github_integration", [False, True])
+def test_acting_user_skips_member_without_project_access(organization, team, with_github_integration, denial):
+ organization.available_product_features = [
+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL},
+ ]
+ organization.save()
+ denied_member = _create_user("[email protected]", organization, level=OrganizationMembership.Level.MEMBER)
+ admin = _create_user("[email protected]", organization, level=OrganizationMembership.Level.ADMIN)
+ denied_by_member = (
+ OrganizationMembership.objects.get(user=denied_member, organization=organization)
+ if denial == "per_member_rule"
+ else None
+ )
+ AccessControl.objects.create(
+ team=team,
+ resource="project",
+ resource_id=str(team.id),
+ organization_member=denied_by_member,
+ access_level="none",
+ )
+ if with_github_integration:
+ _create_github_integration(team, created_by=denied_member)
+
+ assert resolve_acting_user_id_for_team(team.id) == admin.id