From eb4b4e4805da51913f656886cd63aa42970d7d89 Mon Sep 17 00:00:00 2001 From: Anders Asheim Hennum Date: Mon, 30 Nov 2026 11:35:13 +0100 Subject: [PATCH] test(flags): validate failed reads against contract 11.0.0 Copy the vendored feature flag rules v2 contract files from harness release 1.38.0 (contract 11.0.0). Only the $feature_flag_called context schema changed: $feature_flag_response may now be absent or null, and a config version 2 call may leave it out only when $feature_flag_error lists evaluation_failed. The exposure tests skipped the schema for failed and missing-flag reads, because the old schema required a non-null response. They now validate every $feature_flag_called through the same projection, and a new case checks that the schema still rejects a config version 2 call with no value whose error does not list evaluation_failed. --- .../feature_flag_rules_v2/SOURCE.json | 8 ++-- .../feature_flag_called_context.schema.json | 35 +++++++++++++-- posthog/test/test_experiment_exposure.py | 43 +++++++++++++++---- posthog/test/test_flags_v4_response.py | 6 +-- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/posthog/test/fixtures/feature_flag_rules_v2/SOURCE.json b/posthog/test/fixtures/feature_flag_rules_v2/SOURCE.json index 8274761b1..6c34379a9 100644 --- a/posthog/test/fixtures/feature_flag_rules_v2/SOURCE.json +++ b/posthog/test/fixtures/feature_flag_rules_v2/SOURCE.json @@ -1,10 +1,10 @@ { "description": "Copied unchanged from the released feature flag rules v2 contract in PostHog/posthog-sdk-test-harness. Do not edit these files; copy a newer release instead.", "repository": "https://github.com/PostHog/posthog-sdk-test-harness", - "release": "1.30.0", - "revision": "6414114501396d96aadf2412fdd8054a03076782", + "release": "1.38.0", + "revision": "9637117c6977265fdc3ea62bdd7c80cae7c044ca", "contract_directory": "contracts/feature_flag_rules_v2", - "contract_version": "10.0.0", + "contract_version": "11.0.0", "files": { "readers.json": { "source_path": "fixtures/wire/readers.json", @@ -24,7 +24,7 @@ }, "feature_flag_called_context.schema.json": { "source_path": "schemas/feature_flag_called_context.schema.json", - "sha256": "8a475f5ab56ba4dba76501c4a4e23f26b62c2c2f8037f23d21d0ea7650bd2cf8" + "sha256": "490b2e3226c52ab11b0415804ac00b46496037428321f0a0d47b6ee6088e3b0e" }, "experiment_exposure_properties.schema.json": { "source_path": "schemas/experiment_exposure_properties.schema.json", diff --git a/posthog/test/fixtures/feature_flag_rules_v2/feature_flag_called_context.schema.json b/posthog/test/fixtures/feature_flag_rules_v2/feature_flag_called_context.schema.json index 1829dde76..650838646 100644 --- a/posthog/test/fixtures/feature_flag_rules_v2/feature_flag_called_context.schema.json +++ b/posthog/test/fixtures/feature_flag_rules_v2/feature_flag_called_context.schema.json @@ -5,8 +5,7 @@ "type": "object", "additionalProperties": false, "required": [ - "$feature_flag", - "$feature_flag_response" + "$feature_flag" ], "properties": { "$feature_flag": { @@ -21,9 +20,15 @@ { "type": "string", "minLength": 1 + }, + { + "type": "null" } ] }, + "$feature_flag_error": { + "type": "string" + }, "$feature_flag_config_version": { "enum": [ 1, @@ -56,7 +61,7 @@ "const": true } }, - "description": "Closed projection of the flag-access context properties. Other existing capture fields are outside this projection. Complete v2 producer context follows the terminal outcome. A tolerant reader drops the whole attribution of a record that breaks its reason's rows (a rule ID without its rule type or the reverse, a rule type the reason does not allow, or a malformed split): for every reason that names a rule, a call may keep the config version and reason code and omit all rule, variant and experiment attribution, while a partly populated or mismatched tuple is rejected. A config version 2 call whose reason code is missing or not registered keeps the raw code, if any, and carries no attribution; a registered code only matches its own reason's branches. A config version 2 call never carries $feature_flag_reason, the reason description that only version 1 calls carry.", + "description": "Closed projection of the flag-access context properties. Other existing capture fields are outside this projection. Complete v2 producer context follows the terminal outcome. A tolerant reader drops the whole attribution of a record that breaks its reason's rows (a rule ID without its rule type or the reverse, a rule type the reason does not allow, or a malformed split): for every reason that names a rule, a call may keep the config version and reason code and omit all rule, variant and experiment attribution, while a partly populated or mismatched tuple is rejected. A config version 2 call whose reason code is missing or not registered keeps the raw code, if any, and carries no attribution; a registered code only matches its own reason's branches. A config version 2 call never carries $feature_flag_reason, the reason description that only version 1 calls carry. $feature_flag_response may be absent or null when the read has no value. A call without config version 2 may do so, because a missing flag carries no config version. A config version 2 call may do so only when $feature_flag_error lists evaluation_failed: a failed record with no successful fallback.", "allOf": [ { "if": { @@ -84,6 +89,30 @@ } ] }, + "anyOf": [ + { + "required": [ + "$feature_flag_response" + ], + "properties": { + "$feature_flag_response": { + "not": { + "type": "null" + } + } + } + }, + { + "required": [ + "$feature_flag_error" + ], + "properties": { + "$feature_flag_error": { + "pattern": "(^|,)evaluation_failed(,|$)" + } + } + } + ], "oneOf": [ { "properties": { diff --git a/posthog/test/test_experiment_exposure.py b/posthog/test/test_experiment_exposure.py index 6148aa460..20b54ce77 100644 --- a/posthog/test/test_experiment_exposure.py +++ b/posthog/test/test_experiment_exposure.py @@ -322,10 +322,7 @@ def _check_reader_events(case: Dict[str, Any], events: List[_Event]) -> None: assert called.properties.get("$feature_flag_config_version") == ( 2 if config_version == 2 else None ), key - if called.properties["$feature_flag_response"] is not None: - FLAG_CALLED_SCHEMA.validate( - _projection(FLAG_CALLED_SCHEMA, called.properties) - ) + FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) for exposure in exposures: EXPOSURE_SCHEMA.validate(_projection(EXPOSURE_SCHEMA, exposure.properties)) assert not set(EXPOSURE_LEAVES_OUT) & set(exposure.properties), key @@ -524,14 +521,12 @@ def test_flag_called_attribution_follows_the_record(source, expected, path): [called] = _named(events, FLAG_CALLED, "garden-layout") assert _attribution(called.properties) == expected if failed: - # The vendored schema rejects the null $feature_flag_response a failed record reports. assert called.properties["$feature_flag_response"] is None assert set(called.properties["$feature_flag_error"].split(",")) == { "evaluation_failed", "errors_while_computing_flags", } - else: - FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) + FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) exposures = _named(events, EXPOSURE, "garden-layout") assert len(exposures) == (1 if "$feature_flag_variant_key" in expected else 0) @@ -539,6 +534,7 @@ def test_flag_called_attribution_follows_the_record(source, expected, path): # A record whose reason names no rule, or that failed, reports no rule even when it # carries one. Neither sends an exposure. The last element says whether a successful # record is a valid server outcome, which the contract's event schema then accepts. +# The schema accepts the call of every failed record. STRAY_RULE_CASES = [ ("no_rule_match", {}, True), ("missing_group_key", {}, True), @@ -579,10 +575,11 @@ def test_rule_fields_are_sent_only_for_reasons_that_name_a_rule( "$feature_flag_reason_code": code, } assert _named(events, EXPOSURE) == [] - if fields.get("failed") is True: + failed = fields.get("failed") is True + if failed: assert called.properties["$feature_flag_response"] is None assert called.properties["$feature_flag_error"] == "evaluation_failed" - elif valid_outcome: + if failed or valid_outcome: FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) @@ -603,6 +600,33 @@ async def read(client: Any) -> None: "$feature_flag_reason_code": "experiment_split", } assert _named(events, EXPOSURE) == [] + FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) + + +@pytest.mark.parametrize("response", ["null", "absent"]) +@pytest.mark.parametrize( + "error", + [None, "flag_missing", "errors_while_computing_flags"], + ids=["no_error", "flag_missing", "errors_while_computing_flags"], +) +def test_a_config_version_2_call_without_a_value_must_list_evaluation_failed( + error, response +): + # The failed read is valid only because its error lists evaluation_failed. + record = _record("error", None, failed=True) + events = _read_events("get_feature_flag_result", _response(record)) + [called] = _named(events, FLAG_CALLED) + properties = _projection(FLAG_CALLED_SCHEMA, called.properties) + FLAG_CALLED_SCHEMA.validate(properties) + + if response == "absent": + del properties["$feature_flag_response"] + if error is None: + del properties["$feature_flag_error"] + else: + properties["$feature_flag_error"] = error + with pytest.raises(jsonschema.ValidationError): + FLAG_CALLED_SCHEMA.validate(properties) @pytest.mark.parametrize("path", ["get_feature_flag_result", "snapshot.get_flag"]) @@ -752,6 +776,7 @@ def test_a_config_version_2_call_reports_its_reason_by_code_only(record, path): [called] = _named(events, FLAG_CALLED) assert called.properties["$feature_flag_reason_code"] == record["reason"]["code"] assert "$feature_flag_reason" not in called.properties + FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties)) @pytest.mark.parametrize("path", list(SYNC_READS)) diff --git a/posthog/test/test_flags_v4_response.py b/posthog/test/test_flags_v4_response.py index ee5c367bf..bf5520225 100644 --- a/posthog/test/test_flags_v4_response.py +++ b/posthog/test/test_flags_v4_response.py @@ -83,9 +83,9 @@ def _evaluate(client: Client, response: Dict[str, Any]) -> FeatureFlagEvaluation def test_vendored_fixtures_match_the_recorded_release_digests(): source = _load("SOURCE.json") - assert source["release"] == "1.30.0" - assert source["revision"] == "6414114501396d96aadf2412fdd8054a03076782" - assert source["contract_version"] == "10.0.0" + assert source["release"] == "1.38.0" + assert source["revision"] == "9637117c6977265fdc3ea62bdd7c80cae7c044ca" + assert source["contract_version"] == "11.0.0" for name, entry in source["files"].items(): digest = hashlib.sha256((FIXTURES / name).read_bytes()).hexdigest() assert digest == entry["sha256"], name