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.
- agent_usage_tracker/__init__.py +8 -0
- agent_usage_tracker/backup.py +225 -0
- agent_usage_tracker/cli.py +479 -0
- agent_usage_tracker/collectors/__init__.py +115 -0
- agent_usage_tracker/collectors/_util.py +310 -0
- agent_usage_tracker/collectors/activity_common.py +294 -0
- agent_usage_tracker/collectors/activitywatch.py +394 -0
- agent_usage_tracker/collectors/claude_code.py +484 -0
- agent_usage_tracker/collectors/claude_code_activity.py +317 -0
- agent_usage_tracker/collectors/codex.py +612 -0
- agent_usage_tracker/collectors/commits.py +154 -0
- agent_usage_tracker/collectors/cursor.py +193 -0
- agent_usage_tracker/collectors/cursor_api.py +247 -0
- agent_usage_tracker/collectors/cursor_cli.py +592 -0
- agent_usage_tracker/collectors/cursor_sdk.py +95 -0
- agent_usage_tracker/collectors/generic_sdk.py +100 -0
- agent_usage_tracker/collectors/pi.py +397 -0
- agent_usage_tracker/collectors/session_digest.py +379 -0
- agent_usage_tracker/collectors/t3.py +490 -0
- agent_usage_tracker/config.py +392 -0
- agent_usage_tracker/coverage.py +615 -0
- agent_usage_tracker/coverage_catalog.json +588 -0
- agent_usage_tracker/cursor_run.py +62 -0
- agent_usage_tracker/diagnostics.py +74 -0
- agent_usage_tracker/episode_store.py +78 -0
- agent_usage_tracker/event.py +132 -0
- agent_usage_tracker/git_day_cache.py +128 -0
- agent_usage_tracker/git_days.py +499 -0
- agent_usage_tracker/git_safe.py +58 -0
- agent_usage_tracker/harness_inventory.py +208 -0
- agent_usage_tracker/history_steers.py +88 -0
- agent_usage_tracker/improve/__init__.py +1 -0
- agent_usage_tracker/improve/case_view.py +112 -0
- agent_usage_tracker/improve/cli.py +138 -0
- agent_usage_tracker/improve/desktop.py +347 -0
- agent_usage_tracker/improve/evidence.py +322 -0
- agent_usage_tracker/improve/judge.py +671 -0
- agent_usage_tracker/improve/proposals.py +72 -0
- agent_usage_tracker/improve/rubric.py +360 -0
- agent_usage_tracker/improve/rubrics/avoidable_recovery.md +20 -0
- agent_usage_tracker/improve/rubrics/code_quality.md +22 -0
- agent_usage_tracker/improve/rubrics/completion_supported.md +20 -0
- agent_usage_tracker/improve/rubrics/redundant_tests.md +21 -0
- agent_usage_tracker/improve/rubrics/verbosity.md +21 -0
- agent_usage_tracker/improve/runtime.py +117 -0
- agent_usage_tracker/improve/schedule.py +291 -0
- agent_usage_tracker/improve/search_check.py +563 -0
- agent_usage_tracker/improve/store.py +244 -0
- agent_usage_tracker/improve/studies.py +285 -0
- agent_usage_tracker/improve/study_cli.py +46 -0
- agent_usage_tracker/improve/study_decision.py +121 -0
- agent_usage_tracker/improve/study_followup.py +174 -0
- agent_usage_tracker/improve/t3.py +269 -0
- agent_usage_tracker/improve/trial.py +318 -0
- agent_usage_tracker/improve/validation.py +148 -0
- agent_usage_tracker/inspector.py +350 -0
- agent_usage_tracker/label/__init__.py +22 -0
- agent_usage_tracker/label/backends.py +742 -0
- agent_usage_tracker/label/bakeoff.py +661 -0
- agent_usage_tracker/label/baseline.py +173 -0
- agent_usage_tracker/label/clef.py +258 -0
- agent_usage_tracker/label/context.py +382 -0
- agent_usage_tracker/label/core.py +355 -0
- agent_usage_tracker/label/covered_run.py +92 -0
- agent_usage_tracker/label/evalset.py +154 -0
- agent_usage_tracker/label/gold.py +209 -0
- agent_usage_tracker/label/local.py +326 -0
- agent_usage_tracker/label/local_parallel.py +232 -0
- agent_usage_tracker/label/merge.py +57 -0
- agent_usage_tracker/label/outcome_coverage.py +57 -0
- agent_usage_tracker/label/replay.py +194 -0
- agent_usage_tracker/label/run.py +339 -0
- agent_usage_tracker/label/tasks.py +579 -0
- agent_usage_tracker/label/teach.py +145 -0
- agent_usage_tracker/local_page.py +128 -0
- agent_usage_tracker/manual_time.py +69 -0
- agent_usage_tracker/net.py +23 -0
- agent_usage_tracker/observation_cli.py +65 -0
- agent_usage_tracker/observation_report.py +116 -0
- agent_usage_tracker/observation_server.py +110 -0
- agent_usage_tracker/observations.py +365 -0
- agent_usage_tracker/outcome_git.py +383 -0
- agent_usage_tracker/outcomes.py +464 -0
- agent_usage_tracker/pricing.py +248 -0
- agent_usage_tracker/push.py +530 -0
- agent_usage_tracker/redact.py +45 -0
- agent_usage_tracker/repo_keys.py +165 -0
- agent_usage_tracker/review.py +252 -0
- agent_usage_tracker/scan.py +472 -0
- agent_usage_tracker/scheduled.py +164 -0
- agent_usage_tracker/sdk.py +79 -0
- agent_usage_tracker/sink.py +82 -0
- agent_usage_tracker/sink_stack/README.md +71 -0
- agent_usage_tracker/sink_stack/apply.sh +63 -0
- agent_usage_tracker/sink_stack/compose.yaml +94 -0
- agent_usage_tracker/sink_stack/env.example +20 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-effort.json +634 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-usage.json +1279 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/dashboards/aut.yaml +9 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/datasources/agents.yaml +16 -0
- agent_usage_tracker/sink_stack/sql/01_schema.sql +1186 -0
- agent_usage_tracker/sink_stack/sql/02_price.sql +60 -0
- agent_usage_tracker/sink_stack/sql/03_effort.sql +1180 -0
- agent_usage_tracker/sink_stack/sql/04_grants.sql +149 -0
- agent_usage_tracker/sink_stack/sql/refresh.sql +66 -0
- agent_usage_tracker/sources.py +180 -0
- agent_usage_tracker/state.py +191 -0
- agent_usage_tracker/telemetry.py +165 -0
- agent_usage_tracker-0.1.0.dist-info/METADATA +366 -0
- agent_usage_tracker-0.1.0.dist-info/RECORD +113 -0
- agent_usage_tracker-0.1.0.dist-info/WHEEL +4 -0
- agent_usage_tracker-0.1.0.dist-info/entry_points.txt +3 -0
- agent_usage_tracker-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"""What this machine's own logs say about a session, for the local pages only.
|
|
2
|
+
|
|
3
|
+
`local_info` reads Claude Code, Codex, Pi and Cursor CLI logs through their collectors and returns
|
|
4
|
+
one dict per session: agent, folder, when it ran, prompts (redacted, clipped), the commit subjects
|
|
5
|
+
and PR titles it made, the files it edited most, its titles and branches. `aut episodes review` and
|
|
6
|
+
`aut inspect` put that on a page served from 127.0.0.1; it is never written to Postgres, a log, the
|
|
7
|
+
terminal or a file.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
import sys
|
|
16
|
+
from collections import Counter
|
|
17
|
+
from datetime import datetime, timezone
|
|
18
|
+
from functools import partial
|
|
19
|
+
from pathlib import Path, PurePath
|
|
20
|
+
from typing import Any, Callable, Iterator, TypedDict
|
|
21
|
+
|
|
22
|
+
from ..redact import clean_text
|
|
23
|
+
from ..state import parse_record
|
|
24
|
+
from . import claude_code, codex, cursor, cursor_cli, pi
|
|
25
|
+
from ._util import dict_or_empty, epoch_ms_to_iso
|
|
26
|
+
from .activity_common import first_record, text_of
|
|
27
|
+
from .commits import COMMIT_LINE, commit_args_at, commit_call
|
|
28
|
+
|
|
29
|
+
_TITLE = re.compile(r'"(?:aiTitle|customTitle)":\s*"((?:[^"\\]|\\.)*)"')
|
|
30
|
+
_BRANCH = re.compile(r'"(?:gitBranch|worktreeBranch)":\s*"((?:[^"\\]|\\.)*)"')
|
|
31
|
+
_TEXT_MAX = 200
|
|
32
|
+
_TAG = re.compile(r"</?[\w:-]+[^<>]{0,200}>")
|
|
33
|
+
_WS = re.compile(r"\s+")
|
|
34
|
+
_Q = r"""(?:\\"((?:[^"\\]|\\[^"])*)\\"|"((?:[^"\\]|\\.)*)"|'([^']*)'|(\S+))"""
|
|
35
|
+
_MSG = re.compile(rf"\s(?:-m|--message)(?:=|\s+){_Q}")
|
|
36
|
+
_PR_TITLE = re.compile(rf"\s(?:-t|--title)(?:=|\s+){_Q}")
|
|
37
|
+
# a command up to its first unquoted `&&`, `||`, `;`, `|` or newline
|
|
38
|
+
_SEGMENT = re.compile(r"""(?:"(?:[^"\\]|\\.)*"|'[^']*'|(?<!&)&(?!&)|[^"'&;|\n])*""")
|
|
39
|
+
# Cursor text with no <user_query>: every tagged block in it is harness context
|
|
40
|
+
_BLOCK = re.compile(r"<([\w-]+)[^<>]*>.*?</\1>", re.S)
|
|
41
|
+
_EDIT_TOOLS = {"Edit", "MultiEdit", "Write", "NotebookEdit", "StrReplace", "edit", "write"}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _uniq(xs: list[str]) -> list[str]:
|
|
45
|
+
return list(dict.fromkeys(x for x in xs if x))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _unescape(s: str) -> str:
|
|
49
|
+
try:
|
|
50
|
+
return json.loads(f'"{s}"')
|
|
51
|
+
except ValueError:
|
|
52
|
+
return s
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def clip(text: str, n: int = _TEXT_MAX) -> str:
|
|
56
|
+
"""Local text as one short line for the page: context blocks and tags out, secrets redacted,
|
|
57
|
+
then cut to about n characters."""
|
|
58
|
+
t = _WS.sub(" ", _TAG.sub(" ", claude_code.CONTEXT_BLOCK_RE.sub("", text))).strip()
|
|
59
|
+
t = clean_text(t)
|
|
60
|
+
return t if len(t) <= n else t[:n - 1].rstrip() + "\u2026"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _quoted(rx: re.Pattern[str], cmd: str, at: int) -> str | None:
|
|
64
|
+
"""First line of the quoted argument rx finds after `at`; for -m "$(cat <<EOF ...)" the
|
|
65
|
+
heredoc's first line. Only the command segment at `at` is searched."""
|
|
66
|
+
seg = _SEGMENT.match(cmd, at)
|
|
67
|
+
# _SEGMENT also matches empty, so seg is never None; the check narrows its type
|
|
68
|
+
m = rx.search(cmd[:seg.end() if seg else len(cmd)], at)
|
|
69
|
+
if not m:
|
|
70
|
+
return None
|
|
71
|
+
v = next(g for g in m.groups() if g is not None)
|
|
72
|
+
lines = v.split("\n")
|
|
73
|
+
v = lines[1] if v.startswith("$(") and len(lines) > 1 else lines[0]
|
|
74
|
+
return v.strip() or None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _when(ts: object) -> datetime | None:
|
|
78
|
+
try:
|
|
79
|
+
d = datetime.fromisoformat(str(ts).replace("Z", "+00:00"))
|
|
80
|
+
except ValueError:
|
|
81
|
+
return None
|
|
82
|
+
return d if d.tzinfo else None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class SessionView(TypedDict, total=False):
|
|
86
|
+
"""What Digest.view shows. Digest.view sets every key; total=False so a session with no local
|
|
87
|
+
log can be the empty view."""
|
|
88
|
+
agent: str
|
|
89
|
+
folder: str
|
|
90
|
+
start: datetime | None
|
|
91
|
+
minutes: int | None
|
|
92
|
+
n_prompts: int
|
|
93
|
+
first: str
|
|
94
|
+
last: str
|
|
95
|
+
commits: list[str]
|
|
96
|
+
prs: list[str]
|
|
97
|
+
files: list[str]
|
|
98
|
+
titles: list[str]
|
|
99
|
+
branches: list[str]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class Digest:
|
|
103
|
+
"""What one session's local log says, for the page only."""
|
|
104
|
+
|
|
105
|
+
def __init__(self, agent: str) -> None:
|
|
106
|
+
self.agent = agent
|
|
107
|
+
self.cwd: str | None = None
|
|
108
|
+
self.first: datetime | None = None
|
|
109
|
+
self.last: datetime | None = None
|
|
110
|
+
self.prompts: list[str] = []
|
|
111
|
+
self.commits: list[str] = []
|
|
112
|
+
self.prs: list[str] = []
|
|
113
|
+
self.files: Counter[str] = Counter()
|
|
114
|
+
self.titles: list[str] = []
|
|
115
|
+
self.branches: list[str] = []
|
|
116
|
+
|
|
117
|
+
def note_time(self, ts: object, cwd: object = None) -> None:
|
|
118
|
+
d = _when(ts)
|
|
119
|
+
if d:
|
|
120
|
+
self.first = min(self.first or d, d)
|
|
121
|
+
self.last = max(self.last or d, d)
|
|
122
|
+
if isinstance(cwd, str) and cwd and not self.cwd:
|
|
123
|
+
self.cwd = cwd
|
|
124
|
+
|
|
125
|
+
def note_shell(self, cmd: object, out: object, err: bool) -> None:
|
|
126
|
+
"""A shell call that succeeded: the commits it made (git's `[branch sha] subject` line,
|
|
127
|
+
else the -m message) and the PR it opened (gh pr create --title)."""
|
|
128
|
+
if not isinstance(cmd, str) or err:
|
|
129
|
+
return
|
|
130
|
+
out = out if isinstance(out, str) else ""
|
|
131
|
+
if commit_call(cmd):
|
|
132
|
+
subj = [m["subj"] for m in COMMIT_LINE.finditer(out)]
|
|
133
|
+
at = commit_args_at(cmd) if not subj else None
|
|
134
|
+
msg = _quoted(_MSG, cmd, at) if at is not None else None
|
|
135
|
+
self.commits += subj or ([msg] if msg else [])
|
|
136
|
+
i = cmd.find("gh pr create")
|
|
137
|
+
title = _quoted(_PR_TITLE, cmd, i) if i >= 0 else None
|
|
138
|
+
if title:
|
|
139
|
+
self.prs.append(title)
|
|
140
|
+
|
|
141
|
+
def note_edit(self, name: object, args: object) -> None:
|
|
142
|
+
if name in _EDIT_TOOLS and isinstance(args, dict):
|
|
143
|
+
p = args.get("file_path") or args.get("path") or args.get("notebook_path")
|
|
144
|
+
if isinstance(p, str) and p:
|
|
145
|
+
self.files[p] += 1
|
|
146
|
+
|
|
147
|
+
def _file(self, p: str) -> str:
|
|
148
|
+
base = (self.cwd or "").rstrip("/\\")
|
|
149
|
+
if base and p.startswith(base) and p[len(base):len(base) + 1] in ("/", "\\"):
|
|
150
|
+
return p[len(base) + 1:]
|
|
151
|
+
return "/".join(PurePath(p).parts[-2:])
|
|
152
|
+
|
|
153
|
+
def view(self) -> SessionView:
|
|
154
|
+
mins = int((self.last - self.first).total_seconds() // 60) \
|
|
155
|
+
if self.first and self.last else None
|
|
156
|
+
return {"agent": self.agent,
|
|
157
|
+
"folder": "/".join(PurePath(self.cwd).parts[-2:]) if self.cwd else "",
|
|
158
|
+
"start": self.first.astimezone() if self.first else None, "minutes": mins,
|
|
159
|
+
"n_prompts": len(self.prompts),
|
|
160
|
+
"first": clip(self.prompts[0]) if self.prompts else "",
|
|
161
|
+
"last": clip(self.prompts[-1]) if len(self.prompts) > 1 else "",
|
|
162
|
+
"commits": [clip(c, 120) for c in _uniq(self.commits)][:5],
|
|
163
|
+
"prs": [clip(t, 120) for t in _uniq(self.prs)][:3],
|
|
164
|
+
"files": [clean_text(self._file(p)) for p, _n in self.files.most_common(3)],
|
|
165
|
+
"titles": [clean_text(t) for t in _uniq(self.titles)],
|
|
166
|
+
"branches": [clean_text(b) for b in _uniq(self.branches)]}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _claude_blocks(content: object, d: Digest, pend: dict[object, Any], seen: set[object]) -> None:
|
|
170
|
+
for b in content if isinstance(content, list) else ():
|
|
171
|
+
if not isinstance(b, dict):
|
|
172
|
+
continue
|
|
173
|
+
if b.get("type") == "tool_use" and b.get("id") not in seen:
|
|
174
|
+
seen.add(b.get("id")) # Claude Code re-logs a message's blocks
|
|
175
|
+
d.note_edit(b.get("name"), b.get("input"))
|
|
176
|
+
if b.get("name") == "Bash" and isinstance(b.get("input"), dict):
|
|
177
|
+
pend[b.get("id")] = b["input"].get("command")
|
|
178
|
+
elif b.get("type") == "tool_result" and b.get("tool_use_id") in pend:
|
|
179
|
+
d.note_shell(pend.pop(b["tool_use_id"]), text_of(b.get("content")),
|
|
180
|
+
b.get("is_error") is True)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _digest_claude_log(f: str, d: Digest) -> None:
|
|
184
|
+
pend: dict[object, Any] = {}
|
|
185
|
+
seen: set[object] = set()
|
|
186
|
+
loops = 0
|
|
187
|
+
with open(f, encoding="utf-8", errors="replace") as fh:
|
|
188
|
+
for line in fh:
|
|
189
|
+
d.titles += map(_unescape, _TITLE.findall(line))
|
|
190
|
+
d.branches += map(_unescape, _BRANCH.findall(line))
|
|
191
|
+
o = parse_record(line) # a malformed line holds nothing to show
|
|
192
|
+
if o is None:
|
|
193
|
+
continue
|
|
194
|
+
d.note_time(o.get("timestamp"), o.get("cwd"))
|
|
195
|
+
text = claude_code.human_text(o)
|
|
196
|
+
if text is not None:
|
|
197
|
+
loops += claude_code.is_old_loop(o, text)
|
|
198
|
+
if loops <= 1 or not claude_code.is_old_loop(o, text):
|
|
199
|
+
d.prompts.append(text)
|
|
200
|
+
continue
|
|
201
|
+
m = o.get("message")
|
|
202
|
+
_claude_blocks(m.get("content") if isinstance(m, dict) else None, d, pend, seen)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _codex_item(it: dict[str, Any], d: Digest) -> None:
|
|
206
|
+
if it.get("type") == "CommandExecution":
|
|
207
|
+
code = it.get("exit_code")
|
|
208
|
+
d.note_shell(codex.script_of(it.get("command")), it.get("aggregated_output"),
|
|
209
|
+
isinstance(code, int) and code != 0)
|
|
210
|
+
elif it.get("type") == "FileChange" and isinstance(it.get("changes"), dict):
|
|
211
|
+
d.files.update(k for k in it["changes"] if isinstance(k, str))
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _codex_record(t: object, p: dict[str, Any], has_um: bool, human: bool, d: Digest) -> None:
|
|
215
|
+
pr = codex.prompt_of(t, p, has_um)
|
|
216
|
+
if pr is not None:
|
|
217
|
+
if human and not codex.is_scheduled_automation(pr[0]):
|
|
218
|
+
d.prompts.append(pr[0])
|
|
219
|
+
elif t == "event_msg" and p.get("type") == "item_completed" and isinstance(p.get("item"), dict):
|
|
220
|
+
_codex_item(p["item"], d)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _digest_codex_log(f: str, title: str, d: Digest) -> None:
|
|
224
|
+
d.titles.append(title)
|
|
225
|
+
with open(f, encoding="utf-8", errors="replace") as fh: # one bad byte keeps its record
|
|
226
|
+
recs = codex.parsed_records([line.encode() for line in fh], 0, {}, Counter())
|
|
227
|
+
if not recs:
|
|
228
|
+
return
|
|
229
|
+
meta = recs[0].payload if recs[0].type == "session_meta" else {}
|
|
230
|
+
git = dict_or_empty(meta.get("git"))
|
|
231
|
+
if isinstance(git.get("branch"), str):
|
|
232
|
+
d.branches.append(git["branch"])
|
|
233
|
+
human = codex.run_origin(meta) not in ("subagent", "automated")
|
|
234
|
+
has_um = any(r.type == "event_msg" and r.payload.get("type") == "user_message" for r in recs)
|
|
235
|
+
for i, (o, t, p, copied) in enumerate(recs):
|
|
236
|
+
if copied and i:
|
|
237
|
+
continue
|
|
238
|
+
d.note_time(o.get("timestamp"), p.get("cwd") if t in ("session_meta", "turn_context") else None)
|
|
239
|
+
_codex_record(t, p, has_um, human, d)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _pi_message(m: dict[str, Any], d: Digest, pend: dict[object, Any]) -> None:
|
|
243
|
+
role, content = m.get("role"), m.get("content")
|
|
244
|
+
if role == "user":
|
|
245
|
+
text = text_of(content)
|
|
246
|
+
sk = pi.SKILL.match(text) # a skill invocation: its name, not SKILL.md
|
|
247
|
+
d.prompts.append(f"/{sk[1]} {text.partition('</skill>')[2]}".strip() if sk else text)
|
|
248
|
+
elif role == "assistant" and isinstance(content, list):
|
|
249
|
+
for b in content:
|
|
250
|
+
if isinstance(b, dict) and b.get("type") == "toolCall":
|
|
251
|
+
d.note_edit(b.get("name"), b.get("arguments"))
|
|
252
|
+
args = b.get("arguments")
|
|
253
|
+
if b.get("name") == "bash" and isinstance(args, dict):
|
|
254
|
+
pend[b.get("id")] = args.get("command")
|
|
255
|
+
elif role == "toolResult" and m.get("toolCallId") in pend:
|
|
256
|
+
d.note_shell(pend.pop(m["toolCallId"]), text_of(content), m.get("isError") is True)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _digest_pi_log(f: str, hdr: dict[str, Any], d: Digest) -> None:
|
|
260
|
+
d.note_time(hdr["ts"], hdr["cwd"])
|
|
261
|
+
pend: dict[object, Any] = {}
|
|
262
|
+
with open(f, encoding="utf-8", errors="replace") as fh:
|
|
263
|
+
for line in fh:
|
|
264
|
+
o = parse_record(line) # a malformed line holds nothing to show
|
|
265
|
+
if o is None or not isinstance(o.get("message"), dict) \
|
|
266
|
+
or o.get("type") != "message" or pi.is_fork_copy(o, hdr):
|
|
267
|
+
continue
|
|
268
|
+
d.note_time(o.get("timestamp"))
|
|
269
|
+
_pi_message(o["message"], d, pend)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _cursor_prompts(msgs: dict[str, dict[str, Any]], created: str | None, d: Digest,
|
|
273
|
+
calls: dict[object, Any]) -> None:
|
|
274
|
+
prompts: dict[str, Any] = {}
|
|
275
|
+
for m in msgs.values():
|
|
276
|
+
r = cursor_cli.request_id(m)
|
|
277
|
+
if r and r not in prompts:
|
|
278
|
+
text = text_of(m.get("content"))
|
|
279
|
+
q = cursor_cli.USER_QUERY.search(text)
|
|
280
|
+
ts = cursor_cli.prompt_ts(text, created)
|
|
281
|
+
d.note_time(ts)
|
|
282
|
+
prompts[r] = (_when(ts) or datetime.min.replace(tzinfo=timezone.utc),
|
|
283
|
+
q[1] if q else _BLOCK.sub(" ", text))
|
|
284
|
+
content = m.get("content")
|
|
285
|
+
for b in content if isinstance(content, list) else ():
|
|
286
|
+
if isinstance(b, dict) and b.get("type") == "tool-call":
|
|
287
|
+
calls[b.get("toolCallId")] = b.get("args")
|
|
288
|
+
d.note_edit(b.get("toolName"), b.get("args"))
|
|
289
|
+
d.prompts = [t for _ts, t in sorted(prompts.values(), key=lambda x: x[0])]
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _cursor_shell_results(msgs: dict[str, dict[str, Any]], d: Digest,
|
|
293
|
+
calls: dict[object, Any]) -> None:
|
|
294
|
+
for m in msgs.values():
|
|
295
|
+
content = m.get("content") if m.get("role") == "tool" else None
|
|
296
|
+
for b in content if isinstance(content, list) else ():
|
|
297
|
+
args = calls.get(b.get("toolCallId")) if isinstance(b, dict) else None
|
|
298
|
+
if isinstance(args, dict) and b.get("type") == "tool-result" \
|
|
299
|
+
and b.get("toolName") == "Shell":
|
|
300
|
+
res = b.get("result") if isinstance(b.get("result"), str) else ""
|
|
301
|
+
ex = cursor_cli.EXIT_LINE.match(res)
|
|
302
|
+
d.note_time(None, args.get("working_directory"))
|
|
303
|
+
d.note_shell(args.get("command"), res[ex.end():] if ex else res,
|
|
304
|
+
not ex or ex[1] != "0")
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _digest_cursor_chat(f: str, d: Digest) -> None:
|
|
308
|
+
con = cursor.connect_ro(f)
|
|
309
|
+
if con is None:
|
|
310
|
+
return
|
|
311
|
+
try:
|
|
312
|
+
meta = cursor_cli.chat_meta(con) or {}
|
|
313
|
+
msgs = cursor_cli.messages(con, {i for seq in cursor_cli.root_lists(con) for i in seq})
|
|
314
|
+
finally:
|
|
315
|
+
con.close()
|
|
316
|
+
created = epoch_ms_to_iso(meta.get("createdAt"))
|
|
317
|
+
d.note_time(created)
|
|
318
|
+
calls: dict[object, Any] = {}
|
|
319
|
+
_cursor_prompts(msgs, created, d, calls)
|
|
320
|
+
_cursor_shell_results(msgs, d, calls)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _pi_logs(home: Path, sessions: set[str]) -> Iterator[tuple[str, str, dict[str, Any]]]:
|
|
324
|
+
"""(session id, file, header) of each wanted Pi session. The id is in the file name, except
|
|
325
|
+
where only the header has it."""
|
|
326
|
+
for f in pi.session_files(home):
|
|
327
|
+
u = pi.uuid_in(f)
|
|
328
|
+
if u and u not in sessions:
|
|
329
|
+
continue
|
|
330
|
+
first = first_record(f) or {}
|
|
331
|
+
hdr = pi.header(first if first.get("type") == "session" else {}, f)
|
|
332
|
+
if hdr["id"] in sessions:
|
|
333
|
+
yield hdr["id"], f, hdr
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _session_logs(home: Path, sessions: set[str], failed: Counter[str]) -> Iterator[tuple[str, str,
|
|
337
|
+
Callable[[Digest], None]]]:
|
|
338
|
+
"""(session id, agent name, the reader that fills a Digest) of each local log of a wanted
|
|
339
|
+
session."""
|
|
340
|
+
try:
|
|
341
|
+
claude_files = claude_code.main_session_files(home)
|
|
342
|
+
except Exception as exc: # noqa: BLE001 # local discovery must not hide other providers
|
|
343
|
+
failed[type(exc).__name__] += 1
|
|
344
|
+
claude_files = []
|
|
345
|
+
for f in claude_files:
|
|
346
|
+
if Path(f).stem in sessions:
|
|
347
|
+
yield Path(f).stem, "Claude Code", partial(_digest_claude_log, f)
|
|
348
|
+
names = codex.thread_names(home)
|
|
349
|
+
for f in codex.session_files(home):
|
|
350
|
+
sid = next((s for s in sessions if f.endswith(f"-{s}.jsonl")), None)
|
|
351
|
+
if sid:
|
|
352
|
+
yield sid, "Codex", partial(_digest_codex_log, f, names.get(sid, ""))
|
|
353
|
+
for sid, f, hdr in _pi_logs(home, sessions):
|
|
354
|
+
yield sid, "Pi", partial(_digest_pi_log, f, hdr)
|
|
355
|
+
# shortcut: a chat is found by its directory name, which is its agentId in every store seen
|
|
356
|
+
for f in cursor_cli.chat_stores(home):
|
|
357
|
+
sid = os.path.basename(os.path.dirname(f))
|
|
358
|
+
if sid in sessions:
|
|
359
|
+
yield sid, "Cursor", partial(_digest_cursor_chat, f)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def local_info(home: Path, sessions: set[str]) -> dict[str, SessionView]:
|
|
363
|
+
"""session id -> what the logs on this machine say about it (see Digest.view). Best effort: a
|
|
364
|
+
session that cannot be read or shown is skipped, and only its count and the exception's type
|
|
365
|
+
reach stderr, because a message or traceback could print local text. shortcut: Cursor IDE chats
|
|
366
|
+
(state.vscdb) are not read; their sessions show no local text."""
|
|
367
|
+
info: dict[str, SessionView] = {}
|
|
368
|
+
failed: Counter[str] = Counter()
|
|
369
|
+
for sid, agent, read in _session_logs(home, sessions, failed):
|
|
370
|
+
d = Digest(agent)
|
|
371
|
+
try:
|
|
372
|
+
read(d)
|
|
373
|
+
info[sid] = d.view()
|
|
374
|
+
except Exception as exc: # noqa: BLE001 # any shape of old log record can land here
|
|
375
|
+
failed[type(exc).__name__] += 1
|
|
376
|
+
if failed:
|
|
377
|
+
print(f"aut: {sum(failed.values())} local session log(s) not shown "
|
|
378
|
+
f"({', '.join(sorted(failed))})", file=sys.stderr)
|
|
379
|
+
return info
|