-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeScanService.java
More file actions
703 lines (641 loc) · 30.5 KB
/
Copy pathCodeScanService.java
File metadata and controls
703 lines (641 loc) · 30.5 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package com.dbaagent.service.codescan;
import com.dbaagent.model.SchemaMetadata;
import com.dbaagent.model.code.CodeKnowledgeSuggestion;
import com.dbaagent.model.code.CodeScanJob;
import com.dbaagent.model.code.CodeScanSource;
import com.dbaagent.model.code.CodeScanFileHash;
import com.dbaagent.repository.CodeKnowledgeSuggestionRepository;
import com.dbaagent.repository.CodeScanFileHashRepository;
import com.dbaagent.repository.CodeScanJobRepository;
import com.dbaagent.repository.CodeScanSourceRepository;
import com.dbaagent.service.SchemaScannerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import jakarta.annotation.PreDestroy;
/**
* Orchestrates the end-to-end code scan: upload → extract archive → walk + chunk →
* LLM extract → aggregate → persist as PENDING suggestions, with live progress
* reported through SSE.
*
* Mirrors the {@code TrainingJobService} pattern (executor + per-connection
* emitters), so the runtime characteristics are familiar to the rest of the
* codebase.
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class CodeScanService {
private final CodeScanSourceRepository sourceRepository;
private final CodeScanJobRepository jobRepository;
private final CodeKnowledgeSuggestionRepository suggestionRepository;
private final CodeScanFileHashRepository fileHashRepository;
private final CodeArchiveExtractor archiveExtractor;
private final CodeFileScanner fileScanner;
private final CodeKnowledgeExtractor knowledgeExtractor;
private final CodeSuggestionAggregator aggregator;
private final CodeSuggestionApplier applier;
private final SchemaScannerService schemaScannerService;
private final SchemaAmbiguityService schemaAmbiguityService;
@Value("${code-scan.executor.core:2}")
private int executorCore;
@Value("${code-scan.executor.max:4}")
private int executorMax;
@Value("${code-scan.max-chunks-per-job:400}")
private int maxChunksPerJob;
@Value("${code-scan.max-parallel-chunks:8}")
private int maxParallelChunks;
private ExecutorService executor;
private final Map<String, CopyOnWriteArrayList<SseEmitter>> emittersByJob = new ConcurrentHashMap<>();
private synchronized ExecutorService executor() {
if (executor == null) {
executor = new ThreadPoolExecutor(
Math.max(1, executorCore),
Math.max(executorCore, executorMax),
60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
Executors.defaultThreadFactory()
);
}
return executor;
}
@PreDestroy
void shutdown() {
if (executor != null) {
executor.shutdown();
}
}
// ---- Source management ----
@Transactional
public CodeScanSource createSourceFromUpload(String connectionId,
MultipartFile file,
String name,
String focusText,
String createdBy) throws IOException {
if (connectionId == null || connectionId.isBlank()) {
throw new IllegalArgumentException("connectionId is required");
}
if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("upload is required");
}
// We extract once here just to get sha + size; the active job re-extracts so
// we don't have to keep a temp dir alive between requests.
CodeArchiveExtractor.Result probe = archiveExtractor.extract(file);
try {
String trimmedFocus = focusText == null ? null : focusText.trim();
String hash = computeFocusHash(connectionId, trimmedFocus);
CodeScanSource source = CodeScanSource.builder()
.connectionId(connectionId)
.name((name == null || name.isBlank()) ? defaultName(file) : name)
.kind(CodeScanSource.Kind.UPLOAD)
.archiveSha256(probe.archiveSha256())
.totalBytes(probe.totalBytes())
.fileCount(probe.fileCount())
.createdBy(createdBy)
.active(Boolean.TRUE)
.focusText(trimmedFocus)
.focusHash(hash)
.focusUpdatedAt(trimmedFocus != null && !trimmedFocus.isBlank() ? LocalDateTime.now() : null)
.build();
return sourceRepository.save(source);
} finally {
deleteRecursively(probe.root());
}
}
@Transactional
public CodeScanSource updateFocus(String sourceId, String focusText) {
CodeScanSource source = sourceRepository.findById(sourceId)
.orElseThrow(() -> new IllegalArgumentException("Source not found: " + sourceId));
String trimmed = focusText == null ? null : focusText.trim();
source.setFocusText(trimmed);
source.setFocusHash(computeFocusHash(source.getConnectionId(), trimmed));
source.setFocusUpdatedAt(LocalDateTime.now());
return sourceRepository.save(source);
}
/**
* Hash represents (userFocus + ambiguity-derived focus) so two sources
* with identical inputs collapse and so the UI can detect "the source's
* focus changed since its last successful job".
*/
private String computeFocusHash(String connectionId, String userFocus) {
try {
String ambiguity = schemaAmbiguityService.summariseForFocus(connectionId);
String combined = (userFocus == null ? "" : userFocus) + "\n" + (ambiguity == null ? "" : ambiguity);
if (combined.isBlank()) return null;
java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(combined.getBytes(java.nio.charset.StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder(digest.length * 2);
for (byte b : digest) sb.append(String.format("%02x", b));
return sb.toString();
} catch (Exception e) {
log.debug("focus hash compute failed: {}", e.getMessage());
return null;
}
}
public List<CodeScanSource> listSources(String connectionId) {
return sourceRepository.findByConnectionIdAndActiveTrueOrderByCreatedAtDesc(connectionId);
}
@Transactional
public void deleteSource(String sourceId) {
sourceRepository.findById(sourceId).ifPresent(s -> {
s.setActive(false);
sourceRepository.save(s);
});
// Clear cached file hashes — re-activating the source later (or
// creating a different one with the same id, which won't happen but
// belt-and-braces) shouldn't reuse stale dedup state.
try {
fileHashRepository.deleteBySourceId(sourceId);
} catch (Exception e) {
log.debug("file hash cleanup on source delete failed: {}", e.getMessage());
}
}
// ---- Job management ----
/**
* Starts a scan against the given source. The supplied {@link MultipartFile}
* is the archive to scan. We require it on each scan so we never need to
* persist the source archive on disk between requests.
*/
@Transactional
public CodeScanJob startScan(String sourceId,
MultipartFile archive,
String triggeredBy) throws IOException {
return startScan(sourceId, archive, null, triggeredBy);
}
@Transactional
public CodeScanJob startScan(String sourceId,
MultipartFile archive,
String focusOverride,
String triggeredBy) throws IOException {
CodeScanSource source = sourceRepository.findById(sourceId)
.orElseThrow(() -> new IllegalArgumentException("Source not found: " + sourceId));
if (!Boolean.TRUE.equals(source.getActive())) {
throw new IllegalStateException("Source has been deleted");
}
if (archive == null || archive.isEmpty()) {
throw new IllegalArgumentException("archive upload is required to start a scan");
}
// Persist override (if any) onto the source before computing effective focus.
if (focusOverride != null) {
source.setFocusText(focusOverride.trim());
source.setFocusHash(computeFocusHash(source.getConnectionId(), focusOverride.trim()));
source.setFocusUpdatedAt(LocalDateTime.now());
source = sourceRepository.save(source);
}
// Compose effective focus (user + ambiguity) once at scan-start so
// every chunk in this run sees the same prior.
String userFocus = source.getFocusText();
String ambiguityFocus = "";
try {
ambiguityFocus = schemaAmbiguityService.summariseForFocus(source.getConnectionId());
} catch (Exception e) {
log.warn("ambiguity focus compute failed: {}", e.getMessage());
}
String effectiveFocus = renderEffectiveFocus(userFocus, ambiguityFocus);
String effectiveFocusHash = computeFocusHash(source.getConnectionId(), userFocus);
CodeScanJob job = CodeScanJob.builder()
.sourceId(sourceId)
.connectionId(source.getConnectionId())
.status(CodeScanJob.Status.PENDING)
.progress(0)
.currentStep("queued")
.triggeredBy(triggeredBy)
.focusText(effectiveFocus)
.focusHash(effectiveFocusHash)
.build();
job = jobRepository.save(job);
final String jobId = job.getId();
final String connectionId = source.getConnectionId();
final String capturedFocus = effectiveFocus;
// Capture the upload to a temp dir up-front; the request thread holds
// the multipart, but the worker runs after we return.
CodeArchiveExtractor.Result extracted = archiveExtractor.extract(archive);
// Defer the worker submission until AFTER the surrounding @Transactional
// commits — otherwise the worker's first findById can race the commit
// and miss the row, leaving status stuck at PENDING.
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
executor().submit(() -> runJob(jobId, connectionId, extracted.root(), sourceId, capturedFocus));
}
});
} else {
executor().submit(() -> runJob(jobId, connectionId, extracted.root(), sourceId, capturedFocus));
}
return job;
}
/**
* After a new run's suggestions land, mark any prior PENDING suggestion
* from earlier jobs of the same source that targeted the same object as
* SUPERSEDED. We only touch PENDING — APPROVED / REJECTED / already
* SUPERSEDED rows are preserved.
*/
private void supersedeStalePending(String connectionId,
String sourceId,
String newJobId,
List<CodeKnowledgeSuggestion> newBatch) {
java.util.Set<String> newTargetKeys = new java.util.HashSet<>();
for (CodeKnowledgeSuggestion s : newBatch) {
newTargetKeys.add(targetKey(s.getTargetKind(), s.getTargetObject()));
}
// Find prior jobs for this source.
List<CodeScanJob> priorJobs = jobRepository.findBySourceIdOrderByCreatedAtDesc(sourceId);
java.util.Set<String> priorJobIds = new java.util.HashSet<>();
for (CodeScanJob j : priorJobs) {
if (!java.util.Objects.equals(j.getId(), newJobId)) {
priorJobIds.add(j.getId());
}
}
if (priorJobIds.isEmpty()) return;
int superseded = 0;
for (CodeKnowledgeSuggestion existing : suggestionRepository
.findByConnectionIdAndStatusOrderByConfidenceDesc(
connectionId, CodeKnowledgeSuggestion.Status.PENDING)) {
if (!priorJobIds.contains(existing.getJobId())) continue;
String key = targetKey(existing.getTargetKind(), existing.getTargetObject());
if (!newTargetKeys.contains(key)) continue;
existing.setStatus(CodeKnowledgeSuggestion.Status.SUPERSEDED);
existing.setDecisionNote("superseded by job " + newJobId);
existing.setDecidedAt(LocalDateTime.now());
suggestionRepository.save(existing);
superseded++;
}
if (superseded > 0) {
log.info("supersession: marked {} prior PENDING suggestions SUPERSEDED for source {}",
superseded, sourceId);
}
}
private static String targetKey(CodeKnowledgeSuggestion.TargetKind kind, String target) {
return (kind == null ? "_" : kind.name()) + "|" + (target == null ? "_" : target.toLowerCase(java.util.Locale.ROOT));
}
/** Returns previously-recorded (path → sha256) for this source. */
private Map<String, String> loadPriorFileHashes(String sourceId) {
var rows = fileHashRepository.findBySourceId(sourceId);
if (rows.isEmpty()) return java.util.Collections.emptyMap();
Map<String, String> out = new HashMap<>(rows.size());
for (CodeScanFileHash row : rows) {
out.put(row.getRelativePath(), row.getSha256());
}
return out;
}
/** Focus hash of the most recent successful job for this source, excluding the current one. */
private String lastSuccessfulFocusHash(String sourceId, String currentJobId) {
for (CodeScanJob j : jobRepository.findBySourceIdOrderByCreatedAtDesc(sourceId)) {
if (java.util.Objects.equals(j.getId(), currentJobId)) continue;
if (j.getStatus() == CodeScanJob.Status.COMPLETED) {
return j.getFocusHash();
}
}
return null;
}
/**
* Upsert the file-hash table for every file we saw in this scan. We store
* even unchanged files so the next scan recognises them as unchanged again.
* Files that disappeared from the archive remain in the table with their
* stale hash — harmless (they'll never match a future scan's chunk).
*/
private void persistFileHashes(String sourceId, String jobId, List<CodeFileScanner.CodeChunk> chunks) {
// One hash per unique file path (chunks within a file share a fileSha256).
Map<String, String> byPath = new HashMap<>();
for (var c : chunks) {
byPath.put(c.path(), c.fileSha256());
}
if (byPath.isEmpty()) return;
try {
LocalDateTime now = LocalDateTime.now();
List<CodeScanFileHash> rows = new ArrayList<>(byPath.size());
for (var e : byPath.entrySet()) {
rows.add(CodeScanFileHash.builder()
.sourceId(sourceId)
.relativePath(e.getKey())
.sha256(e.getValue())
.lastJobId(jobId)
.lastScannedAt(now)
.build());
}
fileHashRepository.saveAll(rows);
} catch (Exception e) {
log.warn("file hash persist failed for source {}: {}", sourceId, e.getMessage());
}
}
private static String renderEffectiveFocus(String userFocus, String ambiguityFocus) {
boolean hasUser = userFocus != null && !userFocus.isBlank();
boolean hasAmb = ambiguityFocus != null && !ambiguityFocus.isBlank();
if (!hasUser && !hasAmb) return "";
StringBuilder sb = new StringBuilder();
if (hasUser) {
sb.append("User-supplied focus:\n").append(userFocus.trim()).append("\n\n");
}
if (hasAmb) {
sb.append(ambiguityFocus.trim());
}
// Cap to ~3 KB; keep user portion intact, truncate ambiguity tail.
if (sb.length() > 3000) {
return sb.substring(0, 3000) + "\n…(truncated)";
}
return sb.toString();
}
private void runJob(String jobId, String connectionId, Path workdir, String sourceId, String effectiveFocus) {
try {
updateJob(jobId, j -> {
j.setStatus(CodeScanJob.Status.RUNNING);
j.setStartedAt(LocalDateTime.now());
j.setCurrentStep("scanning files");
j.setProgress(5);
});
List<CodeFileScanner.CodeChunk> chunks = fileScanner.scan(workdir, effectiveFocus);
// Per-file dedup: skip files whose SHA matches the last successful
// scan, but ONLY when this scan's focus matches the prior job's
// focus. If focus changed, the LLM prompt is different, so unchanged
// code may still produce different suggestions — re-run everything.
Map<String, String> priorHashes = loadPriorFileHashes(sourceId);
String priorJobFocusHash = lastSuccessfulFocusHash(sourceId, jobId);
String currentJobFocusHash = jobRepository.findById(jobId)
.map(CodeScanJob::getFocusHash).orElse(null);
boolean focusChanged = !java.util.Objects.equals(priorJobFocusHash, currentJobFocusHash);
List<CodeFileScanner.CodeChunk> emittedChunks;
int skippedChunks = 0;
if (priorHashes.isEmpty() || focusChanged) {
emittedChunks = chunks;
if (focusChanged && !priorHashes.isEmpty()) {
log.info("focus changed since last scan — re-LLMing all chunks "
+ "(prior focus_hash={}, current focus_hash={})",
priorJobFocusHash, currentJobFocusHash);
}
} else {
emittedChunks = new ArrayList<>(chunks.size());
for (var c : chunks) {
String prior = priorHashes.get(c.path());
if (prior != null && prior.equals(c.fileSha256())) {
skippedChunks++;
} else {
emittedChunks.add(c);
}
}
log.info("file-hash dedup: kept {} chunks, skipped {} unchanged",
emittedChunks.size(), skippedChunks);
}
int chunkCount = Math.min(emittedChunks.size(), maxChunksPerJob);
updateJob(jobId, j -> {
j.setFilesTotal(chunkCount);
j.setCurrentStep("loading schema");
j.setProgress(10);
});
SchemaMetadata schema;
try {
schema = schemaScannerService.scanSchema(connectionId);
} catch (Exception e) {
throw new RuntimeException("Schema scan failed: " + e.getMessage(), e);
}
updateJob(jobId, j -> {
j.setCurrentStep("extracting knowledge");
j.setProgress(15);
});
// Cap, then parallelize LLM extraction. Each chunk is one Azure
// OpenAI call; serial execution would take ~80 min for 400 chunks.
List<CodeFileScanner.CodeChunk> bounded = emittedChunks.size() > maxChunksPerJob
? emittedChunks.subList(0, maxChunksPerJob) : emittedChunks;
List<CodeKnowledgeExtractor.RawSuggestion> raw =
java.util.Collections.synchronizedList(new ArrayList<>());
java.util.concurrent.atomic.AtomicInteger processedRef =
new java.util.concurrent.atomic.AtomicInteger();
try (var llmPool = java.util.concurrent.Executors.newFixedThreadPool(maxParallelChunks)) {
List<java.util.concurrent.CompletableFuture<Void>> futures = new ArrayList<>();
for (var chunk : bounded) {
futures.add(java.util.concurrent.CompletableFuture.runAsync(() -> {
var emitted = knowledgeExtractor.extractFromChunk(chunk, schema, effectiveFocus);
raw.addAll(emitted);
int p = processedRef.incrementAndGet();
if (p % 10 == 0 || p == bounded.size()) {
int rawSize = raw.size();
updateJob(jobId, j -> {
j.setFilesParsed(p);
j.setChunksSent(p);
j.setSuggestionsEmitted(rawSize);
j.setProgress(15 + (int) Math.round(70.0 * p / Math.max(1, bounded.size())));
});
}
}, llmPool));
}
java.util.concurrent.CompletableFuture
.allOf(futures.toArray(new java.util.concurrent.CompletableFuture[0]))
.join();
}
updateJob(jobId, j -> {
j.setCurrentStep("aggregating");
j.setProgress(90);
});
List<CodeKnowledgeSuggestion> aggregated = aggregator.aggregate(connectionId, jobId, raw, schema);
if (!aggregated.isEmpty()) {
suggestionRepository.saveAll(aggregated);
supersedeStalePending(connectionId, sourceId, jobId, aggregated);
}
updateJob(jobId, j -> {
j.setStatus(CodeScanJob.Status.COMPLETED);
j.setCurrentStep("done");
j.setProgress(100);
j.setCompletedAt(LocalDateTime.now());
j.setSuggestionsEmitted(aggregated.size());
j.setMessage("Generated " + aggregated.size() + " suggestions for review");
});
sourceRepository.findById(sourceId).ifPresent(s -> {
s.setLastScannedAt(LocalDateTime.now());
sourceRepository.save(s);
});
// Persist current file hashes for ALL chunks we saw (not just the
// ones we sent to the LLM) so the next scan's dedup can recognise
// unchanged files even when this run skipped them.
persistFileHashes(sourceId, jobId, chunks);
} catch (Exception e) {
log.error("code scan job {} failed", jobId, e);
updateJob(jobId, j -> {
j.setStatus(CodeScanJob.Status.FAILED);
j.setCompletedAt(LocalDateTime.now());
j.setMessage(truncate(e.getMessage(), 800));
});
} finally {
deleteRecursively(workdir);
closeEmitters(jobId);
}
}
public Optional<CodeScanJob> getJob(String jobId) {
return jobRepository.findById(jobId);
}
public List<CodeScanJob> recentJobs(String sourceId) {
return jobRepository.findBySourceIdOrderByCreatedAtDesc(sourceId);
}
public SseEmitter subscribeJob(String jobId) {
SseEmitter emitter = new SseEmitter(0L); // no timeout — we'll close on terminal state
emittersByJob.computeIfAbsent(jobId, k -> new CopyOnWriteArrayList<>()).add(emitter);
// Send initial snapshot so UI doesn't have to make a separate GET.
jobRepository.findById(jobId).ifPresent(j -> sendSafe(emitter, j));
emitter.onCompletion(() -> removeEmitter(jobId, emitter));
emitter.onTimeout(() -> removeEmitter(jobId, emitter));
emitter.onError(t -> removeEmitter(jobId, emitter));
return emitter;
}
// ---- Suggestion review ----
public Page<CodeKnowledgeSuggestion> listSuggestions(String connectionId,
CodeKnowledgeSuggestion.Status status,
int page,
int size) {
var pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "confidence", "createdAt"));
return suggestionRepository.findByConnectionIdAndStatus(connectionId, status, pageable);
}
/**
* Routes to applier.approve / applier.reject. NOT @Transactional itself —
* each applier call carries its own transaction so a failure on one item
* does not poison the surrounding context.
*/
public CodeKnowledgeSuggestion decide(String suggestionId,
String decision,
String decidedBy,
String note) {
if ("APPROVED".equalsIgnoreCase(decision)) {
return applier.approve(suggestionId, decidedBy, note);
} else if ("REJECTED".equalsIgnoreCase(decision)) {
return applier.reject(suggestionId, decidedBy, note);
}
throw new IllegalArgumentException("decision must be APPROVED or REJECTED");
}
/**
* Bulk-decide is intentionally NOT @Transactional. Each item is its own
* transaction inside applier.approve/reject, so one bad row cannot mark a
* shared transaction rollback-only and break every subsequent item.
*/
public BulkDecideResult bulkDecide(List<String> ids,
String decision,
String decidedBy,
String note) {
List<CodeKnowledgeSuggestion> out = new ArrayList<>();
List<Map<String, String>> failures = new ArrayList<>();
for (String id : ids) {
try {
out.add(decide(id, decision, decidedBy, note));
} catch (Exception e) {
String message = rootMessage(e);
failures.add(Map.of("id", id, "error", message));
log.warn("bulk decide skipped {}: {}", id, message);
}
}
if (!failures.isEmpty()) {
log.info("bulk decide: {} succeeded, {} failed", out.size(), failures.size());
}
return new BulkDecideResult(out, failures);
}
public record BulkDecideResult(
List<CodeKnowledgeSuggestion> succeeded,
List<Map<String, String>> failures
) {}
private static String rootMessage(Throwable e) {
Throwable cur = e;
String best = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
while (cur != null) {
if (cur.getMessage() != null && !cur.getMessage().isBlank()) {
best = cur.getMessage();
}
cur = cur.getCause();
}
// Keep API payloads short — full stack stays in logs.
if (best.length() > 400) {
return best.substring(0, 397) + "...";
}
return best;
}
// ---- Helpers ----
private void updateJob(String jobId, java.util.function.Consumer<CodeScanJob> mutator) {
jobRepository.findById(jobId).ifPresent(j -> {
mutator.accept(j);
CodeScanJob saved = jobRepository.save(j);
broadcast(jobId, saved);
});
}
private void broadcast(String jobId, CodeScanJob job) {
var emitters = emittersByJob.get(jobId);
if (emitters == null) return;
for (SseEmitter emitter : emitters) {
sendSafe(emitter, job);
}
if (job.getStatus() == CodeScanJob.Status.COMPLETED
|| job.getStatus() == CodeScanJob.Status.FAILED
|| job.getStatus() == CodeScanJob.Status.CANCELLED) {
for (SseEmitter emitter : emitters) {
try {
emitter.complete();
} catch (Exception ignore) { /* already closed */ }
}
emittersByJob.remove(jobId);
}
}
private void sendSafe(SseEmitter emitter, CodeScanJob job) {
try {
Map<String, Object> payload = new HashMap<>();
payload.put("jobId", job.getId());
payload.put("status", job.getStatus().name());
payload.put("progress", job.getProgress());
payload.put("currentStep", job.getCurrentStep());
payload.put("filesParsed", job.getFilesParsed());
payload.put("filesTotal", job.getFilesTotal());
payload.put("suggestionsEmitted", job.getSuggestionsEmitted());
payload.put("message", job.getMessage());
emitter.send(SseEmitter.event().name("progress").data(payload));
} catch (Exception e) {
removeEmitter(job.getId(), emitter);
}
}
private void removeEmitter(String jobId, SseEmitter emitter) {
var emitters = emittersByJob.get(jobId);
if (emitters != null) emitters.remove(emitter);
}
private void closeEmitters(String jobId) {
var emitters = emittersByJob.remove(jobId);
if (emitters == null) return;
for (SseEmitter emitter : emitters) {
try { emitter.complete(); } catch (Exception ignore) { /* */ }
}
}
private static String defaultName(MultipartFile file) {
String name = file.getOriginalFilename();
return (name == null || name.isBlank()) ? "code-source" : name;
}
private static String truncate(String s, int max) {
if (s == null) return null;
return s.length() <= max ? s : s.substring(0, max);
}
private static void deleteRecursively(Path root) {
if (root == null) return;
try (var stream = java.nio.file.Files.walk(root)) {
stream.sorted(java.util.Comparator.reverseOrder())
.forEach(p -> {
try { java.nio.file.Files.deleteIfExists(p); } catch (IOException ignore) { /* */ }
});
} catch (IOException ignore) {
// workdir may already be cleaned
}
}
}