@diffci.com/diffci 0.3.44 → 0.3.45

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.
@@ -32,7 +32,7 @@ export interface Patterns {
32
32
  ciOnlyJob: RegExp;
33
33
  }
34
34
  export declare const DEFAULT_PATTERNS: Patterns;
35
- export declare function bucketJob(job: JobSummary, patterns?: Patterns): JobBucket;
35
+ export declare function bucketJob(job: JobSummary, patterns?: Patterns, workflowName?: string): JobBucket;
36
36
  export declare function isAggregateJob(job: JobSummary): boolean;
37
37
  /** Failure signatures of one job: annotation path, title and first message line, with volatile parts blurred. */
38
38
  export declare function signatures(annotations: Annotation[]): Set<string>;
@@ -120,7 +120,10 @@ export declare function pickMainWorkflow(sample: any[]): {
120
120
  id: number;
121
121
  name: string;
122
122
  path: string;
123
+ confident: boolean;
123
124
  } | undefined;
125
+ /** Likely main CI workflows from the repository's workflow list, best first, for a --workflow suggestion. */
126
+ export declare function suggestWorkflows(workflows: any[]): string[];
124
127
  export interface CollectOptions {
125
128
  repository: string;
126
129
  get: GitHubGet;
@@ -35,6 +35,11 @@ const CHECK_JOB = /\b(lint(ing)?|eslint|biome|prettier|format(ting)?|fmt|style|t
35
35
  /** Aggregate gate jobs ("all required jobs passed"): they fail because another job did, so they are ignored
36
36
  * whenever another job in the same run failed too. */
37
37
  const AGGREGATE_JOB = /^(verify|gate|ci|required|all[- ]?(required|checks|jobs|green)?([- ]?(passed|pass|green|ok))?|ci[- ]?(ok|success|passed|status|result)|status|summary|results?|conclusion|done|complete|finish(ed)?|success|alls-green|check[- ]results?)$/i;
38
+ /** Gate jobs named after what they check: "Unit Tests - Status Check", "require-all-checks-to-pass". */
39
+ const AGGREGATE_SUFFIX = /(^|\s-\s|\s)(status[- ]checks?|requires?d?[- ]all[- ]checks?([- ]to)?[- ](pass(ed)?|succeed(ed)?))$/i;
40
+ /** Workflows whose every job is a local check, so a job named only by its matrix ("JavaScript (Node.js 24)
41
+ * 1/4") inherits the workflow's kind. Kept narrow: a workflow called "CI" or "Build and Test" says nothing. */
42
+ const LOCAL_CHECK_WORKFLOW = /\b(unit[- ]?tests?|lint(ing)?|typecheck|type[- ]?checks?|static[- ]analysis)\b/i;
38
43
  const CI_ONLY_STEP = /lockfile|lock file|migration ordering/i;
39
44
  const GENERIC_ANNOTATION = /process completed with exit code|^command failed|elifecycle|exited with code/i;
40
45
  const TIMEOUT = /timed out|timeout/i;
@@ -42,7 +47,7 @@ const AGENT_TRAILER = /co-authored-by:[^\n]*?\b(claude|cursor|copilot|codex|devi
42
47
  const AGENT_BRANCH = /^(claude|codex|copilot|cursor|devin|jules)[/-]/i;
43
48
  export const VERDICTS = ["preventable", "partly", "likely", "shared", "ci-only", "flaky", "infra"];
44
49
  export const DEFAULT_PATTERNS = { checkJob: CHECK_JOB, ciOnlyJob: CI_ONLY_JOB };
45
- export function bucketJob(job, patterns = DEFAULT_PATTERNS) {
50
+ export function bucketJob(job, patterns = DEFAULT_PATTERNS, workflowName = "") {
46
51
  if (job.failedSteps.length === 0)
47
52
  return "infra"; // cancelled, timed out or runner lost: no step failed
48
53
  if (job.failedSteps.every((step) => INFRA_STEP.test(step)))
@@ -57,6 +62,8 @@ export function bucketJob(job, patterns = DEFAULT_PATTERNS) {
57
62
  return "ci-only";
58
63
  if (patterns.checkJob.test(name))
59
64
  return "check";
65
+ if (LOCAL_CHECK_WORKFLOW.test(workflowName) && !CI_ONLY_STRONG.test(workflowName) && !patterns.ciOnlyJob.test(name))
66
+ return "check";
60
67
  return "ci-only"; // a CI-side job by name, or one matching neither list: never inflates the preventable count
61
68
  }
62
69
  /** The job's own name, without a "Workflow / " or "caller / " prefix. */
@@ -64,7 +71,8 @@ function jobName(job) {
64
71
  return (job.name.split(" / ").at(-1) ?? job.name).trim();
65
72
  }
66
73
  export function isAggregateJob(job) {
67
- return AGGREGATE_JOB.test(jobName(job).replace(/[()]/g, "").trim());
74
+ const name = jobName(job).replace(/[()]/g, "").trim();
75
+ return AGGREGATE_JOB.test(name) || AGGREGATE_SUFFIX.test(name);
68
76
  }
69
77
  /** Failure signatures of one job: annotation path, title and first message line, with volatile parts blurred. */
70
78
  export function signatures(annotations) {
@@ -98,15 +106,19 @@ export function classify(input) {
98
106
  const passed = new Set(input.runs.filter((r) => r.conclusion === "success").map((r) => `${r.workflowId}:${r.headSha}`));
99
107
  const prKey = (r) => `${r.headRepo}:${r.headBranch}`;
100
108
  const failingOf = (jobs) => jobs.filter((j) => j.conclusion === "failure" || j.conclusion === "timed_out");
101
- // Which PRs each failure signature appeared on, across the whole window.
109
+ // Which PRs each failure signature, and each failing check job, appeared on across the whole window.
102
110
  const seen = new Map();
111
+ const jobFailedOn = new Map();
103
112
  for (const run of input.runs) {
104
113
  const jobs = input.jobs.get(run.id);
105
114
  if (run.conclusion !== "failure" || !jobs)
106
115
  continue;
107
116
  for (const job of failingOf(jobs)) {
108
- if (bucketJob(job, patterns) !== "check")
117
+ if (bucketJob(job, patterns, run.workflowName) !== "check")
109
118
  continue;
119
+ if (!jobFailedOn.has(job.name))
120
+ jobFailedOn.set(job.name, new Set());
121
+ jobFailedOn.get(job.name).add(prKey(run));
110
122
  for (const sig of signatures(input.annotations.get(job.id) ?? [])) {
111
123
  if (!seen.has(sig))
112
124
  seen.set(sig, new Set());
@@ -114,6 +126,13 @@ export function classify(input) {
114
126
  }
115
127
  }
116
128
  }
129
+ // A check job with no annotations that fails on most pull requests is broken for everyone, not for this
130
+ // change (continue's jetbrains-tests failed on all 185 red runs). It counts as shared, never as own.
131
+ const prs = new Set(input.runs.map(prKey)).size;
132
+ const brokenForEveryone = (name) => {
133
+ const failedOn = jobFailedOn.get(name)?.size ?? 0;
134
+ return failedOn >= 5 && failedOn * 2 >= prs;
135
+ };
117
136
  const rows = [];
118
137
  for (const run of input.runs) {
119
138
  const jobs = input.jobs.get(run.id);
@@ -123,14 +142,14 @@ export function classify(input) {
123
142
  // A gate job that only reports "something else failed" says nothing on its own.
124
143
  const failing = allFailing.some((j) => !isAggregateJob(j)) ? allFailing.filter((j) => !isAggregateJob(j)) : allFailing;
125
144
  const detail = failing.map((job) => {
126
- const bucket = bucketJob(job, patterns);
145
+ const bucket = bucketJob(job, patterns, run.workflowName);
127
146
  if (bucket !== "check")
128
147
  return { name: job.name, bucket };
129
148
  const annotations = input.annotations.get(job.id);
130
149
  const sigs = signatures(annotations ?? []);
131
150
  let ownership;
132
151
  if (sigs.size === 0)
133
- ownership = "unknown";
152
+ ownership = brokenForEveryone(job.name) ? "shared" : "unknown";
134
153
  else if ([...sigs].every((s) => TIMEOUT.test(s)))
135
154
  ownership = "timeout";
136
155
  else if ([...sigs].some((s) => !TIMEOUT.test(s) && (seen.get(s)?.size ?? 0) > 1))
@@ -295,9 +314,29 @@ export function githubClient(token, options = {}) {
295
314
  };
296
315
  }
297
316
  /** Workflows that run on pull requests but are not the test CI: scanners, previews, benchmarks, bots. */
298
- const NOT_MAIN_WORKFLOW = /\b(preview|codeql|security|semgrep|sonar(qube|cloud)?|snyk|scorecard|bench(mark)?s?|size|label(er|s)?|stale|triage|greet(ings?)?|welcome|translations?|i18n|docs?|documentation|docker|image|deploy(ment)?|release|licen[cs]e|title|conventional|commits?|semantic|submodule|e2e|playwright|cypress|lighthouse|dependabot|renovate|cla|dco|changeset|storybook|chromatic)\b/i;
317
+ const NOT_MAIN_WORKFLOW = /\b(preview|codeql|security|semgrep|sonar(qube|cloud)?|snyk|scorecard|bench(mark)?s?|size|label(er|s)?|stale|triage|greet(ings?)?|welcome|translations?|i18n|docs?|documentation|docker|image|deploy(ment)?|release|licen[cs]e|title|conventional|commits?|semantic|submodule|e2e|end ?(to|2) ?end|playwright|cypress|lighthouse|dependabot|renovate|cla|dco|changeset|storybook|chromatic|clean(up)?|caches?|housekeeping|perf(ormance)?|ios|android)\b/i;
299
318
  /** Names and file names the main pull request CI usually has. */
300
- const MAIN_WORKFLOW = /\b(ci|cd|tests?|pipeline|pr|pull[-_ ]request|build|checks?|main|verify|validate|unit|lint|quality)\b/i;
319
+ const MAIN_WORKFLOW = /\b(ci|cd|tests?|pipeline|pr|pull[-_ ]request|build|checks?|main|verify|validate|unit|unittests?|lint|quality)\b/i;
320
+ /** Test words: a tie-breaker between two CI-looking workflows of about the same weight (unit tests over lint). */
321
+ const TEST_WORKFLOW = /\b(tests?|unit|unittests?|ci)\b/i;
322
+ /** Runs that waited for approval or never ran: their wall-clock time is waiting, not CI. */
323
+ const IDLE_CONCLUSION = new Set(["action_required", "skipped", "startup_failure"]);
324
+ /** No real pull request run takes longer; a longer span is a run left waiting or queued. */
325
+ const MAX_RUN_MINUTES = 360;
326
+ /** True when every word of the name is a generic CI word ("PR Checks", "Test CI", "02-unittest"), so it is
327
+ * the repository's main CI rather than one sub-project's ("CLI PR Checks"). */
328
+ function genericName(name) {
329
+ const words = name.toLowerCase().split(/[^a-z]+/).filter(Boolean);
330
+ return words.length > 0 && words.every((word) => MAIN_WORKFLOW.test(word) || word === "and" || word === "run");
331
+ }
332
+ function workflowScore(name, path, minutes) {
333
+ const text = `${name} ${path.replace(/\.ya?ml$/, "").replace(/[-_.]/g, " ")}`;
334
+ const notMain = NOT_MAIN_WORKFLOW.test(text);
335
+ const main = MAIN_WORKFLOW.test(text) && !notMain;
336
+ const score = (minutes + 0.01) * (notMain ? 0.05 : 1) * (MAIN_WORKFLOW.test(text) ? 2 : 1)
337
+ * (main && TEST_WORKFLOW.test(text) ? 1.05 : 1) * (main && genericName(name) ? 1.5 : 1);
338
+ return { score, main };
339
+ }
301
340
  /**
302
341
  * The main pull request CI among one sample page of runs: wall-clock minutes per workflow, doubled when
303
342
  * its name or file looks like CI and cut to 5% when it looks like a scanner, preview, benchmark or bot.
@@ -309,17 +348,30 @@ export function pickMainWorkflow(sample) {
309
348
  const span = (Date.parse(r.updated_at) - Date.parse(r.run_started_at ?? r.created_at)) / 60_000;
310
349
  const path = String(r.path ?? "").replace(/^\.github\/workflows\//, "");
311
350
  const entry = weight.get(r.workflow_id) ?? { name: String(r.name ?? r.workflow_id), path, minutes: 0 };
312
- entry.minutes += Number.isFinite(span) && span > 0 ? span : 0;
351
+ if (!IDLE_CONCLUSION.has(r.conclusion) && Number.isFinite(span) && span > 0)
352
+ entry.minutes += Math.min(span, MAX_RUN_MINUTES);
313
353
  weight.set(r.workflow_id, entry);
314
354
  }
315
355
  let best;
316
356
  for (const [id, w] of weight) {
317
- const text = `${w.name} ${w.path.replace(/\.ya?ml$/, "").replace(/[-_.]/g, " ")}`;
318
- const score = (w.minutes + 0.01) * (NOT_MAIN_WORKFLOW.test(text) ? 0.05 : 1) * (MAIN_WORKFLOW.test(text) ? 2 : 1);
357
+ const { score, main } = workflowScore(w.name, w.path, w.minutes);
319
358
  if (!best || score > best.score)
320
- best = { id, name: w.name, path: w.path, score };
359
+ best = { id, name: w.name, path: w.path, score, main };
321
360
  }
322
- return best && { id: best.id, name: best.name, path: best.path };
361
+ return best && { id: best.id, name: best.name, path: best.path, confident: best.main };
362
+ }
363
+ /** Likely main CI workflows from the repository's workflow list, best first, for a --workflow suggestion. */
364
+ export function suggestWorkflows(workflows) {
365
+ return workflows
366
+ .filter((w) => w.state === "active" && String(w.path ?? "").startsWith(".github/workflows/"))
367
+ .map((w) => {
368
+ const file = String(w.path).replace(/^\.github\/workflows\//, "");
369
+ return { file, name: String(w.name ?? file), ...workflowScore(String(w.name ?? file), file, 1) };
370
+ })
371
+ .filter((w) => w.main)
372
+ .sort((a, b) => b.score - a.score || a.name.length - b.name.length)
373
+ .slice(0, 5)
374
+ .map((w) => `${w.file} ("${w.name}")`);
323
375
  }
324
376
  /** Fetches runs, jobs, annotations and commit messages, then classifies. Partial data on a stop. */
325
377
  export async function collectRedBuilds(options) {
@@ -344,6 +396,12 @@ export async function collectRedBuilds(options) {
344
396
  const chosen = pickMainWorkflow(body?.workflow_runs ?? []);
345
397
  if (!chosen)
346
398
  return summarise(repository, [], [], null);
399
+ if (!chosen.confident) {
400
+ // Only scanners, cleanups or bots ran in the sample: guessing would analyse the wrong workflow.
401
+ const list = await get(`repos/${repository}/actions/workflows?per_page=100`);
402
+ const suggestions = list.status === 200 ? suggestWorkflows(list.body?.workflows ?? []) : [];
403
+ throw new Error(`no workflow among the latest pull request runs looks like the main CI (the busiest was "${chosen.name}", ${chosen.path}). Pass --workflow <file>${suggestions.length ? `; likely candidates: ${suggestions.join(", ")}` : ""}`);
404
+ }
347
405
  workflow = String(chosen.id);
348
406
  workflowLabel = chosen.name;
349
407
  log(`Using the "${workflowLabel}" workflow (${chosen.path}), which looks like the main pull request CI. Pass --workflow to choose another.`);
@@ -390,7 +448,7 @@ export async function collectRedBuilds(options) {
390
448
  if (readable)
391
449
  jobs.set(run.id, list);
392
450
  }
393
- const checkJobs = red.flatMap((run) => (jobs.get(run.id) ?? []).filter((j) => (j.conclusion === "failure" || j.conclusion === "timed_out") && bucketJob(j, patterns) === "check"));
451
+ const checkJobs = red.flatMap((run) => (jobs.get(run.id) ?? []).filter((j) => (j.conclusion === "failure" || j.conclusion === "timed_out") && bucketJob(j, patterns, run.workflowName) === "check"));
394
452
  log(`Reading failure annotations of ${checkJobs.length} failing check jobs...`);
395
453
  for (const job of checkJobs) {
396
454
  const { status, body } = await get(`repos/${repository}/check-runs/${job.id}/annotations?per_page=50`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diffci.com/diffci",
3
- "version": "0.3.44",
3
+ "version": "0.3.45",
4
4
  "description": "Verification for AI coding agents: runs the tests and checks a change affects, with full fallback and a receipt",
5
5
  "keywords": [
6
6
  "ci",