Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4d539ca
test(context): pin shared variable key winner and its single exposure
Nov 23, 2026
b67d637
fix: only the winning experiment is exposed for a shared variable key
Nov 23, 2026
867c4aa
test(context): pin that a traffic-excluded or audience-mismatched can…
Nov 23, 2026
b14ad93
test(context): pin that checking a shared-key candidate leaves it une…
Nov 24, 2026
cd40dd9
docs(changelog): file released notes under 1.0.0; test winner after a…
Nov 24, 2026
5d15893
test(context): pin a rule-won winner exposed once and a custom-assign…
Nov 24, 2026
6223db2
test(context): pin a custom-assigned shared-key winner through peek
Nov 24, 2026
f07687a
docs(readme): peek_variable_value also returns the default for a winn…
Nov 24, 2026
abc764c
docs: say peek also records no exposure and that the old JS-shared ru…
Nov 24, 2026
c45201b
test(context): pin that a custom assignment alone does not make an un…
Nov 24, 2026
3b1915e
test(context): pin that a logger refreshing during a shared-key expos…
Nov 24, 2026
8fc3e51
fix(context): keep shared-key reads stable across callbacks and refre…
Nov 26, 2026
63a3780
fix(context): rebind unloaded overrides once; pin the value snapshot …
Nov 26, 2026
94a5440
test(context): pin the id-0 overridden winner; drop restating comment…
Nov 26, 2026
8e7d29c
test(context): use the direct refresh overload in the winner-removal …
Nov 26, 2026
d033110
fix(context): bind pre-load overrides on refresh without an id sentin…
Nov 27, 2026
dd5b15c
fix(context): keep an override bound to unchanged returning experimen…
Nov 27, 2026
6e54bb9
test(context): pin the one-past-last override variant across leave an…
Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@

## Unreleased

### Fixed

- **Shared variable keys exposed experiments the user never saw.** When two or
more experiments define the same variable key, `variable_value()` queued an
exposure for every candidate it visited on the way to the winner, including
experiments the unit was not in (a strict audience mismatch, no unit for the
experiment's unit type) and experiments that ended on the caller default.
Those users were recorded in experiments that never affected them. Both
`variable_value()` and `peek_variable_value()` could also return a value from
a later experiment when the winner's assigned arm did not define the key, so
the arm a user got decided which experiment they were counted in. This
affects anyone with two or more running experiments that share a variable
key. The rule now applied, matching the Java SDK: the candidates are the
experiments whose variants define the key, by experiment id ascending; the
winner is the first one the unit is assigned to (including a unit outside the
traffic split, which gets the control value), overridden into or
rule-assigned to; if the winner's variant does not define the key the caller
default is returned and no later experiment is tried; if no candidate
qualifies the default is returned. Only the winner is exposed, and
`peek_variable_value()` exposes nothing. This replaces the earlier rule
(shared with the JavaScript SDK) of exposing every candidate checked and
falling through to a later experiment when the key was missing.

This also applies when only one experiment defines the key: a unit that is
not in it (a strict audience mismatch, or no unit for its unit type) gets the
default and is no longer exposed by `variable_value()`, so exposure counts
for such experiments can drop. `treatment()` still exposes it. An override to
a variant the experiment does not have now makes that experiment the winner
with the default value and an exposure, instead of falling through to a
later experiment's value. An override set before its experiment is loaded
now binds to that experiment once a refresh brings it in, so the value and
the exposure (id, unit type) come from the loaded experiment, whatever its
id; if it was already exposed before the refresh, it is exposed once more
with that data. An override already bound to an experiment that leaves and
returns unchanged is not exposed again. `variable_value()` and `treatment()`
return the value and variant they exposed even when an exposure event
handler changes overrides or refreshes the context.

## 1.0.0 (2026-11-16)

### Added

- Ordered assignment rules (`ExperimentData::assignmentRules`). The first
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ nlohmann::json show_banner = context.variable_value("show_banner", false);
nlohmann::json banner_height = context.variable_value("banner_height", 200);
```

#### Shared variable keys

When several experiments define the same variable key, the experiments whose variants define it are checked in ascending experiment id order. The first one the user is in (assigned, overridden or rule-assigned) wins, and only that experiment is exposed. A user who is assigned but outside the traffic split still counts: they get the experiment's control (variant 0) value and are exposed to it. If the winner's assigned variant does not define the key, `variable_value()` and `peek_variable_value()` return your default rather than trying a later experiment. If the user is in none of them, both return your default and no exposure is recorded.

### Peek at Treatment Variants

Although generally not recommended, it is sometimes necessary to peek at a treatment or variable without triggering an exposure. The A/B Smartly SDK provides `peek()` and `peek_variable_value()` methods for that.
Expand Down
2 changes: 2 additions & 0 deletions include/absmartly/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <memory>
#include <optional>
#include <utility>
#include <future>

namespace absmartly {
Expand Down Expand Up @@ -132,6 +133,7 @@ class Context {
friend struct ContextTestAccess;

Assignment& get_or_create_assignment(const std::string& experiment_name);
std::pair<const std::string*, Assignment*> variable_assignment(const std::string& key);
void queue_exposure(const std::string& experiment_name, const Assignment& assignment);

void build_index();
Expand Down
88 changes: 59 additions & 29 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ int Context::treatment(const std::string& experiment_name) {
check_ready(true);

auto& assignment = get_or_create_assignment(experiment_name);
// Read before the exposure logger runs: it may refresh or override, replacing this entry.
int variant = assignment.variant;

if (!assignment.exposed) {
assignment.exposed = true;
queue_exposure(experiment_name, assignment);
}

return assignment.variant;
return variant;
}

int Context::peek(const std::string& experiment_name) {
Expand All @@ -293,54 +295,58 @@ int Context::peek(const std::string& experiment_name) {
return assignment.variant;
}

nlohmann::json Context::variable_value(const std::string& key, const nlohmann::json& default_value) {
check_ready(true);

// The candidates for a key are the experiments whose variants define it, by id ascending. The
// first one the unit is in (assigned, overridden or rule-assigned) wins, and only it is exposed:
// a candidate that was checked but lost was never seen by the user. If the winner's variant does
// not define the key the caller default applies; later candidates are not tried, or the arm the
// unit got would decide which experiment it is counted in. Matches the Java SDK's
// Context.getVariableAssignment.
std::pair<const std::string*, Assignment*> Context::variable_assignment(const std::string& key) {
auto var_it = index_variables_.find(key);
if (var_it == index_variables_.end()) {
return default_value;
return {nullptr, nullptr};
}

for (const auto* experiment_index : var_it->second) {
const std::string& experiment_name = experiment_index->data->name;
auto& assignment = get_or_create_assignment(experiment_name);

if (assignment.has_variables) {
if (!assignment.exposed) {
assignment.exposed = true;
queue_exposure(experiment_name, assignment);
}

if ((assignment.assigned || assignment.overridden || assignment.rule_override) &&
assignment.variables.count(key) > 0) {
return assignment.variables.at(key);
}
if (assignment.assigned || assignment.overridden || assignment.rule_override) {
Comment thread
joalves marked this conversation as resolved.
Resolved
return {&experiment_name, &assignment};
}
}

return default_value;
return {nullptr, nullptr};
}

nlohmann::json Context::peek_variable_value(const std::string& key, const nlohmann::json& default_value) {
nlohmann::json Context::variable_value(const std::string& key, const nlohmann::json& default_value) {
check_ready(true);

auto var_it = index_variables_.find(key);
if (var_it == index_variables_.end()) {
auto [experiment_name, assignment] = variable_assignment(key);
if (assignment == nullptr) {
return default_value;
}

for (const auto* experiment_index : var_it->second) {
const std::string& experiment_name = experiment_index->data->name;
auto& assignment = get_or_create_assignment(experiment_name);
auto value_it = assignment->variables.find(key);
nlohmann::json value = value_it != assignment->variables.end() ? value_it->second : default_value;

if (assignment.has_variables &&
(assignment.assigned || assignment.overridden || assignment.rule_override) &&
assignment.variables.count(key) > 0) {
return assignment.variables.at(key);
}
if (!assignment->exposed) {
assignment->exposed = true;
queue_exposure(*experiment_name, *assignment);
}

return default_value;
return value;
}

nlohmann::json Context::peek_variable_value(const std::string& key, const nlohmann::json& default_value) {
check_ready(true);

const auto* assignment = variable_assignment(key).second;
if (assignment == nullptr) {
return default_value;
}

auto value_it = assignment->variables.find(key);
return value_it != assignment->variables.end() ? value_it->second : default_value;
}

std::map<std::string, std::vector<std::string>> Context::variable_keys() {
Expand Down Expand Up @@ -588,8 +594,32 @@ void Context::refresh(const ContextData& new_data) {
// Cached assignments are kept (sticky): get_or_create_assignment() decides
// per experiment whether a cached one is still valid against the new data.
// An override stays valid while its variant is unchanged, as in JS.
// An override cached while its experiment was not loaded holds no experiment data, so when
// the experiment arrives it is dropped and the next read binds it to that data. One already
// bound to the same experiment data is kept, so it is not exposed again.
std::vector<std::string> unloaded_overrides;
for (const auto& [name, assignment] : assignments_) {
if (assignment.overridden && index_.count(name) == 0) {
unloaded_overrides.push_back(name);
}
}

data_ = new_data;
build_index();
for (const auto& name : unloaded_overrides) {
auto exp_it = index_.find(name);
if (exp_it == index_.end()) {
continue;
}
const auto& existing = assignments_[name];
const auto& exp_data = *exp_it->second.data;
bool variant_has_variables = existing.variant >= 0 &&
static_cast<size_t>(existing.variant) < exp_it->second.variables.size();
if (existing.id != exp_data.id || existing.unit_type != exp_data.unitType ||
existing.iteration != exp_data.iteration || existing.has_variables != variant_has_variables) {
assignments_.erase(name);
}
}
hashes_.clear();
assigners_.clear();

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_executable(absmartly_tests
system_attributes_test.cpp
assignment_rules_test.cpp
http_integration_test.cpp
shared_variable_key_test.cpp
)

target_link_libraries(absmartly_tests PRIVATE absmartly-sdk Catch2::Catch2WithMain)
Expand Down
Loading
Loading