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.

fix(cohorts): keep the backfill origin on cascaded membership changes (#118434)

Harley Alexander7aae6b5117b9
diff --git a/rust/cohort-stream-processor/src/cascade/decision.rs b/rust/cohort-stream-processor/src/cascade/decision.rs
index efff26153a18..c57b1cea57eb 100644
--- a/rust/cohort-stream-processor/src/cascade/decision.rs
+++ b/rust/cohort-stream-processor/src/cascade/decision.rs
@@ -37,7 +37,9 @@ pub fn should_emit(
person_id: incoming.change.person_id.clone(),
last_updated: new_last_updated.to_string(),
status: new_status,
- origin: None,
+ // A flip caused by a seed or reconcile restates membership too, so consumers that skip
+ // backfill output must also skip its cascades.
+ origin: incoming.change.origin,
run_id: None,
},
// Carried forward so the chain keeps one stable replay identity across hops.
@@ -65,7 +67,10 @@ pub fn first_cascade(change: CohortMembershipChange, source_offset: i64) -> Casc
#[cfg(test)]
mod tests {
+ use cohort_core::seed::RunId;
+
use super::*;
+ use crate::producer::ChangeOrigin;
const TS_IN: &str = "2026-05-26 12:34:56.789123";
const TS_OUT: &str = "2026-06-16 00:00:00.000000";
@@ -95,7 +100,9 @@ mod tests {
#[test]
fn normal_hop_increments_depth_and_extends_the_chain_carrying_fields() {
- let incoming = incoming(10, 1, vec![10]);
+ let mut incoming = incoming(10, 1, vec![10]);
+ incoming.change.origin = Some(ChangeOrigin::Seed);
+ incoming.change.run_id = Some(RunId(uuid::Uuid::nil()));
let decision = should_emit(&incoming, CohortId(20), MembershipStatus::Left, TS_OUT, 8);
let CascadeDecision::Emit { outgoing } = decision else {
@@ -116,6 +123,11 @@ mod tests {
assert_eq!(outgoing.change.person_id, incoming.change.person_id);
assert_eq!(outgoing.source_offset, incoming.source_offset);
assert_eq!(outgoing.originating_cohort_id, 10);
+ assert_eq!(outgoing.change.origin, Some(ChangeOrigin::Seed));
+ assert_eq!(
+ outgoing.change.run_id, None,
+ "the referrer is not part of the run"
+ );
}
#[test]
diff --git a/rust/cohort-stream-processor/src/producer/mod.rs b/rust/cohort-stream-processor/src/producer/mod.rs
index 77f0a696cf10..6327cd2128de 100644
--- a/rust/cohort-stream-processor/src/producer/mod.rs
+++ b/rust/cohort-stream-processor/src/producer/mod.rs
@@ -55,7 +55,8 @@ pub struct CohortMembershipChange {
/// Backfill provenance; `None` on the live path.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<ChangeOrigin>,
- /// The backfill run that emitted this change; set iff `origin` is.
+ /// The backfill run that emitted this change. A cascade keeps `origin` but not `run_id`, because
+ /// the membership writer sweeps each (run_id, cohort) and the referrer cohort is not in that run.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run_id: Option<RunId>,
}
diff --git a/rust/cohort-stream-processor/src/workers/cascade_path.rs b/rust/cohort-stream-processor/src/workers/cascade_path.rs
index d925b71191f2..56ed91a70739 100644
--- a/rust/cohort-stream-processor/src/workers/cascade_path.rs
+++ b/rust/cohort-stream-processor/src/workers/cascade_path.rs
@@ -148,7 +148,7 @@ pub(crate) async fn handle_cascade(
person_id: message.change.person_id.clone(),
last_updated: last_updated.to_string(),
status,
- origin: None,
+ origin: message.change.origin,
run_id: None,
});
// The external flip above is unconditional; only the onward hop is depth/cycle bounded.
@@ -264,13 +264,14 @@ fn clickhouse_millis(ts: &str) -> i64 {
mod tests {
use super::*;
use chrono_tz::UTC;
+ use cohort_core::seed::RunId;
use serde_json::{json, Value};
use tempfile::TempDir;
use crate::cascade::first_cascade;
use crate::filters::{FilterCatalog, TeamFiltersBuilder};
use crate::producer::{
- CaptureCascadeSink, CaptureSink, CaptureStreamEventSink, CaptureTransferSink,
+ CaptureCascadeSink, CaptureSink, CaptureStreamEventSink, CaptureTransferSink, ChangeOrigin,
MembershipStatus,
};
use crate::stage1::key::LeafStateKey;
@@ -501,7 +502,10 @@ mod tests {
let membership = CaptureSink::new();
let cascade = CaptureCascadeSink::new();
let (sink, deps) = deps(&membership, &cascade, true, 1000);
- let msg = first_cascade(incoming(2, alice, 1, vec![2]).change, 99);
+ let mut flipped = incoming(2, alice, 1, vec![2]).change;
+ flipped.origin = Some(ChangeOrigin::Seed);
+ flipped.run_id = Some(RunId(Uuid::nil()));
+ let msg = first_cascade(flipped, 99);
handle_cascade(
PARTITION,
@@ -520,6 +524,12 @@ mod tests {
assert_eq!(changes[0].cohort_id, 1);
assert_eq!(changes[0].status, MembershipStatus::Entered);
assert_eq!(changes[0].person_id, alice.to_string());
+ assert_eq!(
+ changes[0].origin,
+ Some(ChangeOrigin::Seed),
+ "a seed flip's cascade stays a seed change"
+ );
+ assert_eq!(changes[0].run_id, None, "B is not part of A's run");
let onward = cascade.messages();
assert_eq!(onward.len(), 1, "one onward cascade for B");