Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8a3501b
feat(context): holdouts, matching javascript-sdk
Nov 17, 2026
9c6cdaa
docs: document holdouts; version 1.1.0
Nov 17, 2026
d3a9ff3
fix(models): read holdout iteration and seeds without float-to-int UB…
Nov 17, 2026
d7acb7d
fix(models): read holdout seeds as JS ToInt32; pin the exact 3-arm check
Nov 17, 2026
23c5fe2
fix(models): keep public headers -Wfloat-equal clean; JS seed parity …
Nov 17, 2026
400f52f
fix(context): survive a refresh from a variable_value exposure; re-re…
Nov 18, 2026
a5ce4ea
docs(readme): holdout variable fallback and setting the unit before e…
Nov 18, 2026
1edb544
fix(context): keep uncovered overrides across a unit type change; bou…
Nov 18, 2026
ae04117
fix(context): retain the variable candidate walk across a logger refr…
Nov 18, 2026
11369c4
test(holdouts): restore reused-model coverage and pin lazy variable c…
Nov 18, 2026
23073a1
fix(context): queue every exposure of a call before a logger can fina…
Nov 18, 2026
95f23f2
fix(models): replace holdout coverage on every ExperimentData decode
Nov 18, 2026
f0472b6
fix(context): keep JS's variable_value walk after a logger finalizes;…
Nov 18, 2026
3f4b14b
chore: trim holdout comments that restate the code
Nov 18, 2026
393fe60
fix(context): return the exposed decision across reentrant refreshes;…
Nov 20, 2026
de6fc96
fix(context): keep the data when a logger refreshes with data() durin…
Nov 20, 2026
c34f727
test(holdouts): pin that a rule-won held-out candidate does not decid…
Nov 20, 2026
702624d
Merge remote-tracking branch 'origin/main' into feat/holdouts
Nov 20, 2026
47ac7a6
perf(context): activate walk retention only before a callback and reu…
Nov 20, 2026
9d76dba
fix(context): retain only walked data generations; report parse error…
Nov 20, 2026
decb4af
fix(models): preserve released ContextData structured bindings and pi…
Nov 20, 2026
3215513
perf(context): reuse live variable index entries without redundant na…
Nov 20, 2026
9bd2f72
test(holdouts): pin that variable reads reuse each candidate's index …
Nov 20, 2026
069236f
test(holdouts): widen the lookup counter and pin that it counts
Nov 20, 2026
55aee96
test(holdouts): scope the absolute first-treatment allocation count t…
Nov 20, 2026
7160898
test: let ctest report Catch2 skips as skipped
Nov 20, 2026
a025af1
test: drop the Catch2 skip instead of mapping its exit code
Nov 20, 2026
5545e99
test(holdouts): assert the absolute allocation count on the measured …
Nov 20, 2026
1fa778e
fix(models): preserve released ExperimentData structured bindings
Nov 21, 2026
4bc4c17
test(holdouts): pin every ExperimentData structured-binding position
Nov 21, 2026
c478745
fix(context): install a refresh only after its payload is copied and …
Nov 21, 2026
cf58fa9
test(holdouts): extend the failed-refresh injection into the first in…
Nov 21, 2026
a27f5d7
test(holdouts): name the failed-refresh test for both phases and reco…
Nov 21, 2026
ea5cafa
Merge origin/main (1.0.1) into feat/holdouts
Nov 30, 2026
a8ac27f
perf(context): keep cached treatment()/peek() at 1.0.1 cost
Nov 30, 2026
5d3ef36
test(holdouts): pin that a no-winner variable read logs the visited h…
Nov 30, 2026
f84af87
fix(holdouts): address the independent review of 5d3ef36
Nov 30, 2026
48d819e
test(holdouts): pin a grown holdout set and the uncovered variable fa…
Nov 30, 2026
c7edb06
perf(context): skip the holdout comparison for an assignment validate…
Nov 30, 2026
a9a3248
feat(client): send a configured X-Application-Version; pin the review…
Dec 1, 2026
9458a29
test: pin the review's application-version and binding test gaps; run…
Dec 1, 2026
df94ae7
test(client): pin that a const rvalue ClientConfig binding aliases it…
Dec 1, 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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,27 @@ jobs:
test "$installed" = "./bin/app"
grep -q 'set(CPACK_PACKAGE_NAME "embedding-app")' "$dir/CPackConfig.cmake"
grep -q 'set(CPACK_GENERATOR "TGZ")' "$dir/CPackConfig.cmake"

sanitizers:
name: Test with ASan and UBSan
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake g++ libcurl4-openssl-dev

- name: Configure
run: >-
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DABSMARTLY_BUILD_TESTS=ON
-DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer"

- name: Build
run: cmake --build build --parallel --target absmartly_tests

- name: Test
run: build/tests/absmartly_tests
73 changes: 73 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
# Changelog

## 1.1.0 (unreleased)

### Added

- Holdouts (`ContextData::holdouts`, `HoldoutData`, `ExperimentData::holdoutIds`
and `ExperimentData::holdoutType`, all new fields appended last and empty by
default). An experiment covered by a holdout that holds the unit out
(arm 0, or arm 1 of a three-arm holdout for an experiment that is not
full-on) returns variant 0 with control variables and publishes no exposure
of its own; each covering holdout publishes one ordinary exposure per unit
type. Overrides and matching assignment rules win over a holdout. Payloads
without holdouts assign and publish exactly as before. A holdout arm is
chosen with javascript-sdk's probability scale, so a unit whose hash lands
exactly on a split boundary gets the same arm in every SDK. Ports
javascript-sdk#65 and java-sdk#12.
- Shared variable keys with holdouts, as in java-sdk: a held-out candidate
never wins a key over a candidate the unit is in; when no candidate wins, the
first held-out candidate supplies its control value (or the caller default
if its variant lacks the key). `variable_value()` publishes the holdout
exposures of every candidate it checks, and the winner's own exposure only
when the winner is not held out. `peek_variable_value()` publishes nothing.
- `contains` operator, an alias of `in`, so the app can start sending
`contains`. The C++ evaluator has registered `contains` as the same
operator as `in` (haystack first, then needle) since 1.0.0; 1.1.0 documents
it and tests every `in` case through both names. `in` is unchanged.
- `ClientConfig::application_version`: the application version sent as
`X-Application-Version`. A whole number of 1 to 18 digits is sent as-is;
anything else (such as `2.4.1`) is sent as `0`, as in java-sdk and
javascript-sdk. `ClientConfig` keeps its 1.0.0 four-element
structured-binding shape.

### Fixed

- **`X-Application-Version` was always `0`.** Every 1.0.x client sent
`X-Application-Version: 0` on context fetches and publishes, and there was no
option to set it, so the collector could not attribute events to an
application version. This affects every C++ SDK user who reports on or
targets application versions; set `ClientConfig::application_version` to
send yours. Clients that leave it empty still send `0`.

### Notes

- New fields grow the public model and `Context` layouts; rebuild everything
that includes the SDK headers when upgrading (see Installed Package in the
README). `ContextData` and `ExperimentData` keep their 1.0.0
structured-binding shapes (1 and 19 elements); reach `holdouts`,
`holdoutIds` and `holdoutType` by name. A type derived from either one has
no such shape: a structured binding of it now binds every member (2 and 21
names). The same holds for `ClientConfig` (4 elements; `application_version`
by name). Code that aggregate-initializes `ExperimentData`, `ContextData` or
`ClientConfig` positionally still compiles, but `-Wextra` reports the
appended fields as missing initializers.

- Cached reads cost the same as in 1.0.1 or less. Measured with GCC 15.2 on
aarch64 with 50 experiments and no holdouts, other paths are slower: the
first resolution of each experiment by about 2-3% (first `peek()` 295 to
304 ns, first `treatment()` 2,168 to 2,184 ns, with the same allocation
count), partly from the 24-byte larger cached assignment and partly from
the holdout checks; and `refresh()` by about 12% (112 to 126 us, 151 more
allocations), because it copies the new payload into fresh storage before
installing it (see below).

- `refresh()` copies and indexes the new payload before installing it, so a
`std::bad_alloc` while doing so leaves the previous data installed and
readable. As in 1.0.1, an allocation failure inside a nlohmann/json
destructor during that copy still terminates the process.

- All exposures of one `treatment()` call on a covered experiment, and of one
`variable_value()` call (its holdouts' and the winner's), are queued before
the event logger sees the first of them, so a logger that calls `finalize()`
or `publish()` from that callback sends them all, as javascript-sdk 1.14
does.

## 1.0.1 (2026-11-27)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(absmartly-sdk VERSION 1.0.1 LANGUAGES CXX)
project(absmartly-sdk VERSION 1.1.0 LANGUAGES CXX)

include(FetchContent)
include(GNUInstallDirs)
Expand Down
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ find_package(absmartly-sdk 1.0 REQUIRED)
target_link_libraries(your_target PRIVATE absmartly::absmartly-sdk)
```

The package's version check (`SameMajorVersion`) selects a package with the
same major version; it is not a binary-compatibility guarantee. Public types
such as `ContextData`, `ExperimentData` and `Context` gain fields in 1.1.0.
Existing positional initializers remain valid, and `ContextData` and
`ExperimentData` keep their 1.0.0 structured-binding shapes (`auto&
[experiments] = data`, and the 19 released `ExperimentData` members); access
the new holdout fields by name. The binary layout changes, so rebuild
your application and every library that includes the SDK headers against the
SDK version you link. Do not mix objects compiled against different SDK versions.

## Getting Started

Please follow the [installation](#installation) instructions before trying the following code.
Expand All @@ -85,6 +95,7 @@ int main() {
client_config.api_key = "YOUR_API_KEY";
client_config.application = "website";
client_config.environment = "production";
client_config.application_version = "100204"; // optional, sent as X-Application-Version
auto sdk = absmartly::SDK::create_simple(client_config);

absmartly::ContextConfig ctx_config;
Expand All @@ -97,6 +108,10 @@ int main() {
}
```

`application_version` is sent as `X-Application-Version` with every request.
The collector reads it as a whole number, so only 1 to 18 digits are sent
as-is; anything else, such as `"2.4.1"`, or no version, is sent as `0`.

#### Alternative: Using Configuration Objects

For use cases with custom HTTP clients or providers:
Expand Down Expand Up @@ -426,6 +441,59 @@ later rules are not tried. Rules that are malformed in any other way are
skipped. Rule-assigned exposures are flagged `ruleOverride` so the backend can
exclude them from analysis.

### Holdouts

A holdout keeps a share of your units out of a group of experiments, so you can
measure the combined effect of everything the group ships. Holdouts are set up
in the Web Console and arrive with the context data
(`ContextData::holdouts`); each experiment lists the holdouts covering it in
`ExperimentData::holdoutIds`. There is nothing to call: `treatment()`,
`peek()`, `variable_value()` and `peek_variable_value()` apply them.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

- The unit is randomised into one of the holdout's arms with the covered
experiment's unit type. Arm 0 holds the unit out. A holdout with three arms
(a three-way split) also holds the unit out at arm 1, but only of covered
experiments that are not full-on; full-on experiments keep their full-on
variant there. Any other arm leaves the experiment to its normal assignment.
- A unit held out by any one of an experiment's holdouts gets variant 0, and
the experiment publishes no exposure of its own. A holdout beats audience
targeting, full-on and custom assignments. `set_override()` and a matching
assignment rule still win over it.
- For a variable key (see Shared variable keys), a held-out experiment never
wins over one the unit is assigned to, overridden or rule-assigned in. When
no candidate wins, the first held-out candidate supplies its control value,
or your default if its control variant lacks the key.
- The first `treatment()` on a covered experiment also publishes one exposure
for each covering holdout (its id, name and arm), once per holdout and unit
type. `variable_value()` does so for every candidate it checks for the key,
as the Java SDK does, while still exposing only the winning experiment.
`peek()` and `peek_variable_value()` publish nothing.
- The arm is kept until the holdout's iteration changes; other edits to the
holdout do not re-randomise the unit.
- Set the unit of each covered experiment's unit type before the first
`treatment()` or `variable_value()` on it. Without the unit the holdout
cannot be evaluated, and an experiment already exposed that way keeps its
decision (so it never publishes a second, conflicting exposure). A `peek()`
or `peek_variable_value()` before the unit is set is fine: the decision is
made again once the unit arrives.

Holdout ids are integers on the wire. A holdout id sent as a string (`"11"`)
is ignored here, whereas javascript-sdk coerces it; the Collector never sends
one. Likewise a holdout without a `name` publishes its exposure with an empty
name, where javascript-sdk leaves the name out.

Payloads without holdouts behave exactly as before.

### Containment Operators

`in` and its alias `contains` take the haystack first, then the needle: a list
contains an equal element, a string contains a substring, and an object
contains a key. A null haystack or needle gives no match.

```cpp
// {"contains": [{"var": "tags"}, {"value": "beta"}]} matches tags ["beta", "pro"]
```

### Semantic Version Operators

Audience filters and rule conditions can compare versions with `semver_eq`,
Expand Down
45 changes: 45 additions & 0 deletions include/absmartly/client_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <cstddef>
#include <string>
#include <tuple>
#include <utility>

namespace absmartly {

Expand All @@ -9,6 +12,48 @@ struct ClientConfig {
std::string api_key;
std::string application;
std::string environment;
// Sent as X-Application-Version. The collector reads it as a 64-bit integer, so only a
// whole number of 1 to 18 digits (surrounding whitespace ignored) is sent; anything else,
// such as "2.4.1", or no version at all, is sent as "0".
std::string application_version;
};

// Keep the four-element structured-binding shape published in 1.0.0. application_version
// is reached by name.
template <std::size_t I>
decltype(auto) get(ClientConfig& config) noexcept {
static_assert(I < 4, "ClientConfig has four structured-binding elements");
if constexpr (I == 0) return (config.endpoint);
else if constexpr (I == 1) return (config.api_key);
else if constexpr (I == 2) return (config.application);
else return (config.environment);
}

template <std::size_t I>
decltype(auto) get(const ClientConfig& config) noexcept {
return static_cast<const std::string&>(get<I>(const_cast<ClientConfig&>(config)));
}

template <std::size_t I>
decltype(auto) get(ClientConfig&& config) noexcept {
return static_cast<std::string&&>(get<I>(config));
}

template <std::size_t I>
decltype(auto) get(const ClientConfig&& config) noexcept {
return static_cast<const std::string&&>(get<I>(const_cast<ClientConfig&>(config)));
}

} // namespace absmartly

namespace std {

template <>
struct tuple_size<absmartly::ClientConfig> : integral_constant<size_t, 4> {};

template <size_t I>
struct tuple_element<I, absmartly::ClientConfig> {
using type = string;
};

} // namespace std
49 changes: 47 additions & 2 deletions include/absmartly/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@

namespace absmartly {

// A unit's arm in one holdout, cached per (holdout id, unit type) while the
// holdout's iteration is unchanged. The arm count is pinned with the arm: arm 1
// means something else in a 2-arm and a 3-arm holdout.
struct HoldoutAssignment {
int id = 0;
int iteration = 0;
std::string unit_type;
int variant = 0;
size_t arm_count = 0;
bool exposed = false;
};

// A holdout covering an experiment, as it was when the assignment was resolved.
// `assignment` is null when the experiment's unit was missing.
struct HoldoutPin {
int id = 0;
int iteration = 0;
std::string name;
std::shared_ptr<HoldoutAssignment> assignment;
};

struct Assignment {
int id = 0;
int iteration = 0;
Expand Down Expand Up @@ -50,13 +71,18 @@ struct Assignment {
std::map<std::string, nlohmann::json> variables;
bool has_variables = false;
int attrs_seq = 0;
bool suppressed = false;
// The holdouts covering the experiment when this was resolved, in id order;
// null when there are none. Exposures fire from these, never re-resolved.
std::shared_ptr<const std::vector<HoldoutPin>> holdouts;
};

struct ExperimentIndex {
const ExperimentData* data = nullptr; // Non-owning; rebuilt whenever Context::data_ changes.
std::vector<std::map<std::string, nlohmann::json>> variables;
// data->assignmentRules parsed once; null when there are none or they are invalid.
nlohmann::json assignment_rules;
std::vector<const HoldoutData*> holdouts; // Sorted by id, unique; non-owning, like data.
};

struct RuleResolution {
Expand Down Expand Up @@ -132,11 +158,23 @@ class Context {
// changes only CPU cost.
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);
// A supplied index must belong to the live data generation; all validation still runs.
Assignment& get_or_create_assignment(const std::string& experiment_name, const ExperimentIndex* experiment = nullptr);
std::pair<const std::string*, Assignment*> variable_assignment(const std::string& key,
std::vector<nlohmann::json>* holdout_events);
void queue_exposure(const std::string& experiment_name, const Assignment& assignment);
void trigger_exposures(const std::string& experiment_name, const Assignment& assignment);
nlohmann::json enqueue_exposure(const std::string& experiment_name, const Assignment& assignment);
void enqueue_holdout_exposures(const std::vector<HoldoutPin>& holdouts, std::vector<nlohmann::json>& events);
std::shared_ptr<HoldoutAssignment> get_holdout_assignment(const HoldoutData& holdout, const std::string& unit_type);
static bool holdouts_match(const ExperimentIndex& experiment, const Assignment& assignment);

void build_index();
// Indexes data into the given maps (pointers into data) and collects variant parse errors;
// touches no member, so a throw leaves the context unchanged.
static void index_data(const ContextData& data, std::map<std::string, ExperimentIndex>& index,
std::map<std::string, std::vector<const ExperimentIndex*>>& index_variables,
std::vector<nlohmann::json>& errors);

bool experiment_matches(const ExperimentData& experiment, const Assignment& assignment) const;
bool audience_matches(const ExperimentData& experiment, Assignment& assignment);
Expand Down Expand Up @@ -198,6 +236,13 @@ class Context {
std::future<ContextData> data_future_;

AudienceMatcher audience_matcher_;

// Kept across refreshes so a holdout exposure fires once per (id, unit type) and iteration.
std::map<std::pair<int, std::string>, std::shared_ptr<HoldoutAssignment>> holdout_assignments_;

// Variable reads that reused their candidate's index entry instead of looking the name up.
unsigned long long index_reuses_ = 0;

};

} // namespace absmartly
Loading
Loading