clew 0.1.3

A lightweight, local, git-native project tracker for humans and agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python3
"""
Extract and summarize clew CLI invocations from Pi and Claude Code session logs.

Usage:
    python3 dev-tools/extract_clew_usage.py ~/.pi/agent/sessions/--Users-npatten-Projects-clew--/*.jsonl
    python3 dev-tools/extract_clew_usage.py session1.jsonl session2.jsonl > report.md
"""

import json
import re
import sys
from datetime import datetime, timezone
from pathlib import Path

# Match ./clew or clew only as an actual shell command — anchored to command
# boundaries (start-of-string, or after ; & | && || \n), not inside quoted strings.
CLEW_RE = re.compile(
    r'(?:^|[;&|]\s*|\n\s*)\.?/clew\b'
)

MAX_CONTEXT = 160
MAX_RESULT  = 120


def is_clew(cmd: str) -> bool:
    return bool(CLEW_RE.search(cmd))


def trunc(s: str, n: int) -> str:
    s = " ".join(s.split())  # collapse whitespace / newlines
    return s[:n] + "…" if len(s) > n else s


# ---------------------------------------------------------------------------
# Format detection
# ---------------------------------------------------------------------------

def detect_format(lines: list[str]) -> str:
    for line in lines[:6]:
        try:
            rec = json.loads(line)
        except Exception:
            continue
        t = rec.get("type", "")
        if t == "session":
            return "pi"
        if t == "queue-operation":
            return "claude"
    return "unknown"


# ---------------------------------------------------------------------------
# Normalised event stream
#
# Both parsers emit the same event shapes:
#   {"kind": "text",        "role": "assistant", "text": "...", "ts": "..."}
#   {"kind": "tool_call",   "role": "assistant", "id": "...", "cmd": "...", "ts": "..."}
#   {"kind": "tool_result", "id": "...", "output": "...", "ts": "..."}
#   {"kind": "turn_end"}   — emitted after each assistant turn's results arrive
#
# turn_end events break up the cluster detection so that parallel tool calls
# in one assistant turn don't bleed into the next turn's calls.
# ---------------------------------------------------------------------------

def _result_text(content) -> str:
    if isinstance(content, str):
        return content
    if isinstance(content, list):
        parts = []
        for item in content:
            if isinstance(item, dict):
                parts.append(item.get("text", ""))
            elif isinstance(item, str):
                parts.append(item)
        return " ".join(parts)
    return str(content)


def parse_pi(lines: list[str]) -> tuple[dict, str, list[dict]]:
    """
    Pi JSONL structure:
      - type:"session"            — session header
      - type:"model_change"       — model switch
      - type:"message" role:"user"       — human turn
      - type:"message" role:"assistant"  — content: thinking + toolCall blocks
      - type:"message" role:"toolResult" — one per tool call, toolCallId links back
    """
    session_info: dict = {}
    model = "unknown"
    events: list[dict] = []

    # We need to track how many results are expected per assistant turn so we
    # can emit a turn_end after the last result arrives.
    pending_calls: int = 0   # tool calls emitted in the current turn
    received_results: int = 0

    for line in lines:
        try:
            rec = json.loads(line)
        except Exception:
            continue

        t = rec.get("type", "")

        if t == "session":
            session_info = rec

        elif t == "model_change":
            model = rec.get("modelId", model)

        elif t == "message":
            msg = rec.get("message", {})
            role = msg.get("role", "")
            content = msg.get("content", [])
            ts = rec.get("timestamp", "")

            if role == "user":
                # Real user message — reset turn tracking
                pending_calls = 0
                received_results = 0

            elif role == "assistant":
                # Reset result counter for this new assistant turn
                pending_calls = 0
                received_results = 0

                # Extract thinking as context (often the only text in Pi turns)
                thinking_parts = []
                for block in content:
                    if not isinstance(block, dict):
                        continue
                    bt = block.get("type", "")
                    if bt == "thinking":
                        t_text = block.get("thinking", block.get("text", "")).strip()
                        if t_text:
                            thinking_parts.append(t_text)
                    elif bt == "text":
                        text = block.get("text", "").strip()
                        if text:
                            events.append({"kind": "text", "role": "assistant", "text": text, "ts": ts})
                    elif bt == "toolCall":
                        name = block.get("name", "").lower()
                        if name in ("bash", "shell", "execute", "run_bash", "computer"):
                            args = block.get("arguments", {})
                            cmd = args.get("command", args.get("cmd", ""))
                            events.append({"kind": "tool_call", "role": "assistant",
                                           "id": block.get("id", ""), "cmd": cmd, "ts": ts})
                            pending_calls += 1

                # Emit condensed thinking as context only if no explicit text was emitted
                # and there are tool calls (thinking is pre-call intent)
                if thinking_parts and pending_calls > 0:
                    first_thought = thinking_parts[0]
                    events.append({"kind": "text", "role": "assistant",
                                   "text": first_thought, "ts": ts, "source": "thinking"})

            elif role == "toolResult":
                result_content = _result_text(content)
                call_id = msg.get("toolCallId", "")
                events.append({"kind": "tool_result",
                               "id": call_id,
                               "output": result_content,
                               "ts": ts})
                received_results += 1
                # Once all results for this assistant turn are in, signal turn end
                if pending_calls > 0 and received_results >= pending_calls:
                    events.append({"kind": "turn_end"})
                    pending_calls = 0
                    received_results = 0

    return session_info, model, events


def parse_claude(lines: list[str]) -> tuple[dict, str, list[dict]]:
    """
    Claude Code JSONL structure:
      - type:"queue-operation"  — session bookkeeping
      - type:"assistant"        — content: text + tool_use blocks
      - type:"user"             — content: tool_result blocks (after assistant turn)
    """
    session_info: dict = {}
    model = "claude"
    events: list[dict] = []
    pending_calls: int = 0

    for line in lines:
        try:
            rec = json.loads(line)
        except Exception:
            continue

        t = rec.get("type", "")
        ts = rec.get("timestamp", "")

        if t == "assistant":
            msg = rec.get("message", {})
            m = msg.get("model", "")
            if m:
                model = m
            if not session_info:
                session_info = {"id": rec.get("sessionId", ""), "timestamp": ts,
                                "cwd": rec.get("cwd", "")}

            pending_calls = 0
            for block in msg.get("content", []):
                if not isinstance(block, dict):
                    continue
                bt = block.get("type", "")
                if bt == "text":
                    text = block.get("text", "").strip()
                    if text:
                        events.append({"kind": "text", "role": "assistant", "text": text, "ts": ts})
                elif bt == "tool_use" and block.get("name") == "Bash":
                    cmd = block.get("input", {}).get("command", "")
                    events.append({"kind": "tool_call", "role": "assistant",
                                   "id": block.get("id", ""), "cmd": cmd, "ts": ts})
                    pending_calls += 1

        elif t == "user":
            msg = rec.get("message", {})
            result_count = 0
            for block in msg.get("content", []):
                if not isinstance(block, dict):
                    continue
                if block.get("type") == "tool_result":
                    result_content = block.get("content", "")
                    events.append({"kind": "tool_result",
                                   "id": block.get("tool_use_id", ""),
                                   "output": _result_text(result_content),
                                   "ts": ts})
                    result_count += 1
            if result_count > 0:
                events.append({"kind": "turn_end"})
                pending_calls = 0

    return session_info, model, events


# ---------------------------------------------------------------------------
# Cluster extraction
# ---------------------------------------------------------------------------

def extract_clusters(events: list[dict]) -> list[dict]:
    """
    A cluster = an assistant turn (or consecutive turns with no text between)
    that contains at least one clew call.

    turn_end events break the run — they mark the boundary between turns.
    """
    results: dict[str, str] = {}
    for ev in events:
        if ev["kind"] == "tool_result" and ev.get("id"):
            results[ev["id"]] = ev["output"]

    clusters = []
    last_text = ""
    i = 0

    while i < len(events):
        ev = events[i]

        if ev["kind"] == "text" and ev["role"] == "assistant":
            last_text = ev["text"]
            i += 1
            continue

        if ev["kind"] in ("tool_result", "turn_end"):
            i += 1
            continue

        if ev["kind"] == "tool_call":
            # Collect ALL calls up to the next turn_end (= one assistant turn)
            run: list[dict] = []
            while i < len(events) and events[i]["kind"] == "tool_call":
                e = events[i]
                run.append({
                    "cmd": e["cmd"],
                    "output": results.get(e.get("id", ""), ""),
                    "is_clew": is_clew(e["cmd"]),
                    "ts": e["ts"],
                })
                i += 1

            if any(c["is_clew"] for c in run):
                context_after = ""
                for j in range(i, min(i + 12, len(events))):
                    if events[j]["kind"] == "text" and events[j]["role"] == "assistant":
                        src = events[j].get("source", "")
                        if src != "thinking":  # prefer real text for "after"
                            context_after = events[j]["text"]
                            break
                    if events[j]["kind"] == "tool_call":
                        break  # next agent action started, no text interlude

                clusters.append({
                    "before": last_text,
                    "calls": run,
                    "after": context_after,
                    "ts": run[0]["ts"],
                })
            continue

        i += 1

    return clusters


# ---------------------------------------------------------------------------
# Markdown formatting
# ---------------------------------------------------------------------------

def _fmt_ts(ts: str) -> str:
    if not ts:
        return "unknown time"
    try:
        dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))
        return dt.strftime("%Y-%m-%d %H:%M UTC")
    except Exception:
        return ts


def format_session(path: str, session_info: dict, model: str, clusters: list[dict]) -> str:
    sid = (session_info.get("id") or "")[:8] or "unknown"
    ts_str = _fmt_ts(session_info.get("timestamp", ""))
    cwd = session_info.get("cwd", "")
    filename = Path(path).name

    lines = [f"## Session `{sid}` — {ts_str}  ({model})"]
    lines.append(f"_file: `{filename}`_")
    if cwd:
        lines.append(f"_cwd: `{cwd}`_")
    lines.append("")

    if not clusters:
        lines.append("_No clew invocations._\n")
        return "\n".join(lines)

    for idx, cluster in enumerate(clusters, 1):
        first_clew_cmd = next(
            (c["cmd"] for c in cluster["calls"] if c["is_clew"]),
            cluster["calls"][0]["cmd"]
        )
        lines.append(f"### Cluster {idx} — `{trunc(first_clew_cmd, 70)}`")
        lines.append("")

        if cluster["before"]:
            lines.append(f"**Before:** {trunc(cluster['before'], MAX_CONTEXT)}")
            lines.append("")

        for call in cluster["calls"]:
            marker = "" if call["is_clew"] else "_(side)_ "
            output = trunc(call["output"], MAX_RESULT) if call["output"].strip() else "_(no output)_"
            lines.append(f"- {marker}`{call['cmd']}`")
            lines.append(f"  → {output}")

        if cluster["after"]:
            lines.append("")
            lines.append(f"**After:** {trunc(cluster['after'], MAX_CONTEXT)}")

        lines.append("")

    return "\n".join(lines)


# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------

def process_file(path: str) -> str:
    with open(path, encoding="utf-8", errors="replace") as f:
        lines = f.readlines()

    fmt = detect_format(lines)

    if fmt == "pi":
        session_info, model, events = parse_pi(lines)
    elif fmt == "claude":
        session_info, model, events = parse_claude(lines)
    else:
        return f"## `{Path(path).name}`\n_Unknown format — skipping._\n"

    clusters = extract_clusters(events)
    return format_session(path, session_info, model, clusters)


def main() -> None:
    if len(sys.argv) < 2:
        print("Usage: extract_clew_usage.py <session.jsonl> ...", file=sys.stderr)
        sys.exit(1)

    paths = sys.argv[1:]
    print("# Clew Usage Report\n")
    print(f"_Generated {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')} from {len(paths)} session(s)_\n")
    print("---\n")

    for path in paths:
        print(process_file(path))
        print("---\n")


if __name__ == "__main__":
    main()