diff --git a/README.md b/README.md index db1335d..2a9e39e 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,38 @@ week, with the share of sessions in plan mode and auto-approve; how many session surface (CLI, desktop, IDE, SDK, and the Copilot and Cursor surfaces with `--source`). PRs are the `pr-link` entries Claude Code records when it creates a PR; prompts exclude agent-to-agent messages and -task notifications. `--json` returns the per-session stats and phase. +task notifications. `--json` returns the per-session stats, phase and intent. + +It closes with **session intent**, what each session was for: bug fix, new +development, refactoring, tests, planning & design, docs & content, ops & +config, review, research & analysis, or other. The intent comes from rules, +not a model, and the strongest signal wins: + +1. conventional commit messages and PR titles the session wrote (`fix:`, `feat:`, `refactor:`, `docs:`, `chore:`, ...), +2. review commands (`/code-review`, `/review`, `/security-review`), +3. words in the session title, then the git branch prefix (`fix/`, `feature/`, ...), then the first prompt, +4. what it changed: only plan / spec documents, docs, tests or CI and config files; mostly new files; or nothing at all (research). + +Each session in `--json` carries its `intent` and the `intentEvidence` that +decided it. Prompts themselves are not in the report. Cursor logs no git +branch. + +`ccreports intents` lists every session with its intent and evidence. With +`-j` it also prints the signals (title, every prompt typed in the session, +branches, commit messages, changed files per kind), so that a model can +classify the sessions as a whole; `--intents ` then applies its answer to any report, +replacing the rules' guess: + +```sh +node bin/ccreports.mjs intents -j -o signals.json # signals per session +# ... classify, writing {"": "bugfix", ...} to intents.json +node bin/ccreports.mjs adoption --intents intents.json # evidence: claude +``` + +The `/ccreports:report` skill does this itself when you ask what your +sessions were for: Claude reads all prompts of each session, classifies it, +and runs the report with `--intents`. The rules themselves only look at the +first prompt. ```sh node bin/ccreports.mjs adoption -s 20261001 -f html -o adoption.html @@ -205,6 +236,7 @@ byte order mark so Excel shows non-ASCII characters correctly. | `-x, --shell` | Programs run by Bash / PowerShell calls, grouped by purpose: a table per shell in the summary, `[bash]` / `[powershell]` sub-rows in period reports | | `-a, --adoption` | Append the adoption assessment to the report | | `-r, --recommendations` | Append best-practice recommendations to the report | +| `--intents ` | Session intents assigned elsewhere (`{"": ""}`), replacing the rules' guess | | `-n, --top ` | Rows per ranked list (`0` = all) | | `-f, --format ` | `table` (default), `json`, `csv`, `markdown` / `md`, `html` | | `-j, --json` | Same as `--format json` | diff --git a/bin/ccreports.mjs b/bin/ccreports.mjs index 9a427e1..9f4e052 100644 --- a/bin/ccreports.mjs +++ b/bin/ccreports.mjs @@ -1,5 +1,5 @@ #!/usr/bin/env node -import { writeFile, mkdtemp } from 'node:fs/promises'; +import { writeFile, mkdtemp, readFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { parseArgs } from 'node:util'; @@ -11,7 +11,8 @@ import { aggregate, parseDateArg, toJSON, REPORTS } from '../src/aggregate.mjs'; import { buildPeriodReport, buildSummary } from '../src/report.mjs'; import { FORMATS, toTerminal, toMarkdown, toCsv, toHtml } from '../src/formats.mjs'; import { loadPricingTable, createPricer } from '../src/pricing.mjs'; -import { buildAdoption, PHASES, AUTONOMY_THRESHOLD } from '../src/adoption.mjs'; +import { buildAdoption, buildIntents, parseIntents, PHASES, AUTONOMY_THRESHOLD } from '../src/adoption.mjs'; +import { INTENTS } from '../src/intent.mjs'; import { buildRecommendations } from '../src/recommendations.mjs'; import { openFile, FORMAT_EXTENSIONS } from '../src/open.mjs'; import { loadPlugins, loadCopilotPlugins, pluginRows, pluginsJSON } from '../src/plugins.mjs'; @@ -30,6 +31,9 @@ Reports: session One row per session project One row per project (working directory) adoption Adoption assessment: sessions by phase (chat, assisted, agent-first, multi-agent) + and by intent (bug fix, new development, refactoring, research, ...) + intents Each session's intent and the evidence for it; in JSON also the signals + (title, first prompt, branch, commits, changed files) to classify from recommendations Best-practice recommendations based on your usage, with the cost at stake Options: @@ -42,6 +46,8 @@ Options: -x, --shell Programs run by Bash / PowerShell calls (summary tables, sub-rows in period reports) -a, --adoption Append the adoption assessment to the report -r, --recommendations Append best-practice recommendations to the report + --intents Session intents assigned elsewhere (JSON: {"": ""}), + replacing the rules' guess, e.g. classified by Claude in the skill -n, --top Rows per ranked list (default 15 summary, 5 breakdown; 0 = all) -f, --format table (default), json, csv, markdown (md) or html -j, --json Same as --format json @@ -79,6 +85,7 @@ try { shell: { type: 'boolean', short: 'x' }, adoption: { type: 'boolean', short: 'a' }, recommendations: { type: 'boolean', short: 'r' }, + intents: { type: 'string' }, top: { type: 'string', short: 'n' }, format: { type: 'string', short: 'f' }, json: { type: 'boolean', short: 'j' }, @@ -147,6 +154,23 @@ if (opts.timezone) { } } +let intents; +if (opts.intents) { + let json; + try { + json = JSON.parse((await readFile(opts.intents, 'utf8')).replace(/^/, '')); + } catch (err) { + fail(`cannot read --intents ${opts.intents}: ${err.message}`); + } + const parsedIntents = parseIntents(json); + intents = parsedIntents.intents; + if (parsedIntents.unknown.length) { + console.error( + `ccreports: ignored unknown intents in ${opts.intents}: ${[...new Set(parsedIntents.unknown)].join(', ')} (expected ${INTENTS.map((i) => i.key).join(', ')})`, + ); + } +} + const toTTY = !opts.output && !opts.open && process.stdout.isTTY; const includeSubagents = !opts['no-subagents']; @@ -223,6 +247,7 @@ const docOpts = { pricer, plugins, product, + intents, // No token usage at all (Cursor alone): reports leave out the token columns. tokens: data.usage.length > 0, }; @@ -230,6 +255,7 @@ const docOpts = { // The adoption assessment and recommendations: their own reports, or appended // to another with --adoption / --recommendations. const adoption = report === 'adoption' || opts.adoption ? buildAdoption(data, docOpts) : null; +const intentDoc = report === 'intents' ? buildIntents(data, docOpts) : null; // Recommendations per product: each names its own settings and commands, and // is weighed against that product's own cost. let advice = null; @@ -264,6 +290,20 @@ if (format === 'json') { let json; if (report === 'adoption') { json = { ...meta, ...adoptionJson }; + } else if (report === 'intents') { + json = { + ...meta, + intents: INTENTS, + sessions: intentDoc.stats.map((s) => ({ + id: s.id, + title: s.title, + project: s.project, + firstActivity: s.firstActivity, + intent: s.intent, + evidence: s.intentEvidence, + signals: s.intentSignals, + })), + }; } else if (report === 'recommendations') { json = { ...meta, recommendations: advice.items }; } else if (report === 'summary') { @@ -277,7 +317,7 @@ if (format === 'json') { } else if (result.totals.sessions.size === 0) { fail('no usage found for the selected filters.'); } else { - const own = { adoption, recommendations: advice }[report]; + const own = { adoption, intents: intentDoc, recommendations: advice }[report]; let doc = own ?? (report === 'summary' ? buildSummary(result, docOpts) : buildPeriodReport(report, result, docOpts)); // Appended parts: their summary lines on top, their sections after the report's own. for (const part of [adoption, advice]) { diff --git a/skills/report/SKILL.md b/skills/report/SKILL.md index 87a859a..f7bd16e 100644 --- a/skills/report/SKILL.md +++ b/skills/report/SKILL.md @@ -1,7 +1,7 @@ --- name: report -description: Report the user's own Claude Code usage from their local session logs (~/.claude/projects), their GitHub Copilot CLI / VS Code agent usage (~/.copilot/session-state), and their Cursor CLI / editor agent activity (~/.cursor/projects) - tokens and estimated API cost, tool / skill / slash-command / hook / subagent counts and the tokens each consumed, context size, sessions, an adoption assessment (chat, assisted, agent-first, multi-agent) and best-practice recommendations backed by their data. Use when the user asks how much they used or spent, which tools or skills they use most, what consumes their tokens, how their usage trends per day/week/month/project/session, how far they have adopted agentic workflows, how to use Claude Code, Copilot or Cursor more effectively or cheaply, how their usage of these tools compares, or wants such a report as a table, Markdown, CSV, JSON or HTML page. -argument-hint: "[summary|daily|weekly|monthly|session|project|adoption|recommendations] [options]" +description: Report the user's own Claude Code usage from their local session logs (~/.claude/projects), their GitHub Copilot CLI / VS Code agent usage (~/.copilot/session-state), and their Cursor CLI / editor agent activity (~/.cursor/projects) - tokens and estimated API cost, tool / skill / slash-command / hook / subagent counts and the tokens each consumed, context size, sessions, an adoption assessment (chat, assisted, agent-first, multi-agent), what each session was for (bug fix, new development, refactoring, research, ...) and best-practice recommendations backed by their data. Use when the user asks how much they used or spent, which tools or skills they use most, what consumes their tokens, how their usage trends per day/week/month/project/session, how far they have adopted agentic workflows, what they spend their sessions on, how to use Claude Code, Copilot or Cursor more effectively or cheaply, how their usage of these tools compares, or wants such a report as a table, Markdown, CSV, JSON or HTML page. +argument-hint: "[summary|daily|weekly|monthly|session|project|adoption|intents|recommendations] [options]" --- # ccreports @@ -31,6 +31,7 @@ the request: | what each tool, skill or subagent consumed | add `-b` (breakdown) to a period report | | which programs / CLI commands Bash and PowerShell calls ran | add `-x` (`--shell`) to `summary` or a period report | | an adoption / maturity assessment | `adoption`, or `-a` to add it to another report | +| what sessions were for (bug fixes, features, refactoring, research, ...) | `adoption` (its Session intent section), or `intents` per session; classify them yourself, see [Classifying session intent](#classifying-session-intent) | | advice: how to use Claude Code better or cheaper | `recommendations`, or `-r` to add it to another report | | the same for GitHub Copilot (CLI or VS Code agent) | any of the above with `--source copilot` | | the same for Cursor (CLI or editor agent; activity only, no tokens or cost) | any of the above with `--source cursor` | @@ -53,6 +54,49 @@ Useful options: `-s/--since` and `-u/--until` (`YYYYMMDD`), `-p/--project not open files unless `--open` is passed. - If Node.js is missing or older than 20, say so; don't install it for them. +## Classifying session intent + +ccreports guesses each session's intent with rules. When the user asks what +their sessions were for, or asks for intent to be classified by Claude, classify +the sessions yourself instead; it is more accurate than the rules. For an +adoption report where intent is not the question, the rules are enough. + +1. Export the signals, with the same `--source`, `-s`, `-u` and `-p` options as + the report, to a temporary file (your scratchpad directory if you have one): + + ```sh + node "/../../bin/ccreports.mjs" intents -j --no-open -o "/ccreports-intent-signals.json" [options] + ``` + + With more than about 200 sessions, or a file over about 300 KB (every + prompt is in it), narrow the range with `--since` or ask the user which + period to classify. +2. Read the file (in parts if it is large). `intents` lists the keys to choose + from. Each session has a `title` and `signals`: `prompts` (every prompt the + user typed, in order, each cut at 500 characters), `branches`, + `workMessages` (commit messages and PR titles it wrote), `commands`, + `files` (changed files per kind: code, tests, docs, ops, plans), `newFiles`, + `edits`, and the rules' guess (`ruleIntent`, `ruleEvidence`). Read all of a + session's prompts and pick one key for its main purpose: + - Judge the session as a whole: the first prompt sets the task, but later + prompts show where the work went (a question that turns into "now build + it" is new development; a feature session that becomes chasing an error + is a bug fix when most prompts are about that error). + - Commits and PR titles are the strongest evidence, then the prompts and + the title, then the branch (a long-lived `feature/` branch also hosts + fixes). + - A session that changed nothing is research, even if it talks about a bug. + - Keep `ruleIntent` when the signals don't point elsewhere; use `other` only + when nothing hints at a purpose. +3. Write `{ "": "", ... }` to `/ccreports-intents.json`. +4. Run the report the user asked for with `--intents "/ccreports-intents.json"`, + e.g. `adoption --intents …` or `intents --intents …` (not `-j`, which would + print the prompts again). The section notes how many sessions you classified; + their evidence reads `claude`. + +The signals hold the user's own prompts from their local logs. Use them only to +classify; don't quote them in the report or the answer unless asked. + ## Explaining the results Lead with what stands out (biggest cost driver, trend, phase), not a restatement @@ -65,6 +109,10 @@ of every table. Keep these caveats in mind and mention them when relevant: - Context size counts the main conversation only. - The adoption phases are per session, from the user's own logs; PRs are those Claude Code created. +- Session intent (bug fix, new development, refactoring, ...) comes from rules + over commit messages, titles, branch names, the first prompt and the files + changed, unless you classified it. Either way it is a best guess per session; + `intents` shows the evidence. With `--source copilot`, `cursor` or `all`, also keep in mind: diff --git a/src/adoption.mjs b/src/adoption.mjs index c971cd0..dc4d9ce 100644 --- a/src/adoption.mjs +++ b/src/adoption.mjs @@ -3,7 +3,8 @@ // compared on PRs, tokens and cost. Built as a report document (src/report.mjs). import { aggregate } from './aggregate.mjs'; -import { money, share, average } from './report.mjs'; +import { money, share, average, sessionName } from './report.mjs'; +import { INTENTS, classifyIntent, fileKind } from './intent.mjs'; export const PHASES = [ { key: 'chat', title: 'Chat', description: 'Questions and exploration, no code changes' }, @@ -15,6 +16,7 @@ export const PHASES = [ // Tool calls per prompt you typed at which a session counts as agent-first. export const AUTONOMY_THRESHOLD = 10; const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit']); +const samePath = (p) => String(p).replace(/\\/g, '/').toLowerCase(); // Modes that run without asking per tool call: Claude Code's auto mode, Copilot's autopilot. const AUTO_MODES = ['auto', 'autopilot']; @@ -60,12 +62,22 @@ const FEATURES = [ ]; /** Per-session stats for the sessions active in the selected range. */ -export function sessionStats(data, { since, until, project, timeZone, pricer } = {}) { +export function sessionStats(data, { since, until, project, timeZone, pricer, intents, withSignals = false } = {}) { const { buckets } = aggregate(data, { report: 'session', since, until, project, timeZone, pricer }); const inRange = new Set(buckets.map((b) => b.key)); const counts = new Map(); + const changed = new Map(); // sessionId -> { files: Set, newFiles: Set } for (const t of data.tools) { if (!inRange.has(t.sessionId)) continue; + if (t.editFiles || t.writeFile) { + const ch = changed.get(t.sessionId) ?? { files: new Set(), newFiles: new Set() }; + for (const f of t.editFiles ?? []) ch.files.add(samePath(f)); + if (t.writeFile) { + ch.files.add(samePath(t.writeFile)); + ch.newFiles.add(samePath(t.writeFile)); + } + changed.set(t.sessionId, ch); + } const c = counts.get(t.sessionId) ?? { edits: 0, backgroundAgents: 0, worktrees: 0, agentMessages: 0, mcpCalls: 0, webCalls: 0 }; // edit / web: flags set by the GitHub Copilot loader, whose tools have other names. if (EDIT_TOOLS.has(t.name) || t.edit) c.edits += 1; @@ -76,6 +88,11 @@ export function sessionStats(data, { since, until, project, timeZone, pricer } = if (t.name === 'WebSearch' || t.name === 'WebFetch' || t.web) c.webCalls += 1; counts.set(t.sessionId, c); } + const commandsBySession = new Map(); + for (const c of data.commands ?? []) { + if (!inRange.has(c.sessionId)) continue; + commandsBySession.set(c.sessionId, [...(commandsBySession.get(c.sessionId) ?? []), c.name]); + } return buckets.map((b) => { const info = data.sessions.get(b.key); const s = { @@ -101,6 +118,32 @@ export function sessionStats(data, { since, until, project, timeZone, pricer } = }; s.toolCallsPerPrompt = s.toolCalls / Math.max(s.prompts, 1); s.phase = classify(s); + const ch = changed.get(b.key); + const files = { code: 0, tests: 0, docs: 0, ops: 0, plans: 0 }; + for (const f of ch?.files ?? []) files[fileKind(f)] += 1; + const signals = { + title: data.titles?.get(b.key) ?? null, + firstPrompt: info?.firstPrompt ?? null, + branches: [...(info?.branches ?? [])], + workMessages: [...(info?.workMessages?.values() ?? [])].flat(), + commands: commandsBySession.get(b.key) ?? [], + files, + newFiles: ch?.newFiles.size ?? 0, + edits: s.edits, + }; + const { intent, evidence } = classifyIntent(signals); + // An intent Claude assigned (--intents) replaces the rules' guess. + const assigned = intents?.get(b.key); + s.intent = assigned ?? intent; + s.intentEvidence = assigned ? ['claude'] : evidence; + if (withSignals) { + // Every prompt typed in the session, in order (resumed copies counted once). + const prompts = [...(info?.promptTexts?.values() ?? [])] + .sort((a, b) => (a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0)) + .map((p) => p.text); + const { firstPrompt, ...rest } = signals; + s.intentSignals = { ...rest, prompts: prompts.length ? prompts : firstPrompt ? [firstPrompt] : [], ruleIntent: intent, ruleEvidence: evidence }; + } return s; }); } @@ -256,10 +299,30 @@ export function buildAdoption(data, opts) { return { cells: [label, n, share(n, logged ? stats.filter(logged).length : stats.length)] }; }); + // What sessions were for: one row per intent seen, the most common first, Other last. + const byIntent = INTENTS.map((i) => ({ ...i, list: stats.filter((s) => s.intent === i.key) })) + .filter((i) => i.list.length) + .sort((a, b) => (a.key === 'other') - (b.key === 'other') || b.list.length - a.list.length); + const intentRows = byIntent.map(({ title, description, list }) => ({ + cells: [ + title, + description, + list.length, + share(list.length, stats.length), + sum(list, (s) => s.prs), + average(sum(list, (s) => s.edits), list.length), + ...(opts.tokens !== false ? [Math.round(sum(list, (s) => s.totalTokens) / list.length)] : []), + ...(showCost ? [money(sum(list, (s) => s.costUSD) / list.length)] : []), + ], + })); + const summary = assess(stats, byPhase, features); + const named = byIntent.filter((i) => i.key !== 'other').slice(0, 3); + if (named.length) summary.push(`By intent: ${named.map((i) => `${i.title.toLowerCase()} ${pct(i.list.length / stats.length)}`).join(', ')}.`); + const range = opts.since || opts.until ? ` (${opts.since ?? '…'} → ${opts.until ?? 'today'})` : ''; return { title: `${opts.product ?? 'Claude Code'} adoption${range}`, - summary: assess(stats, byPhase, features), + summary, sections: [ { title: 'Adoption cohorts', note: `sessions by how they used ${opts.product ?? 'Claude Code'}`, cards: true, columns: cohortColumns, rows: cohortRows }, { @@ -286,6 +349,83 @@ export function buildAdoption(data, opts) { columns: [left('Surface'), right('Sessions'), right('Share of sessions'), right('PRs')], rows: surfaceRows, }, + { + title: 'Session intent', + note: intentNote(stats), + columns: [ + left('Intent'), + left('What'), + right('Sessions'), + right('Share of sessions'), + right('PRs'), + right('Edits per session'), + ...(opts.tokens !== false ? [right('Tokens per session')] : []), + ...(showCost ? [right('Cost per session')] : []), + ], + rows: intentRows, + }, + ], + stats, + }; +} + +function intentNote(stats) { + const byClaude = stats.filter((s) => s.intentEvidence?.includes('claude')).length; + if (!byClaude) return 'what sessions were for, from commits, titles, branches, prompts and changed files'; + if (byClaude === stats.length) return 'what sessions were for, as classified by Claude'; + return `what sessions were for: ${byClaude} of ${stats.length} classified by Claude, the rest by rules`; +} + +/** + * Intents assigned outside ccreports (by Claude, through the skill), from JSON: + * { "": "" }, [{ id, intent }] or { sessions: [{ id, intent }] }. + * Intents are keys or titles from INTENTS. Returns { intents: Map, unknown: [values] }. + */ +export function parseIntents(json) { + const byName = new Map(INTENTS.flatMap((i) => [[i.key, i.key], [i.title.toLowerCase(), i.key]])); + const list = Array.isArray(json) + ? json + : Array.isArray(json?.sessions) + ? json.sessions + : Object.entries(json ?? {}).map(([id, intent]) => ({ id, intent })); + const intents = new Map(); + const unknown = []; + for (const { id, intent } of list) { + const key = byName.get(String(intent ?? '').trim().toLowerCase()); + if (id && key) intents.set(id, key); + else if (id) unknown.push(String(intent)); + } + return { intents, unknown }; +} + +/** + * The `intents` report: one row per session with its intent and the evidence. + * In JSON (see bin) each session also carries the signals, for Claude to classify. + */ +export function buildIntents(data, opts) { + const stats = sessionStats(data, { ...opts, withSignals: true }); + const title = (key) => INTENTS.find((i) => i.key === key)?.title ?? key; + const sorted = [...stats].sort((a, b) => (a.firstActivity < b.firstActivity ? -1 : 1)); + const range = opts.since || opts.until ? ` (${opts.since ?? '…'} → ${opts.until ?? 'today'})` : ''; + return { + title: `${opts.product ?? 'Claude Code'} session intent${range}`, + summary: [], + sections: [ + { + title: 'Sessions', + note: intentNote(stats), + columns: [left('Session'), left('Started'), left('Intent'), left('Evidence'), right('Edits'), right('PRs')], + rows: sorted.map((s) => ({ + cells: [ + sessionName(s.title ?? s.id.slice(0, 8)), + String(s.firstActivity).slice(0, 10), + title(s.intent), + s.intentEvidence.join(', '), + s.edits, + s.prs, + ], + })), + }, ], stats, }; diff --git a/src/aggregate.mjs b/src/aggregate.mjs index 07f9263..77966fa 100644 --- a/src/aggregate.mjs +++ b/src/aggregate.mjs @@ -5,7 +5,7 @@ import { commandCategory } from './shell.mjs'; const QUIET_CATEGORY = 'Change directory'; -export const REPORTS = ['summary', 'daily', 'weekly', 'monthly', 'session', 'project', 'adoption', 'recommendations']; +export const REPORTS = ['summary', 'daily', 'weekly', 'monthly', 'session', 'project', 'adoption', 'intents', 'recommendations']; export function dateFormatter(timeZone) { // en-CA formats as YYYY-MM-DD. diff --git a/src/copilot.mjs b/src/copilot.mjs index d99efa5..72ce3e2 100644 --- a/src/copilot.mjs +++ b/src/copilot.mjs @@ -22,7 +22,8 @@ import { homedir } from 'node:os'; import path from 'node:path'; import { FileAttribution, createCalibration, charsPerToken, resolveTokens } from './attribution.mjs'; import { shellCommands } from './shell.mjs'; -import { errorLine, normalizeSkillName } from './loader.mjs'; +import { errorLine, normalizeSkillName, patchFiles } from './loader.mjs'; +import { promptText, workMessages } from './intent.mjs'; const NANO_AIU_PER_USD = 1e11; const SHELLS = { powershell: 'PowerShell', bash: 'Bash' }; @@ -132,6 +133,10 @@ export async function loadCopilotRecords({ dirs = defaultCopilotDirs(), includeS prs: new Set(), compactions: new Set(), modes: new Set(), // interactive, plan, autopilot + branches: new Set(), + firstPrompt: null, + promptTexts: new Map(), // event id -> { timestamp, text } + workMessages: new Map(), // toolCallId -> commit messages / PR titles }; sessions.set(id, info); @@ -172,6 +177,7 @@ export async function loadCopilotRecords({ dirs = defaultCopilotDirs(), includeS case 'session.start': case 'session.resume': project ??= data.context?.cwd ?? null; + if (data.context?.branch && data.context.branch !== 'HEAD') info.branches.add(data.context.branch); break; case 'session.mode_changed': @@ -244,6 +250,18 @@ export async function loadCopilotRecords({ dirs = defaultCopilotDirs(), includeS if (SHELLS[raw] && typeof args.command === 'string') { const names = shellCommands(args.command, SHELLS[raw]); tool.shellCommands = SHELLS[raw] === 'PowerShell' ? names.map(psName) : names; + const messages = workMessages(args.command); + if (messages.length) info.workMessages.set(data.toolCallId ?? d.id, messages); + } + // Changed files, for the session intent. create/write make new files. + if (raw === 'create' || raw === 'write' || (raw === 'str_replace_editor' && args.command === 'create')) { + if (args.path) tool.writeFile = args.path; + } else if ((raw === 'edit' || raw === 'str_replace_editor') && args.command !== 'view') { + if (args.path) tool.editFiles = [args.path]; + } else if (raw === 'apply_patch') { + const { updated, added } = patchFiles(typeof args === 'string' ? args : (args.input ?? args.patch)); + if (updated.length) tool.editFiles = updated; + if (added.length) tool.writeFile = added[0]; } tools.set(data.toolCallId ?? d.id, tool); if (!d.agentId && createsPr(raw, data, args)) prCalls.add(data.toolCallId); @@ -288,6 +306,11 @@ export async function loadCopilotRecords({ dirs = defaultCopilotDirs(), includeS const content = String(data.content ?? ''); attributionOf(null).human(); info.prompts.add(d.id ?? d.timestamp); + const said = promptText(content); + if (said) { + info.firstPrompt ??= said; + info.promptTexts.set(d.id ?? d.timestamp, { timestamp: d.timestamp, text: said }); + } const m = /^\/([\w:.-]+)/.exec(content.trim()); pendingCommand = null; if (m) { diff --git a/src/cursor.mjs b/src/cursor.mjs index 6412503..82ef6f8 100644 --- a/src/cursor.mjs +++ b/src/cursor.mjs @@ -28,11 +28,13 @@ import { createInterface } from 'node:readline'; import { homedir } from 'node:os'; import path from 'node:path'; import { shellCommands } from './shell.mjs'; -import { normalizeSkillName } from './loader.mjs'; +import { normalizeSkillName, patchFiles } from './loader.mjs'; +import { promptText, workMessages } from './intent.mjs'; import { readCursorState } from './cursor-state.mjs'; const EDIT_TOOLS = new Set(['Write', 'Edit', 'StrReplace', 'MultiEdit', 'Delete', 'ApplyPatch', 'EditNotebook']); const WEB_TOOLS = new Set(['WebSearch', 'WebFetch']); +const FILE_EDIT_TOOLS = new Set(['Edit', 'StrReplace', 'MultiEdit', 'EditNotebook']); const AGENT_TOOLS = new Set(['Task', 'Agent']); // Cursor's Shell tool runs the platform's shell. const SHELL = process.platform === 'win32' ? 'PowerShell' : 'Bash'; @@ -199,6 +201,10 @@ export async function loadCursorRecords({ dirs = defaultCursorDirs(), userDirs = prs: new Set(), compactions: new Set(), modes: new Set(), + branches: new Set(), // Cursor logs none + firstPrompt: null, + promptTexts: new Map(), // turn -> { timestamp, text } + workMessages: new Map(), // tool call -> commit messages / PR titles }; sessions.set(id, info); @@ -224,6 +230,11 @@ export async function loadCursorRecords({ dirs = defaultCursorDirs(), userDirs = turn += 1; timestamp = parsePromptTime(/([^<]+)<\/timestamp>/.exec(text)?.[1]) ?? timestamp; info.prompts.add(turn); + const said = promptText(query[1]); + if (said) { + info.firstPrompt ??= said; + info.promptTexts.set(turn, { timestamp: timestamp ?? mtime, text: said }); + } events.push(base()); pendingCommand = null; const cmd = /^\/([\w:.-]+)/.exec(query[1]); @@ -257,6 +268,18 @@ export async function loadCursorRecords({ dirs = defaultCursorDirs(), userDirs = if (name === 'Shell' && typeof input.command === 'string') { const names = shellCommands(input.command, SHELL); tool.shellCommands = SHELL === 'PowerShell' ? names.map(psName) : names; + const messages = workMessages(input.command); + if (messages.length) info.workMessages.set(c.id ?? `${turn}:${tools.length}`, messages); + } + // Changed files, for the session intent. + if (name === 'Write' && (input.path ?? input.file_path)) tool.writeFile = input.path ?? input.file_path; + if (FILE_EDIT_TOOLS.has(name) && (input.path ?? input.file_path ?? input.target_notebook)) { + tool.editFiles = [input.path ?? input.file_path ?? input.target_notebook]; + } + if (name === 'ApplyPatch') { + const { updated, added } = patchFiles(Object.values(input).filter((v) => typeof v === 'string').join('\n')); + if (updated.length) tool.editFiles = updated; + if (added.length) tool.writeFile = added[0]; } if (name === 'SwitchMode' && input.target_mode_id) info.modes.add(input.target_mode_id); if (name === 'CreatePlan') info.modes.add('plan'); diff --git a/src/intent.mjs b/src/intent.mjs new file mode 100644 index 0000000..0320a63 --- /dev/null +++ b/src/intent.mjs @@ -0,0 +1,138 @@ +// Session intent: what a session was for (bug fix, new development, +// refactoring, ...), from rules over what the logs say about it. Signals, from +// strong to weak: conventional commit and PR titles the session wrote, review +// commands, the session title, the git branch, the first prompt, and what the +// session did (which files it changed, or that it changed none). + +export const INTENTS = [ + { key: 'bugfix', title: 'Bug fix', description: 'Fixing something that is broken' }, + { key: 'feature', title: 'New development', description: 'Adding features, pages, apps' }, + { key: 'refactor', title: 'Refactoring', description: 'Restructuring, renaming, upgrading' }, + { key: 'tests', title: 'Tests', description: 'Writing or fixing tests' }, + { key: 'planning', title: 'Planning & design', description: 'Plans, specs, architecture' }, + { key: 'docs', title: 'Docs & content', description: 'READMEs, docs, articles, course material' }, + { key: 'ops', title: 'Ops & config', description: 'CI, deploys, settings, dependencies' }, + { key: 'review', title: 'Review', description: 'Reviewing code or a PR' }, + { key: 'research', title: 'Research & analysis', description: 'Questions, explanations, investigations' }, + { key: 'other', title: 'Other', description: 'No clear signal' }, +]; +// On a tie the earlier intent wins. +const ORDER = INTENTS.map((i) => i.key); + +// Words in a title, prompt or commit message. English, plus a few Dutch. +const KEYWORDS = { + bugfix: + /\b(bugs?|bugfix|fix(es|ed|ing)?|hotfix|errors?|exceptions?|crash(es|ed|ing)?|broken|breaks|fails?|failing|failed|failure|not working|(does ?n[o']t|did ?n[o']t|is ?n[o']t|wo ?n[o']t) (work|show|load|run|start|give|return)|no output|wrong|incorrect|regression|stack ?trace|debug(ging)?|repro|fout(en|melding)?|werkt niet|kapot)\b/i, + feature: + /\b(add(s|ed|ing)?|implement(s|ed|ing)?|build|new (feature|page|app|application|endpoint|command|option|screen|tool|report)|(create|write|make) (a|an) (new )?(tool|app|application|script|page|site|website|service|api|cli|bot|dashboard|component|generator|plugin|extension)|i want (an?|to have)|support for|introduce|scaffold|make me|features?|improv(e|es|ed|ing|ements?)|enhanc(e|es|ed|ing|ements?)|poc|prototype|toevoegen|bouw)\b/i, + refactor: + /\b(refactor(s|ed|ing)?|renam(e|es|ed|ing)|clean(ing)? ?up|simplif(y|ies|ied|ication)|restructur(e|ing)|extract|reorgani[sz](e|ing)|migrat(e|es|ed|ing|ion)|upgrad(e|es|ed|ing)|moderni[sz]e|dedup(licate)?|split (up|into)|consolidat(e|ing)|opschonen|herschrijf)\b/i, + tests: /\b(unit[- ]?tests?|tests?|testing|test (case|suite)s?|coverage|e2e|playwright|jest|vitest|xunit|nunit|pytest)\b/i, + planning: /\b(plan(s|ning)?(?! mode)|specs?|specification|design( doc)?|architecture|proposal|roadmap|rfc|adr|requirements|feasibility|ideas)\b/i, + docs: /\b(readme|docs|documentation|document(ed|ing)?|changelog|blog|article|course|module|slides?|presentation|tutorial|guide|write[- ]?up|explainer)\b/i, + ops: /\b(deploy(s|ed|ing|ment)?|pipelines?|ci|cd|github actions|workflows?|docker(file)?|kubernetes|k8s|helm|terraform|bicep|infra(structure)?|config(ure|uration)?|settings|install(ation)?|dependenc(y|ies)|packages?|publish(ing)?|release|version bump|permissions?|hooks?)\b/i, + review: /\b(review(ing)?|code review|pull request #?\d+|pr #?\d+|feedback on)\b/i, + research: + /\b(explain|what (is|are|does|do|could)|why|how (does|do|is|can|could|would)|analy[sz](e|is|ing)|investigat(e|ion)|research|compar(e|ing|ison)|evaluat(e|ion)|verif(y|ication)|validat(e|ion)|breakdown|overview|look (at|into)|find (out|anything)|understand|due diligence|options|pros and cons|which|is (it|there)|can you find|vs\.?)\b/i, +}; + +// Conventional commit types, and git branch prefixes. +const COMMIT_TYPES = { fix: 'bugfix', hotfix: 'bugfix', feat: 'feature', feature: 'feature', refactor: 'refactor', perf: 'refactor', style: 'refactor', test: 'tests', tests: 'tests', docs: 'docs', doc: 'docs', chore: 'ops', ci: 'ops', build: 'ops', deps: 'ops' }; +const BRANCH_TYPES = { ...COMMIT_TYPES, bugfix: 'bugfix', bug: 'bugfix', features: 'feature', refactoring: 'refactor', infra: 'ops', ops: 'ops', release: 'ops' }; +const REVIEW_COMMANDS = /^\/(code-review|review|security-review|pr-review|ultrareview)\b/; + +/** The first line of the commit messages and PR titles in one shell command. */ +export function workMessages(command) { + const out = []; + const text = String(command ?? ''); + const re = /\b(?:git\s+commit\b[^\n]*?\s(?:-m|--message)|gh\s+pr\s+create\b[^\n]*?\s(?:-t|--title))[\s=]*(?:"((?:[^"\\]|\\.)*)"|'([^']*)'|@['"]\r?\n([^\r\n]*))/g; + for (const m of text.matchAll(re)) { + const body = (m[1] ?? m[2] ?? m[3] ?? '').replace(/^\$\(cat\s*<<-?\s*'?\w+'?\s*/, ''); + const line = body.split(/\r?\n|\\n/).map((l) => l.trim()).find(Boolean); + if (line) out.push(line.slice(0, 200)); + } + return out; +} + +/** + * What a prompt says, without the tags Claude Code wraps around commands and + * attachments. A bare slash command (/clear, /compact) says nothing: ''. + */ +export function promptText(text) { + const args = /([\s\S]*?)<\/command-args>/.exec(text)?.[1]; + const name = /([^<]*)<\/command-name>/.exec(text)?.[1]; + const plain = name ? `${name} ${args ?? ''}` : String(text ?? '').replace(/<[^>]+>[\s\S]*?<\/[^>]+>/g, ' '); + const clean = plain.replace(/\s+/g, ' ').trim(); + return /^\/\S+$/.test(clean) ? '' : clean.slice(0, 500); +} + +/** A changed file's kind: plans (plan / spec documents), docs, tests, ops (CI, containers, settings, manifests) or code. */ +export function fileKind(file) { + const p = String(file).replace(/\\/g, '/').toLowerCase(); + const name = p.slice(p.lastIndexOf('/') + 1); + if (/\.(md|mdx|markdown|rst|adoc|txt)$/.test(name)) { + const plan = /(^|\/)(specs?|plans?|rfcs?|adrs?|designs?)\//.test(p) || /^(plan|spec|rfc|adr|design)[-_.]/.test(name); + return plan ? 'plans' : 'docs'; + } + if (/(^|\/)(tests?|__tests__|testing)\//.test(p) || /[._-](test|tests|spec)\.[a-z]+$/.test(name) || /^test_/.test(name)) return 'tests'; + if ( + /(^|\/)\.(github|gitlab|azure|circleci|devcontainer|claude|vscode)\//.test(p) || + /\.(ya?ml|tf|tfvars|bicep|toml|ini|env|config|props|targets)$/.test(name) || + /^(dockerfile|docker-compose.*|makefile|package(-lock)?\.json|pnpm-lock\.yaml|.*\.csproj|.*\.sln|nuget\.config|settings(\.local)?\.json|tsconfig.*\.json|\.gitignore|\.gitattributes|\.npmrc|requirements.*\.txt|pyproject\.toml|go\.mod|cargo\.toml)$/.test(name) + ) { + return 'ops'; + } + return 'code'; +} + +const WEIGHTS = { commit: 4, command: 4, title: 3, branch: 2, prompt: 2, activity: 3 }; + +/** + * Places one session in an intent. `s` holds: title, firstPrompt, branches[], + * workMessages[] (commits / PR titles), commands[] (slash commands), files + * { code, tests, docs, ops, plans } (changed files per kind), newFiles, edits. + * Returns { intent, evidence } - evidence names the signals that decided it. + */ +export function classifyIntent(s) { + const score = Object.fromEntries(ORDER.map((k) => [k, 0])); + const why = Object.fromEntries(ORDER.map((k) => [k, new Set()])); + const add = (key, weight, source) => { + score[key] += weight; + why[key].add(source); + }; + const words = (text, weight, source) => { + if (!text) return; + for (const [key, re] of Object.entries(KEYWORDS)) if (re.test(text)) add(key, weight, source); + }; + + for (const msg of s.workMessages ?? []) { + const type = /^(\w+)(\([^)]*\))?!?:/.exec(msg)?.[1]?.toLowerCase(); + if (COMMIT_TYPES[type]) add(COMMIT_TYPES[type], WEIGHTS.commit, 'commit'); + else words(msg, 1, 'commit'); + } + if ((s.commands ?? []).some((c) => REVIEW_COMMANDS.test(c))) add('review', WEIGHTS.command, 'command'); + words(s.title, WEIGHTS.title, 'title'); + for (const branch of s.branches ?? []) { + const prefix = /^([\w-]+)\//.exec(branch)?.[1]?.toLowerCase(); + if (BRANCH_TYPES[prefix]) add(BRANCH_TYPES[prefix], WEIGHTS.branch, 'branch'); + } + words(s.firstPrompt, WEIGHTS.prompt, 'prompt'); + + const f = { code: 0, tests: 0, docs: 0, ops: 0, plans: 0, ...s.files }; + const changed = f.code + f.tests + f.docs + f.ops + f.plans; + if (!changed && !(s.edits > 0) && !(s.workMessages ?? []).length) { + add('research', WEIGHTS.activity, 'activity'); // read and answered, changed nothing + } else if (changed) { + // Research that ends in code changes was work on that code. + score.research /= 2; + if (f.plans === changed) add('planning', WEIGHTS.activity, 'activity'); + else if (f.docs === changed) add('docs', WEIGHTS.activity, 'activity'); + else if (f.tests === changed) add('tests', WEIGHTS.activity, 'activity'); + else if (f.ops === changed) add('ops', WEIGHTS.activity, 'activity'); + else if (s.newFiles >= 2 && s.newFiles >= changed / 2) add('feature', 1, 'activity'); + } + + const best = ORDER.reduce((a, k) => (score[k] > score[a] ? k : a), ORDER[0]); + if (score[best] <= 0) return { intent: 'other', evidence: [] }; + return { intent: best, evidence: [...why[best]] }; +} diff --git a/src/loader.mjs b/src/loader.mjs index b26abe8..6ae1c45 100644 --- a/src/loader.mjs +++ b/src/loader.mjs @@ -12,11 +12,24 @@ import { homedir } from 'node:os'; import path from 'node:path'; import { FileAttribution, createCalibration, charsPerToken, resolveTokens } from './attribution.mjs'; import { SHELL_TOOLS, shellCommands } from './shell.mjs'; +import { promptText, workMessages } from './intent.mjs'; const SKILL_CONTENT_PREFIX = 'Base directory for this skill'; const COMMAND_RE = /([^<]+)<\/command-name>/; // "Task" is the tool's name in older Claude Code versions. const AGENT_TOOLS = new Set(['Agent', 'Task']); +// Tools that change part of an existing file (Write creates or replaces one). +const FILE_EDIT_TOOLS = new Set(['Edit', 'MultiEdit', 'NotebookEdit']); +const INTERRUPT_PREFIX = '[Request interrupted by user'; + +/** The files an apply_patch-style patch changes and adds ("*** Update File: ", "*** Add File: "). */ +export function patchFiles(patch) { + const files = { updated: [], added: [] }; + for (const m of String(patch ?? '').matchAll(/^\*\*\* (Update|Add) File: (.+?)\s*$/gm)) { + files[m[1] === 'Add' ? 'added' : 'updated'].push(m[2]); + } + return files; +} async function readAgentType(transcript) { try { @@ -99,10 +112,23 @@ export async function loadRecords({ dirs = defaultProjectDirs(), includeSubagent const commands = new Map(); // user-message uuid -> record const hooks = new Map(); // attachment uuid -> record const titles = new Map(); // sessionId -> ai title - // sessionId -> { entrypoint, prompts: Set, prs: Set, compactions: Set, modes: Set } (adoption / recommendations) + // sessionId -> { entrypoint, prompts: Set, prs: Set, compactions: Set, modes: Set, + // branches: Set, firstPrompt, workMessages: Map } (adoption / recommendations) const sessions = new Map(); const sessionInfo = (id) => { - if (!sessions.has(id)) sessions.set(id, { entrypoint: null, prompts: new Set(), prs: new Set(), compactions: new Set(), modes: new Set() }); + if (!sessions.has(id)) { + sessions.set(id, { + entrypoint: null, + prompts: new Set(), + prs: new Set(), + compactions: new Set(), + modes: new Set(), + branches: new Set(), + firstPrompt: null, + promptTexts: new Map(), // prompt uuid -> { timestamp, text } + workMessages: new Map(), + }); + } return sessions.get(id); }; const sessionStart = new Map(); // sessionId -> { rank, timestamp, cwd } of its earliest record @@ -175,6 +201,7 @@ export async function loadRecords({ dirs = defaultProjectDirs(), includeSubagent agent, }; if (!base.timestamp) continue; + if (!agent && d.gitBranch && d.gitBranch !== 'HEAD') sessionInfo(base.sessionId).branches.add(d.gitBranch); // A session's project is where it started: later records follow Claude into // subfolders (and subagents into worktrees), which would split one session // over several projects. @@ -259,7 +286,13 @@ export async function loadRecords({ dirs = defaultProjectDirs(), includeSubagent if (SHELL_TOOLS.has(c.name) && typeof c.input?.command === 'string') { const names = shellCommands(c.input.command, c.name); tool.shellCommands = c.name === 'PowerShell' ? names.map(psName) : names; + const messages = workMessages(c.input.command); + if (messages.length) sessionInfo(base.sessionId).workMessages.set(id, messages); } + // Changed files, for the session intent. + const edited = FILE_EDIT_TOOLS.has(c.name) && (c.input?.file_path ?? c.input?.notebook_path); + if (edited) tool.editFiles = [edited]; + if (c.name === 'Write' && c.input?.file_path) tool.writeFile = c.input.file_path; tools.set(id, tool); attribution.toolUse(key, id, JSON.stringify(c.input ?? {}).length, c.name === 'Skill'); if (c.name === 'Skill' && c.input?.skill) { @@ -293,7 +326,14 @@ export async function loadRecords({ dirs = defaultProjectDirs(), includeSubagent // Prompts a person sent - not agent-to-agent or task notifications, not subagent tasks. const origin = d.origin?.kind ?? 'human'; if (!agent && !d.isSidechain && origin === 'human' && d.promptSource !== 'system') { - sessionInfo(base.sessionId).prompts.add(d.uuid ?? `${file}:${base.timestamp}`); + const info = sessionInfo(base.sessionId); + const promptId = d.uuid ?? `${file}:${base.timestamp}`; + info.prompts.add(promptId); + const said = text.startsWith(INTERRUPT_PREFIX) ? '' : promptText(text); + if (said) { + info.firstPrompt ??= said; + info.promptTexts.set(promptId, { timestamp: base.timestamp, text: said }); + } } } if (!agent && d.entrypoint) sessionInfo(base.sessionId).entrypoint ??= d.entrypoint; diff --git a/test/adoption.test.mjs b/test/adoption.test.mjs index 40586e4..aac8441 100644 --- a/test/adoption.test.mjs +++ b/test/adoption.test.mjs @@ -1,6 +1,6 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { classify, buildAdoption, AUTONOMY_THRESHOLD } from '../src/adoption.mjs'; +import { classify, buildAdoption, buildIntents, parseIntents, AUTONOMY_THRESHOLD } from '../src/adoption.mjs'; import { toHtml, toMarkdown } from '../src/formats.mjs'; const stats = (over) => ({ @@ -91,6 +91,56 @@ test('buildAdoption: cohorts, weekly mix, capabilities, surfaces and summary', ( assert.ok(doc.summary.some((l) => l.includes('Not used in this period') && l.includes('worktrees'))); }); +test('buildAdoption: session intent per session and as a section', () => { + const data = fixture(); + data.sessions.get('assist').branches = new Set(['fix/login']); + data.sessions.get('agent').workMessages = new Map([['t', ['feat: new page']]]); + data.sessions.get('multi').firstPrompt = 'rename the services'; + const doc = buildAdoption(data, { timeZone: 'UTC', showCost: false }); + assert.deepEqual( + Object.fromEntries(doc.stats.map((s) => [s.id, s.intent])), + { chat: 'research', assist: 'bugfix', agent: 'feature', multi: 'refactor' }, + ); + assert.deepEqual(doc.stats.find((s) => s.id === 'agent').intentEvidence, ['commit']); + const section = doc.sections.find((s) => s.title === 'Session intent'); + assert.deepEqual(section.rows.map((r) => r.cells[0]), ['Bug fix', 'New development', 'Refactoring', 'Research & analysis']); + assert.ok(doc.summary.some((l) => l.startsWith('By intent: bug fix 25%'))); +}); + +test('parseIntents accepts a map, a list or { sessions }, keys or titles', () => { + const { intents, unknown } = parseIntents({ a: 'bugfix', b: 'New development', c: 'banana' }); + assert.deepEqual([...intents], [['a', 'bugfix'], ['b', 'feature']]); + assert.deepEqual(unknown, ['banana']); + assert.deepEqual([...parseIntents([{ id: 'x', intent: 'Research & analysis' }]).intents], [['x', 'research']]); + assert.deepEqual([...parseIntents({ sessions: [{ id: 'y', intent: 'ops' }] }).intents], [['y', 'ops']]); +}); + +test('intents assigned by Claude replace the rules, in adoption and the intents report', () => { + const data = fixture(); + data.sessions.get('assist').branches = new Set(['fix/login']); + data.sessions.get('assist').firstPrompt = 'login fails'; + data.sessions.get('assist').promptTexts = new Map([ + ['p2', { timestamp: '2026-10-02T10:05:00Z', text: 'now rename it' }], + ['p1', { timestamp: '2026-10-02T10:00:00Z', text: 'login fails' }], + ]); + const intents = new Map([['assist', 'refactor']]); + const doc = buildAdoption(data, { timeZone: 'UTC', showCost: false, intents }); + const assist = doc.stats.find((s) => s.id === 'assist'); + assert.equal(assist.intent, 'refactor'); + assert.deepEqual(assist.intentEvidence, ['claude']); + assert.equal(assist.intentSignals, undefined); // signals only in the intents report + assert.match(doc.sections.find((s) => s.title === 'Session intent').note, /1 of 4 classified by Claude/); + + const report = buildIntents(data, { timeZone: 'UTC', showCost: false, intents }); + const row = report.sections[0].rows.find((r) => r.cells[2] === 'Refactoring').cells; + assert.equal(row[3], 'claude'); + const signals = report.stats.find((s) => s.id === 'assist').intentSignals; + assert.equal(signals.ruleIntent, 'bugfix'); + assert.deepEqual(signals.branches, ['fix/login']); + assert.deepEqual(signals.prompts, ['login fails', 'now rename it']); // all prompts, in order + assert.equal(signals.firstPrompt, undefined); +}); + test('adoption renders as cards in HTML and as tables in Markdown', () => { const doc = buildAdoption(fixture(), { timeZone: 'UTC', showCost: false }); const html = toHtml(doc); diff --git a/test/ccreports.test.mjs b/test/ccreports.test.mjs index e57f16a..a0afc04 100644 --- a/test/ccreports.test.mjs +++ b/test/ccreports.test.mjs @@ -88,6 +88,35 @@ test('loads and deduplicates records', async () => { } }); +test('records the signals for session intent: branch, first prompt, commits, changed files', async () => { + const root = await mkdtemp(path.join(tmpdir(), 'ccreports-')); + try { + const proj = path.join(root, '-work-app'); + await mkdir(proj, { recursive: true }); + const lines = [ + { ...user('2026-10-01T10:00:00Z', 'u1', 'the login button does not work'), gitBranch: 'fix/login' }, + { ...user('2026-10-01T10:00:01Z', 'u2', 'and the logout one'), gitBranch: 'HEAD' }, + assistant('2026-10-01T10:00:05Z', 'm1', 'r1', [ + { type: 'tool_use', id: 't1', name: 'Edit', input: { file_path: '/work/app/a.js' } }, + { type: 'tool_use', id: 't2', name: 'Write', input: { file_path: '/work/app/b.js', content: '' } }, + { type: 'tool_use', id: 't3', name: 'Bash', input: { command: 'git commit -m "fix: login button"' } }, + ]), + ]; + await writeFile(path.join(proj, 's1.jsonl'), lines.map((l) => JSON.stringify(l)).join('\n') + '\n'); + const data = await loadRecords({ dirs: [root] }); + const info = data.sessions.get('s1'); + assert.deepEqual([...info.branches], ['fix/login']); + assert.equal(info.firstPrompt, 'the login button does not work'); + assert.deepEqual([...info.promptTexts.values()].map((p) => p.text), ['the login button does not work', 'and the logout one']); + assert.deepEqual([...info.workMessages.values()], [['fix: login button']]); + const byName = Object.fromEntries(data.tools.map((t) => [t.name, t])); + assert.deepEqual(byName.Edit.editFiles, ['/work/app/a.js']); + assert.equal(byName.Write.writeFile, '/work/app/b.js'); + } finally { + await rm(root, { recursive: true, force: true }); + } +}); + test('a session keeps the project it started in', async () => { const root = await mkdtemp(path.join(tmpdir(), 'ccreports-')); try { diff --git a/test/intent.test.mjs b/test/intent.test.mjs new file mode 100644 index 0000000..9de5da4 --- /dev/null +++ b/test/intent.test.mjs @@ -0,0 +1,71 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { classifyIntent, fileKind, promptText, workMessages } from '../src/intent.mjs'; +import { patchFiles } from '../src/loader.mjs'; + +const intent = (s) => classifyIntent({ files: {}, newFiles: 0, edits: 1, ...s }).intent; + +test('workMessages reads commit messages and PR titles in any quoting', () => { + assert.deepEqual(workMessages('git add . && git commit -m "fix: keep the account" && git push'), ['fix: keep the account']); + assert.deepEqual(workMessages("git commit -q -m 'feat(ui): fullscreen view'"), ['feat(ui): fullscreen view']); + assert.deepEqual(workMessages(`git commit -m "$(cat <<'EOF'\nrefactor: split loader\n\nBody\nEOF\n)"`), ['refactor: split loader']); + assert.deepEqual(workMessages("git commit -q -m @'\nAdd working habits\n\nBody\n'@"), ['Add working habits']); + assert.deepEqual(workMessages('gh pr create --title "docs: README" --body "x"'), ['docs: README']); + assert.deepEqual(workMessages('git status; git log --oneline'), []); +}); + +test('promptText drops the tags around commands and attachments', () => { + assert.equal(promptText('/fix-bug\nlogin fails'), '/fix-bug login fails'); + assert.equal(promptText('ignore why is it slow?'), 'why is it slow?'); + assert.equal(promptText('/clear\n'), ''); + assert.equal(promptText('/compact'), ''); +}); + +test('fileKind tells plans, docs, tests, ops and code apart', () => { + assert.equal(fileKind('Z:\\repo\\.agentic\\specs\\plan-caching.md'), 'plans'); + assert.equal(fileKind('docs/plan-x.md'), 'plans'); + assert.equal(fileKind('README.md'), 'docs'); + assert.equal(fileKind('test/adoption.test.mjs'), 'tests'); + assert.equal(fileKind('src/app.spec.ts'), 'tests'); + assert.equal(fileKind('.github/workflows/ci.yml'), 'ops'); + assert.equal(fileKind('src/Api/Api.csproj'), 'ops'); + assert.equal(fileKind('src/loader.mjs'), 'code'); +}); + +test('patchFiles splits updated and added files', () => { + const patch = '*** Begin Patch\n*** Update File: C:\\src\\a.cs\r\n@@\n*** Add File: b.cs\n+z\n*** Update File: src/c.ts\n*** End Patch'; + assert.deepEqual(patchFiles(patch), { updated: ['C:\\src\\a.cs', 'src/c.ts'], added: ['b.cs'] }); + assert.deepEqual(patchFiles(undefined), { updated: [], added: [] }); +}); + +test('classifyIntent: commits outrank branches, titles and prompts', () => { + assert.equal(intent({ workMessages: ['fix: null check'], branches: ['feature/login'] }), 'bugfix'); + assert.equal(intent({ workMessages: ['refactor(loader): split'], title: 'Add a page' }), 'refactor'); + assert.equal(intent({ branches: ['feature/login'], firstPrompt: 'go on' }), 'feature'); + assert.equal(intent({ branches: ['main'], title: 'Login button does not work' }), 'bugfix'); + assert.equal(intent({ title: 'Rename the services and clean up' }), 'refactor'); + assert.equal(intent({ commands: ['/code-review'] }), 'review'); + assert.equal(intent({ firstPrompt: 'create a tool that generates an html file per repo' }), 'feature'); + assert.equal(intent({ title: 'Adoption cohorts plan mode', firstPrompt: 'add plan mode to the report' }), 'feature'); +}); + +test('classifyIntent: what the session changed', () => { + // Read and answered, nothing changed. + assert.equal(classifyIntent({ firstPrompt: 'ls', edits: 0 }).intent, 'research'); + assert.equal(intent({ firstPrompt: 'why is there a single database?', edits: 0 }), 'research'); + // Research words, but code changed: the work wins. + assert.equal(intent({ firstPrompt: 'look at the login, the button is broken', files: { code: 2 } }), 'bugfix'); + assert.equal(intent({ firstPrompt: 'go on', files: { plans: 2 } }), 'planning'); + assert.equal(intent({ firstPrompt: 'go on', files: { docs: 1 } }), 'docs'); + assert.equal(intent({ firstPrompt: 'go on', files: { tests: 3 } }), 'tests'); + assert.equal(intent({ firstPrompt: 'go on', files: { ops: 1 } }), 'ops'); + assert.equal(intent({ firstPrompt: 'go on', files: { code: 3 }, newFiles: 3 }), 'feature'); + const other = classifyIntent({ firstPrompt: 'go on', files: { code: 3 }, newFiles: 0, edits: 3 }); + assert.deepEqual(other, { intent: 'other', evidence: [] }); +}); + +test('classifyIntent names the signals that decided', () => { + const { intent: i, evidence } = classifyIntent({ workMessages: ['feat: x'], branches: ['feature/x'], files: { code: 3 }, newFiles: 3, edits: 3 }); + assert.equal(i, 'feature'); + assert.deepEqual(evidence, ['commit', 'branch', 'activity']); +});