clockwork-cli 0.4.0__tar.gz → 0.4.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clockwork-cli
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: Mine your Claude Code and Codex session logs to estimate active time
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://github.com/danielsich/clockwork
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "clockwork-cli"
7
- version = "0.4.0"
7
+ version = "0.4.1"
8
8
  description = "Mine your Claude Code and Codex session logs to estimate active time"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -47,7 +47,7 @@ Examples:
47
47
 
48
48
  Data sources:
49
49
  claude → ~/.claude/projects/<encoded-path>/*.jsonl
50
- codex → $CODEX_HOME/sessions/**/rollout-*.jsonl (default ~/.codex/sessions)
50
+ codex → $CODEX_HOME/sessions/**/rollout-*.jsonl[.zst] (default ~/.codex/sessions)
51
51
 
52
52
  Token usage (--tokens):
53
53
  Both tools log per-response usage, counted differently, so clockwork
@@ -76,7 +76,7 @@ import hashlib
76
76
  from datetime import datetime, timedelta, timezone
77
77
  from collections import defaultdict
78
78
 
79
- __version__ = "0.4.0"
79
+ __version__ = "0.4.1"
80
80
 
81
81
 
82
82
  def _resolve_claude_dir():
@@ -626,13 +626,79 @@ def claude_list():
626
626
 
627
627
 
628
628
  # --------------------------------------------------------------------------- #
629
- # Codex backend ($CODEX_HOME/sessions/**/rollout-*.jsonl)
629
+ # Codex backend ($CODEX_HOME/sessions/**/rollout-*.jsonl[.zst])
630
630
  # --------------------------------------------------------------------------- #
631
631
 
632
632
  def _codex_files():
633
+ """Every rollout under $CODEX_HOME/sessions, plain or zstd-compressed.
634
+
635
+ Newer Codex versions save finished rollouts as `rollout-*.jsonl.zst`.
636
+ If a rollout exists in both forms, the newer file wins so it is counted
637
+ once.
638
+ """
633
639
  codex_home = os.environ.get("CODEX_HOME", os.path.expanduser("~/.codex"))
634
- pattern = os.path.join(codex_home, "sessions", "**", "rollout-*.jsonl")
635
- return glob.glob(pattern, recursive=True)
640
+ root = os.path.join(codex_home, "sessions", "**")
641
+ found = {}
642
+ for pattern in ("rollout-*.jsonl", "rollout-*.jsonl.zst"):
643
+ for f in glob.glob(os.path.join(root, pattern), recursive=True):
644
+ key = f[:-4] if f.endswith(".zst") else f
645
+ prev = found.get(key)
646
+ if prev is None or _mtime(f) > _mtime(prev):
647
+ found[key] = f
648
+ return sorted(found.values())
649
+
650
+
651
+ def _mtime(path):
652
+ try:
653
+ return os.path.getmtime(path)
654
+ except OSError:
655
+ return 0
656
+
657
+
658
+ def _zstd_decompress(data):
659
+ """Decompress zstd bytes with whatever is available.
660
+
661
+ Python 3.14 ships `compression.zstd`; otherwise try the `zstandard`
662
+ package, then the `zstd` command-line tool.
663
+ """
664
+ try:
665
+ from compression import zstd
666
+ return zstd.decompress(data)
667
+ except ImportError:
668
+ pass
669
+ try:
670
+ import zstandard
671
+ return zstandard.ZstdDecompressor().decompressobj().decompress(data)
672
+ except ImportError:
673
+ pass
674
+ import subprocess
675
+ try:
676
+ return subprocess.run(["zstd", "-dc"], input=data, stdout=subprocess.PIPE,
677
+ stderr=subprocess.DEVNULL, check=True).stdout
678
+ except (OSError, subprocess.CalledProcessError) as e:
679
+ raise OSError("cannot decompress zstd file (needs Python 3.14+, "
680
+ "the zstandard package, or the zstd tool)") from e
681
+
682
+
683
+ _zstd_warned = False
684
+
685
+
686
+ def _open_log(path):
687
+ """Open a JSONL log for text reading, decompressing `.zst` files."""
688
+ global _zstd_warned
689
+ if not path.endswith(".zst"):
690
+ return open(path, encoding="utf-8")
691
+ import io
692
+ with open(path, "rb") as fh:
693
+ raw = fh.read()
694
+ try:
695
+ data = _zstd_decompress(raw)
696
+ except OSError as e:
697
+ if not _zstd_warned:
698
+ _zstd_warned = True
699
+ print("clockwork: skipping .zst Codex logs: %s" % e, file=sys.stderr)
700
+ raise
701
+ return io.StringIO(data.decode("utf-8", errors="replace"))
636
702
 
637
703
 
638
704
  _CODEX_TOKEN_KEYS = ("input_tokens", "cached_input_tokens",
@@ -710,7 +776,7 @@ def _codex_scan_file(path):
710
776
  first_model = None
711
777
  running = {k: 0 for k in _CODEX_TOKEN_KEYS}
712
778
  try:
713
- with open(path, encoding="utf-8") as fh:
779
+ with _open_log(path) as fh:
714
780
  for line in fh:
715
781
  line = line.strip()
716
782
  if not line:
@@ -812,7 +878,7 @@ def _codex_cwd_only(path):
812
878
  the `session_meta` record carrying `cwd` is at the top of the rollout.
813
879
  """
814
880
  try:
815
- with open(path, encoding="utf-8") as fh:
881
+ with _open_log(path) as fh:
816
882
  for line in fh:
817
883
  line = line.strip()
818
884
  if not line:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clockwork-cli
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Summary: Mine your Claude Code and Codex session logs to estimate active time
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://github.com/danielsich/clockwork
@@ -17,8 +17,13 @@ from datetime import datetime, timezone
17
17
 
18
18
  import pytest
19
19
 
20
+ import os
21
+ import shutil
22
+ import subprocess
23
+
20
24
  from clockwork_cli import (
21
25
  _claude_scan_files,
26
+ _codex_files,
22
27
  _codex_scan_file,
23
28
  summarize_tokens,
24
29
  tokens_by_day,
@@ -226,6 +231,38 @@ def test_codex_returns_cwd_and_prompts(codex_log):
226
231
  assert len(timestamps) == 1
227
232
 
228
233
 
234
+ def _zstd_compress(src):
235
+ dst = str(src) + ".zst"
236
+ try:
237
+ from compression import zstd
238
+ with open(src, "rb") as fh, open(dst, "wb") as out:
239
+ out.write(zstd.compress(fh.read()))
240
+ except ImportError:
241
+ if not shutil.which("zstd"):
242
+ pytest.skip("no zstd compressor available")
243
+ subprocess.run(["zstd", "-q", "-f", str(src), "-o", dst], check=True)
244
+ return dst
245
+
246
+
247
+ def test_codex_reads_zstd_rollouts(codex_log):
248
+ # Newer Codex saves finished rollouts as rollout-*.jsonl.zst.
249
+ zst = _zstd_compress(codex_log)
250
+ assert _codex_scan_file(zst) == _codex_scan_file(codex_log)
251
+
252
+
253
+ def test_codex_files_finds_zst_and_counts_each_rollout_once(tmp_path, monkeypatch):
254
+ day = tmp_path / "sessions" / "2026" / "07" / "01"
255
+ day.mkdir(parents=True)
256
+ for name in ("rollout-a.jsonl", "rollout-b.jsonl.zst",
257
+ "rollout-c.jsonl", "rollout-c.jsonl.zst", "other.jsonl"):
258
+ (day / name).write_bytes(b"")
259
+ os.utime(day / "rollout-c.jsonl", (1, 1)) # the .zst is newer
260
+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))
261
+
262
+ names = sorted(os.path.basename(f) for f in _codex_files())
263
+ assert names == ["rollout-a.jsonl", "rollout-b.jsonl.zst", "rollout-c.jsonl.zst"]
264
+
265
+
229
266
  def codex_user_item(ts, text):
230
267
  return {"timestamp": ts, "type": "event_msg",
231
268
  "payload": {"type": "item_completed",
File without changes
File without changes
File without changes