Skip to content

Replay your coding agent sessions as a map and a timeline

fix(builder): cap maps by time and keep whole-session totals

rav4nn9fa06934c7e0
diff --git a/docs/format.md b/docs/format.md
index 789ebe8..62a365c 100644
--- a/docs/format.md
+++ b/docs/format.md
@@ -69,7 +69,7 @@ The reference producer ([`python/src/agentplay/builder.py`](../python/src/agentp
| `durationMs` | number | Time from the first event to the last |
| `models` | string[] | Every model that served a request |
| `pathMode` | `relative` \| `dirs_only` \| `hashed` | See the redaction contract |
-| `truncated` | bool | `true` when the producer stopped at its event cap (20,000 in the reference producer) |
+| `truncated` | bool | `true` when the producer stopped at its event cap (20,000 in the reference producer). `events` then holds the earliest events, while `calls`, `requests`, `errors`, `tokens` and `costUsd` in `totals` still count the whole session |
| `outcome` | string | See [Outcomes](#outcomes) |
| `role` | `root` \| `subagent` \| `helper` \| `unknown` \| null | `subagent` and `helper` sessions are usually hidden from lists; their work already appears inside their parent |
| `parentSessionId` | string \| null | Set when `role` is `subagent` |
diff --git a/python/src/agentplay/builder.py b/python/src/agentplay/builder.py
index 09ecd1a..62b3959 100644
--- a/python/src/agentplay/builder.py
+++ b/python/src/agentplay/builder.py
@@ -1646,9 +1646,6 @@ class MapBuilder:
def add(self, ev: dict[str, Any], ts: float | None) -> dict[str, Any]:
ev["_ts"] = ts
ev.setdefault("err", False)
- if len(self.events) >= MAX_EVENTS:
- self.truncated = True
- return ev
self.events.append(ev)
return ev
@@ -1950,22 +1947,18 @@ class MapBuilder:
e for e in self.events if e.get("_ts") is None
]
evs.sort(key=lambda e: e["_ts"] if e["_ts"] is not None else float("inf"))
- derived = self._derive(evs)
- room = max(MAX_EVENTS - len(evs), 0) # raw events are already capped in add()
- if len(derived) > room:
- derived = derived[:room]
- self.truncated = True
- evs.extend(derived)
+ evs.extend(self._derive(evs))
evs.sort(key=lambda e: e["_ts"] if e["_ts"] is not None else float("inf"))
switches = self._assign_models(evs)
- room = max(MAX_EVENTS - len(evs), 0)
- if len(switches) > room:
- switches = switches[:room]
- self.truncated = True
if switches:
evs.extend(switches)
evs.sort(key=lambda e: e["_ts"] if e["_ts"] is not None else float("inf"))
+ all_evs = evs
+ if len(evs) > MAX_EVENTS:
+ evs = evs[:MAX_EVENTS]
+ self.truncated = True
t0 = next((e["_ts"] for e in evs if e["_ts"] is not None), None)
+ t_end = next((e["_ts"] for e in reversed(all_evs) if e["_ts"] is not None), None)
last_t = 0
for i, e in enumerate(evs):
for private in ("_intents", "_cwd", "_prIntent"):
@@ -1989,9 +1982,9 @@ class MapBuilder:
e["ref"] = ref["seq"]
self._mark_retries(evs)
tasks = self._assign_tasks(evs, t0)
- # A PR-creating call past MAX_EVENTS never got a home (add() drops it without a
- # seq once truncated) — the PR still happened, but there's no event to link it
- # to, so it's dropped from the list rather than crashing the whole build.
+ # A PR-creating call past the MAX_EVENTS cut has no seq — the PR still happened,
+ # but there's no event to link it to, so it's dropped from the list rather than
+ # crashing the whole build.
kept: list[dict[str, Any]] = []
for pr in self.prs_created:
ev_ref = pr.pop("_ev")
@@ -2015,7 +2008,7 @@ class MapBuilder:
outcome=classify_outcome(mine, self.last_agent_text.get(a["id"], "")),
)
agents = [a for a in self.agents if "start" in a]
- llm = [e for e in evs if e["kind"] == "llm"]
+ llm = [e for e in all_evs if e["kind"] == "llm"]
prs = [dict(pr) for pr in self.prs_created]
created_numbers = [pr["number"] for pr in prs]
for pr in prs:
@@ -2029,11 +2022,11 @@ class MapBuilder:
elif created_numbers.count(pr["number"]) != 1:
continue # a repo-less outcome is only attributable when unambiguous
pr["state"] = "merged" if e["kind"] == "pr_merge" else "closed"
- users = [e for e in evs if e["kind"] == "user" and e["agent"] == "main"]
+ users = [e for e in all_evs if e["kind"] == "user" and e["agent"] == "main"]
def _n(kind: str, **match: Any) -> int:
return sum(
- 1 for e in evs if e["kind"] == kind and all(e.get(k) == v for k, v in match.items())
+ 1 for e in all_evs if e["kind"] == kind and all(e.get(k) == v for k, v in match.items())
)
doc = {
@@ -2041,25 +2034,25 @@ class MapBuilder:
"session": {
**session,
"startedAt": _iso(t0) if t0 else session.get("startedAt"),
- "durationMs": evs[-1]["t"] if evs else 0,
+ "durationMs": int((t_end - t0) * 1000) if t0 is not None and t_end is not None else 0,
"models": sorted({e["model"] for e in llm if e.get("model")}),
"pathMode": self.path_mode,
"truncated": self.truncated,
"outcome": classify_outcome(evs, self.last_agent_text.get("main", "")),
},
"totals": {
- "calls": sum(1 for e in evs if e["kind"] not in _NON_CALL_KINDS),
+ "calls": sum(1 for e in all_evs if e["kind"] not in _NON_CALL_KINDS),
"requests": len(llm),
- "userTurns": sum(1 for e in evs if e["kind"] == "user"),
+ "userTurns": sum(1 for e in all_evs if e["kind"] == "user"),
"subagents": len(agents) - 1 if agents else 0,
- "errors": sum(1 for e in evs if e.get("err")),
+ "errors": sum(1 for e in all_evs if e.get("err")),
"tokens": sum(e["in"] + e["cw"] + e["cr"] + e["out"] for e in llm),
"costUsd": round(sum(e.get("cost", 0.0) for e in llm), 4),
"filesTouched": len(self.files),
"prsCreated": len(prs),
"filesCreated": sum(1 for f in self.files.values() if f.get("op") == "create"),
"filesDeleted": sum(1 for f in self.files.values() if f.get("op") == "delete"),
- **_signal_totals(evs),
+ **_signal_totals(all_evs),
"tasks": len(tasks),
"commits": _n("commit"),
"prsMerged": _n("pr_merge"),
diff --git a/python/tests/test_builder.py b/python/tests/test_builder.py
index 3090604..36a87c5 100644
--- a/python/tests/test_builder.py
+++ b/python/tests/test_builder.py
@@ -620,6 +620,71 @@ def test_derived_delays_respect_max_events(monkeypatch):
assert all(e["kind"] != "delay" or isinstance(e.get("ref"), int) for e in doc["events"])
+def test_event_cap_keeps_the_first_events_and_whole_session_totals(tmp_path):
+ from datetime import UTC, datetime, timedelta
+
+ t0 = datetime(2026, 9, 10, 1, 0, tzinfo=UTC)
+ usage = {
+ "input_tokens": 10,
+ "cache_creation_input_tokens": 100,
+ "cache_read_input_tokens": 1000,
+ "output_tokens": 50,
+ }
+ requests, calls_each = 210, 100
+ lines = []
+ for m in range(requests):
+ ids = [f"t{m}-{i}" for i in range(calls_each)]
+ last = m == requests - 1
+ lines.append(
+ _line(
+ type="assistant",
+ timestamp=(t0 + timedelta(seconds=2 * m)).isoformat(),
+ message={
+ "id": f"m{m}",
+ "model": "claude-sonnet-5",
+ "usage": usage,
+ "content": [
+ {"type": "tool_use", "id": i, "name": "Bash", "input": {"command": "true"}}
+ for i in ids
+ ],
+ },
+ )
+ )
+ lines.append(
+ _line(
+ type="user",
+ timestamp=(t0 + timedelta(seconds=2 * m + 1)).isoformat(),
+ message={
+ "role": "user",
+ "content": [
+ {"type": "tool_result", "tool_use_id": i, "content": "x", "is_error": last}
+ for i in ids
+ ],
+ },
+ )
+ )
+ path = tmp_path / "big.jsonl"
+ path.write_text("".join(lines))
+
+ doc = sm.build_claude_code_map(path)
+
+ one = sm.estimate_cost_usd("claude-sonnet-5", 10, 100, 1000, 50)
+ t = doc["totals"]
+ assert t["calls"] == requests * calls_each > sm.MAX_EVENTS
+ assert t["requests"] == requests
+ assert t["errors"] == calls_each
+ assert t["tokens"] == requests * 1160
+ assert t["outputTokens"] == requests * 50
+ assert t["costUsd"] == pytest.approx(requests * one, abs=1e-3) and t["costUsd"] > 0
+ assert doc["session"]["models"] == ["claude-sonnet-5"]
+ assert doc["session"]["durationMs"] == 2 * (requests - 1) * 1000
+ assert doc["session"]["truncated"] is True
+ evs = doc["events"]
+ assert len(evs) == sm.MAX_EVENTS
+ assert {e["kind"] for e in evs} >= {"llm", "exec"}
+ assert evs[-1]["t"] < doc["session"]["durationMs"]
+
+
def _span(span_id, name, start, end=None, parent=None, **attrs):
return SimpleNamespace(
span_id=span_id,