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,382 @@
1
+ """Read local agent text against collector identities, without advancing ingestion state."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import sqlite3
6
+ from collections import Counter
7
+ from dataclasses import dataclass
8
+ from datetime import datetime
9
+ from itertools import islice
10
+ from pathlib import Path
11
+ from typing import Any, Iterable
12
+
13
+ from ..collectors import claude_code, codex, cursor_cli, pi, t3
14
+ from ..collectors.activity_common import first_record, text_of
15
+ from ..collectors.cursor import connect_ro
16
+ from ..cursor_run import automated_sessions
17
+ from ..event import CollectorUnavailable, make_event_id
18
+ from ..repo_keys import RepoKeys
19
+ from ..state import parse_record
20
+
21
+ AGENTS = frozenset({"claude_code", "codex", "cursor_cli", "pi", "t3"})
22
+
23
+
24
+ def session_paths(home: Path, agent: str) -> dict[str, list[tuple[int, str]]]:
25
+ """Canonical session identities and paths in discovery order, without retaining text."""
26
+ if agent == "t3":
27
+ try:
28
+ with t3.connection(home) as con:
29
+ return {"t3:" + tid: [(order, str(t3.database_path(home)))]
30
+ for order, (tid,) in enumerate(t3._read(con,
31
+ f"SELECT thread_id FROM {t3._PREFIX}threads"))}
32
+ except (OSError, sqlite3.Error, ValueError, TypeError) as exc:
33
+ raise CollectorUnavailable("t3: local context index unavailable") from exc
34
+ discover = {"claude_code": claude_code.main_session_files, "codex": codex.session_files,
35
+ "cursor_cli": cursor_cli.chat_stores, "pi": pi.session_files}[agent]
36
+ index: dict[str, list[tuple[int, str]]] = {}
37
+ for order, path in enumerate(discover(home)):
38
+ sid: Any = None
39
+ if agent == "claude_code":
40
+ sid = Path(path).stem
41
+ elif agent == "cursor_cli":
42
+ sid = Path(path).parent.name
43
+ else:
44
+ head = first_record(path)
45
+ if agent == "codex":
46
+ payload = head.get("payload") if head else None
47
+ if isinstance(payload, dict):
48
+ sid = payload.get("id") or payload.get("session_id")
49
+ elif head and head.get("type") == "session":
50
+ sid = pi.header(head, path)["id"]
51
+ if isinstance(sid, str):
52
+ index.setdefault(sid, []).append((order, path))
53
+ return index
54
+
55
+
56
+ @dataclass(frozen=True)
57
+ class Context:
58
+ text: str
59
+ before: str = ""
60
+ after: str = ""
61
+ prompt_ordinal: int | None = None
62
+
63
+
64
+ def _records(path: str) -> Iterable[dict[str, Any]]:
65
+ with open(path, encoding="utf-8", errors="replace") as stream:
66
+ for line in stream:
67
+ record = parse_record(line)
68
+ if record is not None:
69
+ yield record
70
+
71
+
72
+ def _in_time(record: dict[str, Any], until: datetime | None) -> bool:
73
+ if until is None:
74
+ return True
75
+ try:
76
+ return datetime.fromisoformat(str(record.get("timestamp")).replace("Z", "+00:00")) < until
77
+ except (ValueError, TypeError):
78
+ return False
79
+
80
+
81
+ def _join(events: Iterable[tuple[str | None, str, int | None]], want: set[str]) -> dict[str, Context]:
82
+ out: dict[str, Context] = {}
83
+ last, current = "", None
84
+ for eid, text, ordinal in events:
85
+ if eid is not None:
86
+ current = eid if eid in want else None
87
+ if current:
88
+ out[current] = Context(text, last, prompt_ordinal=ordinal)
89
+ elif text.strip():
90
+ last = text
91
+ if current:
92
+ previous = out[current]
93
+ out[current] = Context(previous.text, previous.before, text, previous.prompt_ordinal)
94
+ return out
95
+
96
+
97
+ def _codex_records(path: str, cur: codex.FileState) -> Iterable[codex.ParsedRecord]:
98
+ with open(path, "rb") as stream:
99
+ offset = 0
100
+ while lines := list(islice(stream, 1000)):
101
+ yield from codex.parsed_records(lines, offset, cur, Counter())
102
+ offset += len(lines)
103
+
104
+
105
+ def _codex_prefixes(path: str, keys: RepoKeys, want: set[str]) -> list[int]:
106
+ # An incrementally collected old file can acquire user_message records later. Resolve
107
+ # the retained legacy prefix against existing IDs, rather than renumber those inputs.
108
+ cur = codex._fresh_state(Path(path).stat())
109
+ legacy: list[str] = []
110
+ seen_modern = False
111
+ modern: list[tuple[str, str, int]] = []
112
+ for record, kind, payload, copied in _codex_records(path, cur):
113
+ codex._note_context(kind, payload, copied, cur, keys)
114
+ if copied:
115
+ continue
116
+ is_modern = kind == "event_msg" and payload.get("type") == "user_message"
117
+ seen_modern |= is_modern
118
+ prompt = codex.prompt_of(kind, payload, seen_modern)
119
+ if prompt is None or codex.is_scheduled_automation(prompt[0]):
120
+ continue
121
+ seed = cur["session_id"] or Path(path).name
122
+ stamp = record.get("timestamp") or ""
123
+ if is_modern:
124
+ modern.append((seed, stamp, len(modern) + 1))
125
+ else:
126
+ legacy.append(make_event_id("codex", "steer", seed, stamp, len(legacy) + 1))
127
+ if not modern:
128
+ return [len(legacy)] if set(legacy) & want else []
129
+ profiles: dict[tuple[tuple[str, str, int], ...], tuple[int, int]] = {}
130
+ for offset in range(len(legacy) + 1):
131
+ matched = [(eid, "legacy", i) for i, eid in enumerate(legacy[:offset]) if eid in want]
132
+ matched.extend((eid, "modern", ordinal) for seed, stamp, ordinal in modern
133
+ if (eid := make_event_id("codex", "steer", seed, stamp,
134
+ offset + ordinal)) in want)
135
+ # Equivalent interpretations need only one bounded replay of the log.
136
+ signature = tuple(sorted(matched))
137
+ profiles[signature] = len({eid for eid, kind, index in matched}), offset
138
+ best = max(count for count, offset in profiles.values())
139
+ return [offset for count, offset in profiles.values() if count == best] if best else []
140
+
141
+
142
+ def _codex_events(path: str, until: datetime | None, keys: RepoKeys, prefix: int,
143
+ imported_until: datetime | None = None,
144
+ ) -> Iterable[tuple[str | None, str, int | None]]:
145
+ cur = codex._fresh_state(Path(path).stat())
146
+ legacy, modern_n, seen_modern = 0, 0, False
147
+ for record, kind, payload, copied in _codex_records(path, cur):
148
+ codex._note_context(kind, payload, copied, cur, keys)
149
+ if copied:
150
+ continue
151
+ is_modern = kind == "event_msg" and payload.get("type") == "user_message"
152
+ seen_modern |= is_modern
153
+ prompt = codex.prompt_of(kind, payload, seen_modern)
154
+ if prompt is not None and codex.is_scheduled_automation(prompt[0]):
155
+ yield "", "", None # A software prompt ends the preceding human prompt's reply interval.
156
+ elif prompt is not None:
157
+ if is_modern:
158
+ modern_n += 1
159
+ ordinal = prefix + modern_n
160
+ else:
161
+ legacy += 1
162
+ if legacy > prefix:
163
+ yield "", "", None
164
+ continue
165
+ ordinal = legacy
166
+ if codex.imported_record(record, imported_until):
167
+ yield "", "", None
168
+ elif _in_time(record, until):
169
+ yield make_event_id("codex", "steer", cur["session_id"] or Path(path).name,
170
+ record.get("timestamp") or "", ordinal), prompt[0], ordinal
171
+ elif not codex.imported_record(record, imported_until) and _in_time(record, until):
172
+ if kind == "response_item" and payload.get("type") == "message" \
173
+ and payload.get("role") == "assistant":
174
+ content = payload.get("content")
175
+ blocks = content if isinstance(content, list) else []
176
+ yield None, "\n".join(b["text"] for b in blocks if isinstance(b, dict)
177
+ and b.get("type") in {"text", "output_text"}
178
+ and isinstance(b.get("text"), str)), None
179
+ elif kind == "event_msg" and payload.get("type") == "agent_message":
180
+ message = payload.get("message")
181
+ if isinstance(message, str):
182
+ yield None, message, None
183
+
184
+
185
+ def codex_context(home: Path, sessions: set[str], want: set[str],
186
+ until: datetime | None, keys: RepoKeys, *,
187
+ paths: Iterable[str] | None = None) -> dict[str, Context]:
188
+ out: dict[str, Context] = {}
189
+ imports = codex.imported_until(home)
190
+ for path in codex.session_files(home) if paths is None else paths:
191
+ head = first_record(path)
192
+ payload = head.get("payload") if head else None
193
+ if not isinstance(payload, dict):
194
+ continue
195
+ sid = payload.get("id") or payload.get("session_id")
196
+ if not isinstance(sid, str) or sid not in sessions:
197
+ continue
198
+ if codex.run_origin(payload) in {"subagent", "automated"}:
199
+ continue
200
+ imported_until = imports.get(sid)
201
+ profiles = [_join(_codex_events(path, until, keys, prefix, imported_until), want)
202
+ for prefix in _codex_prefixes(path, keys, want)]
203
+ if not profiles:
204
+ continue
205
+ for eid in set.intersection(*(set(p) for p in profiles)):
206
+ values = [p[eid] for p in profiles]
207
+ if len({v.text for v in values}) == 1:
208
+ out[eid] = Context(values[0].text,
209
+ values[0].before if len({v.before for v in values}) == 1 else "",
210
+ values[0].after if len({v.after for v in values}) == 1 else "",
211
+ values[0].prompt_ordinal if len({v.prompt_ordinal for v in values}) == 1 else None)
212
+ return out
213
+
214
+
215
+ def _pi_context(path: str, want: set[str], until: datetime | None) -> dict[str, Context]:
216
+ head = first_record(path)
217
+ if not head or head.get("type") != "session":
218
+ return {}
219
+ hdr = pi.header(head, path)
220
+ nodes: dict[str, dict[str, Any]] = {}
221
+ for record in _records(path):
222
+ if pi.is_fork_copy(record, hdr) or not _in_time(record, until):
223
+ continue
224
+ rid = record.get("id")
225
+ if isinstance(rid, str):
226
+ nodes[rid] = record
227
+
228
+ def ancestors(record: dict[str, Any]) -> Iterable[str]:
229
+ parent = record.get("parentId")
230
+ seen = set()
231
+ while isinstance(parent, str) and parent in nodes and parent not in seen:
232
+ seen.add(parent)
233
+ yield parent
234
+ parent = nodes[parent].get("parentId")
235
+
236
+ def message(record: dict[str, Any]) -> dict[str, Any]:
237
+ value = record.get("message")
238
+ return value if record.get("type") == "message" and isinstance(value, dict) else {}
239
+
240
+ replies: dict[str, list[str]] = {}
241
+ for rid, record in nodes.items():
242
+ msg = message(record)
243
+ if msg.get("role") == "assistant" and text_of(msg.get("content")).strip():
244
+ for parent in ancestors(record):
245
+ if message(nodes[parent]).get("role") == "user":
246
+ replies.setdefault(parent, []).append(rid)
247
+ break
248
+ out: dict[str, Context] = {}
249
+ for rid, record in nodes.items():
250
+ msg = message(record)
251
+ eid = make_event_id("pi", "steer", f"{hdr['id']}|{rid}")
252
+ if msg.get("role") != "user" or eid not in want:
253
+ continue
254
+ before = next((text_of(message(nodes[p]).get("content")) for p in ancestors(record)
255
+ if message(nodes[p]).get("role") == "assistant"
256
+ and text_of(message(nodes[p]).get("content")).strip()), "")
257
+ candidates = replies.get(rid, [])
258
+ terminal = [p for p in candidates if not any(
259
+ p in ancestors(nodes[q]) for q in candidates if p != q)]
260
+ # An append-only Pi file can contain several branches. Only a unique reply path is safe.
261
+ after = text_of(message(nodes[terminal[0]]).get("content")) if len(terminal) == 1 else ""
262
+ out[eid] = Context(text_of(msg.get("content")), before, after)
263
+ return out
264
+
265
+
266
+ def pi_context(home: Path, sessions: set[str], want: set[str],
267
+ until: datetime | None, *, paths: Iterable[str] | None = None) -> dict[str, Context]:
268
+ out: dict[str, Context] = {}
269
+ for path in pi.session_files(home) if paths is None else paths:
270
+ head = first_record(path)
271
+ if not head or head.get("type") != "session" or pi.header(head, path)["id"] not in sessions:
272
+ continue
273
+ out.update(_pi_context(path, want, until))
274
+ return out
275
+
276
+
277
+ def cursor_context(home: Path, sessions: set[str], want: set[str],
278
+ until: datetime | None, keys: RepoKeys, *,
279
+ paths: Iterable[str] | None = None) -> dict[str, Context]:
280
+ out: dict[str, Context] = {}
281
+ ambiguous: set[str] = set()
282
+ for path in cursor_cli.chat_stores(home) if paths is None else paths:
283
+ if Path(path).parent.name not in sessions:
284
+ continue
285
+ con = connect_ro(path)
286
+ if con is None:
287
+ raise CollectorUnavailable("cursor_cli: context store unreadable")
288
+ try:
289
+ meta = cursor_cli.chat_meta(con)
290
+ if not meta or meta.get("subagentInfo"):
291
+ continue
292
+ sid = meta.get("agentId") or Path(path).parent.name
293
+ if sid not in sessions:
294
+ continue
295
+ lists = cursor_cli.root_lists(con)
296
+ messages = cursor_cli.messages(con, {i for seq in lists for i in seq})
297
+ if sid in automated_sessions():
298
+ continue
299
+ first_ids: set[str] = set()
300
+ listed = {mid for seq in lists for mid in seq}
301
+ for seq in lists:
302
+ seen_requests: set[str] = set()
303
+ for mid in seq:
304
+ rid = cursor_cli.request_id(messages.get(mid, {}))
305
+ if rid and rid not in seen_requests:
306
+ first_ids.add(mid)
307
+ seen_requests.add(rid)
308
+ bases: dict[str, set[str]] = {}
309
+ prompts: list[tuple[str, str]] = []
310
+ for mid, prompt in messages.items():
311
+ rid = cursor_cli.request_id(prompt)
312
+ if not rid:
313
+ continue
314
+ text = text_of(prompt.get("content"))
315
+ query = cursor_cli.USER_QUERY.search(text)
316
+ text = query[1] if query else text
317
+ # Blob-suffixed IDs identify their text even if a later root starts with that blob.
318
+ prompts.append((make_event_id("cursor_cli", "steer", f"{sid}|{rid}|{mid}"), text))
319
+ if mid in first_ids or mid not in listed:
320
+ bases.setdefault(rid, set()).add(text)
321
+ for rid, texts in bases.items():
322
+ eid = make_event_id("cursor_cli", "steer", f"{sid}|{rid}")
323
+ if len(texts) != 1:
324
+ ambiguous.add(eid)
325
+ else:
326
+ prompts.append((eid, next(iter(texts))))
327
+ for eid, text in prompts:
328
+ if eid not in want:
329
+ continue
330
+ value = Context(text)
331
+ # Reply order is unknown; conflicting base prompt interpretations are omitted too.
332
+ if eid in out and out[eid] != value:
333
+ ambiguous.add(eid)
334
+ out[eid] = value
335
+ except (sqlite3.DatabaseError, TypeError, ValueError, IndexError) as exc:
336
+ raise CollectorUnavailable("cursor_cli: invalid context store") from exc
337
+ finally:
338
+ con.close()
339
+ return {eid: value for eid, value in out.items() if eid not in ambiguous}
340
+
341
+
342
+ def t3_context(home: Path, sessions: set[str], want: set[str],
343
+ until: datetime | None) -> dict[str, Context]:
344
+ """Resolve app message IDs locally; text never enters collector rows or watermarks."""
345
+ out: dict[str, Context] = {}
346
+ threads = tuple(sorted(sid[3:] for sid in sessions if sid.startswith("t3:")))
347
+ if not threads:
348
+ return out
349
+ try:
350
+ with t3.connection(home) as con:
351
+ placeholders = ','.join('?' for _ in threads)
352
+ condition = f'thread_id IN ({placeholders})'
353
+ params: tuple[Any, ...] = threads
354
+ if until is not None:
355
+ condition += ' AND julianday(created_at)<julianday(?)'
356
+ params += (until.isoformat(),)
357
+ rows = t3._read(con, f"SELECT message_id,thread_id,run_id,role,created_at,payload_json "
358
+ f"FROM {t3._PREFIX}messages WHERE {condition} "
359
+ "ORDER BY created_at,message_id", params)
360
+ by_thread: dict[str, list[tuple[str | None, str, int | None]]] = {}
361
+ counts: Counter[str] = Counter()
362
+ for mid, tid, runid, role, ts, raw in rows:
363
+ if "t3:" + tid not in sessions or not _in_time({"timestamp": ts}, until):
364
+ continue
365
+ payload = t3._payload(raw)
366
+ text_value = payload.get("text")
367
+ text = text_value if isinstance(text_value, str) else ""
368
+ if role == "user":
369
+ if not t3.confirmed_human(payload):
370
+ # An automated message ends any prior human reply interval.
371
+ by_thread.setdefault(tid, []).append(("", "", None))
372
+ continue
373
+ counts[tid] += 1
374
+ by_thread.setdefault(tid, []).append(
375
+ (make_event_id("t3", "steer", mid), text, counts[tid]))
376
+ elif role == "assistant":
377
+ by_thread.setdefault(tid, []).append((None, text, None))
378
+ for events in by_thread.values():
379
+ out.update(_join(events, want))
380
+ except (OSError, sqlite3.Error, ValueError, TypeError) as exc:
381
+ raise CollectorUnavailable("t3: local context unavailable") from exc
382
+ return out