Adding personhog rpc

From PostHog/posthog · View interactive version

Adding personhog rpc is an AI agent skill from the GitHub repository PostHog/posthog. Guide for adding a new RPC to personhog-replica and personhog-router. Covers eligibility checks, proto definition, code generation for Python and Node.js clients, Rust implementation (storage trait, postgres queries, service handler, router wiring), and index compatibility validation. Use when addin

How to use this skill

Skill file

Adding a personhog RPC

This skill walks through adding a new RPC end-to-end:

proto definition, code generation, Rust implementation, and client updates.

Before you start: eligibility check

Personhog serves person, distinct ID, group, group type mapping, cohort membership, and feature flag hash key override data.

If the data being accessed doesn't live in one of these tables, this RPC doesn't belong in personhog:

| Table | Data category | Routing |

| ------------------------------------ | ------------- | ------------------------------------------------------------ |

| posthog_person | PersonData | Reads: replica (eventual) or leader (strong). Writes: leader |

| posthog_persondistinctid | PersonData | Same as person |

| posthog_group | NonPersonData | All ops: replica |

| posthog_grouptypemapping | NonPersonData | All ops: replica |

| posthog_cohortpeople | NonPersonData | All ops: replica |

| posthog_featureflaghashkeyoverride | NonPersonData | All ops: replica |

| posthog_personoverride | PersonData | Reads/writes follow person routing |

| posthog_personlessdistinctid | PersonData | Same as person |

If the table is not listed above, stop — this data should not go through personhog.

Step 0: design the data access pattern

Before writing any proto, figure out the SQL query you need.

Then validate it against the available indexes — see references/database-indexes.md.

Key questions:

Step 1: define proto messages and RPC

All proto files live in proto/personhog/.

See references/proto-conventions.md for message conventions and a worked example.

Where to add what

  1. Message types → proto/personhog/types/v1/<domain>.proto (person.proto, group.proto, cohort.proto, feature_flag.proto, or common.proto)
  2. Service RPC → proto/personhog/service/v1/service.proto (the public API clients call)
  3. Replica RPC → proto/personhog/replica/v1/replica.proto (the internal API the router delegates to)
  4. Leader RPC → proto/personhog/leader/v1/leader.proto (only if this is a person-data write routed to leader)

The service and replica protos must both declare the RPC with identical signature.

The router delegates from service → replica (or leader) transparently.

Leader RPCs must be safe under at-least-once delivery

A leader-path request can be delivered more than once: clients retry UNAVAILABLE after ambiguous failures,

and the router internally replays fence- and transport-bounced requests.

Every leader RPC must therefore converge under redelivery —

read-only lookups, merges that re-apply to the same state, tombstone-style deletes, max-merge version floors —

or carry explicit operation identity so duplicates can be detected.

Note the precise contract: a convergent merge still loses to interleaving

(a replay can clobber a same-field write another caller made in between);

that residual is accepted today and closes with operation identity (see personhog-leader's README).

An RPC that neither converges nor carries identity (an unguarded increment, an append) must not be added.

If the operation you need fits neither shape, redesign it to carry an idempotency key before defining the proto.

Step 2: generate client stubs

Python

bin/generate_personhog_proto.sh

Then update three files:

Node.js

cd nodejs && pnpm run generate:personhog-proto

Then update:

matching operations class (PersonHogGroupOperations / PersonHogPersonOperations),

following the pattern of existing methods. client.ts only constructs and exposes

these operation objects; it holds no RPC wrappers itself.

Rust

No generation step needed — tonic regenerates on cargo build.

But you must implement the RPC (next step), or the build will fail.

Step 3: implement in Rust

The compiler guides you — once the proto is defined, cargo build errors tell you exactly which trait methods are missing.

3a. Storage layer (personhog-replica)

  1. Add a trait method in rust/personhog-replica/src/storage/traits/<domain>.rs
  2. Implement the query in rust/personhog-replica/src/storage/postgres/<domain>.rs
  1. Add storage tests in rust/personhog-replica/tests/storage_tests.rs

3b. Service layer (personhog-replica)

  1. Add the RPC handler in rust/personhog-replica/src/service/mod.rs
  1. Add service tests in rust/personhog-replica/tests/service_tests.rs

3c. Router wiring (personhog-router)

The router forwards request bytes without decoding them, so a new RPC needs no handler there.

  1. Add the method name to KNOWN_METHODS in rust/personhog-router/src/proxy.rs, keeping the list sorted
  1. Add the method to every PersonHogService mock that implements the generated trait, including the ones outside personhog (rust/personhog-router/tests/common/mod.rs, rust/property-defs-rs/tests/); tonic traits have no default methods

Use rstest parameterized tests where multiple variations of the same behavior are being tested.

Step 4: verify

cargo build -p personhog-proto
cargo build -p personhog-replica
cargo build -p personhog-router
cargo test -p personhog-replica
cargo test -p personhog-router

Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.