Skip to content

docs: clarify anonymous context key generation behavior#249

Merged
kinyoklion merged 6 commits intomainfrom
devin/1773183728-anon-context-key-docs
Mar 12, 2026
Merged

docs: clarify anonymous context key generation behavior#249
kinyoklion merged 6 commits intomainfrom
devin/1773183728-anon-context-key-docs

Conversation

@kinyoklion
Copy link
Member

@kinyoklion kinyoklion commented Mar 10, 2026

Requirements

  • I have added test coverage for new or changed functionality — Documentation only; no functional changes.
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

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() or identify(). However, the API documentation did not clearly explain this, and the kind() 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:

  1. 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).
  2. LDContextBuilder.kind() — Corrected doc to clarify that omitting the key requires .anonymous(true) for key generation; otherwise the context is invalid.
  3. LDClient.start() — Notes that anonymous contexts without keys get auto-generated keys during startup.
  4. LDClient.identify() — Notes that anonymous contexts without keys get auto-generated keys before identify.

Items for reviewer attention:

  • The previous 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

  • Considered making a behavioral change to auto-set anonymous when no key is provided (explored in earlier commits on this branch), but decided to keep this as documentation-only per reviewer feedback.

Additional context

Verification:

  • All existing unit tests pass (ld_context_test.dart: 35 pass, anonymous_context_modifier_test.dart: 6 pass)
  • dart analyze clean on all changed files
  • CI: all checks pass, Bugbot found no issues

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>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@kinyoklion kinyoklion marked this pull request as ready for review March 10, 2026 23:05
@kinyoklion kinyoklion requested a review from a team as a code owner March 10, 2026 23:05
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>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@devin-ai-integration devin-ai-integration bot changed the title docs: clarify anonymous context key generation behavior feat: auto-set anonymous for contexts without keys at build time Mar 10, 2026
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>
@kinyoklion kinyoklion changed the title feat: auto-set anonymous for contexts without keys at build time fix: auto-set anonymous for contexts without keys at build time Mar 11, 2026
@kinyoklion
Copy link
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 kind method.

- 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>
@devin-ai-integration devin-ai-integration bot changed the title fix: auto-set anonymous for contexts without keys at build time feat: auto-set anonymous for contexts without keys at build time Mar 11, 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>
@devin-ai-integration devin-ai-integration bot changed the title feat: auto-set anonymous for contexts without keys at build time docs: clarify anonymous context key generation behavior Mar 12, 2026
@kinyoklion kinyoklion merged commit 28289d0 into main Mar 12, 2026
8 checks passed
@kinyoklion kinyoklion deleted the devin/1773183728-anon-context-key-docs branch March 12, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants