Commit 47c4f36
authored
feat: Agent graph support (#181)
## Summary
Adds **agent graph support** — flag evaluation, graph validation, BFS
traversal, graph-level tracking, and resumption tokens. Callers fetch a
graph definition via `agentGraph(graphKey, context, variables)`, inspect
or traverse the node topology, and track graph-level metrics (invocation
success/failure, duration, tokens, path) plus edge-level events
(redirect, handoff) through `AIGraphTracker`.
### New types
**`GraphEdge`** — immutable edge holding target `key` and optional
`handoff` metadata map (unmodifiable defensive copy).
**`AgentGraphNode`** — wraps a node key, its resolved `AIAgentConfig`,
and an unmodifiable outgoing `GraphEdge` list (defensive copy).
`isTerminal()` returns true when edges are empty.
**`AgentGraphFlagValue`** (package-private) — parses the graph flag JSON
protocol: `root`, `edges` adjacency map, and `_ldMeta` (enabled,
variationKey, version). Defensively handles malformed input without
throwing.
**`AgentGraphDefinition`** — the resolved graph:
```java
boolean isEnabled();
AgentGraphNode rootNode();
AgentGraphNode getNode(String nodeKey);
List<AgentGraphNode> getChildNodes(String nodeKey);
List<AgentGraphNode> getParentNodes(String nodeKey);
List<AgentGraphNode> terminalNodes();
AIGraphTracker createTracker();
void traverse(BiFunction<AgentGraphNode, Map<String, Object>, Object> fn, Map<String, Object> ctx);
void reverseTraverse(BiFunction<AgentGraphNode, Map<String, Object>, Object> fn, Map<String, Object> ctx);
```
`traverse` is BFS root-to-leaves; `reverseTraverse` is BFS
terminals-to-root (root always processed last). Both are cycle-safe —
each node visited at most once. Visitor results stored in the context
map under the node's key.
**`AIGraphTracker`** — graph-level tracking:
```java
// At-most-once (invocation success/failure share one guard):
void trackInvocationSuccess();
void trackInvocationFailure();
void trackDuration(double durationMs);
void trackTotalTokens(TokenUsage tokens);
void trackPath(List<String> path);
// Multi-fire:
void trackRedirect(String sourceKey, String redirectedTarget);
void trackHandoffSuccess(String sourceKey, String targetKey);
void trackHandoffFailure(String sourceKey, String targetKey);
AIGraphMetricSummary getSummary();
String getResumptionToken();
```
Uses `AtomicReference.compareAndSet(null, value)` for at-most-once.
Non-finite durations (NaN, infinity) are rejected without consuming the
at-most-once slot. Reconstruction from a resumption token is handled via
`LDAIClient.createGraphTracker(token, context)`, which pipes the
configured logger from the client (the static `fromResumptionToken`
method is package-private).
**`AIGraphMetricSummary`** — immutable snapshot of graph tracker state
(success, durationMs, tokens, path, resumptionToken). All nullable
except resumptionToken.
### Client methods
```java
AgentGraphDefinition agentGraph(String graphKey, LDContext context, Map<String, Object> variables);
AgentGraphDefinition agentGraph(String graphKey, LDContext context);
AIGraphTracker createGraphTracker(String resumptionToken, LDContext context);
```
`agentGraph` validates that `graphKey` is non-null and non-blank (throws
`NullPointerException` / `IllegalArgumentException`), then evaluates the
graph flag, validates (enabled -> root present -> all nodes reachable
from root -> all child configs enabled), fetches each node's
`AIAgentConfig` passing `graphKey` for tracker correlation. Returns
disabled definition on any validation failure. Emits
`$ld:ai:usage:agent-graph` usage event.
`createGraphTracker` reconstructs an `AIGraphTracker` from a resumption
token, preserving the original run identity while using the client's
configured logger.
### Logging
Default logger resolution (`Loggers.defaultLogger()`) is centralized in
`internal/Loggers.java` and used only from the `LDAIClientImpl`
single-argument constructor as an entry-point fallback. All other code
paths receive the logger from the top via constructor injection,
consistent with the convention in other LaunchDarkly SDKs (.NET, JS).
### Other changes
- `ResumptionTokens` extended with `encodeGraph`/`decodeGraph` for
graph-specific tokens (fields: `runId`, `graphKey`, `variationKey`,
`version`). Made `public` for access from `AIGraphTracker`. Decode
enforces non-blank `runId` and `graphKey`.
- `agentConfigs()` reordered to emit usage count before fetching
configs.
- Config evaluation methods gain `graphKey` parameter so child node
trackers include graph identity in their track data.
## Test plan
- [ ] `./gradlew :lib:sdk:server-ai:test` passes
- [ ] `AIGraphTrackerTest` — invocation success/failure + shared guard,
duration (including non-finite rejection), total tokens, path,
redirect/handoff multi-fire, base data correctness, variationKey
omission, getSummary, resumption token round-trip, concurrency
(20-thread contention for invocation and duration)
- [ ] `AgentGraphDefinitionTest` — buildNodes, collectAllKeys,
traverse/reverseTraverse (including cycles, single-node, diamond
graphs), rootNode/getNode/getChildNodes/getParentNodes/terminalNodes,
disabled graph behavior, createTracker
- [ ] `LDAIClientImplTest` — agentGraph usage event, enabled/disabled
graph, unreachable node validation, non-enabled child config validation,
graphKey threading to child trackers, createGraphTracker delegation,
null/blank graphKey rejection
- [ ] `AgentGraphFlagValueTest` — parse root/edges/meta, missing fields,
disabled flag, malformed input, handoff metadata, edge with missing key
skipped
- [ ] `ResumptionTokensTest` — graph token encode/decode round-trips,
blank field rejection1 parent a32c4fa commit 47c4f36
14 files changed
Lines changed: 2891 additions & 31 deletions
File tree
- lib/sdk/server-ai/src
- main/java/com/launchdarkly/sdk/server/ai
- internal
- test/java/com/launchdarkly/sdk/server/ai
- internal
Lines changed: 84 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 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 | + | |
Lines changed: 303 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 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 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 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 | + | |
0 commit comments