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.

chore(cdp): remove the self-loop guard mode flag (#69659)

Daniel Marchukb1bab009afca
diff --git a/nodejs/src/cdp/cdp-services.ts b/nodejs/src/cdp/cdp-services.ts
index 1a84f231922c..adfdaa499d7d 100644
--- a/nodejs/src/cdp/cdp-services.ts
+++ b/nodejs/src/cdp/cdp-services.ts
@@ -147,7 +147,6 @@ export type CdpCoreServicesConfig = Pick<
| 'CDP_FETCH_RETRIES'
| 'CDP_FETCH_BACKOFF_BASE_MS'
| 'CDP_FETCH_BACKOFF_MAX_MS'
- | 'CDP_SELF_LOOP_GUARD_MODE'
| 'CDP_EMAIL_TRACKING_URL'
| 'HOG_FUNCTION_MONITORING_APP_METRICS_TOPIC'
| 'HOG_FUNCTION_MONITORING_APP_METRICS_PRODUCER'
@@ -416,7 +415,6 @@ export function createCdpCoreServices(
fetchRetries: config.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: config.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: config.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: config.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: deps.teamManager, siteUrl: config.SITE_URL },
hogInputsService,
diff --git a/nodejs/src/cdp/config.ts b/nodejs/src/cdp/config.ts
index 13fd2a7b5d2e..c3ab8ceb5bfc 100644
--- a/nodejs/src/cdp/config.ts
+++ b/nodejs/src/cdp/config.ts
@@ -18,7 +18,6 @@ import {
WARPSTREAM_CYCLOTRON_PRODUCER,
WARPSTREAM_INGESTION_PRODUCER,
} from './outputs/producers'
-import { SelfLoopGuardMode } from './services/self-loop-guard'
import { CyclotronJobQueueKind, CyclotronJobQueueSource } from './types'
// CdpConfig intersects ClickhouseConfig so any consumer reading
@@ -96,7 +95,6 @@ export type CdpConfig = ClickhouseConfig & {
CDP_FETCH_RETRIES: number
CDP_FETCH_BACKOFF_BASE_MS: number
CDP_FETCH_BACKOFF_MAX_MS: number
- CDP_SELF_LOOP_GUARD_MODE: SelfLoopGuardMode
CDP_OVERFLOW_QUEUE_ENABLED: boolean
HOG_FUNCTION_MONITORING_APP_METRICS_TOPIC: string
HOG_FUNCTION_MONITORING_APP_METRICS_PRODUCER: CdpProducerName
@@ -216,10 +214,6 @@ export function getDefaultCdpConfig(): CdpConfig {
CDP_FETCH_RETRIES: 3,
CDP_FETCH_BACKOFF_BASE_MS: 1000,
CDP_FETCH_BACKOFF_MAX_MS: 30000,
- // Observe-only by default. Values: 'disabled' | 'warn' | 'enforce'. 'warn' detects
- // and emits cdp_self_loop_guard_total without blocking; 'enforce' bounds true loops
- // at SELF_LOOP_MAX_DEPTH hops. Roll out warn -> enforce per environment.
- CDP_SELF_LOOP_GUARD_MODE: 'warn',
CDP_OVERFLOW_QUEUE_ENABLED: false,
HOG_FUNCTION_MONITORING_APP_METRICS_TOPIC: KAFKA_APP_METRICS_2,
HOG_FUNCTION_MONITORING_APP_METRICS_PRODUCER: WARPSTREAM_INGESTION_PRODUCER,
diff --git a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
index 0e55e2eae6af..6c92d845b9c9 100644
--- a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
+++ b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
@@ -544,7 +544,6 @@ export function createHogTransformerService(
fetchRetries: config.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: config.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: config.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: config.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: deps.teamManager, siteUrl: config.SITE_URL },
hogInputsService,
diff --git a/nodejs/src/cdp/services/hog-executor.service.test.ts b/nodejs/src/cdp/services/hog-executor.service.test.ts
index 27bcbe73ba89..0a95de3a17fd 100644
--- a/nodejs/src/cdp/services/hog-executor.service.test.ts
+++ b/nodejs/src/cdp/services/hog-executor.service.test.ts
@@ -75,7 +75,6 @@ describe('Hog Executor', () => {
fetchRetries: hub.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: hub.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: hub.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: hub.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: hub.teamManager, siteUrl: hub.SITE_URL },
hogInputsService,
@@ -2059,10 +2058,6 @@ describe('Hog Executor', () => {
} as any)
}
- const setMode = (mode: 'disabled' | 'warn' | 'enforce'): void => {
- ;(executor as any).config.selfLoopGuardMode = mode
- }
-
const ownTokenCaptureBody = (): string =>
JSON.stringify({ api_key: OWN_TOKEN, event: 'replicated', distinct_id: 'u1', properties: {} })
@@ -2098,56 +2093,7 @@ describe('Hog Executor', () => {
return metric.values.find((v) => v.labels.mode === mode && v.labels.action === action)?.value ?? 0
}
- // The detected count is the production signal that drives the enforce decision,
- // so assert it actually moves - not just the human-facing log.
- const readDetectedCount = (): Promise<number> => readActionCount('warn', 'detected')
-
- it('detects a self-referential ingest fetch and logs + meters it without blocking (warn)', async () => {
- setMode('warn')
- mockOwnTeam()
- const invocation = await createFetchInvocation({
- url: INGEST_URL,
- method: 'POST',
- body: ownTokenCaptureBody(),
- })
- ;(fetch as jest.Mock).mockImplementationOnce(() =>
- Promise.resolve({ status: 200, headers: {}, text: () => Promise.resolve('ok') })
- )
- const detectedBefore = await readDetectedCount()
-
- const result = await executor.executeFetch(invocation)
-
- // Observe-only: the fetch still happens and nothing errors.
- expect(result.error).toBeUndefined()
- expect(cleanLogs(result.logs.map((l) => l.message))).toEqual(
- expect.arrayContaining([expect.stringContaining('can form an event-forwarding loop')])
- )
- expect(await readDetectedCount()).toBe(detectedBefore + 1)
- })
-
- it('does not flag a normal external fetch even with the project token in the body', async () => {
- setMode('warn')
- mockOwnTeam()
- const invocation = await createFetchInvocation({
- url: `${baseUrl}/test`,
- method: 'POST',
- body: ownTokenCaptureBody(),
- })
- mockRequest.mockClear()
- const detectedBefore = await readDetectedCount()
-
- const result = await executor.executeFetch(invocation)
-
- expect(result.error).toBeUndefined()
- expect(mockRequest).toHaveBeenCalled()
- expect(cleanLogs(result.logs.map((l) => l.message))).not.toEqual(
- expect.arrayContaining([expect.stringContaining('event-forwarding loop')])
- )
- expect(await readDetectedCount()).toBe(detectedBefore)
- })
-
it('fails open: a team lookup error never breaks the fetch', async () => {
- setMode('warn')
jest.spyOn(hub.teamManager, 'getTeam').mockRejectedValue(new Error('db unavailable'))
const invocation = await createFetchInvocation({
url: INGEST_URL,
@@ -2172,7 +2118,6 @@ describe('Hog Executor', () => {
{ case: 'mid-chain under the cap', depth: 2, stampedTo: 3 },
{ case: 'the last hop under the cap', depth: 9, stampedTo: 10 },
])('enforce: allows + stamps the next hop ($case)', async ({ depth, stampedTo }) => {
- setMode('enforce')
mockOwnTeam()
const invocation = await createFetchInvocation({
url: INGEST_URL,
@@ -2197,7 +2142,6 @@ describe('Hog Executor', () => {
// depth for a DIFFERENT function is treated as depth 0 here, so a legitimately
// running destination is never blocked by an unrelated deep chain.
it('enforce: does NOT block when the high depth belongs to another function', async () => {
- setMode('enforce')
mockOwnTeam()
const invocation = await createFetchInvocation({
url: INGEST_URL,
@@ -2219,7 +2163,6 @@ describe('Hog Executor', () => {
})
it('enforce: breaks the chain once it reaches the cap', async () => {
- setMode('enforce')
mockOwnTeam()
const invocation = await createFetchInvocation({
url: INGEST_URL,
@@ -2243,7 +2186,6 @@ describe('Hog Executor', () => {
})
it('enforce: leaves a normal external fetch untouched', async () => {
- setMode('enforce')
mockOwnTeam()
const invocation = await createFetchInvocation({
url: `${baseUrl}/test`,
diff --git a/nodejs/src/cdp/services/hog-executor.service.ts b/nodejs/src/cdp/services/hog-executor.service.ts
index 7c98c195798a..1772dbef713c 100644
--- a/nodejs/src/cdp/services/hog-executor.service.ts
+++ b/nodejs/src/cdp/services/hog-executor.service.ts
@@ -37,7 +37,6 @@ import { EmailService } from './messaging/email.service'
import { RecipientTokensService } from './messaging/recipient-tokens.service'
import {
SELF_LOOP_MAX_DEPTH,
- SelfLoopGuardMode,
getSelfLoopDepth,
injectSelfLoopDepth,
isPostHogIngestUrl,
@@ -57,7 +56,6 @@ export interface HogExecutorConfig {
fetchRetries: number
fetchBackoffBaseMs: number
fetchBackoffMaxMs: number
- selfLoopGuardMode: SelfLoopGuardMode
}
export interface HogExecutorAsyncContext {
@@ -820,8 +818,7 @@ export class HogExecutorService {
// ingest-URL check gates the team lookup so external fetches (the common case) pay
// nothing, and the whole block fails open - the guard must never break a destination
// it was only meant to protect.
- const guardMode = this.config.selfLoopGuardMode
- if (guardMode !== 'disabled' && isPostHogIngestUrl(params.url)) {
+ if (isPostHogIngestUrl(params.url)) {
try {
const team = await this.asyncContext.teamManager.getTeam(invocation.teamId)
if (team && isSelfReferentialIngestFetch({ url: params.url, body: params.body, team })) {
@@ -831,15 +828,9 @@ export class HogExecutorService {
const functionId = invocation.hogFunction.id
const depth = getSelfLoopDepth(invocation.state.globals.event?.properties, functionId)
- if (guardMode === 'warn') {
- selfLoopGuardCounter.inc({ mode: guardMode, action: 'detected' })
- addLog(
- 'warn',
- `This fetch targets a PostHog ingestion endpoint using this project's own API key, which can form an event-forwarding loop. To capture an event back into this project use the 'postHogCapture' helper, or to enrich incoming events use a transformation.`
- )
- } else if (depth >= SELF_LOOP_MAX_DEPTH) {
- // enforce, this destination has re-fed itself to the cap - break it.
- selfLoopGuardCounter.inc({ mode: guardMode, action: 'blocked' })
+ if (depth >= SELF_LOOP_MAX_DEPTH) {
+ // This destination has re-fed itself to the cap - break it.
+ selfLoopGuardCounter.inc({ mode: 'enforce', action: 'blocked' })
addLog(
'error',
`Refusing to fetch a PostHog ingestion endpoint using this project's own API key - this destination's event-forwarding loop has already repeated ${SELF_LOOP_MAX_DEPTH} times. To capture an event back into this project use the 'postHogCapture' helper, or to enrich incoming events use a transformation.`
@@ -847,11 +838,10 @@ export class HogExecutorService {
result.error = new Error('Self-referential event-forwarding loop blocked at max depth')
result.finished = true
return result
- } else {
- // enforce, under the cap - stamp this destination's next hop and proceed.
- selfLoopGuardCounter.inc({ mode: guardMode, action: 'allowed_with_counter' })
- params.body = injectSelfLoopDepth(params.body, functionId, depth + 1)
}
+ // Under the cap - stamp this destination's next hop and proceed.
+ selfLoopGuardCounter.inc({ mode: 'enforce', action: 'allowed_with_counter' })
+ params.body = injectSelfLoopDepth(params.body, functionId, depth + 1)
}
} catch (err) {
logger.warn('🦔', '[HogExecutor] Self-loop guard skipped due to an internal error', {
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
index 4667b42144f9..cde61aeb1d47 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -66,7 +66,6 @@ describe('HogFunctionHandler', () => {
fetchRetries: hub.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: hub.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: hub.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: hub.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: hub.teamManager, siteUrl: hub.SITE_URL },
hogInputsService,
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
index dd0c56ffd714..a011a4da58b9 100644
--- a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
+++ b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.test.ts
@@ -85,7 +85,6 @@ describe('Hogflow Executor', () => {
fetchRetries: hub.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: hub.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: hub.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: hub.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: hub.teamManager, siteUrl: hub.SITE_URL },
hogInputsService,
diff --git a/nodejs/src/cdp/services/self-loop-guard.ts b/nodejs/src/cdp/services/self-loop-guard.ts
index 6d7073376246..cc54b161e74f 100644
--- a/nodejs/src/cdp/services/self-loop-guard.ts
+++ b/nodejs/src/cdp/services/self-loop-guard.ts
@@ -7,13 +7,9 @@ import { Team } from '../../types'
// A destination that fetches one of PostHog's own ingestion endpoints, authenticating
// as its own project, re-enters the event pipeline. If that re-ingested event then
// re-triggers the same destination, the chain forms an event-forwarding loop that
-// doubles traffic on every hop.
-//
-// - 'disabled': no-op.
-// - 'warn': detect the shape and emit a metric, but never block (observe-only).
-// - 'enforce': bound the loop by counting how many times *this specific destination* has
-// re-fed the pipeline - allow the first SELF_LOOP_MAX_DEPTH hops, then break it.
-export type SelfLoopGuardMode = 'disabled' | 'warn' | 'enforce'
+// doubles traffic on every hop. The guard bounds the loop by counting how many times
+// *this specific destination* has re-fed the pipeline - allowing the first
+// SELF_LOOP_MAX_DEPTH hops, then breaking it.
// Event property carrying the self-loop depth *per hog function*, keyed by function id:
// { "<functionId>": <hops this destination has re-fed itself> }
diff --git a/nodejs/src/cdp/templates/test/test-helpers.ts b/nodejs/src/cdp/templates/test/test-helpers.ts
index 9dcaee246c29..4a443039969f 100644
--- a/nodejs/src/cdp/templates/test/test-helpers.ts
+++ b/nodejs/src/cdp/templates/test/test-helpers.ts
@@ -216,7 +216,6 @@ export class TemplateTester {
fetchRetries: config.CDP_FETCH_RETRIES,
fetchBackoffBaseMs: config.CDP_FETCH_BACKOFF_BASE_MS,
fetchBackoffMaxMs: config.CDP_FETCH_BACKOFF_MAX_MS,
- selfLoopGuardMode: config.CDP_SELF_LOOP_GUARD_MODE,
},
{ teamManager: this.mockTeamManager as any, siteUrl: config.SITE_URL },
hogInputsService,