: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
chore(desktop): extract the sandbox gateway env into a shared function (#105729)
Alessandro Pogliaghi09fcabb8b4f0
diff --git a/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts b/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.tsindex 7017b114d2d3..69c9c2208c99 100644--- a/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts+++ b/products/desktop/packages/agent/src/server/agent-server.configure-environment.test.ts@@ -2,7 +2,8 @@ import { type SpanContext, TraceFlags } from "@opentelemetry/api";import { afterEach, beforeEach, describe, expect, it } from "vitest";import type { GatewayEnv } from "../adapters/claude/session/options";import type { Task } from "../types";-import { AgentServer, codexAuthFromGatewayEnv } from "./agent-server";+import { AgentServer } from "./agent-server";+import { codexAuthFromGatewayEnv } from "./gateway-env";interface TestableServer {configureEnvironment(args?: {diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.tsindex f4c784d82b11..ee72f393df5f 100644--- a/products/desktop/packages/agent/src/server/agent-server.ts+++ b/products/desktop/packages/agent/src/server/agent-server.ts@@ -15,7 +15,6 @@ import {RequestError,} from "@agentclientprotocol/sdk";import { type ServerType, serve } from "@hono/node-server";-import type { SpanContext } from "@opentelemetry/api";import { execGh } from "@posthog/git/gh";import { getCurrentBranch, getRemoteUrl } from "@posthog/git/queries";import { ghTokenEnv } from "@posthog/git/signed-commit";@@ -61,12 +60,6 @@ import {sleepWithBackoff,toAcpMcpServers,} from "@posthog/shared";-import {- buildPosthogPropertiesHeaderLines,- buildPosthogPropertiesHeaderRecord,- buildPosthogScopedPropertyHeaderLines,- buildPosthogScopedPropertyHeaderRecord,-} from "@posthog/shared/posthog-property-headers";import { prependProductEngineerPrompt } from "@posthog/shared/product-engineer-prompt";import { appendRichOutputPrompt } from "@posthog/shared/rich-output-prompt";import { unzipSync } from "fflate";@@ -129,7 +122,6 @@ import type {import { resourceLink } from "../utils/acp-content";import { withTimeout } from "../utils/common";import { createEventIdSource } from "../utils/event-id";-import { resolveGatewayProduct, resolveGatewayTarget } from "../utils/gateway";import { Logger } from "../utils/logger";import { redactSecrets, SecretEventRedactor } from "../utils/redact-secrets";import { logAgentshRuntimeInfo } from "./agentsh-runtime";@@ -140,6 +132,11 @@ import {} from "./cloud-prompt";import { CredentialRelay, CredentialRelayError } from "./credential-relay";import { TaskRunEventStreamSender } from "./event-stream-sender";+import {+ buildGatewayEnv,+ codexAuthFromGatewayEnv,+ type GatewayEnvInput,+} from "./gateway-env";import { type JwtPayload, JwtValidationError, validateJwt } from "./jwt";import { type McpRelayResponse, McpRelayServer } from "./mcp-relay-server";import {@@ -450,18 +447,6 @@ function buildMissingAttachmentNotice(count: number): string {);}-/**- * The codex session's LLM auth, from the resolved gateway env. Codex must never- * read the raw run credential: on the Go-gateway path the bearer is the per-run- * scoped token (see configureEnvironment).- */-export function codexAuthFromGatewayEnv(env: GatewayEnv): {- apiBaseUrl: string;- apiKey: string;-} {- return { apiBaseUrl: env.openaiBaseUrl, apiKey: env.openaiApiKey };-}-interface PrAttribution {createdAt: string | null;author: string | null;@@ -4696,154 +4681,9 @@ export class AgentServer {);}- private configureEnvironment({- runSpanContext,- isInternal = false,- originProduct,- signalReportId,- aiStage,- aiAgentName,- taskId,- taskRunId,- taskUserId,- taskTitle,- taskOriginKey,- repositories,- runtimeAdapter,- sandboxEnvironmentId,- snapshotKind,- prewarmed,- executionEnvironment,- }: {- runSpanContext?: SpanContext;- isInternal?: boolean;- originProduct?: Task["origin_product"] | null;- signalReportId?: string | null;- aiStage?: string | null;- aiAgentName?: string | null;- taskId?: string | null;- taskRunId?: string | null;- taskUserId?: number | null;- taskTitle?: string | null;- taskOriginKey?: string | null;- repositories?: string[];- runtimeAdapter?: string | null;- sandboxEnvironmentId?: string | null;- snapshotKind?: string | null;- prewarmed?: boolean | null;- executionEnvironment?: "local" | "cloud";- } = {}): GatewayEnv {+ private configureEnvironment(input: GatewayEnvInput = {}): GatewayEnv {const { apiKey, apiUrl, projectId } = this.config;- const product = resolveGatewayProduct({ isInternal, originProduct });- // Go-gateway runs authenticate with the per-run scoped token minted by the- // worker (pinned product + on-behalf-of team, per-run spend cap), not the- // run's per-team OAuth token, whose team has no gateway wallet. A routed- // product with no token therefore stays on the Python gateway. The worker's env values- // win, because the token is pinned to the product they name.- const gatewayToken = process.env.AI_GATEWAY_TOKEN?.trim() || undefined;- let target = resolveGatewayTarget({- product,- aiStage,- posthogHost: apiUrl,- });- if (target.isAiGateway && !gatewayToken) {- this.logger.warn(- `AI_GATEWAY_TOKEN missing for routed product ${target.aiProduct}; falling back to the Python gateway`,- );- target = resolveGatewayTarget({- product,- aiStage,- posthogHost: apiUrl,- env: { ...process.env, AI_GATEWAY_URL: undefined },- });- }- const {- baseUrl: gatewayUrl,- isAiGateway,- aiProduct,- aiStage: resolvedStage,- } = target;- const llmBearer = isAiGateway && gatewayToken ? gatewayToken : apiKey;- const openaiBaseUrl = gatewayUrl.endsWith("/v1")- ? gatewayUrl- : `${gatewayUrl}/v1`;- // Forward task metadata as `x-posthog-property-*` headers so the gateway- // lifts them onto the $ai_generation event. The Claude path routes these- // through the Anthropic SDK's ANTHROPIC_CUSTOM_HEADERS env var; the codex- // path sets them as `model_providers.posthog.http_headers` instead, so we- // also expose the record form below.- const gatewayProperties = {- // Gateway headers live for the session, so correlate with its enclosing run.- task_run_trace_id: runSpanContext?.traceId,- task_run_span_id: runSpanContext?.spanId,- task_origin_product: originProduct,- task_internal: isInternal,- signal_report_id: signalReportId,- ai_stage: resolvedStage,- // The team-scoped agent name; `ai_stage` stays a bounded fleet-wide tag.- ai_agent_name: aiAgentName,- task_id: taskId,- task_run_id: taskRunId,- task_user_id: taskUserId,- task_title: taskTitle,- task_origin_key: taskOriginKey,- task_repositories: repositories?.length- ? JSON.stringify(repositories)- : null,- task_runtime_adapter: runtimeAdapter,- task_sandbox_environment_id: sandboxEnvironmentId,- task_snapshot_kind: snapshotKind,- task_prewarmed: prewarmed,- task_execution_environment: executionEnvironment ?? "cloud",- };- // The Claude path appends the project scope in buildEnvironment from- // POSTHOG_PROJECT_ID; the codex path has no such hook, so its record below- // carries the same scope.- let customHeaders: string;- let openaiCustomHeaders: Record<string, string>;- if (isAiGateway) {- // The Go gateway reads one X-PostHog-Properties JSON blob and ignores- // per-property headers, and it has no product route, so `ai_product`- // has to travel in the blob or the spend lands unattributed. `team_id`- // is included for both adapters because the Go gateway does not read- // the Python gateway's project-scope header.- const properties = {- ...gatewayProperties,- ai_product: aiProduct,- team_id: projectId,- };- customHeaders = buildPosthogPropertiesHeaderLines(properties);- openaiCustomHeaders = buildPosthogPropertiesHeaderRecord(properties);- // The Go gateway writes this into the OpenAI body's `service_tier`, which- // is the only way a Codex run reaches the flex or priority queue: Codex- // itself omits a tier its model catalogue does not advertise. Codex-only,- // so it rides the OpenAI record; the Claude path has no tier concept.- if (this.config.serviceTier) {- openaiCustomHeaders["X-PostHog-Service-Tier"] = this.config.serviceTier;- }- // Codex sends no trace header, so the gateway stamps a fresh id per- // request and a run's generations each land in a trace of one. Codex-only:- // this header outranks `traceparent`, so setting it for Claude would- // replace the per-turn ids its CLI mints with one id for the whole run.- if (taskRunId && runtimeAdapter === "codex") {- openaiCustomHeaders["X-PostHog-Trace-Id"] = taskRunId;- }- } else {- customHeaders = buildPosthogScopedPropertyHeaderLines(- gatewayProperties,- projectId,- );- // No $ai_session_id on the Go-gateway path above: it strips $-prefixed- // blob keys, so the session id would be silently dropped there.- openaiCustomHeaders = buildPosthogScopedPropertyHeaderRecord(- {- ...gatewayProperties,- team_id: projectId,- $ai_session_id: taskId,- },- projectId,- );- }+ const gatewayEnv = buildGatewayEnv(this.config, input, this.logger);// Server-level constants that don't vary per task — safe to keep in// process.env so spawned tools (PostHog MCP, workspace-server, etc.) can@@ -4859,15 +4699,7 @@ export class AgentServer {// Task-specific gateway config is returned rather than written to// process.env so that concurrent sessions do not clobber each other's// gateway URL, auth token, or custom headers.- return {- anthropicBaseUrl: gatewayUrl,- anthropicAuthToken: llmBearer,- openaiBaseUrl,- openaiApiKey: llmBearer,- anthropicCustomHeaders: customHeaders,- openaiCustomHeaders,- posthogProjectId: String(projectId),- };+ return gatewayEnv;}private buildSlackQuestionRelayResponse(diff --git a/products/desktop/packages/agent/src/server/gateway-env.ts b/products/desktop/packages/agent/src/server/gateway-env.tsnew file mode 100644index 000000000000..026ac95dbb2d--- /dev/null+++ b/products/desktop/packages/agent/src/server/gateway-env.ts@@ -0,0 +1,193 @@+import type { SpanContext } from "@opentelemetry/api";+import {+ buildPosthogPropertiesHeaderLines,+ buildPosthogPropertiesHeaderRecord,+ buildPosthogScopedPropertyHeaderLines,+ buildPosthogScopedPropertyHeaderRecord,+} from "@posthog/shared/posthog-property-headers";+import type { GatewayEnv } from "../adapters/claude/session/options";+import type { Task } from "../types";+import { resolveGatewayProduct, resolveGatewayTarget } from "../utils/gateway";+import type { Logger } from "../utils/logger";+import type { AgentServerConfig } from "./types";++export interface GatewayEnvInput {+ runSpanContext?: SpanContext;+ isInternal?: boolean;+ originProduct?: Task["origin_product"] | null;+ signalReportId?: string | null;+ aiStage?: string | null;+ aiAgentName?: string | null;+ taskId?: string | null;+ taskRunId?: string | null;+ taskUserId?: number | null;+ taskTitle?: string | null;+ taskOriginKey?: string | null;+ repositories?: string[];+ runtimeAdapter?: string | null;+ sandboxEnvironmentId?: string | null;+ snapshotKind?: string | null;+ prewarmed?: boolean | null;+ executionEnvironment?: "local" | "cloud";+}++export function buildGatewayEnv(+ config: Pick<+ AgentServerConfig,+ "apiKey" | "apiUrl" | "projectId" | "serviceTier"+ >,+ {+ runSpanContext,+ isInternal = false,+ originProduct,+ signalReportId,+ aiStage,+ aiAgentName,+ taskId,+ taskRunId,+ taskUserId,+ taskTitle,+ taskOriginKey,+ repositories,+ runtimeAdapter,+ sandboxEnvironmentId,+ snapshotKind,+ prewarmed,+ executionEnvironment,+ }: GatewayEnvInput = {},+ logger: Logger,+): GatewayEnv {+ const { apiKey, apiUrl, projectId, serviceTier } = config;+ const product = resolveGatewayProduct({ isInternal, originProduct });+ // Go-gateway runs authenticate with the per-run scoped token minted by the+ // worker (pinned product + on-behalf-of team, per-run spend cap), not the+ // run's per-team OAuth token, whose team has no gateway wallet. A routed+ // product with no token therefore stays on the Python gateway. The worker's env values+ // win, because the token is pinned to the product they name.+ const gatewayToken = process.env.AI_GATEWAY_TOKEN?.trim() || undefined;+ let target = resolveGatewayTarget({+ product,+ aiStage,+ posthogHost: apiUrl,+ });+ if (target.isAiGateway && !gatewayToken) {+ logger.warn(+ `AI_GATEWAY_TOKEN missing for routed product ${target.aiProduct}; falling back to the Python gateway`,+ );+ target = resolveGatewayTarget({+ product,+ aiStage,+ posthogHost: apiUrl,+ env: { ...process.env, AI_GATEWAY_URL: undefined },+ });+ }+ const {+ baseUrl: gatewayUrl,+ isAiGateway,+ aiProduct,+ aiStage: resolvedStage,+ } = target;+ const llmBearer = isAiGateway && gatewayToken ? gatewayToken : apiKey;+ const openaiBaseUrl = gatewayUrl.endsWith("/v1")+ ? gatewayUrl+ : `${gatewayUrl}/v1`;+ // Forward task metadata as `x-posthog-property-*` headers so the gateway+ // lifts them onto the $ai_generation event. The Claude path routes these+ // through the Anthropic SDK's ANTHROPIC_CUSTOM_HEADERS env var; the codex+ // path sets them as `model_providers.posthog.http_headers` instead, so we+ // also expose the record form below.+ const gatewayProperties = {+ // Gateway headers live for the session, so correlate with its enclosing run.+ task_run_trace_id: runSpanContext?.traceId,+ task_run_span_id: runSpanContext?.spanId,+ task_origin_product: originProduct,+ task_internal: isInternal,+ signal_report_id: signalReportId,+ ai_stage: resolvedStage,+ // The team-scoped agent name; `ai_stage` stays a bounded fleet-wide tag.+ ai_agent_name: aiAgentName,+ task_id: taskId,+ task_run_id: taskRunId,+ task_user_id: taskUserId,+ task_title: taskTitle,+ task_origin_key: taskOriginKey,+ task_repositories: repositories?.length+ ? JSON.stringify(repositories)+ : null,+ task_runtime_adapter: runtimeAdapter,+ task_sandbox_environment_id: sandboxEnvironmentId,+ task_snapshot_kind: snapshotKind,+ task_prewarmed: prewarmed,+ task_execution_environment: executionEnvironment ?? "cloud",+ };+ // The Claude path appends the project scope in buildEnvironment from+ // POSTHOG_PROJECT_ID; the codex path has no such hook, so its record below+ // carries the same scope.+ let customHeaders: string;+ let openaiCustomHeaders: Record<string, string>;+ if (isAiGateway) {+ // The Go gateway reads one X-PostHog-Properties JSON blob and ignores+ // per-property headers, and it has no product route, so `ai_product`+ // has to travel in the blob or the spend lands unattributed. `team_id`+ // is included for both adapters because the Go gateway does not read+ // the Python gateway's project-scope header.+ const properties = {+ ...gatewayProperties,+ ai_product: aiProduct,+ team_id: projectId,+ };+ customHeaders = buildPosthogPropertiesHeaderLines(properties);+ openaiCustomHeaders = buildPosthogPropertiesHeaderRecord(properties);+ // The Go gateway writes this into the OpenAI body's `service_tier`, which+ // is the only way a Codex run reaches the flex or priority queue: Codex+ // itself omits a tier its model catalogue does not advertise. Codex-only,+ // so it rides the OpenAI record; the Claude path has no tier concept.+ if (serviceTier) {+ openaiCustomHeaders["X-PostHog-Service-Tier"] = serviceTier;+ }+ // Codex sends no trace header, so the gateway stamps a fresh id per+ // request and a run's generations each land in a trace of one. Codex-only:+ // this header outranks `traceparent`, so setting it for Claude would+ // replace the per-turn ids its CLI mints with one id for the whole run.+ if (taskRunId && runtimeAdapter === "codex") {+ openaiCustomHeaders["X-PostHog-Trace-Id"] = taskRunId;+ }+ } else {+ customHeaders = buildPosthogScopedPropertyHeaderLines(+ gatewayProperties,+ projectId,+ );+ // No $ai_session_id on the Go-gateway path above: it strips $-prefixed+ // blob keys, so the session id would be silently dropped there.+ openaiCustomHeaders = buildPosthogScopedPropertyHeaderRecord(+ {+ ...gatewayProperties,+ team_id: projectId,+ $ai_session_id: taskId,+ },+ projectId,+ );+ }++ return {+ anthropicBaseUrl: gatewayUrl,+ anthropicAuthToken: llmBearer,+ openaiBaseUrl,+ openaiApiKey: llmBearer,+ anthropicCustomHeaders: customHeaders,+ openaiCustomHeaders,+ posthogProjectId: String(projectId),+ };+}++/**+ * The codex session's LLM auth, from the resolved gateway env. Codex must never+ * read the raw run credential: on the Go-gateway path the bearer is the per-run+ * scoped token (see buildGatewayEnv).+ */+export function codexAuthFromGatewayEnv(env: GatewayEnv): {+ apiBaseUrl: string;+ apiKey: string;+} {+ return { apiBaseUrl: env.openaiBaseUrl, apiKey: env.openaiApiKey };+}