/ opencodehub / src / lib / queue-worker.ts
Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
334 lines · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { getDatabase, schema } from "@/db";
import { eq, and, asc } from "drizzle-orm";
import { logger } from "@/lib/logger";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { mergeBranch, createSpeculativeBranch } from "@/lib/git";
import { acquireRepo, releaseRepo } from "@/lib/git-storage";
import { acquireLock, isDistributedLocking } from "@/lib/distributed-lock";

export class QueueWorker {
    private isProcessing = false;

    async processQueue(repositoryId: string) {
        if (this.isProcessing) return;
        this.isProcessing = true;
        const lockKey = `merge-queue-worker:${repositoryId}`;
        const distributedLock = await acquireLock(lockKey, {
            ttlSeconds: 120,
            retryCount: 0,
        });

        if (!distributedLock) {
            logger.debug({ repositoryId, lockKey }, "Queue worker skipped - lock held by another instance");
            this.isProcessing = false;
            return;
        }

        const db = getDatabase() as NodePgDatabase<typeof schema>;

        try {
            // 1. Check if anything is currently running
            const runningItem = await db.query.mergeQueueItems.findFirst({
                where: and(
                    eq(schema.mergeQueueItems.repositoryId, repositoryId),
                    eq(schema.mergeQueueItems.status, "running")
                ),
                with: {
                    repository: {
                        with: { owner: true }
                    },
                    pullRequest: true
                }
            });

            let itemToProcess: typeof runningItem | null = runningItem ?? null;
            if (runningItem) {
                const runtime = Date.now() - (runningItem.startedAt?.getTime() || 0);
                if (runtime > 10 * 60 * 1000) {
                    logger.warn(`Queue item ${runningItem.id} timed out. Marking failed.`);
                    await db.update(schema.mergeQueueItems)
                        .set({ status: "failed", completedAt: new Date() })
                        .where(eq(schema.mergeQueueItems.id, runningItem.id));
                    itemToProcess = null;
                } else {
                    const ciState = await this.getQueueItemCIState(runningItem.pullRequestId, db);
                    if (ciState === "pending") {
                        logger.info(`Queue item ${runningItem.id} waiting for CI checks.`);
                        return;
                    }
                    if (ciState === "failed") {
                        await db.update(schema.mergeQueueItems)
                            .set({ status: "failed", completedAt: new Date() })
                            .where(eq(schema.mergeQueueItems.id, runningItem.id));
                        logger.warn(`Queue item ${runningItem.id} failed due to failed checks.`);
                        itemToProcess = null;
                    }
                }
            }

            // 2. Pick next item
            if (!itemToProcess) {
                const nextItem = await db.query.mergeQueueItems.findFirst({
                    where: and(
                        eq(schema.mergeQueueItems.repositoryId, repositoryId),
                        eq(schema.mergeQueueItems.status, "queued")
                    ),
                    orderBy: [asc(schema.mergeQueueItems.queuedAt)],
                    with: {
                        repository: {
                            with: { owner: true }
                        },
                        pullRequest: true
                    }
                });

                if (!nextItem) {
                    return;
                }

                await db.update(schema.mergeQueueItems)
                    .set({
                        status: "running",
                        startedAt: new Date(),
                        attemptCount: (nextItem.attemptCount || 0) + 1,
                        lastAttemptAt: new Date()
                    })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));

                itemToProcess = {
                    ...nextItem,
                    status: "running",
                } as typeof nextItem;
            }

            if (!itemToProcess) return;
            const nextItem = itemToProcess;

            logger.info(`Starting merge for PR ${nextItem.pullRequestId} (${nextItem.repository.owner.username}/${nextItem.repository.name})`);

            // 3b. Trigger Speculative Builds for upcoming PRs
            // We do this concurrently while the main PR is entering its build phase
            this.triggerSpeculativeBuilds(repositoryId, nextItem, db).catch(err => {
                logger.error("Failed to trigger speculative builds", err);
            });

            const ciState = await this.getQueueItemCIState(nextItem.pullRequestId, db);
            if (ciState === "pending") {
                logger.info(`Queue item ${nextItem.id} still waiting for CI checks.`);
                return;
            }
            if (ciState === "failed") {
                await db.update(schema.mergeQueueItems)
                    .set({ status: "failed", completedAt: new Date() })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));
                logger.warn(`Queue item ${nextItem.id} failed due to failed checks.`);
                return;
            }

            // 4. Execution
            // A. Acquire Repo (Local or Cloud)
            const repoPath = await acquireRepo(nextItem.repository.owner.username, nextItem.repository.name);

            // Attempt Merge Locally first to check conflicts
            const result = await mergeBranch(repoPath, nextItem.pullRequest.baseBranch, nextItem.pullRequest.headBranch);

            if (result.success) {
                // Success!
                // 1. Update PR Status
                const now = new Date();
                await db.update(schema.pullRequests)
                    .set({
                        state: "merged",
                        isMerged: true,
                        mergedAt: now,
                        // mergedById: system user?
                        updatedAt: now,
                    })
                    .where(eq(schema.pullRequests.id, nextItem.pullRequestId));

                // 2. Update Queue Item
                await db.update(schema.mergeQueueItems)
                    .set({ status: "merged", completedAt: now })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));

                // 3. Sync Logic (Release Repo with modified=true)
                // Since mergeBranch modified the repo (update-ref), we must push back.
                await releaseRepo(nextItem.repository.owner.username, nextItem.repository.name, true);

                logger.info(`PR ${nextItem.pullRequestId} merged successfully via queue.`);
            } else {
                // Failure (Conflict)
                await db.update(schema.mergeQueueItems)
                    .set({ status: "failed", completedAt: new Date() })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));

                // Release without sync (no changes to base, strict speaking merge-tree doesn't touch base if fail? 
                // Actually mergeBranch implementation might have failed before update-ref or during conflict check.
                await releaseRepo(nextItem.repository.owner.username, nextItem.repository.name, false);

                logger.warn(`PR ${nextItem.pullRequestId} failed to merge: ${result.message}`);
            }

        } catch (error) {
            logger.error("Error processing merge queue", error);
            // Try to release repo if stuck?
            // We don't have scope here easily, but acquireRepo is safe to re-acquire (cleans up).
        } finally {
            await distributedLock.release();
            this.isProcessing = false;
            // Trigger next immediately?
            // this.processQueue(repositoryId);
        }
    }

    private async getQueueItemCIState(
        pullRequestId: string,
        db: NodePgDatabase<typeof schema>
    ): Promise<"pending" | "passed" | "failed"> {
        // 1. External/ingested checks (GitHub-style check runs)
        const checks = await db.query.pullRequestChecks.findMany({
            where: eq(schema.pullRequestChecks.pullRequestId, pullRequestId),
        });

        if (checks.length > 0) {
            const hasPending = checks.some((check) => check.status !== "completed");
            if (hasPending) return "pending";
            const hasFailed = checks.some((check) =>
                ["failure", "cancelled", "timed_out", "action_required"].includes(check.conclusion || "")
            );
            if (hasFailed) return "failed";
            return "passed";
        }

        // 2. Native pipeline runs (WS1-15): gate on the persisted workflow
        //    runs for this PR's head branch (push-triggered or speculative)
        const pr = await db.query.pullRequests.findFirst({
            where: eq(schema.pullRequests.id, pullRequestId),
        });
        if (pr) {
            const runs = await db.query.workflowRuns.findMany({
                where: eq(schema.workflowRuns.headBranch, pr.headBranch),
                orderBy: (runs, { desc }) => [desc(runs.runNumber)],
                limit: 5,
            });
            if (runs.length > 0) {
                if (runs.some((r) => r.status === "queued" || r.status === "in_progress")) {
                    return "pending";
                }
                const latest = runs[0];
                if (latest.status === "completed") {
                    if (latest.conclusion === "failure") return "failed";
                    if (latest.conclusion === "success") return "passed";
                }
            }
        }

        // 3. No checks and no native runs: allow the queue to proceed.
        return "passed";
    }

    /**
     * Look ahead in the queue and trigger speculative builds
     */
    private async triggerSpeculativeBuilds(
        repositoryId: string,
        currentItem: any,
        db: NodePgDatabase<typeof schema>
    ) {
        // Fetch next 2 items
        const lookaheadItems = await db.query.mergeQueueItems.findMany({
            where: and(
                eq(schema.mergeQueueItems.repositoryId, repositoryId),
                eq(schema.mergeQueueItems.status, "queued")
            ),
            orderBy: [asc(schema.mergeQueueItems.queuedAt)],
            limit: 2,
        });

        if (lookaheadItems.length === 0) return;

        logger.info(`Found ${lookaheadItems.length} items for speculative execution`);

        const repo = currentItem.repository;
        const repoPath = await acquireRepo(repo.owner.username, repo.name); // Re-acquire safe? Yes

        try {
            // Build the chain of PRs: [Current, Next 1, Next 2]
            const prChain = [
                { headBranch: currentItem.pullRequest.headBranch, number: currentItem.pullRequest.number }
            ];

            for (const item of lookaheadItems) {
                // If already has an execution branch, skip (assume already handling)
                if (item.executionBranch) continue;

                // Fetch PR details
                const pr = await db.query.pullRequests.findFirst({
                    where: eq(schema.pullRequests.id, item.pullRequestId)
                });

                if (!pr) continue;

                prChain.push({ headBranch: pr.headBranch, number: pr.number });

                logger.info(`Creating speculative branch for PR #${pr.number} (Chain: ${prChain.map(p => p.number).join("+")})`);

                const result = await createSpeculativeBranch(
                    repoPath,
                    pr.baseBranch, // Assuming all target same base
                    [...prChain] // Copy array
                );

                if (result.success) {
                    await db.update(schema.mergeQueueItems)
                        .set({
                            executionBranch: result.branchName,
                            // We don't set status to 'running' to keep them in 'queued' for the main worker loop,
                            // but having executionBranch implies speculative CI is running.
                        })
                        .where(eq(schema.mergeQueueItems.id, item.id));

                    // ── WS1-09: actually RUN the speculative CI ──────────
                    try {
                        const { triggerSpecWorkflow } = await import("@/lib/workflows");
                        const specSha = await (async () => {
                            const { getGit } = await import("@/lib/git");
                            return getGit(repoPath).revparse([result.branchName]).catch(() => "");
                        })();
                        if (specSha) {
                            triggerSpecWorkflow({
                                repositoryId,
                                repositoryPath: repoPath,
                                specBranch: result.branchName,
                                baseBranch: pr.baseBranch,
                                commitSha: specSha.trim(),
                            }).catch((err) => logger.error({ err }, "Speculative CI trigger failed"));
                        }
                    } catch (err) {
                        logger.warn({ err }, "Speculative CI not triggered");
                    }

                    logger.info(`Speculative branch created: ${result.branchName}`);
                } else {
                    logger.warn(`Failed to create speculative branch: ${result.message}`);
                }
            }
        } finally {
            // We don't release here because the main loop holds the lock? 
            // acquireRepo is usually re-entrant for same process/path or handles locking. 
            // Checks if git-storage supports concurrent access. 
            // Assuming it does simply resolve path.
        }
    }
}

export const queueWorker = new QueueWorker();

export function getQueueWorkerScalingReadiness() {
    return {
        inProcessGuardEnabled: true,
        distributedLockingEnabled: isDistributedLocking,
        multiInstanceSafe: isDistributedLocking,
    };
}