cctally 1.18.0 β†’ 1.20.0

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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
5
 
6
6
  ## [Unreleased]
7
7
 
8
+ ## [1.20.0] - 2026-05-28
9
+
10
+ ### Added
11
+ - **`cctally statusline` (and `cctally claude statusline`) β€” a one-line status string for Claude Code's `statusLine` hook, drop-in for `ccusage statusline` with cctally-only extensions appended.** Reads the Claude Code hook stdin and emits five `|`-delimited segments: `πŸ€– <model>` (display name) Β· `πŸ’° $X.XX session / $Y.YY today / $Z.ZZ block (Hh Mm left)` cost Β· `πŸ”₯ $X.XX/hr` burn rate with optional `🟒/🟑/πŸ”΄ (Normal/Moderate/High)` visual Β· `🧠 N%` context-window utilization Β· `5h X% (Hh Mm) Β· 7d Y% (Dd Hh)` cctally extension sourced from stdin `rate_limits` with the existing monotonic HWM clamp and DB-latest-row fallback. Flag surface mirrors ccusage's: `-B {off,emoji,text,emoji-text}`, `--cost-source {auto,cctally,cc,both}` (the `ccusage` value name is renamed; passing it errors with a one-line rename hint), `--context-low-threshold`/`--context-medium-threshold` (defaults 50/80, with `[0, 100]` range + `low < medium` ordering validation), `-z/--timezone`, `-d/--debug`, plus `--cache`/`--no-cache`/`--refresh-interval`/`-O/--offline`/`--single-thread` as documented no-op aliases and `--config PATH` honored as a real per-invocation override (parity with the 10 sibling Claude reporting commands). cctally adds `--cctally-extensions`/`--no-cctally-extensions` (default-on) to toggle segment 5 and persists three config keys (`statusline.{visual_burn_rate,cost_source,cctally_extensions}`) with CLI > config > built-in default precedence. Cost sources: `auto` uses the cctally JSONL-derived cost when the transcript is readable and the session exists in the cache, falling back to stdin `cost.total_cost_usd` otherwise; `cctally`/`cc`/`both` force one path or render side-by-side. Today's bucket honors `display.tz`; the active block segment reuses Session F's `_lib_blocks` kernel and burn-rate bands are `<$15/hr` green, `<$30/hr` yellow, `β‰₯$30/hr` red. Context % is computed from a memory-safe tail-walk of the transcript JSONL (last assistant turn's usage block Γ· per-model context window β€” 200K default, 1M for `[1m]` variants); unknown models render `🧠 N/A` with a one-shot stderr warn. Stdin contract is deliberately graceful (an intentional ccusage divergence, documented in the spec): only malformed JSON or a non-object root exits 1; every other field absence β€” missing `model`, `transcript_path`, `session_id`, `cost`, even `rate_limits` β€” produces a degraded but non-broken line at exit 0, so the status line never fails the hook on a partial payload. Architecture: pure-function render kernel at `bin/_lib_statusline.py` (no I/O β€” every side-effecting dep dataclass-injected); I/O glue in `bin/cctally::cmd_statusline` wires DB + transcript reader + HWM clamp callables; built once and registered twice (flat + nested) per the Session B parser pattern, so `cctally statusline` and `cctally claude statusline` produce byte-identical output (only `--help` xref differs). Regression: `bin/cctally-statusline-test` (32 fixture scenarios β€” every cost-source value, every `-B` variant, every context color band + the unknown-model + 1M-window paths, extension HWM clamp + DB fallback + suppression, resumed-session merge, every graceful-degradation case, bad-stdin, tz buckets, config persistence + CLI override + `--config PATH`), `bin/cctally-reconcile-test` (+6 statusline invariants binding the segments to `cctally session`/`daily`/`blocks --active`/`weekly_usage_snapshots` within 1e-9 USD tolerance), `bin/cctally-subgroup-test` (new `compare_forms_stdin` proves flat≑`claude statusline` over a subgroup-parity fixture), and `tests/test_statusline.py` (66 kernel units). (#86)
12
+
13
+ ## [1.19.0] - 2026-05-28
14
+
15
+ ### Added
16
+ - **`cctally blocks` (and `cctally claude blocks`) gain the ccusage drop-in flags `-a/--active`, `-r/--recent`, `-t/--token-limit N|max`, and `-n/--session-length N`.** `-a` filters to the single live block and renders it as a "Current Session Block Status" detail box β€” Block Started (+ "Xh Ym ago" / approximate-start `~` cue when the window has no recorded Anthropic reset), Time Remaining, Current Usage, Burn Rate and Projected Usage (when available), and β€” only when `-t` is passed explicitly β€” a Token Limit Status block whose `OK`/`WARNING`/`EXCEEDS LIMIT` color tracks the projected percent; with no active block it prints `No active session block found.` to stdout (JSON: `{"blocks": [], "message": "No active block"}`) and exits 0. `-r` keeps only blocks from the last 3 days plus the active block. `-t N` keys the table's `%`/REMAINING/PROJECTED surface (and the `-a` box's Token Limit Status) to an explicit limit even with no completed history, while `-t max` (the default) derives it from the largest completed block and prints `Using max tokens from previous sessions: N` to stdout (suppressed under `--json`); `--json` additionally gains an additive `tokenLimitStatus` key on active blocks under an explicit positive `-t`. `-n` is accepted for drop-in compatibility but is a no-op β€” cctally blocks follow Anthropic's real 5-hour resets and are not re-sizable β€” except `-n <= 0`, which errors (exit 1). cctally's block projection keeps its real-reset formula (a documented third intentional divergence from upstream's entry-span model). (#86)
17
+
8
18
  ## [1.18.0] - 2026-05-27
9
19
 
10
20
  ### Added
@@ -11,8 +11,8 @@ pipeline.
11
11
  Holds:
12
12
  - ``ProjectKey`` (frozen dataclass) + ``_resolve_project_key`` β€”
13
13
  canonical project bucket identity for the ``project`` subcommand.
14
- - ``_get_codex_sessions_dir`` / ``_discover_codex_session_files`` β€”
15
- Codex JSONL discovery primitives.
14
+ - ``_discover_codex_session_files`` / ``_iter_codex_jsonl_paths`` β€”
15
+ Codex JSONL discovery primitives (multi-root $CODEX_HOME walk).
16
16
  - ``IngestStats`` / ``CodexIngestStats`` (dataclasses), ``_progress_stderr``
17
17
  / ``_progress_codex_stderr`` β€” ingest progress + per-call telemetry.
18
18
  - ``_ensure_session_files_row`` β€” idempotent backfill of
@@ -82,7 +82,7 @@ in the sibling graph):
82
82
  ``get_entries``, ``get_claude_session_entries``, ``get_codex_entries``,
83
83
  ``_resolve_project_key``, ``ProjectKey``, ``IngestStats``,
84
84
  ``CodexIngestStats``, ``_JoinedClaudeEntry``, ``_ensure_session_files_row``,
85
- ``_discover_codex_session_files``, ``_get_codex_sessions_dir``,
85
+ ``_discover_codex_session_files``,
86
86
  ``cmd_cache_sync``, ``_progress_stderr``, ``_progress_codex_stderr``,
87
87
  ``_collect_entries_direct``, ``_collect_codex_entries_direct``,
88
88
  ``_direct_parse_claude_session_entries``, ``iter_codex_entries``)
@@ -107,7 +107,7 @@ import pathlib
107
107
  import sqlite3
108
108
  import sys
109
109
  from dataclasses import dataclass, field
110
- from typing import Any, Callable
110
+ from typing import Any, Callable, Iterator
111
111
 
112
112
 
113
113
  def _cctally():
@@ -265,27 +265,54 @@ def _resolve_project_key(
265
265
  # === Region 2: Codex sessions-dir helpers (was bin/cctally:2072-2099) ===
266
266
 
267
267
 
268
- def _get_codex_sessions_dir() -> pathlib.Path | None:
269
- """Return the Codex sessions directory if present, else None."""
270
- c = _cctally()
271
- if c.CODEX_SESSIONS_DIR.is_dir():
272
- return c.CODEX_SESSIONS_DIR
273
- return None
268
+ def _iter_codex_jsonl_paths(roots: list[pathlib.Path]) -> Iterator[pathlib.Path]:
269
+ """Yield each existing *.jsonl under the given roots, de-duped by RESOLVED
270
+ path (first occurrence wins β€” collapses overlapping/prefix roots and
271
+ symlink/`..` aliases of the same physical file).
272
+
273
+ Pure read: globs + is_file() only, no DB access. Shared by both Codex
274
+ walkers (_discover_codex_session_files and sync_codex_cache) so they stay
275
+ in lock-step on dedup + is_file() ordering.
276
+ """
277
+ seen: set[pathlib.Path] = set()
278
+ for root in roots:
279
+ for jp in root.glob("**/*.jsonl"):
280
+ # Dedup on the RESOLVED path, not the raw spelling. A symlinked
281
+ # $CODEX_HOME root or an alias entry (`.../.codex`,
282
+ # `.../sub/../.codex`) can glob the same physical file under
283
+ # different spellings; UNIQUE(source_path, line_offset) keys on the
284
+ # string, so distinct spellings would double-ingest (2-3x tokens /
285
+ # cost) on a fresh walk. resolve() collapses the aliases (issue
286
+ # #108). First spelling still wins for the yielded source_path.
287
+ try:
288
+ key = jp.resolve()
289
+ except OSError:
290
+ key = jp # unresolvable (broken symlink, perms) β€” key on raw
291
+ if key in seen:
292
+ continue
293
+ seen.add(key)
294
+ if jp.is_file():
295
+ yield jp
274
296
 
275
297
 
276
298
  def _discover_codex_session_files(
277
299
  range_start: dt.datetime,
278
300
  ) -> list[pathlib.Path]:
279
- """Glob ~/.codex/sessions/**/*.jsonl, filtering by mtime >= range_start."""
280
- root = _get_codex_sessions_dir()
281
- if root is None:
282
- eprint("[codex] no ~/.codex/sessions directory found")
301
+ """Glob each $CODEX_HOME session root's **/*.jsonl, mtime >= range_start.
302
+
303
+ Iterates _cctally()._codex_session_roots() (multi-root). The "none found"
304
+ notice fires ONLY when there are zero session-root directories at all (the
305
+ multi-root analogue of the old single-dir-missing check) β€” NOT when roots
306
+ exist but the mtime filter leaves the set empty (that stays silent, as
307
+ today, so narrow-range queries gain no new stderr).
308
+ """
309
+ roots = _cctally()._codex_session_roots()
310
+ if not roots:
311
+ eprint("[codex] no Codex session directory found")
283
312
  return []
284
313
  start_ts = range_start.timestamp()
285
314
  result: list[pathlib.Path] = []
286
- for jp in root.glob("**/*.jsonl"):
287
- if not jp.is_file():
288
- continue
315
+ for jp in _iter_codex_jsonl_paths(roots):
289
316
  try:
290
317
  mtime = jp.stat().st_mtime
291
318
  except OSError:
@@ -1190,6 +1217,9 @@ class CodexIngestStats:
1190
1217
  # ``IngestStats`` (Claude path) which carries an UPSERT and
1191
1218
  # therefore counts both new INSERTs and DO UPDATE replacements.
1192
1219
  rows_changed: int = 0
1220
+ # Count of cached files dropped because they fall outside the CURRENT
1221
+ # $CODEX_HOME root set (issue #108 β€” a prior-root purge, not a delta).
1222
+ files_pruned: int = 0
1193
1223
  lock_contended: bool = False
1194
1224
 
1195
1225
 
@@ -1243,14 +1273,52 @@ def sync_codex_cache(
1243
1273
  conn.commit()
1244
1274
  eprint("[cache-sync] rebuild: cleared Codex cached entries")
1245
1275
 
1246
- root = _get_codex_sessions_dir()
1247
- paths: list[pathlib.Path] = []
1248
- if root is not None:
1249
- for jp in root.glob("**/*.jsonl"):
1250
- if jp.is_file():
1251
- paths.append(jp)
1276
+ roots = _cctally()._codex_session_roots()
1277
+ # Pure read (glob + is_file only); safe to run before the SELECT and
1278
+ # the per-file loop, where no cache.db write lock may be held.
1279
+ paths: list[pathlib.Path] = list(_iter_codex_jsonl_paths(roots))
1252
1280
  stats.files_total = len(paths)
1253
1281
 
1282
+ # Scope the cache to the CURRENT root set: drop rows ingested under a
1283
+ # prior $CODEX_HOME (issue #108). iter_codex_entries() has NO root
1284
+ # predicate β€” it reads every row in range β€” so without this, reusing
1285
+ # the same cache.db across `CODEX_HOME=/A` then `CODEX_HOME=/B` runs
1286
+ # returns A+B instead of just B. Prune every real (absolute) row
1287
+ # outside the current set, even when that set is empty (an empty
1288
+ # current root then prunes the cache to empty): the cache is fully
1289
+ # re-derivable, so honoring the override beats retaining unreachable
1290
+ # rows. Done INSIDE the lock and committed BEFORE the existing-SELECT
1291
+ # + parse loop so no cache.db write lock is held across the read-heavy
1292
+ # ingest (same invariant as the --rebuild clear above). Concurrent
1293
+ # processes with different $CODEX_HOME would prune each other; the
1294
+ # flock serializes them and that is a pathological configuration.
1295
+ if not rebuild: # --rebuild already cleared both tables above
1296
+ current_paths = {str(p) for p in paths}
1297
+ # Only prune ABSOLUTE source_paths. _codex_home_roots() makes
1298
+ # every real root absolute (via .absolute()), so a real ingested
1299
+ # row always stores an absolute str(jp) β€” INCLUDING a relative
1300
+ # $CODEX_HOME like `./codexA`, which is canonicalized before the
1301
+ # glob. A relative path here is therefore β€” by construction β€” a
1302
+ # synthetic baked-cache fixture row (e.g. build-speed-fixtures.py)
1303
+ # with no on-disk JSONL to scope against; pruning it would wipe a
1304
+ # cache meant to be read as-is (issue #108).
1305
+ orphan_paths = [
1306
+ row[0]
1307
+ for row in conn.execute("SELECT path FROM codex_session_files")
1308
+ if row[0] not in current_paths and os.path.isabs(row[0])
1309
+ ]
1310
+ if orphan_paths:
1311
+ conn.executemany(
1312
+ "DELETE FROM codex_session_entries WHERE source_path = ?",
1313
+ [(p,) for p in orphan_paths],
1314
+ )
1315
+ conn.executemany(
1316
+ "DELETE FROM codex_session_files WHERE path = ?",
1317
+ [(p,) for p in orphan_paths],
1318
+ )
1319
+ conn.commit()
1320
+ stats.files_pruned = len(orphan_paths)
1321
+
1254
1322
  # This SELECT does NOT open an implicit transaction (Python's
1255
1323
  # sqlite3 module only BEGINs on DML). Do NOT add any INSERT/
1256
1324
  # UPDATE/DELETE/REPLACE statement between here and the per-file
@@ -298,9 +298,71 @@ ALLOWED_CONFIG_KEYS = (
298
298
  "dashboard.bind",
299
299
  "update.check.enabled",
300
300
  "update.check.ttl_hours",
301
+ "statusline.visual_burn_rate",
302
+ "statusline.cost_source",
303
+ "statusline.cctally_extensions",
301
304
  )
302
305
 
303
306
 
307
+ # === statusline config validators (issue #86 Session G) ===================
308
+
309
+ _STATUSLINE_VBR_VALUES = ("off", "emoji", "text", "emoji-text")
310
+ _STATUSLINE_COST_SOURCE_VALUES = ("auto", "cctally", "cc", "both")
311
+
312
+
313
+ def _validate_statusline_visual_burn_rate(value):
314
+ """Validate ``statusline.visual_burn_rate``.
315
+
316
+ Accepts any of ``off`` / ``emoji`` / ``text`` / ``emoji-text``. Other
317
+ strings raise ``ValueError`` with a hint listing the valid values.
318
+ """
319
+ if isinstance(value, str) and value in _STATUSLINE_VBR_VALUES:
320
+ return value
321
+ raise ValueError(
322
+ f"statusline.visual_burn_rate must be one of "
323
+ f"{', '.join(_STATUSLINE_VBR_VALUES)} (got {value!r})"
324
+ )
325
+
326
+
327
+ def _validate_statusline_cost_source(value):
328
+ """Validate ``statusline.cost_source``.
329
+
330
+ Accepts ``auto`` / ``cctally`` / ``cc`` / ``both``. The ``ccusage``
331
+ value name is rejected at config set time too β€” the rename hint
332
+ is surfaced both here AND at flag-parse time by the argparse choice
333
+ rejection inside ``cmd_statusline``.
334
+ """
335
+ if isinstance(value, str) and value in _STATUSLINE_COST_SOURCE_VALUES:
336
+ return value
337
+ if value == "ccusage":
338
+ raise ValueError(
339
+ "statusline.cost_source 'ccusage' was renamed; use 'cctally'"
340
+ )
341
+ raise ValueError(
342
+ f"statusline.cost_source must be one of "
343
+ f"{', '.join(_STATUSLINE_COST_SOURCE_VALUES)} (got {value!r})"
344
+ )
345
+
346
+
347
+ def _validate_statusline_cctally_extensions(value):
348
+ """Validate ``statusline.cctally_extensions``.
349
+
350
+ Accepts booleans (preferred) or canonical truthy/falsy strings
351
+ (``true``/``false``/``yes``/``no``/``on``/``off``/``1``/``0``).
352
+ """
353
+ if isinstance(value, bool):
354
+ return value
355
+ if isinstance(value, str):
356
+ lo = value.strip().lower()
357
+ if lo in ("true", "yes", "on", "1"):
358
+ return True
359
+ if lo in ("false", "no", "off", "0"):
360
+ return False
361
+ raise ValueError(
362
+ f"statusline.cctally_extensions must be boolean (got {value!r})"
363
+ )
364
+
365
+
304
366
  def cmd_config(args: argparse.Namespace) -> int:
305
367
  """Get/set/unset persisted user preferences in config.json.
306
368
 
@@ -374,6 +436,33 @@ def _config_known_value(config: dict, key: str) -> "object":
374
436
  return c._validate_update_check_ttl_hours_value(stored)
375
437
  except ValueError:
376
438
  return c.UPDATE_DEFAULT_TTL_HOURS
439
+ if key in (
440
+ "statusline.visual_burn_rate",
441
+ "statusline.cost_source",
442
+ "statusline.cctally_extensions",
443
+ ):
444
+ sl_block = config.get("statusline") if isinstance(config, dict) else None
445
+ if not isinstance(sl_block, dict):
446
+ sl_block = {}
447
+ inner = key.split(".", 1)[1]
448
+ stored = sl_block.get(inner)
449
+ defaults = {
450
+ "visual_burn_rate": "off",
451
+ "cost_source": "auto",
452
+ "cctally_extensions": True,
453
+ }
454
+ if stored is None:
455
+ return defaults[inner]
456
+ validator = {
457
+ "visual_burn_rate": _validate_statusline_visual_burn_rate,
458
+ "cost_source": _validate_statusline_cost_source,
459
+ "cctally_extensions": _validate_statusline_cctally_extensions,
460
+ }[inner]
461
+ try:
462
+ return validator(stored)
463
+ except ValueError:
464
+ # Hand-edited junk: surface the default β€” mirrors dashboard.bind.
465
+ return defaults[inner]
377
466
  return None
378
467
 
379
468
 
@@ -509,6 +598,44 @@ def _cmd_config_set(args: argparse.Namespace) -> int:
509
598
  else:
510
599
  print(f"dashboard.bind={canonical}")
511
600
  return 0
601
+ if key in (
602
+ "statusline.visual_burn_rate",
603
+ "statusline.cost_source",
604
+ "statusline.cctally_extensions",
605
+ ):
606
+ inner_key = key.split(".", 1)[1]
607
+ validator = {
608
+ "visual_burn_rate": _validate_statusline_visual_burn_rate,
609
+ "cost_source": _validate_statusline_cost_source,
610
+ "cctally_extensions": _validate_statusline_cctally_extensions,
611
+ }[inner_key]
612
+ try:
613
+ normalized = validator(raw)
614
+ except ValueError as exc:
615
+ print(f"cctally: {exc}", file=sys.stderr)
616
+ return 2
617
+ with config_writer_lock():
618
+ config = _load_config_unlocked()
619
+ existing = config.get("statusline")
620
+ if existing is not None and not isinstance(existing, dict):
621
+ print(
622
+ "cctally: statusline config error: statusline must be an object",
623
+ file=sys.stderr,
624
+ )
625
+ return 2
626
+ block = dict(existing or {})
627
+ block[inner_key] = normalized
628
+ config["statusline"] = block
629
+ save_config(config)
630
+ if getattr(args, "emit_json", False):
631
+ print(json.dumps({"statusline": {inner_key: normalized}}, indent=2))
632
+ else:
633
+ if isinstance(normalized, bool):
634
+ rendered = "true" if normalized else "false"
635
+ else:
636
+ rendered = str(normalized)
637
+ print(f"{key}={rendered}")
638
+ return 0
512
639
  if key in ("update.check.enabled", "update.check.ttl_hours"):
513
640
  # Validate first; rejection short-circuits before lock acquisition.
514
641
  if key == "update.check.enabled":
@@ -607,6 +734,22 @@ def _cmd_config_unset(args: argparse.Namespace) -> int:
607
734
  save_config(config)
608
735
  # idempotent: silent on missing key
609
736
  return 0
737
+ if key in (
738
+ "statusline.visual_burn_rate",
739
+ "statusline.cost_source",
740
+ "statusline.cctally_extensions",
741
+ ):
742
+ inner_key = key.split(".", 1)[1]
743
+ with config_writer_lock():
744
+ config = _load_config_unlocked()
745
+ block = config.get("statusline")
746
+ if isinstance(block, dict) and inner_key in block:
747
+ del block[inner_key]
748
+ if not block:
749
+ config.pop("statusline", None)
750
+ save_config(config)
751
+ # idempotent: silent on missing key
752
+ return 0
610
753
  if key in ("update.check.enabled", "update.check.ttl_hours"):
611
754
  # Mirror the dashboard.bind branch: drop the leaf, then prune
612
755
  # empty `check` and empty `update` so config.json stays tidy.
@@ -375,6 +375,14 @@ class CodexSessionUsage:
375
375
  models: list[str]
376
376
  model_breakdowns: list[dict[str, Any]]
377
377
  last_activity: dt.datetime
378
+ # Issue #110: the matched $CODEX_HOME root in home-root form
379
+ # (e.g. "<root>/.codex", or "<root>" for a direct-JSONL root). Used ONLY
380
+ # to disambiguate the displayed / JSON label when two cross-root sessions
381
+ # share the same relative `session_id_path`. "" for the bare-relative
382
+ # fixture form (which cannot collide cross-root). Single-root data leaves
383
+ # every row's `codex_root` constant, so the renderers' collision check
384
+ # never fires and output stays byte-identical.
385
+ codex_root: str = ""
378
386
 
379
387
 
380
388
  @dataclass
@@ -531,24 +539,30 @@ def _aggregate_codex_weekly(
531
539
  def _session_path_parts(source_path: str) -> tuple[str, str, str]:
532
540
  """Return (session_id_path, session_file, directory) from a full path.
533
541
 
534
- session_id_path = relative path under CODEX_SESSIONS_DIR with .jsonl
535
- stripped (e.g. "2025/12/25/rollout-...").
542
+ session_id_path = relative path under the matched $CODEX_HOME session
543
+ root with .jsonl stripped (e.g. "2025/12/25/rollout-...").
536
544
  session_file = basename without .jsonl extension.
537
- directory = relative parent path under CODEX_SESSIONS_DIR.
538
-
539
- Accepts three input shapes:
540
- 1. Absolute path under CODEX_SESSIONS_DIR (the runtime sync path).
541
- 2. Bare-relative path starting with ".codex/sessions/..." β€” the form
542
- emitted by build-codex-fixtures.py so committed fixture cache.db
543
- files stay free of maintainer absolute paths (public-mirror safe).
544
- 3. Anything else β€” falls back to basename-only.
545
+ directory = relative parent path under the matched root.
546
+
547
+ Tries each root in _codex_session_roots() order (the same list/order the
548
+ discovery walkers use, so overlapping/prefix roots resolve to the FIRST
549
+ matching root deterministically); first relative_to() that succeeds wins.
550
+ Falls back to the bare-relative ".codex/sessions/<rest>" fixture form (the
551
+ shape emitted by build-codex-fixtures.py so committed fixture cache.db
552
+ files stay free of maintainer absolute paths), then basename. Direct-JSONL
553
+ roots yield an id relative to <entry> itself (no sessions/ prefix).
545
554
  """
546
- CODEX_SESSIONS_DIR = _cctally().CODEX_SESSIONS_DIR
555
+ roots = _cctally()._codex_session_roots()
547
556
  p = pathlib.Path(source_path)
548
- try:
549
- rel = p.relative_to(CODEX_SESSIONS_DIR)
550
- except ValueError:
551
- # Try bare-relative ".codex/sessions/<rest>" before basename fallback.
557
+ rel: pathlib.PurePath | None = None
558
+ for root in roots:
559
+ try:
560
+ rel = p.relative_to(root)
561
+ break
562
+ except ValueError:
563
+ continue
564
+ if rel is None:
565
+ # Bare-relative ".codex/sessions/<rest>" (fixture form), else basename.
552
566
  # Use PurePosixPath to avoid Windows-style drive parsing on unusual
553
567
  # inputs; fixture-emitted paths are always POSIX.
554
568
  parts = pathlib.PurePosixPath(source_path).parts
@@ -560,6 +574,22 @@ def _session_path_parts(source_path: str) -> tuple[str, str, str]:
560
574
  return str(stem), stem.name, str(stem.parent)
561
575
 
562
576
 
577
+ def _codex_home_root_from_prefix(root_prefix: str) -> str:
578
+ """Normalize the aggregator's `root_prefix` to the matched $CODEX_HOME entry.
579
+
580
+ `root_prefix` is `source_path` with the relative `id_path` tail removed, so a
581
+ Codex-home match looks like "<root>/.codex/sessions/" and a direct-JSONL
582
+ match like "<root>/". Strip the trailing slash and any "/sessions" tail to
583
+ recover the home root the user configured β€” the unit the issue #110
584
+ disambiguator labels by. The bare-relative fixture prefix ".codex/sessions/"
585
+ normalizes to ".codex" (constant across fixtures, so it never collides).
586
+ """
587
+ s = root_prefix.rstrip("/")
588
+ if s.endswith("/sessions"):
589
+ s = s[: -len("/sessions")]
590
+ return s
591
+
592
+
563
593
  def _aggregate_codex_sessions(entries: list[CodexEntry], speed: str = "standard") -> list[CodexSessionUsage]:
564
594
  """Group by session file path (upstream-compatible).
565
595
 
@@ -570,13 +600,31 @@ def _aggregate_codex_sessions(entries: list[CodexEntry], speed: str = "standard"
570
600
  Per-model breakdowns include `isFallback: bool` β€” true when the model is
571
601
  absent from CODEX_MODEL_PRICING.
572
602
  """
573
- by_session: dict[str, dict[str, Any]] = {}
603
+ by_session: dict[tuple[str, str], dict[str, Any]] = {}
574
604
  for entry in entries:
575
605
  id_path, file_name, directory = _session_path_parts(entry.source_path)
576
- sess = by_session.setdefault(id_path, {
606
+ # Disambiguate identical relative paths under DIFFERENT $CODEX_HOME
607
+ # roots (issue #108). _session_path_parts strips the matched root, so
608
+ # <rootA>/sessions/2026/04/17/rollout-x.jsonl and the same relative
609
+ # path under <rootB> both yield id_path "2026/04/17/rollout-x";
610
+ # grouping on id_path alone would silently merge two distinct sessions
611
+ # (summed tokens, one UUID). Key on (root_prefix, id_path), where
612
+ # root_prefix is source_path with the id_path tail removed. Single-root
613
+ # data β€” and the bare-relative fixture form β€” has a constant prefix, so
614
+ # the grouping, insertion order, and every golden stay byte-identical;
615
+ # only a genuine cross-root collision splits into separate rows.
616
+ suffix = id_path + ".jsonl"
617
+ sp = entry.source_path
618
+ root_prefix = sp[: -len(suffix)] if sp.endswith(suffix) else sp
619
+ sess = by_session.setdefault((root_prefix, id_path), {
577
620
  "session_id_uuid": entry.session_id,
621
+ "session_id_path": id_path,
578
622
  "session_file": file_name,
579
623
  "directory": directory,
624
+ # Matched $CODEX_HOME root (home-root form) β€” issue #110 display
625
+ # disambiguator. Derived from the same root_prefix that keys the
626
+ # group, so it's constant per group.
627
+ "codex_root": _codex_home_root_from_prefix(root_prefix),
580
628
  "input": 0, "cached_input": 0, "output": 0, "reasoning": 0,
581
629
  "cost": 0.0, "models": {}, "models_order": [],
582
630
  "last": entry.timestamp,
@@ -606,7 +654,7 @@ def _aggregate_codex_sessions(entries: list[CodexEntry], speed: str = "standard"
606
654
  sess["last"] = entry.timestamp
607
655
 
608
656
  result: list[CodexSessionUsage] = []
609
- for id_path, s in by_session.items():
657
+ for _group_key, s in by_session.items():
610
658
  model_breakdowns = [
611
659
  {
612
660
  "modelName": model,
@@ -623,7 +671,7 @@ def _aggregate_codex_sessions(entries: list[CodexEntry], speed: str = "standard"
623
671
  model_breakdowns.sort(key=lambda m: m["cost"], reverse=True)
624
672
  result.append(CodexSessionUsage(
625
673
  session_id=s["session_id_uuid"],
626
- session_id_path=id_path,
674
+ session_id_path=s["session_id_path"],
627
675
  session_file=s["session_file"],
628
676
  directory=s["directory"],
629
677
  input_tokens=s["input"],
@@ -641,6 +689,7 @@ def _aggregate_codex_sessions(entries: list[CodexEntry], speed: str = "standard"
641
689
  models=list(s["models_order"]),
642
690
  model_breakdowns=model_breakdowns,
643
691
  last_activity=s["last"],
692
+ codex_root=s["codex_root"],
644
693
  ))
645
694
  result.sort(key=lambda x: x.last_activity, reverse=True)
646
695
  return result
@@ -36,6 +36,7 @@ import bisect
36
36
  import datetime as dt
37
37
  import json
38
38
  import pathlib
39
+ import re
39
40
  import sys
40
41
  from dataclasses import dataclass
41
42
  from typing import Any
@@ -432,7 +433,42 @@ def _build_activity_block(
432
433
  )
433
434
 
434
435
 
435
- def _blocks_to_json(blocks: list[Block]) -> str:
436
+ def _max_completed_block_tokens(blocks: list["Block"]) -> int:
437
+ """Largest total_tokens among completed (non-gap, non-active) blocks.
438
+
439
+ The auto-derived token-limit baseline for the `%`/REMAINING/PROJECTED
440
+ surface β€” matches ccusage's `maxTokensFromAll` (computed over all blocks
441
+ before any --recent/--active filtering). Returns 0 when there is no
442
+ completed block with tokens.
443
+ """
444
+ best = 0
445
+ for b in blocks:
446
+ if not b.is_gap and not b.is_active and b.total_tokens > best:
447
+ best = b.total_tokens
448
+ return best
449
+
450
+
451
+ def _parse_blocks_token_limit(
452
+ raw: "str | None", max_from_completed: int
453
+ ) -> "int | None":
454
+ """Resolve the `-t/--token-limit` value to an int limit or None.
455
+
456
+ Mirrors ccusage `parseTokenLimit`: `None`/`""`/`"max"` β†’ the auto-derived
457
+ `max_from_completed` (or None when it is 0); otherwise replicate JS
458
+ `Number.parseInt(raw, 10)` β€” leading optional sign + run of digits, stop at
459
+ the first non-digit (`"123abc"`β†’123, `"12.5"`β†’12, `"abc"`/`""`β†’None). A
460
+ non-positive result still returns the int; the caller's `limit > 0` gate
461
+ suppresses the `%` column (same observable result as upstream).
462
+ """
463
+ if raw is None or raw in ("", "max"):
464
+ return max_from_completed if max_from_completed > 0 else None
465
+ m = re.match(r"\s*([+-]?\d+)", raw)
466
+ return int(m.group(1)) if m else None
467
+
468
+
469
+ def _blocks_to_json(
470
+ blocks: list[Block], *, token_limit_status_limit: int | None = None
471
+ ) -> str:
436
472
  """Serialize blocks to JSON matching upstream ccusage's output structure."""
437
473
 
438
474
  def _iso_utc(ts: dt.datetime) -> str:
@@ -470,6 +506,24 @@ def _blocks_to_json(blocks: list[Block]) -> str:
470
506
  "burnRate": block.burn_rate,
471
507
  "projection": block.projection,
472
508
  })
509
+ if (token_limit_status_limit is not None
510
+ and token_limit_status_limit > 0
511
+ and not block.is_gap
512
+ and block.is_active and block.projection):
513
+ limit = token_limit_status_limit
514
+ proj_tokens = block.projection["totalTokens"]
515
+ pct = (proj_tokens / limit) * 100.0
516
+ # Keep the exceeds/warning/ok thresholds (>100% / >80%) in sync with
517
+ # the box status ladder in _lib_render._render_active_block_box.
518
+ status = ("exceeds" if proj_tokens > limit
519
+ else "warning" if proj_tokens > limit * 0.8
520
+ else "ok")
521
+ obj["tokenLimitStatus"] = {
522
+ "limit": limit,
523
+ "projectedUsage": proj_tokens,
524
+ "percentUsed": pct,
525
+ "status": status,
526
+ }
473
527
  result.append(obj)
474
528
 
475
529
  return json.dumps({"blocks": result}, indent=2)
@@ -530,7 +530,7 @@ def _check_data_codex_cache(s: DoctorState) -> CheckResult:
530
530
  if count == 0 and not s.codex_jsonl_present:
531
531
  return CheckResult(
532
532
  id="data.codex_cache", title="Codex cache",
533
- severity="ok", summary="none (no ~/.codex/sessions/)",
533
+ severity="ok", summary="none (no Codex session JSONL found)",
534
534
  remediation=None,
535
535
  details={"entries": 0, "codex_jsonl_present": False},
536
536
  )