@tamng0905/builder-essential-skills 0.1.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.
Files changed (66) hide show
  1. package/.claude/agents/lead-builder.md +55 -0
  2. package/.claude/agents/lead-reviewer.md +39 -0
  3. package/README.md +276 -0
  4. package/assets/readme-hero.png +0 -0
  5. package/assets/skill-cards/async-learning-teacher.svg +7 -0
  6. package/assets/skill-cards/code-standards.svg +7 -0
  7. package/assets/skill-cards/lead-research.svg +7 -0
  8. package/assets/skill-cards/lead.svg +7 -0
  9. package/assets/skill-cards/orwell-writing.svg +7 -0
  10. package/assets/skill-cards/session-profiler.svg +7 -0
  11. package/assets/skill-cards/validate-market.svg +7 -0
  12. package/assets/skill-cards/write-blog.svg +7 -0
  13. package/bin/builder-essential-skills.js +134 -0
  14. package/install.ps1 +58 -0
  15. package/install.sh +63 -0
  16. package/package.json +24 -0
  17. package/skills/async-learning-teacher/README.md +50 -0
  18. package/skills/async-learning-teacher/SKILL.md +192 -0
  19. package/skills/async-learning-teacher/agents/openai.yaml +4 -0
  20. package/skills/code-standards/README.md +27 -0
  21. package/skills/code-standards/SKILL.md +51 -0
  22. package/skills/lead/README.md +29 -0
  23. package/skills/lead/SKILL.md +222 -0
  24. package/skills/lead/config.py +359 -0
  25. package/skills/lead/dispatch.md +342 -0
  26. package/skills/lead/loop.md +103 -0
  27. package/skills/lead/models.json +34 -0
  28. package/skills/lead/research.md +76 -0
  29. package/skills/lead/status.ps1 +114 -0
  30. package/skills/lead/status.sh +110 -0
  31. package/skills/lead/watchdog.ps1 +90 -0
  32. package/skills/lead/watchdog.sh +88 -0
  33. package/skills/lead-research/README.md +27 -0
  34. package/skills/lead-research/SKILL.md +157 -0
  35. package/skills/lead-research/tactics.md +150 -0
  36. package/skills/orwell-writing/README.md +27 -0
  37. package/skills/orwell-writing/SKILL.md +44 -0
  38. package/skills/orwell-writing/agents/openai.yaml +4 -0
  39. package/skills/session-profiler/README.md +25 -0
  40. package/skills/session-profiler/SKILL.md +103 -0
  41. package/skills/session-profiler/agents/openai.yaml +4 -0
  42. package/skills/session-profiler/scripts/requirements.txt +2 -0
  43. package/skills/session-profiler/scripts/session_profiler/__init__.py +5 -0
  44. package/skills/session-profiler/scripts/session_profiler/analyses.py +246 -0
  45. package/skills/session-profiler/scripts/session_profiler/cli.py +77 -0
  46. package/skills/session-profiler/scripts/session_profiler/dataset.py +369 -0
  47. package/skills/session-profiler/scripts/session_profiler/discover.py +191 -0
  48. package/skills/session-profiler/scripts/session_profiler/toc.py +74 -0
  49. package/skills/session-profiler/scripts/session_profiler/trace.py +146 -0
  50. package/skills/session-profiler/scripts/sp +20 -0
  51. package/skills/session-profiler/scripts/tests/codex_home/sessions/2026/01/01/rollout-child.jsonl +7 -0
  52. package/skills/session-profiler/scripts/tests/codex_home/sessions/2026/01/01/rollout-root.jsonl +10 -0
  53. package/skills/session-profiler/scripts/tests/fixture/main/subagents/agent-abc.jsonl +2 -0
  54. package/skills/session-profiler/scripts/tests/fixture/main/subagents/agent-abc.meta.json +1 -0
  55. package/skills/session-profiler/scripts/tests/fixture/main.jsonl +7 -0
  56. package/skills/session-profiler/scripts/tests/test_fixture.py +58 -0
  57. package/skills/validate-market/README.md +27 -0
  58. package/skills/validate-market/SKILL.md +202 -0
  59. package/skills/write-blog/README.md +27 -0
  60. package/skills/write-blog/SKILL.md +142 -0
  61. package/skills/write-blog/check.sh +52 -0
  62. package/skills/write-blog/reference/angles.md +51 -0
  63. package/skills/write-blog/reference/craft.md +94 -0
  64. package/skills/write-blog/reference/seo.md +116 -0
  65. package/skills/write-blog/reference/voice.md +64 -0
  66. package/skills/write-blog/reference/wiring.md +113 -0
@@ -0,0 +1,44 @@
1
+ ---
2
+ name: orwell-writing
3
+ description: Use when an agent is asked to draft, rewrite, edit, review, polish, copyedit, simplify, humanize, or create written prose, including creative writing, essays, posts, scripts, speeches, emails, documentation, product copy, and other style-sensitive text. Apply George Orwell's six rules from "Politics and the English Language" as a plain-English style discipline while preserving the user's intended meaning, audience, tone, and explicit constraints.
4
+ ---
5
+
6
+ # Orwell Writing
7
+
8
+ ## Overview
9
+
10
+ Use Orwell's rules as a practical filter for clarity, force, and honesty in English prose. Apply them to both drafting and revision, but do not erase deliberate voice, character, rhythm, humor, or genre when the user clearly wants them.
11
+
12
+ ## Core Rules
13
+
14
+ Remember these rules from "Politics and the English Language":
15
+
16
+ 1. Never use a metaphor, simile, or other figure of speech which you are used to seeing in print.
17
+ 2. Never use a long word where a short one will do.
18
+ 3. If it is possible to cut a word out, always cut it out.
19
+ 4. Never use the passive where you can use the active.
20
+ 5. Never use a foreign phrase, a scientific word, or a jargon word if you can think of an everyday English equivalent.
21
+ 6. Break any of these rules sooner than say anything outright barbarous.
22
+
23
+ ## Workflow
24
+
25
+ When writing from scratch:
26
+
27
+ 1. Identify the audience, purpose, and promised tone from the user's request.
28
+ 2. Draft in concrete, direct English.
29
+ 3. Remove stock phrases, dead metaphors, filler, pompous diction, needless abstraction, and avoidable jargon.
30
+ 4. Prefer active verbs and clear subjects unless passive voice better serves emphasis, tact, suspense, or technical accuracy.
31
+ 5. Keep necessary nuance; do not make prose crude, false, or flat just to make it short.
32
+
33
+ When revising existing text:
34
+
35
+ 1. Preserve the user's meaning and any explicit tone or format constraints.
36
+ 2. Cut words, clauses, and sentences that do no work.
37
+ 3. Replace stale figures of speech with plain phrasing or a fresh, specific image.
38
+ 4. Replace long, foreign, scientific, or jargon terms with everyday English when accuracy permits.
39
+ 5. Convert passive constructions to active ones when the actor matters and is known.
40
+ 6. Flag any remaining jargon, passive voice, or ornate phrasing that is necessary rather than silently removing important precision.
41
+
42
+ ## Creative Writing
43
+
44
+ For fiction, poetry, memoir, scripts, and lyrical prose, treat the rules as pressure, not law. Keep intentional ambiguity, cadence, dialogue style, image, and character voice when they create a real effect. Remove only the language that feels inherited, inflated, evasive, or lazy.
@@ -0,0 +1,4 @@
1
+ interface:
2
+ display_name: "Orwell Writing"
3
+ short_description: "Plain English rules for clear writing"
4
+ default_prompt: "Use $orwell-writing to revise this draft into clear, direct English."
@@ -0,0 +1,25 @@
1
+ # Session Profiler
2
+
3
+ <p align="center">
4
+ <img src="../../assets/skill-cards/session-profiler.svg" alt="session-profiler skill card" width="100%">
5
+ </p>
6
+
7
+ Inspect Claude Code and Codex session transcripts, summarize agent activity,
8
+ break down time, tools, tokens, and estimated cost, and export Perfetto traces.
9
+
10
+ ## Install
11
+
12
+ Install this skill for your user account:
13
+
14
+ ```bash
15
+ npx @tamng0905/builder-essential-skills --skill session-profiler
16
+ ```
17
+
18
+ Install it into the current repository instead:
19
+
20
+ ```bash
21
+ npx @tamng0905/builder-essential-skills --skill session-profiler --project
22
+ ```
23
+
24
+ Restart Claude Code or Codex. Python 3 is required for the profiler's bundled
25
+ tools; see [SKILL.md](SKILL.md) for usage and transcript details.
@@ -0,0 +1,103 @@
1
+ ---
2
+ name: session-profiler
3
+ description: Profile and debug Claude Code and OpenAI Codex sessions from their JSONL transcripts. Find a session and its subagents, build a queryable event table, summarize the work as a hierarchical table of contents, break down wall time, inference, tools, tokens, and estimated cost per agent, identify errors and improvement opportunities, and export a shareable Perfetto trace. Use when a user wants to inspect what a Claude or Codex session did, debug agent or subagent activity, understand session cost or latency, create a session timeline, generate a trace, or learn how to improve the next agent run.
4
+ ---
5
+
6
+ # Session Profiler
7
+
8
+ Turn one Claude Code or Codex main transcript plus its subagent transcripts into analysis artifacts. Route every operation through the bundled wrapper so it uses a consistent environment and remembers the latest parsed work directory.
9
+
10
+ ```bash
11
+ SP="$SKILL/scripts/sp"
12
+ ```
13
+
14
+ Resolve `SKILL` to this skill's directory. If it is unavailable, use the absolute path to `scripts/sp` beside this file.
15
+
16
+ ## Workflow
17
+
18
+ 1. Find and inspect the session.
19
+
20
+ ```bash
21
+ $SP projects
22
+ $SP list [--provider claude|codex] [--n 20]
23
+ $SP list --project-dir ~/.claude/projects/<munged-cwd>
24
+ $SP find <session-id-prefix>
25
+ $SP info <session-id-or-path>
26
+ ```
27
+
28
+ Claude Code stores main transcripts at `~/.claude/projects/<munged-cwd>/<session-id>.jsonl`; its subagents normally live at `<session-id>/subagents/agent-*.jsonl`. Codex stores rollouts under `${CODEX_HOME:-~/.codex}/sessions/YYYY/MM/DD/rollout-*.jsonl` and archived rollouts under `archived_sessions/`. Codex subagent rollouts identify their parent thread recursively in `session_meta`.
29
+
30
+ 2. Parse the session before analyzing it. Re-run this for a session that is still active.
31
+
32
+ ```bash
33
+ $SP parse <session-id-or-main-jsonl-path>
34
+ ```
35
+
36
+ The command prints and remembers its work directory. It writes `events.parquet`, `events.csv`, and `agents.json`. Override the remembered directory with global `--data-dir <dir>` or `SESSION_DATA_DIR`.
37
+
38
+ 3. Start with a readable profile, then narrow the investigation.
39
+
40
+ ```bash
41
+ $SP brief
42
+ $SP agent-summary
43
+ $SP costs
44
+ $SP slowest-tools --n 20 [--agent <id-or-name>]
45
+ $SP tool-breakdown [--agent <id-or-name>]
46
+ $SP inference-vs-tool
47
+ $SP errors
48
+ $SP turns
49
+ $SP timeline 2026-07-08T19:14:00Z --window 120
50
+ $SP events [--agent main] [--tool Bash] [--grep rebase] [--since 2026-07-08] [--n 30] [--long]
51
+ ```
52
+
53
+ Use `brief` first when explaining a session to a person. It writes `brief.md`
54
+ with the session's headline metrics, standout slow paths or failures, prompt
55
+ trail or TOC storyline, and an agent scoreboard. Use the table commands for
56
+ specific questions after the brief identifies the interesting region.
57
+
58
+ For ad hoc pandas analysis, set `SESSION_DATA_DIR`, add `scripts/` to `PYTHONPATH`, and use `from session_profiler.dataset import load; df = load()` inside the wrapper's venv.
59
+
60
+ 4. Summarize the session and preserve lessons for the next run.
61
+
62
+ ```bash
63
+ $SP toc --dry-run
64
+ $SP toc # auto-selects claude or codex from the parsed provider
65
+ $SP toc --engine codex # optional override; --model is also supported
66
+ $SP review
67
+ ```
68
+
69
+ Use `toc --dry-run` to inspect the sanitized, junk-free digest without calling another model. `toc` automatically calls `claude -p` for Claude data or an ephemeral, read-only `codex exec` for Codex data, then writes `toc.json` plus `toc.md`. Show `toc.md` to orient the user. `review` writes `review.md` with deterministic observations about failed calls, slow paths, active-time mix, delegation, and estimated cost. Turn those observations into concrete changes to prompts, tool batching, preflight checks, or subagent assignments; do not claim the profiler automatically changes agent behavior.
70
+
71
+ 5. Build and open the trace.
72
+
73
+ ```bash
74
+ $SP trace
75
+ $SP open
76
+ ```
77
+
78
+ Load the emitted `trace.json.gz` in [Perfetto](https://ui.perfetto.dev). Read
79
+ it top-down: `Session overview` gives the whole run, `Table of contents` shows
80
+ the story when `toc.json` exists, each agent's `work timeline` shows inference
81
+ and tools, `prompt trail` marks user turns, and `usage tokens + cost` shows
82
+ cumulative counters. Click a slice to inspect sanitized input/output previews,
83
+ duration, concurrency, failure state, token count, and estimated cost.
84
+
85
+ ## Interpretation Rules
86
+
87
+ Trust these derivations instead of recomputing them from raw rows:
88
+
89
+ - For Claude, deduplicate usage per API `message.id`: content-block records repeat a growing usage snapshot, so keep only the final snapshot once. For Codex, consume `last_token_usage` once per `token_count` event and treat `cached_input_tokens` as a subset of input tokens.
90
+ - Sum tokens across agents. Subagent token usage lives in the subagent transcript, not the parent transcript.
91
+ - Measure a tool from `tool_use` to its matching `tool_result` within the same transcript. Treat multiple tool uses sharing one assistant message as concurrent.
92
+ - Measure inference from the first to last assistant record for one message ID. Do not treat human idle time as inference.
93
+ - Measure each subagent's wall runtime from the first to last timestamp in its own transcript, not from the parent's spawn tool span.
94
+ - Treat active-time totals as sums of spans, not wall time. Concurrent tools and agents can make summed active time exceed elapsed wall time.
95
+ - Label all dollar values as estimates. Claude uses the source specification's Opus-tier reference rates. Codex uses model-specific public OpenAI API rates when the model is known, including long-context multipliers recorded by the pricing docs. Codex subscriptions, credits, priority processing, tool fees, and negotiated rates can differ; an unknown model is marked unavailable instead of guessed.
96
+
97
+ ## Privacy And Failure Handling
98
+
99
+ - Treat transcripts and traces as sensitive. Previews redact long opaque tokens and common auth/cookie headers, but can still contain source code, prompts, paths, personal data, or secrets. Review before sharing and share only where the session owner would.
100
+ - Expect UTC timestamps throughout.
101
+ - If parsing a growing transcript, rerun `parse` immediately before final analysis.
102
+ - If dependency bootstrap fails, report the `uv` or `pip` error and let the user fix local network/package configuration. Do not add machine-specific proxy settings to this skill.
103
+ - If `toc` fails because the matching `claude` or `codex` CLI is missing or unauthenticated, keep the parsed data and analyses; use `toc --dry-run` as a digest and explain the missing prerequisite.
@@ -0,0 +1,4 @@
1
+ interface:
2
+ display_name: "Session Profiler"
3
+ short_description: "Profile Claude and Codex agent sessions"
4
+ default_prompt: "Use $session-profiler to profile this Claude Code or Codex session, explain where time and tokens went, and identify improvements for the next run."
@@ -0,0 +1,2 @@
1
+ pandas>=2.1
2
+ pyarrow>=14
@@ -0,0 +1,5 @@
1
+ """Claude Code JSONL session profiler."""
2
+
3
+ from .dataset import load
4
+
5
+ __all__ = ["load"]
@@ -0,0 +1,246 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ import pandas as pd
6
+
7
+ from .dataset import _price, load, load_agents, work_dir
8
+
9
+
10
+ def _duration(seconds: float) -> str:
11
+ seconds = max(0.0, float(seconds or 0))
12
+ if seconds < 60:
13
+ return f"{seconds:.1f}s"
14
+ minutes, sec = divmod(seconds, 60)
15
+ if minutes < 60:
16
+ return f"{int(minutes)}m {int(sec)}s"
17
+ hours, minutes = divmod(minutes, 60)
18
+ return f"{int(hours)}h {int(minutes)}m"
19
+
20
+
21
+ def _markdown_table(headers: list[str], rows: list[list[object]]) -> str:
22
+ if not rows:
23
+ return ""
24
+ def cell(value: object) -> str:
25
+ return str(value).replace("|", "\\|")
26
+
27
+ lines = [
28
+ "| " + " | ".join(cell(header) for header in headers) + " |",
29
+ "| " + " | ".join("---" for _ in headers) + " |",
30
+ ]
31
+ for row in rows:
32
+ lines.append("| " + " | ".join(cell(value) for value in row) + " |")
33
+ return "\n".join(lines)
34
+
35
+
36
+ def _fmt(df: pd.DataFrame) -> str:
37
+ if df.empty:
38
+ return "No matching events."
39
+ return df.to_string(index=False, max_colwidth=80)
40
+
41
+
42
+ def agent_summary(data_dir: str | Path | None = None) -> str:
43
+ df = load(data_dir)
44
+ agents = load_agents(data_dir)["agents"]
45
+ rows = []
46
+ for agent in agents:
47
+ part = df[df.agent_id == agent["id"]]
48
+ inf = part[part.event_type == "inference"]
49
+ tools = part[part.event_type == "tool"]
50
+ start = pd.to_datetime(agent.get("start_ts"), utc=True)
51
+ end = pd.to_datetime(agent.get("end_ts"), utc=True)
52
+ priced = all(_price(row.provider, row.model) for row in inf.itertuples()) if not inf.empty else True
53
+ rows.append({
54
+ "agent": agent["name"], "id": agent["id"], "depth": agent["spawn_depth"],
55
+ "wall_s": round((end - start).total_seconds(), 2) if not pd.isna(start) and not pd.isna(end) else 0,
56
+ "inference_s": round(inf.duration_ms.sum() / 1000, 2), "tool_s": round(tools.duration_ms.sum() / 1000, 2),
57
+ "tokens": int(inf[["input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "output_tokens"]].sum().sum()),
58
+ "est_cost_usd": round(inf.estimated_cost_usd.sum(), 4), "pricing": "reference" if priced else "partial/unavailable",
59
+ })
60
+ return "Estimated cost (public price table; not billing data)\n" + _fmt(pd.DataFrame(rows))
61
+
62
+
63
+ def costs(data_dir=None) -> str:
64
+ df = load(data_dir)
65
+ inf = df[df.event_type == "inference"]
66
+ cols = ["input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "output_tokens", "estimated_cost_usd"]
67
+ result = inf.groupby(["provider", "agent_name", "model"], dropna=False)[cols].sum().reset_index()
68
+ result["pricing"] = result.apply(lambda row: "reference" if _price(row.provider, row.model) else "unavailable", axis=1)
69
+ result["estimated_cost_usd"] = result.estimated_cost_usd.round(5)
70
+ return "ESTIMATES from public rates; not invoice/billing data.\n" + _fmt(result)
71
+
72
+
73
+ def slowest_tools(data_dir=None, n=20, agent=None) -> str:
74
+ df = load(data_dir)
75
+ part = df[df.event_type == "tool"]
76
+ if agent:
77
+ part = part[(part.agent_id == agent) | (part.agent_name == agent)]
78
+ return _fmt(part.nlargest(n, "duration_ms")[["ts", "agent_name", "tool_name", "duration_ms", "is_error", "input_preview"]])
79
+
80
+
81
+ def tool_breakdown(data_dir=None, agent=None) -> str:
82
+ df = load(data_dir)
83
+ part = df[df.event_type == "tool"]
84
+ if agent:
85
+ part = part[(part.agent_id == agent) | (part.agent_name == agent)]
86
+ out = part.groupby(["agent_name", "tool_name"]).agg(calls=("tool_name", "size"), errors=("is_error", "sum"), total_s=("duration_ms", lambda x: round(x.sum()/1000, 2)), mean_ms=("duration_ms", lambda x: round(x.mean(), 1)), max_ms=("duration_ms", lambda x: round(x.max(), 1))).reset_index()
87
+ return _fmt(out.sort_values("total_s", ascending=False))
88
+
89
+
90
+ def inference_vs_tool(data_dir=None) -> str:
91
+ df = load(data_dir)
92
+ part = df[df.event_type.isin(["inference", "tool"])]
93
+ out = part.groupby(["agent_name", "event_type"]).duration_ms.sum().unstack(fill_value=0) / 1000
94
+ return _fmt(out.round(2).reset_index())
95
+
96
+
97
+ def errors(data_dir=None) -> str:
98
+ df = load(data_dir)
99
+ return _fmt(df[(df.event_type == "tool") & df.is_error][["ts", "agent_name", "tool_name", "duration_ms", "input_preview", "output_preview"]])
100
+
101
+
102
+ def turns(data_dir=None) -> str:
103
+ df = load(data_dir)
104
+ prompts = df[(df.agent_id == "main") & (df.event_type == "user_prompt")].sort_values("ts").copy()
105
+ prompts["time_to_next_prompt_s"] = (prompts.ts.shift(-1) - prompts.ts).dt.total_seconds()
106
+ prompts["prompt"] = prompts.text.str.replace(r"\s+", " ", regex=True).str[:160]
107
+ return _fmt(prompts[["ts", "time_to_next_prompt_s", "prompt"]])
108
+
109
+
110
+ def brief(data_dir=None) -> str:
111
+ out = work_dir(data_dir)
112
+ df = load(out)
113
+ metadata = load_agents(out)
114
+ agents = metadata["agents"]
115
+ inf = df[df.event_type == "inference"]
116
+ tools = df[df.event_type == "tool"]
117
+ prompts = df[(df.agent_id == "main") & (df.event_type == "user_prompt")]
118
+ start, end = df.ts.min(), df.end_ts.max()
119
+ wall_s = (end - start).total_seconds() if not df.empty else 0
120
+ token_cols = ["input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "output_tokens"]
121
+ total_tokens = int(inf[token_cols].sum().sum()) if not inf.empty else 0
122
+ total_cost = float(inf.estimated_cost_usd.sum()) if not inf.empty else 0.0
123
+ lines = [
124
+ "# Session profile",
125
+ "",
126
+ f"Provider: `{metadata.get('provider', 'unknown')}`",
127
+ f"Session: `{metadata.get('session_id', 'unknown')}`",
128
+ "",
129
+ "## At a glance",
130
+ "",
131
+ _markdown_table(
132
+ ["Wall", "Agents", "Prompts", "Tool calls", "Errors", "Tokens", "Est. cost"],
133
+ [[_duration(wall_s), len(agents), len(prompts), len(tools), int(tools.is_error.sum()) if not tools.empty else 0, f"{total_tokens:,}", f"${total_cost:.4f}"]],
134
+ ),
135
+ "",
136
+ ]
137
+
138
+ highlights = []
139
+ if not tools.empty:
140
+ slow = tools.nlargest(1, "duration_ms").iloc[0]
141
+ highlights.append(f"Longest tool call: `{slow.tool_name}` on `{slow.agent_name}` took {_duration(slow.duration_ms / 1000)}.")
142
+ if not inf.empty:
143
+ by_agent = inf.groupby("agent_name").estimated_cost_usd.sum().sort_values(ascending=False)
144
+ if not by_agent.empty:
145
+ highlights.append(f"Highest estimated cost: `{by_agent.index[0]}` at ${by_agent.iloc[0]:.4f}.")
146
+ if not tools.empty and tools.is_error.any():
147
+ failed = tools[tools.is_error].groupby("tool_name").size().sort_values(ascending=False)
148
+ highlights.append(f"Tool failures: {int(tools.is_error.sum())}, led by `{failed.index[0]}`.")
149
+ if not highlights:
150
+ highlights.append("No failed tool calls were recorded.")
151
+ lines.extend(["## What stands out", "", *[f"- {item}" for item in highlights], ""])
152
+
153
+ toc_path = out / "toc.json"
154
+ if toc_path.exists():
155
+ import json
156
+
157
+ toc = json.loads(toc_path.read_text())
158
+ rows = []
159
+ for phase in toc.get("phases", []):
160
+ try:
161
+ phase_start = pd.to_datetime(phase["start_ts"], utc=True)
162
+ phase_end = pd.to_datetime(phase["end_ts"], utc=True)
163
+ except (KeyError, ValueError):
164
+ continue
165
+ rows.append([
166
+ phase.get("title", "Phase"),
167
+ _duration((phase_end - phase_start).total_seconds()),
168
+ phase.get("summary", ""),
169
+ ])
170
+ if rows:
171
+ lines.extend(["## Storyline", "", _markdown_table(["Phase", "Duration", "Summary"], rows), ""])
172
+ elif not prompts.empty:
173
+ prompt_rows = []
174
+ for _, row in prompts.sort_values("ts").head(6).iterrows():
175
+ prompt_rows.append([row.ts.isoformat(), " ".join(str(row.text).split())[:120]])
176
+ lines.extend(["## Prompt trail", "", _markdown_table(["Time", "Prompt"], prompt_rows), ""])
177
+
178
+ score_rows = []
179
+ for agent in agents:
180
+ part = df[df.agent_id == agent["id"]]
181
+ agent_inf = part[part.event_type == "inference"]
182
+ agent_tools = part[part.event_type == "tool"]
183
+ start = pd.to_datetime(agent.get("start_ts"), utc=True)
184
+ end = pd.to_datetime(agent.get("end_ts"), utc=True)
185
+ agent_wall_s = (end - start).total_seconds() if not pd.isna(start) and not pd.isna(end) else 0
186
+ score_rows.append([
187
+ agent["name"],
188
+ agent["agent_type"],
189
+ _duration(agent_wall_s),
190
+ _duration(agent_inf.duration_ms.sum() / 1000),
191
+ _duration(agent_tools.duration_ms.sum() / 1000),
192
+ f"{int(agent_inf[token_cols].sum().sum()):,}" if not agent_inf.empty else "0",
193
+ f"${agent_inf.estimated_cost_usd.sum():.4f}" if not agent_inf.empty else "$0.0000",
194
+ ])
195
+ lines.extend(["## Agent scoreboard", "", _markdown_table(["Agent", "Role", "Wall", "Inference", "Tools", "Tokens", "Est. cost"], score_rows), ""])
196
+ lines.append("Costs are estimates from public reference rates, not billing data.")
197
+ result = "\n".join(lines) + "\n"
198
+ (out / "brief.md").write_text(result)
199
+ return result
200
+
201
+
202
+ def timeline(moment, window=120, data_dir=None) -> str:
203
+ df = load(data_dir)
204
+ center = pd.to_datetime(moment, utc=True)
205
+ start, end = center - pd.Timedelta(seconds=window), center + pd.Timedelta(seconds=window)
206
+ part = df[(df.ts <= end) & (df.end_ts >= start) & df.event_type.isin(["user_prompt", "inference", "tool"])]
207
+ return _fmt(part[["ts", "end_ts", "agent_name", "event_type", "tool_name", "duration_ms", "is_error", "input_preview"]])
208
+
209
+
210
+ def events_query(data_dir=None, agent=None, tool=None, grep=None, since=None, n=30, long=False) -> str:
211
+ df = load(data_dir)
212
+ if agent:
213
+ df = df[(df.agent_id == agent) | (df.agent_name == agent)]
214
+ if tool:
215
+ df = df[df.tool_name == tool]
216
+ if since:
217
+ df = df[df.ts >= pd.to_datetime(since, utc=True)]
218
+ if grep:
219
+ searchable = df[["text", "input_preview", "output_preview", "tool_name"]].fillna("").agg(" ".join, axis=1)
220
+ df = df[searchable.str.contains(grep, case=False, regex=False)]
221
+ cols = ["ts", "agent_name", "event_type", "tool_name", "duration_ms", "is_error", "text", "input_preview", "output_preview"] if long else ["ts", "agent_name", "event_type", "tool_name", "duration_ms", "is_error", "text"]
222
+ return _fmt(df.head(n)[cols])
223
+
224
+
225
+ def review(data_dir=None) -> str:
226
+ df = load(data_dir)
227
+ tools = df[df.event_type == "tool"]
228
+ inf = df[df.event_type == "inference"]
229
+ total_s = (tools.duration_ms.sum() + inf.duration_ms.sum()) / 1000
230
+ lines = ["# Session improvement review", "", "Use these observations to adjust the next agent run:", ""]
231
+ if tools.is_error.any():
232
+ lines.append(f"- Investigate {int(tools.is_error.sum())} failed tool calls; repeated failures usually indicate missing preflight checks.")
233
+ if not tools.empty:
234
+ slow = tools.nlargest(3, "duration_ms")
235
+ for _, row in slow.iterrows():
236
+ lines.append(f"- Slow path: {row.tool_name} on {row.agent_name} took {row.duration_ms/1000:.1f}s. Consider batching, narrowing, or running independent work concurrently.")
237
+ tool_share = tools.duration_ms.sum() / (tools.duration_ms.sum() + inf.duration_ms.sum()) if total_s else 0
238
+ lines.append(f"- Observed summed active time was {total_s:.1f}s ({tool_share:.0%} tools, {1-tool_share:.0%} inference). Overlapping spans make this larger than wall time.")
239
+ lines.append(f"- Estimated cost was ${inf.estimated_cost_usd.sum():.4f}; this uses provider-specific public API reference rates, not subscription or billing data.")
240
+ if len(load_agents(data_dir)["agents"]) == 1:
241
+ lines.append("- No subagents were recorded. For separable research or verification work, delegation may reduce wall time.")
242
+ else:
243
+ lines.append("- Compare subagent outputs and runtimes before reusing the same delegation shape; keep assignments bounded and independently verifiable.")
244
+ result = "\n".join(lines) + "\n"
245
+ (work_dir(data_dir) / "review.md").write_text(result)
246
+ return result
@@ -0,0 +1,77 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ from datetime import datetime
6
+ from pathlib import Path
7
+
8
+ from . import analyses
9
+ from .dataset import parse, work_dir
10
+ from .discover import codex_transcripts, find_sessions, is_codex_subagent, main_transcripts, project_dirs, resolve_session, session_info
11
+ from .toc import create as create_toc
12
+ from .trace import create as create_trace, open_ui
13
+
14
+
15
+ def parser() -> argparse.ArgumentParser:
16
+ p = argparse.ArgumentParser(prog="sp", description="Profile Claude Code and Codex JSONL sessions")
17
+ p.add_argument("--data-dir", help="Parsed work directory (also SESSION_DATA_DIR)")
18
+ sub = p.add_subparsers(dest="command", required=True)
19
+ sub.add_parser("projects")
20
+ x = sub.add_parser("list"); x.add_argument("--project-dir", type=Path); x.add_argument("--provider", choices=["claude", "codex"]); x.add_argument("--n", type=int, default=20)
21
+ x = sub.add_parser("find"); x.add_argument("prefix")
22
+ x = sub.add_parser("info"); x.add_argument("session")
23
+ x = sub.add_parser("parse"); x.add_argument("session"); x.add_argument("--out", type=Path)
24
+ sub.add_parser("agent-summary"); sub.add_parser("costs")
25
+ x = sub.add_parser("slowest-tools"); x.add_argument("--n", type=int, default=20); x.add_argument("--agent")
26
+ x = sub.add_parser("tool-breakdown"); x.add_argument("--agent")
27
+ sub.add_parser("inference-vs-tool"); sub.add_parser("errors"); sub.add_parser("turns"); sub.add_parser("brief"); sub.add_parser("review")
28
+ x = sub.add_parser("timeline"); x.add_argument("moment"); x.add_argument("--window", type=int, default=120)
29
+ x = sub.add_parser("events"); x.add_argument("--agent"); x.add_argument("--tool"); x.add_argument("--grep"); x.add_argument("--since"); x.add_argument("--n", type=int, default=30); x.add_argument("--long", action="store_true")
30
+ x = sub.add_parser("toc"); x.add_argument("--dry-run", action="store_true"); x.add_argument("--engine", choices=["auto", "claude", "codex"], default="auto"); x.add_argument("--model")
31
+ sub.add_parser("trace"); sub.add_parser("open"); sub.add_parser("where")
32
+ return p
33
+
34
+
35
+ def main() -> None:
36
+ args = parser().parse_args(); data = args.data_dir
37
+ if args.command == "projects":
38
+ for path in project_dirs(): print(datetime.fromtimestamp(path.stat().st_mtime).isoformat(timespec="seconds"), path)
39
+ elif args.command == "list":
40
+ if args.project_dir:
41
+ paths = main_transcripts(args.project_dir.expanduser())
42
+ else:
43
+ paths = []
44
+ if args.provider != "codex":
45
+ for d in project_dirs():
46
+ if ".claude/projects" in str(d): paths.extend(main_transcripts(d))
47
+ if args.provider != "claude": paths.extend(p for p in codex_transcripts() if not is_codex_subagent(p))
48
+ paths = sorted(paths, key=lambda path: path.stat().st_mtime, reverse=True)
49
+ shown = 0
50
+ for path in paths:
51
+ info = session_info(path)
52
+ span = f"{info['span_seconds']:.0f}s" if info["span_seconds"] is not None else "?s"; print(f"{info['provider']:<6} {info['session_id']} {info['bytes']/1024:.1f} KiB {len(info['subagents'])} subagents {span} {info['first_prompt']}")
53
+ shown += 1
54
+ if shown >= args.n: break
55
+ elif args.command == "find":
56
+ for path in find_sessions(args.prefix): print(path)
57
+ elif args.command == "info": print(json.dumps(session_info(resolve_session(args.session)), indent=2))
58
+ elif args.command == "parse": print(parse(args.session, args.out))
59
+ elif args.command == "agent-summary": print(analyses.agent_summary(data))
60
+ elif args.command == "costs": print(analyses.costs(data))
61
+ elif args.command == "slowest-tools": print(analyses.slowest_tools(data, args.n, args.agent))
62
+ elif args.command == "tool-breakdown": print(analyses.tool_breakdown(data, args.agent))
63
+ elif args.command == "inference-vs-tool": print(analyses.inference_vs_tool(data))
64
+ elif args.command == "errors": print(analyses.errors(data))
65
+ elif args.command == "turns": print(analyses.turns(data))
66
+ elif args.command == "brief": print(analyses.brief(data))
67
+ elif args.command == "timeline": print(analyses.timeline(args.moment, args.window, data))
68
+ elif args.command == "events": print(analyses.events_query(data, args.agent, args.tool, args.grep, args.since, args.n, args.long))
69
+ elif args.command == "review": print(analyses.review(data))
70
+ elif args.command == "toc": print(create_toc(data, args.dry_run, args.model, args.engine))
71
+ elif args.command == "trace": print(create_trace(data))
72
+ elif args.command == "open": print(open_ui())
73
+ elif args.command == "where": print(work_dir(data))
74
+
75
+
76
+ if __name__ == "__main__":
77
+ main()