Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/cli/src/report/evidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,41 @@ export function groupTotals(rule: {
}
return totals;
}

/**
* Non-zero counts by kind, larger first, `reviews` group before `conventions`
* on a tie.
*/
export function sourceCounts(rule: {
surfaced?: { reviewComments: number };
evidence?: Evidence;
}): Array<{ source: RuleSource; count: number }> {
const counts: Record<RuleSource, number> = {
reviews: rule.surfaced?.reviewComments ?? 0,
commits: rule.evidence?.commits ?? 0,
issues: rule.evidence?.issues ?? 0,
"agent-files": rule.evidence?.["agent-files"] ?? 0,
conventions: rule.evidence?.conventions ?? 0,
docs: rule.evidence?.docs ?? 0,
tests: rule.evidence?.tests ?? 0,
};
const order: readonly RuleSource[] = [
"reviews",
"commits",
"issues",
"agent-files",
"conventions",
"docs",
"tests",
];
return order
.filter((source) => counts[source] > 0)
.map((source) => ({ source, count: counts[source] }))
.toSorted((a, b) => {
if (b.count !== a.count) return b.count - a.count;
return (
(SOURCE_GROUPS[a.source] === "reviews" ? 0 : 1) -
(SOURCE_GROUPS[b.source] === "reviews" ? 0 : 1)
);
});
}
28 changes: 17 additions & 11 deletions packages/cli/src/report/summary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groupTotals, type SourceGroup } from "./evidence";
import { sourceCounts, type RuleSource } from "./evidence";
import type { ReportRule, ReportV1 } from "./types";

/**
Expand Down Expand Up @@ -39,19 +39,25 @@ function plural(count: number, one: string, many: string): string {
return `${String(count)} ${count === 1 ? one : many}`;
}

const SOURCE_LABELS: Record<RuleSource, readonly [string, string]> = {
reviews: ["review comment", "review comments"],
commits: ["fix-up commit", "fix-up commits"],
issues: ["issue", "issues"],
"agent-files": ["agent file", "agent files"],
conventions: ["convention", "conventions"],
docs: ["doc", "docs"],
tests: ["test", "tests"],
};

/**
* "6 times in reviews, 1 time in conventions": each group above zero, the
* larger first, reviews first on a tie. Empty when the rule has neither.
* "4 fix-up commits, 2 review comments, 1 doc": each kind above zero,
* larger first, reviews group first on a tie. Empty when the rule has none.
*/
function evidenceSuffix(rule: ReportRule): string {
const totals = groupTotals(rule);
const order: SourceGroup[] =
totals.conventions > totals.reviews
? ["conventions", "reviews"]
: ["reviews", "conventions"];
const parts = order
.filter((group) => totals[group] > 0)
.map((group) => `${plural(totals[group], "time", "times")} in ${group}`);
const parts = sourceCounts(rule).map(({ source, count }) => {
const [one, many] = SOURCE_LABELS[source];
return plural(count, one, many);
});
return parts.length === 0 ? "" : `: ${parts.join(", ")}`;
}

Expand Down
43 changes: 39 additions & 4 deletions packages/cli/test/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ describe("renderSummary", () => {
invocation: "npx @taskless/cli",
});
expect(summary).toContain(
"Rule a (Taskless ast-grep rule): 9 times in reviews\n"
"Rule a (Taskless ast-grep rule): 9 review comments\n"
);
expect(summary).toContain("https://tskl.es/r/abc");
expect(summary).toContain("can be deleted after review");
Expand Down Expand Up @@ -918,17 +918,52 @@ describe("renderSummary", () => {
invocation: "npx @taskless/cli",
});
expect(summary).toContain(
"Rule a (Taskless ast-grep rule): 6 times in reviews, 1 time in conventions\n"
"Rule a (Taskless ast-grep rule): 4 fix-up commits, 2 review comments, 1 doc\n"
);
expect(summary).toContain(
"Rule b (Taskless ast-grep rule): 5 times in conventions\n"
"Rule b (Taskless ast-grep rule): 3 tests, 2 agent files\n"
);
expect(summary).toContain("Rule c (Taskless ast-grep rule)\n");
expect(summary).toContain(
"Rule d (Taskless ast-grep rule): 1 time in reviews, 1 time in conventions\n"
"Rule d (Taskless ast-grep rule): 1 review comment, 1 agent file\n"
);
});

it("credits commits by kind and never says reviews when there are no review comments", () => {
const report = assembleReport(
draftOf(["a", "b", "c"].map((id) => localRule(id))),
factsFor([], {
rules: [
ruleWithEvidence("a", 0, {
sources: ["commits"],
evidence: { commits: 3 },
}),
ruleWithEvidence("b", 0, {
sources: ["issues"],
evidence: { issues: 1 },
}),
ruleWithEvidence("c", 0, {
sources: ["docs"],
evidence: { docs: 2 },
}),
],
})
);
const summary = renderSummary({
report,
url: "https://tskl.es/r/abc",
expiresAt: "2026-11-12T00:00:00Z",
record: "2026-10-13_142200.json",
invocation: "npx @taskless/cli",
});
expect(summary).toContain(
"Rule a (Taskless ast-grep rule): 3 fix-up commits\n"
);
expect(summary).toContain("Rule b (Taskless ast-grep rule): 1 issue\n");
expect(summary).toContain("Rule c (Taskless ast-grep rule): 2 docs\n");
expect(summary).not.toContain("reviews");
});

it("states no problem count it does not have", () => {
const summary = summaryOf(
draftOf(["a", "b", "c"].map((id) => localRule(id)))
Expand Down