Compare commits

..

1 Commits

Author SHA1 Message Date
u80864958
ac5ff7aeeb feat(pierre): add diff chunking and configurable review settings 2026-02-13 17:04:22 +01:00
2 changed files with 18 additions and 27 deletions

View File

@@ -9,8 +9,7 @@ import (
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
)
// DefaultChunkSize is the fallback maximum size (in bytes) for a diff chunk when no explicit value is configured.
const DefaultChunkSize = 60000
const defaultChunkSize = 60000
type Comment struct {
File string `json:"file"`
@@ -27,41 +26,36 @@ func (s *Service) judgePR(ctx context.Context, diff io.Reader) (comments []Comme
// Determine chunk size (use default if not set)
maxSize := s.maxChunkSize
if maxSize <= 0 {
maxSize = DefaultChunkSize // default 60KB ~ 15k tokens
maxSize = defaultChunkSize // default 60KB ~ 15k tokens
}
chunks := splitDiffIntoChunks(diffBytes, maxSize)
allComments := []Comment{}
// Build optional guidelines text (added as a separate section with a clear delimiter)
// Build optional guidelines text
guidelinesText := ""
if len(s.guidelines) > 0 {
// Two newlines ensure the guidelines start on a fresh paragraph.
guidelinesText = "\n\nProject guidelines:\n"
guidelinesText = "Project guidelines:\n"
for _, g := range s.guidelines {
guidelinesText += "- " + g + "\n"
}
}
// System prompt that instructs the LLM precisely.
baseSystem := strings.TrimSpace(`
You are a strict senior software architect.
Only comment on newly added or modified lines in the diff; ignore deletions, pure formatting, or reordering that does not change behavior.
For each issue output a JSON object with fields "file", "line", and "message" (message should be concise, ≤2 sentences, and actionable).
If project guidelines are provided, treat them as hard rules that must be respected.`) + guidelinesText
You are a very strict senior software architect.
You review **only** newly added or modified lines in a unified diff, together with the immediate hunk context.
You do **not** report issues that appear **solely** in deleted lines (“-") or that have already been fixed by the change.
No comments are made on pure formatting/whitespace changes or reordering that does not alter the programs behavior.`) + guidelinesText
for i, chunk := range chunks {
// Include the chunk identifier in the system message only if there are multiple chunks.
systemContent := baseSystem
if len(chunks) > 1 {
systemContent = fmt.Sprintf("%s\nChunk %d of %d.", baseSystem, i+1, len(chunks))
}
userContent := chunk
// Add a small header so the model knows this is a fragment
header := fmt.Sprintf("\n--- Chunk %d of %d ---\n", i+1, len(chunks))
userContent := fmt.Sprintf("Hello please review my PR. Write comments where improvements are necessary in new lines.%s\nHere is the git diff of it: %s", header, chunk)
var chunkComments []Comment
err = s.chat.GenerateStructured(ctx, []chatter.Message{{
Role: chatter.RoleSystem,
Content: systemContent,
Content: baseSystem,
}, {
Role: chatter.RoleUser,
Content: userContent,

View File

@@ -26,18 +26,15 @@ func (s *Service) MakeReview(ctx context.Context, organisation string, repo stri
for _, c := range comments {
c.Message = fmt.Sprintf("%s (Generated by: %s)", c.Message, model)
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
c.File, c.Line, c.Message, "---")
if s.disableComments {
// Dryrun: just log what would have been posted.
log.Printf("dryrun: %s:%d => %s", c.File, c.Line, c.Message)
} else {
// Normal mode: print to stdout and post the comment to the VCS.
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
c.File, c.Line, c.Message, "---")
if err := s.git.AddComment(ctx, organisation, repo, prID, c); err != nil {
log.Printf("Failed to add comment: %v", err)
}
log.Printf("dry-run: not posting comment for %s:%d", c.File, c.Line)
} else if err := s.git.AddComment(ctx, organisation, repo, prID, c); err != nil {
log.Printf("Failed to add comment: %v", err)
}
}
return nil