-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMergeConflictShell.logic.ts
More file actions
248 lines (220 loc) · 8.13 KB
/
MergeConflictShell.logic.ts
File metadata and controls
248 lines (220 loc) · 8.13 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
import type {
GitResolvedPullRequest,
PrConflictAnalysis,
PrConflictCandidateResolution,
} from "@okcode/contracts";
export type MergeConflictFeedbackDisposition = "accept" | "review" | "escalate" | "blocked";
export interface MergeConflictCandidateGroup {
path: string;
candidates: PrConflictCandidateResolution[];
recommendedCandidate: PrConflictCandidateResolution | null;
}
export type MergeConflictRecommendedAction =
| "prepare-local"
| "prepare-worktree"
| "review-candidate"
| "capture-note"
| null;
export interface MergeConflictRecommendation {
candidateId: string | null;
recommendedAction: MergeConflictRecommendedAction;
tone: "neutral" | "success" | "warning";
title: string;
detail: string;
}
function candidatePriority(candidate: PrConflictCandidateResolution): number {
return candidate.confidence === "safe" ? 0 : 1;
}
export function sortConflictCandidates(
candidates: readonly PrConflictCandidateResolution[],
): PrConflictCandidateResolution[] {
return [...candidates].toSorted((left, right) => {
const priorityDiff = candidatePriority(left) - candidatePriority(right);
if (priorityDiff !== 0) return priorityDiff;
return left.title.localeCompare(right.title);
});
}
export function pickRecommendedConflictCandidate(
analysis: Pick<PrConflictAnalysis, "candidates"> | null | undefined,
): PrConflictCandidateResolution | null {
const sorted = sortConflictCandidates(analysis?.candidates ?? []);
return sorted[0] ?? null;
}
export function groupConflictCandidatesByFile(
candidates: readonly PrConflictCandidateResolution[],
): MergeConflictCandidateGroup[] {
const groups = new Map<string, PrConflictCandidateResolution[]>();
for (const candidate of sortConflictCandidates(candidates)) {
const nextGroup = groups.get(candidate.path) ?? [];
nextGroup.push(candidate);
groups.set(candidate.path, nextGroup);
}
return [...groups.entries()]
.toSorted(([leftPath], [rightPath]) => leftPath.localeCompare(rightPath))
.map(([path, groupedCandidates]) => ({
path,
candidates: groupedCandidates,
recommendedCandidate: groupedCandidates[0] ?? null,
}));
}
export function buildConflictRecommendation(input: {
analysis: PrConflictAnalysis | undefined;
hasPreparedWorkspace: boolean;
}): MergeConflictRecommendation {
if (!input.analysis || input.analysis.status === "unavailable") {
return {
candidateId: null,
recommendedAction: null,
tone: "neutral",
title: "Resolve a pull request link to start.",
detail:
"Paste a GitHub pull request URL to inspect mergeability, pull candidate resolutions, and stage a human-readable handoff note.",
};
}
if (input.analysis.status === "clean") {
return {
candidateId: null,
recommendedAction: null,
tone: "success",
title: "No merge conflicts are active.",
detail: input.analysis.summary,
};
}
const recommendedCandidate = pickRecommendedConflictCandidate(input.analysis);
if (recommendedCandidate?.confidence === "safe") {
return {
candidateId: recommendedCandidate.id,
recommendedAction: "review-candidate",
tone: "success",
title: "Recommended resolution is ready.",
detail:
"OK Code found a deterministic candidate. Review the patch, capture the operator note, and then apply the recommendation.",
};
}
if (recommendedCandidate) {
return {
candidateId: recommendedCandidate.id,
recommendedAction: "review-candidate",
tone: "warning",
title: "Review-required options are available.",
detail:
"OK Code found possible resolutions, but none are deterministic. Compare both sides, leave a readable decision note, and only then apply one.",
};
}
if (input.hasPreparedWorkspace) {
return {
candidateId: null,
recommendedAction: "capture-note",
tone: "warning",
title: "Manual merge work is still required.",
detail:
"The workspace is prepared, but no candidate patch was safe to generate. Resolve the markers manually and keep the handoff note explicit.",
};
}
return {
candidateId: null,
recommendedAction: "prepare-worktree",
tone: "warning",
title: "Prepare a local workspace to continue.",
detail:
"GitHub reports merge conflicts, but file-level candidates need a checked-out pull request branch or worktree before OK Code can inspect markers locally.",
};
}
function feedbackDispositionSentence(disposition: MergeConflictFeedbackDisposition): string {
switch (disposition) {
case "accept":
return "Accept the proposed resolution after reviewing the resulting diff.";
case "review":
return "Keep this in review until a human confirms the chosen side.";
case "escalate":
return "Escalate this conflict to the PR author or code owner for direction.";
case "blocked":
return "Treat this conflict as blocked until the workspace or intent is clarified.";
}
}
export function buildConflictFeedbackPreview(input: {
disposition: MergeConflictFeedbackDisposition;
note: string;
pullRequest: GitResolvedPullRequest | null;
selectedCandidate: PrConflictCandidateResolution | null;
workspaceLabel: string;
}): string {
const lines = [
input.pullRequest
? `Merge conflict brief for PR #${input.pullRequest.number}: ${input.pullRequest.title}`
: "Merge conflict brief",
`Workspace: ${input.workspaceLabel}`,
input.selectedCandidate
? `Candidate: ${input.selectedCandidate.title} on ${input.selectedCandidate.path} (${input.selectedCandidate.confidence} confidence).`
: "Candidate: No deterministic candidate is selected yet.",
input.selectedCandidate
? `Rationale: ${input.selectedCandidate.description}`
: "Rationale: Keep the branch prepared and inspect the conflict manually.",
`Disposition: ${feedbackDispositionSentence(input.disposition)}`,
];
const trimmedNote = input.note.trim();
if (trimmedNote.length > 0) {
lines.push(`Operator note: ${trimmedNote}`);
}
return lines.join("\n");
}
const KNOWN_ERROR_PATTERNS: ReadonlyArray<{
pattern: string;
summary: string;
detail: string;
}> = [
{
pattern: "already checked out",
summary: "Branch already checked out",
detail:
"The PR branch is already active in another worktree or the main repo. Close the other checkout or use \u201cPrepare worktree\u201d to create an isolated copy.",
},
{
pattern: "not a git repository",
summary: "Not a git repository",
detail:
"The selected project directory is not a valid git repository. Check the project path in the intake panel.",
},
];
const ERROR_PREFIX_RE = /^[A-Za-z ]+(?:failed|error) in [A-Za-z]+:\s*/i;
export function humanizeConflictError(rawMessage: string): {
summary: string;
detail: string;
} {
const lower = rawMessage.toLowerCase();
for (const known of KNOWN_ERROR_PATTERNS) {
if (lower.includes(known.pattern)) {
return { summary: known.summary, detail: known.detail };
}
}
const stripped = rawMessage.replace(ERROR_PREFIX_RE, "");
const firstSentenceEnd = stripped.search(/[.!]\s|[.!]$/);
const summary =
firstSentenceEnd > 0 ? stripped.slice(0, firstSentenceEnd + 1) : stripped.slice(0, 80);
return { summary, detail: rawMessage };
}
export function computeActiveStepIndex(
steps: ReadonlyArray<{ status: "done" | "active" | "todo" | "blocked" }>,
): number {
const index = steps.findIndex((step) => step.status !== "done");
return index === -1 ? steps.length : index;
}
export interface PreparedWorkspace {
branch: string;
cwd: string;
mode: "local" | "worktree";
worktreePath: string | null;
}
export function workspaceModeLabel(workspace: PreparedWorkspace | null): string {
if (!workspace) return "Repo scan";
return workspace.mode === "worktree" ? "Dedicated worktree" : "Prepared in repo";
}
export function pullRequestStateBadgeClassName(state: GitResolvedPullRequest["state"]): string {
switch (state) {
case "open":
return "border-emerald-500/30 bg-emerald-500/12 text-emerald-700 dark:text-emerald-300";
case "merged":
case "closed":
return "border-border bg-muted/70 text-foreground";
}
}