fix: drop alias() calls with a missing identity (sdk-specs alias) - #831
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix: drop alias() calls with a missing identity (sdk-specs alias)#831posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
The sdk-specs `alias` contract requires both identities to be present, and its @both scenario "Alias is dropped when required identities are missing" says no event should be enqueued and a validation warning should be recorded. posthog-python did neither for `previous_id`: `alias(None, "user-123")` (or an empty string) enqueued a `$create_alias` event with a null/empty `distinct_id`, which links nothing. The missing-alias-target case already dropped the call but logged nothing. Both cases now log a warning and return None. `previous_id` is also stringified once so `distinct_id` and `properties.distinct_id` agree for non-string ids. Generated-By: PostHog Code Task-Id: 0f22ca69-60f9-4887-908d-efef6c63b603
Contributor
posthog-python Compliance ReportDate: 2026-08-05 07:57:08 UTC ✅ All Tests Passed!111/111 tests passed Capture_V1 Tests✅ 94/94 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
The sdk-specs
aliascontract requires both identities to be present before an alias event is emitted:openspec/specs/alias/spec.md— Behavior → Server-side flow, step 1: "Validate inputs. Both the existing id and the alias target id must be present."acceptance/public/alias.feature)Client.alias()did neither forprevious_id— it was passed straight through with no check:An alias event with a null source id cannot link anything, so this is a silently unusable event rather than a caught mistake. The same happened for
previous_id="".The other half of the validation — a missing alias target — was already dropped (
get_identity_statereportspersonlesswhen neither an explicit nor a context distinct id is available), but it returnedNonewith no diagnostic at all, so the caller had no way to tell a dropped alias from a delivered one.Change
previous_idis stringified and checked; empty/missing drops the call, logs a warning, and returnsNone.previous_idis stringified once and reused, sodistinct_idandproperties.distinct_idagree for non-string ids (alias(0, ...)previously produced"0"at the top level and0in properties —_enqueueonly stringifies the top-level field).Signatures and the public API snapshot are unchanged;
previous_idstays annotatedstrsince passingNonewas never a supported call shape.Callers who pass a missing or empty
previous_idstop getting a$create_aliasevent, andalias()returnsNoneinstead of a UUID for those calls. The events this removes were already inert — a$create_aliaswith a nulldistinct_idlinks no identities — so no working alias is affected. Callers checking the return value for success will now correctly see the failure, and will see a warning in the logs explaining it. Valid calls, including non-string ids such asalias(0, "user-123"), are unchanged.💚 How did you test it?
Added four tests in
posthog/test/test_client.py:previous_id=Noneandprevious_id=""are dropped with a warning and nothing posted,previous_id=0still sends and is stringified consistently, and a missingdistinct_idis dropped with a warning. Confirmed the pre-change behavior first by running the SDK locally against a mockedbatch_post.Ran
pytest posthog/test/test_client.py posthog/test/test_module.py posthog/test/test_contexts.py(190 passed),ruff format --check .,ruff check .,mypy ... | mypy-baseline filter(clean),check_public_api.py(snapshot up to date), andpython -W error -c "import posthog". No manual or integration testing beyond that.📝 Checklist
alias()docstring documents the drop behavior.If releasing new changes
sampo addto generate a changeset file — hand-written at.sampo/changesets/alias-validates-identities.md(thesampoCLI isn't available in this environment); please sanity-check the format.🤖 Agent context
Autonomy: Fully autonomous
Opened by the SDK Spec Compliance Enforcer loop, which audits posthog-python against the contracts in PostHog/sdk-specs. This run re-checked the spec capabilities whose
Applicabilityisbothorserver, using parallel read-only sub-agents (Claude Codegeneral-purposeagents with Read/Grep/Bash) and confirming each candidate by exercising the SDK locally.This finding was chosen because the spec scenario is explicit and tagged
@both, the current behavior produces an event that provably cannot do its job, and the fix is contained to one method. Runners-up left for humans, all from the same audit:group_identify()has the identical missing-validation gap against its own@both"Group identify requires type and key" scenario (a natural follow-up, kept out of this PR to keep it focused);capture()does not reject an empty event name, which the capture spec's Behavior/Error-handling prose requires but no scenario covers; and the capture spec asks for UUIDv7 event ids where the SDK generates UUIDv4, which is a larger change needing a product call. Note the previously closed #794 (wontfix) is unrelated to this change.Agent-authored — requires human review; not self-merged.
Created with PostHog Code