feat(cdp): add enforce mode to the self-loop guard (depth-counter)
Problem
The self-loop detection guard has been observing in production in warn mode. A destination that fetch()es back into its own project's ingestion endpoint can form an event-forwarding loop that doubles traffic on every hop. warn measures this but never stops it.
The warn data made the key constraint clear: most self-referential ingest fetches are legitimate self-terminating self-captures — a destination writes a derived event or a $set person property back into its own project, where the captured event does not re-trigger the destination. A blanket block would wrongly reject these (the class an earlier over-broad attempt broke). Only destinations whose re-ingested event re-triggers the same destination form a true unbounded loop.
So enforcement must cap genuine re-triggering loops without ever stopping a destination that is supposed to be running.
Changes
Adds enforce mode, which bounds a loop by counting how many times a specific destination has re-fed the pipeline — keyed by function_id:
- New event property
$hog_function_self_loop_depth = { "<functionId>": depth }. - On a self-referential ingest fetch, the guard reads this destination's own depth from the triggering event.
- Under
SELF_LOOP_MAX_DEPTH(10): stampdepth + 1for this function onto the outgoing capture body and let the fetch proceed. - At the cap: break the chain (error + finish, no fetch), emit
cdp_self_loop_guard_total{action="blocked"}.
Why per-function rather than a shared counter: an earlier iteration reused the shared $hog_function_execution_count (the one postHogCapture increments). That counter reflects the depth of the whole chain, so an event that merely passed through many unrelated functions could arrive at a legitimate self-referential destination already at the cap and be blocked on its first, legitimate hop. Keying the depth by function_id removes that failure mode by construction: a destination is bounded only by its own re-entries, so no unrelated chain can ever stop a legitimately-running destination.
CDP_SELF_LOOP_GUARD_MODE is disabled | warn | enforce, still defaulting to warn — this PR ships the capability; flipping a region to enforce is a separate env-var change.
How did you test this code?
I'm an agent. Automated tests:
self-loop-guard.test.ts: per-functioninjectSelfLoopDepth/getSelfLoopDepth(single + batch, preserves other functions' entries), the URL/credential classification matrix, and the cross-block regression — a depth of 50 for a different function reads as 0 for this one.executeFetchwiring tests: allow + stamp under the cap (hop 0 / mid-chain / last hop), block at the cap, external fetch untouched, fails-open on team-lookup error, and does NOT block when the high depth belongs to another function.- Typecheck, eslint, prettier clean.
Manual E2E on a local stack running this branch in enforce mode, driving a real template-posthog-replicator destination configured to re-emit the event it triggers on (a true loop), through the full event → ingestion → cyclotron → executeFetch pipeline:
- The per-function depth climbed organically (
allowed_with_counter0→10), the chain broke at exactly 10 hops (blocked+1) and stayed stable — no runaway. - The re-ingested events carry
$hog_function_self_loop_depth = { "<functionId>": 1..10 }, confirming the per-function keying propagates through the real template.
This verifies both halves: a real loop caps at exactly 10, and (via the cross-block tests) a destination is never blocked by depth that belongs to another function.
Rollout
Merge is behavior-neutral (warn stays default). Then flip CDP_SELF_LOOP_GUARD_MODE=enforce per environment (dev → one region → both), watching cdp_self_loop_guard_total{action="blocked"} to confirm it only bites true loops.
Reviewer notes:
- Per-destination, not per-chain. A destination is capped only by its own re-entries; a mutual loop between two distinct destinations (A↔B) is still bounded, but each caps after it has run 10 times (~20 total hops) rather than 10.
- Deliberate divergence from
postHogCapture's shared$hog_function_execution_count, which serves a different (per-chain) purpose. - The depth property is user-visible on re-ingested events (a
$-prefixed system property, not in the curated taxonomy). It only lands on same-project self-captures. If we'd rather it never surface, a follow-up can strip it at ingest — there is existing precedent for stripping the analogouspostHogCaptureproperty in the source-webhooks path.
🤖 Agent context
Authored with Claude Code. The design evolved from warn-stage production data: a blanket block was rejected because most detections are legitimate self-terminating self-captures, and a shared-counter depth was rejected because it could block a legitimate destination sitting at the end of an unrelated deep chain. The per-function depth counter is the result — it keeps enforcement general (new loops auto-cap) while making "never stop a legitimately-running destination" a structural property rather than a tuning choice.
Reviews (1): Last reviewed commit: "feat(cdp): add enforce mode to the self-..." | Re-trigger Greptile
Reviews (2): Last reviewed commit: "feat(cdp): add enforce mode to the self-..." | Re-trigger Greptile
PR overview
This pull request adds an enforce mode to the CDP self-loop guard in the Hog executor, using a depth counter to detect and limit recursive ingestion behavior. The touched code focuses on stamping or checking loop-depth metadata around capture and batch ingestion calls made by Hog functions.
The PR is moving in the right direction, with one issue already addressed, but one bypass remains open. The current guard does not cover all payload formats accepted by ingestion, so a function author could submit URL-encoded or GET data payloads that avoid depth stamping and continue recursive same-project events past the intended limit. This leaves a concrete denial-of-service style risk around uncontrolled self-looping until the guard handles the same payload shapes ingestion accepts.
Open issues (1)
- Medium: Self-loop guard bypass for form payloads — nodejs/src/cdp/services/hog-executor.service.ts:785
Fixed/addressed: 1 · PR risk: 6/10