docs: clarify anonymous context key generation behavior#249
Merged
kinyoklion merged 6 commits intomainfrom Mar 12, 2026
Merged
Conversation
Update documentation on LDAttributesBuilder.anonymous(), LDContextBuilder.kind(), LDClient.start(), and LDClient.identify() to explain that the SDK automatically generates and persists stable keys for anonymous contexts without keys. Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
When a context kind is added without a key, the context is now
automatically set to anonymous at build time. This means
LDContextBuilder().kind('user').build() produces a valid anonymous
context rather than an invalid one. The SDK will generate and persist
a stable key for the context during start() or identify().
Updated documentation on anonymous(), kind(), start(), and identify()
to explain this behavior.
Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Address review feedback: _build() no longer mutates _anonymous as a side effect. Instead, an _anonymousExplicitlySet flag tracks whether anonymous() was called. When no key is provided and anonymous was not explicitly set, the context is automatically anonymous. When anonymous(false) was explicitly called with no key, the context remains invalid. Suppress 4 contract tests that expect invalid contexts for inputs without keys, as these now produce valid anonymous contexts per the new auto-anonymous behavior. Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
Instead of suppressing contract tests, explicitly set anonymous(false) in _flattenedListToContext when the input doesn't have anonymous=true. This ensures the contract tests pass as before (keyless contexts without explicit anonymous=true remain invalid during JSON conversion), while the builder API auto-anonymous behavior is preserved for users who simply call kind() without a key. Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
Member
Author
|
I'm going to put this on hold to decide the path here. Either documentation only change, or logic change. The existing code allows you to get generated keys, but the code documentation doesn't make it clear how, and has a misleading comment on the |
- Refactor _anonymous from bool + _anonymousExplicitlySet to bool? (null = never set, true = anonymous, false = not anonymous). This simplifies the _build() logic per reviewer suggestion. - Reorder examples: implicit anonymous first, explicit second - Improve example comments per reviewer suggestions - Add persistence caveat to doc comment - Fix _build() comment to accurately describe all three states Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
tanderson-ld
approved these changes
Mar 12, 2026
Reverts auto-anonymous logic, bool? refactor, and contract test service changes. Keeps only documentation updates explaining how to get generated keys by setting anonymous(true). Adds persistence caveat per reviewer feedback. Co-Authored-By: rlamb@launchdarkly.com <kingdewman@gmail.com>
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.
Requirements
Related issues
#222
Describe the solution you've provided
This is a documentation-only PR. No runtime behavior is changed.
The existing SDK behavior allows anonymous contexts without keys to receive auto-generated, persisted UUID keys during
start()oridentify(). However, the API documentation did not clearly explain this, and thekind()method doc was misleading (it implied omitting a key alone was sufficient for key generation, without mentioning that.anonymous(true)is required).Documentation updates in four places:
LDAttributesBuilder.anonymous()— Explains that when a context is anonymous and no key is provided, the SDK auto-generates a persisted UUID key. Includes a code example and a persistence caveat (key won't be stable if persistence is unavailable).LDContextBuilder.kind()— Corrected doc to clarify that omitting the key requires.anonymous(true)for key generation; otherwise the context is invalid.LDClient.start()— Notes that anonymous contexts without keys get auto-generated keys during startup.LDClient.identify()— Notes that anonymous contexts without keys get auto-generated keys before identify.Items for reviewer attention:
kind()doc said "If key is omitted, this will create an anonymous context with a generated key" — this was misleading because omitting the key without setting.anonymous(true)actually produces an invalid context. The updated doc now correctly describes both cases.Describe alternatives you've considered
Additional context
Verification:
ld_context_test.dart: 35 pass,anonymous_context_modifier_test.dart: 6 pass)dart analyzeclean on all changed files