ef9807bd72
* fix: coalesce ci auto-fix queue rounds * fix: centralize ci auto-fix round cap * fix: keep ci auto-fix state fresh * fix: refine ci auto-fix round status * fix: handle ci auto-fix exhaustion edge cases * fix: reduce ci auto-fix dispatch complexity * fix: refresh pending ci auto-fix before direct prompt * fix: harden ci auto-fix queue replacement --------- Co-authored-by: root <root@kandev.ts.net>
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import type { TaskCIPRAutomationState, TaskPR } from "@/lib/types/github";
|
|
|
|
const DEFAULT_AUTO_FIX_MAX_ROUNDS = 10;
|
|
|
|
export type AutoFixRoundInfo = {
|
|
current: number;
|
|
max: number;
|
|
exhausted: boolean;
|
|
};
|
|
|
|
export function findCIAutomationStateForPR(
|
|
states: TaskCIPRAutomationState[] | undefined,
|
|
pr: TaskPR,
|
|
): TaskCIPRAutomationState | undefined {
|
|
const repositoryID = pr.repository_id ?? "";
|
|
return states?.find(
|
|
(state) => state.pr_number === pr.pr_number && state.repository_id === repositoryID,
|
|
);
|
|
}
|
|
|
|
export function autoFixRoundForState(
|
|
state: TaskCIPRAutomationState | undefined,
|
|
maxRounds: number | null | undefined,
|
|
): AutoFixRoundInfo {
|
|
const max = normalizeAutoFixMaxRounds(maxRounds);
|
|
const current = clampAutoFixRound(state?.auto_fix_round_count, max);
|
|
return {
|
|
current,
|
|
max,
|
|
exhausted: Boolean(state?.auto_fix_exhausted_at),
|
|
};
|
|
}
|
|
|
|
export function normalizeAutoFixMaxRounds(value: number | null | undefined) {
|
|
if (typeof value !== "number" || !Number.isFinite(value)) return DEFAULT_AUTO_FIX_MAX_ROUNDS;
|
|
return Math.max(1, Math.trunc(value));
|
|
}
|
|
|
|
export function clampAutoFixRound(value: number | null | undefined, maxRounds: number) {
|
|
if (typeof value !== "number" || !Number.isFinite(value)) return 0;
|
|
return Math.min(maxRounds, Math.max(0, Math.trunc(value)));
|
|
}
|