Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
6c80884
fix: Allow async dispatches and error page rendering without authenti…
rostilos Jan 23, 2026
b5cab46
fix: Add analysis lock checks to prevent duplicate PR analysis in web…
rostilos Jan 23, 2026
d3a4c5f
Merge pull request #87 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
3bc5967
fix: Implement atomic upsert for command rate limiting to prevent rac…
rostilos Jan 23, 2026
8f3a3bb
Merge pull request #89 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
28a4007
fix: Improve alias management by ensuring direct collections are dele…
rostilos Jan 23, 2026
efc42d2
fix: Enhance locking mechanism in PR webhook handlers to prevent race…
rostilos Jan 23, 2026
c673dce
Merge pull request #90 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
13a63c8
fix: Enhance alias management by implementing backup and migration st…
rostilos Jan 23, 2026
adb68cb
Merge pull request #91 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
95d74e1
fix: Enhance AI analysis by incorporating full PR issue history and r…
rostilos Jan 23, 2026
85c47e3
Merge pull request #92 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
e509ffc
fix: Update issue reconciliation logic to handle previous issues in b…
rostilos Jan 23, 2026
ead4b08
Merge pull request #93 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
a2889c7
fix: Improve handling of issue resolution status and logging for bett…
rostilos Jan 23, 2026
7a30044
Merge pull request #94 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
6fb5693
fix: Implement method to retrieve branch differences from GitLab API
rostilos Jan 23, 2026
c6337aa
Merge pull request #95 from rostilos/bugfix/analysis-issues
rostilos Jan 23, 2026
56761fb
fix: Enhance logging and implement deterministic context retrieval in…
rostilos Jan 23, 2026
8fc46f3
Refactor AI connection handling and improve job deletion logic
rostilos Jan 26, 2026
7c78057
feat: Add pre-acquired lock key to prevent double-locking in PR analy…
rostilos Jan 26, 2026
b7be7fe
Merge pull request #97 from rostilos/feature/rag-smart-querying
rostilos Jan 26, 2026
6d80d71
feat: Implement handling for AnalysisLockedException and DiffTooLarge…
rostilos Jan 26, 2026
e2c1474
feat: Re-fetch job entities in transaction methods to handle detached…
rostilos Jan 27, 2026
342c4fa
feat: Update JobService and WebhookAsyncProcessor to manage job entit…
rostilos Jan 27, 2026
409c42d
feat: Enable transaction management in processWebhookAsync to support…
rostilos Jan 27, 2026
11c983c
feat: Re-fetch job entities in JobService methods to ensure consisten…
rostilos Jan 27, 2026
c75eaba
feat: Add @Transactional annotation to processWebhookAsync for lazy l…
rostilos Jan 27, 2026
8afc0ad
feat: Implement self-injection in WebhookAsyncProcessor for proper tr…
rostilos Jan 27, 2026
402486b
feat: Enhance logging and error handling in processWebhookAsync for i…
rostilos Jan 27, 2026
fdcdca0
feat: Implement webhook deduplication service to prevent duplicate co…
rostilos Jan 27, 2026
e321361
feat: Enhance job deletion process with logging and persistence conte…
rostilos Jan 27, 2026
ebd0fad
feat: Improve job deletion process with enhanced logging and error ha…
rostilos Jan 27, 2026
092b361
feat: Add method to delete job by ID in JobRepository and update JobS…
rostilos Jan 27, 2026
61d2620
feat: Simplify job handling by marking ignored jobs as SKIPPED instea…
rostilos Jan 27, 2026
704a7a2
feat: Enhance AI connection logging and refactor placeholder manageme…
rostilos Jan 28, 2026
2e42ebc
feat: Add logging for LLM creation and enhance diff snippet extractio…
rostilos Jan 28, 2026
d036fa9
feat: Implement AST-based code splitter and scoring configuration
rostilos Jan 28, 2026
642bda0
feat: Enhance lock management in PullRequestAnalysisProcessor and imp…
rostilos Jan 28, 2026
5add89c
feat: Enhance AST processing and metadata extraction in RAG pipeline …
rostilos Jan 28, 2026
1fc484c
feat: Improve deduplication strategy in RAGQueryService to prioritize…
rostilos Jan 28, 2026
c03591e
feat: Enhance comments for clarity on target branch indexing and incr…
rostilos Jan 28, 2026
0bb9ca8
feat: Update default configuration values for chunk size and text chu…
rostilos Jan 28, 2026
3bb9025
feat: Implement PR-specific indexing and hybrid query support in RAG …
rostilos Jan 29, 2026
585ab6d
Merge pull request #106 from rostilos/feature/pr-analysis-rate-limiting
rostilos Jan 29, 2026
29859ee
feat: Enhance PR indexing with improved error handling and content pr…
rostilos Jan 29, 2026
7770367
feat: Add resolution tracking fields to CodeReviewIssue and enhance i…
rostilos Jan 30, 2026
c9899fc
Refactor RAG pipeline: Remove deprecated methods and classes, update …
rostilos Feb 1, 2026
2ae1345
Batch review in MCP-client, batch size from 3 to 5 files from a diff
rostilos Feb 1, 2026
15d209f
Merge pull request #108 from rostilos/feature/PR-qdrant-indexes
rostilos Feb 1, 2026
6ad8ed9
feat: Enhance webhook processing and deduplication
rostilos Feb 1, 2026
a3c978a
Merge pull request #110 from rostilos/feature/PR-qdrant-indexes
rostilos Feb 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions java-ecosystem/libs/analysis-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<artifactId>okhttp</artifactId>
</dependency>

<!-- JTokkit for token counting -->
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
requires com.fasterxml.jackson.annotation;
requires jakarta.persistence;
requires kotlin.stdlib;
requires jtokkit;

exports org.rostilos.codecrow.analysisengine.aiclient;
exports org.rostilos.codecrow.analysisengine.config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public Map<String, Object> performAnalysis(AiAnalysisRequest request, java.util.

/**
* Extracts analysis data from nested response structure.
* Expected: response -> result -> {comment, issues}
* Expected: response -> result -> {comment, issues, inference_stats}
* Issues can be either a List (array) or Map (object with numeric keys)
*/
private Map<String, Object> extractAndValidateAnalysisData(Map response) throws IOException {
Expand All @@ -176,6 +176,15 @@ private Map<String, Object> extractAndValidateAnalysisData(Map response) throws
if (result == null) {
throw new IOException("Missing 'result' field in AI response");
}

// Check for error response from MCP client
Object errorFlag = result.get("error");
if (Boolean.TRUE.equals(errorFlag) || "true".equals(String.valueOf(errorFlag))) {
String errorMessage = result.get("error_message") != null
? String.valueOf(result.get("error_message"))
: String.valueOf(result.get("comment"));
throw new IOException("Analysis failed: " + errorMessage);
}

if (!result.containsKey("comment") || !result.containsKey("issues")) {
throw new IOException("Analysis data missing required fields: 'comment' and/or 'issues'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package org.rostilos.codecrow.analysisengine.dto.request.ai;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -278,6 +278,113 @@
return self();
}

/**
* Set previous issues from ALL PR analysis versions.
* This provides the LLM with complete issue history including resolved issues,
* helping it understand what was already found and fixed.
*
* Issues are deduplicated by fingerprint (file + line ±3 + severity + truncated reason).
* When duplicates exist across versions, we keep the most recent version's data
* but preserve resolved status if ANY version marked it resolved.
*
* @param allPrAnalyses List of all analyses for this PR, ordered by version DESC (newest first)
*/
public T withAllPrAnalysesData(List<CodeAnalysis> allPrAnalyses) {
if (allPrAnalyses == null || allPrAnalyses.isEmpty()) {
return self();
}

// Convert all issues to DTOs
List<AiRequestPreviousIssueDTO> allIssues = allPrAnalyses.stream()
.flatMap(analysis -> analysis.getIssues().stream())
.map(AiRequestPreviousIssueDTO::fromEntity)
.toList();

// Deduplicate: group by fingerprint, keep most recent version but preserve resolved status
java.util.Map<String, AiRequestPreviousIssueDTO> deduped = new java.util.LinkedHashMap<>();

for (AiRequestPreviousIssueDTO issue : allIssues) {
String fingerprint = computeIssueFingerprint(issue);
AiRequestPreviousIssueDTO existing = deduped.get(fingerprint);

if (existing == null) {
// First occurrence of this issue
deduped.put(fingerprint, issue);
} else {
// Duplicate found - keep the one with higher prVersion (more recent)
// But if older version is resolved and newer is not, preserve resolved status
int existingVersion = existing.prVersion() != null ? existing.prVersion() : 0;
int currentVersion = issue.prVersion() != null ? issue.prVersion() : 0;

boolean existingResolved = "resolved".equalsIgnoreCase(existing.status());
boolean currentResolved = "resolved".equalsIgnoreCase(issue.status());

if (currentVersion > existingVersion) {
// Current is newer - use it, but preserve resolved status if existing was resolved
if (existingResolved && !currentResolved) {
// Older version was resolved but newer one isn't marked - use resolved data from older
deduped.put(fingerprint, mergeResolvedStatus(issue, existing));
} else {
deduped.put(fingerprint, issue);
}
} else if (existingVersion == currentVersion) {
// Same version - prefer resolved one
if (currentResolved && !existingResolved) {
deduped.put(fingerprint, issue);
}
}
// If existing is newer, keep it (already in map)
}
}

this.previousCodeAnalysisIssues = new java.util.ArrayList<>(deduped.values());

return self();
}

/**
* Compute a fingerprint for an issue to detect duplicates across PR versions.
* Uses: file + normalized line (±3 tolerance) + severity + first 50 chars of reason.
*/
private String computeIssueFingerprint(AiRequestPreviousIssueDTO issue) {
String file = issue.file() != null ? issue.file() : "";
// Normalize line to nearest multiple of 3 for tolerance
int lineGroup = issue.line() != null ? (issue.line() / 3) : 0;
String severity = issue.severity() != null ? issue.severity() : "";
String reasonPrefix = issue.reason() != null
? issue.reason().substring(0, Math.min(50, issue.reason().length())).toLowerCase().trim()
: "";

return file + "::" + lineGroup + "::" + severity + "::" + reasonPrefix;
}

/**
* Merge resolved status from an older issue version into a newer one.
* Creates a new DTO with the newer issue's data but the older issue's resolution info.
*/
private AiRequestPreviousIssueDTO mergeResolvedStatus(
AiRequestPreviousIssueDTO newer,
AiRequestPreviousIssueDTO resolvedOlder) {
return new AiRequestPreviousIssueDTO(
newer.id(),
newer.type(),
newer.severity(),
newer.reason(),
newer.suggestedFixDescription(),
newer.suggestedFixDiff(),
newer.file(),
newer.line(),
newer.branch(),
newer.pullRequestId(),
resolvedOlder.status(), // Use resolved status from older
newer.category(),
newer.prVersion(),
resolvedOlder.resolvedDescription(),
resolvedOlder.resolvedByCommit(),
resolvedOlder.resolvedInAnalysisId()
);
}

public T withMaxAllowedTokens(int maxAllowedTokens) {
this.maxAllowedTokens = maxAllowedTokens;
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ public record AiRequestPreviousIssueDTO(
String branch,
String pullRequestId,
String status, // open|resolved|ignored
String category
String category,
// Resolution tracking fields
Integer prVersion, // Which PR iteration this issue was found in
String resolvedDescription, // Description of how the issue was resolved
String resolvedByCommit, // Commit hash that resolved the issue
Long resolvedInAnalysisId // Analysis ID where this was resolved (null if still open)
) {
public static AiRequestPreviousIssueDTO fromEntity(CodeAnalysisIssue issue) {
String categoryStr = issue.getIssueCategory() != null
? issue.getIssueCategory().name()
: IssueCategory.CODE_QUALITY.name();

Integer prVersion = null;
if (issue.getAnalysis() != null) {
prVersion = issue.getAnalysis().getPrVersion();
}

return new AiRequestPreviousIssueDTO(
String.valueOf(issue.getId()),
categoryStr,
Expand All @@ -33,7 +44,11 @@ public static AiRequestPreviousIssueDTO fromEntity(CodeAnalysisIssue issue) {
issue.getAnalysis() == null ? null : issue.getAnalysis().getBranchName(),
issue.getAnalysis() == null || issue.getAnalysis().getPrNumber() == null ? null : String.valueOf(issue.getAnalysis().getPrNumber()),
issue.isResolved() ? "resolved" : "open",
categoryStr
categoryStr,
prVersion,
issue.getResolvedDescription(),
issue.getResolvedCommitHash(),
issue.getResolvedAnalysisId()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public class PrProcessRequest implements AnalysisProcessRequest {
public String prAuthorId;

public String prAuthorUsername;

/**
* Optional pre-acquired lock key. If set, the processor will skip lock acquisition
* and use this lock key directly. This prevents double-locking when the webhook handler
* has already acquired the lock before calling the processor.
*/
public String preAcquiredLockKey;


public Long getProjectId() {
Expand Down Expand Up @@ -64,4 +71,6 @@ public String getSourceBranchName() {
public String getPrAuthorId() { return prAuthorId; }

public String getPrAuthorUsername() { return prAuthorUsername; }

public String getPreAcquiredLockKey() { return preAcquiredLockKey; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.rostilos.codecrow.analysisengine.exception;

/**
* Exception thrown when a diff exceeds the configured token limit for analysis.
* This is a soft skip - the analysis is not performed but the job is not marked as failed.
*/
public class DiffTooLargeException extends RuntimeException {

private final int estimatedTokens;
private final int maxAllowedTokens;
private final Long projectId;
private final Long pullRequestId;

public DiffTooLargeException(int estimatedTokens, int maxAllowedTokens, Long projectId, Long pullRequestId) {
super(String.format(
"PR diff exceeds token limit: estimated %d tokens, max allowed %d tokens (project=%d, PR=%d)",
estimatedTokens, maxAllowedTokens, projectId, pullRequestId
));
this.estimatedTokens = estimatedTokens;
this.maxAllowedTokens = maxAllowedTokens;
this.projectId = projectId;
this.pullRequestId = pullRequestId;
}

public int getEstimatedTokens() {
return estimatedTokens;
}

public int getMaxAllowedTokens() {
return maxAllowedTokens;
}

public Long getProjectId() {
return projectId;
}

public Long getPullRequestId() {
return pullRequestId;
}

/**
* Returns the percentage of the token limit that would be used.
*/
public double getUtilizationPercentage() {
return maxAllowedTokens > 0 ? (estimatedTokens * 100.0 / maxAllowedTokens) : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -90,34 +91,45 @@ public Map<String, Object> process(
// Publish analysis started event
publishAnalysisStartedEvent(project, request, correlationId);

Optional<String> lockKey = analysisLockService.acquireLockWithWait(
project,
request.getSourceBranchName(),
AnalysisLockType.PR_ANALYSIS,
request.getCommitHash(),
request.getPullRequestId(),
consumer::accept
);

if (lockKey.isEmpty()) {
String message = String.format(
"Failed to acquire lock after %d minutes for project=%s, PR=%d, branch=%s. Another analysis is still in progress.",
analysisLockService.getLockWaitTimeoutMinutes(),
project.getId(),
request.getPullRequestId(),
request.getSourceBranchName()
);
log.warn(message);

// Publish failed event due to lock timeout
publishAnalysisCompletedEvent(project, request, correlationId, startTime,
AnalysisCompletedEvent.CompletionStatus.FAILED, 0, 0, "Lock acquisition timeout");

throw new AnalysisLockedException(
AnalysisLockType.PR_ANALYSIS.name(),
// Check if a lock was already acquired by the caller (e.g., webhook handler)
// to prevent double-locking which causes unnecessary 2-minute waits
String lockKey;
boolean isPreAcquired = false;
if (request.getPreAcquiredLockKey() != null && !request.getPreAcquiredLockKey().isBlank()) {
lockKey = request.getPreAcquiredLockKey();
isPreAcquired = true;
log.info("Using pre-acquired lock: {} for project={}, PR={}", lockKey, project.getId(), request.getPullRequestId());
} else {
Optional<String> acquiredLock = analysisLockService.acquireLockWithWait(
project,
request.getSourceBranchName(),
project.getId()
AnalysisLockType.PR_ANALYSIS,
request.getCommitHash(),
request.getPullRequestId(),
consumer::accept
);

if (acquiredLock.isEmpty()) {
String message = String.format(
"Failed to acquire lock after %d minutes for project=%s, PR=%d, branch=%s. Another analysis is still in progress.",
analysisLockService.getLockWaitTimeoutMinutes(),
project.getId(),
request.getPullRequestId(),
request.getSourceBranchName()
);
log.warn(message);

// Publish failed event due to lock timeout
publishAnalysisCompletedEvent(project, request, correlationId, startTime,
AnalysisCompletedEvent.CompletionStatus.FAILED, 0, 0, "Lock acquisition timeout");

throw new AnalysisLockedException(
AnalysisLockType.PR_ANALYSIS.name(),
request.getSourceBranchName(),
project.getId()
);
}
lockKey = acquiredLock.get();
}

try {
Expand All @@ -139,16 +151,24 @@ public Map<String, Object> process(
return Map.of("status", "cached", "cached", true);
}

Optional<CodeAnalysis> previousAnalysis = codeAnalysisService.getPreviousVersionCodeAnalysis(
// Get all previous analyses for this PR to provide full issue history to AI
List<CodeAnalysis> allPrAnalyses = codeAnalysisService.getAllPrAnalyses(
project.getId(),
request.getPullRequestId()
);

// Get the most recent analysis for incremental diff calculation
Optional<CodeAnalysis> previousAnalysis = allPrAnalyses.isEmpty()
? Optional.empty()
: Optional.of(allPrAnalyses.get(0));

// Ensure branch index exists for target branch if configured
// Ensure branch index exists for TARGET branch (e.g., "1.2.1-rc")
// This is where the PR will merge TO - we want RAG context from this branch
ensureRagIndexForTargetBranch(project, request.getTargetBranchName(), consumer);

VcsAiClientService aiClientService = vcsServiceFactory.getAiClientService(provider);
AiAnalysisRequest aiRequest = aiClientService.buildAiAnalysisRequest(project, request, previousAnalysis);
AiAnalysisRequest aiRequest = aiClientService.buildAiAnalysisRequest(
project, request, previousAnalysis, allPrAnalyses);

Map<String, Object> aiResponse = aiAnalysisClient.performAnalysis(aiRequest, event -> {
try {
Expand Down Expand Up @@ -208,7 +228,9 @@ public Map<String, Object> process(

return Map.of("status", "error", "message", e.getMessage());
} finally {
analysisLockService.releaseLock(lockKey.get());
if (!isPreAcquired) {
analysisLockService.releaseLock(lockKey);
}
}
}

Expand Down
Loading