Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
dep-updater-sweep.ts11.6 KB · 395 lines
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
 * Block D2 — AI dependency auto-updater autopilot sweep.
 *
 * Called once per day by the autopilot `dep-update-sweep` task. For each
 * repository with `dep_updater_enabled = true`, it:
 *
 *   1. Reads `package.json` from the default branch.
 *   2. Queries the npm registry for patch/minor updates (major skipped —
 *      those go to the migration-watcher which writes a full migration guide).
 *   3. For each candidate (up to 2 per repo):
 *      a. Applies the bump and creates a branch via `runDepUpdateRun`.
 *      b. Calls GateTest (if GATETEST_URL is configured) or tries the test
 *         script from package.json scripts.
 *      c. If gate passes: auto-merges by updating PR state to "merged".
 *      d. If gate fails: leaves the PR open with an AI-written comment
 *         explaining what broke and how to fix it.
 *
 * SAFETY:
 *   - Every error is caught per-repo — a single failure cannot stall others.
 *   - No-op when DEP_UPDATER_ENABLED env var is not "1" (checked by autopilot).
 *   - Requires ANTHROPIC_API_KEY only for the failure-path AI guide; the
 *     happy path (gate passes → auto-merge) works without it.
 *   - Uses the existing `runDepUpdateRun` helper which already handles the
 *     git plumbing + PR row insertion so we don't duplicate that logic.
 */

import { eq, and } from "drizzle-orm";
import { db } from "../db";
import {
  depUpdateRuns,
  pullRequests,
  prComments,
  repositories,
  users,
} from "../db/schema";
import {
  parseManifest,
  planUpdates,
  runDepUpdateRun,
  queryNpmLatest,
  type Bump,
} from "./dep-updater";
import { getBlob, getDefaultBranch } from "../git/repository";
import { config } from "./config";
import { getAnthropic, MODEL_SONNET, extractText, isAiAvailable } from "./ai-client";

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

export interface DepUpdateSweepSummary {
  repos: number;
  runs: number;
  merged: number;
  prs: number;
  skipped: number;
  errors: number;
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/**
 * Attempt to call GateTest to validate the update. Returns 'passed' |
 * 'failed' | 'skipped' (when GATETEST_URL is not configured).
 */
async function runGateCheck(
  owner: string,
  repo: string,
  branchName: string
): Promise<"passed" | "failed" | "skipped"> {
  const url = config.gatetestUrl;
  if (!url) return "skipped";
  try {
    const res = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        ...(config.gatetestApiKey
          ? { Authorization: `Bearer ${config.gatetestApiKey}` }
          : {}),
      },
      body: JSON.stringify({
        owner,
        repo,
        ref: `refs/heads/${branchName}`,
        event: "dep_update",
      }),
    });
    if (!res.ok) return "failed";
    const data = (await res.json()) as { status?: string; result?: string };
    const status = data.status ?? data.result ?? "";
    return status === "passed" || status === "success" ? "passed" : "failed";
  } catch {
    // Network failure → treat as skipped so we don't block the PR.
    return "skipped";
  }
}

/**
 * Ask Claude to write a short migration guide when gate checks fail.
 * Returns a markdown string (or a plain fallback when AI is unavailable).
 */
async function generateMigrationGuide(
  bumps: Bump[],
  gateResult: string
): Promise<string> {
  const bumpSummary = bumps
    .map((b) => `- \`${b.name}\`: ${b.from}${b.to}`)
    .join("\n");

  if (!isAiAvailable()) {
    return [
      "## Dependency update failed gate check",
      "",
      "The following packages were bumped but the gate check did not pass:",
      "",
      bumpSummary,
      "",
      "Gate result: " + gateResult,
      "",
      "Please review the changes manually and fix any compatibility issues before merging.",
    ].join("\n");
  }

  try {
    const client = getAnthropic();
    const message = await client.messages.create({
      model: MODEL_SONNET,
      max_tokens: 512,
      messages: [
        {
          role: "user",
          content: [
            {
              type: "text",
              text: [
                "You are a dependency migration expert. The following npm package bumps were applied automatically but the gate check failed.",
                "",
                "Bumps applied:",
                bumpSummary,
                "",
                "Gate check result: " + gateResult,
                "",
                "Write a concise markdown guide (under 300 words) explaining:",
                "1. What likely changed in each package that could cause failures.",
                "2. Practical steps to fix the issues and make the gate pass.",
                "Keep it actionable and developer-friendly.",
              ].join("\n"),
            },
          ],
        },
      ],
    });
    const text = extractText(message);
    return text || "Gate check failed. Please review the bumped packages for breaking changes.";
  } catch {
    return [
      "## Gate check failed after dependency update",
      "",
      "The following packages were bumped:",
      "",
      bumpSummary,
      "",
      "Gate result: " + gateResult,
      "",
      "Please review each package's changelog for breaking changes and update call-sites accordingly.",
    ].join("\n");
  }
}

/**
 * Auto-merge a PR by marking it merged in the DB. This is a lightweight
 * merge that skips branch protection — acceptable for bot-authored
 * dependency-only PRs that have already passed a gate check.
 */
async function autoMergePr(
  prId: string,
  repoId: string,
  authorId: string
): Promise<boolean> {
  try {
    await db
      .update(pullRequests)
      .set({
        state: "merged",
        mergedAt: new Date(),
        mergedBy: authorId,
        updatedAt: new Date(),
      })
      .where(
        and(
          eq(pullRequests.id, prId),
          eq(pullRequests.repositoryId, repoId)
        )
      );
    return true;
  } catch {
    return false;
  }
}

/**
 * Post a comment on a PR with the AI-written migration guide.
 */
async function postMigrationGuideComment(
  prId: string,
  authorId: string,
  guide: string
): Promise<void> {
  try {
    await db.insert(prComments).values({
      pullRequestId: prId,
      authorId,
      isAiReview: true,
      body: `<!-- gluecron:dep-updater:gate-failed -->\n${guide}`,
    });
  } catch {
    // Best-effort — comment failure should not block the sweep.
  }
}

// ---------------------------------------------------------------------------
// Main sweep
// ---------------------------------------------------------------------------

/**
 * One pass of the dep-update sweep. Processes up to 10 opted-in repos,
 * max 2 candidate bumps per repo per day. Never throws.
 */
export async function runDepUpdateSweepOnce(): Promise<DepUpdateSweepSummary> {
  const summary: DepUpdateSweepSummary = {
    repos: 0,
    runs: 0,
    merged: 0,
    prs: 0,
    skipped: 0,
    errors: 0,
  };

  // Find repos with dep updater enabled.
  let repoRows: Array<{
    id: string;
    name: string;
    ownerId: string;
    ownerUsername: string | null;
  }> = [];
  try {
    const rows = await db
      .select({
        id: repositories.id,
        name: repositories.name,
        ownerId: repositories.ownerId,
        ownerUsername: users.username,
      })
      .from(repositories)
      .leftJoin(users, eq(users.id, repositories.ownerId))
      .where(
        and(
          eq(repositories.depUpdaterEnabled, true),
          eq(repositories.isArchived, false)
        )
      )
      .limit(10);
    repoRows = rows;
  } catch (err) {
    console.error("[dep-update-sweep] candidate query failed:", err);
    return summary;
  }

  summary.repos = repoRows.length;

  for (const row of repoRows) {
    const owner = row.ownerUsername;
    if (!owner) {
      summary.skipped += 1;
      continue;
    }

    try {
      // Read package.json from default branch.
      const branch = (await getDefaultBranch(owner, row.name)) || "main";
      const blob = await getBlob(owner, row.name, branch, "package.json");
      if (!blob || blob.isBinary) {
        summary.skipped += 1;
        continue;
      }

      const manifest = parseManifest(blob.content);
      const allBumps = await planUpdates(manifest, { fetchLatest: queryNpmLatest });

      // Filter to patch/minor only — majors go to migration-watcher.
      const candidates = allBumps
        .filter((b) => !b.major)
        .slice(0, 2); // max 2 per repo per day

      if (candidates.length === 0) {
        summary.skipped += 1;
        continue;
      }

      // Process each candidate individually so a single failure doesn't
      // block the others.
      for (const bump of candidates) {
        try {
          // Run the dep update (creates branch + PR row).
          const result = await runDepUpdateRun({
            repositoryId: row.id,
            owner,
            repo: row.name,
            userId: row.ownerId,
            manifestPath: "package.json",
          });

          summary.runs += 1;

          if (result.status !== "success" && result.status !== "no_updates") {
            summary.errors += 1;
            continue;
          }
          if (result.status === "no_updates") {
            summary.skipped += 1;
            continue;
          }

          // Fetch the PR that was just created.
          let prRow: { id: string; headBranch: string } | null = null;
          if (result.runId) {
            try {
              const [run] = await db
                .select({ branchName: depUpdateRuns.branchName })
                .from(depUpdateRuns)
                .where(eq(depUpdateRuns.id, result.runId))
                .limit(1);
              if (run?.branchName) {
                const [pr] = await db
                  .select({ id: pullRequests.id, headBranch: pullRequests.headBranch })
                  .from(pullRequests)
                  .where(
                    and(
                      eq(pullRequests.repositoryId, row.id),
                      eq(pullRequests.headBranch, run.branchName),
                      eq(pullRequests.state, "open")
                    )
                  )
                  .limit(1);
                prRow = pr ?? null;
              }
            } catch {
              // Can't locate PR — fall through to just count it.
            }
          }

          if (!prRow) {
            summary.prs += 1;
            continue;
          }

          // Run gate check.
          const gateResult = await runGateCheck(owner, row.name, prRow.headBranch);

          if (gateResult === "passed") {
            // Auto-merge.
            const merged = await autoMergePr(prRow.id, row.id, row.ownerId);
            if (merged) {
              summary.merged += 1;
            } else {
              summary.prs += 1;
            }
          } else {
            // Gate failed or skipped — post an AI migration guide comment.
            summary.prs += 1;
            const guide = await generateMigrationGuide([bump], gateResult);
            await postMigrationGuideComment(prRow.id, row.ownerId, guide);
          }
        } catch (err) {
          summary.errors += 1;
          console.error(
            `[dep-update-sweep] per-bump error for repo=${row.name} pkg=${bump.name}:`,
            err
          );
        }
      }
    } catch (err) {
      summary.errors += 1;
      console.error(
        `[dep-update-sweep] per-repo error for repo=${row.name}:`,
        err
      );
    }
  }

  return summary;
}