Commit 9a9941b
authored
feat: Add AIConfigTracker with at-most-once tracking and resumption tokens (#179)
## Summary
Implements the full `LDAIConfigTracker` interface — previously a stub.
Callers can now record AI operation metrics (duration, tokens,
success/error, feedback, tool calls, judge results) with at-most-once
enforcement, extract metrics from runner operations via
`trackMetricsOf`, and reconstruct trackers across processes via
resumption tokens.
### Tracking methods
```java
void trackDuration(Duration duration);
<T> T trackDurationOf(Callable<T> operation) throws Exception;
```
Records wall-clock duration. Null silently dropped (debug log);
negatives clamped to zero. `trackDurationOf` wraps a `Callable`,
measures via `System.nanoTime()`, records duration in `finally` even on
exception.
```java
<T> T trackMetricsOf(Function<? super T, AIMetrics> metricsExtractor, Callable<T> operation) throws Exception;
```
All-in-one wrapper: starts timer, invokes operation, stops clock before
calling the extractor (slow extractors don't inflate duration). On
success: prefers runner-reported `durationMs` over wall-clock, then
delegates to `trackSuccess`/`trackError`, `trackTokens`,
`trackToolCalls`. On exception: records wall-clock duration, calls
`trackError`, rethrows. If the extractor itself throws, operation
duration is still recorded before propagating — `trackError` is NOT
called since the AI operation succeeded.
```java
void trackSuccess();
void trackError();
```
Share a single `AtomicReference<Boolean>` guard — only the first to fire
wins.
```java
void trackFeedback(FeedbackKind kind);
```
Validates and resolves the event name before claiming the at-most-once
guard, so null/invalid input doesn't burn the slot.
```java
void trackTokens(TokenUsage tokens);
```
Emits events for each positive count (total, input, output). All-zero
usage does not consume the at-most-once slot.
```java
void trackToolCall(String toolKey);
void trackToolCalls(List<String> toolKeys);
```
Multi-fire (not at-most-once). Each call emits a separate
`$ld:ai:tool_call` event.
```java
void trackJudgeResult(JudgeResult result);
```
Silently dropped when not sampled, not successful, or when `metricKey`
is blank/null or `score` is null/non-finite. Multi-fire.
```java
void trackTimeToFirstToken(Duration duration);
```
Records time-to-first-token duration. At-most-once.
### Resumption tokens
```java
String getResumptionToken(); // on LDAIConfigTracker
LDAIConfigTracker createTracker(String resumptionToken, LDContext context); // on LDAIClient
```
`getResumptionToken()` returns URL-safe Base64 (no padding) JSON
containing `{ runId, configKey, variationKey, version, graphKey }`.
`variationKey` and `graphKey` omitted when null. No length cap — large
config keys are supported. Empty `runId` / `configKey` are rejected on
decode.
### Tracker factory wiring
`LDAIClientImpl` now creates real `LDAIConfigTrackerImpl` instances. A
private `trackerFactory` method captures config identity and returns a
`Supplier<LDAIConfigTracker>` producing a fresh tracker with a new
`runId` on each call. Default configs also get real trackers. Default
version is `1`.
`NoOpAIConfigTracker` deleted — no longer needed.
### New types
**`FeedbackKind`** — enum: `POSITIVE`, `NEGATIVE`.
**`TokenUsage`** — immutable record: `total`, `input`, `output`.
**`AIMetrics`** — immutable builder: `success`, optional `tokens`,
`durationMs`, `toolCalls`.
**`JudgeResult`** — immutable builder: `metricKey`, `score`, `sampled`,
`success`, optional `judgeConfigKey`, `reasoning`, `errorMessage`.
**`MetricSummary`** — snapshot of all tracked metrics plus resumption
token.
**`TrackData`** — run identity fields with `toLDValue()`.
### Thread safety
All at-most-once slots use `AtomicReference<T>.compareAndSet(null,
value)` — single atomic guard+value, no race window. Tool calls use
`CopyOnWriteArrayList`.
## Test plan
- [ ] `./gradlew :lib:sdk:server-ai:test` passes
- [ ] `LDAIConfigTrackerImplTest` — duration (emit, clamp, at-most-once,
null), durationOf (success + exception), success/error (emit, shared
guard both directions), feedback (emit, at-most-once, null slot
preservation), tokens (positive counts, zero skip, slot preservation),
tool calls (multi-fire, null), judge result
(sampled/success/metricKey/score guards, multi-fire), trackMetricsOf
(success path, error path, extractor failure duration tracking, null
AIMetrics guard), variationKey/graphKey in payload, concurrency
(20-thread contention), constructor null rejection
- [ ] `ResumptionTokensTest` — encode/decode round-trips, large keys,
special character escaping, null/malformed rejection, empty
runId/configKey rejection
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> New public tracking API and telemetry emission change observability
behavior; resumption tokens embed flag-targeting metadata if exposed to
clients.
>
> **Overview**
> Replaces the **no-op** `LDAIConfigTracker` stub with a full
implementation that emits LaunchDarkly custom metrics for AI runs
(duration, time-to-first-token, success/error, feedback, tokens, tool
calls, and judge scores).
>
> **`LDAIClientImpl`** now supplies a per-config `Supplier` that creates
**`LDAIConfigTrackerImpl`** instances (new UUID `runId` per
`createTracker()`), including when falling back to caller defaults.
**`NoOpAIConfigTracker`** is removed.
**`LDAIClient#createTracker(String, LDContext)`** decodes a resumption
token to continue the same run across requests.
>
> The expanded **`LDAIConfigTracker`** API adds **`trackMetricsOf`**,
**`getSummary`**, **`getTrackData`**, and **`getResumptionToken`**, with
**at-most-once** semantics on most metrics (tool calls and judge results
are multi-fire). **`LDAITrackingTypes`** holds the new immutable value
types; **`ResumptionTokens`** encodes/decodes URL-safe Base64 JSON for
run identity (docs warn tokens can expose **variation key** / version
and should stay server-side).
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
121b140. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent ad2ac08 commit 9a9941b
9 files changed
Lines changed: 2525 additions & 36 deletions
File tree
- lib/sdk/server-ai/src
- main/java/com/launchdarkly/sdk/server/ai
- datamodel
- internal
- test/java/com/launchdarkly/sdk/server/ai/internal
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
84 | 103 | | |
Lines changed: 47 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | 11 | | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
55 | | - | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
| |||
187 | 186 | | |
188 | 187 | | |
189 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
190 | 192 | | |
191 | 193 | | |
192 | 194 | | |
| |||
197 | 199 | | |
198 | 200 | | |
199 | 201 | | |
200 | | - | |
| 202 | + | |
201 | 203 | | |
202 | 204 | | |
203 | 205 | | |
| |||
206 | 208 | | |
207 | 209 | | |
208 | 210 | | |
209 | | - | |
| 211 | + | |
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
| |||
217 | 219 | | |
218 | 220 | | |
219 | 221 | | |
220 | | - | |
| 222 | + | |
221 | 223 | | |
222 | 224 | | |
223 | 225 | | |
| |||
231 | 233 | | |
232 | 234 | | |
233 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
234 | 239 | | |
235 | 240 | | |
236 | 241 | | |
| |||
242 | 247 | | |
243 | 248 | | |
244 | 249 | | |
245 | | - | |
| 250 | + | |
246 | 251 | | |
247 | 252 | | |
248 | 253 | | |
| |||
253 | 258 | | |
254 | 259 | | |
255 | 260 | | |
256 | | - | |
| 261 | + | |
257 | 262 | | |
258 | 263 | | |
259 | 264 | | |
| |||
266 | 271 | | |
267 | 272 | | |
268 | 273 | | |
269 | | - | |
| 274 | + | |
270 | 275 | | |
271 | 276 | | |
272 | 277 | | |
273 | 278 | | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
274 | 311 | | |
275 | 312 | | |
276 | 313 | | |
| |||
Lines changed: 160 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
3 | 15 | | |
4 | 16 | | |
5 | 17 | | |
6 | | - | |
7 | | - | |
8 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
9 | 26 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 27 | + | |
14 | 28 | | |
15 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
16 | 169 | | |
0 commit comments