@triagepilot/core 1.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.
- package/LICENSE +105 -0
- package/dist/availability.d.ts +25 -0
- package/dist/availability.js +31 -0
- package/dist/branch.d.ts +1 -0
- package/dist/branch.js +4 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/ownership.d.ts +21 -0
- package/dist/ownership.js +24 -0
- package/dist/review-policy.d.ts +20 -0
- package/dist/review-policy.js +67 -0
- package/dist/reviewer-selection.d.ts +9 -0
- package/dist/reviewer-selection.js +33 -0
- package/dist/risk.d.ts +49 -0
- package/dist/risk.js +164 -0
- package/dist/routing.d.ts +25 -0
- package/dist/routing.js +51 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Miroslav Babjak
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface ReviewerAbsenceWindow {
|
|
2
|
+
externalActorId: string;
|
|
3
|
+
startAt: Date;
|
|
4
|
+
endAt: Date;
|
|
5
|
+
}
|
|
6
|
+
export declare function availableActorsAt(input: {
|
|
7
|
+
actors: string[];
|
|
8
|
+
absences: ReviewerAbsenceWindow[];
|
|
9
|
+
now: Date;
|
|
10
|
+
}): string[];
|
|
11
|
+
export declare function selectReplacement(input: {
|
|
12
|
+
author: string;
|
|
13
|
+
unavailableActor: string;
|
|
14
|
+
activeCohort: string[];
|
|
15
|
+
approvedActors: string[];
|
|
16
|
+
originalEligibleActors: string[];
|
|
17
|
+
originalPreferredActors: string[];
|
|
18
|
+
absences: ReviewerAbsenceWindow[];
|
|
19
|
+
load: Record<string, number>;
|
|
20
|
+
selectionKey: string;
|
|
21
|
+
now: Date;
|
|
22
|
+
}): {
|
|
23
|
+
replacementActor: string | null;
|
|
24
|
+
candidates: string[];
|
|
25
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { normalizeReviewer, selectTieredReviewers, uniqueReviewers } from "./reviewer-selection.js";
|
|
2
|
+
export function availableActorsAt(input) {
|
|
3
|
+
const unavailableActors = new Set(input.absences
|
|
4
|
+
.filter((absence) => absence.startAt <= input.now && input.now < absence.endAt)
|
|
5
|
+
.map((absence) => normalizeReviewer(absence.externalActorId))
|
|
6
|
+
.filter(Boolean));
|
|
7
|
+
return uniqueReviewers(input.actors).filter((actor) => !unavailableActors.has(actor));
|
|
8
|
+
}
|
|
9
|
+
export function selectReplacement(input) {
|
|
10
|
+
const excludedActors = new Set(uniqueReviewers([
|
|
11
|
+
input.author,
|
|
12
|
+
input.unavailableActor,
|
|
13
|
+
...input.activeCohort,
|
|
14
|
+
...input.approvedActors,
|
|
15
|
+
]));
|
|
16
|
+
const candidates = availableActorsAt({
|
|
17
|
+
actors: input.originalEligibleActors,
|
|
18
|
+
absences: input.absences,
|
|
19
|
+
now: input.now,
|
|
20
|
+
})
|
|
21
|
+
.filter((actor) => !excludedActors.has(actor))
|
|
22
|
+
.sort();
|
|
23
|
+
const replacementActor = selectTieredReviewers({
|
|
24
|
+
candidates,
|
|
25
|
+
preferredReviewers: input.originalPreferredActors,
|
|
26
|
+
load: input.load,
|
|
27
|
+
selectionKey: input.selectionKey,
|
|
28
|
+
count: 1,
|
|
29
|
+
})[0] ?? null;
|
|
30
|
+
return { replacementActor, candidates };
|
|
31
|
+
}
|
package/dist/branch.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isBranchExcluded(branch: string, patterns: string[]): boolean;
|
package/dist/branch.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface OwnershipRule {
|
|
2
|
+
paths: string[];
|
|
3
|
+
reviewers: string[];
|
|
4
|
+
}
|
|
5
|
+
export interface OwnershipInput {
|
|
6
|
+
files: string[];
|
|
7
|
+
rules: OwnershipRule[];
|
|
8
|
+
fallbackReviewers: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface OwnershipMatchResult {
|
|
11
|
+
matchedRules: Array<{
|
|
12
|
+
pattern: string;
|
|
13
|
+
reviewers: string[];
|
|
14
|
+
matchedFiles: string[];
|
|
15
|
+
}>;
|
|
16
|
+
preferredReviewers: string[];
|
|
17
|
+
eligibleReviewers: string[];
|
|
18
|
+
uncoveredFiles: string[];
|
|
19
|
+
usedFallback: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function matchOwnership(input: OwnershipInput): OwnershipMatchResult;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { minimatch } from "minimatch";
|
|
2
|
+
export function matchOwnership(input) {
|
|
3
|
+
const matchedRules = input.rules
|
|
4
|
+
.flatMap((rule) => rule.paths.map((pattern) => ({
|
|
5
|
+
pattern,
|
|
6
|
+
reviewers: rule.reviewers,
|
|
7
|
+
matchedFiles: input.files.filter((file) => minimatch(file, pattern, { dot: true, matchBase: true })),
|
|
8
|
+
})))
|
|
9
|
+
.filter((rule) => rule.matchedFiles.length > 0);
|
|
10
|
+
const matchedFiles = new Set(matchedRules.flatMap((rule) => rule.matchedFiles));
|
|
11
|
+
const matchedReviewers = dedupe(matchedRules.flatMap((rule) => rule.reviewers));
|
|
12
|
+
const usedFallback = matchedReviewers.length === 0;
|
|
13
|
+
const preferredReviewers = usedFallback ? dedupe(input.fallbackReviewers) : matchedReviewers;
|
|
14
|
+
return {
|
|
15
|
+
matchedRules,
|
|
16
|
+
preferredReviewers,
|
|
17
|
+
eligibleReviewers: dedupe([...preferredReviewers, ...input.fallbackReviewers]),
|
|
18
|
+
uncoveredFiles: input.files.filter((file) => !matchedFiles.has(file)),
|
|
19
|
+
usedFallback,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function dedupe(values) {
|
|
23
|
+
return [...new Set(values)];
|
|
24
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type HumanReviewPolicyState = "in_progress" | "success" | "failure";
|
|
2
|
+
export interface ReviewMetadata {
|
|
3
|
+
actor: string;
|
|
4
|
+
actorType?: "human" | "bot" | string;
|
|
5
|
+
state: "approved" | "changes_requested" | "commented" | "pending" | "dismissed" | string;
|
|
6
|
+
submittedAt: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface HumanReviewPolicyEvaluationInput {
|
|
9
|
+
route: "no_human" | "human_review" | "no_eligible_reviewer";
|
|
10
|
+
selectedReviewers: string[];
|
|
11
|
+
requiredApprovalCount?: number;
|
|
12
|
+
reviews: ReviewMetadata[];
|
|
13
|
+
}
|
|
14
|
+
export interface HumanReviewPolicyEvaluation {
|
|
15
|
+
state: HumanReviewPolicyState;
|
|
16
|
+
summary: string;
|
|
17
|
+
missingReviewers: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare function evaluateHumanReviewPolicy(input: HumanReviewPolicyEvaluationInput): HumanReviewPolicyEvaluation;
|
|
20
|
+
export declare function activeApprovedReviewers(reviews: ReviewMetadata[]): string[];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function evaluateHumanReviewPolicy(input) {
|
|
2
|
+
if (input.route === "no_human") {
|
|
3
|
+
return {
|
|
4
|
+
state: "success",
|
|
5
|
+
summary: "No human approval is required for this pull request.",
|
|
6
|
+
missingReviewers: [],
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
if (input.route === "no_eligible_reviewer") {
|
|
10
|
+
return {
|
|
11
|
+
state: "failure",
|
|
12
|
+
summary: "No eligible human reviewer is available for this required review.",
|
|
13
|
+
missingReviewers: [],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const approvedReviewers = new Set(activeApprovedReviewers(input.reviews).map(normalizeReviewer));
|
|
17
|
+
const requiredApprovalCount = input.requiredApprovalCount ?? input.selectedReviewers.length;
|
|
18
|
+
const missingApprovalCount = Math.max(requiredApprovalCount - approvedReviewers.size, 0);
|
|
19
|
+
const missingReviewers = input.selectedReviewers
|
|
20
|
+
.filter((reviewer) => !approvedReviewers.has(normalizeReviewer(reviewer)))
|
|
21
|
+
.slice(0, missingApprovalCount);
|
|
22
|
+
if (missingApprovalCount === 0) {
|
|
23
|
+
return {
|
|
24
|
+
state: "success",
|
|
25
|
+
summary: "Required human approval count met.",
|
|
26
|
+
missingReviewers: [],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
state: "in_progress",
|
|
31
|
+
summary: missingApprovalCount === 1
|
|
32
|
+
? "Waiting for 1 more human approval."
|
|
33
|
+
: `Waiting for ${missingApprovalCount} more human approvals.`,
|
|
34
|
+
missingReviewers,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function activeApprovedReviewers(reviews) {
|
|
38
|
+
const latestReviews = new Map();
|
|
39
|
+
reviews.forEach((review, index) => {
|
|
40
|
+
if (review.actorType !== undefined && review.actorType !== "human")
|
|
41
|
+
return;
|
|
42
|
+
const actor = normalizeReviewer(review.actor);
|
|
43
|
+
const submittedAt = parseSubmittedAt(review.submittedAt);
|
|
44
|
+
const latest = latestReviews.get(actor);
|
|
45
|
+
if (!latest || isLaterReview({ submittedAt, index }, latest)) {
|
|
46
|
+
latestReviews.set(actor, { review, submittedAt, index });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return [...latestReviews.entries()]
|
|
50
|
+
.filter(([, entry]) => entry.review.state === "approved")
|
|
51
|
+
.map(([actor]) => `@${actor}`);
|
|
52
|
+
}
|
|
53
|
+
function normalizeReviewer(reviewer) {
|
|
54
|
+
return reviewer.replace(/^@/, "").toLowerCase();
|
|
55
|
+
}
|
|
56
|
+
function isLaterReview(candidate, current) {
|
|
57
|
+
if (candidate.submittedAt === null || current.submittedAt === null)
|
|
58
|
+
return candidate.index > current.index;
|
|
59
|
+
return candidate.submittedAt > current.submittedAt ||
|
|
60
|
+
(candidate.submittedAt === current.submittedAt && candidate.index > current.index);
|
|
61
|
+
}
|
|
62
|
+
function parseSubmittedAt(value) {
|
|
63
|
+
if (value === null)
|
|
64
|
+
return null;
|
|
65
|
+
const timestamp = Date.parse(value);
|
|
66
|
+
return Number.isNaN(timestamp) ? null : timestamp;
|
|
67
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function normalizeReviewer(reviewer: string): string;
|
|
2
|
+
export declare function uniqueReviewers(reviewers: string[]): string[];
|
|
3
|
+
export declare function selectTieredReviewers(input: {
|
|
4
|
+
candidates: string[];
|
|
5
|
+
preferredReviewers: string[];
|
|
6
|
+
load: Record<string, number>;
|
|
7
|
+
selectionKey: string;
|
|
8
|
+
count: number;
|
|
9
|
+
}): string[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function normalizeReviewer(reviewer) {
|
|
2
|
+
const normalized = reviewer.trim().replace(/^@/, "").toLowerCase();
|
|
3
|
+
return normalized ? `@${normalized}` : "";
|
|
4
|
+
}
|
|
5
|
+
export function uniqueReviewers(reviewers) {
|
|
6
|
+
return [...new Set(reviewers.map(normalizeReviewer).filter(Boolean))];
|
|
7
|
+
}
|
|
8
|
+
export function selectTieredReviewers(input) {
|
|
9
|
+
const candidateSet = new Set(input.candidates);
|
|
10
|
+
const preferredCandidates = uniqueReviewers(input.preferredReviewers).filter((reviewer) => candidateSet.has(reviewer));
|
|
11
|
+
const preferredCandidateSet = new Set(preferredCandidates);
|
|
12
|
+
const fallbackCandidates = input.candidates.filter((reviewer) => !preferredCandidateSet.has(reviewer));
|
|
13
|
+
const selectedPreferredReviewers = selectLowestLoadReviewers(preferredCandidates, input.load, input.selectionKey, input.count);
|
|
14
|
+
const selectedFallbackReviewers = selectLowestLoadReviewers(fallbackCandidates, input.load, input.selectionKey, input.count - selectedPreferredReviewers.length);
|
|
15
|
+
return [...selectedPreferredReviewers, ...selectedFallbackReviewers];
|
|
16
|
+
}
|
|
17
|
+
function selectLowestLoadReviewers(candidates, load, selectionKey, count) {
|
|
18
|
+
return [...candidates].sort((a, b) => {
|
|
19
|
+
const loadDifference = (load[a] ?? 0) - (load[b] ?? 0);
|
|
20
|
+
if (loadDifference !== 0)
|
|
21
|
+
return loadDifference;
|
|
22
|
+
const rankDifference = stableRank(`${selectionKey}:${a}`) - stableRank(`${selectionKey}:${b}`);
|
|
23
|
+
return rankDifference === 0 ? a.localeCompare(b) : rankDifference;
|
|
24
|
+
}).slice(0, count);
|
|
25
|
+
}
|
|
26
|
+
function stableRank(value) {
|
|
27
|
+
let hash = 0x811c9dc5;
|
|
28
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
29
|
+
hash ^= value.charCodeAt(index);
|
|
30
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
return hash;
|
|
33
|
+
}
|
package/dist/risk.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { RiskTier, ScoreComponent } from "@triagepilot/contracts";
|
|
2
|
+
export declare const CLASSIFIER_VERSION = "risk-v2";
|
|
3
|
+
export interface ChangedFileMetadata {
|
|
4
|
+
path: string;
|
|
5
|
+
additions: number;
|
|
6
|
+
deletions: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RiskPathConfig {
|
|
9
|
+
pattern: string;
|
|
10
|
+
weight: number;
|
|
11
|
+
tag: string;
|
|
12
|
+
}
|
|
13
|
+
export interface RiskSuppressorConfig {
|
|
14
|
+
ifAllMatch: string[];
|
|
15
|
+
ceiling: number;
|
|
16
|
+
}
|
|
17
|
+
export interface AiAuthorshipConfig {
|
|
18
|
+
enabled: boolean;
|
|
19
|
+
modifier: number;
|
|
20
|
+
}
|
|
21
|
+
export interface RiskThresholdConfig {
|
|
22
|
+
low: number;
|
|
23
|
+
high: number;
|
|
24
|
+
}
|
|
25
|
+
export interface RiskSizeConfig {
|
|
26
|
+
highChangedFiles: number;
|
|
27
|
+
highChangedLines: number;
|
|
28
|
+
}
|
|
29
|
+
export interface RiskScoringConfig {
|
|
30
|
+
size: RiskSizeConfig;
|
|
31
|
+
thresholds: RiskThresholdConfig;
|
|
32
|
+
paths: RiskPathConfig[];
|
|
33
|
+
suppressors: RiskSuppressorConfig[];
|
|
34
|
+
aiAuthorship: AiAuthorshipConfig;
|
|
35
|
+
}
|
|
36
|
+
export interface RiskScoringInput {
|
|
37
|
+
files: ChangedFileMetadata[];
|
|
38
|
+
author: string;
|
|
39
|
+
branchName: string;
|
|
40
|
+
commitMessages: string[];
|
|
41
|
+
config: RiskScoringConfig;
|
|
42
|
+
}
|
|
43
|
+
export interface RiskScoringResult {
|
|
44
|
+
classifierVersion: typeof CLASSIFIER_VERSION;
|
|
45
|
+
score: number;
|
|
46
|
+
tier: RiskTier;
|
|
47
|
+
components: ScoreComponent[];
|
|
48
|
+
}
|
|
49
|
+
export declare function scorePullRequestRisk(input: RiskScoringInput): RiskScoringResult;
|
package/dist/risk.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { minimatch } from "minimatch";
|
|
2
|
+
export const CLASSIFIER_VERSION = "risk-v2";
|
|
3
|
+
const LOCKFILE_NAMES = new Set([
|
|
4
|
+
"package-lock.json",
|
|
5
|
+
"pnpm-lock.yaml",
|
|
6
|
+
"yarn.lock",
|
|
7
|
+
"bun.lockb",
|
|
8
|
+
"Cargo.lock",
|
|
9
|
+
"Gemfile.lock",
|
|
10
|
+
]);
|
|
11
|
+
const MIGRATION_OR_SCHEMA_PATTERNS = [
|
|
12
|
+
"**/migrations/**",
|
|
13
|
+
"migrations/**",
|
|
14
|
+
"**/migration/**",
|
|
15
|
+
"migration/**",
|
|
16
|
+
"**/schema/**",
|
|
17
|
+
"schema/**",
|
|
18
|
+
"**/schema.sql",
|
|
19
|
+
"**/schema.ts",
|
|
20
|
+
"**/schema.prisma",
|
|
21
|
+
"prisma/schema.prisma",
|
|
22
|
+
];
|
|
23
|
+
const TEST_PATH_PATTERNS = ["**/*.spec.ts", "**/*.test.ts", "**/__tests__/**", "test/**", "tests/**"];
|
|
24
|
+
const AI_BRANCH_PREFIXES = ["codex/", "copilot/", "cursor/", "devin/", "lovable/"];
|
|
25
|
+
const AI_AUTHORS = new Set(["copilot", "copilot-swe-agent", "devin-ai-integration", "lovable"]);
|
|
26
|
+
const AI_COAUTHOR_MARKERS = ["copilot", "codex", "cursor", "devin", "lovable"];
|
|
27
|
+
export function scorePullRequestRisk(input) {
|
|
28
|
+
const components = [];
|
|
29
|
+
const { config, files } = input;
|
|
30
|
+
const scoredFiles = files.filter((file) => !isTestPath(file.path));
|
|
31
|
+
if (scoredFiles.length > 0) {
|
|
32
|
+
components.push({
|
|
33
|
+
reason: "changed_file_count",
|
|
34
|
+
score: Math.min(20, Math.ceil(scoredFiles.length / 5) * 5),
|
|
35
|
+
detail: `${scoredFiles.length} changed file${scoredFiles.length === 1 ? "" : "s"}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const lineDelta = scoredFiles.reduce((sum, file) => sum + file.additions + file.deletions, 0);
|
|
39
|
+
if (lineDelta >= 200) {
|
|
40
|
+
components.push({
|
|
41
|
+
reason: "large_line_delta",
|
|
42
|
+
score: 15,
|
|
43
|
+
detail: `${lineDelta} changed lines`,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
for (const riskPath of config.paths) {
|
|
47
|
+
const matchedFiles = scoredFiles.filter((file) => matchesPath(file.path, riskPath.pattern));
|
|
48
|
+
if (matchedFiles.length > 0) {
|
|
49
|
+
components.push({
|
|
50
|
+
reason: `high_risk_path:${riskPath.tag}`,
|
|
51
|
+
score: riskPath.weight,
|
|
52
|
+
detail: `${matchedFiles.length} file${matchedFiles.length === 1 ? "" : "s"} matched ${riskPath.pattern}`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (files.some((file) => LOCKFILE_NAMES.has(pathBasename(file.path)))) {
|
|
57
|
+
components.push({
|
|
58
|
+
reason: "dependency_lockfile_change",
|
|
59
|
+
score: 15,
|
|
60
|
+
detail: "Dependency lockfile changed",
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (files.some((file) => MIGRATION_OR_SCHEMA_PATTERNS.some((pattern) => matchesPath(file.path, pattern)))) {
|
|
64
|
+
components.push({
|
|
65
|
+
reason: "migration_or_schema_change",
|
|
66
|
+
score: 20,
|
|
67
|
+
detail: "Migration or schema file changed",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (config.aiAuthorship.enabled && hasAiAuthorshipSignal(input)) {
|
|
71
|
+
components.push({
|
|
72
|
+
reason: "ai_authorship_signal",
|
|
73
|
+
score: config.aiAuthorship.modifier,
|
|
74
|
+
detail: "Author, branch, or commit metadata indicates AI-authored changes",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const reachedFileThreshold = scoredFiles.length >= config.size.highChangedFiles;
|
|
78
|
+
const reachedLineThreshold = lineDelta >= config.size.highChangedLines;
|
|
79
|
+
const sizeEscalated = reachedFileThreshold || reachedLineThreshold;
|
|
80
|
+
if (sizeEscalated) {
|
|
81
|
+
const scoreFloor = Math.min(100, config.thresholds.high + 1);
|
|
82
|
+
components.push({
|
|
83
|
+
reason: "large_change_size",
|
|
84
|
+
score: Math.max(0, scoreFloor - sumComponentScores(components)),
|
|
85
|
+
detail: formatSizeEscalationDetail({
|
|
86
|
+
changedFiles: scoredFiles.length,
|
|
87
|
+
changedLines: lineDelta,
|
|
88
|
+
highChangedFiles: config.size.highChangedFiles,
|
|
89
|
+
highChangedLines: config.size.highChangedLines,
|
|
90
|
+
reachedFileThreshold,
|
|
91
|
+
reachedLineThreshold,
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
let score = Math.min(100, sumComponentScores(components));
|
|
96
|
+
const suppressor = findMatchingSuppressor(files, config.suppressors);
|
|
97
|
+
if (suppressor) {
|
|
98
|
+
if (!sizeEscalated) {
|
|
99
|
+
score = Math.min(score, suppressor.ceiling);
|
|
100
|
+
}
|
|
101
|
+
components.push({
|
|
102
|
+
reason: "docs_or_test_suppressor",
|
|
103
|
+
score: 0,
|
|
104
|
+
detail: sizeEscalated
|
|
105
|
+
? "Risk suppressor ignored for a size-escalated pull request"
|
|
106
|
+
: `Risk score capped at ${suppressor.ceiling}`,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
classifierVersion: CLASSIFIER_VERSION,
|
|
111
|
+
score,
|
|
112
|
+
tier: sizeEscalated ? "high" : tierForScore(score, config),
|
|
113
|
+
components,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function sumComponentScores(components) {
|
|
117
|
+
return components.reduce((sum, component) => sum + component.score, 0);
|
|
118
|
+
}
|
|
119
|
+
function tierForScore(score, config) {
|
|
120
|
+
if (score <= config.thresholds.low) {
|
|
121
|
+
return "low";
|
|
122
|
+
}
|
|
123
|
+
if (score <= config.thresholds.high) {
|
|
124
|
+
return "medium";
|
|
125
|
+
}
|
|
126
|
+
return "high";
|
|
127
|
+
}
|
|
128
|
+
function findMatchingSuppressor(files, suppressors) {
|
|
129
|
+
if (files.length === 0) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
return suppressors.find((suppressor) => suppressor.ifAllMatch.length > 0 &&
|
|
133
|
+
files.every((file) => suppressor.ifAllMatch.some((pattern) => matchesPath(file.path, pattern))));
|
|
134
|
+
}
|
|
135
|
+
function hasAiAuthorshipSignal(input) {
|
|
136
|
+
const author = input.author.toLowerCase();
|
|
137
|
+
const branchName = input.branchName.toLowerCase();
|
|
138
|
+
return (AI_AUTHORS.has(author) ||
|
|
139
|
+
AI_BRANCH_PREFIXES.some((prefix) => branchName.startsWith(prefix)) ||
|
|
140
|
+
input.commitMessages.some(hasAiCommitMarker));
|
|
141
|
+
}
|
|
142
|
+
function hasAiCommitMarker(message) {
|
|
143
|
+
return message
|
|
144
|
+
.toLowerCase()
|
|
145
|
+
.split(/\r?\n/)
|
|
146
|
+
.some((line) => line.trimStart().startsWith("co-authored-by:") &&
|
|
147
|
+
AI_COAUTHOR_MARKERS.some((marker) => line.includes(marker)));
|
|
148
|
+
}
|
|
149
|
+
function matchesPath(path, pattern) {
|
|
150
|
+
return minimatch(path, pattern, { dot: true, matchBase: true });
|
|
151
|
+
}
|
|
152
|
+
function isTestPath(path) {
|
|
153
|
+
return TEST_PATH_PATTERNS.some((pattern) => matchesPath(path, pattern));
|
|
154
|
+
}
|
|
155
|
+
function formatSizeEscalationDetail({ changedFiles, changedLines, highChangedFiles, highChangedLines, reachedFileThreshold, reachedLineThreshold, }) {
|
|
156
|
+
const thresholds = [
|
|
157
|
+
reachedFileThreshold && `${highChangedFiles} file threshold`,
|
|
158
|
+
reachedLineThreshold && `${highChangedLines} line threshold`,
|
|
159
|
+
].filter((threshold) => Boolean(threshold));
|
|
160
|
+
return `${changedFiles} non-test changed file${changedFiles === 1 ? "" : "s"}, ${changedLines} non-test changed lines; reached ${thresholds.join(" and ")}`;
|
|
161
|
+
}
|
|
162
|
+
function pathBasename(path) {
|
|
163
|
+
return path.split("/").at(-1) ?? path;
|
|
164
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RiskTier, RoutingAction } from "@triagepilot/contracts";
|
|
2
|
+
export interface RoutingInput {
|
|
3
|
+
risk: {
|
|
4
|
+
score: number;
|
|
5
|
+
tier: RiskTier;
|
|
6
|
+
};
|
|
7
|
+
author: string;
|
|
8
|
+
preferredReviewers?: string[];
|
|
9
|
+
eligibleReviewers: string[];
|
|
10
|
+
existingApprovedReviewers?: string[];
|
|
11
|
+
load: Record<string, number>;
|
|
12
|
+
highRiskReviewers: 1 | 2;
|
|
13
|
+
selectionKey: string;
|
|
14
|
+
}
|
|
15
|
+
export interface RoutingDecision {
|
|
16
|
+
action: Exclude<RoutingAction, "configuration_failure">;
|
|
17
|
+
candidates: string[];
|
|
18
|
+
requestedReviewerCount: number;
|
|
19
|
+
selectedReviewers: string[];
|
|
20
|
+
reviewersToRequest: string[];
|
|
21
|
+
reviewerShortfall: number;
|
|
22
|
+
noHumanReason?: string;
|
|
23
|
+
loadSnapshot: Record<string, number>;
|
|
24
|
+
}
|
|
25
|
+
export declare function decideRouting(input: RoutingInput): RoutingDecision;
|
package/dist/routing.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { normalizeReviewer, selectTieredReviewers, uniqueReviewers } from "./reviewer-selection.js";
|
|
2
|
+
export function decideRouting(input) {
|
|
3
|
+
const existingApprovedReviewers = uniqueReviewers(input.existingApprovedReviewers ?? []).filter((reviewer) => reviewer !== normalizeReviewer(input.author));
|
|
4
|
+
const candidates = uniqueReviewers(input.eligibleReviewers)
|
|
5
|
+
.filter((reviewer) => reviewer !== normalizeReviewer(input.author) && !existingApprovedReviewers.includes(reviewer))
|
|
6
|
+
.sort();
|
|
7
|
+
const loadSnapshot = Object.fromEntries(candidates.map((candidate) => [candidate, input.load[candidate] ?? 0]));
|
|
8
|
+
if (input.risk.tier === "low") {
|
|
9
|
+
return {
|
|
10
|
+
action: "policy_approval",
|
|
11
|
+
candidates,
|
|
12
|
+
requestedReviewerCount: 0,
|
|
13
|
+
selectedReviewers: [],
|
|
14
|
+
reviewersToRequest: [],
|
|
15
|
+
reviewerShortfall: 0,
|
|
16
|
+
noHumanReason: "risk_at_or_below_low_threshold",
|
|
17
|
+
loadSnapshot,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const requestedReviewerCount = input.risk.tier === "high" ? input.highRiskReviewers : 1;
|
|
21
|
+
const reviewersToRequest = selectTieredReviewers({
|
|
22
|
+
candidates,
|
|
23
|
+
preferredReviewers: input.preferredReviewers ?? [],
|
|
24
|
+
load: input.load,
|
|
25
|
+
selectionKey: input.selectionKey,
|
|
26
|
+
count: requestedReviewerCount,
|
|
27
|
+
});
|
|
28
|
+
const selectedReviewers = reviewersToRequest;
|
|
29
|
+
const reviewerShortfall = requestedReviewerCount - selectedReviewers.length;
|
|
30
|
+
if (selectedReviewers.length === 0) {
|
|
31
|
+
return {
|
|
32
|
+
action: "no_eligible_reviewer",
|
|
33
|
+
candidates,
|
|
34
|
+
requestedReviewerCount,
|
|
35
|
+
selectedReviewers,
|
|
36
|
+
reviewersToRequest,
|
|
37
|
+
reviewerShortfall,
|
|
38
|
+
noHumanReason: "no_eligible_reviewer",
|
|
39
|
+
loadSnapshot,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
action: "request_human_review",
|
|
44
|
+
candidates,
|
|
45
|
+
requestedReviewerCount,
|
|
46
|
+
selectedReviewers,
|
|
47
|
+
reviewersToRequest,
|
|
48
|
+
reviewerShortfall,
|
|
49
|
+
loadSnapshot,
|
|
50
|
+
};
|
|
51
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@triagepilot/core",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/TriagePilot/triage-pilot.git",
|
|
8
|
+
"directory": "packages/core"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@triagepilot/contracts": "1.1.0",
|
|
25
|
+
"minimatch": "^10.0.1"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"publishedAt": "2026-09-22T09:41:47.000Z",
|
|
31
|
+
"futureLicenseEffectiveAt": "2028-09-22T09:41:47.000Z",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
34
|
+
"check": "tsc -p tsconfig.json --noEmit"
|
|
35
|
+
}
|
|
36
|
+
}
|