Skip to content

Commit c910696

Browse files
committed
fix: guard low-signal PR closes
1 parent 03caf65 commit c910696

6 files changed

Lines changed: 576 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ checkpoint, and status-only commits are intentionally omitted.
3333

3434
### Fixed
3535

36+
- Required a live `DIRTY` merge conflict and at least 30 days without contributor comments or head activity before closing low-signal pull requests, honoring longer configured stale thresholds and applying the same fail-closed policy to stale-review promotion and trusted close routing.
3637
- Retried successful GitHub CLI JSON-lines responses when their output is truncated, preventing transient list-page corruption from aborting close-apply runs.
3738
- Allowed conflict-free canonical PRs that only need a base update to back duplicate or superseded closures while retaining proof, review, check, draft, and conflict guards.
3839
- Bounded broad reconciliation with batched Git I/O and tuple checkpoints that report progress and resume safely under concurrent state writers.

‎src/clawsweeper.ts‎

Lines changed: 239 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ const STALLED_UNPROVEN_PR_MIN_AGE_DAYS = 14;
11991199
const STALLED_UNPROVEN_PR_MIN_INACTIVE_DAYS = 14;
12001200
const ABANDONED_PR_MIN_AGE_DAYS = 30;
12011201
const ABANDONED_PR_MIN_INACTIVE_DAYS = 30;
1202+
const LOW_SIGNAL_UNMERGEABLE_PR_MIN_INACTIVE_DAYS = 30;
12021203
const DAY_MS = 24 * 60 * 60 * 1000;
12031204
const RECENT_MISSING_OPEN_MS = DAY_MS;
12041205
const DEFAULT_CODEX_MODEL = PUBLIC_CODEX_MODEL;
@@ -3644,7 +3645,127 @@ function maintainerAssociatedEntries(entries: readonly unknown[]): unknown[] {
36443645
);
36453646
}
36463647

3647-
function lowSignalUnmergeablePrApplyBlockReason(number: number): string | null {
3648+
function lowSignalUnmergeablePrConflictBlockReason(pullValue: unknown): string | null {
3649+
const pull = asRecord(pullValue);
3650+
const mergeableState = (
3651+
stringOrUndefined(pull.mergeableState) ??
3652+
stringOrUndefined(pull.mergeable_state) ??
3653+
"unknown"
3654+
).toLowerCase();
3655+
if (pull.mergeable === false && mergeableState === "dirty") return null;
3656+
const mergeable = typeof pull.mergeable === "boolean" ? String(pull.mergeable) : "unknown";
3657+
return `low_signal_unmergeable_pr requires a live merge conflict; GitHub reports mergeable=${mergeable}, mergeable_state=${mergeableState}`;
3658+
}
3659+
3660+
function githubActivityTimestampMs(value: unknown): number | null {
3661+
const record = asRecord(value);
3662+
for (const candidate of [
3663+
record.updatedAt,
3664+
record.updated_at,
3665+
record.submitted_at,
3666+
record.createdAt,
3667+
record.created_at,
3668+
]) {
3669+
const timestamp = Date.parse(typeof candidate === "string" ? candidate : "");
3670+
if (Number.isFinite(timestamp)) return timestamp;
3671+
}
3672+
return null;
3673+
}
3674+
3675+
function githubActivityLogin(value: unknown): string {
3676+
const record = asRecord(value);
3677+
return (
3678+
stringOrUndefined(record.author) ??
3679+
login(record.user) ??
3680+
stringOrUndefined(record.actor) ??
3681+
login(record.actor) ??
3682+
""
3683+
)
3684+
.trim()
3685+
.toLowerCase();
3686+
}
3687+
3688+
function latestPullRequestAuthorActivityAtMs(options: {
3689+
author: string;
3690+
createdAt: string;
3691+
comments?: readonly unknown[];
3692+
reviews?: readonly unknown[];
3693+
inlineComments?: readonly unknown[];
3694+
timeline?: readonly unknown[];
3695+
headActivityAtMs?: number | null;
3696+
}): number | null {
3697+
const author = options.author.trim().toLowerCase();
3698+
if (!author) return null;
3699+
let latest = Date.parse(options.createdAt);
3700+
if (!Number.isFinite(latest)) latest = Number.NEGATIVE_INFINITY;
3701+
const observe = (value: unknown): void => {
3702+
if (githubActivityLogin(value) !== author) return;
3703+
const timestamp = githubActivityTimestampMs(value);
3704+
if (timestamp !== null && timestamp > latest) latest = timestamp;
3705+
};
3706+
options.comments?.forEach(observe);
3707+
options.reviews?.forEach(observe);
3708+
options.inlineComments?.forEach(observe);
3709+
for (const event of options.timeline ?? []) {
3710+
const record = asRecord(event);
3711+
const eventName = stringOrUndefined(record.event) ?? "";
3712+
const commitId = stringOrUndefined(record.commitId) ?? stringOrUndefined(record.commit_id);
3713+
if (
3714+
eventName === "commented" ||
3715+
eventName === "committed" ||
3716+
eventName === "head_ref_force_pushed" ||
3717+
eventName === "head_ref_restored" ||
3718+
Boolean(commitId)
3719+
) {
3720+
observe(event);
3721+
}
3722+
}
3723+
if (options.headActivityAtMs !== null && options.headActivityAtMs !== undefined) {
3724+
latest = Math.max(latest, options.headActivityAtMs);
3725+
}
3726+
return Number.isFinite(latest) ? latest : null;
3727+
}
3728+
3729+
function lowSignalUnmergeablePrAuthorActivityBlockReason(options: {
3730+
author: string;
3731+
createdAt: string;
3732+
comments?: readonly unknown[];
3733+
reviews?: readonly unknown[];
3734+
inlineComments?: readonly unknown[];
3735+
timeline?: readonly unknown[];
3736+
headActivityAtMs?: number | null;
3737+
staleMinAgeDays: number;
3738+
requireHeadActivityEvidence?: boolean;
3739+
now?: number;
3740+
}): string | null {
3741+
if (
3742+
options.requireHeadActivityEvidence &&
3743+
(options.headActivityAtMs === null || options.headActivityAtMs === undefined)
3744+
) {
3745+
return "low_signal_unmergeable_pr requires dated activity evidence for the current head";
3746+
}
3747+
const latestActivityAtMs = latestPullRequestAuthorActivityAtMs(options);
3748+
if (latestActivityAtMs === null) {
3749+
return "low_signal_unmergeable_pr requires dated author and current-head activity evidence";
3750+
}
3751+
const now = options.now ?? Date.now();
3752+
const configuredInactiveDays = Number.isFinite(options.staleMinAgeDays)
3753+
? Math.max(0, options.staleMinAgeDays)
3754+
: LOW_SIGNAL_UNMERGEABLE_PR_MIN_INACTIVE_DAYS;
3755+
const minimumInactiveDays = Math.max(
3756+
LOW_SIGNAL_UNMERGEABLE_PR_MIN_INACTIVE_DAYS,
3757+
configuredInactiveDays,
3758+
);
3759+
if (now - latestActivityAtMs <= minimumInactiveDays * DAY_MS) {
3760+
return `low_signal_unmergeable_pr requires ${minimumInactiveDays} days without author comments or head activity`;
3761+
}
3762+
return null;
3763+
}
3764+
3765+
function lowSignalUnmergeablePrApplyBlockReason(
3766+
number: number,
3767+
staleMinAgeDays: number,
3768+
): string | null {
36483769
const issue = ghJson<{ assignees?: unknown[] }>([
36493770
"api",
36503771
`repos/${targetRepo()}/issues/${number}`,
@@ -3653,27 +3774,62 @@ function lowSignalUnmergeablePrApplyBlockReason(number: number): string | null {
36533774
]);
36543775
if ((issue.assignees ?? []).length > 0) return "assigned PR has maintainer/human signal";
36553776

3656-
const pull = ghJson<{ requested_reviewers?: unknown[]; requested_teams?: unknown[] }>([
3657-
"api",
3658-
`repos/${targetRepo()}/pulls/${number}`,
3659-
"--jq",
3660-
"{requested_reviewers:[.requested_reviewers[]? | {login:.login}],requested_teams:[.requested_teams[]? | {slug:.slug}]}",
3661-
]);
3777+
const pull = ghJson<{
3778+
created_at?: string;
3779+
mergeable?: boolean | null;
3780+
mergeable_state?: string | null;
3781+
requested_reviewers?: unknown[];
3782+
requested_teams?: unknown[];
3783+
user?: GitHubUser;
3784+
head?: { ref?: string; repo?: { full_name?: string; id?: unknown }; sha?: string };
3785+
}>(["api", `repos/${targetRepo()}/pulls/${number}`]);
36623786
if ((pull.requested_reviewers ?? []).length > 0 || (pull.requested_teams ?? []).length > 0) {
36633787
return "requested reviewers or teams indicate active review signal";
36643788
}
36653789

3666-
const maintainerComments = maintainerAssociatedEntries(
3667-
ghPaged<unknown>(`repos/${targetRepo()}/issues/${number}/comments`),
3668-
);
3790+
const comments = ghPaged<unknown>(`repos/${targetRepo()}/issues/${number}/comments`);
3791+
const maintainerComments = maintainerAssociatedEntries(comments);
36693792
if (maintainerComments.length > 0) return "maintainer issue comment blocks low-signal auto-close";
36703793

3671-
const maintainerReviews = maintainerAssociatedEntries(
3672-
ghPaged<unknown>(`repos/${targetRepo()}/pulls/${number}/reviews`),
3673-
);
3794+
const reviews = ghPaged<unknown>(`repos/${targetRepo()}/pulls/${number}/reviews`);
3795+
const maintainerReviews = maintainerAssociatedEntries(reviews);
36743796
if (maintainerReviews.length > 0) return "maintainer PR review blocks low-signal auto-close";
36753797

3676-
return null;
3798+
const inlineComments = ghPaged<unknown>(`repos/${targetRepo()}/pulls/${number}/comments`);
3799+
const maintainerInlineComments = maintainerAssociatedEntries(inlineComments);
3800+
if (maintainerInlineComments.length > 0) {
3801+
return "maintainer inline review comment blocks low-signal auto-close";
3802+
}
3803+
3804+
const conflictBlock = lowSignalUnmergeablePrConflictBlockReason(pull);
3805+
if (conflictBlock) return conflictBlock;
3806+
3807+
const timeline = ghPaged<unknown>(`repos/${targetRepo()}/issues/${number}/timeline`);
3808+
const headActivity = pullRequestHeadActivity(number, pull, timeline);
3809+
return lowSignalUnmergeablePrAuthorActivityBlockReason({
3810+
author: pull.user?.login ?? "",
3811+
createdAt: pull.created_at ?? "",
3812+
comments,
3813+
reviews,
3814+
inlineComments,
3815+
timeline,
3816+
headActivityAtMs: headActivity.headActivityAtMs,
3817+
staleMinAgeDays,
3818+
requireHeadActivityEvidence: true,
3819+
});
3820+
}
3821+
3822+
function lowSignalUnmergeablePrApplyBlockReasonSafe(
3823+
number: number,
3824+
staleMinAgeDays: number,
3825+
): string | null {
3826+
try {
3827+
return lowSignalUnmergeablePrApplyBlockReason(number, staleMinAgeDays);
3828+
} catch (error) {
3829+
return `low-signal conflict/activity check failed: ${
3830+
error instanceof Error ? error.message : String(error)
3831+
}`;
3832+
}
36773833
}
36783834

36793835
function unconfirmedProductDirectionApplyBlockReason(
@@ -3795,15 +3951,16 @@ const FAILING_CHECK_RUN_CONCLUSIONS = new Set(["failure", "timed_out"]);
37953951
// A pull_request workflow run associated with this PR is tied to source
37963952
// activity, while rerunning its checks leaves created_at unchanged. Missing
37973953
// source-run data keeps the PR open.
3798-
function pullRequestLiveActivity(number: number): PullRequestLiveActivity {
3799-
const pull = ghJson<{
3954+
function pullRequestHeadActivity(
3955+
number: number,
3956+
pull: {
38003957
created_at?: string;
3801-
draft?: boolean;
38023958
head?: { ref?: string; repo?: { full_name?: string; id?: unknown }; sha?: string };
3803-
}>(["api", `repos/${targetRepo()}/pulls/${number}`]);
3959+
},
3960+
timeline = ghPaged<unknown>(`repos/${targetRepo()}/issues/${number}/timeline`),
3961+
): Pick<PullRequestLiveActivity, "headSha" | "headActivityAtMs"> {
38043962
const headSha = typeof pull.head?.sha === "string" ? pull.head.sha : "";
38053963
let headActivityAtMs: number | null = null;
3806-
let headChecksFailing = false;
38073964
const observe = (value: unknown): void => {
38083965
const ms = Date.parse(typeof value === "string" ? value : "");
38093966
if (Number.isFinite(ms) && (headActivityAtMs === null || ms > headActivityAtMs)) {
@@ -3839,12 +3996,26 @@ function pullRequestLiveActivity(number: number): PullRequestLiveActivity {
38393996
observe(record.created_at);
38403997
}
38413998
}
3842-
for (const event of ghPaged<unknown>(`repos/${targetRepo()}/issues/${number}/timeline`)) {
3999+
for (const event of timeline) {
38434000
const record = asRecord(event);
3844-
if (record.event === "head_ref_force_pushed" && record.commit_id === headSha) {
3845-
observe(record.created_at);
4001+
const commitId = stringOrUndefined(record.commitId) ?? stringOrUndefined(record.commit_id);
4002+
if (record.event === "head_ref_force_pushed" && commitId === headSha) {
4003+
observe(stringOrUndefined(record.createdAt) ?? record.created_at);
38464004
}
38474005
}
4006+
}
4007+
return { headSha, headActivityAtMs };
4008+
}
4009+
4010+
function pullRequestLiveActivity(number: number): PullRequestLiveActivity {
4011+
const pull = ghJson<{
4012+
created_at?: string;
4013+
draft?: boolean;
4014+
head?: { ref?: string; repo?: { full_name?: string; id?: unknown }; sha?: string };
4015+
}>(["api", `repos/${targetRepo()}/pulls/${number}`]);
4016+
const { headSha, headActivityAtMs } = pullRequestHeadActivity(number, pull);
4017+
let headChecksFailing = false;
4018+
if (headSha) {
38484019
const combined = ghJson<{ state?: string }>([
38494020
"api",
38504021
`repos/${targetRepo()}/commits/${headSha}/status`,
@@ -14799,6 +14970,7 @@ function recommendedPauseOrCloseOption(markdown: string): MergeRiskOption | null
1479914970
function staleFRatedPullRequestPromotion(
1480014971
markdown: string,
1480114972
item: Item,
14973+
context: ItemContext,
1480214974
staleMinAgeDays: number,
1480314975
): PullRequestClosePromotion | null {
1480414976
const proof = reportRealBehaviorProof(markdown);
@@ -14813,6 +14985,49 @@ function staleFRatedPullRequestPromotion(
1481314985
) {
1481414986
return null;
1481514987
}
14988+
if (
14989+
context.counts?.commentsTruncated ||
14990+
context.counts?.timelineTruncated ||
14991+
context.counts?.pullReviewCommentsTruncated
14992+
) {
14993+
return null;
14994+
}
14995+
let livePull: {
14996+
created_at?: string;
14997+
mergeable?: boolean | null;
14998+
mergeable_state?: string | null;
14999+
user?: GitHubUser;
15000+
head?: { ref?: string; repo?: { full_name?: string; id?: unknown }; sha?: string };
15001+
};
15002+
let reviews: unknown[];
15003+
let headActivityAtMs: number | null;
15004+
try {
15005+
livePull = ghJson(["api", `repos/${targetRepo()}/pulls/${item.number}`]);
15006+
if (lowSignalUnmergeablePrConflictBlockReason(livePull)) return null;
15007+
reviews = ghPaged<unknown>(`repos/${targetRepo()}/pulls/${item.number}/reviews`);
15008+
headActivityAtMs = pullRequestHeadActivity(
15009+
item.number,
15010+
livePull,
15011+
context.timeline,
15012+
).headActivityAtMs;
15013+
} catch {
15014+
return null;
15015+
}
15016+
if (
15017+
lowSignalUnmergeablePrAuthorActivityBlockReason({
15018+
author: livePull.user?.login ?? item.author,
15019+
createdAt: livePull.created_at ?? item.createdAt,
15020+
comments: context.comments,
15021+
reviews,
15022+
inlineComments: context.pullReviewComments ?? [],
15023+
timeline: context.timeline,
15024+
headActivityAtMs,
15025+
staleMinAgeDays,
15026+
requireHeadActivityEvidence: true,
15027+
})
15028+
) {
15029+
return null;
15030+
}
1481615031
return {
1481715032
closeReason: "low_signal_unmergeable_pr",
1481815033
summary:
@@ -14893,7 +15108,7 @@ function pullRequestClosePromotion(
1489315108
// A live canonical candidate that is itself unsafe cannot justify treating the
1489415109
// source as generic low-signal work. Missing or non-covering references can.
1489515110
if (linkedSupersession.unsafeReason) return null;
14896-
return staleFRatedPullRequestPromotion(markdown, item, staleMinAgeDays);
15111+
return staleFRatedPullRequestPromotion(markdown, item, context, staleMinAgeDays);
1489715112
}
1489815113

1489915114
function workPlanPathForReport(file: string, plansDir = defaultPlansDir()): string {
@@ -21911,7 +22126,7 @@ function applyDecisionsCommandInner(args: Args, runtimeBudget: GitHubRuntimeBudg
2191122126
}
2191222127
const lowSignalBlockReason =
2191322128
closeReason === "low_signal_unmergeable_pr"
21914-
? lowSignalUnmergeablePrApplyBlockReason(number)
22129+
? lowSignalUnmergeablePrApplyBlockReasonSafe(number, staleMinAgeDays)
2191522130
: null;
2191622131
if (lowSignalBlockReason) {
2191722132
if (markApplySkipped("kept_open", lowSignalBlockReason)) break;

‎src/repair/comment-router-core.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,7 @@ function trustedCloseReasonSpecificBlockReason({
12351235
productDirection: false,
12361236
});
12371237
if (humanSignal) return humanSignal;
1238+
return "low_signal_unmergeable_pr closes require apply-decisions live conflict and author-activity proof";
12381239
}
12391240
return null;
12401241
}

0 commit comments

Comments
 (0)