debrief-cli 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.
- debrief/__init__.py +0 -0
- debrief/build_artifact.py +83 -0
- debrief/classify.py +812 -0
- debrief/cli.py +291 -0
- debrief/compare_definitions.py +147 -0
- debrief/compare_llm.py +250 -0
- debrief/config.py +204 -0
- debrief/db.py +341 -0
- debrief/export.py +156 -0
- debrief/export_site.py +661 -0
- debrief/github.py +540 -0
- debrief/normalize.py +635 -0
- debrief/paths.py +17 -0
- debrief/prune.py +117 -0
- debrief/recommend.py +271 -0
- debrief/relay.py +83 -0
- debrief/runner.py +163 -0
- debrief/scrape.py +208 -0
- debrief/site/app.js +1613 -0
- debrief/site/index.html +32 -0
- debrief/site/method.js +117 -0
- debrief/site/style.css +520 -0
- debrief/skills.py +107 -0
- debrief/sources.py +341 -0
- debrief/synthesize.py +1052 -0
- debrief/verify_evidence.py +154 -0
- debrief_cli-0.1.0.dist-info/METADATA +205 -0
- debrief_cli-0.1.0.dist-info/RECORD +31 -0
- debrief_cli-0.1.0.dist-info/WHEEL +4 -0
- debrief_cli-0.1.0.dist-info/entry_points.txt +2 -0
- debrief_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
debrief/export_site.py
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
"""Build the debrief site: copy the static files to SITE_OUT and write its data beside them.
|
|
2
|
+
|
|
3
|
+
data/index.json holds everything the overview pages need (recommendations, findings, resolved
|
|
4
|
+
evidence, per-session stats, flagged turns, CI and review rows, skill uses). Session
|
|
5
|
+
transcripts load on demand from shard files, data/sessions/sNN.json, each a JSON object
|
|
6
|
+
of {session_id: transcript}; each session in index.json names its shard.
|
|
7
|
+
|
|
8
|
+
Every table but sessions and messages may be empty. meta.has tells the site which optional
|
|
9
|
+
data exists (GitHub, people, checkpoints, projects, labels) so it can hide the rest.
|
|
10
|
+
|
|
11
|
+
Only picked projects (`[projects] include` in config.toml) are exported: their sessions, and
|
|
12
|
+
the findings, ideas and evidence that belong to them. index.json's "projects" lists every
|
|
13
|
+
project, picked or not, with its size and when it was last analysed.
|
|
14
|
+
|
|
15
|
+
uv run python -m debrief.export_site
|
|
16
|
+
uv run python -m debrief.export_site --run 20260925T182434Z --since 2026-08-11
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import argparse
|
|
20
|
+
import json
|
|
21
|
+
import re
|
|
22
|
+
import shutil
|
|
23
|
+
import sqlite3
|
|
24
|
+
from collections import Counter, defaultdict
|
|
25
|
+
from datetime import UTC, datetime
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
|
|
28
|
+
from debrief import config, skills
|
|
29
|
+
from debrief.db import connect, repo_projects, session_projects
|
|
30
|
+
from debrief.paths import SITE_OUT, SITE_SRC
|
|
31
|
+
from debrief.recommend import LEVERS
|
|
32
|
+
from debrief.relay import is_relayed
|
|
33
|
+
from debrief.synthesize import ref_project
|
|
34
|
+
|
|
35
|
+
MIN_CONF = 0.7 # same default as synthesize.py
|
|
36
|
+
PR_KEY = "p.repo || '#' || p.pr_number" # how the export names a PR: owner/name#12; p is the PR row
|
|
37
|
+
SUBTYPE_CONF = 0.5 # correction_type on a turn already labeled a correction
|
|
38
|
+
MSG_CAP = 8_000 # chars of message text in a session file
|
|
39
|
+
TOOL_IN_CAP = 300
|
|
40
|
+
TOOL_OUT_CAP = 800 # output kept for failed calls only
|
|
41
|
+
SHARD_BYTES = 1_500_000 # target size of a transcript shard file
|
|
42
|
+
|
|
43
|
+
# Friction signals per human turn: code -> (field, value).
|
|
44
|
+
SIGNALS = {
|
|
45
|
+
"correction": ("turn_kind", "correction"),
|
|
46
|
+
"reversal": ("design_reversal", "true"),
|
|
47
|
+
"defect": ("human_caught_defect", "true"),
|
|
48
|
+
"could_find": ("agent_could_have_found_it", "true"),
|
|
49
|
+
"not_filed": ("found_not_filed", "true"),
|
|
50
|
+
}
|
|
51
|
+
CORRECTING = ("correction", "reversal", "defect") # together, the site's "Agent corrected"
|
|
52
|
+
|
|
53
|
+
# Secret-shaped strings, redacted from every string before it is serialized. Redacting the
|
|
54
|
+
# serialized JSON instead misses a secret that follows an escape: in "\nghp_..." the "n" of
|
|
55
|
+
# the escape is a word character, so \b never matches.
|
|
56
|
+
SECRETS = [
|
|
57
|
+
(re.compile(r"\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{40,})"), "[redacted]"),
|
|
58
|
+
(re.compile(r"\bsk-(?=[A-Za-z0-9_-]*\d)(?:ant-|proj-|or-v1-)?[A-Za-z0-9_-]{20,}"), "[redacted]"), # needs a digit
|
|
59
|
+
(re.compile(r"\b(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{20,}"), "[redacted]"), # Stripe
|
|
60
|
+
(re.compile(r"\b(?:AKIA|ASIA)[A-Z0-9]{16}\b"), "[redacted]"),
|
|
61
|
+
(re.compile(r"\bxox[abeprs]-[A-Za-z0-9-]{10,}"), "[redacted]"),
|
|
62
|
+
(re.compile(r"\bglpat-[A-Za-z0-9_-]{20,}"), "[redacted]"),
|
|
63
|
+
(re.compile(r"\bAIza[A-Za-z0-9_-]{35}"), "[redacted]"), # Google API key
|
|
64
|
+
(re.compile(r"\b(?:npm|hf)_[A-Za-z0-9]{30,}"), "[redacted]"),
|
|
65
|
+
(re.compile(r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}"), "[redacted]"),
|
|
66
|
+
(re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(?:-----END [A-Z ]*PRIVATE KEY-----|\Z)", re.DOTALL), "[redacted]"),
|
|
67
|
+
(re.compile(r"(?i)\b(bearer\s+)[A-Za-z0-9._~+/-]{24,}=*"), r"\1[redacted]"),
|
|
68
|
+
(re.compile(r"(https?://)[^/\s:@\"\\]+:[^/\s@\"\\]+@"), r"\1[redacted]@"),
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def redact(text: str) -> str:
|
|
73
|
+
"""Redact secrets in one plain (not JSON-encoded) string."""
|
|
74
|
+
for pattern, repl in SECRETS:
|
|
75
|
+
text = pattern.sub(repl, text)
|
|
76
|
+
return text
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# A run of non-space text that holds U+FFFD and is at least this long without it is blanked whole.
|
|
80
|
+
# Shorter than any token pattern above can match, so no secret touching a mark survives.
|
|
81
|
+
DAMAGED_MIN = 16
|
|
82
|
+
DAMAGED_RUN = re.compile(r"\S*\ufffd\S*")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# In damaged text a private key's header may not survive well enough to match, so any text
|
|
86
|
+
# that spells PRIVATE KEY once marks and spaces are ignored is cut from its first dashes on.
|
|
87
|
+
DAMAGED_KEY = re.compile(r"P\W*R\W*I\W*V\W*A\W*T\W*E\W*K\W*E\W*Y")
|
|
88
|
+
DAMAGED_KEY_END = re.compile(r"E\W*N\W*D\b[^\n]{0,60}?-{3,}")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def redact_damaged(text: str) -> str:
|
|
92
|
+
"""Redact a string holding U+FFFD, which marks bytes the source could not decode, then drop it.
|
|
93
|
+
|
|
94
|
+
A mark can sit inside a secret, or be the only break before or after one, in any mix, so
|
|
95
|
+
no single way of reading the marks finds every secret. Three steps, each on what the
|
|
96
|
+
last one left:
|
|
97
|
+
1. blank what the patterns find with the marks removed, and with the marks as spaces
|
|
98
|
+
(this is what catches a secret of several words, such as a private key or a bearer
|
|
99
|
+
token, whose first word a mark touches);
|
|
100
|
+
2. blank any long run of text that still touches a mark, secret or not;
|
|
101
|
+
3. the normal pass.
|
|
102
|
+
"""
|
|
103
|
+
key = DAMAGED_KEY.search(text)
|
|
104
|
+
if key:
|
|
105
|
+
start = max(text.rfind("---", 0, key.start()), 0)
|
|
106
|
+
start = len(text[:start].rstrip("-")) if start else 0
|
|
107
|
+
ends = [m.end() for m in DAMAGED_KEY_END.finditer(text, key.end())]
|
|
108
|
+
text = text[:start] + "[redacted]" + (text[ends[-1]:].lstrip("-") if ends else "")
|
|
109
|
+
keep = [i for i, c in enumerate(text) if c != "\ufffd"]
|
|
110
|
+
joined = "".join(text[i] for i in keep)
|
|
111
|
+
spaced = text.replace("\ufffd", " ")
|
|
112
|
+
spans = []
|
|
113
|
+
for pattern, _ in SECRETS:
|
|
114
|
+
spans += [(keep[m.start()], keep[m.end() - 1] + 1) for m in pattern.finditer(joined) if m.end() > m.start()]
|
|
115
|
+
spans += [m.span() for m in pattern.finditer(spaced)]
|
|
116
|
+
out, at = [], 0
|
|
117
|
+
for start, end in sorted(spans):
|
|
118
|
+
if start >= at:
|
|
119
|
+
out += [text[at:start], "[redacted]"]
|
|
120
|
+
at = max(at, end)
|
|
121
|
+
text = "".join([*out, text[at:]])
|
|
122
|
+
|
|
123
|
+
def blank(m: re.Match) -> str:
|
|
124
|
+
rest = m.group().replace("\ufffd", "")
|
|
125
|
+
return "[redacted]" if len(rest) >= DAMAGED_MIN else rest
|
|
126
|
+
return redact(DAMAGED_RUN.sub(blank, text))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def redact_tree(obj):
|
|
130
|
+
"""Redact every string, keys included, in a JSON-like structure, and drop U+FFFD (the artifact host rejects it)."""
|
|
131
|
+
if isinstance(obj, str):
|
|
132
|
+
return redact_damaged(obj) if "\ufffd" in obj else redact(obj)
|
|
133
|
+
if isinstance(obj, (list, tuple)):
|
|
134
|
+
return [redact_tree(v) for v in obj]
|
|
135
|
+
if isinstance(obj, dict):
|
|
136
|
+
return {redact_tree(k): redact_tree(v) for k, v in obj.items()}
|
|
137
|
+
return obj
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def excerpt(text, n: int) -> str:
|
|
141
|
+
text = re.sub(r"\s+", " ", str(text or "")).strip()
|
|
142
|
+
return text if len(text) <= n else text[: n - 1] + "…"
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def tail(text, n: int) -> str:
|
|
146
|
+
text = str(text or "").strip()
|
|
147
|
+
return text if len(text) <= n else "…" + text[-n:]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def labels_by_turn(conn) -> dict[tuple, dict]:
|
|
151
|
+
"""Labels per labeled human turn, plus a "relayed" pseudo-label on pasted bot output."""
|
|
152
|
+
out: dict[tuple, dict] = defaultdict(dict)
|
|
153
|
+
for r in conn.execute("SELECT session_id, msg_idx, field, value, confidence FROM turn_labels"):
|
|
154
|
+
out[(r[0], r[1])][r[2]] = (r[3], r[4] or 0)
|
|
155
|
+
for sid, idx, text in conn.execute("SELECT session_id, idx, text FROM messages "
|
|
156
|
+
"WHERE role = 'user' AND user_kind = 'human'"):
|
|
157
|
+
if (sid, idx) in out and is_relayed(text):
|
|
158
|
+
out[(sid, idx)]["relayed"] = ("true", 1.0)
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def signals_of(labels: dict) -> list[str]:
|
|
163
|
+
"""A relayed bot finding isn't the person correcting the agent; the agent could have fetched it."""
|
|
164
|
+
relayed = "relayed" in labels
|
|
165
|
+
return [code for code, (field, value) in SIGNALS.items()
|
|
166
|
+
if (relayed and code == "could_find")
|
|
167
|
+
or (not (relayed and code in CORRECTING)
|
|
168
|
+
and labels.get(field, (None, 0))[0] == value and labels[field][1] >= MIN_CONF)]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def correction_type(labels: dict) -> str:
|
|
172
|
+
value, conf = labels.get("correction_type", (None, 0))
|
|
173
|
+
return value if value not in (None, "none") and conf >= SUBTYPE_CONF else "unclassified"
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
# --- evidence -------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
def ref_id(ref: dict) -> str:
|
|
179
|
+
if "session_id" in ref:
|
|
180
|
+
return f"m:{ref['session_id']}:{ref['msg_idx']}"
|
|
181
|
+
return f"e:{ref['event_type']}:{ref['event_id']}"
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def resolve_msg(conn, labels, session_id: str, idx: int) -> dict:
|
|
185
|
+
m = conn.execute("SELECT role, text FROM messages WHERE session_id = ? AND idx = ?", (session_id, idx)).fetchone()
|
|
186
|
+
prev = conn.execute(
|
|
187
|
+
"SELECT MAX(idx) FROM messages WHERE session_id = ? AND idx < ? AND role = 'user' AND user_kind = 'human'",
|
|
188
|
+
(session_id, idx)).fetchone()[0]
|
|
189
|
+
end = idx if m and m[0] == "assistant" else idx - 1
|
|
190
|
+
agent = [r[0] for r in conn.execute(
|
|
191
|
+
"SELECT text FROM messages WHERE session_id = ? AND idx > ? AND idx <= ? AND role = 'assistant' "
|
|
192
|
+
"AND text != '' ORDER BY idx", (session_id, -1 if prev is None else prev, end))]
|
|
193
|
+
lab = labels.get((session_id, idx), {})
|
|
194
|
+
return {"type": "msg", "session_id": session_id, "msg_idx": idx, "role": m[0] if m else None,
|
|
195
|
+
"text": excerpt(m[1] if m else "", 500), "agent_before": tail(" ".join(" ".join(agent).split()), 400),
|
|
196
|
+
"signals": signals_of(lab), "correction_type": correction_type(lab) if "correction" in signals_of(lab)
|
|
197
|
+
else None}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def event_label(conn, event_type: str, event_id: int, field: str) -> str | None:
|
|
201
|
+
r = conn.execute("SELECT value FROM event_labels WHERE event_type = ? AND event_id = ? AND field = ?",
|
|
202
|
+
(event_type, event_id, field)).fetchone()
|
|
203
|
+
return r[0] if r else None
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def resolve_check(conn, check_id: int) -> dict:
|
|
207
|
+
c = conn.execute("SELECT * FROM gh_checks WHERE check_id = ?", (check_id,)).fetchone()
|
|
208
|
+
if not c:
|
|
209
|
+
return {"type": "missing"}
|
|
210
|
+
prs = [r[0] for r in conn.execute(f"SELECT {PR_KEY} FROM gh_commit_prs p WHERE commit_sha = ?", (c["commit_sha"],))]
|
|
211
|
+
sessions = [r[0] for r in conn.execute(
|
|
212
|
+
"SELECT DISTINCT session_id FROM session_commits WHERE commit_sha = ?", (c["commit_sha"],))]
|
|
213
|
+
return {"type": "check", "check_id": check_id, "name": c["name"], "conclusion": c["conclusion"],
|
|
214
|
+
"failure_class": event_label(conn, "check_failure", check_id, "failure_class"),
|
|
215
|
+
"commit": (c["commit_sha"] or "")[:10], "prs": prs, "sessions": sessions,
|
|
216
|
+
"log": tail(c["failure_log"], 1200), "at": c["completed_at"]}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def resolve_review(conn, comment_id: int) -> dict:
|
|
220
|
+
c = conn.execute("SELECT * FROM gh_review_comments WHERE comment_id = ?", (comment_id,)).fetchone()
|
|
221
|
+
if not c:
|
|
222
|
+
return {"type": "missing"}
|
|
223
|
+
sessions = [r[0] for r in conn.execute(
|
|
224
|
+
"SELECT DISTINCT session_id FROM session_pr_links WHERE repo = ? AND pr_number = ?",
|
|
225
|
+
(c["repo"], c["pr_number"]))]
|
|
226
|
+
return {"type": "review", "comment_id": comment_id, "sessions": sessions, "author": c["author"],
|
|
227
|
+
"is_bot": bool(c["is_bot"]), "kind": c["kind"], "path": c["path"], "line": c["line"], "pr": f"{c['repo']}#{c['pr_number']}",
|
|
228
|
+
"body": excerpt(c["body"], 600), "resolved": c["is_resolved"], "outdated": c["is_outdated"],
|
|
229
|
+
"comment_type": event_label(conn, "review_comment", comment_id, "comment_type"),
|
|
230
|
+
"held_up": event_label(conn, "review_comment", comment_id, "held_up")}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def resolve(conn, labels, ref: dict) -> dict:
|
|
234
|
+
if "session_id" in ref:
|
|
235
|
+
return resolve_msg(conn, labels, ref["session_id"], int(ref["msg_idx"]))
|
|
236
|
+
if ref["event_type"] == "check_failure":
|
|
237
|
+
return resolve_check(conn, int(ref["event_id"]))
|
|
238
|
+
return resolve_review(conn, int(ref["event_id"]))
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
# --- sections -------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
def findings_and_recs(conn, labels, run_id: str, picked: set[str]) -> tuple[dict, list, dict]:
|
|
244
|
+
"""The run's findings and ideas, less what belongs to a project that is not picked: findings
|
|
245
|
+
about one, evidence from one, and whatever that leaves with nothing to cite."""
|
|
246
|
+
project, repos = session_projects(conn), repo_projects(conn)
|
|
247
|
+
findings, refs = {}, {}
|
|
248
|
+
for r in conn.execute("SELECT * FROM findings WHERE run_id = ? ORDER BY finding_id", (run_id,)).fetchall():
|
|
249
|
+
evidence = [e for e in json.loads(r["evidence"] or "[]") if ref_project(conn, project, repos, e) in picked]
|
|
250
|
+
if not evidence or r["project"] not in (None, *picked):
|
|
251
|
+
continue
|
|
252
|
+
for ref in evidence:
|
|
253
|
+
refs.setdefault(ref_id(ref), resolve(conn, labels, ref))
|
|
254
|
+
findings[r["finding_id"]] = {"id": r["finding_id"], "kind": r["kind"], "title": r["title"],
|
|
255
|
+
"detail": r["detail"], "fix": r["suggested_fix"], "project": r["project"],
|
|
256
|
+
"evidence": [ref_id(e) for e in evidence]}
|
|
257
|
+
# verify_evidence.py's verdicts: rec_id -> ref ids that support the idea. No rows: not checked.
|
|
258
|
+
supported: dict[int, set] = {}
|
|
259
|
+
for rec_id, ref, ok in conn.execute("SELECT rec_id, ref, supports FROM evidence_checks"):
|
|
260
|
+
hits = supported.setdefault(rec_id, set()) # an idea whose refs all failed is still checked
|
|
261
|
+
if ok:
|
|
262
|
+
hits.add(ref)
|
|
263
|
+
recs = []
|
|
264
|
+
for r in conn.execute("SELECT * FROM recommendations WHERE run_id = ? ORDER BY priority", (run_id,)):
|
|
265
|
+
ids = [i for i in json.loads(r["finding_ids"] or "[]") if i in findings]
|
|
266
|
+
if not ids:
|
|
267
|
+
continue
|
|
268
|
+
cited = list(dict.fromkeys(e for i in ids for e in findings[i]["evidence"]))
|
|
269
|
+
checked = r["rec_id"] in supported
|
|
270
|
+
evidence = [e for e in cited if e in supported[r["rec_id"]]] if checked else cited
|
|
271
|
+
# Reach (sessions, people) comes from `evidence` in main(), so it counts supported refs only.
|
|
272
|
+
# project: the folder the idea is about; None when it spans projects, and in runs from before projects.
|
|
273
|
+
recs.append({"id": r["rec_id"], "priority": r["priority"], "lever": r["lever"], "title": r["title"],
|
|
274
|
+
"project": r["project"],
|
|
275
|
+
"problem": r["problem"], "change": r["change"], "target": r["target"], "effort": r["effort"],
|
|
276
|
+
"impact": r["impact"], "findings": ids, "evidence": evidence, "evidence_all": len(cited),
|
|
277
|
+
"checked": checked, "weak": checked and not evidence})
|
|
278
|
+
return findings, recs, refs
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def evidence_sessions(ref: dict) -> list[str]:
|
|
282
|
+
if ref["type"] == "msg":
|
|
283
|
+
return [ref["session_id"]]
|
|
284
|
+
return ref.get("sessions", [])
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def sessions_section(conn, labels, since: str, picked: set[str]) -> tuple[list, list]:
|
|
288
|
+
"""Per-session stats, and every human turn with a friction signal, for sessions of picked
|
|
289
|
+
projects last active since `since`."""
|
|
290
|
+
slabels: dict[str, dict] = defaultdict(dict)
|
|
291
|
+
for r in conn.execute("SELECT session_id, field, value, confidence FROM session_labels"):
|
|
292
|
+
slabels[r[0]][r[1]] = r[2] if (r[3] or 0) >= 0.5 or r[1].endswith("_score") else None
|
|
293
|
+
prs: dict[str, list] = defaultdict(list)
|
|
294
|
+
for r in conn.execute(f"""
|
|
295
|
+
SELECT l.session_id, {PR_KEY}, CASE WHEN p.merged = 1 THEN 'merged' ELSE p.state END
|
|
296
|
+
FROM session_pr_links l JOIN gh_prs p ON p.repo = l.repo AND p.pr_number = l.pr_number
|
|
297
|
+
ORDER BY p.repo, p.pr_number"""):
|
|
298
|
+
prs[r[0]].append({"n": r[1], "state": r[2]})
|
|
299
|
+
tools = {r[0]: (r[1], r[2] or 0) for r in conn.execute(
|
|
300
|
+
"SELECT session_id, COUNT(*), SUM(is_error) FROM tool_calls GROUP BY session_id")}
|
|
301
|
+
# app 'entire' is Entire's approval gate, which fails until a person approves: not a CI failure.
|
|
302
|
+
ci_fail = dict(conn.execute("""
|
|
303
|
+
SELECT cp.session_id, COUNT(DISTINCT k.check_id) FROM gh_checks k
|
|
304
|
+
JOIN session_commits cp ON cp.commit_sha = k.commit_sha
|
|
305
|
+
WHERE k.conclusion = 'failure' AND COALESCE(k.app, '') != 'entire'
|
|
306
|
+
GROUP BY cp.session_id"""))
|
|
307
|
+
turns_by_session: dict[str, list] = defaultdict(list)
|
|
308
|
+
for r in conn.execute("SELECT session_id, idx, text, ts FROM messages "
|
|
309
|
+
"WHERE role = 'user' AND user_kind = 'human' ORDER BY session_id, idx"):
|
|
310
|
+
turns_by_session[r[0]].append(r)
|
|
311
|
+
# Entire's started_at is the first checkpoint, often long after the first message.
|
|
312
|
+
first_ts = dict(conn.execute("SELECT session_id, MIN(ts) FROM messages WHERE ts IS NOT NULL GROUP BY session_id"))
|
|
313
|
+
project = session_projects(conn)
|
|
314
|
+
|
|
315
|
+
sessions, flagged = [], []
|
|
316
|
+
for s in conn.execute("SELECT * FROM sessions WHERE COALESCE(last_activity_at, started_at, '') >= ? "
|
|
317
|
+
"ORDER BY started_at", (since,)):
|
|
318
|
+
sid = s["session_id"]
|
|
319
|
+
if project.get(sid) not in picked:
|
|
320
|
+
continue
|
|
321
|
+
started = first_ts.get(sid) or s["started_at"] or s["last_activity_at"]
|
|
322
|
+
kinds: Counter = Counter()
|
|
323
|
+
friction = []
|
|
324
|
+
for t in turns_by_session.get(sid, []):
|
|
325
|
+
lab = labels.get((sid, t[1]), {})
|
|
326
|
+
kinds[lab.get("turn_kind", ("unlabeled",))[0] or "unlabeled"] += 1
|
|
327
|
+
sig = [x for x in signals_of(lab) if x != "not_filed"]
|
|
328
|
+
if sig:
|
|
329
|
+
friction.append([t[1], sig])
|
|
330
|
+
flagged.append({"s": sid, "i": t[1], "sig": sig, "ctype": correction_type(lab)
|
|
331
|
+
if "correction" in sig else None, "text": excerpt(t[2], 260), "ts": t[3]})
|
|
332
|
+
not_filed = [idx for (ss, idx), lab in labels.items() if ss == sid and "not_filed" in signals_of(lab)]
|
|
333
|
+
sl = slabels.get(sid, {})
|
|
334
|
+
n_tools, n_err = tools.get(sid, (0, 0))
|
|
335
|
+
count = Counter(sig for _, sigs in friction for sig in sigs)
|
|
336
|
+
pr_list = prs.get(sid, [])
|
|
337
|
+
sessions.append({
|
|
338
|
+
"id": sid, "name": s["display_name"], "agent": s["agent"], "model": s["model"],
|
|
339
|
+
"author": s["author"] or None, "started": started, "last": s["last_activity_at"] or started,
|
|
340
|
+
# The session's project, a folder; the site shows its last part and scopes on the whole path.
|
|
341
|
+
"project": project[sid],
|
|
342
|
+
"human_turns": len(turns_by_session.get(sid, [])), "turn_kinds": dict(kinds),
|
|
343
|
+
"corrections": count["correction"], "reversals": count["reversal"], "defects": count["defect"],
|
|
344
|
+
"could_find": count["could_find"], "not_filed": len(not_filed), "friction": friction,
|
|
345
|
+
"relayed": sum("relayed" in labels.get((sid, t[1]), {}) for t in turns_by_session.get(sid, [])),
|
|
346
|
+
"tool_calls": n_tools, "tool_errors": n_err, "checkpoints": s["checkpoint_count"],
|
|
347
|
+
"outcome": sl.get("outcome"), "task_type": sl.get("task_type"), "prompt_style": sl.get("prompt_style"),
|
|
348
|
+
"specificity": sl.get("prompt_specificity"), "planned_first": sl.get("planned_first"),
|
|
349
|
+
"tests_added": sl.get("tests_added"), "prs": pr_list,
|
|
350
|
+
"settled": any(p["state"] in ("merged", "closed") for p in pr_list),
|
|
351
|
+
"ci_failures": ci_fail.get(sid, 0), "prompt": excerpt(s["prompt"], 400),
|
|
352
|
+
})
|
|
353
|
+
return sessions, flagged
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def hotspot_files(conn, labels, ids: set[str]) -> list[dict]:
|
|
357
|
+
"""Repo files the agent wrote in the stretch before a correction, reversal or caught defect, in sessions `ids`."""
|
|
358
|
+
# shortcut: paths are relative to each session's repo, so src/ in two repos counts as one place.
|
|
359
|
+
# sessions.cwd can't split them: worktrees of one repo have different cwds. Split once sessions name their repo.
|
|
360
|
+
files: dict[str, dict] = {}
|
|
361
|
+
rows = conn.execute("""
|
|
362
|
+
SELECT m.session_id, m.idx,
|
|
363
|
+
COALESCE((SELECT MAX(p.idx) FROM messages p WHERE p.session_id = m.session_id AND p.idx < m.idx
|
|
364
|
+
AND p.role = 'user' AND p.user_kind = 'human'), -1) AS prev
|
|
365
|
+
FROM messages m WHERE m.role = 'user' AND m.user_kind = 'human'""").fetchall()
|
|
366
|
+
for sid, idx, prev in rows:
|
|
367
|
+
sig = [x for x in signals_of(labels.get((sid, idx), {})) if x in CORRECTING]
|
|
368
|
+
if not sig or sid not in ids:
|
|
369
|
+
continue
|
|
370
|
+
paths = {r[0] for r in conn.execute("""
|
|
371
|
+
SELECT DISTINCT f.value FROM tool_calls tc JOIN json_each(tc.files) f
|
|
372
|
+
WHERE tc.session_id = ? AND tc.msg_idx > ? AND tc.msg_idx < ? AND tc.writes = 1
|
|
373
|
+
AND f.value NOT LIKE '/%'""", (sid, prev, idx))}
|
|
374
|
+
for path in paths:
|
|
375
|
+
f = files.setdefault(path, {"path": path, "sessions": set(), "turns": []})
|
|
376
|
+
f["sessions"].add(sid)
|
|
377
|
+
f["turns"].append({"s": sid, "i": idx, "sig": sig})
|
|
378
|
+
return sorted(({**f, "sessions": sorted(f["sessions"])} for f in files.values()),
|
|
379
|
+
key=lambda f: (-len(f["sessions"]), -len(f["turns"])))
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
# Where a failed CI run broke, read from the tail of its log.
|
|
383
|
+
PKG_RE = re.compile(r"^FAIL[ \t]+([\w.-]+\.[a-z]+/\S+)", re.MULTILINE) # go test: FAIL<tab>host/owner/repo/pkg<tab>1.2s
|
|
384
|
+
MODULE_RE = re.compile(r"^[\w.-]+\.[a-z]+/[^/]+/[^/]+") # a Go module path: host/owner/repo
|
|
385
|
+
GOTESTSUM_RE = re.compile(r"^=== FAIL: (\S+)(?: (Test\S+))?", re.MULTILINE) # gotestsum: === FAIL: pkg TestX
|
|
386
|
+
TEST_RE = re.compile(r"^\s*--- FAIL: (Test\S+)", re.MULTILINE)
|
|
387
|
+
RERUN_RE = re.compile(r"^\s*mise run test:\S+ .*?(Test\w+)\s*$", re.MULTILINE) # e2e report's rerun hints
|
|
388
|
+
FILE_RE = re.compile(r"(?<![\w./\\@-])((?:[A-Za-z]:)?[\w./\\-]*?[\w-]+\.(?:go|sh|ps1|ya?ml)):\d+")
|
|
389
|
+
# GitHub's checkout dirs, Linux and Windows: <work>/<repo>/<repo>/
|
|
390
|
+
RUNNER_RE = re.compile(r"^(?:/?home/runner/work/([^/]+)/\1/|[A-Za-z]:/a/([^/]+)/\2/)")
|
|
391
|
+
MAX_FILES = 5
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def failure_places(log: str | None) -> dict:
|
|
395
|
+
"""Failing packages, tests and files named in a CI log, plus the best single area."""
|
|
396
|
+
log = log or ""
|
|
397
|
+
pkgs, tests, files = [], [], []
|
|
398
|
+
|
|
399
|
+
def add(xs: list, x: str) -> None:
|
|
400
|
+
if x and x not in xs:
|
|
401
|
+
xs.append(x)
|
|
402
|
+
|
|
403
|
+
def pkg(p: str) -> str:
|
|
404
|
+
p = MODULE_RE.sub("", p).strip("/")
|
|
405
|
+
return p or "(root)"
|
|
406
|
+
|
|
407
|
+
for m in PKG_RE.finditer(log):
|
|
408
|
+
add(pkgs, pkg(m.group(1)))
|
|
409
|
+
for m in GOTESTSUM_RE.finditer(log):
|
|
410
|
+
add(pkgs, pkg(m.group(1)))
|
|
411
|
+
if m.group(2):
|
|
412
|
+
add(tests, m.group(2).split("/")[0])
|
|
413
|
+
for rx in (TEST_RE, RERUN_RE):
|
|
414
|
+
for m in rx.finditer(log):
|
|
415
|
+
add(tests, m.group(1).split("/")[0])
|
|
416
|
+
# Skip the first line: the log is a tail, so it may start mid-word.
|
|
417
|
+
for m in FILE_RE.finditer(log.partition("\n")[2]):
|
|
418
|
+
path = RUNNER_RE.sub("", m.group(1).replace("\\", "/"))
|
|
419
|
+
if ":" in path or path.startswith("/") or "/go/" in path or "mise/installs" in path:
|
|
420
|
+
continue # outside the repo: Go toolchain, module cache
|
|
421
|
+
if path != "testing.go" and not path.startswith("_"): # Go's test runner and generated main
|
|
422
|
+
add(files, path)
|
|
423
|
+
# A bare name adds nothing once the same file shows up with its path.
|
|
424
|
+
full = {f.rsplit("/", 1)[-1] for f in files if "/" in f}
|
|
425
|
+
files = [f for f in files if "/" in f or f not in full]
|
|
426
|
+
area = pkgs[0] if pkgs else next((f.rsplit("/", 1)[0] for f in files if "/" in f), None)
|
|
427
|
+
return {"pkgs": pkgs, "tests": tests, "files": files[:MAX_FILES], "area": area}
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def ci_section(conn, since: str) -> dict:
|
|
431
|
+
"""Failed runs on settled PRs that finished inside the window."""
|
|
432
|
+
rows = conn.execute(f"""
|
|
433
|
+
SELECT k.check_id, k.name, k.failure_log, k.completed_at,
|
|
434
|
+
(SELECT value FROM event_labels l WHERE l.event_type = 'check_failure' AND l.event_id = k.check_id
|
|
435
|
+
AND l.field = 'failure_class') AS class,
|
|
436
|
+
EXISTS (SELECT 1 FROM gh_checks s WHERE s.commit_sha = k.commit_sha AND s.name = k.name
|
|
437
|
+
AND s.conclusion = 'success' AND s.completed_at > k.completed_at) AS flaky,
|
|
438
|
+
(SELECT {PR_KEY} FROM gh_commit_prs g JOIN settled_prs p ON p.repo = g.repo AND p.pr_number = g.pr_number
|
|
439
|
+
WHERE g.commit_sha = k.commit_sha ORDER BY g.pr_number LIMIT 1) AS pr
|
|
440
|
+
FROM gh_checks k
|
|
441
|
+
WHERE k.conclusion = 'failure' AND COALESCE(k.app, '') != 'entire'
|
|
442
|
+
AND k.completed_at >= ?""", (since,)).fetchall()
|
|
443
|
+
# Completed check runs per settled PR, so the site can scope failure rates to a set of sessions.
|
|
444
|
+
runs = dict(conn.execute(f"""
|
|
445
|
+
SELECT {PR_KEY}, COUNT(DISTINCT k.check_id) FROM gh_checks k
|
|
446
|
+
JOIN gh_commit_prs g ON g.commit_sha = k.commit_sha
|
|
447
|
+
JOIN settled_prs p ON p.repo = g.repo AND p.pr_number = g.pr_number
|
|
448
|
+
WHERE k.conclusion IS NOT NULL AND COALESCE(k.app, '') != 'entire'
|
|
449
|
+
AND k.completed_at >= ?
|
|
450
|
+
GROUP BY 1""", (since,)))
|
|
451
|
+
failures = [{"id": r[0], "name": r[1], "at": r[3], "class": r[4], "flaky": bool(r[5]), "pr": r[6],
|
|
452
|
+
**failure_places(r[2])} for r in rows if r[6] is not None]
|
|
453
|
+
return {"runs_by_pr": runs, "failures": failures}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def review_section(conn) -> list[dict]:
|
|
457
|
+
rows = conn.execute(f"""
|
|
458
|
+
SELECT c.comment_id, c.author, c.is_bot, c.kind, {PR_KEY}, c.is_resolved, c.is_outdated, c.path,
|
|
459
|
+
(SELECT value FROM event_labels l WHERE l.event_type = 'review_comment' AND l.event_id = c.comment_id
|
|
460
|
+
AND l.field = 'comment_type') AS type,
|
|
461
|
+
(SELECT value FROM event_labels l WHERE l.event_type = 'review_comment' AND l.event_id = c.comment_id
|
|
462
|
+
AND l.field = 'held_up') AS held
|
|
463
|
+
FROM gh_review_comments c JOIN settled_prs p ON p.repo = c.repo AND p.pr_number = c.pr_number
|
|
464
|
+
WHERE c.in_reply_to IS NULL""").fetchall()
|
|
465
|
+
return [{"id": r[0], "author": r[1], "bot": bool(r[2]), "kind": r[3], "pr": r[4], "resolved": r[5],
|
|
466
|
+
"outdated": r[6], "path": r[7] or None, "type": r[8], "held": r[9]} for r in rows]
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def prs_section(conn) -> dict:
|
|
470
|
+
return {r[0]: {"title": r[1], "state": "merged" if r[3] else r[2], "url": r[4]}
|
|
471
|
+
for r in conn.execute(f"SELECT {PR_KEY}, title, state, merged, url FROM gh_prs p")}
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def session_file(conn, labels, sid: str) -> dict:
|
|
475
|
+
calls: dict[int, list] = defaultdict(list)
|
|
476
|
+
for c in conn.execute("SELECT msg_idx, name, input, output, is_error, files, writes FROM tool_calls "
|
|
477
|
+
"WHERE session_id = ? ORDER BY msg_idx, call_idx", (sid,)):
|
|
478
|
+
try:
|
|
479
|
+
args = json.loads(c["input"] or "{}")
|
|
480
|
+
except json.JSONDecodeError:
|
|
481
|
+
args = c["input"]
|
|
482
|
+
if isinstance(args, dict):
|
|
483
|
+
keys = ("command", "cmd", "file_path", "path", "pattern", "url", "query", "description", "skill")
|
|
484
|
+
summary = next((str(args[k]) for k in keys if args.get(k)), None) or json.dumps(args)
|
|
485
|
+
else:
|
|
486
|
+
summary = str(args)
|
|
487
|
+
call = {"n": c["name"], "in": excerpt(summary, TOOL_IN_CAP)}
|
|
488
|
+
if c["is_error"]:
|
|
489
|
+
call["err"] = tail(c["output"], TOOL_OUT_CAP)
|
|
490
|
+
if c["writes"]:
|
|
491
|
+
call["w"] = json.loads(c["files"] or "[]")
|
|
492
|
+
calls[c["msg_idx"]].append(call)
|
|
493
|
+
msgs = []
|
|
494
|
+
for m in conn.execute("SELECT idx, role, user_kind, ts, text FROM messages WHERE session_id = ? ORDER BY idx",
|
|
495
|
+
(sid,)):
|
|
496
|
+
text = m["text"] or ""
|
|
497
|
+
item = {"i": m["idx"], "r": m["role"], "ts": m["ts"],
|
|
498
|
+
"t": text if len(text) <= MSG_CAP else text[:MSG_CAP] + "\n…[truncated]"}
|
|
499
|
+
if m["role"] == "user":
|
|
500
|
+
item["k"] = m["user_kind"]
|
|
501
|
+
if m["idx"] in calls:
|
|
502
|
+
item["c"] = calls[m["idx"]]
|
|
503
|
+
lab = labels.get((sid, m["idx"]))
|
|
504
|
+
if lab:
|
|
505
|
+
item["sig"] = signals_of(lab)
|
|
506
|
+
item["l"] = {f: [v, round(c, 2)] for f, (v, c) in lab.items() if v not in ("none", "false", None)}
|
|
507
|
+
msgs.append(item)
|
|
508
|
+
checkpoints = [{"sha": (r[0] or "")[:10], "msg": excerpt((r[1] or "").split("\n")[0], 120), "ts": r[2]}
|
|
509
|
+
for r in conn.execute("SELECT commit_sha, message, ts FROM checkpoints WHERE session_id = ? "
|
|
510
|
+
"ORDER BY ts", (sid,))]
|
|
511
|
+
return {"id": sid, "messages": msgs, "checkpoints": checkpoints}
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def pack_shards(blobs: dict[str, str]) -> list[list[str]]:
|
|
515
|
+
"""Group session ids into shards of about SHARD_BYTES, largest first; a session bigger than
|
|
516
|
+
that gets a shard to itself."""
|
|
517
|
+
shards: list[list[str]] = []
|
|
518
|
+
sizes: list[int] = []
|
|
519
|
+
for sid in sorted(blobs, key=lambda k: -len(blobs[k].encode())):
|
|
520
|
+
n = len(blobs[sid].encode())
|
|
521
|
+
slot = next((i for i, used in enumerate(sizes) if used + n <= SHARD_BYTES), None)
|
|
522
|
+
if slot is None:
|
|
523
|
+
shards.append([])
|
|
524
|
+
sizes.append(0)
|
|
525
|
+
slot = len(shards) - 1
|
|
526
|
+
shards[slot].append(sid)
|
|
527
|
+
sizes[slot] += n
|
|
528
|
+
return shards
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def skills_section(conn, ids: set[str]) -> list[dict]:
|
|
532
|
+
return [{"s": u["session_id"], "i": u["msg_idx"], "skill": u["skill"], "by": u["invoked_by"], "ts": u["ts"]}
|
|
533
|
+
for u in skills.uses(conn) if u["session_id"] in ids]
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def volume(conn, ids: set[str]) -> dict:
|
|
537
|
+
"""How much raw material the analysis read for sessions `ids`, for the methodology page."""
|
|
538
|
+
conn.execute("CREATE TEMP TABLE IF NOT EXISTS window_sessions (session_id TEXT PRIMARY KEY)")
|
|
539
|
+
conn.execute("DELETE FROM window_sessions")
|
|
540
|
+
conn.executemany("INSERT INTO window_sessions VALUES (?)", [(i,) for i in ids])
|
|
541
|
+
# PRs, checks and review comments reach the window through the sessions' PRs.
|
|
542
|
+
prs = ("SELECT DISTINCT repo, pr_number FROM session_pr_links "
|
|
543
|
+
"WHERE session_id IN (SELECT session_id FROM window_sessions)")
|
|
544
|
+
checks = (f"SELECT k.* FROM gh_checks k JOIN gh_commit_prs g ON g.commit_sha = k.commit_sha "
|
|
545
|
+
f"WHERE (g.repo, g.pr_number) IN ({prs})")
|
|
546
|
+
in_window = "session_id IN (SELECT session_id FROM window_sessions)"
|
|
547
|
+
|
|
548
|
+
def one(sql: str) -> int:
|
|
549
|
+
return conn.execute(sql).fetchone()[0]
|
|
550
|
+
|
|
551
|
+
return {
|
|
552
|
+
"messages": one(f"SELECT COUNT(*) FROM messages WHERE {in_window}"),
|
|
553
|
+
"tool_calls": one(f"SELECT COUNT(*) FROM tool_calls WHERE {in_window}"),
|
|
554
|
+
"prs": one(f"SELECT COUNT(*) FROM ({prs})"),
|
|
555
|
+
"checks": one(f"SELECT COUNT(DISTINCT check_id) FROM ({checks})"),
|
|
556
|
+
"check_failures": one(f"SELECT COUNT(DISTINCT check_id) FROM ({checks}) WHERE conclusion = 'failure'"),
|
|
557
|
+
"review_comments": one(f"SELECT COUNT(*) FROM gh_review_comments WHERE (repo, pr_number) IN ({prs})"),
|
|
558
|
+
"labels": one(f"SELECT (SELECT COUNT(*) FROM turn_labels WHERE {in_window}) "
|
|
559
|
+
f"+ (SELECT COUNT(*) FROM session_labels WHERE {in_window})"),
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
def projects_section(conn, picked: set[str]) -> list[dict]:
|
|
564
|
+
"""Every project with sessions, picked or not, biggest first: its size, and when synthesize
|
|
565
|
+
last finished a pass over it."""
|
|
566
|
+
project = session_projects(conn)
|
|
567
|
+
turns = dict(conn.execute("SELECT session_id, COUNT(*) FROM messages WHERE role = 'user' AND user_kind = 'human' "
|
|
568
|
+
"GROUP BY session_id"))
|
|
569
|
+
analysed = dict(conn.execute("SELECT project, analysed_at FROM project_runs"))
|
|
570
|
+
out: dict[str, dict] = {}
|
|
571
|
+
for sid, p in project.items():
|
|
572
|
+
if p:
|
|
573
|
+
row = out.setdefault(p, {"path": p, "name": Path(p).name or p, "sessions": 0, "turns": 0,
|
|
574
|
+
"picked": p in picked, "analysed_at": analysed.get(p)})
|
|
575
|
+
row["sessions"] += 1
|
|
576
|
+
row["turns"] += turns.get(sid, 0)
|
|
577
|
+
names = Counter(r["name"] for r in out.values())
|
|
578
|
+
for r in out.values():
|
|
579
|
+
if names[r["name"]] > 1: # two folders with one name show their parent too
|
|
580
|
+
r["name"] = "/".join(Path(r["path"]).parts[-2:])
|
|
581
|
+
return sorted(out.values(), key=lambda r: (-r["sessions"], r["path"]))
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def repo_of(prs: dict) -> str | None:
|
|
585
|
+
"""owner/name when every PR is in one repo; None with several, or without GitHub data."""
|
|
586
|
+
repos = {key.rsplit("#", 1)[0] for key in prs}
|
|
587
|
+
return repos.pop() if len(repos) == 1 else None
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def main(argv: list[str] | None = None) -> None:
|
|
591
|
+
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
592
|
+
parser.add_argument("--run", help="Findings run_id (default: latest with recommendations)")
|
|
593
|
+
parser.add_argument("--since", default="", metavar="YYYY-MM-DD",
|
|
594
|
+
help="Only sessions active since this date (default: all)")
|
|
595
|
+
args = parser.parse_args(argv)
|
|
596
|
+
|
|
597
|
+
conn: sqlite3.Connection = connect()
|
|
598
|
+
run_id = args.run or conn.execute("SELECT MAX(run_id) FROM recommendations").fetchone()[0] \
|
|
599
|
+
or conn.execute("SELECT MAX(run_id) FROM findings").fetchone()[0]
|
|
600
|
+
labels = labels_by_turn(conn)
|
|
601
|
+
picked = set(config.projects())
|
|
602
|
+
findings, recs, refs = findings_and_recs(conn, labels, run_id, picked)
|
|
603
|
+
sessions, flagged = sessions_section(conn, labels, args.since, picked)
|
|
604
|
+
authors = {s["id"]: s["author"] for s in sessions}
|
|
605
|
+
# GitHub rows reach the report through the sessions' PRs, so keep only those.
|
|
606
|
+
linked = {p["n"] for s in sessions for p in s["prs"]}
|
|
607
|
+
prs = {k: v for k, v in prs_section(conn).items() if k in linked}
|
|
608
|
+
ci = ci_section(conn, args.since)
|
|
609
|
+
ci = {"runs_by_pr": {k: v for k, v in ci["runs_by_pr"].items() if k in linked},
|
|
610
|
+
"failures": [f for f in ci["failures"] if f["pr"] in linked]}
|
|
611
|
+
for rec in recs:
|
|
612
|
+
touched = {sid for e in rec["evidence"] for sid in evidence_sessions(refs[e])}
|
|
613
|
+
rec["sessions"] = sorted(touched)
|
|
614
|
+
rec["people"] = len({authors.get(s) for s in touched} - {None})
|
|
615
|
+
started = [s["started"] for s in sessions if s["started"]]
|
|
616
|
+
vol = volume(conn, set(authors))
|
|
617
|
+
index = {
|
|
618
|
+
"meta": {"generated": datetime.now(UTC).isoformat(timespec="seconds"), "run_id": run_id,
|
|
619
|
+
"from": args.since or min(started, default=None), "to": max(started, default=None),
|
|
620
|
+
"sessions": len(sessions), "repo": repo_of(prs), "min_conf": MIN_CONF,
|
|
621
|
+
"human_turns": sum(s["human_turns"] for s in sessions), **vol,
|
|
622
|
+
# Optional data. The site hides pages, columns and tiles for whatever is False.
|
|
623
|
+
"has": {"github": bool(prs),
|
|
624
|
+
"people": len(set(authors.values()) - {None}) > 1, # one person: nothing to compare
|
|
625
|
+
"checkpoints": any(s["checkpoints"] for s in sessions),
|
|
626
|
+
"projects": bool(sessions),
|
|
627
|
+
"labels": vol["labels"] > 0}},
|
|
628
|
+
"levers": LEVERS, "recommendations": recs, "findings": findings, "refs": refs,
|
|
629
|
+
"sessions": sessions, "flagged": flagged, "files": hotspot_files(conn, labels, set(authors))[:60],
|
|
630
|
+
"projects": projects_section(conn, picked),
|
|
631
|
+
"ci": ci, "reviews": [r for r in review_section(conn) if r["pr"] in linked], "prs": prs,
|
|
632
|
+
"skills": skills_section(conn, set(authors)),
|
|
633
|
+
}
|
|
634
|
+
SITE_OUT.mkdir(parents=True, exist_ok=True)
|
|
635
|
+
for static in SITE_SRC.iterdir():
|
|
636
|
+
if static.is_file():
|
|
637
|
+
shutil.copy(static, SITE_OUT / static.name)
|
|
638
|
+
out = SITE_OUT / "data"
|
|
639
|
+
shard_dir = out / "sessions"
|
|
640
|
+
shard_dir.mkdir(parents=True, exist_ok=True)
|
|
641
|
+
for stale in shard_dir.glob("*.json"):
|
|
642
|
+
stale.unlink()
|
|
643
|
+
blobs = {s["id"]: json.dumps(redact_tree(session_file(conn, labels, s["id"])), ensure_ascii=False,
|
|
644
|
+
separators=(",", ":"))
|
|
645
|
+
for s in sessions}
|
|
646
|
+
shards = pack_shards(blobs)
|
|
647
|
+
shard_of = {}
|
|
648
|
+
for n, ids in enumerate(shards):
|
|
649
|
+
name = f"s{n:02d}"
|
|
650
|
+
(shard_dir / f"{name}.json").write_text("{" + ",".join(f"{json.dumps(sid)}:{blobs[sid]}" for sid in ids) + "}")
|
|
651
|
+
shard_of.update(dict.fromkeys(ids, name))
|
|
652
|
+
for s in sessions:
|
|
653
|
+
s["shard"] = shard_of[s["id"]]
|
|
654
|
+
(out / "index.json").write_text(json.dumps(redact_tree(index), ensure_ascii=False, separators=(",", ":")))
|
|
655
|
+
size = sum(p.stat().st_size for p in out.rglob("*.json"))
|
|
656
|
+
print(f"run {run_id or '(none yet)'}: {len(recs)} recommendations, {len(findings)} findings, {len(sessions)} sessions "
|
|
657
|
+
f"in {len(shards)} shards; {size / 1e6:.1f} MB in {out}")
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
if __name__ == "__main__":
|
|
661
|
+
main()
|