ReviewOS

also looking at this

stacks/buddy

fix(ci): give the repair loop the guard it already had

#1407
Merged glennmichael123 wants to merge fix/wire-ci-repair-attempt-guard into main
10 files +229 -34
src/ci/attempts.tsadded+81-0
Changes to src/ci/attempts.ts
@@ -0,0 +1,81 @@
1/**
2 * How many times Buddy has already tried to repair a pull request.
3 *
4 * `attemptFix` refuses to keep trying past `maxAttempts`, but it can only do
5 * that if someone tells it how many attempts came before — and a CI job has no
6 * memory of its own. The count therefore lives on the pull request, the same
7 * place review state does, because it is the only store that survives the run.
8 */
9
10/** Marker carrying the attempt count, hidden in the pull request body. */
11const MARKER_REGEX = /<!--\s*buddy:fix-ci\s+v(\d+)\s*([\s\S]*?)-->/
12
13/** Schema version emitted by this build. */
14export const FIX_ATTEMPTS_VERSION = 1
15
16/** Attempt bookkeeping for one pull request. */
17export interface FixAttemptState {
18 /** Repairs attempted so far, successful or not */
19 attempts: number
20 /** ISO timestamp of the most recent attempt */
21 lastAttemptAt: string
22}
23
24/**
25 * Read the attempt count out of a pull request body.
26 *
27 * A missing or unreadable marker reads as zero attempts rather than as an
28 * error: the guard exists to stop a loop, and refusing to act because the
29 * bookkeeping is unparseable would be a worse failure than one extra try.
30 *
31 * @param body - Pull request body, possibly null
32 * @returns The recorded state, or `null` when absent or unreadable
33 */
34export function parseFixAttempts(body: string | null | undefined): FixAttemptState | null {
35 if (!body)
36 return null
37
38 const match = body.match(MARKER_REGEX)
39 if (!match)
40 return null
41
42 try {
43 const parsed = JSON.parse(match[2].trim()) as Partial<FixAttemptState>
44 const attempts = Number(parsed.attempts)
45 if (!Number.isFinite(attempts) || attempts < 0)
46 return null
47
48 return {
49 attempts: Math.floor(attempts),
50 lastAttemptAt: typeof parsed.lastAttemptAt === 'string' ? parsed.lastAttemptAt : new Date(0).toISOString(),
51 }
52 }
53 catch {
54 return null
55 }
56}
57
58/**
59 * Record an attempt on a pull request body.
60 *
61 * Counted per pull request rather than per commit, deliberately. Buddy's own
62 * repair pushes a commit, so keying the count to the head sha would reset it
63 * every time a fix landed and defeat the guard it exists to provide.
64 *
65 * @param body - Current pull request body
66 * @param attempts - Total attempts including the one just made
67 * @param now - Timestamp to record
68 * @returns The body with exactly one, current, marker
69 * @example
70 * ```ts
71 * const attempts = (parseFixAttempts(pr.body)?.attempts ?? 0) + 1
72 * await provider.updatePullRequest(number, { body: upsertFixAttempts(pr.body, attempts) })
73 * ```
74 */
75export function upsertFixAttempts(body: string | null | undefined, attempts: number, now: Date = new Date()): string {
76 const state: FixAttemptState = { attempts, lastAttemptAt: now.toISOString() }
77 const marker = `<!-- buddy:fix-ci v${FIX_ATTEMPTS_VERSION}\n${JSON.stringify(state)}\n-->`
78 const withoutMarker = (body ?? '').replace(MARKER_REGEX, '').trimEnd()
79
80 return `${withoutMarker}\n\n${marker}`
81}