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,350 @@
1
+ """`aut inspect --episode ID | --day YYYY-MM-DD | --label UNIT_ID`: the evidence inspector.
2
+
3
+ Shows why an episode (or each episode of a local day, or the episode a label belongs to) exists:
4
+ its member sessions, the anchors by type that the linker joins on, the outcome rule that fired,
5
+ labels with probability and model, and hand corrections. Everything is read from the sink, ids
6
+ and keys only; titles and branch names come from THIS machine's logs (session_digest) and
7
+ exist only in the page served from 127.0.0.1 under a random token. The page itself only reads;
8
+ its one write is the correction form of `aut episodes review` it links to, for the episodes it shows.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import html
14
+ import re
15
+ from collections import Counter
16
+ from datetime import datetime, timedelta, timezone
17
+ from typing import Any, Callable, Literal
18
+
19
+ from . import episode_store, local_page, review
20
+ from .episode_store import Episode
21
+ from .collectors.session_digest import SessionView, local_info
22
+ from .config import AutError, Config
23
+ from .sink import connect
24
+
25
+ # (shown name, link row attrs): a commit key is a printed commit (commit_keys) or a quoted SHA
26
+ # (sha_refs), both HMACs of the first 7 hex, so they meet in one type
27
+ ANCHORS = (("commit key", ("commit_keys", "sha_refs")), ("pr key", ("pr_keys",)),
28
+ ("branch key", ("branch_key",)), ("ticket key (a hint, never an edge)", ("ticket_keys",)))
29
+ RULES = { # keep in step with "Episodes (sink-built)" in CONTRACT.md, which states episode_link()
30
+ "manual": "manual outcome: the latest `aut outcome` on a seed session wins over git",
31
+ "reworked": "a commit was reverted or reworked",
32
+ "shipped": "a commit merged (survival at least 0.5 where known), none reverted or reworked",
33
+ "open": "no commit merged or reworked, and a commit is still open",
34
+ "ruled_out": "every commit was abandoned",
35
+ "undecided": "a commit still waits for an outcome (`aut outcomes`)",
36
+ "no_code": "no commits: research or review work",
37
+ }
38
+ Selector = Literal["episode", "day", "label"]
39
+ _SHA = re.compile(r"[0-9a-f]{7,40}")
40
+ _EP_PREFIX = re.compile(r"[0-9a-f-]{4,36}")
41
+ _EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
42
+ _SHOW = 25 # rows per list on the page; a giant episode would otherwise render megabytes
43
+ _CSS = """section { border: 1px solid var(--line); border-radius: 6px; margin: 0 0 1rem;
44
+ padding: 0 1rem; overflow-x: auto; }
45
+ table { border-collapse: collapse; font-size: 14px; }
46
+ td, th { border-bottom: 1px solid var(--line); padding: .2rem .5rem; text-align: left;
47
+ vertical-align: top; }
48
+ .meta { color: var(--mut); font-size: 13px; } code { font-size: 13px; }
49
+ """
50
+
51
+
52
+ def _day_bounds(day: str) -> tuple[datetime, datetime]:
53
+ try:
54
+ d = datetime.strptime(day, "%Y-%m-%d")
55
+ except ValueError:
56
+ raise AutError("inspect: --day is YYYY-MM-DD") from None
57
+ return d.astimezone(), (d + timedelta(days=1)).astimezone() # local midnights, DST-safe
58
+
59
+
60
+ def _episode_prefix(episode: str) -> str:
61
+ """An episode id or prefix: uuid characters only, so no LIKE wildcard gets through."""
62
+ ep = episode.strip().lower()
63
+ if not _EP_PREFIX.fullmatch(ep):
64
+ raise AutError("inspect: --episode is an episode id or prefix (4 or more of 0-9a-f-)")
65
+ return ep
66
+
67
+
68
+ def fetch(dsn: str, kind: Selector, value: str) -> dict[str, Any]:
69
+ """Raw rows for one view. Read-only; every SELECT is ids, keys, enums and numbers."""
70
+ start = end = _EPOCH
71
+ label = None
72
+ params: tuple[Any, ...]
73
+ if kind == "episode":
74
+ where, params = "e.episode_id::text LIKE %s", (_episode_prefix(value) + "%",)
75
+ elif kind == "day":
76
+ start, end = _day_bounds(value)
77
+ # a member row that day, as agents.episode_activity_day counts it (not span overlap)
78
+ where = ("EXISTS (SELECT 1 FROM agents.episode_member em JOIN agents.activity_event a "
79
+ "ON a.session_id = em.session_id WHERE em.episode_id = e.episode_id AND a.kind "
80
+ "IN ('steer', 'link', 'commit', 'pr') AND a.ts >= %s AND a.ts < %s "
81
+ "AND a.ts BETWEEN e.first_ts AND e.last_ts)")
82
+ params = (start, end)
83
+ else: # the label's own episode (episode, episode_day) or its steer's session's episodes
84
+ label = value
85
+ where = ("e.episode_id::text = %s OR e.episode_id IN (SELECT m.episode_id FROM "
86
+ "agents.episode_member m JOIN agents.activity_event a ON a.session_id = "
87
+ "m.session_id WHERE a.kind = 'steer' AND a.event_id = %s)")
88
+ params = (label.split(":")[0], label)
89
+ with connect(dsn) as conn:
90
+ def q(sql: str, p: tuple[Any, ...]) -> list[tuple[Any, ...]]:
91
+ return conn.execute(sql, p).fetchall()
92
+ eps = episode_store.load_episodes(conn, where, params)
93
+ sids = sorted({m["session_id"] for ep in eps for m in ep["members"]})
94
+ acts = q("SELECT session_id, kind, attrs FROM agents.activity_event "
95
+ "WHERE session_id = ANY(%s) AND kind IN ('link', 'commit')", (sids,))
96
+ sess = q("SELECT session_id, min(agent), count(*) FILTER (WHERE kind = 'steer'), "
97
+ "min(ts), max(ts) FROM agents.activity_event WHERE session_id = ANY(%s) "
98
+ "GROUP BY 1", (sids,))
99
+ steers = q("SELECT event_id, session_id FROM agents.activity_event "
100
+ "WHERE kind = 'steer' AND session_id = ANY(%s)", (sids,))
101
+ shas = sorted({str((a or {}).get("sha") or "").lower()[:7] for _s, k, a in acts
102
+ if k == "commit"})
103
+ outcomes = q("SELECT DISTINCT ON (left(lower(sha), 7)) left(lower(sha), 7), state, "
104
+ "survival FROM agents.commit_outcome WHERE left(lower(sha), 7) = ANY(%s) "
105
+ "ORDER BY left(lower(sha), 7), decided_at DESC", (shas,))
106
+ manual = q("SELECT session_id, outcome, ts FROM agents.manual_outcome "
107
+ "WHERE session_id = ANY(%s) ORDER BY ts DESC", (sids,))
108
+ corr = q("SELECT ts, action, a_session, b_session FROM agents.episode_correction "
109
+ "WHERE a_session = ANY(%s) OR b_session = ANY(%s) ORDER BY ts", (sids, sids))
110
+ eids = [ep["id"] for ep in eps]
111
+ labels = q("SELECT event_id, ts, attrs FROM agents.activity_event WHERE kind = 'label' "
112
+ "AND (attrs->>'unit_id' = ANY(%s) OR split_part(attrs->>'unit_id', ':', 1) "
113
+ "= ANY(%s) OR attrs->>'unit_id' = %s OR (ts >= %s AND ts < %s AND "
114
+ "attrs->>'unit' IN ('aw_block', 'aw_title')))",
115
+ ([s for s, _ in steers] + eids, eids, label, start, end))
116
+ return {"episodes": eps, "acts": acts, "sessions": sess, "steers": steers,
117
+ "outcomes": outcomes, "manual": manual, "corrections": corr, "labels": labels}
118
+
119
+
120
+ def latest_labels(rows: list[Any]) -> list[dict[str, Any]]:
121
+ """The current labels (core.current_labels), oldest first."""
122
+ from .label.core import current_labels # imported where used, as in cli.py
123
+
124
+ return sorted(current_labels(rows), key=lambda d: (d["ts"], d["task"] or ""))
125
+
126
+
127
+ def _add_anchors(anchors: dict[str, dict[str, set[str]]], sid: str, a: dict[str, Any]) -> None:
128
+ """Add one link row's keys, by anchor type."""
129
+ for name, fields in ANCHORS:
130
+ for f in fields:
131
+ v = a.get(f)
132
+ for x in v if isinstance(v, list) else [v] if v else []:
133
+ anchors[name].setdefault(str(x), set()).add(sid)
134
+
135
+
136
+ def _link_keys(ep_members: dict[str, dict[str, Any]], seeds: set[str],
137
+ acts: list[Any]) -> tuple[dict[str, dict[str, set[str]]], dict[str, set[str]]]:
138
+ """(anchor type -> key -> seed sessions holding it, 7-hex commit -> sessions) of one episode."""
139
+ anchors: dict[str, dict[str, set[str]]] = {name: {} for name, _f in ANCHORS}
140
+ commits: dict[str, set[str]] = {}
141
+ for sid, kind, a in acts:
142
+ a = a or {}
143
+ keys = {k for k in ep_members.get(sid, {}).get("repo_keys", []) if k}
144
+ if sid not in ep_members or (keys and a.get("repo_key") and a["repo_key"] not in keys):
145
+ continue # a seed session's unit in another repo belongs to another episode
146
+ if kind == "commit":
147
+ sha = str(a.get("sha") or "").lower()
148
+ if _SHA.fullmatch(sha):
149
+ commits.setdefault(sha[:7], set()).add(sid)
150
+ elif sid in seeds: # children attach to their parent and never join
151
+ _add_anchors(anchors, sid, a)
152
+ return anchors, commits
153
+
154
+
155
+ def _rule_of(ep: Episode, manual: list[Any], seeds: set[str]) -> str:
156
+ """Which rule of agents.episode_link() fired."""
157
+ if ep["outcome_source"] != "manual":
158
+ return RULES.get(ep["outcome"], ep["outcome"])
159
+ man = [(s, o, ts) for s, o, ts in manual if s in seeds]
160
+ return RULES["manual"] + (f" ({man[0][1]}, {man[0][2].astimezone():%Y-%m-%d %H:%M})"
161
+ if man else "")
162
+
163
+
164
+ def _episode_labels(ep: Episode, members: dict[str, dict[str, Any]], labels: list[dict[str, Any]], steer_sid: dict[str, Any],
165
+ used: set[int]) -> tuple[list[dict[str, Any]], Counter[tuple[str, str]]]:
166
+ """(the episode's own labels, its steers' labels per (task, label)); marks the labels it took."""
167
+ own: list[dict[str, Any]] = []
168
+ steer_mix: Counter[tuple[str, str]] = Counter()
169
+ for i, lab in enumerate(labels):
170
+ uid, unit = lab["unit_id"] or "", lab["unit"]
171
+ if (unit == "episode" and uid == ep["id"]) or \
172
+ (unit == "episode_day" and uid.split(":")[0] == ep["id"]):
173
+ own.append(lab)
174
+ elif unit == "steer" and steer_sid.get(uid) in members:
175
+ steer_mix[(lab["task"], "unknown" if lab["below_tau"] else lab["label"])] += 1
176
+ else:
177
+ continue
178
+ used.add(i)
179
+ return own, steer_mix
180
+
181
+
182
+ def build(raw: dict[str, Any]) -> dict[str, Any]:
183
+ """Assemble each episode's evidence from fetch()'s rows. Pure: tests feed it rows."""
184
+ sess = {s: {"agent": ag, "steers": n, "first": f, "last": la}
185
+ for s, ag, n, f, la in raw["sessions"]}
186
+ steer_sid = dict(raw["steers"])
187
+ states = {k: (st, sv) for k, st, sv in raw["outcomes"]}
188
+ labels = latest_labels(raw["labels"])
189
+ used: set[int] = set()
190
+ out = []
191
+ for ep in raw["episodes"]:
192
+ mem = {m["session_id"]: m for m in ep["members"]}
193
+ seeds = {s for s, m in mem.items() if m["role"] == "seed"}
194
+ anchors, commits = _link_keys(mem, seeds, raw["acts"])
195
+ own, steer_mix = _episode_labels(ep, mem, labels, steer_sid, used)
196
+ out.append({**ep, "rule": _rule_of(ep, raw["manual"], seeds), "anchors": anchors,
197
+ "labels": own, "steer_labels": sorted(steer_mix.items()),
198
+ "commits": [(k, *states.get(k, ("no outcome yet", None)), sorted(v))
199
+ for k, v in sorted(commits.items())],
200
+ "member_activity": [{**m, **sess.get(m["session_id"], {})} for m in ep["members"]],
201
+ "corrections": [c for c in raw["corrections"]
202
+ if c[2] in mem or (c[3] and c[3] in mem)]})
203
+ rest = [lab for i, lab in enumerate(labels) if i not in used]
204
+ return {"episodes": out, "labels": rest, "history": raw["labels"]}
205
+
206
+
207
+ def _short(x: object) -> str:
208
+ return str(x)[:12]
209
+
210
+
211
+ def _table(head: list[str], rows: list[list[Any]]) -> str:
212
+ e = html.escape
213
+ return ("<table><tr>" + "".join(f"<th>{e(h)}</th>" for h in head) + "</tr>" +
214
+ "".join("<tr>" + "".join(f"<td>{e(str(c))}</td>" for c in r) + "</tr>"
215
+ for r in rows) + "</table>")
216
+
217
+
218
+ def _labels_table(labs: list[dict[str, Any]]) -> str:
219
+ return _table(["task", "unit", "unit id", "label", "p", "model", "version"],
220
+ [[x["task"], x["unit"], x["unit_id"],
221
+ "unknown (below tau: " + str(x["label"]) + ")" if x["below_tau"] else x["label"],
222
+ "" if x["p"] is None else f"{float(x['p']):.2f}", x["model_id"], x["task_ver"]]
223
+ for x in labs])
224
+
225
+
226
+ def _member_table(ep: dict[str, Any], info: dict[str, SessionView]) -> str:
227
+ rows = []
228
+ for s in ep["member_activity"][:4 * _SHOW]: # seeds first (load_episodes' order)
229
+ d = info.get(s["session_id"], {})
230
+ span = f"{s['first']:%m-%d %H:%M} to {s['last']:%m-%d %H:%M}" if s.get("first") else ""
231
+ rows.append([s["role"], s.get("agent", ""), _short(s["session_id"]),
232
+ s.get("steers", 0), span,
233
+ ", ".join(_short(k) for k in s["repo_keys"] if k),
234
+ "; ".join(d.get("titles", [])[-3:]) or "(no local title)",
235
+ ", ".join(d.get("branches", [])[:5])])
236
+ table = _table(["role", "agent", "session", "steers", "active", "repo keys", "local titles",
237
+ "local branches"], rows)
238
+ if len(ep["member_activity"]) > 4 * _SHOW:
239
+ table += f"<p class=meta>First {4 * _SHOW} of {len(ep['member_activity'])}.</p>"
240
+ return table
241
+
242
+
243
+ def _anchor_items(anchors: dict[str, dict[str, set[str]]]) -> str:
244
+ e, parts = html.escape, []
245
+ for name, vals in anchors.items():
246
+ shared = {v: ss for v, ss in vals.items() if len(ss) > 1}
247
+ top = sorted(shared.items(), key=lambda kv: (-len(kv[1]), kv[0]))[:_SHOW]
248
+ parts.append(f"<li>{e(name)}: {len(vals)} keys, {len(shared)} shared" + "".join(
249
+ f"<br><code>{e(_short(v))}</code> in " + ", ".join(e(_short(s)) for s in sorted(ss))
250
+ for v, ss in top) + "</li>")
251
+ return "".join(parts)
252
+
253
+
254
+ def _labels_part(ep: dict[str, Any]) -> str:
255
+ parts = ["<h3>Labels</h3>"]
256
+ if ep["labels"]:
257
+ parts.append(_labels_table(ep["labels"]))
258
+ if ep["steer_labels"]:
259
+ parts.append("<p>Steer labels: " + html.escape(", ".join(
260
+ f"{t} {lab} {n}" for (t, lab), n in ep["steer_labels"])) + "</p>")
261
+ if not ep["labels"] and not ep["steer_labels"]:
262
+ parts.append("<p class=meta>No labels yet.</p>")
263
+ return "".join(parts)
264
+
265
+
266
+ def _episode_section(ep: dict[str, Any], info: dict[str, SessionView]) -> str:
267
+ e, parts = html.escape, []
268
+ parts.append(f"<section><h2>Episode <code>{e(ep['id'])}</code></h2><p class=meta>"
269
+ f"{ep['first_ts']:%Y-%m-%d %H:%M} to {ep['last_ts']:%Y-%m-%d %H:%M}; "
270
+ f"{ep['n_sessions']} sessions ({ep['n_seed_sessions']} seed), "
271
+ f"{ep['n_steers']} steers, {ep['n_commits']} commits, {ep['n_prs']} PRs; "
272
+ f"{'confirmed by hand' if ep['confirmed'] else 'not confirmed'}</p>"
273
+ f"<h3>Outcome: {e(ep['outcome'])}</h3><p>Rule that fired: "
274
+ f"{e(ep['rule'])}</p>")
275
+ if ep["commits"]:
276
+ parts.append(_table(["commit", "state", "survival", "sessions"],
277
+ [[k, st, "" if sv is None else f"{float(sv):.2f}",
278
+ ", ".join(_short(s) for s in ss)]
279
+ for k, st, sv, ss in ep["commits"][:_SHOW]]))
280
+ if len(ep["commits"]) > _SHOW:
281
+ parts.append(f"<p class=meta>First {_SHOW} of {len(ep['commits'])}.</p>")
282
+ parts.append("<h3>Member sessions</h3>" + _member_table(ep, info))
283
+ parts.append("<h3>Anchors by type</h3><p class=meta>Seed sessions only; a key held by "
284
+ "two or more sessions is what joined them.</p><ul>"
285
+ + _anchor_items(ep["anchors"]) + "</ul>" + _labels_part(ep)
286
+ + "<h3>Corrections</h3>")
287
+ if ep["corrections"]:
288
+ parts.append(_table(["when", "action", "session", "other session"],
289
+ [[f"{ts.astimezone():%Y-%m-%d %H:%M}", act, _short(a), _short(b or "")]
290
+ for ts, act, a, b in ep["corrections"]]))
291
+ else:
292
+ parts.append("<p class=meta>None.</p>")
293
+ parts.append("</section>")
294
+ return "".join(parts)
295
+
296
+
297
+ def _label_history(view: dict[str, Any], label: str) -> str:
298
+ hist = sorted((r for r in view["history"] if (r[2] or {}).get("unit_id") == label),
299
+ key=lambda r: r[1])
300
+ return (f"<section><h2>Every label row of <code>{html.escape(label)}</code></h2>"
301
+ + _table(["when", "task", "version", "label", "p", "below tau", "model", "labelled at"],
302
+ [[f"{ts.astimezone():%Y-%m-%d %H:%M}", a.get("task"), a.get("task_ver"),
303
+ a.get("label"), a.get("p"), a.get("below_tau"), a.get("model_id"),
304
+ a.get("labelled_at") or ""] for _eid, ts, a in hist])
305
+ + "</section>")
306
+
307
+
308
+ def render(view: dict[str, Any], info: dict[str, SessionView], token: str, title: str, label: str | None = None) -> str:
309
+ e = html.escape
310
+ parts = [local_page.head("Evidence inspector", "64rem", _CSS) + f"""<h1>{e(title)}</h1>
311
+ <p>Private page on this machine only; its only write is the correction form it links to. Titles
312
+ and branch names come from this machine's logs and are never sent.</p>"""]
313
+ if view["episodes"]:
314
+ parts.append(f'<p><a href="/{e(token)}/correct">Correct these episodes</a> (the '
315
+ "<code>aut episodes review</code> form)</p>")
316
+ parts += [_episode_section(ep, info) for ep in view["episodes"]]
317
+ if view["labels"]:
318
+ parts.append("<section><h2>Labels without an episode here</h2>"
319
+ + _labels_table(view["labels"]) + "</section>")
320
+ if label:
321
+ parts.append(_label_history(view, label))
322
+ parts.append("</body></html>")
323
+ return "".join(parts)
324
+
325
+
326
+ def pages(view: dict[str, Any], info: dict[str, SessionView], title: str,
327
+ label: str | None = None) -> Callable[[str], dict[str, str]]:
328
+ """The evidence at /<token> and the correction form for its episodes at /<token>/correct."""
329
+ return lambda token: {"": render(view, info, token, title, label),
330
+ "/correct": review.render(view["episodes"], info, token, title.lower())}
331
+
332
+
333
+ def run_inspect(cfg: Config, kind: Selector, value: str) -> dict[str, int]:
334
+ dsn = cfg.require_pg("inspect")
335
+ if kind == "episode":
336
+ value = _episode_prefix(value)
337
+ title = f"{kind.capitalize()} {value}"
338
+ view = build(fetch(dsn, kind, value))
339
+ if not view["episodes"] and not view["labels"]:
340
+ raise AutError(f"inspect: nothing found for {title.lower()}")
341
+ info = local_info(cfg.home, {s["session_id"] for ep in view["episodes"]
342
+ for s in ep["member_activity"][:4 * _SHOW]
343
+ if s["role"] == "seed"}) # children have no title
344
+ return review.serve(view["episodes"], lambda rows: episode_store.insert_corrections(dsn, rows),
345
+ pages(view, info, title, value if kind == "label" else None))
346
+
347
+
348
+ def print_inspect(summary: dict[str, Any]) -> None:
349
+ if summary["saved"]:
350
+ print(f"inspect: {summary['saved']} corrections saved")
@@ -0,0 +1,22 @@
1
+ """`aut label`: a local model labels units of work on this machine (optional extra `aut[label]`).
2
+
3
+ One interface for every task (core.Request -> backend distribution -> core.Response), one stored
4
+ shape (an agents.activity_event row of kind `label`: an enum value, a probability, versions;
5
+ never text). The text a decision reads (steer text, window titles) comes from this machine's own
6
+ logs at run time, is redacted, and is never stored or logged.
7
+
8
+ core Request / Response, temperature and Platt scaling, per-label tau, the label row
9
+ backends the model table by short name and one backend per protocol: mlx_letter (an instruct
10
+ model read through answer-letter logits), mlx_elephant, mlx_card, mlx_pplx, mlx_clef,
11
+ llama_server (a decision GGUF behind llama-server), and the encoder slot
12
+ clef Clef's joint schema reader on MLX, imported only when clef-flash runs
13
+ merge `python -m agent_usage_tracker.label.merge NAME`: fold a LoRA into its base once
14
+ tasks the closed candidate lists, questions and unit builders per task
15
+ run `aut label run` / `aut label backfill`: watermark, time budget, delivery, heartbeat
16
+ evalset the silver, gold, eval and score files under core.data_dir() / "eval", the shared
17
+ unit order, the five folds and the final test days
18
+ teach `aut label teach`: silver labels (local model always, Sonnet for Claude Code units)
19
+ gold `aut label review`: the gold page on 127.0.0.1
20
+ bakeoff `aut label eval` / `choose` / `train-encoder`: saved scores, out-of-fold metrics,
21
+ the table per task, the owner's pick into label.json
22
+ """