Skip to content

Replay your coding agent sessions as a map and a timeline

fix(stats): serve every file of a capped session to the heatmap

aiswaryasankartestbc0f79ac66e7
diff --git a/python/src/agentplay/facts.py b/python/src/agentplay/facts.py
index a01bdd7..5639071 100644
--- a/python/src/agentplay/facts.py
+++ b/python/src/agentplay/facts.py
@@ -145,6 +145,25 @@ def _rereads_after_compaction(evs: list[dict[str, Any]]) -> int:
return n
+def file_counts(doc: dict[str, Any]) -> dict[str, dict[str, int]]:
+ """Every file the session touched, ``repo:path -> {hits, reads, edits}``, uncapped.
+
+ ``compute`` stores at most ``MAX_FACT_FILES`` of these; ``/api/stats`` reads the
+ full set for a capped session so the cross-session heatmap misses no path.
+ """
+ repo_names = {r["id"]: r["name"] for r in doc.get("repos") or [] if "id" in r and "name" in r}
+ files: dict[str, dict[str, int]] = {}
+ for f in doc.get("files") or []:
+ hits, reads, edits = int(f.get("hits") or 0), int(f.get("reads") or 0), int(f.get("edits") or 0)
+ if not (hits or reads or edits) or not f.get("path"):
+ continue
+ key = f"{repo_names.get(f.get('repo'), '')}:{f['path']}"
+ # Two repos can share a display name; add their counters rather than drop one.
+ prev = files.get(key) or {"hits": 0, "reads": 0, "edits": 0}
+ files[key] = {"hits": prev["hits"] + hits, "reads": prev["reads"] + reads, "edits": prev["edits"] + edits}
+ return files
+
+
def compute(doc: dict[str, Any]) -> dict[str, Any]:
"""The facts for one map document.
@@ -159,7 +178,6 @@ def compute(doc: dict[str, Any]) -> dict[str, Any]:
evs = sorted(doc.get("events") or [], key=lambda e: e.get("seq", 0))
outcome = str(s.get("outcome") or "completed")
llm = [e for e in evs if e["kind"] == "llm"]
- repo_names = {r["id"]: r["name"] for r in doc.get("repos") or [] if "id" in r and "name" in r}
cmd: dict[str, dict[str, Any]] = {}
for e in evs:
@@ -178,15 +196,7 @@ def compute(doc: dict[str, Any]) -> dict[str, Any]:
cls = str(e.get("errClass") or "other")
c["byClass"][cls] = c["byClass"].get(cls, 0) + 1
- files: dict[str, dict[str, int]] = {}
- for f in doc.get("files") or []:
- hits, reads, edits = int(f.get("hits") or 0), int(f.get("reads") or 0), int(f.get("edits") or 0)
- if not (hits or reads or edits) or not f.get("path"):
- continue
- key = f"{repo_names.get(f.get('repo'), '')}:{f['path']}"
- # Two repos can share a display name; add their counters rather than drop one.
- prev = files.get(key) or {"hits": 0, "reads": 0, "edits": 0}
- files[key] = {"hits": prev["hits"] + hits, "reads": prev["reads"] + reads, "edits": prev["edits"] + edits}
+ files = file_counts(doc)
files_rest: dict[str, int] = {}
if len(files) > MAX_FACT_FILES:
ranked = sorted(files.items(), key=lambda kv: (-kv[1]["hits"], -(kv[1]["reads"] + kv[1]["edits"]), kv[0]))
diff --git a/python/src/agentplay/server.py b/python/src/agentplay/server.py
index 4de44b0..204dc0a 100644
--- a/python/src/agentplay/server.py
+++ b/python/src/agentplay/server.py
@@ -214,6 +214,8 @@ class _Handler(BaseHTTPRequestHandler):
# map's content hash: the server never writes the index, which a background
# scan may be saving at the same moment. The next build persists them.
facts_cache: dict[tuple[str, str], dict[str, Any] | None] = {}
+ # Uncapped `files` for sessions past facts.MAX_FACT_FILES, keyed like `facts_cache`.
+ files_cache: dict[tuple[str, str], dict[str, Any]] = {}
# search.json as last read (by mtime and size) and postings computed for
# sessions it does not cover yet. In memory only, like `facts_cache`.
search_file: tuple[Any, dict[str, Any]] = (None, {})
@@ -606,6 +608,19 @@ class _Handler(BaseHTTPRequestHandler):
self.facts_cache[key] = facts.stamp(doc)["facts"] if doc is not None else None
return self.facts_cache[key]
+ def _all_files(self, row: dict[str, Any], f: dict[str, Any]) -> dict[str, Any]:
+ """A capped session's full ``files`` (stored facts keep ``facts.MAX_FACT_FILES``).
+
+ The heatmap sums files across sessions, so a path outside each session's
+ top files can still be among the busiest overall. Rare (sessions past the
+ cap), read from the stored map once per content hash.
+ """
+ key = (str(row.get("sessionId") or ""), str(row.get("contentHash") or ""))
+ if key not in self.files_cache:
+ doc = self.store.get(key[0])
+ self.files_cache[key] = facts.file_counts(doc) if doc is not None else f.get("files") or {}
+ return self.files_cache[key]
+
def _stats(self, q: dict[str, list[str]]) -> None:
lo = (q.get("from") or [""])[0]
hi = (q.get("to") or [""])[0]
@@ -616,6 +631,9 @@ class _Handler(BaseHTTPRequestHandler):
continue
with self.lock:
f = self._facts(r)
+ if f is not None and f.get("filesRest"):
+ # The full list replaces the capped one, so its remainder would double-count.
+ f = {**{k: v for k, v in f.items() if k != "filesRest"}, "files": self._all_files(r, f)}
if f is None:
continue
out.append({**{k: r.get(k) for k in _STATS_FIELDS}, "facts": f})
@@ -724,7 +742,7 @@ def serve(
handler = type("Handler", (_Handler,), {
"store": store or Store(), "static": viewer_root(), "scanning": scanning,
# Per server, so two servers in one process never share computed facts or postings.
- "facts_cache": {}, "search_mem": {}, "search_file": (None, {}),
+ "facts_cache": {}, "files_cache": {}, "search_mem": {}, "search_file": (None, {}),
})
try:
return _Server((host, port), handler)
diff --git a/python/tests/test_search_quality.py b/python/tests/test_search_quality.py
index 9ee82d2..d0876f4 100644
--- a/python/tests/test_search_quality.py
+++ b/python/tests/test_search_quality.py
@@ -172,3 +172,30 @@ def test_facts_files_are_capped_to_the_busiest_with_a_remainder():
def test_small_sessions_have_no_remainder():
f = facts.compute(_doc([], [{"id": 0, "repo": "r0", "path": "a.ts", "hits": 1, "reads": 1, "edits": 0}]))
assert "filesRest" not in f
+
+
+def test_stats_serves_every_file_of_a_capped_session(tmp_path):
+ """A path below each session's cap still reaches the cross-session heatmap."""
+ st = Store(tmp_path)
+ for sid in ("s1", "s2"):
+ busy = [{"id": i, "repo": "r0", "path": f"{sid}/f{i}.ts", "hits": 5, "reads": 1, "edits": 0}
+ for i in range(facts.MAX_FACT_FILES + 10)]
+ shared = {"id": 9999, "repo": "r0", "path": "shared.ts", "hits": 1, "reads": 1, "edits": 0}
+ doc = _doc([], busy + [shared])
+ doc["session"]["id"] = sid
+ stamped = facts.stamp(doc)
+ assert "acme/api:shared.ts" not in stamped["facts"]["files"] # the stored facts stay capped
+ st.put(doc, {"sessionId": sid, "contentHash": f"h{sid}", "calls": 1, "sessionDate": "2026-10-01", **stamped})
+ st.save_index()
+ httpd = serve(0, store=Store(tmp_path))
+ threading.Thread(target=httpd.serve_forever, daemon=True).start()
+ try:
+ conn = http.client.HTTPConnection("127.0.0.1", httpd.server_address[1])
+ conn.request("GET", "/api/stats")
+ sessions = json.loads(conn.getresponse().read())["sessions"]
+ assert len(sessions) == 2
+ assert all(s["facts"]["files"]["acme/api:shared.ts"]["hits"] == 1 for s in sessions)
+ assert all(len(s["facts"]["files"]) == facts.MAX_FACT_FILES + 11 for s in sessions)
+ assert not any("filesRest" in s["facts"] for s in sessions)
+ finally:
+ httpd.shutdown()