diff options
| author | Elvis Claros Castro <elvis@claros.ar> | 2026-10-03 19:47:45 -0300 |
|---|---|---|
| committer | Elvis Claros Castro <elvis@claros.ar> | 2026-10-03 19:47:45 -0300 |
| commit | a5896fe2e28eb1e9ff9affe5d47ccd443bee5fc2 (patch) | |
| tree | d76155b2c6aed9b905fe1b04094b879420964177 /claude_logbook | |
| parent | 60c586001cf4d21ef1351be8c8ad7a269a6f005a (diff) | |
| download | claude-logbook-a5896fe2e28eb1e9ff9affe5d47ccd443bee5fc2.tar.gz claude-logbook-a5896fe2e28eb1e9ff9affe5d47ccd443bee5fc2.zip | |
Use the /rename title, mark compactions, count active time
A title set with /rename (custom-title) now wins over Claude's. The
summary Claude Code leaves when the context is compacted is a block of its
own ("c"), shown as a "context compacted here" mark instead of a message of
yours, and not counted. Duration is active time: pauses over 30 minutes
between messages are left out.
Diffstat (limited to 'claude_logbook')
| -rw-r--r-- | claude_logbook/__init__.py | 2 | ||||
| -rw-r--r-- | claude_logbook/sessions.py | 50 | ||||
| -rw-r--r-- | claude_logbook/template.html | 19 | ||||
| -rw-r--r-- | claude_logbook/terminal.py | 10 |
4 files changed, 71 insertions, 10 deletions
diff --git a/claude_logbook/__init__.py b/claude_logbook/__init__.py index b05f474..bcc14b2 100644 --- a/claude_logbook/__init__.py +++ b/claude_logbook/__init__.py @@ -3,6 +3,6 @@ No dependencies: standard library only. """ -__version__ = "1.5.0" +__version__ = "1.5.1" __all__ = ["__version__"] diff --git a/claude_logbook/sessions.py b/claude_logbook/sessions.py index 63b4886..5f3035d 100644 --- a/claude_logbook/sessions.py +++ b/claude_logbook/sessions.py @@ -19,7 +19,8 @@ Schema of the record returned by `read_session`: u/a number of messages from you / from Claude k size of the .jsonl in KB v Claude Code version - c transcript: [{"r": "u" | "a" | "t", "x": text}] + c transcript: [{"r": "u" | "a" | "t" | "c", "x": text}] + (t: tool call summary, c: compaction summary) `project_dir` and `mtime` are internal and never leave the module: `public_records()` drops them before the record is serialized. @@ -33,7 +34,7 @@ from datetime import datetime, timezone # Bump it when the record schema changes: it invalidates old caches instead of # reading records with the previous shape. -CACHE_VERSION = 2 +CACHE_VERSION = 3 EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc) @@ -84,6 +85,10 @@ TOOL_ARG_MAX = 140 # no back and forth, is a pipe on stdin and not a conversation. NONINTERACTIVE_CHARS = 1500 +# Active time: a pause between two messages longer than this is the session +# left open, not work, and does not count towards its duration. +IDLE_GAP_MINUTES = 30 + # For each tool, the parameter that best summarizes what it did. TOOL_KEY = { "Bash": "command", "Read": "file_path", "Edit": "file_path", @@ -140,11 +145,26 @@ def parse_ts(ts): return None +def active_minutes(stamps): + """Minutes of work: the gaps between consecutive messages, leaving out + the pauses longer than IDLE_GAP_MINUTES. None without messages.""" + times = sorted(t for t in map(parse_ts, stamps) if t) + if not times: + return None + total = 0.0 + for a, b in zip(times, times[1:]): + gap = (b - a).total_seconds() / 60 + if gap <= IDLE_GAP_MINUTES: + total += gap + return round(total) + + def read_session(path): """Parses a whole .jsonl and returns that session's record.""" session_id = os.path.basename(path)[:-6] # without .jsonl first_ts = last_ts = cwd = git_branch = version = None - ai_title = fallback_title = None + ai_title = custom_title = fallback_title = None + stamps = [] # of the messages, for the active time user_msgs = assistant_msgs = 0 convo = [] @@ -166,6 +186,10 @@ def read_session(path): if obj.get("aiTitle"): ai_title = obj["aiTitle"] # keep the most recent one continue + if kind == "custom-title": + if obj.get("customTitle"): + custom_title = obj["customTitle"] # /rename, wins over the AI one + continue ts = obj.get("timestamp") if ts: @@ -185,10 +209,20 @@ def read_session(path): message = obj.get("message") if not isinstance(message, dict): continue + if ts: + stamps.append(ts) if kind == "user": if obj.get("isMeta"): continue + # What /compact (or running out of context) left in place of + # the earlier conversation: written by Claude, not by you. + if obj.get("isCompactSummary"): + text = "\n".join( + (b.get("text") or "").strip() for b in blocks_of(message) + if isinstance(b, dict) and b.get("type") == "text").strip() + convo.append({"r": "c", "x": text}) + continue for b in blocks_of(message): if not isinstance(b, dict): continue @@ -217,13 +251,11 @@ def read_session(path): assistant_msgs += 1 st = os.stat(path) - ft, lt = parse_ts(first_ts), parse_ts(last_ts) - # A single huge message and no back and forth is the signature of a # `claude -p` with something piped on stdin (e.g. a git diff to write the # commit message), not of a conversation. noninteractive = ( - user_msgs == 1 and not ai_title and bool(convo) + user_msgs == 1 and not (ai_title or custom_title) and bool(convo) and len(convo[0]["x"]) > NONINTERACTIVE_CHARS ) @@ -232,13 +264,13 @@ def read_session(path): "project_dir": os.path.basename(os.path.dirname(path)), "p": cwd, "b": git_branch, - "t": ai_title or fallback_title, - "ai": bool(ai_title), + "t": custom_title or ai_title or fallback_title, + "ai": bool(custom_title or ai_title), "n": noninteractive, "e": not convo, "f": first_ts, "l": last_ts, - "d": round((lt - ft).total_seconds() / 60) if ft and lt else None, + "d": active_minutes(stamps), "u": user_msgs, "a": assistant_msgs, "k": round(st.st_size / 1024, 1), diff --git a/claude_logbook/template.html b/claude_logbook/template.html index c018320..20dfbee 100644 --- a/claude_logbook/template.html +++ b/claude_logbook/template.html @@ -509,6 +509,23 @@ } .tool b { color: var(--amber); font-weight: 500; flex-shrink: 0; } + .compact { margin: 6px 0; } + .compact summary { + font-family: var(--mono); + font-size: 11.5px; + color: var(--muted); + cursor: pointer; + text-align: center; + padding: 6px 0; + border-top: 1px dashed var(--line); + border-bottom: 1px dashed var(--line); + list-style: none; + } + .compact summary::-webkit-details-marker { display: none; } + .compact summary::before { content: '▸ '; } + .compact[open] summary::before { content: '▾ '; } + .compact .bubble { margin-top: 8px; font-size: 13.5px; } + .reader-empty { color: var(--muted); font-family: var(--mono); font-size: 13px; text-align: center; padding: 60px 20px; } /* ---------- toast ---------- */ @@ -1170,6 +1187,8 @@ function openChat(id) { bodyEl.innerHTML = d.c.length ? d.c.map(m => m.r === 't' ? `<div class="tool"><b>⚒</b><span>${esc(m.x)}</span></div>` + : m.r === 'c' + ? `<details class="compact"><summary>Context compacted here · the summary Claude kept</summary><div class="bubble">${renderBody(m.x)}</div></details>` : `<div class="turn turn-${m.r}"><span class="who">${WHO[m.r]}</span><div class="bubble">${renderBody(m.x)}</div></div>` ).join('') : `<p class="reader-empty">This session has no messages.</p>`; diff --git a/claude_logbook/terminal.py b/claude_logbook/terminal.py index 65af046..61d05d8 100644 --- a/claude_logbook/terminal.py +++ b/claude_logbook/terminal.py @@ -214,6 +214,16 @@ def print_chat(s, st, out, show_tools=True, width=None): first = True for m in s["c"]: + if m["r"] == "c": + # The summary is long and Claude's, not part of the dialogue: + # only the mark of where the earlier context was folded. + if not first: + print(file=out) + first = False + label = " context compacted here " + side = max(3, (min(width, 80) - len(label)) // 2) + print(f"{st.faint}{'─' * side}{label}{'─' * side}{st.reset}", file=out) + continue if m["r"] == "t": if show_tools: print(f" {st.faint}⚒ {clip(m['x'], body - 4)}{st.reset}", file=out) |