trace2rules 0.1.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.
- trace2rules/__init__.py +1 -0
- trace2rules/cli.py +543 -0
- trace2rules/cluster.py +348 -0
- trace2rules/db.py +84 -0
- trace2rules/detectors.py +183 -0
- trace2rules/ingest.py +717 -0
- trace2rules/labels.py +431 -0
- trace2rules/models.py +33 -0
- trace2rules/patterns.py +159 -0
- trace2rules/sources/__init__.py +1 -0
- trace2rules/sources/agent_trajectory.py +456 -0
- trace2rules/sources/common.py +99 -0
- trace2rules/sources/opencode.py +154 -0
- trace2rules/sources/swe_chat.py +117 -0
- trace2rules/synthesize.py +538 -0
- trace2rules-0.1.0.dist-info/METADATA +323 -0
- trace2rules-0.1.0.dist-info/RECORD +20 -0
- trace2rules-0.1.0.dist-info/WHEEL +4 -0
- trace2rules-0.1.0.dist-info/entry_points.txt +3 -0
- trace2rules-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sqlite3
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from ..ingest import IngestStats, write_sessions
|
|
8
|
+
from ..models import Session, ToolCall
|
|
9
|
+
from .common import classify_tool_call, parse_arguments, target_from_args
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def find_database(source: str | Path) -> Path | None:
|
|
13
|
+
root = Path(source).expanduser()
|
|
14
|
+
if root.is_file() and root.suffix == ".db":
|
|
15
|
+
return root
|
|
16
|
+
if root.is_dir():
|
|
17
|
+
for name in ("opencode.db",):
|
|
18
|
+
candidate = root / name
|
|
19
|
+
if candidate.exists():
|
|
20
|
+
return candidate
|
|
21
|
+
candidates = sorted(root.glob("opencode*.db"))
|
|
22
|
+
if candidates:
|
|
23
|
+
return candidates[0]
|
|
24
|
+
default = Path.home() / ".local" / "share" / "opencode" / "opencode.db"
|
|
25
|
+
return default if default.exists() else None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _tables(con: sqlite3.Connection) -> set[str]:
|
|
29
|
+
return {row[0] for row in con.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _tool_call(data: dict) -> ToolCall | None:
|
|
33
|
+
name = data.get("tool") or data.get("name")
|
|
34
|
+
if not isinstance(name, str) or not name:
|
|
35
|
+
return None
|
|
36
|
+
state = data.get("state") or {}
|
|
37
|
+
args = parse_arguments(state.get("input") or data.get("input") or {})
|
|
38
|
+
raw_target = target_from_args(name, args, name)
|
|
39
|
+
action, target, verb, object_id = classify_tool_call(name, args, raw_target)
|
|
40
|
+
output = state.get("output") or state.get("error") or ""
|
|
41
|
+
return ToolCall(
|
|
42
|
+
tool_name=name,
|
|
43
|
+
raw_target=raw_target,
|
|
44
|
+
observation=str(output),
|
|
45
|
+
action_type=action,
|
|
46
|
+
target=target,
|
|
47
|
+
verb=verb,
|
|
48
|
+
object_id=object_id,
|
|
49
|
+
is_error=state.get("status") == "error",
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _v1_calls(con: sqlite3.Connection, session_id: str) -> list[ToolCall]:
|
|
54
|
+
rows = con.execute(
|
|
55
|
+
"""
|
|
56
|
+
SELECT m.data AS message_data, p.ordinal, p.data AS part_data
|
|
57
|
+
FROM message m
|
|
58
|
+
LEFT JOIN part p ON p.message_id = m.id
|
|
59
|
+
WHERE m.session_id = ?
|
|
60
|
+
ORDER BY m.time_created, p.ordinal
|
|
61
|
+
""",
|
|
62
|
+
(session_id,),
|
|
63
|
+
).fetchall()
|
|
64
|
+
calls: list[ToolCall] = []
|
|
65
|
+
segment = 0
|
|
66
|
+
for _message, _ordinal, part_data in rows:
|
|
67
|
+
if not part_data:
|
|
68
|
+
continue
|
|
69
|
+
try:
|
|
70
|
+
data = json.loads(part_data)
|
|
71
|
+
except json.JSONDecodeError:
|
|
72
|
+
continue
|
|
73
|
+
if data.get("type") == "compaction":
|
|
74
|
+
segment += 1
|
|
75
|
+
continue
|
|
76
|
+
if data.get("type") != "tool":
|
|
77
|
+
continue
|
|
78
|
+
call = _tool_call(data)
|
|
79
|
+
if call is not None:
|
|
80
|
+
call.segment_idx = segment
|
|
81
|
+
calls.append(call)
|
|
82
|
+
return calls
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _v2_calls(con: sqlite3.Connection, session_id: str) -> list[ToolCall]:
|
|
86
|
+
rows = con.execute(
|
|
87
|
+
"SELECT data FROM session_message WHERE session_id = ? ORDER BY seq",
|
|
88
|
+
(session_id,),
|
|
89
|
+
).fetchall()
|
|
90
|
+
calls: list[ToolCall] = []
|
|
91
|
+
segment = 0
|
|
92
|
+
for (message_data,) in rows:
|
|
93
|
+
try:
|
|
94
|
+
data = json.loads(message_data)
|
|
95
|
+
except json.JSONDecodeError:
|
|
96
|
+
continue
|
|
97
|
+
content = data.get("content") if isinstance(data, dict) else None
|
|
98
|
+
if not isinstance(content, list):
|
|
99
|
+
continue
|
|
100
|
+
for part in content:
|
|
101
|
+
if not isinstance(part, dict):
|
|
102
|
+
continue
|
|
103
|
+
if part.get("type") == "compaction":
|
|
104
|
+
segment += 1
|
|
105
|
+
continue
|
|
106
|
+
if part.get("type") != "tool":
|
|
107
|
+
continue
|
|
108
|
+
call = _tool_call(part)
|
|
109
|
+
if call is not None:
|
|
110
|
+
call.segment_idx = segment
|
|
111
|
+
calls.append(call)
|
|
112
|
+
return calls
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def ingest_opencode(
|
|
116
|
+
source: str | Path,
|
|
117
|
+
db_path: str | Path,
|
|
118
|
+
*,
|
|
119
|
+
limit: int | None = None,
|
|
120
|
+
repo: str | None = None,
|
|
121
|
+
) -> list[IngestStats]:
|
|
122
|
+
path = find_database(source)
|
|
123
|
+
if path is None or not path.exists():
|
|
124
|
+
raise FileNotFoundError(f"OpenCode database not found under {source}")
|
|
125
|
+
con = sqlite3.connect(f"file:{path}?mode=ro", uri=True)
|
|
126
|
+
con.row_factory = sqlite3.Row
|
|
127
|
+
try:
|
|
128
|
+
tables = _tables(con)
|
|
129
|
+
v2 = "session_v2" in tables
|
|
130
|
+
session_table = "session_v2" if v2 else "session"
|
|
131
|
+
rows = con.execute(f"SELECT * FROM {session_table}").fetchall()
|
|
132
|
+
if limit:
|
|
133
|
+
rows = rows[:limit]
|
|
134
|
+
sessions: list[Session] = []
|
|
135
|
+
for row in rows:
|
|
136
|
+
session_id = str(row["id"])
|
|
137
|
+
parent_id = str(row["parent_id"] or "") if "parent_id" in row.keys() else ""
|
|
138
|
+
directory = str(row["directory"] or "") if "directory" in row.keys() else ""
|
|
139
|
+
title = str(row["title"] or "") if "title" in row.keys() else ""
|
|
140
|
+
calls = _v2_calls(con, session_id) if v2 else _v1_calls(con, session_id)
|
|
141
|
+
sessions.append(
|
|
142
|
+
Session(
|
|
143
|
+
session_id=session_id,
|
|
144
|
+
repo=repo or (Path(directory).name if directory else "unknown"),
|
|
145
|
+
instance_id=parent_id or session_id,
|
|
146
|
+
source="opencode",
|
|
147
|
+
agent="opencode",
|
|
148
|
+
calls=calls,
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
stats = write_sessions(db_path, sessions)
|
|
152
|
+
return [IngestStats(f"opencode:{title}", len(rows), stats.sessions, stats.events)]
|
|
153
|
+
finally:
|
|
154
|
+
con.close()
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import duckdb
|
|
6
|
+
|
|
7
|
+
from ..models import Session, ToolCall
|
|
8
|
+
from .common import classify_tool_call, parse_arguments, target_from_args
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _paths(source: str | Path) -> tuple[Path, Path]:
|
|
12
|
+
root = Path(source).expanduser()
|
|
13
|
+
if root.is_file():
|
|
14
|
+
sessions = root
|
|
15
|
+
else:
|
|
16
|
+
sessions = root / "sessions.parquet"
|
|
17
|
+
conversations = sessions.parent / "conversations.parquet"
|
|
18
|
+
return sessions, conversations
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ingest_swe_chat(
|
|
22
|
+
source: str | Path,
|
|
23
|
+
db_path: str | Path,
|
|
24
|
+
*,
|
|
25
|
+
limit: int | None = None,
|
|
26
|
+
repo: str | None = None,
|
|
27
|
+
):
|
|
28
|
+
"""Import the normalized SALT-NLP/SWE-chat parquet tables."""
|
|
29
|
+
from ..ingest import IngestStats, write_sessions
|
|
30
|
+
|
|
31
|
+
sessions_path, conversations_path = _paths(source)
|
|
32
|
+
if not sessions_path.exists() or not conversations_path.exists():
|
|
33
|
+
raise FileNotFoundError(
|
|
34
|
+
f"Expected sessions.parquet and conversations.parquet under {sessions_path.parent}"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
con = duckdb.connect()
|
|
38
|
+
try:
|
|
39
|
+
limit_sql = f"LIMIT {int(limit)}" if limit else ""
|
|
40
|
+
con.execute(
|
|
41
|
+
f"""
|
|
42
|
+
CREATE TEMP TABLE selected AS
|
|
43
|
+
SELECT session_id, repo_id, agent, session_success
|
|
44
|
+
FROM read_parquet(?)
|
|
45
|
+
ORDER BY repo_id, session_id
|
|
46
|
+
{limit_sql}
|
|
47
|
+
""",
|
|
48
|
+
[str(sessions_path)],
|
|
49
|
+
)
|
|
50
|
+
meta = {
|
|
51
|
+
row[0]: row
|
|
52
|
+
for row in con.execute(
|
|
53
|
+
"SELECT session_id, repo_id, agent, session_success FROM selected"
|
|
54
|
+
).fetchall()
|
|
55
|
+
}
|
|
56
|
+
sessions: dict[str, Session] = {}
|
|
57
|
+
calls: dict[str, list[ToolCall]] = {}
|
|
58
|
+
pending: dict[tuple[str, str], ToolCall] = {}
|
|
59
|
+
cursor = con.execute(
|
|
60
|
+
"""
|
|
61
|
+
SELECT c.session_id, c.turn_type, c.tool_name, c.tool_call_id,
|
|
62
|
+
c.tool_input_json, c.command, c.file_path, c.content
|
|
63
|
+
FROM read_parquet(?) c
|
|
64
|
+
JOIN selected s USING (session_id)
|
|
65
|
+
WHERE c.turn_type IN ('tool_use', 'tool_result')
|
|
66
|
+
ORDER BY c.session_id, c.turn_number
|
|
67
|
+
""",
|
|
68
|
+
[str(conversations_path)],
|
|
69
|
+
)
|
|
70
|
+
while True:
|
|
71
|
+
batch = cursor.fetchmany(10_000)
|
|
72
|
+
if not batch:
|
|
73
|
+
break
|
|
74
|
+
for session_id, turn_type, tool_name, tool_call_id, input_json, command, path, content in batch:
|
|
75
|
+
if session_id not in sessions:
|
|
76
|
+
_, repo_id, agent, success = meta.get(session_id, (session_id, repo or "unknown", "", None))
|
|
77
|
+
sessions[session_id] = Session(
|
|
78
|
+
session_id=session_id,
|
|
79
|
+
repo=repo or repo_id or "unknown",
|
|
80
|
+
instance_id=session_id,
|
|
81
|
+
agent=agent or "",
|
|
82
|
+
source="swe-chat",
|
|
83
|
+
success_score=float(success) if success not in (None, "") else None,
|
|
84
|
+
)
|
|
85
|
+
calls[session_id] = []
|
|
86
|
+
if turn_type == "tool_use":
|
|
87
|
+
args = parse_arguments(input_json)
|
|
88
|
+
raw_target = (
|
|
89
|
+
(command or "").strip()
|
|
90
|
+
or (path or "").strip()
|
|
91
|
+
or target_from_args(tool_name or "", args, input_json or tool_name or "")
|
|
92
|
+
)
|
|
93
|
+
action, target, verb, object_id = classify_tool_call(
|
|
94
|
+
tool_name or "", args, raw_target
|
|
95
|
+
)
|
|
96
|
+
call = ToolCall(
|
|
97
|
+
tool_name=tool_name or "",
|
|
98
|
+
raw_target=raw_target,
|
|
99
|
+
action_type=action,
|
|
100
|
+
target=target,
|
|
101
|
+
verb=verb,
|
|
102
|
+
object_id=object_id,
|
|
103
|
+
)
|
|
104
|
+
calls[session_id].append(call)
|
|
105
|
+
if tool_call_id:
|
|
106
|
+
pending[(session_id, tool_call_id)] = call
|
|
107
|
+
elif tool_call_id:
|
|
108
|
+
call = pending.pop((session_id, tool_call_id), None)
|
|
109
|
+
if call is not None:
|
|
110
|
+
call.observation = content or ""
|
|
111
|
+
for session in sessions.values():
|
|
112
|
+
session.calls = calls.get(session.session_id, [])
|
|
113
|
+
write_sessions(db_path, sessions.values())
|
|
114
|
+
events = sum(len(session.calls) for session in sessions.values())
|
|
115
|
+
return IngestStats("swe-chat", len(sessions), len(sessions), events)
|
|
116
|
+
finally:
|
|
117
|
+
con.close()
|