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.

feat(settings): manage cloud environments on the web under today-rail-nav (#110263)

Shy Alteredcec4f05980
diff --git a/frontend/src/scenes/settings/SettingsMap.tsx b/frontend/src/scenes/settings/SettingsMap.tsx
index cff0c4db50a4..7a1d56805c8d 100644
--- a/frontend/src/scenes/settings/SettingsMap.tsx
+++ b/frontend/src/scenes/settings/SettingsMap.tsx
@@ -91,6 +91,7 @@ import {
} from './environment/ActivityLogSettings'
import { AgentUsageSettings } from './environment/AgentUsageSettings'
import { AutocaptureSettings, WebVitalsAutocaptureSettings } from './environment/AutocaptureSettings'
+import { CloudEnvironmentsSettings } from './environment/CloudEnvironmentsSettings'
import { CorrelationConfig } from './environment/CorrelationConfig'
import { CSPReportingSettings } from './environment/CSPReportingSettings'
import { DataAttributes } from './environment/DataAttributes'
@@ -537,6 +538,24 @@ export const SETTINGS_MAP: SettingSection[] = [
},
],
},
+ {
+ level: 'environment',
+ id: 'environment-ai-cloud-environments',
+ title: 'Cloud environments',
+ group: 'AI',
+ flag: 'TODAY_RAIL_NAV',
+ keywords: ['environments', 'sandbox', 'network access', 'allowed domains', 'desktop'],
+ settings: [
+ {
+ id: 'ai-cloud-environments',
+ title: 'Environments',
+ description:
+ 'Cloud runs start in a sandbox. An environment sets which repositories it applies to and which hosts the sandbox can reach.',
+ component: <CloudEnvironmentsSettings />,
+ keywords: ['sandbox', 'network', 'domains', 'firewall', 'repositories', 'cloud runs'],
+ },
+ ],
+ },
{
level: 'environment',
id: 'posthog-mcp',
diff --git a/frontend/src/scenes/settings/environment/CloudEnvironmentModal.tsx b/frontend/src/scenes/settings/environment/CloudEnvironmentModal.tsx
new file mode 100644
index 000000000000..9c479b3f86b0
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/CloudEnvironmentModal.tsx
@@ -0,0 +1,123 @@
+import { useActions, useValues } from 'kea'
+
+import {
+ LemonButton,
+ LemonCheckbox,
+ LemonInput,
+ LemonInputSelect,
+ LemonLabel,
+ LemonModal,
+ LemonSegmentedButton,
+} from '@posthog/lemon-ui'
+
+import { LemonRadio } from 'lib/lemon-ui/LemonRadio'
+
+import { SandboxEnvironmentNetworkAccessLevelEnumApi } from 'products/tasks/frontend/generated/api.schemas'
+
+import { NETWORK_ACCESS_COPY } from './cloudEnvironmentNetworkAccess'
+import { cloudEnvironmentsLogic } from './cloudEnvironmentsLogic'
+
+export function CloudEnvironmentModal(): JSX.Element | null {
+ const { draft, draftSaving, draftError } = useValues(cloudEnvironmentsLogic)
+ const { updateDraft, closeDraft, saveDraft } = useActions(cloudEnvironmentsLogic)
+
+ if (!draft) {
+ return null
+ }
+
+ return (
+ <LemonModal
+ isOpen
+ onClose={closeDraft}
+ title={draft.id ? 'Edit environment' : 'New environment'}
+ description="Cloud runs start in a sandbox. An environment sets which repositories it applies to and what the sandbox can reach."
+ width={560}
+ footer={
+ <>
+ <LemonButton
+ type="secondary"
+ onClick={closeDraft}
+ disabledReason={draftSaving ? 'Saving' : undefined}
+ >
+ Cancel
+ </LemonButton>
+ <LemonButton
+ type="primary"
+ onClick={saveDraft}
+ loading={draftSaving}
+ disabledReason={draftError ?? undefined}
+ data-attr="cloud-environment-save"
+ >
+ {draft.id ? 'Save' : 'Create environment'}
+ </LemonButton>
+ </>
+ }
+ >
+ <div className="flex flex-col gap-4">
+ <div className="flex flex-col gap-1">
+ <LemonLabel htmlFor="cloud-environment-name">Name</LemonLabel>
+ <LemonInput
+ id="cloud-environment-name"
+ value={draft.name}
+ onChange={(name) => updateDraft({ name })}
+ placeholder="For example: Web app"
+ autoFocus
+ />
+ </div>
+ <div className="flex flex-col gap-1">
+ <LemonLabel>Who can use it</LemonLabel>
+ <LemonSegmentedButton
+ value={draft.private ? 'private' : 'project'}
+ onChange={(value) => updateDraft({ private: value === 'private' })}
+ options={[
+ { value: 'private', label: 'Only you' },
+ { value: 'project', label: 'Everyone in this project' },
+ ]}
+ size="small"
+ />
+ </div>
+ <div className="flex flex-col gap-1">
+ <LemonLabel info="Leave empty to use this environment for any repository.">Repositories</LemonLabel>
+ <LemonInputSelect
+ mode="multiple"
+ allowCustomValues
+ value={draft.repositories}
+ onChange={(repositories) => updateDraft({ repositories })}
+ placeholder="Type org/repo and press Enter"
+ />
+ </div>
+ <div className="flex flex-col gap-1">
+ <LemonLabel>Network access</LemonLabel>
+ <LemonRadio
+ value={draft.network_access_level}
+ onChange={(network_access_level) => updateDraft({ network_access_level })}
+ options={Object.values(SandboxEnvironmentNetworkAccessLevelEnumApi).map((level) => ({
+ value: level,
+ label: NETWORK_ACCESS_COPY[level].label,
+ description: NETWORK_ACCESS_COPY[level].description,
+ }))}
+ />
+ </div>
+ {draft.network_access_level === SandboxEnvironmentNetworkAccessLevelEnumApi.Custom ? (
+ <div className="flex flex-col gap-2">
+ <div className="flex flex-col gap-1">
+ <LemonLabel>Allowed domains</LemonLabel>
+ <LemonInputSelect
+ mode="multiple"
+ allowCustomValues
+ value={draft.allowed_domains}
+ onChange={(allowed_domains) => updateDraft({ allowed_domains })}
+ placeholder="Type a domain, such as api.example.com, and press Enter"
+ />
+ </div>
+ <LemonCheckbox
+ checked={draft.include_default_domains}
+ onChange={(include_default_domains) => updateDraft({ include_default_domains })}
+ label="Also allow the trusted package sources (GitHub, npm, PyPI)"
+ />
+ </div>
+ ) : null}
+ </div>
+ </LemonModal>
+ )
+}
diff --git a/frontend/src/scenes/settings/environment/CloudEnvironmentsSettings.tsx b/frontend/src/scenes/settings/environment/CloudEnvironmentsSettings.tsx
new file mode 100644
index 000000000000..4e2a4254d62b
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/CloudEnvironmentsSettings.tsx
@@ -0,0 +1,117 @@
+import { useActions, useValues } from 'kea'
+
+import { IconEllipsis, IconPlus } from '@posthog/icons'
+import { LemonBanner, LemonButton, LemonDialog, LemonMenu, LemonTable, LemonTag } from '@posthog/lemon-ui'
+
+import type { SandboxEnvironmentDTOApi } from 'products/tasks/frontend/generated/api.schemas'
+
+import { CloudEnvironmentModal } from './CloudEnvironmentModal'
+import { networkAccessLabel } from './cloudEnvironmentNetworkAccess'
+import { cloudEnvironmentsLogic } from './cloudEnvironmentsLogic'
+
+export function CloudEnvironmentsSettings(): JSX.Element {
+ const { environments, environmentsLoading, environmentsFailed } = useValues(cloudEnvironmentsLogic)
+ const { loadEnvironments, startNewEnvironment, editEnvironment, deleteEnvironment } =
+ useActions(cloudEnvironmentsLogic)
+
+ const confirmDelete = (environment: SandboxEnvironmentDTOApi): void => {
+ LemonDialog.open({
+ title: `Delete "${environment.name}"?`,
+ description: 'Cloud runs that use this environment fall back to full network access.',
+ primaryButton: { children: 'Delete', status: 'danger', onClick: () => deleteEnvironment(environment) },
+ secondaryButton: { children: 'Cancel' },
+ })
+ }
+
+ return (
+ <div className="flex flex-col gap-2 max-w-200">
+ <div className="flex">
+ <LemonButton
+ type="primary"
+ size="small"
+ icon={<IconPlus />}
+ onClick={startNewEnvironment}
+ data-attr="cloud-environment-new"
+ >
+ New environment
+ </LemonButton>
+ </div>
+ {environmentsFailed ? (
+ <LemonBanner
+ type="warning"
+ action={{ children: 'Try again', onClick: loadEnvironments, loading: environmentsLoading }}
+ >
+ The environments did not load.
+ </LemonBanner>
+ ) : (
+ <LemonTable
+ size="small"
+ loading={environmentsLoading && environments === null}
+ dataSource={environments ?? []}
+ rowKey="id"
+ emptyState="No environments yet. Cloud runs have full network access until you add one."
+ columns={[
+ {
+ title: 'Name',
+ key: 'name',
+ render: (_, environment) => (
+ <div className="flex flex-wrap items-center gap-1">
+ <span className="font-semibold">{environment.name}</span>
+ {environment.internal ? <LemonTag size="small">Internal</LemonTag> : null}
+ </div>
+ ),
+ },
+ {
+ title: 'Used by',
+ key: 'private',
+ render: (_, environment) => (
+ <LemonTag size="small" type={environment.private ? 'muted' : 'warning'}>
+ {environment.private ? 'Only you' : 'Everyone in this project'}
+ </LemonTag>
+ ),
+ },
+ {
+ title: 'Network access',
+ key: 'network_access_level',
+ render: (_, environment) => networkAccessLabel(environment.network_access_level),
+ },
+ {
+ title: 'Repositories',
+ key: 'repositories',
+ render: (_, environment) =>
+ environment.repositories?.length ? (
+ <span translate="no">{environment.repositories.join(', ')}</span>
+ ) : (
+ <span className="text-secondary">Any repository</span>
+ ),
+ },
+ {
+ key: 'actions',
+ width: 0,
+ render: (_, environment) =>
+ environment.internal ? null : (
+ <LemonMenu
+ items={[
+ { label: 'Edit', onClick: () => editEnvironment(environment) },
+ {
+ label: 'Delete',
+ status: 'danger',
+ onClick: () => confirmDelete(environment),
+ },
+ ]}
+ >
+ <LemonButton
+ size="small"
+ icon={<IconEllipsis />}
+ aria-label={`Actions for ${environment.name}`}
+ />
+ </LemonMenu>
+ ),
+ },
+ ]}
+ />
+ )}
+ <CloudEnvironmentModal />
+ </div>
+ )
+}
diff --git a/frontend/src/scenes/settings/environment/cloudEnvironmentNetworkAccess.ts b/frontend/src/scenes/settings/environment/cloudEnvironmentNetworkAccess.ts
new file mode 100644
index 000000000000..937b1bcdd47e
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/cloudEnvironmentNetworkAccess.ts
@@ -0,0 +1,23 @@
+import { SandboxEnvironmentNetworkAccessLevelEnumApi } from 'products/tasks/frontend/generated/api.schemas'
+
+export const NETWORK_ACCESS_COPY: Record<
+ SandboxEnvironmentNetworkAccessLevelEnumApi,
+ { label: string; description: string }
+> = {
+ [SandboxEnvironmentNetworkAccessLevelEnumApi.Trusted]: {
+ label: 'Trusted',
+ description: 'Downloads packages from verified sources, such as GitHub, npm, and PyPI.',
+ },
+ [SandboxEnvironmentNetworkAccessLevelEnumApi.Full]: {
+ label: 'Full',
+ description: 'Unrestricted internet access.',
+ },
+ [SandboxEnvironmentNetworkAccessLevelEnumApi.Custom]: {
+ label: 'Custom',
+ description: 'Only the domains you list.',
+ },
+}
+
+export function networkAccessLabel(level: string): string {
+ return NETWORK_ACCESS_COPY[level as SandboxEnvironmentNetworkAccessLevelEnumApi]?.label ?? level
+}
diff --git a/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.test.ts b/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.test.ts
new file mode 100644
index 000000000000..bb4f57e9657d
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.test.ts
@@ -0,0 +1,68 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { cloudEnvironmentsLogic } from './cloudEnvironmentsLogic'
+
+const ENVIRONMENT = {
+ id: '0190e5a3-0000-7000-8000-000000000001',
+ name: 'Web app',
+ network_access_level: 'custom',
+ allowed_domains: ['api.example.com'],
+ include_default_domains: true,
+ repositories: ['example/web'],
+ has_environment_variables: true,
+ environment_variable_keys: ['API_TOKEN'],
+ private: false,
+ internal: false,
+}
+
+describe('cloudEnvironmentsLogic', () => {
+ let logic: ReturnType<typeof cloudEnvironmentsLogic.build>
+ let patchBody: Record<string, unknown> | null
+
+ beforeEach(() => {
+ patchBody = null
+ useMocks({
+ get: { '/api/projects/:team/sandbox_environments/': { count: 1, results: [ENVIRONMENT] } },
+ patch: {
+ '/api/projects/:team/sandbox_environments/:id/': async ({ request }) => {
+ patchBody = (await request.json()) as Record<string, unknown>
+ return [200, { ...ENVIRONMENT, ...patchBody }]
+ },
+ },
+ })
+ initKeaTests()
+ logic = cloudEnvironmentsLogic()
+ logic.mount()
+ })
+
+ afterEach(() => logic.unmount())
+
+ it('saves an edit without touching the stored environment variables', async () => {
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.editEnvironment(logic.values.environments![0])
+ logic.actions.updateDraft({ name: 'Web app v2' })
+ await expectLogic(logic, () => logic.actions.saveDraft()).toFinishAllListeners()
+
+ expect(patchBody).toMatchObject({ name: 'Web app v2', allowed_domains: ['api.example.com'], private: false })
+ expect(patchBody).not.toHaveProperty('environment_variables')
+ expect(logic.values.draft).toBeNull()
+ })
+
+ it.each([
+ ['an empty name', { name: ' ' }, 'Enter a name'],
+ [
+ 'custom access with no domains',
+ { network_access_level: 'custom', allowed_domains: [], include_default_domains: false },
+ 'Add at least one allowed domain',
+ ],
+ ])('blocks saving with %s', async (_name, changes, expected) => {
+ logic.actions.startNewEnvironment()
+ logic.actions.updateDraft({ name: 'Web app', ...changes } as any)
+
+ expect(logic.values.draftError).toBe(expected)
+ })
+})
diff --git a/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.ts b/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.ts
new file mode 100644
index 000000000000..c62c78fb1770
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/cloudEnvironmentsLogic.ts
@@ -0,0 +1,237 @@
+import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+import { projectLogic } from 'scenes/projectLogic'
+
+import { sandboxCreate, sandboxDestroy, sandboxList, sandboxPartialUpdate } from 'products/tasks/frontend/generated/api'
+import {
+ type SandboxEnvironmentDTOApi,
+ SandboxEnvironmentNetworkAccessLevelEnumApi,
+} from 'products/tasks/frontend/generated/api.schemas'
+
+export interface CloudEnvironmentDraft {
+ id: string | null
+ name: string
+ network_access_level: SandboxEnvironmentNetworkAccessLevelEnumApi
+ allowed_domains: string[]
+ include_default_domains: boolean
+ repositories: string[]
+ private: boolean
+}
+
+const NEW_ENVIRONMENT: CloudEnvironmentDraft = {
+ id: null,
+ name: '',
+ network_access_level: SandboxEnvironmentNetworkAccessLevelEnumApi.Trusted,
+ allowed_domains: [],
+ include_default_domains: true,
+ repositories: [],
+ private: true,
+}
+
+function isNetworkAccessLevel(value: string): value is SandboxEnvironmentNetworkAccessLevelEnumApi {
+ return (Object.values(SandboxEnvironmentNetworkAccessLevelEnumApi) as string[]).includes(value)
+}
+
+export function draftFromEnvironment(environment: SandboxEnvironmentDTOApi): CloudEnvironmentDraft {
+ return {
+ id: environment.id,
+ name: environment.name,
+ network_access_level: isNetworkAccessLevel(environment.network_access_level)
+ ? environment.network_access_level
+ : SandboxEnvironmentNetworkAccessLevelEnumApi.Full,
+ allowed_domains: environment.allowed_domains ?? [],
+ include_default_domains: environment.include_default_domains,
+ repositories: environment.repositories ?? [],
+ private: environment.private,
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface cloudEnvironmentsLogicValues {
+ currentProjectId: number | null // projectLogic
+ draft: CloudEnvironmentDraft | null
+ draftError: string | null
+ draftSaving: boolean
+ environments: SandboxEnvironmentDTOApi[] | null
+ environmentsFailed: boolean
+ environmentsLoading: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface cloudEnvironmentsLogicActions {
+ closeDraft: () => {
+ value: true
+ }
+ deleteEnvironment: (environment: SandboxEnvironmentDTOApi) => {
+ environment: SandboxEnvironmentDTOApi
+ }
+ editEnvironment: (environment: SandboxEnvironmentDTOApi) => {
+ environment: SandboxEnvironmentDTOApi
+ }
+ loadEnvironments: () => any
+ loadEnvironmentsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadEnvironmentsSuccess: (
+ environments: SandboxEnvironmentDTOApi[] | null,
+ payload?: any
+ ) => {
+ environments: SandboxEnvironmentDTOApi[] | null
+ payload?: any
+ }
+ saveDraft: () => {
+ value: true
+ }
+ saveDraftDone: () => {
+ value: true
+ }
+ startNewEnvironment: () => {
+ value: true
+ }
+ updateDraft: (changes: Partial<CloudEnvironmentDraft>) => {
+ changes: Partial<CloudEnvironmentDraft>
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface cloudEnvironmentsLogicMeta {
+ __keaTypeGenInternalSelectorTypes: {
+ draftError: (draft: CloudEnvironmentDraft | null) => string | null
+ }
+}
+
+export type cloudEnvironmentsLogicType = MakeLogicType<
+ cloudEnvironmentsLogicValues,
+ cloudEnvironmentsLogicActions,
+ Record<string, any>,
+ cloudEnvironmentsLogicMeta
+>
+
+export const cloudEnvironmentsLogic = kea<cloudEnvironmentsLogicType>([
+ path(['scenes', 'settings', 'environment', 'cloudEnvironmentsLogic']),
+ connect(() => ({
+ values: [projectLogic, ['currentProjectId']],
+ })),
+ actions({
+ startNewEnvironment: true,
+ editEnvironment: (environment: SandboxEnvironmentDTOApi) => ({ environment }),
+ updateDraft: (changes: Partial<CloudEnvironmentDraft>) => ({ changes }),
+ closeDraft: true,
+ saveDraft: true,
+ saveDraftDone: true,
+ deleteEnvironment: (environment: SandboxEnvironmentDTOApi) => ({ environment }),
+ }),
+ loaders(({ values }) => ({
+ environments: [
+ null as SandboxEnvironmentDTOApi[] | null,
+ {
+ loadEnvironments: async (): Promise<SandboxEnvironmentDTOApi[] | null> => {
+ if (values.currentProjectId == null) {
+ return null
+ }
+ const page = await sandboxList(String(values.currentProjectId))
+ return page.results
+ },
+ },
+ ],
+ })),
+ reducers({
+ draft: [
+ null as CloudEnvironmentDraft | null,
+ {
+ startNewEnvironment: () => NEW_ENVIRONMENT,
+ editEnvironment: (_, { environment }) => draftFromEnvironment(environment),
+ updateDraft: (draft, { changes }) => (draft ? { ...draft, ...changes } : draft),
+ closeDraft: () => null,
+ },
+ ],
+ draftSaving: [
+ false,
+ {
+ saveDraft: () => true,
+ saveDraftDone: () => false,
+ closeDraft: () => false,
+ },
+ ],
+ environmentsFailed: [
+ false,
+ {
+ loadEnvironments: () => false,
+ loadEnvironmentsSuccess: () => false,
+ loadEnvironmentsFailure: () => true,
+ },
+ ],
+ }),
+ selectors({
+ draftError: [
+ (s) => [s.draft],
+ (draft: CloudEnvironmentDraft | null): string | null => {
+ if (!draft) {
+ return null
+ }
+ if (!draft.name.trim()) {
+ return 'Enter a name'
+ }
+ if (
+ draft.network_access_level === SandboxEnvironmentNetworkAccessLevelEnumApi.Custom &&
+ draft.allowed_domains.length === 0 &&
+ !draft.include_default_domains
+ ) {
+ return 'Add at least one allowed domain'
+ }
+ return null
+ },
+ ],
+ }),
+ listeners(({ actions, values }) => ({
+ saveDraft: async () => {
+ const { draft, currentProjectId } = values
+ if (!draft || currentProjectId == null || values.draftError) {
+ actions.saveDraftDone()
+ return
+ }
+ const body = {
+ name: draft.name.trim(),
+ network_access_level: draft.network_access_level,
+ allowed_domains: draft.allowed_domains,
+ include_default_domains: draft.include_default_domains,
+ repositories: draft.repositories,
+ private: draft.private,
+ }
+ try {
+ if (draft.id) {
+ await sandboxPartialUpdate(String(currentProjectId), draft.id, body)
+ } else {
+ await sandboxCreate(String(currentProjectId), body)
+ }
+ lemonToast.success(draft.id ? 'Environment saved' : 'Environment created')
+ actions.closeDraft()
+ actions.loadEnvironments()
+ } catch (error: any) {
+ lemonToast.error(error?.detail ?? 'The environment did not save. Try again.')
+ actions.saveDraftDone()
+ }
+ },
+ deleteEnvironment: async ({ environment }) => {
+ if (values.currentProjectId == null) {
+ return
+ }
+ try {
+ await sandboxDestroy(String(values.currentProjectId), environment.id)
+ lemonToast.success('Environment deleted')
+ actions.loadEnvironments()
+ } catch {
+ lemonToast.error('The environment was not deleted. Try again.')
+ }
+ },
+ })),
+ afterMount(({ actions }) => {
+ actions.loadEnvironments()
+ }),
+])
diff --git a/frontend/src/scenes/settings/types.ts b/frontend/src/scenes/settings/types.ts
index 9b4457edf5a9..ca27c3269dba 100644
--- a/frontend/src/scenes/settings/types.ts
+++ b/frontend/src/scenes/settings/types.ts
@@ -58,6 +58,7 @@ export type SettingSectionId =
| 'environment-task-agents'
| 'environment-ai-subscriptions'
| 'environment-ai-usage'
+ | 'environment-ai-cloud-environments'
| 'environment-tracing'
| 'environment-web-analytics'
| 'environment-workflows'
@@ -297,6 +298,7 @@ export type SettingId =
| 'ai-subscription-codex'
| 'task-agent-project-instructions'
| 'ai-usage-spend'
+ | 'ai-cloud-environments'
| 'task-agent-project-default'
| 'theme'
| 'tracing-distinct-id-attribute-keys'