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
streaming-review.ts9.1 KB · 283 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
/**
 * Streaming PR review — real-time Claude review via SSE.
 *
 * Additive to the existing batch ai-review.ts. This module streams tokens
 * from Claude as they arrive, allowing the browser to display the review
 * in real time rather than waiting 10–30 seconds for a batch result.
 *
 * Usage:
 *   for await (const token of streamPrReview(...)) {
 *     // send token as SSE event
 *   }
 */

import { eq, and, like } from "drizzle-orm";
import { db } from "../db";
import { pullRequests, prComments } from "../db/schema";
import { getRepoPath } from "../git/repository";
import { getAnthropic, isAiAvailable, MODEL_SONNET } from "./ai-client";
import { getBotUserIdOrFallback } from "./bot-user";

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

export interface StreamingReviewToken {
  type: "token" | "section_start" | "section_end" | "done" | "error";
  content?: string;  // for type="token"
  section?: string;  // for section_start/end: "summary" | "finding" | "verdict"
  error?: string;    // for type="error"
}

// ---------------------------------------------------------------------------
// In-memory streaming state
// ---------------------------------------------------------------------------

/** PR IDs currently being streamed. Prevents duplicate concurrent streams. */
const _streamingPrs = new Set<string>();

/**
 * Returns true when a streaming review is already in progress for the given
 * PR id.
 */
export function isReviewStreaming(prId: string): boolean {
  return _streamingPrs.has(prId);
}

// ---------------------------------------------------------------------------
// Marker embedded in the saved comment body so we can detect duplicates
// ---------------------------------------------------------------------------

export const STREAM_REVIEW_MARKER = "<!-- gluecron:stream-review:v1 -->";

/** Max bytes of diff sent to Claude. Matches the batch reviewer's cap. */
const DIFF_BYTE_CAP = 100_000;

// ---------------------------------------------------------------------------
// Section detection helpers
// ---------------------------------------------------------------------------

/**
 * Detect section transitions by examining accumulated lines.
 *
 * Sections (in order):
 *   1. "summary"  — opening paragraph
 *   2. "finding"  — numbered list items (lines starting with a digit + ".")
 *   3. "verdict"  — line starting with "Verdict:"
 *
 * Returns the section name when a transition is detected, or null otherwise.
 */
function detectSectionTransition(
  accumulated: string,
  prevSection: string | null
): string | null {
  // Split into lines so we can inspect the latest complete line.
  const lines = accumulated.split("\n");
  // Look at lines from the end (most recent first)
  for (let i = lines.length - 1; i >= 0; i--) {
    const line = lines[i].trim();
    if (!line) continue;

    // Verdict line — always wins
    if (line.toLowerCase().startsWith("verdict:") && prevSection !== "verdict") {
      return "verdict";
    }
    // Finding: numbered list item (e.g. "1. ", "2. ", "12. ")
    if (/^\d+\.\s/.test(line) && prevSection === "summary") {
      return "finding";
    }
    break; // Only inspect the last non-empty line
  }
  return null;
}

// ---------------------------------------------------------------------------
// Core streaming generator
// ---------------------------------------------------------------------------

/**
 * Start a streaming review for a PR.
 *
 * Yields `StreamingReviewToken` objects as Claude produces them.
 * On completion, saves the full review text as a PR comment
 * (idempotent — skipped when the marker already exists in comments).
 */
export async function* streamPrReview(
  prId: string,
  ownerName: string,
  repoName: string,
  baseBranch: string,
  headBranch: string
): AsyncGenerator<StreamingReviewToken> {
  // Guard: AI must be configured
  if (!isAiAvailable()) {
    yield { type: "error", error: "AI not available — ANTHROPIC_API_KEY is not set" };
    return;
  }

  // Guard: prevent duplicate concurrent streams
  if (_streamingPrs.has(prId)) {
    yield { type: "error", error: "A streaming review is already in progress for this PR" };
    return;
  }

  // Idempotency: skip if a stream-review comment already exists
  try {
    const [existing] = await db
      .select({ id: prComments.id })
      .from(prComments)
      .where(
        and(
          eq(prComments.pullRequestId, prId),
          eq(prComments.isAiReview, true),
          like(prComments.body, `%${STREAM_REVIEW_MARKER}%`)
        )
      )
      .limit(1);
    if (existing) {
      yield { type: "error", error: "Streaming review already completed for this PR" };
      return;
    }
  } catch {
    // DB check failed — proceed optimistically
  }

  // Mark PR as streaming
  _streamingPrs.add(prId);

  // Accumulate full text for DB persistence
  let accumulated = "";
  let currentSection: string | null = null;

  try {
    // Compute diff via git
    let diffText = "";
    try {
      const cwd = getRepoPath(ownerName, repoName);
      const proc = Bun.spawn(
        ["git", "diff", `${baseBranch}...${headBranch}`, "--"],
        { cwd, stdout: "pipe", stderr: "pipe" }
      );
      const raw = await new Response(proc.stdout).text();
      await proc.exited;
      diffText = raw;
    } catch {
      diffText = "";
    }

    if (!diffText.trim()) {
      yield { type: "error", error: "No diff found between branches — nothing to review" };
      return;
    }

    // Cap diff size
    if (diffText.length > DIFF_BYTE_CAP) {
      diffText = diffText.slice(0, DIFF_BYTE_CAP);
    }

    // Build messages
    const systemPrompt =
      "You are a senior code reviewer. Review this PR diff section by section. " +
      "Start with a one-paragraph summary of the overall change, then list specific findings " +
      "(each as: file:line — issue description), then give a verdict " +
      "(Approve / Request Changes / Comment). Be direct and specific. " +
      "Format findings as a numbered list. Start the verdict line with 'Verdict:'.";

    const userMessage = `PR diff:\n${diffText}`;

    // Open the streaming request
    const anthropic = getAnthropic();

    // Emit the summary section start before streaming begins
    yield { type: "section_start", section: "summary" };
    currentSection = "summary";

    const stream = anthropic.messages.stream({
      model: MODEL_SONNET,
      max_tokens: 2048,
      system: systemPrompt,
      messages: [{ role: "user", content: userMessage }],
    });

    // Stream tokens
    for await (const chunk of stream) {
      if (
        chunk.type === "content_block_delta" &&
        chunk.delta.type === "text_delta"
      ) {
        const text = chunk.delta.text;
        if (!text) continue;

        accumulated += text;

        // Check for section transitions
        const newSection = detectSectionTransition(accumulated, currentSection);
        if (newSection && newSection !== currentSection) {
          // Close the old section
          yield { type: "section_end", section: currentSection ?? "summary" };
          // Open the new section
          yield { type: "section_start", section: newSection };
          currentSection = newSection;
        }

        // Yield the token
        yield { type: "token", content: text };
      }
    }

    // Close the final section
    if (currentSection) {
      yield { type: "section_end", section: currentSection };
    }

    // Persist the full review as a PR comment (idempotent)
    if (accumulated.trim()) {
      try {
        // Re-check idempotency before insert
        const [existingCheck] = await db
          .select({ id: prComments.id })
          .from(prComments)
          .where(
            and(
              eq(prComments.pullRequestId, prId),
              eq(prComments.isAiReview, true),
              like(prComments.body, `%${STREAM_REVIEW_MARKER}%`)
            )
          )
          .limit(1);

        if (!existingCheck) {
          // Resolve author for the comment
          const [pr] = await db
            .select({ authorId: pullRequests.authorId })
            .from(pullRequests)
            .where(eq(pullRequests.id, prId))
            .limit(1);

          if (pr) {
            const commentAuthorId = await getBotUserIdOrFallback(pr.authorId);
            const commentBody = `## AI Stream Review\n\n${accumulated}\n\n${STREAM_REVIEW_MARKER}`;
            await db.insert(prComments).values({
              pullRequestId: prId,
              authorId: commentAuthorId,
              isAiReview: true,
              body: commentBody,
            });
          }
        }
      } catch (err) {
        // Non-fatal — review was streamed, just not persisted
        if (process.env.DEBUG_AI_REVIEW === "1") {
          console.error("[streaming-review] failed to persist review comment:", err);
        }
      }
    }

    yield { type: "done" };
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    yield { type: "error", error: message };
  } finally {
    _streamingPrs.delete(prId);
  }
}