trace2rules 0.1.0__tar.gz → 0.1.2__tar.gz
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-0.1.0 → trace2rules-0.1.2}/PKG-INFO +11 -5
- {trace2rules-0.1.0 → trace2rules-0.1.2}/README.md +10 -4
- {trace2rules-0.1.0 → trace2rules-0.1.2}/pyproject.toml +1 -1
- {trace2rules-0.1.0 → trace2rules-0.1.2}/pyproject.toml.orig +1 -1
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/cli.py +196 -34
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/detectors.py +2 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/ingest.py +45 -30
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/labels.py +3 -2
- trace2rules-0.1.2/src/trace2rules/redact.py +67 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/sources/agent_trajectory.py +152 -11
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/synthesize.py +160 -51
- {trace2rules-0.1.0 → trace2rules-0.1.2}/LICENSE +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/__init__.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/cluster.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/db.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/models.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/patterns.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/sources/__init__.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/sources/common.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/sources/opencode.py +0 -0
- {trace2rules-0.1.0 → trace2rules-0.1.2}/src/trace2rules/sources/swe_chat.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trace2rules
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Mine coding-agent trajectories into evidence-backed AGENTS.md rules
|
|
5
5
|
Keywords: agents,coding-agents,agents.md,trajectories,duckdb
|
|
6
6
|
Author: Rakha Kanz Kautsar
|
|
@@ -76,7 +76,7 @@ Rules are capped at 5 rules, 60 lines, and 2,500 characters per repo.
|
|
|
76
76
|
|
|
77
77
|
## Synthesis
|
|
78
78
|
|
|
79
|
-
Synthesis is LLM-first. `trace2rules synthesize` sends
|
|
79
|
+
Synthesis is LLM-first. `trace2rules synthesize` sends redacted gated findings for each repo to an OpenAI-compatible chat model and asks for structured rules. Common API keys, bearer tokens, URL credentials, query tokens, password assignments, and private keys are redacted before external calls; redaction is best-effort, not a guarantee.
|
|
80
80
|
|
|
81
81
|
Provider selection:
|
|
82
82
|
|
|
@@ -119,7 +119,7 @@ uv sync
|
|
|
119
119
|
|
|
120
120
|
## Quick start: your own sessions
|
|
121
121
|
|
|
122
|
-
Analyze the sessions already on your machine.
|
|
122
|
+
Analyze the sessions already on your machine.
|
|
123
123
|
|
|
124
124
|
```bash
|
|
125
125
|
uv tool install trace2rules
|
|
@@ -128,6 +128,10 @@ trace2rules mine
|
|
|
128
128
|
trace2rules synthesize
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
+
Or hand the workflow to your coding agent:
|
|
132
|
+
|
|
133
|
+
> Read the `trace2rules` README, install it with `uv tool install trace2rules` if it is missing, then ingest my local agent sessions. Use `trace2rules report` to surface recurring friction with evidence and synthesize repo-specific `AGENTS.md` rules from that JSON.
|
|
134
|
+
|
|
131
135
|
`--source` is repeatable. `--source ~` scans all standard local stores: `~/.claude/projects`, `~/.codex/sessions`, `~/.pi/agent/sessions`, and `~/.omp/agent/sessions`. The example imports 50 sessions per source; use `--limit 0` for all.
|
|
132
136
|
|
|
133
137
|
LLM synthesis is the default. Add `--offline` if you do not have an API key.
|
|
@@ -200,6 +204,7 @@ Use `--repo owner/name` to override the repository label for local sessions.
|
|
|
200
204
|
| `ingest` | Load sessions and tool events into DuckDB |
|
|
201
205
|
| `mine` | Show friction clusters, hotspots, recovery loops, and context blasts |
|
|
202
206
|
| `report` | Emit all findings and patterns as evidence-rich JSON for an agent |
|
|
207
|
+
| `doctor` | Check install, Node, credentials, source stores, and database schema |
|
|
203
208
|
| `label` | Optional: classify targets with a decision model (`intent`, `object_type`, confidence) |
|
|
204
209
|
| `patterns` | Mine structural multi-step friction; labels refine when available |
|
|
205
210
|
| `exact` | Show exact recurring sequences with evidence |
|
|
@@ -214,9 +219,10 @@ trace2rules ingest --format swe-rebench # opt into the public dataset
|
|
|
214
219
|
trace2rules ingest --source PATH --limit 200
|
|
215
220
|
trace2rules synthesize
|
|
216
221
|
trace2rules synthesize --offline
|
|
222
|
+
trace2rules synthesize --max-rules 10
|
|
223
|
+
trace2rules synthesize --out-dir ./proposals
|
|
217
224
|
trace2rules synthesize --repo owner/name --out AGENTS.proposed.md
|
|
218
225
|
trace2rules synthesize --repo owner/name --out -
|
|
219
|
-
trace2rules synthesize --out-dir ./proposals
|
|
220
226
|
```
|
|
221
227
|
|
|
222
228
|
`--label` auto-detects an authenticated `wrangler` first, then `GEMINI_API_KEY`/`GOOGLE_API_KEY`, then `OPENAI_API_KEY`. It is opt-in because a full local run can have thousands of unique targets.
|
|
@@ -269,7 +275,7 @@ trace2rules patterns --min-confidence 0.4
|
|
|
269
275
|
|
|
270
276
|
Cloudflare models authenticate through `wrangler auth token --json` and `wrangler whoami --json`; the first account is used. Gemini and OpenAI use their standard keys. Labeling defaults to 270 requests/minute with 6 workers and retries 429/5xx responses.
|
|
271
277
|
|
|
272
|
-
`label` sends the tool name, action type, normalized target, and a raw command example to the selected provider. Do not run
|
|
278
|
+
`label` sends the tool name, action type, redacted normalized target, and a redacted raw command example to the selected provider. The local DuckDB and `report` output remain unredacted. Do not run external labeling or synthesis on private sessions without reviewing the redaction and provider policy.
|
|
273
279
|
|
|
274
280
|
## Configuration
|
|
275
281
|
|
|
@@ -47,7 +47,7 @@ Rules are capped at 5 rules, 60 lines, and 2,500 characters per repo.
|
|
|
47
47
|
|
|
48
48
|
## Synthesis
|
|
49
49
|
|
|
50
|
-
Synthesis is LLM-first. `trace2rules synthesize` sends
|
|
50
|
+
Synthesis is LLM-first. `trace2rules synthesize` sends redacted gated findings for each repo to an OpenAI-compatible chat model and asks for structured rules. Common API keys, bearer tokens, URL credentials, query tokens, password assignments, and private keys are redacted before external calls; redaction is best-effort, not a guarantee.
|
|
51
51
|
|
|
52
52
|
Provider selection:
|
|
53
53
|
|
|
@@ -90,7 +90,7 @@ uv sync
|
|
|
90
90
|
|
|
91
91
|
## Quick start: your own sessions
|
|
92
92
|
|
|
93
|
-
Analyze the sessions already on your machine.
|
|
93
|
+
Analyze the sessions already on your machine.
|
|
94
94
|
|
|
95
95
|
```bash
|
|
96
96
|
uv tool install trace2rules
|
|
@@ -99,6 +99,10 @@ trace2rules mine
|
|
|
99
99
|
trace2rules synthesize
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
+
Or hand the workflow to your coding agent:
|
|
103
|
+
|
|
104
|
+
> Read the `trace2rules` README, install it with `uv tool install trace2rules` if it is missing, then ingest my local agent sessions. Use `trace2rules report` to surface recurring friction with evidence and synthesize repo-specific `AGENTS.md` rules from that JSON.
|
|
105
|
+
|
|
102
106
|
`--source` is repeatable. `--source ~` scans all standard local stores: `~/.claude/projects`, `~/.codex/sessions`, `~/.pi/agent/sessions`, and `~/.omp/agent/sessions`. The example imports 50 sessions per source; use `--limit 0` for all.
|
|
103
107
|
|
|
104
108
|
LLM synthesis is the default. Add `--offline` if you do not have an API key.
|
|
@@ -171,6 +175,7 @@ Use `--repo owner/name` to override the repository label for local sessions.
|
|
|
171
175
|
| `ingest` | Load sessions and tool events into DuckDB |
|
|
172
176
|
| `mine` | Show friction clusters, hotspots, recovery loops, and context blasts |
|
|
173
177
|
| `report` | Emit all findings and patterns as evidence-rich JSON for an agent |
|
|
178
|
+
| `doctor` | Check install, Node, credentials, source stores, and database schema |
|
|
174
179
|
| `label` | Optional: classify targets with a decision model (`intent`, `object_type`, confidence) |
|
|
175
180
|
| `patterns` | Mine structural multi-step friction; labels refine when available |
|
|
176
181
|
| `exact` | Show exact recurring sequences with evidence |
|
|
@@ -185,9 +190,10 @@ trace2rules ingest --format swe-rebench # opt into the public dataset
|
|
|
185
190
|
trace2rules ingest --source PATH --limit 200
|
|
186
191
|
trace2rules synthesize
|
|
187
192
|
trace2rules synthesize --offline
|
|
193
|
+
trace2rules synthesize --max-rules 10
|
|
194
|
+
trace2rules synthesize --out-dir ./proposals
|
|
188
195
|
trace2rules synthesize --repo owner/name --out AGENTS.proposed.md
|
|
189
196
|
trace2rules synthesize --repo owner/name --out -
|
|
190
|
-
trace2rules synthesize --out-dir ./proposals
|
|
191
197
|
```
|
|
192
198
|
|
|
193
199
|
`--label` auto-detects an authenticated `wrangler` first, then `GEMINI_API_KEY`/`GOOGLE_API_KEY`, then `OPENAI_API_KEY`. It is opt-in because a full local run can have thousands of unique targets.
|
|
@@ -240,7 +246,7 @@ trace2rules patterns --min-confidence 0.4
|
|
|
240
246
|
|
|
241
247
|
Cloudflare models authenticate through `wrangler auth token --json` and `wrangler whoami --json`; the first account is used. Gemini and OpenAI use their standard keys. Labeling defaults to 270 requests/minute with 6 workers and retries 429/5xx responses.
|
|
242
248
|
|
|
243
|
-
`label` sends the tool name, action type, normalized target, and a raw command example to the selected provider. Do not run
|
|
249
|
+
`label` sends the tool name, action type, redacted normalized target, and a redacted raw command example to the selected provider. The local DuckDB and `report` output remain unredacted. Do not run external labeling or synthesis on private sessions without reviewing the redaction and provider policy.
|
|
244
250
|
|
|
245
251
|
## Configuration
|
|
246
252
|
|
|
@@ -2,8 +2,13 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
import os
|
|
5
|
+
import platform
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
5
9
|
from dataclasses import asdict
|
|
6
10
|
from datetime import datetime, timezone
|
|
11
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
7
12
|
from pathlib import Path
|
|
8
13
|
from typing import Annotated
|
|
9
14
|
|
|
@@ -12,14 +17,13 @@ import typer
|
|
|
12
17
|
from dotenv import load_dotenv
|
|
13
18
|
from rich.console import Console
|
|
14
19
|
from rich.markup import escape
|
|
20
|
+
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
|
|
15
21
|
from rich.table import Table
|
|
16
22
|
|
|
17
|
-
from .cluster import exact_sequences, mine as mine_friction
|
|
18
23
|
from .db import DB_PATH, connect
|
|
19
24
|
from .ingest import DEFAULT_REPOS, SOURCE, ingest as ingest_traces
|
|
20
25
|
from .labels import label_targets
|
|
21
26
|
from .patterns import mine_sequences
|
|
22
|
-
from .synthesize import EXACT_LENGTHS, synthesize as synthesize_rules
|
|
23
27
|
|
|
24
28
|
load_dotenv(Path.cwd() / ".env")
|
|
25
29
|
|
|
@@ -64,59 +68,69 @@ def _ingest_command(
|
|
|
64
68
|
) -> None:
|
|
65
69
|
fmt = format.strip().lower()
|
|
66
70
|
sources = source or []
|
|
71
|
+
failed_sources: list[tuple[str, Exception]] = []
|
|
67
72
|
if repos and fmt != "swe-rebench":
|
|
68
73
|
raise typer.BadParameter("--repos is only valid with --format swe-rebench.")
|
|
69
74
|
if fmt == "swe-rebench":
|
|
70
75
|
prefixes = repos or DEFAULT_REPOS
|
|
71
76
|
selected_sources = sources or [SOURCE]
|
|
72
77
|
stats = []
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
for selected_source in selected_sources:
|
|
79
|
+
if selected_source.startswith(("hf://", "http://", "https://")):
|
|
80
|
+
console.print("[dim]Scanning remote parquet; first run downloads metadata/data...[/dim]")
|
|
81
|
+
try:
|
|
77
82
|
stats.extend(
|
|
78
83
|
ingest_traces(
|
|
79
84
|
prefixes, limit=limit, db_path=db, source=selected_source
|
|
80
85
|
)
|
|
81
86
|
)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
raise typer.Exit(1) from None
|
|
87
|
+
except (duckdb.Error, OSError, RuntimeError) as error:
|
|
88
|
+
failed_sources.append((selected_source, error))
|
|
85
89
|
elif fmt == "swe-chat":
|
|
86
90
|
from .sources.swe_chat import ingest_swe_chat
|
|
87
91
|
|
|
88
92
|
selected_sources = sources or ["data/swe-chat"]
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
stats = []
|
|
94
|
+
for selected_source in selected_sources:
|
|
95
|
+
try:
|
|
96
|
+
stats.append(
|
|
97
|
+
ingest_swe_chat(
|
|
98
|
+
selected_source,
|
|
99
|
+
db,
|
|
100
|
+
limit=limit or None,
|
|
101
|
+
repo=repo,
|
|
102
|
+
)
|
|
96
103
|
)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
except (FileNotFoundError, ValueError, RuntimeError) as error:
|
|
100
|
-
console.print(f"[red]{escape(str(error))}[/red]")
|
|
101
|
-
raise typer.Exit(1) from None
|
|
104
|
+
except (FileNotFoundError, ValueError, RuntimeError) as error:
|
|
105
|
+
failed_sources.append((selected_source, error))
|
|
102
106
|
elif fmt in {"agent-trajectory", "agent"}:
|
|
103
107
|
from .sources.agent_trajectory import ingest_agent_sessions
|
|
104
108
|
|
|
105
109
|
selected_sources = sources or [str(Path.home())]
|
|
106
110
|
stats = []
|
|
107
|
-
|
|
111
|
+
progress = Progress(
|
|
112
|
+
SpinnerColumn(),
|
|
113
|
+
TextColumn("[progress.description]{task.description}"),
|
|
114
|
+
BarColumn(),
|
|
115
|
+
TextColumn("{task.completed}/{task.total}"),
|
|
116
|
+
TimeElapsedColumn(),
|
|
117
|
+
console=Console(stderr=True),
|
|
118
|
+
transient=False,
|
|
119
|
+
)
|
|
120
|
+
with progress:
|
|
108
121
|
for selected_source in selected_sources:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
try:
|
|
123
|
+
stats.extend(
|
|
124
|
+
ingest_agent_sessions(
|
|
125
|
+
selected_source,
|
|
126
|
+
db,
|
|
127
|
+
limit=limit or None,
|
|
128
|
+
repo=repo,
|
|
129
|
+
progress=progress,
|
|
130
|
+
)
|
|
115
131
|
)
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
console.print(f"[red]{escape(str(error))}[/red]")
|
|
119
|
-
raise typer.Exit(1) from None
|
|
132
|
+
except (FileNotFoundError, ValueError, RuntimeError) as error:
|
|
133
|
+
failed_sources.append((selected_source, error))
|
|
120
134
|
else:
|
|
121
135
|
raise typer.BadParameter(
|
|
122
136
|
"Unknown format. Use swe-rebench, swe-chat, or agent-trajectory."
|
|
@@ -136,6 +150,12 @@ def _ingest_command(
|
|
|
136
150
|
for stat in stats:
|
|
137
151
|
if stat.matched == 0:
|
|
138
152
|
console.print(f"[yellow]No sessions found for {stat.prefix!r}.[/yellow]")
|
|
153
|
+
for failed_source, error in failed_sources:
|
|
154
|
+
console.print(
|
|
155
|
+
f"[red]Failed {escape(str(failed_source))}: {escape(str(error))}[/red]"
|
|
156
|
+
)
|
|
157
|
+
if failed_sources and not stats:
|
|
158
|
+
raise typer.Exit(1)
|
|
139
159
|
if label:
|
|
140
160
|
from .labels import detect_model
|
|
141
161
|
|
|
@@ -281,6 +301,7 @@ def _mine_command(
|
|
|
281
301
|
console.print("[red]No sessions in the database. Run `trace2rules ingest` first.[/red]")
|
|
282
302
|
raise typer.Exit(1)
|
|
283
303
|
from .detectors import session_summary
|
|
304
|
+
from .cluster import mine as mine_friction
|
|
284
305
|
|
|
285
306
|
_print_summary(session_summary(con))
|
|
286
307
|
mined = mine_friction(con, top=top)
|
|
@@ -318,6 +339,9 @@ def _report_command(
|
|
|
318
339
|
"SELECT repo, count(*) FROM sessions GROUP BY repo ORDER BY repo"
|
|
319
340
|
).fetchall()
|
|
320
341
|
|
|
342
|
+
from .cluster import exact_sequences, mine as mine_friction
|
|
343
|
+
from .synthesize import EXACT_LENGTHS
|
|
344
|
+
|
|
321
345
|
mined = mine_friction(con, top=top)
|
|
322
346
|
findings = [
|
|
323
347
|
finding
|
|
@@ -368,6 +392,127 @@ def _report_command(
|
|
|
368
392
|
app.command("report")(_report_command)
|
|
369
393
|
|
|
370
394
|
|
|
395
|
+
def _doctor_command(
|
|
396
|
+
db: Annotated[Path, typer.Option(help="DuckDB database path.")] = DB_PATH,
|
|
397
|
+
) -> None:
|
|
398
|
+
checks: list[tuple[str, str, str]] = []
|
|
399
|
+
|
|
400
|
+
py_ok = sys.version_info >= (3, 12)
|
|
401
|
+
checks.append(("Python", "ok" if py_ok else "fail", platform.python_version()))
|
|
402
|
+
|
|
403
|
+
try:
|
|
404
|
+
package_version = version("trace2rules")
|
|
405
|
+
checks.append(("trace2rules", "ok", package_version))
|
|
406
|
+
except PackageNotFoundError:
|
|
407
|
+
checks.append(("trace2rules", "warn", "not installed as a package"))
|
|
408
|
+
|
|
409
|
+
node = shutil.which("node")
|
|
410
|
+
if not node:
|
|
411
|
+
checks.append(("Node.js", "warn", "not found; non-OpenCode local sources need it"))
|
|
412
|
+
else:
|
|
413
|
+
try:
|
|
414
|
+
node_version = subprocess.run(
|
|
415
|
+
[node, "--version"],
|
|
416
|
+
capture_output=True,
|
|
417
|
+
text=True,
|
|
418
|
+
timeout=5,
|
|
419
|
+
check=False,
|
|
420
|
+
).stdout.strip()
|
|
421
|
+
except OSError:
|
|
422
|
+
node_version = "unavailable"
|
|
423
|
+
checks.append(("Node.js", "ok", node_version or node))
|
|
424
|
+
|
|
425
|
+
try:
|
|
426
|
+
from .labels import _wrangler_ready, detect_model
|
|
427
|
+
|
|
428
|
+
wrangler_ready = _wrangler_ready()
|
|
429
|
+
checks.append(
|
|
430
|
+
(
|
|
431
|
+
"wrangler",
|
|
432
|
+
"ok" if wrangler_ready else "warn",
|
|
433
|
+
"authenticated" if wrangler_ready else "not authenticated",
|
|
434
|
+
)
|
|
435
|
+
)
|
|
436
|
+
decision_model = detect_model()
|
|
437
|
+
checks.append(
|
|
438
|
+
(
|
|
439
|
+
"decision backend",
|
|
440
|
+
"ok" if decision_model else "warn",
|
|
441
|
+
decision_model or "none detected",
|
|
442
|
+
)
|
|
443
|
+
)
|
|
444
|
+
except Exception as error: # noqa: BLE001 - doctor must not fail on probes
|
|
445
|
+
checks.append(("decision backend", "warn", str(error)))
|
|
446
|
+
|
|
447
|
+
for key in ("TRACE2RULES_LLM_API_KEY", "GEMINI_API_KEY", "OPENAI_API_KEY"):
|
|
448
|
+
checks.append(
|
|
449
|
+
(key, "ok" if os.getenv(key) else "warn", "set" if os.getenv(key) else "unset")
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
from .sources.agent_trajectory import _source_roots
|
|
453
|
+
|
|
454
|
+
try:
|
|
455
|
+
roots = _source_roots(Path.home())
|
|
456
|
+
detail = ", ".join(f"{name}:{path}" for name, path in roots) or "none found"
|
|
457
|
+
checks.append(
|
|
458
|
+
("local sources", "ok" if roots else "warn", detail)
|
|
459
|
+
)
|
|
460
|
+
except OSError as error:
|
|
461
|
+
checks.append(("local sources", "warn", str(error)))
|
|
462
|
+
|
|
463
|
+
parent = Path(db).parent
|
|
464
|
+
try:
|
|
465
|
+
parent.mkdir(parents=True, exist_ok=True)
|
|
466
|
+
writable = os.access(parent, os.W_OK)
|
|
467
|
+
except OSError:
|
|
468
|
+
writable = False
|
|
469
|
+
checks.append(
|
|
470
|
+
("database directory", "ok" if writable else "fail", str(parent))
|
|
471
|
+
)
|
|
472
|
+
if Path(db).exists():
|
|
473
|
+
try:
|
|
474
|
+
con = duckdb.connect(str(db), read_only=True)
|
|
475
|
+
try:
|
|
476
|
+
tables = {
|
|
477
|
+
row[0]
|
|
478
|
+
for row in con.execute(
|
|
479
|
+
"SELECT table_name FROM information_schema.tables"
|
|
480
|
+
).fetchall()
|
|
481
|
+
}
|
|
482
|
+
columns = {
|
|
483
|
+
row[1]
|
|
484
|
+
for row in con.execute("PRAGMA table_info('tool_events')").fetchall()
|
|
485
|
+
}
|
|
486
|
+
if "tool_events" not in tables:
|
|
487
|
+
status, detail = "fail", "missing tool_events"
|
|
488
|
+
elif "segment_idx" not in columns:
|
|
489
|
+
status, detail = "warn", "schema needs re-ingest/migration"
|
|
490
|
+
else:
|
|
491
|
+
status, detail = "ok", "schema ready"
|
|
492
|
+
checks.append(("database", status, detail))
|
|
493
|
+
finally:
|
|
494
|
+
con.close()
|
|
495
|
+
except Exception as error: # noqa: BLE001 - report lock/permission issues
|
|
496
|
+
checks.append(("database", "warn", str(error)))
|
|
497
|
+
else:
|
|
498
|
+
checks.append(("database", "warn", f"not created yet: {db}"))
|
|
499
|
+
|
|
500
|
+
table = Table(title="trace2rules doctor", header_style="bold")
|
|
501
|
+
table.add_column("Check")
|
|
502
|
+
table.add_column("Status")
|
|
503
|
+
table.add_column("Detail", overflow="fold")
|
|
504
|
+
for check, status, detail in checks:
|
|
505
|
+
style = {"ok": "green", "warn": "yellow", "fail": "red"}.get(status, "")
|
|
506
|
+
table.add_row(check, f"[{style}]{status}[/{style}]", escape(detail))
|
|
507
|
+
console.print(table)
|
|
508
|
+
|
|
509
|
+
if not py_ok or not writable:
|
|
510
|
+
raise typer.Exit(1)
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
app.command("doctor")(_doctor_command)
|
|
514
|
+
|
|
515
|
+
|
|
371
516
|
def _label_command(
|
|
372
517
|
limit: Annotated[int, typer.Option(help="Max pending targets; 0 = all.")] = 0,
|
|
373
518
|
rpm: Annotated[int, typer.Option(help="Requests per minute cap.")] = 270,
|
|
@@ -456,6 +601,8 @@ def _exact_command(
|
|
|
456
601
|
) -> None:
|
|
457
602
|
con = connect(db)
|
|
458
603
|
try:
|
|
604
|
+
from .cluster import exact_sequences
|
|
605
|
+
|
|
459
606
|
findings = exact_sequences(
|
|
460
607
|
con,
|
|
461
608
|
length=length,
|
|
@@ -500,15 +647,27 @@ app.command("exact")(_exact_command)
|
|
|
500
647
|
|
|
501
648
|
def _synthesize_command(
|
|
502
649
|
out: Annotated[Path | None, typer.Option("--out", "-o", help="Output path for a single --repo; use '-' for stdout.")] = None,
|
|
503
|
-
out_dir: Annotated[
|
|
650
|
+
out_dir: Annotated[
|
|
651
|
+
Path | None,
|
|
652
|
+
typer.Option("--out-dir", help="Directory for per-repo AGENTS files."),
|
|
653
|
+
] = None,
|
|
504
654
|
repo: Annotated[str | None, typer.Option("--repo", help="Synthesize only this repo.")] = None,
|
|
505
655
|
top: Annotated[int, typer.Option(help="Findings to pass to synthesis.")] = 25,
|
|
656
|
+
max_rules: Annotated[
|
|
657
|
+
int,
|
|
658
|
+
typer.Option("--max-rules", min=1, max=20, help="Maximum rules per repo."),
|
|
659
|
+
] = 5,
|
|
506
660
|
offline: Annotated[bool, typer.Option("--offline", help="Skip the LLM and use deterministic rules.")] = False,
|
|
507
661
|
db: Annotated[Path, typer.Option(help="DuckDB database path.")] = DB_PATH,
|
|
508
662
|
) -> None:
|
|
509
663
|
if out is not None and repo is None:
|
|
510
664
|
raise typer.BadParameter("--out requires --repo; use --out-dir for multiple repos.")
|
|
511
|
-
|
|
665
|
+
stdout_mode = out is None and out_dir is None
|
|
666
|
+
cmd_console = (
|
|
667
|
+
Console(stderr=True)
|
|
668
|
+
if stdout_mode or (out is not None and str(out) == "-")
|
|
669
|
+
else console
|
|
670
|
+
)
|
|
512
671
|
con = connect(db)
|
|
513
672
|
try:
|
|
514
673
|
session_count = con.execute("SELECT count(*) FROM sessions").fetchone()[0]
|
|
@@ -516,12 +675,15 @@ def _synthesize_command(
|
|
|
516
675
|
cmd_console.print("[red]No sessions in the database. Run `trace2rules ingest` first.[/red]")
|
|
517
676
|
raise typer.Exit(1)
|
|
518
677
|
try:
|
|
678
|
+
from .synthesize import synthesize as synthesize_rules
|
|
679
|
+
|
|
519
680
|
results = synthesize_rules(
|
|
520
681
|
con,
|
|
521
682
|
out_path=out,
|
|
522
683
|
out_dir=out_dir,
|
|
523
684
|
repo=repo,
|
|
524
685
|
top=top,
|
|
686
|
+
max_rules=max_rules,
|
|
525
687
|
offline=offline,
|
|
526
688
|
console=cmd_console,
|
|
527
689
|
)
|
|
@@ -27,6 +27,7 @@ class Finding:
|
|
|
27
27
|
intent_confidence: float = 0.0
|
|
28
28
|
object_confidence: float = 0.0
|
|
29
29
|
event_keys: tuple[tuple[str, str, str], ...] = field(default_factory=tuple)
|
|
30
|
+
edit_sessions: int = 0
|
|
30
31
|
|
|
31
32
|
@property
|
|
32
33
|
def evidence(self) -> str:
|
|
@@ -102,6 +103,7 @@ def hotspots(con: duckdb.DuckDBPyConnection, limit: int | None = None) -> list[F
|
|
|
102
103
|
sessions=sessions,
|
|
103
104
|
tokens=int(wasted or 0),
|
|
104
105
|
detail=f"read-only in {read_only}/{repo_sessions} sessions ({read_only / repo_sessions:.0%})",
|
|
106
|
+
edit_sessions=sessions - read_only,
|
|
105
107
|
)
|
|
106
108
|
for repo, target, sessions, read_only, repo_sessions, wasted in con.execute(query).fetchall()
|
|
107
109
|
]
|
|
@@ -562,15 +562,22 @@ def write_sessions(
|
|
|
562
562
|
sessions: Iterable[Session],
|
|
563
563
|
) -> IngestStats:
|
|
564
564
|
"""Write canonical sessions/events, replacing rows with the same ids."""
|
|
565
|
-
session_list = list(sessions)
|
|
565
|
+
session_list = list({session.session_id: session for session in sessions}.values())
|
|
566
566
|
con = connect(db_path)
|
|
567
|
-
|
|
567
|
+
con.execute("BEGIN TRANSACTION")
|
|
568
568
|
try:
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
569
|
+
if session_list:
|
|
570
|
+
session_ids = [session.session_id for session in session_list]
|
|
571
|
+
placeholders = ",".join("?" for _ in session_ids)
|
|
572
|
+
con.execute(
|
|
573
|
+
f"DELETE FROM tool_events WHERE session_id IN ({placeholders})",
|
|
574
|
+
session_ids,
|
|
575
|
+
)
|
|
573
576
|
con.execute(
|
|
577
|
+
f"DELETE FROM sessions WHERE session_id IN ({placeholders})",
|
|
578
|
+
session_ids,
|
|
579
|
+
)
|
|
580
|
+
con.executemany(
|
|
574
581
|
"""
|
|
575
582
|
INSERT INTO sessions (
|
|
576
583
|
session_id, repo, instance_id, resolved, total_turns,
|
|
@@ -578,17 +585,22 @@ def write_sessions(
|
|
|
578
585
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
579
586
|
""",
|
|
580
587
|
[
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
588
|
+
(
|
|
589
|
+
session.session_id,
|
|
590
|
+
session.repo or "unknown",
|
|
591
|
+
session.instance_id or session.session_id,
|
|
592
|
+
session.resolved,
|
|
593
|
+
len(session.calls),
|
|
594
|
+
session.source,
|
|
595
|
+
session.agent,
|
|
596
|
+
session.success_score,
|
|
597
|
+
)
|
|
598
|
+
for session in session_list
|
|
589
599
|
],
|
|
590
600
|
)
|
|
591
|
-
|
|
601
|
+
|
|
602
|
+
events = []
|
|
603
|
+
for session in session_list:
|
|
592
604
|
for step_idx, call in enumerate(session.calls):
|
|
593
605
|
if call.action_type:
|
|
594
606
|
action = call.action_type
|
|
@@ -643,29 +655,32 @@ def write_sessions(
|
|
|
643
655
|
observation[:400],
|
|
644
656
|
)
|
|
645
657
|
)
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
658
|
+
|
|
659
|
+
if events:
|
|
660
|
+
con.executemany(
|
|
661
|
+
"""
|
|
662
|
+
INSERT INTO tool_events (
|
|
663
|
+
event_id, session_id, repo, step_idx, segment_idx,
|
|
664
|
+
tool_name, action_type,
|
|
665
|
+
verb, object_id, target, raw_target, outcome, exit_code,
|
|
666
|
+
error_signature, is_error, approx_out_tokens, obs_excerpt
|
|
667
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
668
|
+
""",
|
|
669
|
+
events,
|
|
670
|
+
)
|
|
671
|
+
con.execute("COMMIT")
|
|
659
672
|
return IngestStats(
|
|
660
673
|
prefix="canonical",
|
|
661
674
|
matched=len(session_list),
|
|
662
675
|
sessions=len(session_list),
|
|
663
|
-
events=
|
|
676
|
+
events=len(events),
|
|
664
677
|
)
|
|
678
|
+
except Exception:
|
|
679
|
+
con.execute("ROLLBACK")
|
|
680
|
+
raise
|
|
665
681
|
finally:
|
|
666
682
|
con.close()
|
|
667
683
|
|
|
668
|
-
|
|
669
684
|
def _flush_sessions(con: duckdb.DuckDBPyConnection, sessions: list[dict]) -> None:
|
|
670
685
|
if not sessions:
|
|
671
686
|
return
|
|
@@ -14,6 +14,7 @@ from pathlib import Path
|
|
|
14
14
|
import duckdb
|
|
15
15
|
|
|
16
16
|
from .db import DB_PATH, connect
|
|
17
|
+
from .redact import redact_text
|
|
17
18
|
|
|
18
19
|
DEFAULT_MODEL = "clef-flash"
|
|
19
20
|
DEFAULT_RPM = 270
|
|
@@ -355,8 +356,8 @@ def _state(row: tuple[str, str, str, str]) -> dict:
|
|
|
355
356
|
return {
|
|
356
357
|
"tool": tool_name,
|
|
357
358
|
"action_type": action_type,
|
|
358
|
-
"normalized_target": target[:500],
|
|
359
|
-
"raw_example": (raw_example or "").replace("\x00", " ")[:500],
|
|
359
|
+
"normalized_target": redact_text(target[:500]),
|
|
360
|
+
"raw_example": redact_text((raw_example or "").replace("\x00", " ")[:500]),
|
|
360
361
|
}
|
|
361
362
|
|
|
362
363
|
|