|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import { appendFileSync } from "node:fs"; |
| 3 | +import { pathToFileURL } from "node:url"; |
| 4 | + |
| 5 | +const TRUSTED_AUTHORS = new Set( |
| 6 | + [ |
| 7 | + "clawsweeper", |
| 8 | + "clawsweeper[bot]", |
| 9 | + "openclaw-clawsweeper[bot]", |
| 10 | + process.env.CLAWSWEEPER_COMMENT_AUTHOR_LOGIN?.toLowerCase(), |
| 11 | + ].filter((value): value is string => Boolean(value)), |
| 12 | +); |
| 13 | +const SOURCE_REVISION_PATTERN = |
| 14 | + /<!--\s+clawsweeper-review-version\b[^>]*\bsource_revision=([0-9a-f]{64})\b[^>]*-->/g; |
| 15 | +const REVIEWED_SHA_PATTERN = /<!--\s+clawsweeper-review-version\b[^>]*\bsha=([^\s>]+)\b[^>]*-->/g; |
| 16 | +const SETTLING_WINDOW_MS = 30_000; |
| 17 | +type JsonObject = Record<string, unknown>; |
| 18 | + |
| 19 | +function object(value: unknown): JsonObject { |
| 20 | + return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonObject) : {}; |
| 21 | +} |
| 22 | + |
| 23 | +function scalar(value: unknown): string { |
| 24 | + if (value === null || value === undefined) return ""; |
| 25 | + if (typeof value === "string") return value; |
| 26 | + if (typeof value === "number" || typeof value === "boolean") return String(value); |
| 27 | + return JSON.stringify(value); |
| 28 | +} |
| 29 | + |
| 30 | +function login(value: unknown): string { |
| 31 | + return scalar(object(value).login).trim().toLowerCase(); |
| 32 | +} |
| 33 | + |
| 34 | +function labelName(value: unknown): string { |
| 35 | + return scalar(object(value).name || value) |
| 36 | + .trim() |
| 37 | + .toLowerCase(); |
| 38 | +} |
| 39 | + |
| 40 | +function ignorableOwnedLabel(label: string): boolean { |
| 41 | + if (label === "proof: override") return false; |
| 42 | + return ( |
| 43 | + /^(?:status|rating|proof|merge-risk|impact|issue-rating):/.test(label) || |
| 44 | + /^p[0-3]$/.test(label) || |
| 45 | + label === "maturity:stable" || |
| 46 | + (label.startsWith("clawsweeper:") && |
| 47 | + !["clawsweeper:human-review", "clawsweeper:manual-only", "clawsweeper:bulk-filed"].includes( |
| 48 | + label, |
| 49 | + )) || |
| 50 | + [ |
| 51 | + "clawsweeper-recovery-stuck", |
| 52 | + "no-stale", |
| 53 | + "stale", |
| 54 | + "feature: ✨ showcase", |
| 55 | + "good first issue", |
| 56 | + "mantis: telegram-visible-proof", |
| 57 | + "triage: needs-real-behavior-proof", |
| 58 | + ].includes(label) |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function trustedClawSweeperComment(comment: JsonObject): boolean { |
| 63 | + return TRUSTED_AUTHORS.has(login(comment.user) || scalar(comment.author).trim().toLowerCase()); |
| 64 | +} |
| 65 | + |
| 66 | +export function scheduledReviewSemanticSourceRevision( |
| 67 | + issue: unknown, |
| 68 | + comments: readonly unknown[], |
| 69 | +): string { |
| 70 | + const source = object(issue); |
| 71 | + const snapshot = { |
| 72 | + title: scalar(source.title), |
| 73 | + body: scalar(source.body), |
| 74 | + labels: (Array.isArray(source.labels) ? source.labels : []) |
| 75 | + .map(labelName) |
| 76 | + .filter(Boolean) |
| 77 | + .filter((label) => !ignorableOwnedLabel(label)) |
| 78 | + .sort(), |
| 79 | + comments: comments |
| 80 | + .map(object) |
| 81 | + .filter((comment) => !trustedClawSweeperComment(comment)) |
| 82 | + .map((comment) => ({ |
| 83 | + id: scalar(comment.id), |
| 84 | + author: scalar(login(comment.user) || comment.author), |
| 85 | + body: scalar(comment.body), |
| 86 | + updated_at: scalar(comment.updated_at ?? comment.updatedAt ?? comment.created_at), |
| 87 | + })) |
| 88 | + .sort((left, right) => |
| 89 | + `${left.id}:${left.updated_at}`.localeCompare(`${right.id}:${right.updated_at}`), |
| 90 | + ), |
| 91 | + }; |
| 92 | + return createHash("sha256").update(JSON.stringify(snapshot)).digest("hex"); |
| 93 | +} |
| 94 | + |
| 95 | +function durableSourceRevision(body: string): string | null { |
| 96 | + let revision: string | null = null; |
| 97 | + for (const match of body.matchAll(SOURCE_REVISION_PATTERN)) revision = match[1] ?? null; |
| 98 | + return revision; |
| 99 | +} |
| 100 | + |
| 101 | +function durableReviewedSha(body: string): string | null { |
| 102 | + let sha: string | null = null; |
| 103 | + for (const match of body.matchAll(REVIEWED_SHA_PATTERN)) sha = match[1] ?? null; |
| 104 | + return sha; |
| 105 | +} |
| 106 | + |
| 107 | +export function classifyScheduledReviewNoop(options: { |
| 108 | + decision: unknown; |
| 109 | + issue: unknown; |
| 110 | + comments: readonly unknown[]; |
| 111 | + liveHeadSha?: string; |
| 112 | +}): { noop: boolean; reason: string } { |
| 113 | + const decision = object(options.decision); |
| 114 | + const issue = object(options.issue); |
| 115 | + if (decision.sourceAction !== "scheduled_hot_intake") { |
| 116 | + return { noop: false, reason: "not_scheduled_hot" }; |
| 117 | + } |
| 118 | + if (issue.state !== "open" || issue.locked === true) { |
| 119 | + return { noop: false, reason: "live_state_requires_guard" }; |
| 120 | + } |
| 121 | + const queuedUpdatedAt = Date.parse(scalar(decision.sourceUpdatedAt)); |
| 122 | + const liveUpdatedAt = Date.parse(scalar(issue.updated_at)); |
| 123 | + if (!Number.isFinite(queuedUpdatedAt) || queuedUpdatedAt !== liveUpdatedAt) { |
| 124 | + return { noop: false, reason: "queued_source_drift" }; |
| 125 | + } |
| 126 | + const liveRevision = scheduledReviewSemanticSourceRevision(issue, options.comments); |
| 127 | + for (const comment of [...options.comments].map(object).reverse()) { |
| 128 | + if (!trustedClawSweeperComment(comment)) continue; |
| 129 | + const recordedRevision = durableSourceRevision(scalar(comment.body)); |
| 130 | + const recordedSha = durableReviewedSha(scalar(comment.body)); |
| 131 | + const commentUpdatedAt = Date.parse(scalar(comment.updated_at ?? comment.created_at)); |
| 132 | + if ( |
| 133 | + recordedRevision === liveRevision && |
| 134 | + (!issue.pull_request || |
| 135 | + (Boolean(options.liveHeadSha) && recordedSha === options.liveHeadSha)) && |
| 136 | + Number.isFinite(commentUpdatedAt) && |
| 137 | + liveUpdatedAt >= commentUpdatedAt && |
| 138 | + liveUpdatedAt - commentUpdatedAt <= SETTLING_WINDOW_MS |
| 139 | + ) { |
| 140 | + return { noop: true, reason: "trusted_owned_activity_only" }; |
| 141 | + } |
| 142 | + } |
| 143 | + return { noop: false, reason: "missing_or_changed_durable_source" }; |
| 144 | +} |
| 145 | + |
| 146 | +function run(): void { |
| 147 | + const result = classifyScheduledReviewNoop({ |
| 148 | + decision: JSON.parse(process.env.CLAIM_DECISION || "{}"), |
| 149 | + issue: JSON.parse(process.env.LIVE_ITEM || "{}"), |
| 150 | + comments: JSON.parse(process.env.LIVE_COMMENTS || "[]"), |
| 151 | + liveHeadSha: process.env.LIVE_HEAD_SHA, |
| 152 | + }); |
| 153 | + const output = `scheduled_noop=${result.noop}\nscheduled_noop_reason=${result.reason}\n`; |
| 154 | + if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, output); |
| 155 | + else process.stdout.write(output); |
| 156 | +} |
| 157 | + |
| 158 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) run(); |
0 commit comments