fix(python): add org_id to prompt cache key for cross-org isolation #1268
+129
−6
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.
Summary
org_idto cache key format for proper organization isolationProblem
The prompt cache was using a key format of
{project_name}:{slug}:{version}which did not include the organization ID. This caused a cross-org cache collision bug where two organizations with the same project name and prompt slug could retrieve each other's cached prompts.This was reported by HubSpot where a UI-based Python scorer returned a "legal-related prompt" instead of their SDC coverage prompt because the disk cache (
~/.braintrust/prompt_cache) returned a prompt from a different org that had been previously cached on the same scorer runtime.Root Cause
In
prompt_cache.py:17-34, the_create_cache_key()function was:When
project_nameis used (common in scorers), two different organizations with the same project name and prompt slug would have the same cache key, causing cache collisions.Solution
org_idparameter to_create_cache_key(){org_id}:{prefix}:{slug}:{version}when org_id is availableorg_idthroughPromptCache.get()andPromptCache.set()_state.org_idfromload_prompt()to cache operationsTest plan
test_handle_different_orgs_with_same_project_and_slug- verifies prompts from different orgs with same project/slug are isolatedtest_org_id_isolation_with_disk_cache- verifies isolation works after memory eviction (via disk cache)Backward Compatibility
The fix is backward compatible:
org_idin the key will simply miss and trigger a fresh API fetchorg_idparameter is optional and defaults toNone🤖 Generated with Claude Code