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(personhog): add lifecycle service skeleton and DeletePersons proto (#74203)

José Sequeirafa1a9a60d471
diff --git a/proto/personhog/lifecycle/v1/lifecycle.proto b/proto/personhog/lifecycle/v1/lifecycle.proto
new file mode 100644
index 000000000000..e9aa73cabd9c
--- /dev/null
+++ b/proto/personhog/lifecycle/v1/lifecycle.proto
@@ -0,0 +1,57 @@
+syntax = "proto3";
+package personhog.lifecycle.v1;
+
+// DeletePersonsRequest identifies the persons to destroy. Arguments are
+// already person ids — there is no identity to resolve first, so the RPC
+// goes straight into the saga runner.
+message DeletePersonsRequest {
+ int64 team_id = 1;
+ // posthog_person.id values to delete. Max 250 per request.
+ repeated int64 person_ids = 2;
+ // Caller-generated operation id (UUID string). Persisted with the
+ // operation: a retried call carrying the same op_id attaches to the
+ // existing operation and returns its recorded outcome instead of starting
+ // a new one, so callers must reuse the op_id across retries.
+ string op_id = 3;
+}
+
+enum DeletePersonOutcome {
+ DELETE_PERSON_OUTCOME_UNSPECIFIED = 0;
+ // The person was destroyed (or the recorded outcome of a completed
+ // operation says it was).
+ DELETE_PERSON_OUTCOME_DELETED = 1;
+ // Another live lifecycle operation holds this person's mark; nothing was
+ // done for this person. Retry after that operation finishes.
+ DELETE_PERSON_OUTCOME_SKIPPED_CONFLICT = 2;
+ // No live person row with this id exists in the team.
+ DELETE_PERSON_OUTCOME_NOT_FOUND = 3;
+}
+
+message DeletePersonResult {
+ int64 person_id = 1;
+ DeletePersonOutcome outcome = 2;
+}
+
+message DeletePersonsResponse {
+ // Echo of the request op_id.
+ string op_id = 1;
+ // One result per requested person id, same order as the request.
+ repeated DeletePersonResult results = 2;
+}
+
+// PersonHogLifecycle runs person-destroying operations (delete now, merge
+// case 3 later) as durable sagas: all saga state lives in the lifecycle_op
+// tables on the persons primary, the service itself stays stateless.
+//
+// Ack semantics: the RPC drives the saga to Completed before responding —
+// an OK response means the operation's sync-plane work is committed and the
+// owning leaders have produced the death documents. On a crash or timeout
+// the caller retries with the same op_id; the retry attaches to the
+// existing operation, finishes any incomplete tail steps, and returns the
+// recorded outcome. A background sweeper backstops abandoned operations.
+//
+// Co-served on the identity server, but a separate service so callers are
+// insulated from any future split.
+service PersonHogLifecycle {
+ rpc DeletePersons(DeletePersonsRequest) returns (DeletePersonsResponse);
+}
diff --git a/rust/personhog-identity/src/lib.rs b/rust/personhog-identity/src/lib.rs
index 5f71cac5d383..a3690b4e791e 100644
--- a/rust/personhog-identity/src/lib.rs
+++ b/rust/personhog-identity/src/lib.rs
@@ -1,4 +1,5 @@
pub mod config;
pub mod leader;
+pub mod lifecycle;
pub mod service;
pub mod storage;
diff --git a/rust/personhog-identity/src/lifecycle/mod.rs b/rust/personhog-identity/src/lifecycle/mod.rs
new file mode 100644
index 000000000000..6f118d5cf216
--- /dev/null
+++ b/rust/personhog-identity/src/lifecycle/mod.rs
@@ -0,0 +1,42 @@
+//! Lifecycle sagas: person-destroying operations (delete now, merge case 3
+//! later) run as durable sagas with all state in the lifecycle_op tables on
+//! the persons primary. Self-contained on purpose — no imports from the
+//! get-or-create side; anything shared belongs in personhog-common.
+//!
+//! This module currently carries the service surface and request validation
+//! only. The saga engine (op persistence, lease, step advance, sweeper, GC)
+//! and the delete step handlers land next; until then DeletePersons validates
+//! and returns UNIMPLEMENTED.
+
+pub mod validation;
+
+use tonic::{Request, Response, Status};
+
+use personhog_proto::personhog::lifecycle::v1::person_hog_lifecycle_server::PersonHogLifecycle;
+use personhog_proto::personhog::lifecycle::v1::{DeletePersonsRequest, DeletePersonsResponse};
+
+use crate::lifecycle::validation::validate_delete_persons;
+
+#[derive(Default)]
+pub struct PersonHogLifecycleService {}
+
+impl PersonHogLifecycleService {
+ pub fn new() -> Self {
+ Self {}
+ }
+}
+
+#[tonic::async_trait]
+impl PersonHogLifecycle for PersonHogLifecycleService {
+ async fn delete_persons(
+ &self,
+ request: Request<DeletePersonsRequest>,
+ ) -> Result<Response<DeletePersonsResponse>, Status> {
+ let request = request.into_inner();
+ validate_delete_persons(&request)?;
+
+ Err(Status::unimplemented(
+ "DeletePersons is not enabled yet: the delete saga engine has not landed",
+ ))
+ }
+}
diff --git a/rust/personhog-identity/src/lifecycle/validation.rs b/rust/personhog-identity/src/lifecycle/validation.rs
new file mode 100644
index 000000000000..0f1f8bcd982e
--- /dev/null
+++ b/rust/personhog-identity/src/lifecycle/validation.rs
@@ -0,0 +1,40 @@
+use tonic::Status;
+use uuid::Uuid;
+
+use personhog_proto::personhog::lifecycle::v1::DeletePersonsRequest;
+
+/// Maximum person ids per DeletePersons request. Matches the identity
+/// get-or-create batch cap; GDPR jobs chunk above this.
+pub const MAX_DELETE_BATCH_SIZE: usize = 250;
+
+// tonic Status is a large Err variant; boxing here would diverge from the
+// tonic handler signatures these feed into.
+#[allow(clippy::result_large_err)]
+pub fn validate_delete_persons(request: &DeletePersonsRequest) -> Result<(), Status> {
+ // The persons DB stores team_id as int4 and the storage layer narrows
+ // with `as i32` — an unchecked value above i32::MAX would wrap and read
+ // or write another tenant's rows.
+ if request.team_id <= 0 || request.team_id > i32::MAX as i64 {
+ return Err(Status::invalid_argument(
+ "team_id must be a positive 32-bit integer",
+ ));
+ }
+ if request.person_ids.is_empty() {
+ return Err(Status::invalid_argument("person_ids must not be empty"));
+ }
+ if request.person_ids.len() > MAX_DELETE_BATCH_SIZE {
+ return Err(Status::invalid_argument(format!(
+ "batch size {} exceeds maximum {MAX_DELETE_BATCH_SIZE}",
+ request.person_ids.len()
+ )));
+ }
+ if request.person_ids.iter().any(|&id| id <= 0) {
+ return Err(Status::invalid_argument(
+ "person_ids must be positive integers",
+ ));
+ }
+ if Uuid::parse_str(&request.op_id).is_err() {
+ return Err(Status::invalid_argument("op_id must be a valid UUID"));
+ }
+ Ok(())
+}
diff --git a/rust/personhog-identity/src/main.rs b/rust/personhog-identity/src/main.rs
index 1e5473f13078..4996eb424385 100644
--- a/rust/personhog-identity/src/main.rs
+++ b/rust/personhog-identity/src/main.rs
@@ -9,6 +9,7 @@ use lifecycle::{ComponentOptions, Manager};
use personhog_common::grpc::{tracked_tcp_incoming, GrpcLoadShedLayer, GrpcMetricsLayer};
use personhog_common::{spawn_pool_monitor, MonitoredPool};
use personhog_proto::personhog::identity::v1::person_hog_identity_server::PersonHogIdentityServer;
+use personhog_proto::personhog::lifecycle::v1::person_hog_lifecycle_server::PersonHogLifecycleServer;
use tonic::codec::CompressionEncoding;
use tonic::transport::Server;
use tracing::level_filters::LevelFilter;
@@ -19,6 +20,7 @@ use tracing_subscriber::EnvFilter;
use personhog_common::client::RouterClient;
use personhog_identity::config::Config;
+use personhog_identity::lifecycle::PersonHogLifecycleService;
use personhog_identity::service::PersonHogIdentityService;
use personhog_identity::storage::postgres::PostgresIdentityStorage;
@@ -168,6 +170,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.expect("Invalid router URL"),
);
let service = PersonHogIdentityService::new(storage, property_writer, config.request_limits());
+ // Separate proto service co-served on the same server so lifecycle
+ // callers are insulated from any future split.
+ let lifecycle_service = PersonHogLifecycleService::new();
let grpc_addr = config.grpc_address;
let keepalive_interval = config.grpc_keepalive_interval();
@@ -216,6 +221,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.max_encoding_message_size(max_send)
.max_decoding_message_size(max_recv),
)
+ .add_service(
+ PersonHogLifecycleServer::new(lifecycle_service)
+ .accept_compressed(CompressionEncoding::Gzip)
+ .max_encoding_message_size(max_send)
+ .max_decoding_message_size(max_recv),
+ )
.serve_with_incoming_shutdown(incoming, grpc_handle.shutdown_signal())
.await
{
diff --git a/rust/personhog-identity/tests/lifecycle_service_tests.rs b/rust/personhog-identity/tests/lifecycle_service_tests.rs
new file mode 100644
index 000000000000..c0c51492d26a
--- /dev/null
+++ b/rust/personhog-identity/tests/lifecycle_service_tests.rs
@@ -0,0 +1,84 @@
+use tonic::{Code, Request};
+
+use personhog_identity::lifecycle::validation::MAX_DELETE_BATCH_SIZE;
+use personhog_identity::lifecycle::PersonHogLifecycleService;
+use personhog_proto::personhog::lifecycle::v1::person_hog_lifecycle_server::PersonHogLifecycle;
+use personhog_proto::personhog::lifecycle::v1::DeletePersonsRequest;
+
+fn valid_request() -> DeletePersonsRequest {
+ DeletePersonsRequest {
+ team_id: 1,
+ person_ids: vec![42],
+ op_id: "018f6f60-8c5a-7000-8000-000000000000".to_string(),
+ }
+}
+
+async fn delete_status(request: DeletePersonsRequest) -> Code {
+ let service = PersonHogLifecycleService::new();
+ service
+ .delete_persons(Request::new(request))
+ .await
+ .expect_err("skeleton service never returns Ok")
+ .code()
+}
+
+#[tokio::test]
+async fn invalid_requests_are_rejected_before_the_saga() {
+ let cases: Vec<(&str, DeletePersonsRequest)> = vec![
+ (
+ "zero team_id",
+ DeletePersonsRequest {
+ team_id: 0,
+ ..valid_request()
+ },
+ ),
+ (
+ "team_id above i32 wraps in int4 storage",
+ DeletePersonsRequest {
+ team_id: i64::from(i32::MAX) + 1,
+ ..valid_request()
+ },
+ ),
+ (
+ "empty person_ids",
+ DeletePersonsRequest {
+ person_ids: vec![],
+ ..valid_request()
+ },
+ ),
+ (
+ "batch above cap",
+ DeletePersonsRequest {
+ person_ids: (1..=(MAX_DELETE_BATCH_SIZE as i64 + 1)).collect(),
+ ..valid_request()
+ },
+ ),
+ (
+ "non-positive person id",
+ DeletePersonsRequest {
+ person_ids: vec![42, 0],
+ ..valid_request()
+ },
+ ),
+ (
+ "op_id not a uuid",
+ DeletePersonsRequest {
+ op_id: "not-a-uuid".to_string(),
+ ..valid_request()
+ },
+ ),
+ ];
+
+ for (name, request) in cases {
+ assert_eq!(
+ delete_status(request).await,
+ Code::InvalidArgument,
+ "case: {name}"
+ );
+ }
+}
+
+#[tokio::test]
+async fn valid_request_reports_unimplemented_until_the_engine_lands() {
+ assert_eq!(delete_status(valid_request()).await, Code::Unimplemented);
+}
diff --git a/rust/personhog-proto/build.rs b/rust/personhog-proto/build.rs
index 9e26996fb6fe..25081c24439b 100644
--- a/rust/personhog-proto/build.rs
+++ b/rust/personhog-proto/build.rs
@@ -14,6 +14,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
format!("{proto_root}/personhog/types/v1/feature_flag.proto"),
// Services
format!("{proto_root}/personhog/identity/v1/identity.proto"),
+ format!("{proto_root}/personhog/lifecycle/v1/lifecycle.proto"),
format!("{proto_root}/personhog/leader/v1/leader.proto"),
format!("{proto_root}/personhog/replica/v1/replica.proto"),
format!("{proto_root}/personhog/service/v1/service.proto"),
diff --git a/rust/personhog-proto/src/lib.rs b/rust/personhog-proto/src/lib.rs
index 2f298076106e..9331d677ac83 100644
--- a/rust/personhog-proto/src/lib.rs
+++ b/rust/personhog-proto/src/lib.rs
@@ -9,6 +9,11 @@ pub mod personhog {
tonic::include_proto!("personhog.identity.v1");
}
}
+ pub mod lifecycle {
+ pub mod v1 {
+ tonic::include_proto!("personhog.lifecycle.v1");
+ }
+ }
pub mod leader {
pub mod v1 {
tonic::include_proto!("personhog.leader.v1");