agent-test-kit 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_test_kit-0.0.1.dist-info/METADATA +72 -0
- agent_test_kit-0.0.1.dist-info/RECORD +18 -0
- agent_test_kit-0.0.1.dist-info/WHEEL +5 -0
- agent_test_kit-0.0.1.dist-info/entry_points.txt +2 -0
- agent_test_kit-0.0.1.dist-info/licenses/LICENSE +202 -0
- agent_test_kit-0.0.1.dist-info/top_level.txt +1 -0
- atkit/__init__.py +9 -0
- atkit/claude_code.py +283 -0
- atkit/cli.py +133 -0
- atkit/events.py +121 -0
- atkit/kept.py +424 -0
- atkit/label.html +249 -0
- atkit/labels.py +49 -0
- atkit/out.py +56 -0
- atkit/page.py +191 -0
- atkit/stages.py +82 -0
- atkit/tally.py +40 -0
- atkit/writer.py +72 -0
atkit/kept.py
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"""Was a result kept? Four signals read from the events and the repository's history, and one rule that turns them
|
|
2
|
+
into a label. A signal is a sign for a person to read and not proof, so each is kept beside the label."""
|
|
3
|
+
|
|
4
|
+
from collections import Counter, defaultdict
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timedelta, timezone
|
|
7
|
+
from fractions import Fraction
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
import hashlib
|
|
10
|
+
from itertools import groupby
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
import random
|
|
13
|
+
import re
|
|
14
|
+
import subprocess
|
|
15
|
+
|
|
16
|
+
# ponytail: a list of names, for a harness that does not say. One that knows marks a call_start with "check": true or false.
|
|
17
|
+
CHECKS = frozenset({"pytest", "tox", "nox", "unittest", "mvn", "mvnw", "gradle", "gradlew", "npm", "pnpm", "yarn", "make", "cargo", "go", "dotnet", "ctest", "bazel"})
|
|
18
|
+
# ponytail: test files and their assertions are found by pattern. Make these settings when a repository needs its own.
|
|
19
|
+
TEST_PATH = re.compile(r"(^|/)(tests?|__tests__|spec)/|(^|/)test_[^/]*$|(_test|\.test|\.spec|Tests?)\.\w+$")
|
|
20
|
+
ASSERTS = re.compile(r"\bassert|\bexpect\(|\bshould\b")
|
|
21
|
+
SKIPS = re.compile(r"@(pytest\.mark\.)?(skip|xfail)|@Disabled|@Ignore|\.skip\(|\bxit\(|\bt\.Skip\(")
|
|
22
|
+
DRAWS, SEED = 2000, 1 # a fixed seed, so the same results always give the same range
|
|
23
|
+
GOOD = {"check": "passed", "tests": "not weakened", "merged": "merged as it was"} # with its lines left alone, these make a result kept
|
|
24
|
+
WINDOW = timedelta(days=7) # how long after a result a commit of its files still counts as its commit
|
|
25
|
+
# The share of a result's lines that has to be gone for it to count as reworked or rewritten. A fraction and not a
|
|
26
|
+
# float, so that exactly a third is a third.
|
|
27
|
+
REWORKED = Fraction(1, 3)
|
|
28
|
+
MIN = 8 # a line shorter than this, once stripped, is too common to follow: a lone brace, an else
|
|
29
|
+
LOOK = 10 # how many later commits of a result's files are looked at for the one that holds its lines
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class Result:
|
|
34
|
+
"""One result the agent called ready, with its signals and its label. Four signals make the label: `check`, `tests`,
|
|
35
|
+
`again` and `merged`. `follow_up` is read only where no call names its lines or its files."""
|
|
36
|
+
|
|
37
|
+
task: str
|
|
38
|
+
turn: int
|
|
39
|
+
at: datetime
|
|
40
|
+
commit: str | None
|
|
41
|
+
signals: dict
|
|
42
|
+
label: str
|
|
43
|
+
implied: bool = False # the agent made no claim: its turn ended after it had changed files
|
|
44
|
+
found: bool = False # the commit was not logged: it is the first later commit that touches the result's files
|
|
45
|
+
# What a person needs to know a result again when they label it by hand. Both stay on the machine: no report prints
|
|
46
|
+
# them, and they are written nowhere.
|
|
47
|
+
files: tuple = () # the files the result wrote, as its calls named them
|
|
48
|
+
repo: str | None = None # the root of the repository it was made in
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def key(self):
|
|
52
|
+
"""A name for the result that stays the same from one reading of the events to the next: its task and its
|
|
53
|
+
time. A host keeps a person's own label of the result under it."""
|
|
54
|
+
return f"{self.task}|{self.at.isoformat()}"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def label(signals):
|
|
58
|
+
"""The rule: a strong bad sign decides first, then all four good signs, and every other result is not clear."""
|
|
59
|
+
if signals["merged"] in ("reverted", "reworked") or signals["again"] == "rewritten" or signals["check"] == "failed":
|
|
60
|
+
return "not kept"
|
|
61
|
+
# Was the result's work left alone? Where no call names its lines or files, the only sign is whether the person
|
|
62
|
+
# wrote again.
|
|
63
|
+
untouched = signals["again"] == "left alone" or (signals["again"] == "not known" and signals["follow_up"] == "none")
|
|
64
|
+
if untouched and all(signals[name] == good for name, good in GOOD.items()):
|
|
65
|
+
return "kept"
|
|
66
|
+
return "not clear"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def short(text):
|
|
70
|
+
"""The first ten hexadecimal digits of a text's SHA-1. It names the text and protects nothing."""
|
|
71
|
+
return hashlib.sha1(text.encode(errors="replace"), usedforsecurity=False).hexdigest()[:10]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def fingerprint(line):
|
|
75
|
+
"""A short name for a line's text, the same however the line is indented: enough to tell whether the line is still
|
|
76
|
+
there, and not enough to read it. None for a line too short to tell apart. A harness in another language makes the
|
|
77
|
+
same fingerprints, so what is taken off a line's ends is spelled out here and not left to Python's own idea."""
|
|
78
|
+
text = line.strip(" \t\r\n\v\f")
|
|
79
|
+
return short(text) if len(text) >= MIN else None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def prints(text):
|
|
83
|
+
"""The fingerprints of the lines of a text that are long enough to follow. A line ends at a line feed alone."""
|
|
84
|
+
return {fingerprint(line) for line in text.split("\n")} - {None}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def is_check(start):
|
|
88
|
+
"""Whether a call is a build or a test. A harness that says so, either way, is believed; where it says nothing,
|
|
89
|
+
the program's name decides."""
|
|
90
|
+
return bool(start["check"]) if start.get("check") is not None else start.get("name") in CHECKS
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def check(events, result, counts=None):
|
|
94
|
+
"""Checked at the end: did a build or a test pass after the last call that could write?
|
|
95
|
+
`events` are those of the result's task. `counts` says of a file whether it is part of the work: a call that names
|
|
96
|
+
its files and wrote none of the work, only a note beside it, is not the last write."""
|
|
97
|
+
earlier = [event for event in events if event["at"] <= result["at"]]
|
|
98
|
+
ends = {event["id"]: event for event in earlier if event["kind"] == "call_end"}
|
|
99
|
+
calls = [(start, ends[start["id"]]) for start in earlier if start["kind"] == "call_start" and start["id"] in ends]
|
|
100
|
+
# A test run writes caches of its own, so only the other calls say when the work was last written.
|
|
101
|
+
work = [end for start, end in calls if not is_check(start)]
|
|
102
|
+
if work and all(end.get("wrote") is None for end in work):
|
|
103
|
+
return "not known" # no call says whether it wrote, so the last write cannot be placed
|
|
104
|
+
writes = [end["at"] for end in work if end.get("wrote") and (counts is None or not end.get("files") or any(map(counts, end["files"])))]
|
|
105
|
+
checks = [(start, end) for start, end in calls if is_check(start) and (not writes or start["at"] >= max(writes))]
|
|
106
|
+
if not checks:
|
|
107
|
+
return "none ran"
|
|
108
|
+
last = max(checks, key=lambda call: call[1]["at"])[1]
|
|
109
|
+
return "failed" if last.get("failed") else "passed"
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def follow_up(events, result):
|
|
113
|
+
"""Did the person send another message on the task after the result? In a long conversation one nearly always
|
|
114
|
+
comes, so this is read only where no call names its lines or its files."""
|
|
115
|
+
messages = [event for event in events if event["kind"] == "message"]
|
|
116
|
+
if not messages:
|
|
117
|
+
return "not known" # this harness logs no messages
|
|
118
|
+
return "came" if any(event["at"] > result["at"] for event in messages) else "none"
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def git(repo, *arguments):
|
|
122
|
+
try:
|
|
123
|
+
done = subprocess.run(["git", "-C", str(repo), *arguments], capture_output=True)
|
|
124
|
+
except FileNotFoundError:
|
|
125
|
+
raise ValueError("git is not installed, and reading a repository needs it") from None
|
|
126
|
+
# Not as text: Python would read it in the machine's own encoding and end a line at a lone carriage return too.
|
|
127
|
+
return done.returncode, done.stdout.decode("utf-8", "replace")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@lru_cache(maxsize=None)
|
|
131
|
+
def toplevel(folder):
|
|
132
|
+
"""The root of the repository a folder lies in, or None when it lies in none."""
|
|
133
|
+
failed, root = git(folder, "rev-parse", "--show-toplevel")
|
|
134
|
+
return root.strip() if not failed and root.strip() else None
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def inside(repo, path):
|
|
138
|
+
"""A path as the repository names it, from its root. None for a path that lies outside it."""
|
|
139
|
+
if not Path(path).is_absolute():
|
|
140
|
+
return path
|
|
141
|
+
try:
|
|
142
|
+
return str(Path(path).resolve().relative_to(Path(repo).resolve()))
|
|
143
|
+
except ValueError:
|
|
144
|
+
return None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@lru_cache(maxsize=None)
|
|
148
|
+
def home(path):
|
|
149
|
+
"""The root of the repository a file is work in. None for a note or a scratch file, which an agent also writes: one
|
|
150
|
+
that lies in no repository, in a home or a temporary folder, or one that its own repository ignores. A checkout that
|
|
151
|
+
lies inside another one, in a folder the outer one ignores, is a repository of its own.
|
|
152
|
+
ponytail: one question to git for each file; ask for a result's files in one go when reports get slow."""
|
|
153
|
+
folder = Path(path).parent
|
|
154
|
+
while not folder.is_dir() and folder != folder.parent: # the folder may be gone by now
|
|
155
|
+
folder = folder.parent
|
|
156
|
+
root = toplevel(str(folder))
|
|
157
|
+
name = inside(root, path) if root else None
|
|
158
|
+
# git answers 0 for a file it ignores. A file it tracks is never ignored, whatever pattern it matches.
|
|
159
|
+
return None if name is None or git(root, "check-ignore", "--quiet", "--", name)[0] == 0 else root
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def is_work(path):
|
|
163
|
+
"""Whether a file is part of the work, and not a note beside it. A path from a repository's root is."""
|
|
164
|
+
return not Path(path).is_absolute() or home(path) is not None
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def repository(lines, files, asked=None):
|
|
168
|
+
"""The repository a result's work lies in: the one that holds most of the lines it wrote, each file with no line
|
|
169
|
+
named counting as one. `asked` is the repository of the folder the work was asked in, which wins a tie and stands
|
|
170
|
+
where the files tell none."""
|
|
171
|
+
weight = Counter()
|
|
172
|
+
for path in set(lines) | set(files):
|
|
173
|
+
if Path(path).is_absolute() and home(path):
|
|
174
|
+
weight[home(path)] += len(lines.get(path, ())) or 1
|
|
175
|
+
most = sorted(root for root, count in weight.items() if count == max(weight.values()))
|
|
176
|
+
return asked if asked in most or not most else most[0]
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def changed(repo, commit):
|
|
180
|
+
"""The lines a commit added and removed, as (path, sign, text). Nothing when the commit cannot be read."""
|
|
181
|
+
path = ""
|
|
182
|
+
for line in git(repo, "show", "--format=", "--unified=0", commit)[1].split("\n"):
|
|
183
|
+
if line.startswith("diff --git "):
|
|
184
|
+
path = line.rpartition(" b/")[2]
|
|
185
|
+
elif line.startswith(("+", "-")) and not line.startswith(("+++", "---")):
|
|
186
|
+
yield path, line[0], line[1:]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def tests(repo, commit):
|
|
190
|
+
"""Tests not weakened: did the change remove or skip a test, or remove more assertions than it added in a test file?"""
|
|
191
|
+
failed, deleted = git(repo, "show", "--format=", "--diff-filter=D", "--name-only", commit)
|
|
192
|
+
if failed:
|
|
193
|
+
return "not known"
|
|
194
|
+
if any(TEST_PATH.search(path) for path in deleted.splitlines()):
|
|
195
|
+
return "weakened"
|
|
196
|
+
balance = defaultdict(int)
|
|
197
|
+
for path, sign, text in changed(repo, commit):
|
|
198
|
+
if TEST_PATH.search(path):
|
|
199
|
+
if sign == "+" and SKIPS.search(text):
|
|
200
|
+
return "weakened"
|
|
201
|
+
if ASSERTS.search(text):
|
|
202
|
+
balance[path] += 1 if sign == "+" else -1
|
|
203
|
+
return "weakened" if any(count < 0 for count in balance.values()) else "not weakened"
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def added_by(repo, commit):
|
|
207
|
+
"""The lines a commit added, as {path: fingerprints}."""
|
|
208
|
+
lines = defaultdict(set)
|
|
209
|
+
for path, sign, text in changed(repo, commit):
|
|
210
|
+
if sign == "+" and fingerprint(text):
|
|
211
|
+
lines[path].add(fingerprint(text))
|
|
212
|
+
return dict(lines)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def missing(repo, revision, lines):
|
|
216
|
+
"""The share of these lines, {path: fingerprints}, that the files of a commit do not hold. A file that is gone has
|
|
217
|
+
lost all its lines. None when none of the lines lies in the repository."""
|
|
218
|
+
total = gone = 0
|
|
219
|
+
for path, wanted in lines.items():
|
|
220
|
+
name = inside(repo, path)
|
|
221
|
+
if name is None or not wanted:
|
|
222
|
+
continue
|
|
223
|
+
failed, text = git(repo, "show", f"{revision}:{name}")
|
|
224
|
+
total += len(wanted)
|
|
225
|
+
gone += len(wanted) if failed else len(wanted - prints(text))
|
|
226
|
+
return Fraction(gone, total) if total else None
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@lru_cache(maxsize=None)
|
|
230
|
+
def default_branch(repo):
|
|
231
|
+
"""The branch a result has to reach: the remote's default branch, which is where a merged pull request lands, else
|
|
232
|
+
a local main or master. None when the repository has none of them."""
|
|
233
|
+
failed, name = git(repo, "symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD")
|
|
234
|
+
if not failed and name.strip():
|
|
235
|
+
return name.strip()
|
|
236
|
+
for name in ("main", "master"):
|
|
237
|
+
if git(repo, "rev-parse", "--verify", "--quiet", name)[0] == 0:
|
|
238
|
+
return name
|
|
239
|
+
return None
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def committed(repo, commit):
|
|
243
|
+
"""When a commit was made, or None when it cannot be read."""
|
|
244
|
+
failed, seconds = git(repo, "show", "--no-patch", "--format=%ct", commit + "^{commit}")
|
|
245
|
+
try:
|
|
246
|
+
return None if failed else datetime.fromtimestamp(int(seconds), timezone.utc)
|
|
247
|
+
except ValueError:
|
|
248
|
+
return None
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def arrived(repo, main, since, lines):
|
|
252
|
+
"""The commit with which a piece of work reached the main branch when no commit of that name is there, as after a
|
|
253
|
+
squash merge: the first one on the branch, made at `since` or later, that touches the work's files and holds more
|
|
254
|
+
than a third of its lines. None when there is none yet. ponytail: the first LOOK commits are looked at."""
|
|
255
|
+
names = sorted(name for name in (inside(repo, path) for path in lines) if name is not None)
|
|
256
|
+
failed, commits = git(repo, "log", main, "--format=%H", f"--since={since.isoformat()}", "--", *names) if names and since else (1, "")
|
|
257
|
+
return next((one for one in ([] if failed else commits.split()[::-1][:LOOK]) if (missing(repo, one, lines) or 0) <= 1 - REWORKED), None)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def merged(repo, commit, main=None, lines=None, found=False):
|
|
261
|
+
"""Merged as it was: did the commit reach the main branch as it was? Without `main`, the repository's own
|
|
262
|
+
default branch is used. `lines` are the lines the result wrote, when its calls named them; `found` says that the
|
|
263
|
+
commit is one a person made of the agent's work."""
|
|
264
|
+
main = main or default_branch(str(repo))
|
|
265
|
+
failed, name = git(repo, "rev-parse", "--verify", "--quiet", commit + "^{commit}")
|
|
266
|
+
if failed or not main or git(repo, "rev-parse", "--verify", "--quiet", main)[0]:
|
|
267
|
+
return "not known"
|
|
268
|
+
name = name.strip()
|
|
269
|
+
# ponytail: a revert is found by the message git writes for it, so a revert of a rebased copy is missed.
|
|
270
|
+
if git(repo, "log", main, "--format=%H", "--fixed-strings", f"--grep=This reverts commit {name}")[1].strip():
|
|
271
|
+
return "reverted"
|
|
272
|
+
# A person committed the agent's work: did the agent's own lines get into that commit?
|
|
273
|
+
if found and lines and (missing(repo, name, lines) or 0) >= REWORKED:
|
|
274
|
+
return "reworked"
|
|
275
|
+
if git(repo, "merge-base", "--is-ancestor", name, main)[0] == 0:
|
|
276
|
+
return "merged as it was"
|
|
277
|
+
# A rebase or a cherry-pick gives the change a new name on the main branch; git cherry finds it by its patch.
|
|
278
|
+
if git(repo, "cherry", main, name, name + "^")[1].startswith("-"):
|
|
279
|
+
return "merged as it was"
|
|
280
|
+
# A squash merge, or a merge that changed the work, leaves no commit to find on the main branch. So the lines are
|
|
281
|
+
# looked for there, in the branch as it stood when the work arrived: a change made after that is new work.
|
|
282
|
+
wanted = lines or added_by(repo, name)
|
|
283
|
+
there = arrived(repo, main, committed(repo, name), wanted)
|
|
284
|
+
if there is None:
|
|
285
|
+
return "not merged yet"
|
|
286
|
+
return "reworked" if (missing(repo, there, wanted) or 0) >= REWORKED else "merged as it was"
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def written(events, after, until):
|
|
290
|
+
"""The files the task's calls wrote in a stretch of time, or None when none of them names its files."""
|
|
291
|
+
files = {path for event in events if event["kind"] == "call_end" and event.get("wrote") and (after is None or event["at"] > after) and event["at"] <= until
|
|
292
|
+
for path in event.get("files") or ()}
|
|
293
|
+
return files or None
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def lines_of(events, after, until):
|
|
297
|
+
"""The lines the task's calls wrote in a stretch of time and had not taken out again by its end, as
|
|
298
|
+
{path: fingerprints}. Empty when no call names its lines."""
|
|
299
|
+
lines = defaultdict(set)
|
|
300
|
+
for event in events:
|
|
301
|
+
if event["kind"] == "call_end" and (after is None or event["at"] > after) and event["at"] <= until:
|
|
302
|
+
for path, removed in (event.get("removed") or {}).items():
|
|
303
|
+
lines[path] -= set(removed)
|
|
304
|
+
for path, added in (event.get("added") or {}).items():
|
|
305
|
+
lines[path] |= set(added)
|
|
306
|
+
return {path: found for path, found in lines.items() if found}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def rewritten(events, at, lines, until=None):
|
|
310
|
+
"""The share of a result's lines that later calls of the task took out and did not put back, up to `until`: the
|
|
311
|
+
time the work was committed, after which a change is new work."""
|
|
312
|
+
alive = {path: set(found) for path, found in lines.items()}
|
|
313
|
+
for event in events:
|
|
314
|
+
if event["kind"] == "call_end" and event["at"] > at and (until is None or event["at"] <= until):
|
|
315
|
+
for path, removed in (event.get("removed") or {}).items():
|
|
316
|
+
if path in alive:
|
|
317
|
+
alive[path] -= set(removed)
|
|
318
|
+
for path, added in (event.get("added") or {}).items():
|
|
319
|
+
if path in alive:
|
|
320
|
+
alive[path] |= set(added) & lines[path]
|
|
321
|
+
return 1 - Fraction(sum(map(len, alive.values())), sum(map(len, lines.values())))
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def later_commit(repo, files, at, lines=None):
|
|
325
|
+
"""The commit a person made of a result: the first one within WINDOW after it, on any branch, that touches its files
|
|
326
|
+
and, where its lines are known, still holds a third of them or more. Another commit can touch the same files without
|
|
327
|
+
holding the work at all, so such a commit is passed over. None when there is none."""
|
|
328
|
+
names = sorted(name for name in (inside(repo, path) for path in files) if name is not None)
|
|
329
|
+
if not names:
|
|
330
|
+
return None
|
|
331
|
+
# ponytail: one search of the history for each result. Search once for each repository when reports get slow.
|
|
332
|
+
failed, commits = git(repo, "log", "--all", "--format=%H", f"--since={at.isoformat()}", f"--until={(at + WINDOW).isoformat()}", "--", *names)
|
|
333
|
+
candidates = [] if failed else commits.split()[::-1][:LOOK] # git lists the newest first
|
|
334
|
+
if not lines:
|
|
335
|
+
return candidates[0] if candidates else None
|
|
336
|
+
return next((one for one in candidates if (missing(repo, one, lines) or 0) <= 1 - REWORKED), None)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def results(events, repo=None, main=None):
|
|
340
|
+
"""Every result the agent called ready, labelled, one at a time. The events of a task must come together, as `read`
|
|
341
|
+
gives them, so that memory holds one task and never all of them; ValueError when a task comes back after another.
|
|
342
|
+
Without a repository, its own or `repo`, or for a result with no commit, the two signals that read the repository
|
|
343
|
+
are not known."""
|
|
344
|
+
seen = set()
|
|
345
|
+
for task, group in groupby(events, key=lambda event: event["task"]):
|
|
346
|
+
if task in seen:
|
|
347
|
+
raise ValueError(f"the events of task {task!r} do not come together")
|
|
348
|
+
seen.add(task)
|
|
349
|
+
own, before = list(group), None
|
|
350
|
+
for result in own:
|
|
351
|
+
if result["kind"] != "result":
|
|
352
|
+
continue
|
|
353
|
+
since, before = before, result["at"]
|
|
354
|
+
if not result.get("ready"):
|
|
355
|
+
continue
|
|
356
|
+
folder = result.get("repo") or repo # a result may name the folder it was asked in
|
|
357
|
+
commit = result.get("commit")
|
|
358
|
+
files, lines = written(own, since, result["at"]), lines_of(own, since, result["at"])
|
|
359
|
+
# One session can work in several repositories, so a result's repository is the one its files lie in.
|
|
360
|
+
where = repository(lines, files or (), toplevel(str(folder)) if folder else None)
|
|
361
|
+
# Where the result's repository is known, the notes and scratch files written beside the work are left out.
|
|
362
|
+
# Without one there is no telling them apart, so every file counts.
|
|
363
|
+
counts = is_work if where else None
|
|
364
|
+
if counts:
|
|
365
|
+
named = bool(files or lines)
|
|
366
|
+
files = {path for path in files or () if counts(path)} or None
|
|
367
|
+
lines = {path: found for path, found in lines.items() if counts(path)}
|
|
368
|
+
# A result implied by files having changed is none when those files were notes alone. A claim the
|
|
369
|
+
# agent made itself stays a result whatever it wrote.
|
|
370
|
+
if result.get("implied") and named and not files and not lines:
|
|
371
|
+
continue
|
|
372
|
+
# A person often commits the agent's work later.
|
|
373
|
+
found = not commit and bool(where)
|
|
374
|
+
if found:
|
|
375
|
+
commit = later_commit(where, set(files or ()) | set(lines), result["at"], lines)
|
|
376
|
+
readable = bool(where and commit)
|
|
377
|
+
# Work is accepted when it is committed. What is changed after that is new work, so the result's lines and
|
|
378
|
+
# files are followed up to its commit, and to the end of the task where it has none.
|
|
379
|
+
until = committed(where, commit) if readable else None
|
|
380
|
+
# Work often goes on in steps on the same files, so the lines are followed where they are known.
|
|
381
|
+
if lines:
|
|
382
|
+
again = "rewritten" if rewritten(own, result["at"], lines, until) >= REWORKED else "left alone"
|
|
383
|
+
elif files is not None:
|
|
384
|
+
later = any(event["kind"] == "call_end" and result["at"] < event["at"] and (until is None or event["at"] <= until) and files & set(event.get("files") or ())
|
|
385
|
+
for event in own)
|
|
386
|
+
again = "changed again" if later else "left alone"
|
|
387
|
+
else:
|
|
388
|
+
again = "not known"
|
|
389
|
+
signals = {"check": check(own, result, counts), "tests": tests(where, commit) if readable else "not known", "again": again,
|
|
390
|
+
"merged": merged(where, commit, main, lines, bool(found)) if readable else "not known", "follow_up": follow_up(own, result)}
|
|
391
|
+
yield Result(task, result["turn"], result["at"], commit, signals, label(signals), bool(result.get("implied")), bool(found and commit),
|
|
392
|
+
tuple(sorted(set(files or ()) | set(lines))), where)
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def share(results):
|
|
396
|
+
"""The kept share in percent, as (share, low, high): the results that kept over those that kept or did
|
|
397
|
+
not. No decided result gives no share."""
|
|
398
|
+
by_task = defaultdict(lambda: [0, 0])
|
|
399
|
+
for result in results:
|
|
400
|
+
if result.label != "not clear":
|
|
401
|
+
by_task[result.task][0] += result.label == "kept"
|
|
402
|
+
by_task[result.task][1] += 1
|
|
403
|
+
return ranged(list(by_task.values()))
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def ranged(tasks):
|
|
407
|
+
"""A share in percent with its range, as (share, low, high), from [counted, out of] for each task. The range says
|
|
408
|
+
where the share could lie had other tasks come. Whole tasks are drawn again with replacement, because the results
|
|
409
|
+
of one task share its code and its mistakes. Fewer than three tasks give no range, nor do results that all ended the
|
|
410
|
+
same way, and no task gives no share."""
|
|
411
|
+
if not tasks:
|
|
412
|
+
return None, None, None
|
|
413
|
+
|
|
414
|
+
def percent(drawn):
|
|
415
|
+
return 100 * sum(counted for counted, _ in drawn) / sum(out_of for _, out_of in drawn)
|
|
416
|
+
|
|
417
|
+
counted, out_of = sum(counted for counted, _ in tasks), sum(out_of for _, out_of in tasks)
|
|
418
|
+
# With every result on one side, drawing again can only give that side back: a range of "0 to 0" would look known
|
|
419
|
+
# for sure. So there is none.
|
|
420
|
+
if len(tasks) < 3 or counted in (0, out_of):
|
|
421
|
+
return percent(tasks), None, None
|
|
422
|
+
rng = random.Random(SEED)
|
|
423
|
+
draws = sorted(percent(rng.choices(tasks, k=len(tasks))) for _ in range(DRAWS))
|
|
424
|
+
return percent(tasks), draws[int(0.025 * DRAWS)], draws[int(0.975 * DRAWS) - 1]
|