|
| 1 | +import Foundation |
| 2 | +import RxCodeCore |
| 3 | + |
| 4 | +/// Errors surfaced while starting a manual code review from a briefing card, |
| 5 | +/// project menu, or thread row. |
| 6 | +enum CodeReviewError: LocalizedError { |
| 7 | + case unknownThread |
| 8 | + case unknownProject |
| 9 | + case sendFailed(String) |
| 10 | + |
| 11 | + var errorDescription: String? { |
| 12 | + switch self { |
| 13 | + case .unknownThread: |
| 14 | + return "Couldn't find the thread to review." |
| 15 | + case .unknownProject: |
| 16 | + return "Couldn't find the project to review." |
| 17 | + case .sendFailed(let message): |
| 18 | + return "Couldn't start the code review.\n\n\(message)" |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +extension AppState { |
| 24 | + |
| 25 | + /// Label stamped on manually-started review threads (matches the built-in |
| 26 | + /// Code Review hook so they show the same `[Code Review]` chip and nest in |
| 27 | + /// the sidebar review UI). |
| 28 | + static let manualCodeReviewLabel = "Code Review" |
| 29 | + |
| 30 | + // MARK: - Branch-level review |
| 31 | + |
| 32 | + /// Start a `[Code Review]` thread that reviews *all* the changes on `branch`, |
| 33 | + /// grounded in the branch briefing and the summaries of every thread that ran |
| 34 | + /// on it. The reviewer inspects the branch diff itself (it runs in `.auto` |
| 35 | + /// mode), so no diff needs to be computed here. Returns the new thread id. |
| 36 | + @discardableResult |
| 37 | + func createCodeReviewForBranch(project: Project, branch: String) async throws -> String { |
| 38 | + let briefing = threadStore.allBranchBriefingItems() |
| 39 | + .first(where: { $0.projectId == project.id && $0.branch == branch })? |
| 40 | + .briefing ?? "" |
| 41 | + let summaries = threadStore.allThreadSummaryItems() |
| 42 | + .filter { $0.projectId == project.id && $0.branch == branch } |
| 43 | + .sorted { $0.updatedAt > $1.updatedAt } |
| 44 | + let prompt = Self.branchCodeReviewPrompt(branch: branch, briefing: briefing, summaries: summaries) |
| 45 | + return try await startCodeReviewThread(projectId: project.id, parentThreadId: nil, prompt: prompt) |
| 46 | + } |
| 47 | + |
| 48 | + // MARK: - Thread-level review |
| 49 | + |
| 50 | + /// Start a `[Code Review]` thread nested under `sessionId` that reviews the |
| 51 | + /// files that thread changed — the manual equivalent of the built-in Code |
| 52 | + /// Review hook. Returns the new thread id. |
| 53 | + @discardableResult |
| 54 | + func createCodeReviewForThread(sessionId: String) async throws -> String { |
| 55 | + guard let summary = allSessionSummaries.first(where: { $0.id == sessionId }) |
| 56 | + ?? threadStore.fetch(id: sessionId)?.toSummary() else { |
| 57 | + throw CodeReviewError.unknownThread |
| 58 | + } |
| 59 | + guard let project = projects.first(where: { $0.id == summary.projectId }) else { |
| 60 | + throw CodeReviewError.unknownProject |
| 61 | + } |
| 62 | + |
| 63 | + // Files this thread touched, de-duplicated in first-seen order. |
| 64 | + var seen = Set<String>() |
| 65 | + var changedFiles: [String] = [] |
| 66 | + for edit in threadStore.fetchFileEdits(sessionId: sessionId) where seen.insert(edit.path).inserted { |
| 67 | + changedFiles.append(edit.path) |
| 68 | + } |
| 69 | + |
| 70 | + // Pull the task/response from in-memory state when the thread is loaded; |
| 71 | + // fall back to the thread title (always available) for an idle thread |
| 72 | + // whose messages aren't currently in memory. |
| 73 | + let messages = stateForSession(sessionId).messages |
| 74 | + let task = messages.first(where: { |
| 75 | + $0.role == .user && !$0.content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 76 | + })?.content ?? summary.title |
| 77 | + let finalResponse = lastAssistantResponseText(in: messages) |
| 78 | + |
| 79 | + let prompt = Self.threadCodeReviewPrompt( |
| 80 | + task: task, |
| 81 | + changedFiles: changedFiles, |
| 82 | + finalResponse: finalResponse |
| 83 | + ) |
| 84 | + return try await startCodeReviewThread(projectId: project.id, parentThreadId: sessionId, prompt: prompt) |
| 85 | + } |
| 86 | + |
| 87 | + // MARK: - Shared launch |
| 88 | + |
| 89 | + /// Spawn the review thread through the normal send pipeline. Runs in `.auto` |
| 90 | + /// mode (so the reviewer can read files / run `git diff` without per-tool |
| 91 | + /// prompts) with hooks skipped (the review thread shouldn't trigger its own |
| 92 | + /// review). Fire-and-forget — returns as soon as the thread id is known so |
| 93 | + /// the caller can navigate to it while the review runs. |
| 94 | + private func startCodeReviewThread( |
| 95 | + projectId: UUID, |
| 96 | + parentThreadId: String?, |
| 97 | + prompt: String |
| 98 | + ) async throws -> String { |
| 99 | + let result = try await sendCrossProject( |
| 100 | + projectId: projectId, |
| 101 | + threadId: nil, |
| 102 | + prompt: prompt, |
| 103 | + permissionMode: .auto, |
| 104 | + waitForResponse: false, |
| 105 | + timeoutSeconds: 600, |
| 106 | + parentThreadId: parentThreadId, |
| 107 | + threadLabel: Self.manualCodeReviewLabel, |
| 108 | + skipHooks: true |
| 109 | + ) |
| 110 | + if let error = result.error { throw CodeReviewError.sendFailed(error) } |
| 111 | + return result.threadId |
| 112 | + } |
| 113 | + |
| 114 | + // MARK: - Prompts |
| 115 | + |
| 116 | + private static let reviewMarker = "REVIEW_RESULT:" |
| 117 | + |
| 118 | + static func branchCodeReviewPrompt( |
| 119 | + branch: String, |
| 120 | + briefing: String, |
| 121 | + summaries: [ThreadSummaryItem] |
| 122 | + ) -> String { |
| 123 | + let trimmedBriefing = briefing.trimmingCharacters(in: .whitespacesAndNewlines) |
| 124 | + let briefingSection = trimmedBriefing.isEmpty ? "(no briefing recorded)" : trimmedBriefing |
| 125 | + let threadList: String |
| 126 | + if summaries.isEmpty { |
| 127 | + threadList = "(no thread summaries recorded)" |
| 128 | + } else { |
| 129 | + threadList = summaries.map { item in |
| 130 | + let title = item.title.trimmingCharacters(in: .whitespacesAndNewlines) |
| 131 | + let summary = item.summary |
| 132 | + .trimmingCharacters(in: .whitespacesAndNewlines) |
| 133 | + .split(separator: "\n").first.map(String.init) ?? "" |
| 134 | + return summary.isEmpty ? "- \(title)" : "- \(title) — \(summary)" |
| 135 | + }.joined(separator: "\n") |
| 136 | + } |
| 137 | + |
| 138 | + return """ |
| 139 | + You are reviewing all the code changes on branch `\(branch)` in this repository. Do not edit any files — only review. |
| 140 | +
|
| 141 | + ## What this branch set out to do (briefing) |
| 142 | + \(briefingSection) |
| 143 | +
|
| 144 | + ## Threads that ran on this branch |
| 145 | + \(threadList) |
| 146 | +
|
| 147 | + ## What to do |
| 148 | + 1. Determine the branch's base (usually the repository's default branch, e.g. `main`) and inspect the actual diff. For example run `git diff $(git merge-base HEAD main)...HEAD --stat` then read the changed files, or `git diff main...HEAD`. |
| 149 | + 2. Judge whether the changes correctly and safely accomplish the work described above. Look for bugs, missed requirements, regressions, security issues, and obvious quality problems. |
| 150 | + 3. List the specific, actionable issues you find (file + line where possible). |
| 151 | +
|
| 152 | + End your reply with a single line — exactly one of: |
| 153 | + `\(reviewMarker) PASS` (the changes look good as-is) |
| 154 | + `\(reviewMarker) FAIL` (changes are needed) |
| 155 | + """ |
| 156 | + } |
| 157 | + |
| 158 | + static func threadCodeReviewPrompt( |
| 159 | + task: String, |
| 160 | + changedFiles: [String], |
| 161 | + finalResponse: String |
| 162 | + ) -> String { |
| 163 | + let fileList = changedFiles.isEmpty |
| 164 | + ? "(no recorded file edits — inspect the working tree / recent commits for what changed)" |
| 165 | + : changedFiles.map { "- \($0)" }.joined(separator: "\n") |
| 166 | + let response = finalResponse.trimmingCharacters(in: .whitespacesAndNewlines) |
| 167 | + let responseSection = response.isEmpty ? "(no final response recorded)" : response |
| 168 | + |
| 169 | + return """ |
| 170 | + You are reviewing another agent's code change in this repository. Do not edit any files — only review. |
| 171 | +
|
| 172 | + ## The user's task |
| 173 | + \(task) |
| 174 | +
|
| 175 | + ## Files the agent changed |
| 176 | + \(fileList) |
| 177 | +
|
| 178 | + ## The agent's final response |
| 179 | + \(responseSection) |
| 180 | +
|
| 181 | + ## What to do |
| 182 | + Inspect the changed files and judge whether the change correctly and safely accomplishes the task. Look for bugs, missed requirements, regressions, and obvious quality problems. |
| 183 | +
|
| 184 | + End your reply with a single line — exactly one of: |
| 185 | + `\(reviewMarker) PASS` (the change is good as-is) |
| 186 | + `\(reviewMarker) FAIL` (changes are needed) |
| 187 | +
|
| 188 | + If you FAIL the review, list the specific, actionable issues to fix above that line. |
| 189 | + """ |
| 190 | + } |
| 191 | +} |
0 commit comments