Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion agentrace/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class AgentRun:
@property
def duration_s(self) -> float | None:
if self.started_at and self.ended_at:
return (self.ended_at - self.started_at).total_seconds()
duration = (self.ended_at - self.started_at).total_seconds()
return duration if duration >= 0 else None
return None

@property
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ def test_stats_median(durations, expected, monkeypatch, capsys):
assert " ".join(median_rows[0].split()) == f"median run {expected}"


def test_stats_excludes_clock_skewed_duration(monkeypatch, capsys):
start = datetime(2026, 1, 1, tzinfo=UTC)
runs = [
AgentRun(
tool_use_id=str(i),
description="test",
prompt="prompt",
result="result",
started_at=start,
ended_at=start + timedelta(seconds=duration),
)
for i, duration in enumerate([-5, 10, 20])
]
monkeypatch.setattr(cli, "_load", lambda args: runs)
assert cli.cmd_stats(Namespace(json=True)) == 0
output = capsys.readouterr().out
assert '"total_seconds": 30.0' in output
assert "median run 15 s" in " ".join(output.split())
assert runs[0].duration_s is None


def test_file_and_dir_are_mutually_exclusive(tmp_path):
transcript = tmp_path / "session.jsonl"
transcript.write_text("")
Expand Down
Loading