|
| 1 | +"""What a session actually did, read from its transcript. |
| 2 | +
|
| 3 | +The SessionEnd payload carries `session_id`, `transcript_path` and `cwd`; it |
| 4 | +carries neither the tools the session used nor its turn count, so |
| 5 | +`hooks/session_lifecycle.py` wrote every session entry with `toolsUsed: []` |
| 6 | +and `turnCount: 0` and procedural mining never had an input (issue #591). |
| 7 | +
|
| 8 | +The transcript has both. This reads it as a stream, keeping the tool calls in |
| 9 | +the order they were made, repetitions included, because `core.procedural_memory` |
| 10 | +mines contiguous subsequences: a deduplicated set would describe a sequence |
| 11 | +nobody performed. |
| 12 | +
|
| 13 | +source: ADR-1072""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +from pathlib import Path |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +# A session's tool calls, capped so one long session cannot dominate the |
| 22 | +# session log. |
| 23 | +# source: measured 2026-09-16 over the 374 transcripts under |
| 24 | +# ~/.claude/projects, where the largest carried 539 tool calls and the |
| 25 | +# median non-empty one 34; the decision is recorded in ADR number 1072. |
| 26 | +MAX_TOOL_SEQUENCE = 2000 |
| 27 | + |
| 28 | + |
| 29 | +def _message_content(record: dict) -> Any: |
| 30 | + """The record's message content, or None when the record is not shaped |
| 31 | + the way a transcript record is. Nothing here trusts a field's type: |
| 32 | + every level of a hand-written or truncated transcript can hold anything. |
| 33 | + """ |
| 34 | + message = record.get("message") |
| 35 | + return message.get("content") if isinstance(message, dict) else None |
| 36 | + |
| 37 | + |
| 38 | +def _tool_names(content: Any) -> list[str]: |
| 39 | + if not isinstance(content, list): |
| 40 | + return [] |
| 41 | + return [ |
| 42 | + block["name"] |
| 43 | + for block in content |
| 44 | + if isinstance(block, dict) |
| 45 | + and block.get("type") == "tool_use" |
| 46 | + and block.get("name") |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +def transcript_activity(transcript_path: str | Path | None) -> dict[str, Any]: |
| 51 | + """Return {"tool_sequence": [...], "turn_count": n} for a transcript. |
| 52 | +
|
| 53 | + An unreadable, missing or malformed transcript yields an empty sequence |
| 54 | + and a zero count: the caller keeps whatever the event supplied. A line |
| 55 | + that does not parse as JSON, or parses as anything other than an object, |
| 56 | + is skipped and the rest of the file is still read — this runs inside the |
| 57 | + SessionEnd hook, where an exception would cost the session its log entry |
| 58 | + and its profile update. |
| 59 | + """ |
| 60 | + empty: dict[str, Any] = {"tool_sequence": [], "turn_count": 0} |
| 61 | + if not transcript_path: |
| 62 | + return empty |
| 63 | + path = Path(transcript_path) |
| 64 | + sequence: list[str] = [] |
| 65 | + turns = 0 |
| 66 | + try: |
| 67 | + with path.open(encoding="utf-8", errors="replace") as stream: |
| 68 | + for line in stream: |
| 69 | + if '"assistant"' not in line: |
| 70 | + continue |
| 71 | + try: |
| 72 | + record = json.loads(line) |
| 73 | + except ValueError: |
| 74 | + continue |
| 75 | + if not isinstance(record, dict) or record.get("type") != "assistant": |
| 76 | + continue |
| 77 | + turns += 1 |
| 78 | + if len(sequence) < MAX_TOOL_SEQUENCE: |
| 79 | + sequence.extend(_tool_names(_message_content(record))) |
| 80 | + except OSError: |
| 81 | + return empty |
| 82 | + return {"tool_sequence": sequence[:MAX_TOOL_SEQUENCE], "turn_count": turns} |
0 commit comments