feat: Agent graph support#181
Open
mattrmc1 wants to merge 12 commits into
Open
Conversation
…b.com:launchdarkly/java-core into mmccarthy/AIC-2837/java-ai-sdk-agent-graph
…l-arg logs to debug
…b.com:launchdarkly/java-core into mmccarthy/AIC-2837/java-ai-sdk-agent-graph
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 35d8b02. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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) throughAIGraphTracker.New types
GraphEdge— immutable edge holding targetkeyand optionalhandoffmetadata map (unmodifiable).AgentGraphNode— wraps a node key, its resolvedAIAgentConfig, and outgoingGraphEdgelist.isTerminal()returns true when edges are empty.AgentGraphFlagValue(package-private) — parses the graph flag JSON protocol:root,edgesadjacency map, and_ldMeta(enabled, variationKey, version). Defensively handles malformed input without throwing.AgentGraphDefinition— the resolved graph:traverseis BFS root-to-leaves;reverseTraverseis 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:Uses
AtomicReference.compareAndSet(null, value)for at-most-once. Empty token usage doesn't burn the slot. Version clamped to minimum 1 on resumption decode.AIGraphMetricSummary— immutable snapshot of graph tracker state (success, durationMs, tokens, path, resumptionToken). All nullable except resumptionToken.Client methods
agentGraphevaluates the graph flag, validates (enabled → root present → all nodes reachable from root → all child configs enabled), fetches each node'sAIAgentConfigpassinggraphKeyfor tracker correlation. Returns disabled definition on any validation failure. Emits$ld:ai:usage:agent-graphusage event.Other changes
ResumptionTokensextended withencodeGraph/decodeGraphfor graph-specific tokens (fields:runId,graphKey,variationKey,version). Madepublicfor access fromAIGraphTracker.agentConfigs()reordered to emit usage count before fetching configs.graphKeyparameter so child node trackers include graph identity in their track data.Test plan
./gradlew :lib:sdk:server-ai:testpassesAIGraphTrackerTest— invocation success/failure + shared guard, duration, total tokens (including zero-usage skip), 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, createTrackerLDAIClientImplTest— agentGraph usage event, enabled/disabled graph, unreachable node validation, non-enabled child config validation, graphKey threading to child trackers, createGraphTracker delegationAgentGraphFlagValueTest— parse root/edges/meta, missing fields, disabled flag, malformed input, handoff metadata, edge with missing key skippedResumptionTokensTest— graph token encode/decode round-tripsNote
Medium Risk
New public API and telemetry paths affect how multi-agent graphs are evaluated and tracked; graph fetches perform N flag evaluations per graph without duplicate usage events, so behavior changes for integrators wiring graphs.
Overview
Adds agent graph support to the server-side AI SDK: callers resolve a graph via
LDAIClient#agentGraph, walk nodes with BFS helpers, and emit graph- and edge-level metrics throughAIGraphTracker.agentGraphevaluates the graph flag, parsesroot/edges/_ldMeta(AgentGraphFlagValue), validates enabled state, non-empty root, reachability from root, and that every referenced node’s agent config is enabled. On failure it returns a disabledAgentGraphDefinitionwith an empty node map; on success it buildsAgentGraphNodeinstances (config + outgoingGraphEdgewith optional handoff). It records$ld:ai:usage:agent-graphand loads node configs without per-node$ld:ai:usage:agent-configevents. ChildLDAIConfigTrackerfactories now receivegraphKeyfor correlation.AgentGraphDefinitionexposes topology (rootNode, children/parents, terminals) plus cycle-safetraverse(root → leaves) andreverseTraverse(terminals → root last), storing visitor results in a shared context map.createTracker()issues a new graph run when enabled.AIGraphTrackersends$ld:ai:graph:*metrics (invocation, duration, tokens, path at-most-once; redirect/handoff multi-fire), exposesAIGraphMetricSummary, and supports graph resumption tokens via newResumptionTokens.encodeGraph/decodeGraphandcreateGraphTracker. Config-token validation now rejects whitespace-only required fields.agentConfigscounts non-null requests for the usage metric before building the result map.Reviewed by Cursor Bugbot for commit 80ef017. Bugbot is set up for automated code reviews on this repo. Configure here.