agent-blackbox-cli 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_blackbox_cli-0.3.0.dist-info/METADATA +623 -0
- agent_blackbox_cli-0.3.0.dist-info/RECORD +43 -0
- agent_blackbox_cli-0.3.0.dist-info/WHEEL +5 -0
- agent_blackbox_cli-0.3.0.dist-info/entry_points.txt +2 -0
- agent_blackbox_cli-0.3.0.dist-info/licenses/LICENSE +21 -0
- agent_blackbox_cli-0.3.0.dist-info/top_level.txt +1 -0
- blackbox/__init__.py +61 -0
- blackbox/__main__.py +6 -0
- blackbox/adapters/__init__.py +0 -0
- blackbox/adapters/_linking.py +31 -0
- blackbox/adapters/base.py +38 -0
- blackbox/adapters/claude_code.py +341 -0
- blackbox/adapters/codex.py +318 -0
- blackbox/adapters/labelled_turns.py +371 -0
- blackbox/adapters/registry.py +30 -0
- blackbox/aggregate.py +119 -0
- blackbox/analysis.py +325 -0
- blackbox/bench.py +466 -0
- blackbox/cli.py +405 -0
- blackbox/cost.py +138 -0
- blackbox/describe.py +116 -0
- blackbox/detectors/__init__.py +32 -0
- blackbox/detectors/base.py +83 -0
- blackbox/detectors/context_bloat.py +137 -0
- blackbox/detectors/contradictions.py +180 -0
- blackbox/detectors/error_clusters.py +114 -0
- blackbox/detectors/retry_loops.py +98 -0
- blackbox/detectors/stalls.py +117 -0
- blackbox/detectors/thrash.py +86 -0
- blackbox/diff.py +123 -0
- blackbox/discover.py +189 -0
- blackbox/events.py +163 -0
- blackbox/graph.py +150 -0
- blackbox/guard.py +528 -0
- blackbox/guard_cli.py +349 -0
- blackbox/pricing.py +137 -0
- blackbox/py.typed +0 -0
- blackbox/replay.py +65 -0
- blackbox/report/__init__.py +0 -0
- blackbox/report/html.py +375 -0
- blackbox/report/text.py +287 -0
- blackbox/store.py +215 -0
- blackbox/toolinfo.py +336 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Puja Sridhar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
blackbox
|
blackbox/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Structured analytics for a single agent run, from the trace file it already writes.
|
|
2
|
+
|
|
3
|
+
The command line is the usual entry point, but everything it does is available
|
|
4
|
+
here, because the interesting questions are the ones nobody anticipated:
|
|
5
|
+
|
|
6
|
+
>>> from blackbox import analyze
|
|
7
|
+
>>> run = analyze("session.jsonl")
|
|
8
|
+
>>> run.cost.total_usd
|
|
9
|
+
319.28
|
|
10
|
+
>>> run.wasted()["share_of_run"]
|
|
11
|
+
0.078
|
|
12
|
+
>>> [f.title for f in run.findings if f.severity == "high"]
|
|
13
|
+
|
|
14
|
+
Nothing in this path makes a model call or opens a socket.
|
|
15
|
+
|
|
16
|
+
Note that ``blackbox.replay`` is this function, not the module it lives in.
|
|
17
|
+
Internal code imports it as ``from blackbox.replay import replay`` so the two
|
|
18
|
+
never get confused -- a mistake that broke ``--verify`` for anyone installing
|
|
19
|
+
the package rather than running it from a checkout.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from blackbox.adapters.base import Adapter, Trace
|
|
25
|
+
from blackbox.adapters.registry import ADAPTERS, NoAdapterError
|
|
26
|
+
from blackbox.analysis import Analysis, analyze, analyze_events
|
|
27
|
+
from blackbox.cost import CostReport, ModelCost
|
|
28
|
+
from blackbox.detectors.base import Config, Finding
|
|
29
|
+
from blackbox.events import Event, Usage
|
|
30
|
+
from blackbox.graph import Branch, Graph
|
|
31
|
+
from blackbox.pricing import PRICING_AS_OF, Price
|
|
32
|
+
from blackbox.replay import replay # noqa: F401 (shadows the module of the same name)
|
|
33
|
+
|
|
34
|
+
__version__ = "0.3.0"
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"__version__",
|
|
38
|
+
# analysis
|
|
39
|
+
"analyze",
|
|
40
|
+
"analyze_events",
|
|
41
|
+
"Analysis",
|
|
42
|
+
"replay",
|
|
43
|
+
# configuration and results
|
|
44
|
+
"Config",
|
|
45
|
+
"Finding",
|
|
46
|
+
"CostReport",
|
|
47
|
+
"ModelCost",
|
|
48
|
+
# the canonical model
|
|
49
|
+
"Event",
|
|
50
|
+
"Usage",
|
|
51
|
+
"Graph",
|
|
52
|
+
"Branch",
|
|
53
|
+
# adapters
|
|
54
|
+
"Adapter",
|
|
55
|
+
"Trace",
|
|
56
|
+
"ADAPTERS",
|
|
57
|
+
"NoAdapterError",
|
|
58
|
+
# pricing
|
|
59
|
+
"Price",
|
|
60
|
+
"PRICING_AS_OF",
|
|
61
|
+
]
|
blackbox/__main__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Edge repair shared by every adapter.
|
|
2
|
+
|
|
3
|
+
Vendors record a tool result next to the *message* that issued the call, not
|
|
4
|
+
next to the call itself. With several tool calls in one message that edge
|
|
5
|
+
cannot say which result answers which call, so every per-call duration derived
|
|
6
|
+
from it is a guess. Both adapters re-point the edge using the vendor's own
|
|
7
|
+
call id, which is the one thing that is unambiguous.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import replace
|
|
13
|
+
|
|
14
|
+
from blackbox.events import TOOL_CALL, TOOL_RESULT, Event
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def link_results_to_calls(events: list[Event]) -> list[Event]:
|
|
18
|
+
"""Re-parent each tool result onto the call it answers."""
|
|
19
|
+
call_uid_by_tool_use: dict[str, str] = {}
|
|
20
|
+
for ev in events:
|
|
21
|
+
if ev.kind == TOOL_CALL and ev.tool_use_id:
|
|
22
|
+
call_uid_by_tool_use.setdefault(ev.tool_use_id, ev.uid)
|
|
23
|
+
|
|
24
|
+
linked: list[Event] = []
|
|
25
|
+
for ev in events:
|
|
26
|
+
if ev.kind == TOOL_RESULT and ev.tool_use_id:
|
|
27
|
+
call_uid = call_uid_by_tool_use.get(ev.tool_use_id)
|
|
28
|
+
if call_uid:
|
|
29
|
+
ev = replace(ev, parent_uid=call_uid)
|
|
30
|
+
linked.append(ev)
|
|
31
|
+
return linked
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""The adapter boundary.
|
|
2
|
+
|
|
3
|
+
An adapter is the only part of this tool that knows a vendor's log format.
|
|
4
|
+
Everything else consumes :class:`~blackbox.events.Event`. Vendor trace formats
|
|
5
|
+
are mostly undocumented and they drift; isolating that churn here is what
|
|
6
|
+
keeps a format change from being a rewrite.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any, Protocol, runtime_checkable
|
|
13
|
+
|
|
14
|
+
from blackbox.events import Event
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class Trace:
|
|
19
|
+
"""A parsed agent run."""
|
|
20
|
+
|
|
21
|
+
events: list[Event]
|
|
22
|
+
source: str
|
|
23
|
+
adapter: str
|
|
24
|
+
meta: dict[str, Any] = field(default_factory=dict)
|
|
25
|
+
skipped_lines: int = 0
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@runtime_checkable
|
|
29
|
+
class Adapter(Protocol):
|
|
30
|
+
name: str
|
|
31
|
+
|
|
32
|
+
def sniff(self, path: str) -> bool:
|
|
33
|
+
"""Return True when this adapter recognises the file."""
|
|
34
|
+
...
|
|
35
|
+
|
|
36
|
+
def parse(self, path: str) -> Trace:
|
|
37
|
+
"""Parse the file into canonical events."""
|
|
38
|
+
...
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"""Adapter for Claude Code session transcripts (``~/.claude/projects/**/*.jsonl``).
|
|
2
|
+
|
|
3
|
+
The format is undocumented and will drift. What this adapter relies on, in
|
|
4
|
+
rough order of how load-bearing it is:
|
|
5
|
+
|
|
6
|
+
* ``uuid`` / ``parentUuid`` -- the causal spine of the run
|
|
7
|
+
* ``isSidechain`` -- marks rows belonging to a subagent branch
|
|
8
|
+
* ``message.content[]`` -- ``tool_use`` blocks on assistant rows, ``tool_result``
|
|
9
|
+
blocks on user rows, matched by ``tool_use_id``
|
|
10
|
+
* ``message.usage`` -- token counts, including the cache tiers
|
|
11
|
+
* ``timestamp`` -- advisory only; ordering always comes from line order
|
|
12
|
+
|
|
13
|
+
Anything it does not understand becomes a ``meta`` event rather than an error,
|
|
14
|
+
so a new row type added upstream costs fidelity, never a crash.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
from dataclasses import replace
|
|
21
|
+
from typing import Any, Iterable
|
|
22
|
+
|
|
23
|
+
from blackbox.adapters._linking import link_results_to_calls
|
|
24
|
+
from blackbox.adapters.base import Trace
|
|
25
|
+
from blackbox.toolinfo import is_declined
|
|
26
|
+
from blackbox.events import (
|
|
27
|
+
ASSISTANT_MESSAGE,
|
|
28
|
+
META,
|
|
29
|
+
TOOL_CALL,
|
|
30
|
+
TOOL_RESULT,
|
|
31
|
+
USER_MESSAGE,
|
|
32
|
+
Event,
|
|
33
|
+
Usage,
|
|
34
|
+
parse_ts,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
NAME = "claude_code"
|
|
38
|
+
|
|
39
|
+
# Rows that exist for the client's own bookkeeping and carry no model work.
|
|
40
|
+
_BOOKKEEPING = {
|
|
41
|
+
"queue-operation",
|
|
42
|
+
"last-prompt",
|
|
43
|
+
"custom-title",
|
|
44
|
+
"file-history-snapshot",
|
|
45
|
+
"atis-latch",
|
|
46
|
+
"attachment",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _usage(raw: Any) -> Usage:
|
|
51
|
+
"""Read a ``message.usage`` object, preferring the explicit cache split."""
|
|
52
|
+
if not isinstance(raw, dict):
|
|
53
|
+
return Usage()
|
|
54
|
+
|
|
55
|
+
creation = raw.get("cache_creation")
|
|
56
|
+
write_5m = 0
|
|
57
|
+
write_1h = 0
|
|
58
|
+
if isinstance(creation, dict):
|
|
59
|
+
write_5m = int(creation.get("ephemeral_5m_input_tokens") or 0)
|
|
60
|
+
write_1h = int(creation.get("ephemeral_1h_input_tokens") or 0)
|
|
61
|
+
|
|
62
|
+
total_writes = int(raw.get("cache_creation_input_tokens") or 0)
|
|
63
|
+
if total_writes and (write_5m + write_1h) == 0:
|
|
64
|
+
# Older rows report only the total. Attribute it to the 5-minute tier,
|
|
65
|
+
# which is the cheaper of the two, so an unknown split can only ever
|
|
66
|
+
# understate the bill rather than inflate it.
|
|
67
|
+
write_5m = total_writes
|
|
68
|
+
|
|
69
|
+
details = raw.get("output_tokens_details")
|
|
70
|
+
thinking = 0
|
|
71
|
+
if isinstance(details, dict):
|
|
72
|
+
thinking = int(details.get("thinking_tokens") or 0)
|
|
73
|
+
|
|
74
|
+
return Usage(
|
|
75
|
+
input_tokens=int(raw.get("input_tokens") or 0),
|
|
76
|
+
output_tokens=int(raw.get("output_tokens") or 0),
|
|
77
|
+
cache_read_tokens=int(raw.get("cache_read_input_tokens") or 0),
|
|
78
|
+
cache_write_5m_tokens=write_5m,
|
|
79
|
+
cache_write_1h_tokens=write_1h,
|
|
80
|
+
thinking_tokens=thinking,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _result_text(content: Any) -> str:
|
|
85
|
+
"""Flatten a tool_result payload, which may be a string or a block list."""
|
|
86
|
+
if isinstance(content, str):
|
|
87
|
+
return content
|
|
88
|
+
if isinstance(content, list):
|
|
89
|
+
parts = []
|
|
90
|
+
for block in content:
|
|
91
|
+
if isinstance(block, dict):
|
|
92
|
+
if isinstance(block.get("text"), str):
|
|
93
|
+
parts.append(block["text"])
|
|
94
|
+
elif block.get("type"):
|
|
95
|
+
parts.append(f"[{block['type']}]")
|
|
96
|
+
return "\n".join(parts)
|
|
97
|
+
if content is None:
|
|
98
|
+
return ""
|
|
99
|
+
return str(content)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _message_text(content: Any) -> str:
|
|
103
|
+
if isinstance(content, str):
|
|
104
|
+
return content
|
|
105
|
+
if isinstance(content, list):
|
|
106
|
+
return "\n".join(
|
|
107
|
+
b["text"]
|
|
108
|
+
for b in content
|
|
109
|
+
if isinstance(b, dict) and b.get("type") == "text" and isinstance(b.get("text"), str)
|
|
110
|
+
)
|
|
111
|
+
return ""
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def sniff(path: str) -> bool:
|
|
115
|
+
"""True when the first usable line looks like a Claude Code row."""
|
|
116
|
+
try:
|
|
117
|
+
with open(path, "r", encoding="utf-8", errors="replace") as fh:
|
|
118
|
+
for _ in range(20):
|
|
119
|
+
line = fh.readline()
|
|
120
|
+
if not line:
|
|
121
|
+
break
|
|
122
|
+
line = line.strip()
|
|
123
|
+
if not line:
|
|
124
|
+
continue
|
|
125
|
+
try:
|
|
126
|
+
row = json.loads(line)
|
|
127
|
+
except json.JSONDecodeError:
|
|
128
|
+
return False
|
|
129
|
+
if not isinstance(row, dict):
|
|
130
|
+
return False
|
|
131
|
+
if "sessionId" in row or row.get("type") in {"assistant", "user"}:
|
|
132
|
+
return True
|
|
133
|
+
except OSError:
|
|
134
|
+
return False
|
|
135
|
+
return False
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def parse(path: str) -> Trace:
|
|
139
|
+
with open(path, "r", encoding="utf-8", errors="replace") as fh:
|
|
140
|
+
return parse_lines(fh, source=path)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def parse_lines(lines: Iterable[str], source: str) -> Trace:
|
|
144
|
+
"""Parse transcript rows from any iterable of lines.
|
|
145
|
+
|
|
146
|
+
:func:`parse` reads a whole file through this. The live guard reads only
|
|
147
|
+
the tail of a long session through it, because a hook that has to parse
|
|
148
|
+
fifty megabytes before every tool call would be a hook nobody keeps.
|
|
149
|
+
"""
|
|
150
|
+
events: list[Event] = []
|
|
151
|
+
# A row that carried tool results is represented by synthetic event uids
|
|
152
|
+
# (``<uuid>:tr0``), so a later row whose parentUuid names that row would
|
|
153
|
+
# dangle. This maps each source row uuid to the event that stands for it.
|
|
154
|
+
representative: dict[str, str] = {}
|
|
155
|
+
seq = 0
|
|
156
|
+
skipped = 0
|
|
157
|
+
session_id = None
|
|
158
|
+
cwd = None
|
|
159
|
+
git_branch = None
|
|
160
|
+
versions: set[str] = set()
|
|
161
|
+
|
|
162
|
+
for lineno, line in enumerate(lines, start=1):
|
|
163
|
+
line = line.strip()
|
|
164
|
+
if not line:
|
|
165
|
+
continue
|
|
166
|
+
try:
|
|
167
|
+
row = json.loads(line)
|
|
168
|
+
except json.JSONDecodeError:
|
|
169
|
+
skipped += 1
|
|
170
|
+
continue
|
|
171
|
+
if not isinstance(row, dict):
|
|
172
|
+
skipped += 1
|
|
173
|
+
continue
|
|
174
|
+
|
|
175
|
+
session_id = session_id or row.get("sessionId")
|
|
176
|
+
cwd = cwd or row.get("cwd")
|
|
177
|
+
git_branch = git_branch or row.get("gitBranch")
|
|
178
|
+
if isinstance(row.get("version"), str):
|
|
179
|
+
versions.add(row["version"])
|
|
180
|
+
|
|
181
|
+
raw_type = str(row.get("type") or "unknown")
|
|
182
|
+
uid = row.get("uuid") or f"line{lineno}"
|
|
183
|
+
parent = row.get("parentUuid")
|
|
184
|
+
ts = parse_ts(row.get("timestamp"))
|
|
185
|
+
sidechain = bool(row.get("isSidechain"))
|
|
186
|
+
message = row.get("message") if isinstance(row.get("message"), dict) else {}
|
|
187
|
+
model = message.get("model")
|
|
188
|
+
content = message.get("content")
|
|
189
|
+
|
|
190
|
+
tool_result_payload = row.get("toolUseResult")
|
|
191
|
+
interrupted = bool(
|
|
192
|
+
isinstance(tool_result_payload, dict) and tool_result_payload.get("interrupted")
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
if raw_type == "assistant":
|
|
196
|
+
events.append(
|
|
197
|
+
Event(
|
|
198
|
+
uid=uid,
|
|
199
|
+
seq=seq,
|
|
200
|
+
kind=ASSISTANT_MESSAGE,
|
|
201
|
+
parent_uid=parent,
|
|
202
|
+
ts=ts,
|
|
203
|
+
is_sidechain=sidechain,
|
|
204
|
+
model=model,
|
|
205
|
+
usage=_usage(message.get("usage")),
|
|
206
|
+
text=_message_text(content),
|
|
207
|
+
raw_type=raw_type,
|
|
208
|
+
source_line=lineno,
|
|
209
|
+
)
|
|
210
|
+
)
|
|
211
|
+
seq += 1
|
|
212
|
+
|
|
213
|
+
representative.setdefault(uid, uid)
|
|
214
|
+
|
|
215
|
+
if isinstance(content, list):
|
|
216
|
+
for idx, block in enumerate(content):
|
|
217
|
+
if not isinstance(block, dict) or block.get("type") != "tool_use":
|
|
218
|
+
continue
|
|
219
|
+
tool_input = block.get("input")
|
|
220
|
+
events.append(
|
|
221
|
+
Event(
|
|
222
|
+
uid=f"{uid}:tc{idx}",
|
|
223
|
+
seq=seq,
|
|
224
|
+
kind=TOOL_CALL,
|
|
225
|
+
parent_uid=uid,
|
|
226
|
+
ts=ts,
|
|
227
|
+
is_sidechain=sidechain,
|
|
228
|
+
model=model,
|
|
229
|
+
tool_name=block.get("name"),
|
|
230
|
+
tool_use_id=block.get("id"),
|
|
231
|
+
tool_input=tool_input if isinstance(tool_input, dict) else {},
|
|
232
|
+
raw_type="tool_use",
|
|
233
|
+
source_line=lineno,
|
|
234
|
+
)
|
|
235
|
+
)
|
|
236
|
+
seq += 1
|
|
237
|
+
continue
|
|
238
|
+
|
|
239
|
+
if raw_type == "user":
|
|
240
|
+
results = [
|
|
241
|
+
b
|
|
242
|
+
for b in (content if isinstance(content, list) else [])
|
|
243
|
+
if isinstance(b, dict) and b.get("type") == "tool_result"
|
|
244
|
+
]
|
|
245
|
+
if results:
|
|
246
|
+
representative.setdefault(uid, f"{uid}:tr0")
|
|
247
|
+
for idx, block in enumerate(results):
|
|
248
|
+
is_error = block.get("is_error")
|
|
249
|
+
output = _result_text(block.get("content"))
|
|
250
|
+
flags: tuple[str, ...] = ("interrupted",) if interrupted else ()
|
|
251
|
+
# A human refusing a tool call is not a tool failure.
|
|
252
|
+
# Left unflagged it pollutes the failure count and
|
|
253
|
+
# clusters with real errors.
|
|
254
|
+
if is_error and is_declined(output):
|
|
255
|
+
flags = flags + ("declined",)
|
|
256
|
+
events.append(
|
|
257
|
+
Event(
|
|
258
|
+
uid=f"{uid}:tr{idx}",
|
|
259
|
+
seq=seq,
|
|
260
|
+
kind=TOOL_RESULT,
|
|
261
|
+
parent_uid=parent,
|
|
262
|
+
ts=ts,
|
|
263
|
+
is_sidechain=sidechain,
|
|
264
|
+
tool_use_id=block.get("tool_use_id"),
|
|
265
|
+
tool_ok=(not bool(is_error)) if is_error is not None else None,
|
|
266
|
+
tool_output=output,
|
|
267
|
+
flags=flags,
|
|
268
|
+
raw_type="tool_result",
|
|
269
|
+
source_line=lineno,
|
|
270
|
+
)
|
|
271
|
+
)
|
|
272
|
+
seq += 1
|
|
273
|
+
continue
|
|
274
|
+
|
|
275
|
+
events.append(
|
|
276
|
+
Event(
|
|
277
|
+
uid=uid,
|
|
278
|
+
seq=seq,
|
|
279
|
+
kind=USER_MESSAGE,
|
|
280
|
+
parent_uid=parent,
|
|
281
|
+
ts=ts,
|
|
282
|
+
is_sidechain=sidechain,
|
|
283
|
+
text=_message_text(content),
|
|
284
|
+
raw_type=raw_type,
|
|
285
|
+
source_line=lineno,
|
|
286
|
+
)
|
|
287
|
+
)
|
|
288
|
+
seq += 1
|
|
289
|
+
representative.setdefault(uid, uid)
|
|
290
|
+
continue
|
|
291
|
+
|
|
292
|
+
representative.setdefault(uid, uid)
|
|
293
|
+
events.append(
|
|
294
|
+
Event(
|
|
295
|
+
uid=uid,
|
|
296
|
+
seq=seq,
|
|
297
|
+
kind=META,
|
|
298
|
+
parent_uid=parent,
|
|
299
|
+
ts=ts,
|
|
300
|
+
is_sidechain=sidechain,
|
|
301
|
+
text=str(row.get("content") or "")[:2000],
|
|
302
|
+
raw_type=raw_type,
|
|
303
|
+
source_line=lineno,
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
seq += 1
|
|
307
|
+
|
|
308
|
+
events = _resolve_parents(events, representative)
|
|
309
|
+
events = link_results_to_calls(events)
|
|
310
|
+
|
|
311
|
+
return Trace(
|
|
312
|
+
events=events,
|
|
313
|
+
source=source,
|
|
314
|
+
adapter=NAME,
|
|
315
|
+
skipped_lines=skipped,
|
|
316
|
+
meta={
|
|
317
|
+
"session_id": session_id,
|
|
318
|
+
"cwd": cwd,
|
|
319
|
+
"git_branch": git_branch,
|
|
320
|
+
"client_versions": sorted(versions),
|
|
321
|
+
},
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _resolve_parents(events: list[Event], representative: dict[str, str]) -> list[Event]:
|
|
326
|
+
"""Point every parent edge at an event that actually exists.
|
|
327
|
+
|
|
328
|
+
Without this, any row whose ``parentUuid`` names a row that carried tool
|
|
329
|
+
results dangles -- the row's uuid is not an event uid, the synthetic
|
|
330
|
+
``<uuid>:tr0`` is. Those dangling edges read as orphans, and because a
|
|
331
|
+
sidechain event with no visible parent starts a new branch, one subagent
|
|
332
|
+
would be reported as several.
|
|
333
|
+
"""
|
|
334
|
+
out: list[Event] = []
|
|
335
|
+
for ev in events:
|
|
336
|
+
if ev.parent_uid:
|
|
337
|
+
resolved = representative.get(ev.parent_uid)
|
|
338
|
+
if resolved and resolved != ev.parent_uid:
|
|
339
|
+
ev = replace(ev, parent_uid=resolved)
|
|
340
|
+
out.append(ev)
|
|
341
|
+
return out
|