: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): keep a refresh from reclaiming a handed-over sandbox (#113142)
Vojta Bartoše8b27333e52e
diff --git a/products/tasks/backend/temporal/process_task/activities/refresh_sandbox_credentials.py b/products/tasks/backend/temporal/process_task/activities/refresh_sandbox_credentials.pyindex bc724ff3865d..9e70a16122c7 100644--- a/products/tasks/backend/temporal/process_task/activities/refresh_sandbox_credentials.py+++ b/products/tasks/backend/temporal/process_task/activities/refresh_sandbox_credentials.py@@ -69,20 +69,16 @@ def _probe_sandbox_wedge(sandbox: SandboxBase) -> tuple[str, dict[str, str]]:return _sandbox_wedge_verdict(probe), probe-def _with_current_authorship(ctx: TaskProcessingContext) -> TaskProcessingContext:- """Re-read the run's PR authorship, which `ctx.state` can no longer be trusted for.-- The context is captured once at workflow start, but a run is promoted from bot to user- authorship mid-run when its creator connects GitHub. Reading the stale value here resolves- the run as bot-authored and re-applies the team installation token over the personal one —- handing the creator every repo that installation covers. Only this key is overlaid: the rest- of the snapshot (sandbox id, actor) is what the sandbox being refreshed was built against.+def _with_live_credential_state(ctx: TaskProcessingContext) -> TaskProcessingContext:+ """Re-read the run state keys that name whose credentials this refresh should mint.++ Both move while the run is alive — authorship when its creator connects GitHub, the actor+ on every Slack turn — while `ctx.state` is a snapshot from workflow start, so reading+ either from there mints the wrong person's credentials."""- persisted = TaskRun.objects.filter(id=ctx.run_id).values_list("state", flat=True).first()- mode = (persisted or {}).get("pr_authorship_mode")- if not mode or mode == (ctx.state or {}).get("pr_authorship_mode"):- return ctx- return dataclasses.replace(ctx, state={**(ctx.state or {}), "pr_authorship_mode": mode})+ persisted = TaskRun.objects.filter(id=ctx.run_id).values_list("state", flat=True).first() or {}+ live = {key: persisted[key] for key in ("pr_authorship_mode", "slack_actor_user_id") if key in persisted}+ return dataclasses.replace(ctx, state={**(ctx.state or {}), **live})@dataclass@@ -151,7 +147,7 @@ def refresh_sandbox_credentials(input: RefreshSandboxCredentialsInput) -> Refresrefreshed_kinds=[],no_credentials_left=True,)- ctx = _with_current_authorship(ctx)+ ctx = _with_live_credential_state(ctx)except Task.DoesNotExist:logger.info("sandbox_credentials_refresh_stopped_task_gone",diff --git a/products/tasks/backend/temporal/process_task/activities/send_followup_to_sandbox.py b/products/tasks/backend/temporal/process_task/activities/send_followup_to_sandbox.pyindex 96364162feef..16ba2c57d6d6 100644--- a/products/tasks/backend/temporal/process_task/activities/send_followup_to_sandbox.py+++ b/products/tasks/backend/temporal/process_task/activities/send_followup_to_sandbox.py@@ -947,7 +947,6 @@ def _refresh_sandbox_github(if promoted_state is not None:state = promoted_state- scope = sandbox_identity_scope(run_id, state)if get_pr_authorship_mode(task, state) != PrAuthorshipMode.USER:return None@@ -1029,7 +1028,7 @@ def _refresh_sandbox_github(if applied:# Record the new actor only on a fully-confirmed rebind. A partial write leaves one# credential location on the prior actor's token, so fall through to logout instead.- mark_sandbox_github_identity(scope, actor_user.id)+ mark_sandbox_github_identity(sandbox.id, actor_user.id)logger.info("refresh_github_rebound", run_id=run_id, user_id=actor_user.id)return Nonelogger.warning("refresh_github_apply_incomplete", run_id=run_id, user_id=actor_user.id)@@ -1049,7 +1048,7 @@ def _refresh_sandbox_github(# bound away from the run owner, and clearing it would let the scheduled refresh inject# the owner's token into this actor's session. It does not gate the rebind — every turn# re-establishes — so a reconnect is still picked up.- mark_sandbox_github_identity(scope, actor_user.id)+ mark_sandbox_github_identity(sandbox.id, actor_user.id)logger.info("refresh_github_logged_out", run_id=run_id, user_id=actor_user.id)return Nonelogger.warning("refresh_github_logout_failed", run_id=run_id, user_id=actor_user.id, had_token=bool(token))diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_refresh_sandbox_credentials.py b/products/tasks/backend/temporal/process_task/activities/tests/test_refresh_sandbox_credentials.pyindex c0be06fcca57..5bf84ef4b56f 100644--- a/products/tasks/backend/temporal/process_task/activities/tests/test_refresh_sandbox_credentials.py+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_refresh_sandbox_credentials.py@@ -11,6 +11,8 @@ from django.db import OperationalErrorfrom asgiref.sync import async_to_syncfrom posthog.models.integration import Integration+from posthog.models.organization import OrganizationMembership+from posthog.models.user import Userfrom products.tasks.backend.exceptions import (SandboxExecutionError,@@ -35,6 +37,7 @@ class TestRefreshSandboxCredentialsActivity:@pytest.fixturedef sandbox(self):fake = MagicMock()+ fake.id = "sandbox-abc"fake.is_running.return_value = Truefake.execute.return_value = ExecutionResult(stdout="", stderr="", exit_code=0)fake.write_file.return_value = ExecutionResult(stdout="", stderr="", exit_code=0)@@ -119,6 +122,38 @@ class TestRefreshSandboxCredentialsActivity:assert get_token.call_args.kwargs["state"]["pr_authorship_mode"] == "user"+ def test_refresh_mints_for_the_actor_the_thread_moved_to_not_the_run_owner(+ self, activity_environment, task_context, test_task, test_task_run, sandbox+ ):+ second_actor = User.objects.create_user(+ email="[email protected]", password=None, first_name="Second", is_staff=False+ )+ OrganizationMembership.objects.create(user=second_actor, organization=test_task.team.organization)+ TaskRun.objects.filter(id=test_task_run.id).update(+ state={"interaction_origin": "slack", "slack_actor_user_id": second_actor.id}+ )+ task_context = dataclasses.replace(+ task_context, state={"interaction_origin": "slack", "slack_actor_user_id": test_task.created_by_id}+ )++ with (+ patch(+ "products.tasks.backend.temporal.process_task.activities.refresh_sandbox_credentials.get_sandbox_class_for_sandbox_id",+ **{"return_value.get_by_id.return_value": sandbox},+ ),+ patch(+ "products.tasks.backend.temporal.process_task.sandbox_credentials.get_sandbox_github_token",+ return_value="ghu_second_actor",+ ) as get_token,+ patch("products.tasks.backend.temporal.process_task.activities.refresh_sandbox_credentials.track_event"),+ ):+ async_to_sync(activity_environment.run)(+ refresh_sandbox_credentials,+ RefreshSandboxCredentialsInput(context=task_context, sandbox_id="sandbox-abc"),+ )++ assert get_token.call_args.kwargs["actor_user"].id == second_actor.id+def test_retries_transient_db_connection_drop(self, activity_environment, task_context, test_task, sandbox):# A pooled pgbouncer connection dropped mid-request raises OperationalError on the# activity's early Task read. The retry-once guard must evict the dead connection anddiff --git a/products/tasks/backend/temporal/process_task/sandbox_credentials.py b/products/tasks/backend/temporal/process_task/sandbox_credentials.pyindex 79929566c1e0..58718832479f 100644--- a/products/tasks/backend/temporal/process_task/sandbox_credentials.py+++ b/products/tasks/backend/temporal/process_task/sandbox_credentials.py@@ -31,7 +31,6 @@ from products.tasks.backend.temporal.process_task.utils import (is_caller_token_run,is_slack_interaction_state,resolve_user_github_integration_for_task,- sandbox_identity_scope,)if TYPE_CHECKING:@@ -171,14 +170,14 @@ def _loop_owner_credentials_revoked(task: Task, state: dict | None) -> bool:return not eligible-def _actor_rebound_away_from_owner(run_id: str, state: dict | None, owner_id: int | None) -> int | None:+def _actor_rebound_away_from(sandbox_id: str, permitted_session_user_id: int | None) -> int | None:"""The actor a per-message transition rebound (or logged out) this sandbox to, when that differs- from the run owner (else ``None``). Owner-scoped refresh paths (scheduled refresh, sibling- propagation) carry the owner's token, so re-applying it would resurrect the owner's identity- over the current actor's session — callers skip when this returns a value. Distinct from- `_loop_owner_credentials_revoked`, which gates on owner *eligibility* rather than session rebind."""- bound_actor = get_sandbox_github_identity_user(sandbox_identity_scope(run_id, state))- return bound_actor if bound_actor is not None and bound_actor != owner_id else None+ from the identity the caller may write for (else ``None``). Writing a token over a session+ bound to someone else would resurrect that identity for the current actor — callers skip when+ this returns a value. Distinct from `_loop_owner_credentials_revoked`, which gates on owner+ *eligibility* rather than session rebind."""+ bound_actor = get_sandbox_github_identity_user(sandbox_id)+ return bound_actor if bound_actor is not None and bound_actor != permitted_session_user_id else None# TTL derivation: the lock must outlive the whole critical section, or the lease can expire mid-write@@ -227,31 +226,32 @@ def sandbox_credential_lock(sandbox_id: str) -> Iterator[bool]:yield acquired-def _apply_owner_token_locked(+def _apply_token_locked(sandbox: "SandboxBase",repositories: Sequence[str],token: str,run_id: str,- state: dict | None,- owner_id: int | None,+ permitted_session_user_id: int | None,) -> bool:- """Apply an owner-scoped token only while the sandbox is still bound to the owner.+ """Apply a token only while the sandbox's session still belongs to the permitted identity.- Owner-scoped writers resolve the token from startup context, which can take seconds against- GitHub's API. Serializing the re-check and the write under the per-sandbox lock closes the window- where a per-message transition rebinds the sandbox between the caller's earlier check and this- write. Returns ``True`` only when the token was actually applied."""+ Resolving a token can take seconds against GitHub's API. Serializing the re-check and the write+ under the per-sandbox lock closes the window where a per-message transition rebinds the sandbox+ between the caller's earlier check and this write. Returns ``True`` only when the token was+ actually applied."""with sandbox_credential_lock(sandbox.id) as acquired:if not acquired:- logger.warning(- "owner_token_apply_skipped_lock_unavailable", extra={"run_id": run_id, "sandbox_id": sandbox.id}- )+ logger.warning("token_apply_skipped_lock_unavailable", extra={"run_id": run_id, "sandbox_id": sandbox.id})return False- rebound_actor = _actor_rebound_away_from_owner(run_id, state, owner_id)+ rebound_actor = _actor_rebound_away_from(sandbox.id, permitted_session_user_id)if rebound_actor is not None:logger.info(- "owner_token_apply_skipped_actor_transition",- extra={"run_id": run_id, "bound_actor": rebound_actor, "owner": owner_id},+ "token_apply_skipped_actor_transition",+ extra={+ "run_id": run_id,+ "bound_actor": rebound_actor,+ "permitted_session_user": permitted_session_user_id,+ },)return Falsereturn apply_github_credentials_to_sandbox(sandbox, repositories, token)@@ -268,13 +268,12 @@ def _rotation_lock_key(user_integration_id: int) -> str:class LiveSandbox(NamedTuple):- """A live sandbox eligible for owner-token propagation, with the fields the per-sandbox- actor-rebind re-check needs (``state`` and ``owner_id``) carried alongside."""+ """A live sandbox eligible for owner-token propagation, with the ``owner_id`` the+ per-sandbox actor-rebind re-check needs carried alongside."""run_id: strsandbox_id: strrepository: str | None- state: dict | Noneowner_id: int | None@@ -299,9 +298,9 @@ def _live_sandboxes_for_user_integration(user_integration_id: int) -> list[LiveScontinue# This loop carries the owner's token; skip a sandbox a per-message transition rebound to# a different actor, or re-applying it would resurrect the owner's identity for that actor.- if _actor_rebound_away_from_owner(str(run.id), run.state, run.task.created_by_id) is not None:+ if _actor_rebound_away_from(sandbox_id, run.task.created_by_id) is not None:continue- rows.append(LiveSandbox(str(run.id), sandbox_id, run.task.repository, run.state, run.task.created_by_id))+ rows.append(LiveSandbox(str(run.id), sandbox_id, run.task.repository, run.task.created_by_id))return rows@@ -314,8 +313,8 @@ def _propagate_user_token(user_integration_id: int, token: str) -> int:sandbox = get_sandbox_class_for_sandbox_id(live.sandbox_id).get_by_id(live.sandbox_id)# Re-check the actor binding under the per-sandbox lock: the filter above is not atomic# with this write, so a transition could have rebound the sandbox in between.- if sandbox.is_running() and _apply_owner_token_locked(- sandbox, [live.repository] if live.repository else [], token, live.run_id, live.state, live.owner_id+ if sandbox.is_running() and _apply_token_locked(+ sandbox, [live.repository] if live.repository else [], token, live.run_id, live.owner_id):applied += 1except Exception:@@ -418,20 +417,26 @@ class GitHubSandboxCredential:self.kind, refreshed=False, next_refresh_seconds=DEFAULT_REFRESH_INTERVAL_SECONDS)- # This scheduled refresh resolves the actor from startup context (ctx.state), so it carries- # the owner's token; skip when a per-message transition rebound the sandbox and leave that- # binding intact — the per-message gate keeps the current actor's token fresh.- rebound_actor = _actor_rebound_away_from_owner(ctx.run_id, ctx.state, task.created_by_id)+ # Gate on the session this refresh may land a token in, not the run owner: on a thread+ # that passed to someone else they are not the same person. A sandbox that already+ # belongs to a third party is left alone before any unresolved actor is reported, since+ # nothing would be minted for it either way.+ actor_user = get_task_run_credential_user(task, ctx.state)+ permitted_session_user_id = actor_user.id if actor_user is not None else None+ rebound_actor = _actor_rebound_away_from(sandbox.id, permitted_session_user_id)if rebound_actor is not None:logger.info("github_refresh_skipped_actor_transition",- extra={"run_id": ctx.run_id, "bound_actor": rebound_actor, "owner": task.created_by_id},+ extra={+ "run_id": ctx.run_id,+ "bound_actor": rebound_actor,+ "permitted_session_user": permitted_session_user_id,+ },)return CredentialRefreshOutcome(self.kind, refreshed=False, next_refresh_seconds=DEFAULT_REFRESH_INTERVAL_SECONDS)- actor_user = get_task_run_credential_user(task, ctx.state)if is_slack_interaction_state(ctx.state) and actor_user is None:raise ReauthorizationRequired("Slack run requires an acting user before refreshing GitHub credentials.")@@ -447,7 +452,7 @@ class GitHubSandboxCredential:)if integration is not None:- return self._refresh_shared_user_integration(sandbox, ctx, task, integration)+ return self._refresh_shared_user_integration(sandbox, ctx, task, integration, permitted_session_user_id)github_integration_id = task.github_integration_idgithub_user_integration_id = (@@ -480,6 +485,18 @@ class GitHubSandboxCredential:cause=e,)except ReauthorizationRequired as e:+ # On a Slack run the failure belongs to the current actor, not to the run. The next+ # message can come from an actor who has access, and the per-message rebind then+ # applies their token. An orphaned kind stays excluded until the sandbox goes, so+ # skip this refresh and keep the loop alive to refresh that token later.+ if is_slack_interaction_state(ctx.state):+ logger.info(+ "github_refresh_skipped_actor_without_access",+ extra={"run_id": ctx.run_id, "actor": permitted_session_user_id},+ )+ return CredentialRefreshOutcome(+ self.kind, refreshed=False, next_refresh_seconds=DEFAULT_REFRESH_INTERVAL_SECONDS+ )raise CredentialUnavailableError("GitHub user integration for this run requires reauthorization",{"run_id": ctx.run_id, "task_id": ctx.task_id},@@ -490,13 +507,18 @@ class GitHubSandboxCredential:self.kind, refreshed=False, next_refresh_seconds=DEFAULT_REFRESH_INTERVAL_SECONDS)- applied = _apply_owner_token_locked(sandbox, ctx.repositories, token, ctx.run_id, ctx.state, task.created_by_id)+ applied = _apply_token_locked(sandbox, ctx.repositories, token, ctx.run_id, permitted_session_user_id)return CredentialRefreshOutcome(self.kind, refreshed=applied, next_refresh_seconds=github_refresh_interval_seconds(token))def _refresh_shared_user_integration(- self, sandbox: "SandboxBase", ctx: "TaskProcessingContext", task: Task, integration: UserGitHubIntegration+ self,+ sandbox: "SandboxBase",+ ctx: "TaskProcessingContext",+ task: Task,+ integration: UserGitHubIntegration,+ permitted_session_user_id: int | None,) -> CredentialRefreshOutcome:try:token = resolve_coordinated_user_token(integration)@@ -508,16 +530,16 @@ class GitHubSandboxCredential:return CredentialRefreshOutcome(self.kind, refreshed=False, next_refresh_seconds=DEFAULT_REFRESH_INTERVAL_SECONDS)- applied = _apply_owner_token_locked(- sandbox, ctx.repositories, fallback, ctx.run_id, ctx.state, task.created_by_id- )+ # An installation token is the team's, not a person's, so it may only land in the+ # owner's own session, never in that of someone whose personal token just failed.+ applied = _apply_token_locked(sandbox, ctx.repositories, fallback, ctx.run_id, task.created_by_id)return CredentialRefreshOutcome(self.kind, refreshed=applied, next_refresh_seconds=github_refresh_interval_seconds(fallback))if token and _loop_owner_credentials_revoked(task, ctx.state):token = Noneapplied = (- _apply_owner_token_locked(sandbox, ctx.repositories, token, ctx.run_id, ctx.state, task.created_by_id)+ _apply_token_locked(sandbox, ctx.repositories, token, ctx.run_id, permitted_session_user_id)if tokenelse False)diff --git a/products/tasks/backend/temporal/process_task/tests/test_sandbox_credentials.py b/products/tasks/backend/temporal/process_task/tests/test_sandbox_credentials.pyindex 245482467ed0..79420bbc0946 100644--- a/products/tasks/backend/temporal/process_task/tests/test_sandbox_credentials.py+++ b/products/tasks/backend/temporal/process_task/tests/test_sandbox_credentials.py@@ -241,16 +241,17 @@ class TestGitHubSandboxCredential:assert outcome.refreshed is Truedef test_scheduled_refresh_skips_when_sandbox_bound_to_different_actor(self):- # A per-message actor transition rebound this sandbox to another actor. The scheduled- # refresh resolves the actor from the startup context, so it carries the owner's token;- # applying it would resurrect the owner's identity over the current actor's session.+ # Applying the token this refresh resolved would resurrect the owner's identity over the+ # current actor's session, so it must stop before resolving one at all.from products.tasks.backend.temporal.process_task.utils import mark_sandbox_github_identitysandbox = MagicMock()+ sandbox.id = "sb-transition"task = MagicMock()task.github_integration_id = 123task.created_by_id = 2 # run owner- mark_sandbox_github_identity("run-transition", 99) # transitioned to a different actor+ task.created_by.id = 2+ mark_sandbox_github_identity("sb-transition", 99) # transitioned to a different actorwith patch(f"{MODULE}.get_sandbox_github_token") as resolve:outcome = GitHubSandboxCredential().refresh(sandbox, _context(run_id="run-transition"), task)@@ -260,6 +261,39 @@ class TestGitHubSandboxCredential:sandbox.execute.assert_not_called()sandbox.write_file.assert_not_called()+ @pytest.mark.parametrize(+ "state,stays_alive",+ [+ ({"interaction_origin": "slack", "slack_actor_user_id": 2}, True),+ ({}, False),+ ],+ )+ def test_actor_without_github_access_keeps_the_loop_alive_only_on_slack(self, state, stays_alive):+ from posthog.models.user_integration import ReauthorizationRequired++ from products.tasks.backend.exceptions import CredentialUnavailableError+ from products.tasks.backend.temporal.process_task.utils import PrAuthorshipMode++ sandbox = MagicMock()+ sandbox.id = "sb-actor-without-access"+ task = MagicMock()+ task.github_integration_id = 123+ task.created_by_id = 2+ task.created_by.id = 2++ with (+ patch(f"{MODULE}.get_pr_authorship_mode", return_value=PrAuthorshipMode.BOT),+ patch(f"{MODULE}.is_caller_token_run", return_value=False),+ patch(f"{MODULE}.get_sandbox_github_token", side_effect=ReauthorizationRequired("no access")),+ ):+ if stays_alive:+ outcome = GitHubSandboxCredential().refresh(sandbox, _context(state=state), task)+ assert outcome.refreshed is False+ assert outcome.next_refresh_seconds == DEFAULT_REFRESH_INTERVAL_SECONDS+ else:+ with pytest.raises(CredentialUnavailableError):+ GitHubSandboxCredential().refresh(sandbox, _context(state=state), task)+class TestBuildSandboxCredentials:def test_includes_github_when_credentials_present(self):@@ -378,7 +412,7 @@ class TestSharedUserIntegrationRefresh:apply.assert_called_once_with(sandbox, ["explore-science/paper-wizard-frontend"], "ghu_caller")-class TestApplyOwnerTokenLocked:+class TestApplyTokenLocked:def _lock(self, stack, *, acquired):lock = MagicMock()lock.acquire.return_value = acquired@@ -386,10 +420,10 @@ class TestApplyOwnerTokenLocked:get_client.return_value.lock.return_value = lockreturn lock- def test_applies_while_sandbox_still_bound_to_owner(self):+ def test_applies_while_sandbox_still_bound_to_the_token_owner(self):import contextlib- from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_owner_token_locked+ from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_token_lockedwith contextlib.ExitStack() as stack:self._lock(stack, acquired=True)@@ -398,13 +432,13 @@ class TestApplyOwnerTokenLocked:sandbox = MagicMock()sandbox.id = "sb-1"- assert _apply_owner_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", {}, 7) is True+ assert _apply_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", 7) is Trueapply.assert_called_once_with(sandbox, ["org/repo"], "ghu_x")def test_skips_when_a_transition_rebound_the_sandbox_to_another_actor(self):import contextlib- from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_owner_token_locked+ from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_token_lockedwith contextlib.ExitStack() as stack:self._lock(stack, acquired=True)@@ -414,13 +448,13 @@ class TestApplyOwnerTokenLocked:sandbox = MagicMock()sandbox.id = "sb-1"- assert _apply_owner_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", {}, 7) is False+ assert _apply_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", 7) is Falseapply.assert_not_called()def test_fails_closed_without_applying_when_the_lock_is_contended(self):import contextlib- from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_owner_token_locked+ from products.tasks.backend.temporal.process_task.sandbox_credentials import _apply_token_lockedwith contextlib.ExitStack() as stack:lock = self._lock(stack, acquired=False)@@ -428,7 +462,7 @@ class TestApplyOwnerTokenLocked:sandbox = MagicMock()sandbox.id = "sb-1"- assert _apply_owner_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", {}, 7) is False+ assert _apply_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", 7) is Falseapply.assert_not_called()lock.release.assert_not_called()@@ -441,7 +475,7 @@ class TestApplyOwnerTokenLocked:from products.tasks.backend.temporal.process_task.sandbox_credentials import (_CREDENTIAL_LOCK_TTL_SECONDS,- _apply_owner_token_locked,+ _apply_token_locked,)assert _CREDENTIAL_LOCK_TTL_SECONDS == 5 * 60@@ -457,7 +491,7 @@ class TestApplyOwnerTokenLocked:sandbox = MagicMock()sandbox.id = "sb-1"- _apply_owner_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", {}, 7)+ _apply_token_locked(sandbox, ["org/repo"], "ghu_x", "run-1", 7)# The lock is leased for the full worst-case write, not the old 30s.get_client.return_value.lock.assert_called_once()diff --git a/products/tasks/backend/temporal/process_task/tests/test_send_followup_to_sandbox.py b/products/tasks/backend/temporal/process_task/tests/test_send_followup_to_sandbox.pyindex 078980c4fae8..6d7ec13c621c 100644--- a/products/tasks/backend/temporal/process_task/tests/test_send_followup_to_sandbox.py+++ b/products/tasks/backend/temporal/process_task/tests/test_send_followup_to_sandbox.py@@ -423,6 +423,14 @@ class TestSessionIdentityGate:_GH_MODULE = "products.tasks.backend.temporal.process_task.activities.send_followup_to_sandbox"+# The GitHub identity marker is keyed on the sandbox, so the write and the read below must+# agree on one id.+_GATE_SANDBOX_ID = "sb-gate"+++def _gate_lookup() -> LiveSandboxLookup:+ return LiveSandboxLookup(sandbox=MagicMock(id=_GATE_SANDBOX_ID))+@patch(f"{_GH_MODULE}.upgrade_run_to_user_authorship", return_value=None)@patch(f"{_GH_MODULE}.clear_github_credentials_from_sandbox")@@ -441,10 +449,10 @@ class TestSandboxGithubIdentityGate:# The actor can connect or disconnect between messages, and a resume or snapshot restore# can drop the token, so an unchanged actor is not evidence the sandbox still holds it.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = "ghu_token"mock_apply.return_value = True- mark_sandbox_github_identity("run-1", 42)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 42)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_called_once()@@ -457,10 +465,10 @@ class TestSandboxGithubIdentityGate:# Same actor as last turn, but their install no longer mints: the cheap skip must not# leave their token live in the sandbox until the refresh loop next runs.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = Nonemock_clear.return_value = True- mark_sandbox_github_identity("run-1", 42)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 42)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_clear.assert_called_once()@@ -473,10 +481,10 @@ class TestSandboxGithubIdentityGate:# integration does not revoke the token GitHub already issued, so proceeding could leave it# usable in the sandbox after the user disconnected.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = Nonemock_clear.return_value = False- mark_sandbox_github_identity("run-1", 42)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 42)assert (_refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None)@@ -488,10 +496,10 @@ class TestSandboxGithubIdentityGate:):# A different actor inheriting the previous one's live token is the case the gate exists for.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = Nonemock_clear.return_value = False- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert (_refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None)@@ -504,13 +512,13 @@ class TestSandboxGithubIdentityGate:# Revoke, then reconnect. The logout leaves the sandbox marked against this actor, and the# reconnect must still be picked up — no marker check may short-circuit the rebind.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_clear.return_value = Truemock_get_token.return_value = None- mark_sandbox_github_identity("run-1", 42)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 42)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is None- assert get_sandbox_github_identity_user("run-1") == 42+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 42mock_get_token.return_value = "ghu_reconnected"mock_apply.return_value = True@@ -525,7 +533,7 @@ class TestSandboxGithubIdentityGate:# BOT runs share a single installation token, so every actor is already# the same GitHub identity — nothing to rebind.mock_authorship.return_value = PrAuthorshipMode.BOT- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_get_token.assert_not_called()@@ -536,42 +544,42 @@ class TestSandboxGithubIdentityGate:self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade):mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = "ghu_newtoken"mock_apply.return_value = True- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_called_once()assert mock_apply.call_args.args[2] == "ghu_newtoken"mock_clear.assert_not_called()- assert get_sandbox_github_identity_user("run-1") == 42+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 42def test_transition_without_access_logs_out(self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade):mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.side_effect = ReauthorizationRequired("no repo access")mock_clear.return_value = True- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_not_called()mock_clear.assert_called_once()# Still marked: owner-scoped refreshes read this to know the sandbox is bound away from the# run owner, and must not inject the owner's token into this actor's session.- assert get_sandbox_github_identity_user("run-1") == 42+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 42def test_apply_failure_falls_back_to_logout(self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade):mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = "ghu_newtoken"mock_apply.side_effect = RuntimeError("write failed")mock_clear.return_value = True- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_called_once()@@ -584,18 +592,18 @@ class TestSandboxGithubIdentityGate:# rebind: the prior actor's token may still be live in the other location, so log out# rather than record the new actor.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.return_value = "ghu_newtoken"mock_apply.return_value = Falsemock_clear.return_value = True- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_called_once()mock_clear.assert_called_once()# Still marked: owner-scoped refreshes read this to know the sandbox is bound away from the# run owner, and must not inject the owner's token into this actor's session.- assert get_sandbox_github_identity_user("run-1") == 42 # logout confirmed, bound to new actor+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 42 # logout confirmed, bound to new actor@pytest.mark.parametrize("lookup,expected_reason",@@ -617,13 +625,13 @@ class TestSandboxGithubIdentityGate:):mock_authorship.return_value = PrAuthorshipMode.USERmock_resolve.return_value = lookup- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) == expected_reasonmock_get_token.assert_not_called()mock_apply.assert_not_called()mock_clear.assert_not_called()- assert get_sandbox_github_identity_user("run-1") == 99 # binding unchanged+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 99 # binding unchangeddef test_logout_failure_fails_closed(self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade@@ -631,16 +639,16 @@ class TestSandboxGithubIdentityGate:# New actor has no access and the sandbox can't even be cleared — the# previous actor's creds may still be live, so fail closed.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.side_effect = ReauthorizationRequired("no repo access")mock_clear.return_value = False- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert (_refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None)== SandboxRebindFailure.LOGOUT_UNCONFIRMED)- assert get_sandbox_github_identity_user("run-1") == 99 # binding unchanged+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 99 # binding unchangeddef test_logout_exception_fails_closed(self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade@@ -648,16 +656,16 @@ class TestSandboxGithubIdentityGate:# The clear itself raising (sandbox stopped/timed out between is_running and here) must# fail closed, not escape uncontrolled.mock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.side_effect = ReauthorizationRequired("no repo access")mock_clear.side_effect = RuntimeError("sandbox stopped")- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert (_refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None)== SandboxRebindFailure.LOGOUT_ERRORED)- assert get_sandbox_github_identity_user("run-1") == 99 # binding unchanged+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 99 # binding unchangeddef test_credential_unavailable_logs_out(self, mock_authorship, mock_resolve, mock_get_token, mock_apply, mock_clear, mock_upgrade@@ -667,17 +675,17 @@ class TestSandboxGithubIdentityGate:from products.tasks.backend.exceptions import CredentialUnavailableErrormock_authorship.return_value = PrAuthorshipMode.USER- mock_resolve.return_value = LiveSandboxLookup(sandbox=MagicMock())+ mock_resolve.return_value = _gate_lookup()mock_get_token.side_effect = CredentialUnavailableError("integration disconnected", {})mock_clear.return_value = True- mark_sandbox_github_identity("run-1", 99)+ mark_sandbox_github_identity(_GATE_SANDBOX_ID, 99)assert _refresh_sandbox_github(_make_task_run_mock(), MagicMock(id=42), None) is Nonemock_apply.assert_not_called()mock_clear.assert_called_once()# Still marked: owner-scoped refreshes read this to know the sandbox is bound away from the# run owner, and must not inject the owner's token into this actor's session.- assert get_sandbox_github_identity_user("run-1") == 42+ assert get_sandbox_github_identity_user(_GATE_SANDBOX_ID) == 42class TestSendFollowupActivityRefreshOrdering:diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.pyindex adce0ab119a4..ba35c823461d 100644--- a/products/tasks/backend/temporal/process_task/utils.py+++ b/products/tasks/backend/temporal/process_task/utils.py@@ -465,7 +465,8 @@ MCP_TOKEN_REFRESH_INTERVAL_SECONDS = TOKEN_EXPIRATION_SECONDS / 2 # 3 hoursdef sandbox_identity_scope(run_id: str, state: dict[str, Any] | None) -> str:- """Cache scope for marks describing a run's live sandbox.+ """Cache scope for the MCP-session mark, whose readers reach the agent server over HTTP+ and hold no sandbox handle to key on. The GitHub mark takes a sandbox id directly.Keyed on the sandbox id so a replacement sandbox (fresh provision,snapshot restore, workflow retry) starts unmarked — nothing ever needs@@ -501,24 +502,28 @@ def get_sandbox_mcp_session_user(scope: str) -> int | None:return _get_sandbox_identity_user("mcp-session", scope)-def mark_sandbox_github_identity(scope: str, user_id: int) -> None:+def mark_sandbox_github_identity(sandbox_id: str, user_id: int) -> None:"""Record which actor the sandbox's in-place GitHub credentials reflect.The value is the actor whose token was applied, or who was logged out (no usableaccess) — either way the sandbox no longer carries a *different* actor's identity,- which is what owner-scoped refreshes check before re-applying the owner's token.+ which is what every other writer checks before applying a token of its own.++ Takes the sandbox's own id rather than a scope derived from run state: a reader that+ resolved the id from a snapshot taken before the sandbox existed would look under a key+ nobody writes and read every sandbox as unmarked.Self-expires after MCP_TOKEN_REFRESH_INTERVAL_SECONDS; an absent entry reads as"must re-establish", which is always safe because re-establishing re-applies orclears rather than trusting stale creds."""- _mark_sandbox_identity("github-identity", scope, user_id)+ _mark_sandbox_identity("github-identity", sandbox_id, user_id)-def get_sandbox_github_identity_user(scope: str) -> int | None:+def get_sandbox_github_identity_user(sandbox_id: str) -> int | None:"""Actor id the sandbox's GitHub credentials were last bound to (or loggedout for) within the freshness window, or None when unknown."""- return _get_sandbox_identity_user("github-identity", scope)+ return _get_sandbox_identity_user("github-identity", sandbox_id)@dataclass(frozen=True)