File size: 10,745 Bytes
cc11e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * diffValidator.ts — S210: Diff Pre/Post Validation
 *
 * Due fasi di validazione attorno a `apply_patch`:
 *   1. PRE-APPLY:  verifica che il diff sia sintatticamente valido prima di applicarlo
 *   2. POST-APPLY: verifica che il risultato sia coerente (sintassi, contenuto atteso)
 *
 * Supporta formati:
 *   - Unified diff (--- +++ @@ ... +/- lines)
 *   - SEARCH/REPLACE block (<<<<<<< SEARCH / ======= / >>>>>>> REPLACE)
 *
 * Pure, no side-effects, no external deps — Safari-safe.
 *
 * @module diffValidator
 */

import { checkCodeSyntaxBalance } from "@/lib/ai/output-validator";

// ─── Tipi ─────────────────────────────────────────────────────────────────────

export type DiffFormat = "unified" | "search_replace" | "unknown";

export interface DiffValidationResult {
  valid:     boolean;
  format:    DiffFormat;
  issues:    string[];
  warnings:  string[];
}

export interface PostApplyValidation {
  valid:        boolean;
  syntaxOk:     boolean;
  syntaxError:  string | null;
  lengthDelta:  number;        // byte: positivo = cresciuto, negativo = shrunk
  warnings:     string[];
}

// ─── Rilevamento formato ──────────────────────────────────────────────────────

/**
 * Rileva il formato del diff.
 */
export function detectDiffFormat(diff: string): DiffFormat {
  const trimmed = diff.trim();
  if (/^<<<<<<< SEARCH$/m.test(trimmed) && /^=======$/m.test(trimmed) && /^>>>>>>> REPLACE$/m.test(trimmed)) {
    return "search_replace";
  }
  if (/^---\s+/m.test(trimmed) && /^\+\+\+\s+/m.test(trimmed) && /^@@\s+-\d/m.test(trimmed)) {
    return "unified";
  }
  // Tolera diff unificati senza header (solo @@ ... +/- lines)
  if (/^@@\s+-\d+/m.test(trimmed) || /^[\-\+]{1}[^\-\+]/m.test(trimmed)) {
    return "unified";
  }
  return "unknown";
}

// ─── PRE-APPLY: Validazione sintassi diff ─────────────────────────────────────

/**
 * Valida la sintassi del diff PRIMA di applicarlo.
 *
 * Unified diff checks:
 *   - Ha almeno un hunk (@@ ... @@)
 *   - Ogni hunk ha almeno una riga + o -
 *   - Nessun mix di tab/space nei marcatori
 *
 * SEARCH/REPLACE checks:
 *   - Blocchi SEARCH/REPLACE completi e bilanciati
 *   - Sezione SEARCH non vuota
 *   - Delimitatori non sovrapposti
 */
export function validateDiffSyntax(diff: string): DiffValidationResult {
  const issues:   string[] = [];
  const warnings: string[] = [];
  const format = detectDiffFormat(diff);

  if (!diff.trim()) {
    return { valid: false, format, issues: ["Diff vuoto"], warnings };
  }

  if (format === "unknown") {
    return {
      valid:  false,
      format,
      issues: ["Formato diff non riconosciuto (atteso: unified diff o SEARCH/REPLACE)"],
      warnings,
    };
  }

  if (format === "search_replace") {
    return _validateSearchReplace(diff, issues, warnings);
  }

  // unified
  return _validateUnified(diff, issues, warnings);
}

function _validateSearchReplace(
  diff: string,
  issues: string[],
  warnings: string[],
): DiffValidationResult {
  const blocks = diff.split(/(?=<<<<<<< SEARCH$)/m).filter(b => b.includes("<<<<<<< SEARCH"));

  if (blocks.length === 0) {
    issues.push("Nessun blocco SEARCH/REPLACE trovato");
    return { valid: false, format: "search_replace", issues, warnings };
  }

  let blockIdx = 0;
  for (const block of blocks) {
    blockIdx++;
    const searchMatch  = block.match(/^<<<<<<< SEARCH\n([\s\S]*?)^=======$/m);
    const replaceMatch = block.match(/^=======\n([\s\S]*?)^>>>>>>> REPLACE$/m);

    if (!searchMatch) {
      issues.push(`Blocco ${blockIdx}: delimitatore <<<<<<< SEARCH o ======= mancante o malformato`);
      continue;
    }
    if (!replaceMatch) {
      issues.push(`Blocco ${blockIdx}: delimitatore ======= o >>>>>>> REPLACE mancante o malformato`);
      continue;
    }

    const searchContent = searchMatch[1];
    if (!searchContent.trim()) {
      issues.push(`Blocco ${blockIdx}: sezione SEARCH vuota — impossibile localizzare il testo da sostituire`);
    }

    // Warning: sezione REPLACE vuota = cancellazione (lecita ma rischiosa)
    const replaceContent = replaceMatch[1];
    if (!replaceContent.trim()) {
      warnings.push(`Blocco ${blockIdx}: sezione REPLACE vuota — il testo verrà eliminato senza sostituzione`);
    }
  }

  return { valid: issues.length === 0, format: "search_replace", issues, warnings };
}

function _validateUnified(
  diff: string,
  issues: string[],
  warnings: string[],
): DiffValidationResult {
  const lines  = diff.split("\n");
  const hunks  = lines.filter(l => l.startsWith("@@"));

  if (hunks.length === 0) {
    issues.push("Nessun hunk (@@ ... @@) trovato nel unified diff");
    return { valid: false, format: "unified", issues, warnings };
  }

  // Verifica che ci siano almeno righe + o -
  const hasChanges = lines.some(l => l.startsWith("+") || l.startsWith("-"));
  if (!hasChanges) {
    issues.push("Il diff non contiene righe aggiunte (+) o rimosse (-)");
  }

  // Verifica header hunk: @@ -L,N +L,N @@
  for (const hunk of hunks) {
    if (!/^@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@/.test(hunk)) {
      warnings.push(`Hunk mal formato: "${hunk.slice(0, 60)}"`);
    }
  }

  // Avvisa su diff che rimuovono molte righe senza aggiungerne
  const removes = lines.filter(l => l.startsWith("-") && !l.startsWith("---")).length;
  const adds    = lines.filter(l => l.startsWith("+") && !l.startsWith("+++")).length;
  if (removes > 20 && adds < removes * 0.2) {
    warnings.push(`Rimozione massiccia: -${removes} righe, +${adds} righe — verifica che sia intenzionale`);
  }

  return { valid: issues.length === 0, format: "unified", issues, warnings };
}

// ─── POST-APPLY: Validazione risultato ────────────────────────────────────────

/**
 * Valida il file risultante DOPO l'applicazione del patch.
 *
 * @param originalContent  Contenuto originale (prima del patch)
 * @param patchedContent   Contenuto risultante (dopo il patch)
 * @param filePath         Path del file (usato per rilevare il linguaggio)
 */
export function validatePostApply(
  originalContent: string,
  patchedContent:  string,
  filePath:        string,
): PostApplyValidation {
  const warnings: string[] = [];

  const ext = filePath.split(".").pop()?.toLowerCase() ?? "";
  const CODE_EXTS = new Set(["ts","tsx","js","jsx","mjs"]);

  // Syntax balance check per TS/JS
  let syntaxError: string | null = null;
  let syntaxOk = true;
  if (CODE_EXTS.has(ext)) {
    syntaxError = checkCodeSyntaxBalance(patchedContent);
    if (syntaxError) {
      syntaxOk = false;
      // Verifica se anche l'originale aveva l'errore (pre-esistente)
      const originalSyntaxError = checkCodeSyntaxBalance(originalContent);
      if (originalSyntaxError) {
        // Errore preesistente → downgrade a warning
        syntaxOk = true;
        syntaxError = null;
        warnings.push(`Sintassi già non bilanciata nell'originale (preesistente): ${originalSyntaxError}`);
      }
    }
  }

  // Length delta
  const lengthDelta = patchedContent.length - originalContent.length;

  // Warning: contenuto azzerato
  if (patchedContent.trim().length === 0 && originalContent.trim().length > 0) {
    warnings.push("Il file risultante è vuoto — il patch ha rimosso tutto il contenuto");
  }

  // Warning: shrinkage estremo (> 80% perso)
  if (originalContent.length > 100 && patchedContent.length < originalContent.length * 0.2) {
    warnings.push(
      `Riduzione drastica: ${originalContent.length}${patchedContent.length} chars (` +
      `${Math.round((1 - patchedContent.length / originalContent.length) * 100)}% rimosso)`
    );
  }

  // Warning: import rotti (riga import presente nell'originale ma non nel risultato)
  if (CODE_EXTS.has(ext)) {
    const origImports    = (originalContent.match(/^import .+ from .+;$/gm) ?? []);
    const patchedImports = new Set(patchedContent.match(/^import .+ from .+;$/gm) ?? []);
    const removedImports = origImports.filter(i => !patchedImports.has(i));
    if (removedImports.length > 3) {
      warnings.push(`${removedImports.length} import rimossi — verifica dipendenze`);
    }
  }

  return {
    valid:       syntaxOk,
    syntaxOk,
    syntaxError,
    lengthDelta,
    warnings,
  };
}

// ─── Convenience: applica SEARCH/REPLACE in-process ──────────────────────────

/**
 * Applica un blocco SEARCH/REPLACE a un testo sorgente.
 * Ritorna null se la stringa di ricerca non viene trovata.
 *
 * Utile per pre-verificare che il SEARCH sia effettivamente presente
 * nel file prima di inviare il tool_call al backend.
 */
export function applySearchReplace(
  source:  string,
  diff:    string,
): string | null {
  const blocks = diff.split(/(?=<<<<<<< SEARCH$)/m).filter(b => b.includes("<<<<<<< SEARCH"));
  let result = source;

  for (const block of blocks) {
    const searchMatch  = block.match(/^<<<<<<< SEARCH\n([\s\S]*?)^=======$/m);
    const replaceMatch = block.match(/^=======\n([\s\S]*?)^>>>>>>> REPLACE$/m);
    if (!searchMatch || !replaceMatch) return null;

    const searchStr  = searchMatch[1];
    const replaceStr = replaceMatch[1];

    if (!result.includes(searchStr)) return null;
    result = result.replace(searchStr, replaceStr);
  }

  return result;
}

/**
 * Wrapper tutto-in-uno: valida syntax diff + verifica SEARCH presente + valida post-apply.
 * Ritorna un report completo.
 */
export function fullDiffValidation(
  diff:            string,
  originalContent: string,
  filePath:        string,
): {
  preValidation:  DiffValidationResult;
  searchFound:    boolean | null;     // null = non applicabile (unified diff)
  postValidation: PostApplyValidation | null;  // null = non calcolabile
} {
  const preValidation = validateDiffSyntax(diff);
  let searchFound: boolean | null = null;
  let postValidation: PostApplyValidation | null = null;

  if (!preValidation.valid) {
    return { preValidation, searchFound, postValidation };
  }

  // Per SEARCH/REPLACE: prova applicazione in-process
  if (preValidation.format === "search_replace") {
    const applied = applySearchReplace(originalContent, diff);
    searchFound   = applied !== null;
    if (applied !== null) {
      postValidation = validatePostApply(originalContent, applied, filePath);
    }
  }

  return { preValidation, searchFound, postValidation };
}