agent-usage-tracker 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.
Files changed (113) hide show
  1. agent_usage_tracker/__init__.py +8 -0
  2. agent_usage_tracker/backup.py +225 -0
  3. agent_usage_tracker/cli.py +479 -0
  4. agent_usage_tracker/collectors/__init__.py +115 -0
  5. agent_usage_tracker/collectors/_util.py +310 -0
  6. agent_usage_tracker/collectors/activity_common.py +294 -0
  7. agent_usage_tracker/collectors/activitywatch.py +394 -0
  8. agent_usage_tracker/collectors/claude_code.py +484 -0
  9. agent_usage_tracker/collectors/claude_code_activity.py +317 -0
  10. agent_usage_tracker/collectors/codex.py +612 -0
  11. agent_usage_tracker/collectors/commits.py +154 -0
  12. agent_usage_tracker/collectors/cursor.py +193 -0
  13. agent_usage_tracker/collectors/cursor_api.py +247 -0
  14. agent_usage_tracker/collectors/cursor_cli.py +592 -0
  15. agent_usage_tracker/collectors/cursor_sdk.py +95 -0
  16. agent_usage_tracker/collectors/generic_sdk.py +100 -0
  17. agent_usage_tracker/collectors/pi.py +397 -0
  18. agent_usage_tracker/collectors/session_digest.py +379 -0
  19. agent_usage_tracker/collectors/t3.py +490 -0
  20. agent_usage_tracker/config.py +392 -0
  21. agent_usage_tracker/coverage.py +615 -0
  22. agent_usage_tracker/coverage_catalog.json +588 -0
  23. agent_usage_tracker/cursor_run.py +62 -0
  24. agent_usage_tracker/diagnostics.py +74 -0
  25. agent_usage_tracker/episode_store.py +78 -0
  26. agent_usage_tracker/event.py +132 -0
  27. agent_usage_tracker/git_day_cache.py +128 -0
  28. agent_usage_tracker/git_days.py +499 -0
  29. agent_usage_tracker/git_safe.py +58 -0
  30. agent_usage_tracker/harness_inventory.py +208 -0
  31. agent_usage_tracker/history_steers.py +88 -0
  32. agent_usage_tracker/improve/__init__.py +1 -0
  33. agent_usage_tracker/improve/case_view.py +112 -0
  34. agent_usage_tracker/improve/cli.py +138 -0
  35. agent_usage_tracker/improve/desktop.py +347 -0
  36. agent_usage_tracker/improve/evidence.py +322 -0
  37. agent_usage_tracker/improve/judge.py +671 -0
  38. agent_usage_tracker/improve/proposals.py +72 -0
  39. agent_usage_tracker/improve/rubric.py +360 -0
  40. agent_usage_tracker/improve/rubrics/avoidable_recovery.md +20 -0
  41. agent_usage_tracker/improve/rubrics/code_quality.md +22 -0
  42. agent_usage_tracker/improve/rubrics/completion_supported.md +20 -0
  43. agent_usage_tracker/improve/rubrics/redundant_tests.md +21 -0
  44. agent_usage_tracker/improve/rubrics/verbosity.md +21 -0
  45. agent_usage_tracker/improve/runtime.py +117 -0
  46. agent_usage_tracker/improve/schedule.py +291 -0
  47. agent_usage_tracker/improve/search_check.py +563 -0
  48. agent_usage_tracker/improve/store.py +244 -0
  49. agent_usage_tracker/improve/studies.py +285 -0
  50. agent_usage_tracker/improve/study_cli.py +46 -0
  51. agent_usage_tracker/improve/study_decision.py +121 -0
  52. agent_usage_tracker/improve/study_followup.py +174 -0
  53. agent_usage_tracker/improve/t3.py +269 -0
  54. agent_usage_tracker/improve/trial.py +318 -0
  55. agent_usage_tracker/improve/validation.py +148 -0
  56. agent_usage_tracker/inspector.py +350 -0
  57. agent_usage_tracker/label/__init__.py +22 -0
  58. agent_usage_tracker/label/backends.py +742 -0
  59. agent_usage_tracker/label/bakeoff.py +661 -0
  60. agent_usage_tracker/label/baseline.py +173 -0
  61. agent_usage_tracker/label/clef.py +258 -0
  62. agent_usage_tracker/label/context.py +382 -0
  63. agent_usage_tracker/label/core.py +355 -0
  64. agent_usage_tracker/label/covered_run.py +92 -0
  65. agent_usage_tracker/label/evalset.py +154 -0
  66. agent_usage_tracker/label/gold.py +209 -0
  67. agent_usage_tracker/label/local.py +326 -0
  68. agent_usage_tracker/label/local_parallel.py +232 -0
  69. agent_usage_tracker/label/merge.py +57 -0
  70. agent_usage_tracker/label/outcome_coverage.py +57 -0
  71. agent_usage_tracker/label/replay.py +194 -0
  72. agent_usage_tracker/label/run.py +339 -0
  73. agent_usage_tracker/label/tasks.py +579 -0
  74. agent_usage_tracker/label/teach.py +145 -0
  75. agent_usage_tracker/local_page.py +128 -0
  76. agent_usage_tracker/manual_time.py +69 -0
  77. agent_usage_tracker/net.py +23 -0
  78. agent_usage_tracker/observation_cli.py +65 -0
  79. agent_usage_tracker/observation_report.py +116 -0
  80. agent_usage_tracker/observation_server.py +110 -0
  81. agent_usage_tracker/observations.py +365 -0
  82. agent_usage_tracker/outcome_git.py +383 -0
  83. agent_usage_tracker/outcomes.py +464 -0
  84. agent_usage_tracker/pricing.py +248 -0
  85. agent_usage_tracker/push.py +530 -0
  86. agent_usage_tracker/redact.py +45 -0
  87. agent_usage_tracker/repo_keys.py +165 -0
  88. agent_usage_tracker/review.py +252 -0
  89. agent_usage_tracker/scan.py +472 -0
  90. agent_usage_tracker/scheduled.py +164 -0
  91. agent_usage_tracker/sdk.py +79 -0
  92. agent_usage_tracker/sink.py +82 -0
  93. agent_usage_tracker/sink_stack/README.md +71 -0
  94. agent_usage_tracker/sink_stack/apply.sh +63 -0
  95. agent_usage_tracker/sink_stack/compose.yaml +94 -0
  96. agent_usage_tracker/sink_stack/env.example +20 -0
  97. agent_usage_tracker/sink_stack/grafana/dashboards/agents-effort.json +634 -0
  98. agent_usage_tracker/sink_stack/grafana/dashboards/agents-usage.json +1279 -0
  99. agent_usage_tracker/sink_stack/grafana/provisioning/dashboards/aut.yaml +9 -0
  100. agent_usage_tracker/sink_stack/grafana/provisioning/datasources/agents.yaml +16 -0
  101. agent_usage_tracker/sink_stack/sql/01_schema.sql +1186 -0
  102. agent_usage_tracker/sink_stack/sql/02_price.sql +60 -0
  103. agent_usage_tracker/sink_stack/sql/03_effort.sql +1180 -0
  104. agent_usage_tracker/sink_stack/sql/04_grants.sql +149 -0
  105. agent_usage_tracker/sink_stack/sql/refresh.sql +66 -0
  106. agent_usage_tracker/sources.py +180 -0
  107. agent_usage_tracker/state.py +191 -0
  108. agent_usage_tracker/telemetry.py +165 -0
  109. agent_usage_tracker-0.1.0.dist-info/METADATA +366 -0
  110. agent_usage_tracker-0.1.0.dist-info/RECORD +113 -0
  111. agent_usage_tracker-0.1.0.dist-info/WHEEL +4 -0
  112. agent_usage_tracker-0.1.0.dist-info/entry_points.txt +3 -0
  113. agent_usage_tracker-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,383 @@
1
+ """What a local clone says about a commit, read with git (and, for squash merges, gh).
2
+
3
+ The read side of `aut outcomes`: finding the clones under outcome_repo_roots, fetching them, and
4
+ reading landings, patch-id twins, reverts and surviving lines. Every read says when git failed, so
5
+ a caller never mistakes a failed read for an empty answer. Clone paths stay in memory for the run.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ import re
13
+ import shutil
14
+ import signal
15
+ import subprocess # nosec B404 # argv lists only, no shell in this module
16
+ import tempfile
17
+ import time
18
+ from dataclasses import dataclass, field
19
+ from glob import escape as glob_escape
20
+ from glob import glob
21
+ from typing import Any, NamedTuple
22
+
23
+ from .git_safe import git_argv, git_env
24
+ from .repo_keys import RepoKeys, repo_identity
25
+
26
+ DAY = 86400
27
+ DECIDE_S = 14 * DAY # undecided until day 14; also the revert and survival window
28
+ FETCH_TIMEOUT_S = 30
29
+ PATCH_ID_TIMEOUT_S = 600 # one clone's `git log -p | git patch-id`; a giant repo takes minutes
30
+ BLAME_FILES = 30 # survival blames at most this many files per commit
31
+ _REVERTS = re.compile(r"This reverts commit ([0-9a-f]{7,40})")
32
+ _SKIP_DIRS = {"node_modules", ".venv", "venv", "__pycache__", ".git", ".tox"}
33
+ _MAX_DEPTH = 6
34
+
35
+
36
+ def run_git(path: str, *args: str, inp: str | None = None, timeout: float = 300) -> str | None:
37
+ """stdout of a git command, or None when it fails or times out."""
38
+ try:
39
+ r = subprocess.run( # nosec B603 # no shell; refs and paths are vetted by the callers
40
+ git_argv(path, *args), input=inp, capture_output=True, text=True,
41
+ errors="replace", timeout=timeout, env=git_env())
42
+ except (OSError, subprocess.SubprocessError):
43
+ return None
44
+ return r.stdout if r.returncode == 0 else None
45
+
46
+
47
+ @dataclass(slots=True)
48
+ class Clone:
49
+ path: str # a worktree of the repo (held in memory only)
50
+ key: str | None # repo_key of its origin
51
+ github_slug: str | None # owner/repo when origin is on github.com (for gh)
52
+ default: str | None # origin/HEAD's target ref, or an origin main/master/develop
53
+ fetched_at: float = 0.0 # when origin was last fetched (FETCH_HEAD, or this run's fetch)
54
+ readable: bool = True # False: git failed reading its refs, so `default` says nothing
55
+
56
+
57
+ def _default_ref(path: str) -> tuple[str | None, bool]:
58
+ """(origin's default branch, whether git answered); never a local branch, so unpushed work is
59
+ never merged. for-each-ref exits 0 for an absent ref, so a None from it is a real failure.
60
+ A clone's origin/HEAD can name anything: `ref: --ext-diff` would reach git log as a flag and run
61
+ a repo-defined diff driver, so a target outside origin's refs is ignored."""
62
+ head, *fallbacks = ("refs/remotes/origin/HEAD", "refs/remotes/origin/main",
63
+ "refs/remotes/origin/master", "refs/remotes/origin/develop")
64
+ out = run_git(path, "for-each-ref", "--format=%(refname) %(symref)", head, *fallbacks)
65
+ if out is None:
66
+ return None, False
67
+ refs = dict(line.partition(" ")[::2] for line in out.splitlines())
68
+ target = refs.get(head, "")
69
+ if target.startswith("refs/remotes/origin/"):
70
+ return target, True
71
+ return next((r for r in fallbacks if r in refs), None), True
72
+
73
+
74
+ def _common_dir(path: str) -> str | None:
75
+ """The git common dir of the .git at path; "" when git says no repository is there (a stale
76
+ .git holds nothing), None when git failed, so a repository there may hold commits."""
77
+ try:
78
+ r = subprocess.run( # nosec B603 # constant args after -C path, no shell
79
+ git_argv(path, "rev-parse", "--path-format=absolute", "--git-common-dir"),
80
+ capture_output=True, text=True, errors="replace", timeout=10,
81
+ env={**git_env(), "LC_ALL": "C"}) # the message below is translated
82
+ except (OSError, subprocess.SubprocessError):
83
+ return None
84
+ if r.returncode == 0:
85
+ return r.stdout.strip()
86
+ # an invalid .git file marks a folder and holds no repository (uv's cache keeps one in each sdist)
87
+ return "" if ("not a git repository" in r.stderr
88
+ or "invalid gitfile format" in r.stderr) else None
89
+
90
+
91
+ def _origin_ids(url: str, keys: RepoKeys) -> tuple[str | None, str | None]:
92
+ """(repo_key, owner/repo when on github.com) of origin's URL; neither without a usable one."""
93
+ if not url:
94
+ return None, None
95
+ try:
96
+ ident = repo_identity(url)
97
+ except ValueError:
98
+ return None, None
99
+ return keys.of(ident), (ident.removeprefix("github.com/")
100
+ if ident.startswith("github.com/") else None)
101
+
102
+
103
+ def _origin_url(path: str) -> str | None:
104
+ """origin's URL, "" for a clone without one, None when git failed to say."""
105
+ remotes = run_git(path, "remote", timeout=10)
106
+ if remotes is None or "origin" not in remotes.split():
107
+ return None if remotes is None else ""
108
+ return run_git(path, "remote", "get-url", "origin", timeout=10)
109
+
110
+
111
+ def _clone_at(path: str, common: str, keys: RepoKeys) -> Clone:
112
+ url = _origin_url(path)
113
+ key, slug = _origin_ids((url or "").strip(), keys)
114
+ heads = glob(os.path.join(glob_escape(common), "FETCH_HEAD")) + glob(
115
+ os.path.join(glob_escape(common), "worktrees", "*", "FETCH_HEAD"))
116
+ default, readable = _default_ref(path)
117
+ return Clone(path, key, slug, default, max((os.path.getmtime(h) for h in heads), default=0.0),
118
+ readable and url is not None)
119
+
120
+
121
+ def find_clones(roots: list[str], keys: RepoKeys) -> list[Clone]:
122
+ """One Clone per repository (worktrees of one repo share a git common dir). A repository git
123
+ could not identify is an unreadable Clone: dropping it would make its commits look gone."""
124
+ seen: set[str] = set()
125
+ out: list[Clone] = []
126
+ for root in roots:
127
+ base = os.path.expanduser(root)
128
+ for dirpath, dirnames, filenames in os.walk(base):
129
+ depth = dirpath[len(base):].count(os.sep)
130
+ if ".git" in dirnames or ".git" in filenames:
131
+ common = _common_dir(dirpath)
132
+ if common is None:
133
+ out.append(Clone(dirpath, None, None, None, readable=False))
134
+ elif common and os.path.realpath(common) not in seen:
135
+ seen.add(os.path.realpath(common))
136
+ out.append(_clone_at(dirpath, common, keys))
137
+ dirnames[:] = [] if depth >= _MAX_DEPTH else \
138
+ [n for n in dirnames if n not in _SKIP_DIRS]
139
+ return out
140
+
141
+
142
+ def _fetch(path: str) -> bool:
143
+ """Fetch origin's refs and nothing else: no gc or maintenance (it could prune objects later
144
+ runs need), no FETCH_HEAD, no submodules, no prompt. On timeout SIGTERM the process group so
145
+ git removes its lock files, then SIGKILL."""
146
+ cmd = git_argv(path, "fetch", "--quiet", "--no-tags", "--no-auto-maintenance",
147
+ "--no-write-fetch-head", "--no-recurse-submodules", "origin")
148
+ try:
149
+ p = subprocess.Popen( # nosec B603 # constant args after -C path, no shell
150
+ cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
151
+ stderr=subprocess.DEVNULL, start_new_session=True, env=git_env())
152
+ except OSError:
153
+ return False
154
+ try:
155
+ return p.wait(timeout=FETCH_TIMEOUT_S) == 0
156
+ except subprocess.TimeoutExpired:
157
+ # Windows has no process groups or SIGKILL: there kill() ends git itself
158
+ kill = (lambda s: os.killpg(p.pid, s)) if hasattr(os, "killpg") else (lambda s: p.kill())
159
+ for sig, wait in ((signal.SIGTERM, 5), (getattr(signal, "SIGKILL", signal.SIGTERM), None)):
160
+ try:
161
+ kill(sig)
162
+ p.wait(timeout=wait)
163
+ break
164
+ except (OSError, subprocess.TimeoutExpired):
165
+ pass # already gone, or it ignored SIGTERM: on to SIGKILL
166
+ return False
167
+
168
+
169
+ def fetch_clones(clones: list[Clone]) -> tuple[int, int]:
170
+ """`git fetch` each clone, FETCH_TIMEOUT_S each; (ok, failed). A failure is skipped."""
171
+ ok = failed = 0
172
+ for c in clones:
173
+ if _fetch(c.path):
174
+ c.fetched_at = time.time()
175
+ ok += 1
176
+ else:
177
+ failed += 1
178
+ return ok, failed
179
+
180
+
181
+ def resolve_shas(clone: Clone, shas: list[str]) -> dict[str, ResolvedCommit] | None:
182
+ """sha (as stored, possibly abbreviated) -> its full sha and committer time, for those it
183
+ holds; None when git failed."""
184
+ out = run_git(clone.path, "cat-file", "--batch-check",
185
+ inp="".join(f"{s}^{{commit}}\n" for s in shas))
186
+ if out is None:
187
+ return None
188
+ full = {}
189
+ for s, line in zip(shas, out.splitlines()):
190
+ p = line.split()
191
+ if len(p) == 3 and p[1] == "commit":
192
+ full[s] = p[0]
193
+ if not full:
194
+ return {}
195
+ log = run_git(clone.path, "log", "--no-walk=unsorted", "--stdin", "--format=%H %ct",
196
+ inp="".join(f"{f}\n" for f in set(full.values())))
197
+ if log is None:
198
+ return None
199
+ ct = dict(line.split() for line in log.splitlines() if line.strip())
200
+ return {s: ResolvedCommit(f, int(ct[f])) for s, f in full.items() if f in ct}
201
+
202
+
203
+ def _patch_ids(path: str, args: list[str], inp: str | None = None) -> dict[str, str] | None:
204
+ """full sha -> stable patch id for the commits `git log -p <args>` prints; None when git failed
205
+ or took over PATCH_ID_TIMEOUT_S, and then neither process stays behind."""
206
+ procs: list[subprocess.Popen[Any]] = []
207
+ try:
208
+ with tempfile.TemporaryFile() as revs: # a file, so writing the revs can never block on git
209
+ revs.write((inp or "").encode())
210
+ revs.seek(0)
211
+ log = subprocess.Popen( # nosec B603 # no shell; args are flags and a vetted default ref
212
+ git_argv(path, "log", "-p", "--format=commit %H", *args),
213
+ stdin=revs, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=git_env())
214
+ procs.append(log)
215
+ pid = subprocess.Popen( # nosec B603 # constant args after -C path, no shell
216
+ git_argv(path, "patch-id", "--stable"), stdin=log.stdout,
217
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, env=git_env())
218
+ procs.append(pid)
219
+ if log.stdout: # always set with stdout=PIPE; the check narrows its type
220
+ log.stdout.close()
221
+ out, _ = pid.communicate(timeout=PATCH_ID_TIMEOUT_S)
222
+ log.wait(timeout=60)
223
+ except (OSError, subprocess.SubprocessError):
224
+ return None
225
+ finally:
226
+ for p in procs:
227
+ if p.poll() is None:
228
+ p.kill()
229
+ p.wait()
230
+ if log.returncode or pid.returncode:
231
+ return None
232
+ return {c: p for p, c in (line.split() for line in out.splitlines() if line.strip())}
233
+
234
+
235
+ @dataclass(slots=True)
236
+ class CloneRead:
237
+ """What one clone says about its anchors, read once per run. Not ok: a git read failed, so
238
+ nothing here may decide a commit."""
239
+ ok: bool = False
240
+ tip_t: int = 0
241
+ on_default: dict[str, int] = field(default_factory=dict) # commit -> when it landed
242
+ on_branch: set[str] = field(default_factory=set)
243
+ twin: dict[str, str] = field(default_factory=dict) # off-default full sha -> twin on default
244
+ reverted: dict[str, list[int]] = field(default_factory=dict) # reverted sha -> revert times
245
+
246
+ def reverted_between(self, full: str, start: float, end: float) -> bool:
247
+ return any(start <= t <= end for x, times in self.reverted.items()
248
+ if full.startswith(x) for t in times)
249
+
250
+
251
+ def _landings(log: str) -> tuple[int, dict[str, int]]:
252
+ """From `git log --format='%H %ct %P' <default>`: (tip time, commit -> when it landed = committer
253
+ time of the first-parent commit that brought it in; oldest first, so each commit gets its first
254
+ landing)."""
255
+ par, ct = {}, {}
256
+ for line in log.splitlines():
257
+ h, t, *ps = line.split()
258
+ par[h], ct[h] = ps, int(t)
259
+ tip = log.split(None, 1)[0] if log else None
260
+ fp: list[str] = []
261
+ at = tip
262
+ while at in par:
263
+ fp.append(at)
264
+ at = par[at][0] if par[at] else None
265
+ on_default: dict[str, int] = {}
266
+ for f in reversed(fp):
267
+ stack = [f]
268
+ while stack:
269
+ x = stack.pop()
270
+ if x not in on_default:
271
+ on_default[x] = ct[f]
272
+ stack.extend(q for q in par.get(x, ()) if q in par)
273
+ return (ct[tip] if tip else 0), on_default
274
+
275
+
276
+ def _twins(path: str, default: str, since: int, off: list[str]) -> dict[str, str] | None:
277
+ """off-default full sha -> the commit on default with its patch id; None when git failed."""
278
+ on_default = _patch_ids(path, ["--no-merges", f"--since=@{since}", default])
279
+ theirs = _patch_ids(path, ["--no-walk=unsorted", "--stdin"], "".join(f"{f}\n" for f in off))
280
+ if on_default is None or theirs is None:
281
+ return None
282
+ by_pid = {p: c for c, p in on_default.items()}
283
+ return {c: by_pid[p] for c, p in theirs.items() if p in by_pid and by_pid[p] != c}
284
+
285
+
286
+ def _reverted(log: str) -> dict[str, list[int]]:
287
+ """From `git log --format=%ct%n%B%x00`: reverted sha -> every time it was reverted."""
288
+ reverted: dict[str, list[int]] = {}
289
+ for chunk in log.split("\0"):
290
+ head, _, body = chunk.strip().partition("\n")
291
+ for x in _REVERTS.findall(body) if head.isdigit() else ():
292
+ reverted.setdefault(x, []).append(int(head))
293
+ return reverted
294
+
295
+
296
+ def read_clone(clone: Clone, since: int, off_default: list[str]) -> CloneRead:
297
+ d, path = clone.default, clone.path
298
+ log = run_git(path, "log", "--format=%H %ct %P", d) if d else ""
299
+ branches = run_git(path, "rev-list", "--branches", "--remotes", *([f"^{d}"] if d else []))
300
+ rv = run_git(path, "log", "--branches", "--remotes", f"--since=@{since}",
301
+ "--grep=This reverts commit", "--format=%ct%n%B%x00")
302
+ if not clone.readable or log is None or branches is None or rv is None:
303
+ return CloneRead()
304
+ tip_t, on_default = _landings(log)
305
+ off = [f for f in off_default if f not in on_default]
306
+ twin = _twins(path, d, since, off) if d and off else {}
307
+ if twin is None:
308
+ return CloneRead()
309
+ return CloneRead(True, tip_t, on_default, set(branches.split()), twin, _reverted(rv))
310
+
311
+
312
+ class GitFailed(Exception):
313
+ """A git read failed, so survival was not measured (which is not the same as 0 lines alive)."""
314
+
315
+
316
+ def _added_files(numstat: str) -> list[tuple[int, str]]:
317
+ """(lines added, path) from `git show --numstat -z`, the first BLAME_FILES that added any."""
318
+ files = []
319
+ for item in numstat.split("\0"):
320
+ a, _, rest = item.strip("\n").partition("\t")
321
+ f = rest.partition("\t")[2]
322
+ if a.isdigit() and int(a) > 0 and f:
323
+ files.append((int(a), f))
324
+ return files[:BLAME_FILES]
325
+
326
+
327
+ def _lines_alive(path: str, base: str, target: str, file: str) -> int:
328
+ """How many lines of file at base git blame attributes to target."""
329
+ bl = run_git(path, "blame", "--porcelain", base, "--", file, timeout=120)
330
+ if bl is not None:
331
+ return sum(1 for line in bl.splitlines() if line.startswith(target + " "))
332
+ # blame also fails on a path that is no file at base: that one really has 0 lines alive
333
+ tree = run_git(path, "ls-tree", base, "--", file)
334
+ if tree is None or " blob " in tree.partition("\t")[0]:
335
+ raise GitFailed
336
+ return 0
337
+
338
+
339
+ def survival(clone: Clone, target: str, merge_t: float) -> float | None:
340
+ """Share of target's added lines still attributed to it on the default branch 14 days after
341
+ the merge (first BLAME_FILES files). None for a merge or root commit or a commit adding no
342
+ lines. Raises GitFailed when git could not answer."""
343
+ path, default = clone.path, clone.default
344
+ if not default:
345
+ raise GitFailed
346
+ parents = run_git(path, "rev-list", "--parents", "-n", "1", target)
347
+ if parents is None:
348
+ raise GitFailed
349
+ if len(parents.split()) != 2:
350
+ return None
351
+ base = run_git(path, "rev-list", "-1", "--first-parent", f"--before=@{int(merge_t + DECIDE_S)}",
352
+ default)
353
+ raw = run_git(path, "show", "--numstat", "-z", "--no-renames", "--format=", target)
354
+ if base is None or raw is None:
355
+ raise GitFailed
356
+ base = base.strip()
357
+ if not base:
358
+ return None
359
+ files = _added_files(raw)
360
+ added = sum(a for a, _ in files)
361
+ if not added:
362
+ return None
363
+ alive = sum(_lines_alive(path, base, target, f) for _, f in files)
364
+ return round(min(1.0, alive / added), 4)
365
+
366
+
367
+ def gh_merged(github_slug: str, sha: str) -> bool | None:
368
+ """True when a merged PR of github_slug contains sha; None when gh is missing or fails."""
369
+ if not shutil.which("gh"):
370
+ return None
371
+ try:
372
+ r = subprocess.run( # nosec B603, B607 # no shell; slug and sha are values of their flags
373
+ ["gh", "pr", "list", "--repo", github_slug, "--state", "merged", "--search",
374
+ sha, "--json", "number", "--limit", "1"], capture_output=True,
375
+ text=True, timeout=FETCH_TIMEOUT_S)
376
+ return bool(json.loads(r.stdout)) if r.returncode == 0 else None
377
+ except (OSError, subprocess.SubprocessError, ValueError):
378
+ return None
379
+
380
+
381
+ class ResolvedCommit(NamedTuple):
382
+ full: str
383
+ committed: int