Skip to content

Commit 6e85473

Browse files
committed
Remove unnecessary one shot restrictions
1 parent 8035890 commit 6e85473

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

packages/sdk/server-ai/src/ldai/tracker.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self):
5151
self._feedback: Optional[Dict[str, FeedbackKind]] = None
5252
self._usage: Optional[TokenUsage] = None
5353
self._time_to_first_token: Optional[int] = None
54-
self._tool_calls: Optional[List[str]] = None
54+
self._tool_calls: List[str] = []
5555
self._resumption_token: Optional[str] = None
5656

5757
@property
@@ -89,7 +89,7 @@ def time_to_first_token(self) -> Optional[int]:
8989
return self._time_to_first_token
9090

9191
@property
92-
def tool_calls(self) -> Optional[List[str]]:
92+
def tool_calls(self) -> List[str]:
9393
"""List of tool keys that were invoked during this operation."""
9494
return self._tool_calls
9595

@@ -429,16 +429,13 @@ def track_tool_calls(self, tool_calls: Iterable[str]) -> None:
429429
"""
430430
Track the tool calls made during an AI operation.
431431
432-
Stores the tool call names on the summary (guarding against duplicate
433-
tracking) and fires a ``$ld:ai:tool_call`` event for each tool.
432+
Appends to the summary's tool call list and fires a
433+
``$ld:ai:tool_call`` event for each tool.
434434
435435
:param tool_calls: Tool identifiers (e.g. from a model response).
436436
"""
437-
if self._summary.tool_calls is not None:
438-
log.warning("Tool calls have already been tracked for this execution. %s", self.__get_track_data())
439-
return
440437
tool_calls_list = list(tool_calls)
441-
self._summary._tool_calls = tool_calls_list
438+
self._summary._tool_calls.extend(tool_calls_list)
442439
for tool_key in tool_calls_list:
443440
self.track_tool_call(tool_key)
444441

@@ -749,12 +746,12 @@ def track_path(self, path: List[str]) -> None:
749746
"""
750747
Track the execution path through the graph.
751748
749+
Appends to the summary's path list and fires a ``$ld:ai:graph:path``
750+
event. Can be called multiple times to build the path incrementally.
751+
752752
:param path: An array of configuration keys representing the sequence of nodes executed during graph traversal.
753753
"""
754-
if self._summary.path:
755-
log.warning("Path has already been tracked for this graph execution. %s", self.__get_track_data())
756-
return
757-
self._summary.path = list(path)
754+
self._summary.path.extend(path)
758755
track_data = {**self.__get_track_data(), "path": path}
759756
self._ld_client.track(
760757
"$ld:ai:graph:path",

packages/sdk/server-ai/tests/test_tracker.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,14 @@ def test_ai_graph_tracker_duplicate_success_is_ignored(client: LDClient):
636636
assert g.get_summary().success is True
637637

638638

639-
def test_ai_graph_tracker_duplicate_path_is_ignored(client: LDClient):
639+
def test_ai_graph_tracker_path_accumulates(client: LDClient):
640640
context = Context.create("user-key")
641641
g = AIGraphTracker(client, "variation-key", "graph-key", 2, context)
642642
g.track_path(["a", "b"])
643643
g.track_path(["x", "y", "z"])
644644
path_calls = [c for c in client.track.mock_calls if c.args[0] == "$ld:ai:graph:path"] # type: ignore
645-
assert len(path_calls) == 1
646-
assert g.get_summary().path == ["a", "b"]
645+
assert len(path_calls) == 2
646+
assert g.get_summary().path == ["a", "b", "x", "y", "z"]
647647

648648

649649
def test_ai_graph_tracker_duplicate_tokens_is_ignored(client: LDClient):
@@ -1236,6 +1236,23 @@ def extract(_r):
12361236
assert len(tool_call_events) == 2
12371237

12381238

1239+
def test_track_tool_calls_accumulates(client: LDClient):
1240+
context = Context.create("user-key")
1241+
tracker = LDAIConfigTracker(
1242+
ld_client=client, run_id="test-run-id", config_key="config-key",
1243+
variation_key="variation-key", version=3, model_name="m",
1244+
provider_name="p", context=context,
1245+
)
1246+
tracker.track_tool_calls(["foo", "bar"])
1247+
tracker.track_tool_calls(["baz"])
1248+
assert tracker.get_summary().tool_calls == ["foo", "bar", "baz"]
1249+
tool_call_events = [
1250+
c for c in client.track.mock_calls # type: ignore
1251+
if c.args[0] == "$ld:ai:tool_call"
1252+
]
1253+
assert len(tool_call_events) == 3
1254+
1255+
12391256
def test_track_metrics_of_skips_track_tool_calls_when_absent(client: LDClient):
12401257
context = Context.create("user-key")
12411258
tracker = LDAIConfigTracker(
@@ -1251,7 +1268,7 @@ def extract(_r):
12511268
return LDAIMetrics(success=True, usage=None)
12521269

12531270
tracker.track_metrics_of(extract, fn)
1254-
assert tracker.get_summary().tool_calls is None
1271+
assert tracker.get_summary().tool_calls == []
12551272
tool_call_events = [
12561273
c for c in client.track.mock_calls # type: ignore
12571274
if c.args[0] == "$ld:ai:tool_call"

0 commit comments

Comments
 (0)