style(blackbox): apply ruff formatting to analytics

This commit is contained in:
Kevin Turcios 2026-04-28 17:56:50 -05:00
parent 0d0e43af8d
commit fa770cfe6d
2 changed files with 266 additions and 176 deletions

View file

@ -117,9 +117,7 @@ def extract_meta(path: Path) -> SessionMeta | None: # noqa: C901, PLR0912, PLR0
if not first_prompt:
first_prompt = content[:120]
elif isinstance(content, list):
has_tool_result = any(
isinstance(b, dict) and b.get("type") == "tool_result" for b in content
)
has_tool_result = any(isinstance(b, dict) and b.get("type") == "tool_result" for b in content)
if has_tool_result:
for block in content:
if not isinstance(block, dict) or block.get("type") != "tool_result":
@ -140,11 +138,7 @@ def extract_meta(path: Path) -> SessionMeta | None: # noqa: C901, PLR0912, PLR0
else:
user_messages += 1
if not first_prompt:
texts = [
b.get("text", "")
for b in content
if isinstance(b, dict) and b.get("type") == "text"
]
texts = [b.get("text", "") for b in content if isinstance(b, dict) and b.get("type") == "text"]
first_prompt = " ".join(texts)[:120]
interrupted = False
@ -197,7 +191,10 @@ def extract_meta(path: Path) -> SessionMeta | None: # noqa: C901, PLR0912, PLR0
tool_input = block.get("input", {})
if isinstance(tool_input, dict):
track_file_changes(
tool_name, tool_input, files_modified, languages,
tool_name,
tool_input,
files_modified,
languages,
)
lines_a, lines_r = count_diff_lines(tool_name, tool_input)
lines_added += lines_a

View file

@ -49,21 +49,24 @@ class TestExtractMeta:
def test_basic_session(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "abc123.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "user",
"timestamp": _ts(0),
"message": {"content": "optimize this function"},
},
{
"type": "assistant",
"timestamp": _ts(10),
"message": {
"content": [{"type": "text", "text": "I'll help you."}],
"usage": {"input_tokens": 500, "output_tokens": 200},
_write_jsonl(
p,
[
{
"type": "user",
"timestamp": _ts(0),
"message": {"content": "optimize this function"},
},
},
])
{
"type": "assistant",
"timestamp": _ts(10),
"message": {
"content": [{"type": "text", "text": "I'll help you."}],
"usage": {"input_tokens": 500, "output_tokens": 200},
},
},
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.session_id == "abc123"
@ -77,19 +80,27 @@ class TestExtractMeta:
def test_counts_tool_calls(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "tool_use", "id": "t1", "name": "Read", "input": {"file_path": "/a.py"}},
{"type": "tool_use", "id": "t2", "name": "Edit", "input": {"file_path": "/a.py", "old_string": "x", "new_string": "y"}},
],
"usage": {"input_tokens": 100, "output_tokens": 50},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "tool_use", "id": "t1", "name": "Read", "input": {"file_path": "/a.py"}},
{
"type": "tool_use",
"id": "t2",
"name": "Edit",
"input": {"file_path": "/a.py", "old_string": "x", "new_string": "y"},
},
],
"usage": {"input_tokens": 100, "output_tokens": 50},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.tool_calls == 2
@ -98,21 +109,26 @@ class TestExtractMeta:
def test_counts_git_commits(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{
"type": "tool_use",
"id": "t1",
"name": "Bash",
"input": {"command": "git commit -m 'fix things'"},
}],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Bash",
"input": {"command": "git commit -m 'fix things'"},
}
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.git_commits == 1
@ -120,21 +136,26 @@ class TestExtractMeta:
def test_amend_not_counted_as_commit(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{
"type": "tool_use",
"id": "t1",
"name": "Bash",
"input": {"command": "git commit --amend -m 'fix'"},
}],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Bash",
"input": {"command": "git commit --amend -m 'fix'"},
}
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.git_commits == 0
@ -142,11 +163,14 @@ class TestExtractMeta:
def test_counts_compactions(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{"type": "user", "timestamp": _ts(0), "message": {"content": "hi"}},
{"type": "summary", "timestamp": _ts(5)},
{"type": "summary", "timestamp": _ts(10)},
])
_write_jsonl(
p,
[
{"type": "user", "timestamp": _ts(0), "message": {"content": "hi"}},
{"type": "summary", "timestamp": _ts(5)},
{"type": "summary", "timestamp": _ts(10)},
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.compactions == 2
@ -154,19 +178,22 @@ class TestExtractMeta:
def test_counts_thinking_blocks(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "thinking", "thinking": "let me think..."},
{"type": "text", "text": "here's my answer"},
],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "thinking", "thinking": "let me think..."},
{"type": "text", "text": "here's my answer"},
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.thinking_blocks == 1
@ -174,10 +201,13 @@ class TestExtractMeta:
def test_tracks_permission_mode(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{"type": "permission-mode", "timestamp": _ts(0), "permissionMode": "bypassPermissions"},
{"type": "user", "timestamp": _ts(1), "message": {"content": "go"}},
])
_write_jsonl(
p,
[
{"type": "permission-mode", "timestamp": _ts(0), "permissionMode": "bypassPermissions"},
{"type": "user", "timestamp": _ts(1), "message": {"content": "go"}},
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.permission_mode == "bypassPermissions"
@ -185,20 +215,23 @@ class TestExtractMeta:
def test_tracks_web_usage(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{"type": "text", "text": "searching"}],
"usage": {
"input_tokens": 100,
"output_tokens": 50,
"server_tool_use": {"web_search_requests": 2, "web_fetch_requests": 1},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{"type": "text", "text": "searching"}],
"usage": {
"input_tokens": 100,
"output_tokens": 50,
"server_tool_use": {"web_search_requests": 2, "web_fetch_requests": 1},
},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.web_searches == 2
@ -210,7 +243,13 @@ class TestExtractMeta:
p.write_text(
json.dumps({"type": "user", "timestamp": _ts(0), "message": {"content": "hi"}})
+ "\nnot valid json\n"
+ json.dumps({"type": "assistant", "timestamp": _ts(1), "message": {"content": [{"type": "text", "text": "ok"}], "usage": {}}})
+ json.dumps(
{
"type": "assistant",
"timestamp": _ts(1),
"message": {"content": [{"type": "text", "text": "ok"}], "usage": {}},
}
)
+ "\n"
)
meta = extract_meta(p)
@ -221,23 +260,33 @@ class TestExtractMeta:
def test_tracks_tool_errors(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls /nope"}}],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls /nope"}}],
"usage": {},
},
},
},
{
"type": "user",
"timestamp": _ts(1),
"message": {
"content": [{"type": "tool_result", "tool_use_id": "t1", "is_error": True, "content": "command not found"}],
{
"type": "user",
"timestamp": _ts(1),
"message": {
"content": [
{
"type": "tool_result",
"tool_use_id": "t1",
"is_error": True,
"content": "command not found",
}
],
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.tool_errors == 1
@ -487,17 +536,20 @@ class TestExtractMetaCodeflash:
def test_non_codeflash_session_has_none(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{"type": "user", "timestamp": _ts(0), "message": {"content": "hello"}},
{
"type": "assistant",
"timestamp": _ts(1),
"message": {
"content": [{"type": "text", "text": "hi"}],
"usage": {},
_write_jsonl(
p,
[
{"type": "user", "timestamp": _ts(0), "message": {"content": "hello"}},
{
"type": "assistant",
"timestamp": _ts(1),
"message": {
"content": [{"type": "text", "text": "hi"}],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.codeflash is None
@ -505,21 +557,26 @@ class TestExtractMetaCodeflash:
def test_detects_codeflash_agent_spawn(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{
"type": "tool_use",
"id": "t1",
"name": "Agent",
"input": {"name": "codeflash-python", "prompt": "optimize"},
}],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Agent",
"input": {"name": "codeflash-python", "prompt": "optimize"},
}
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.codeflash is not None
@ -530,21 +587,26 @@ class TestExtractMetaCodeflash:
def test_detects_codeflash_skill(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [{
"type": "tool_use",
"id": "t1",
"name": "Skill",
"input": {"skill": "codeflash-optimize"},
}],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Skill",
"input": {"skill": "codeflash-optimize"},
}
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.codeflash is not None
@ -553,19 +615,27 @@ class TestExtractMetaCodeflash:
def test_detects_team_creates(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "tool_use", "id": "t1", "name": "TeamCreate", "input": {}},
{"type": "tool_use", "id": "t2", "name": "Agent", "input": {"name": "codeflash-deep", "prompt": "go"}},
],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "tool_use", "id": "t1", "name": "TeamCreate", "input": {}},
{
"type": "tool_use",
"id": "t2",
"name": "Agent",
"input": {"name": "codeflash-deep", "prompt": "go"},
},
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
assert meta.codeflash is not None
@ -574,21 +644,44 @@ class TestExtractMetaCodeflash:
def test_detects_multiple_agents(self, tmp_path: Path) -> None:
p = tmp_path / "proj" / "sess.jsonl"
p.parent.mkdir()
_write_jsonl(p, [
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{"type": "tool_use", "id": "t1", "name": "Agent", "input": {"name": "codeflash-python", "prompt": "start"}},
{"type": "tool_use", "id": "t2", "name": "Agent", "input": {"name": "codeflash-deep", "prompt": "optimize"}},
{"type": "tool_use", "id": "t3", "name": "Agent", "input": {"name": "codeflash-researcher", "prompt": "research"}},
{"type": "tool_use", "id": "t4", "name": "Agent", "input": {"name": "codeflash-review", "prompt": "review"}},
],
"usage": {},
_write_jsonl(
p,
[
{
"type": "assistant",
"timestamp": _ts(0),
"message": {
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Agent",
"input": {"name": "codeflash-python", "prompt": "start"},
},
{
"type": "tool_use",
"id": "t2",
"name": "Agent",
"input": {"name": "codeflash-deep", "prompt": "optimize"},
},
{
"type": "tool_use",
"id": "t3",
"name": "Agent",
"input": {"name": "codeflash-researcher", "prompt": "research"},
},
{
"type": "tool_use",
"id": "t4",
"name": "Agent",
"input": {"name": "codeflash-review", "prompt": "review"},
},
],
"usage": {},
},
},
},
])
],
)
meta = extract_meta(p)
assert meta is not None
cf = meta.codeflash