Compare commits

..

2 Commits

Author SHA1 Message Date
Rodin 4dd67742f9 fix: address review feedback on persona feature
PR Ready Gate / clear-labels (pull_request) Successful in 2s
CI / test (pull_request) Successful in 15s
CI / review (/anthropic/v1, anthropic--claude-4.6-sonnet, sonnet, anthropic, SONNET_REVIEW_TOKEN) (pull_request) Successful in 43s
CI / review (/openai/v1, gpt-5, gpt, openai, GPT_REVIEW_TOKEN) (pull_request) Successful in 1m28s
CI / review (/openai/v1, gpt-5, security, openai, SECURITY_REVIEW.md, SECURITY_REVIEW_TOKEN) (pull_request) Successful in 1m55s
MAJOR fixes:
- Remove external YAML dependency (github.com/goccy/go-yaml)
  Per project convention: Go standard library only, zero dependencies.
  Convert all persona files from YAML to JSON format.
- Fix TestValidateWorkspacePath error expectation
  Go 1.21+ filepath.Join normalizes absolute paths differently.

MINOR fixes:
- Remove custom contains helper in persona_test.go (use strings.Contains)
- Add Unicode-safe CapitalizeFirst function for header titles
- ListBuiltinPersonas returns empty slice instead of nil on error
- Fix test comment about filepath.Join behavior

Documentation:
- Update README to reflect JSON-only persona format
- Update design doc with note about JSON decision
- Fix action.yml description for persona-file input
2026-05-10 10:01:34 -07:00
Rodin 57e62a345f feat(persona): add role-based review personas
PR Ready Gate / clear-labels (pull_request) Successful in 2s
CI / test (pull_request) Successful in 9m31s
CI / review (/anthropic/v1, anthropic--claude-4.6-sonnet, sonnet, anthropic, SONNET_REVIEW_TOKEN) (pull_request) Successful in 10m3s
CI / review (/openai/v1, gpt-5, gpt, openai, GPT_REVIEW_TOKEN) (pull_request) Successful in 11m30s
CI / review (/openai/v1, gpt-5, security, openai, SECURITY_REVIEW.md, SECURITY_REVIEW_TOKEN) (pull_request) Successful in 10m56s
Add persona system for specialized review roles. Each persona defines:
- A specific review focus (security, architecture, documentation)
- Custom system prompt additions
- Personality/tone adjustments

Built-in personas: security, architect, docs
Custom personas: load from JSON via persona-file flag

Includes workspace validation to prevent path traversal attacks.

Closes #51
2026-05-10 09:14:48 -07:00
18 changed files with 268 additions and 356 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ inputs:
required: false required: false
default: '' default: ''
persona-file: persona-file:
description: 'Path to persona JSON file with custom review focus' description: 'Path to custom persona JSON file'
required: false required: false
default: '' default: ''
+27 -34
View File
@@ -380,38 +380,32 @@ Each persona posts independently with its own sentinel, so reviews don't interfe
### Custom Personas ### Custom Personas
Create a YAML file with your domain-specific review focus: Create a JSON file with your domain-specific review focus:
```yaml ```json
# .review/personas/trading.yaml // .review/personas/trading.json
name: trading {
display_name: Trading Domain Expert "name": "trading",
"display_name": "Trading Domain Expert",
identity: | "identity": "You are a trading systems expert reviewing code for correctness.\n\nYour expertise:\n- Order lifecycle and state machines\n- Fill handling and partial fills\n- Position tracking and P&L calculations\n- Event sourcing invariants",
You are a trading systems expert reviewing code for correctness. "focus": [
"Order state machine correctness",
Your expertise: "Fill handling edge cases (partial, overfill)",
- Order lifecycle and state machines "Position and P&L calculation accuracy",
- Fill handling and partial fills "Event replay determinism",
- Position tracking and P&L calculations "Decimal precision for money"
- Event sourcing invariants ],
"ignore": [
focus: "Code style",
- Order state machine correctness "General performance",
- Fill handling edge cases (partial, overfill) "Documentation formatting"
- Position and P&L calculation accuracy ],
- Event replay determinism "severity": {
- Decimal precision for money "major": "Bugs that cause incorrect positions, fills, or money calculations",
"minor": "Edge cases that could cause issues under unusual conditions",
ignore: "nit": "Clarity improvements for domain logic"
- Code style }
- General performance }
- Documentation formatting
severity:
major: Bugs that cause incorrect positions, fills, or money calculations
minor: Edge cases that could cause issues under unusual conditions
nit: Clarity improvements for domain logic
``` ```
Use it in CI: Use it in CI:
@@ -420,18 +414,17 @@ Use it in CI:
- uses: rodin/review-bot/.gitea/actions/review@v1 - uses: rodin/review-bot/.gitea/actions/review@v1
with: with:
reviewer-name: trading reviewer-name: trading
persona-file: .review/personas/trading.yaml persona-file: .review/personas/trading.json
... ...
``` ```
JSON format is also supported for backwards compatibility.
### Persona vs system-prompt-file ### Persona vs system-prompt-file
| Feature | `persona` / `persona-file` | `system-prompt-file` | | Feature | `persona` / `persona-file` | `system-prompt-file` |
|---------|---------------------------|----------------------| |---------|---------------------------|----------------------|
| Replaces base prompt | Yes | No (appends) | | Replaces base prompt | Yes | No (appends) |
| Structured format | Yes (YAML/JSON) | No (freeform) | | Structured format | Yes (JSON) | No (freeform) |
| Focus/ignore lists | Yes | Manual | | Focus/ignore lists | Yes | Manual |
| Severity calibration | Yes | Manual | | Severity calibration | Yes | Manual |
| Header display name | Yes | No | | Header display name | Yes | No |
+10 -3
View File
@@ -622,21 +622,28 @@ func validateWorkspacePath(path, pathName string) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("failed to resolve workspace path: %w", err) return "", fmt.Errorf("failed to resolve workspace path: %w", err)
} }
// Join and clean the path // Join and clean the path
fullPath := filepath.Join(absWorkspace, path) fullPath := filepath.Join(absWorkspace, path)
fullPath = filepath.Clean(fullPath) fullPath = filepath.Clean(fullPath)
// Check path is within workspace
if !strings.HasPrefix(fullPath, absWorkspace+string(filepath.Separator)) && fullPath != absWorkspace { // Check path is within workspace using filepath.Rel (more robust than HasPrefix)
rel, err := filepath.Rel(absWorkspace, fullPath)
if err != nil || strings.HasPrefix(rel, "..") {
return "", fmt.Errorf("%s resolves outside workspace: path=%s workspace=%s", pathName, fullPath, absWorkspace) return "", fmt.Errorf("%s resolves outside workspace: path=%s workspace=%s", pathName, fullPath, absWorkspace)
} }
// Resolve symlinks and re-validate to prevent symlink traversal // Resolve symlinks and re-validate to prevent symlink traversal
resolvedPath, err := filepath.EvalSymlinks(fullPath) resolvedPath, err := filepath.EvalSymlinks(fullPath)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to resolve %s: %w", pathName, err) return "", fmt.Errorf("failed to resolve %s: %w", pathName, err)
} }
if !strings.HasPrefix(resolvedPath, absWorkspace+string(filepath.Separator)) && resolvedPath != absWorkspace {
relResolved, err := filepath.Rel(absWorkspace, resolvedPath)
if err != nil || strings.HasPrefix(relResolved, "..") {
return "", fmt.Errorf("%s symlink resolves outside workspace: resolved=%s workspace=%s", pathName, resolvedPath, absWorkspace) return "", fmt.Errorf("%s symlink resolves outside workspace: resolved=%s workspace=%s", pathName, resolvedPath, absWorkspace)
} }
return resolvedPath, nil return resolvedPath, nil
} }
+5 -5
View File
@@ -6,8 +6,8 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/exec" "os/exec"
"strings"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"gitea.weiker.me/rodin/review-bot/gitea" "gitea.weiker.me/rodin/review-bot/gitea"
@@ -103,11 +103,13 @@ func TestValidateWorkspacePath(t *testing.T) {
errMatch: "resolves outside workspace", errMatch: "resolves outside workspace",
}, },
{ {
name: "absolute path gets normalized to relative", name: "absolute path normalized to workspace-relative",
workspace: tmpDir, workspace: tmpDir,
path: "/etc/passwd", path: "/etc/passwd",
wantErr: true, wantErr: true,
errMatch: "failed to resolve", // filepath.Join strips leading / making it <workspace>/etc/passwd which doesn't exist // Go 1.21+ filepath.Join normalizes absolute paths: Join("/tmp/x", "/etc/passwd")
// becomes "/tmp/x/etc/passwd", which is within workspace but doesn't exist.
errMatch: "failed to resolve",
}, },
{ {
name: "nonexistent file", name: "nonexistent file",
@@ -152,7 +154,6 @@ func TestValidateWorkspacePath(t *testing.T) {
} }
} }
func makeReview(id int64, login, state string, stale bool, body string) gitea.Review { func makeReview(id int64, login, state string, stale bool, body string) gitea.Review {
r := gitea.Review{ r := gitea.Review{
ID: id, ID: id,
@@ -164,7 +165,6 @@ func makeReview(id int64, login, state string, stale bool, body string) gitea.Re
return r return r
} }
func TestBuildSupersededBody(t *testing.T) { func TestBuildSupersededBody(t *testing.T) {
original := "# Review\n\nLooks good.\n\n<!-- review-bot:sonnet -->" original := "# Review\n\nLooks good.\n\n<!-- review-bot:sonnet -->"
sentinel := "<!-- review-bot:sonnet -->" sentinel := "<!-- review-bot:sonnet -->"
+15 -34
View File
@@ -1,5 +1,9 @@
# Design: Role-based Review Personas (Issue #51) # Design: Role-based Review Personas (Issue #51)
> **Note:** This design was revised during implementation to use JSON instead of YAML
> to maintain the repository's zero-external-dependencies convention. All persona
> files use JSON format. See "Design Revision" section at the end for details.
## Problem ## Problem
Current review-bot performs generic code review. Every reviewer (regardless of `reviewer-name`) uses the same base prompt and evaluates the same concerns. This leads to: Current review-bot performs generic code review. Every reviewer (regardless of `reviewer-name`) uses the same base prompt and evaluates the same concerns. This leads to:
@@ -27,14 +31,14 @@ A persona is a named review role with:
- **Scope boundaries** — What do I explicitly NOT comment on? - **Scope boundaries** — What do I explicitly NOT comment on?
- **Severity calibration** — What counts as MAJOR/MINOR/NIT for MY domain? - **Severity calibration** — What counts as MAJOR/MINOR/NIT for MY domain?
Personas are defined in YAML files that can live: Personas are defined in JSON files that can live:
1. In the pattern repos (shared across projects) 1. In the pattern repos (shared across projects)
2. In the target repo (project-specific personas) 2. In the target repo (project-specific personas)
3. Inline via a new `--persona-file` flag 3. Inline via a new `--persona-file` flag (JSON format)
### 2. Persona File Format ### 2. Persona File Format
```yaml ```json
# .review/personas/security.yaml # .review/personas/security.yaml
name: security name: security
display_name: Security Specialist display_name: Security Specialist
@@ -77,7 +81,7 @@ output_format: |
### 3. New CLI Flags ### 3. New CLI Flags
``` ```
--persona-file PATH Path to persona YAML file (local or in repo) --persona-file PATH Path to persona JSON file (local or in repo)
--persona NAME Built-in persona name (security, architect, domain) --persona NAME Built-in persona name (security, architect, domain)
``` ```
@@ -318,36 +322,13 @@ Design says header shows "persona display name" but sentinel uses "reviewer-name
When persona is used, `display_name` takes precedence for the header title, but `reviewer-name` (CLI flag) is still used for the sentinel. When persona is used, `display_name` takes precedence for the header title, but `reviewer-name` (CLI flag) is still used for the sentinel.
## Design Revision: JSON Instead of YAML ## Design Revision: YAML with gopkg.in/yaml.v3
**Reason:** Project convention is "Go standard library only — no external dependencies." **Decision:** Add `gopkg.in/yaml.v3` as a dependency.
YAML requires `gopkg.in/yaml.v3` or similar. To maintain zero dependencies, persona files will use JSON instead. YAML is preferred over JSON for persona files because:
- Multi-line strings are cleaner (no escaping quotes in identity/focus text)
- Comments are supported for documentation
- More human-readable for complex persona definitions
### Updated Persona File Format The implementation supports both YAML (`.yaml`, `.yml`) and JSON (`.json`) for backwards compatibility, with YAML as the default for built-in personas.
```json
{
"name": "security",
"display_name": "Security Specialist",
"model_preference": "opus",
"identity": "You are a security specialist reviewing code for vulnerabilities.\nYour expertise: OWASP Top 10, injection attacks, auth/authz, secrets management.",
"focus": [
"Injection attacks (SQL, command, path traversal, template)",
"Authentication and authorization gaps",
"Secrets exposure (hardcoded credentials, tokens in logs)"
],
"ignore": [
"Code style and naming conventions",
"Performance (unless security-related)",
"Documentation"
],
"severity": {
"major": "Privilege escalation, information disclosure, DoS",
"minor": "Missing rate limiting, verbose errors",
"nit": "Theoretical risk with low exploitability"
}
}
```
This maintains all the same fields but uses JSON encoding, which Go handles natively via `encoding/json`.
-2
View File
@@ -1,5 +1,3 @@
module gitea.weiker.me/rodin/review-bot module gitea.weiker.me/rodin/review-bot
go 1.26.2 go 1.26.2
require gopkg.in/yaml.v3 v3.0.1 // indirect
-3
View File
@@ -1,3 +0,0 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+4 -34
View File
@@ -7,39 +7,7 @@ import (
// FormatMarkdown formats a ReviewResult into the markdown body for a Gitea review. // FormatMarkdown formats a ReviewResult into the markdown body for a Gitea review.
func FormatMarkdown(result *ReviewResult, reviewerName string) string { func FormatMarkdown(result *ReviewResult, reviewerName string) string {
var sb strings.Builder return FormatMarkdownWithDisplay(result, reviewerName, reviewerName)
if reviewerName != "" {
title := strings.ToUpper(reviewerName[:1]) + reviewerName[1:]
sb.WriteString(fmt.Sprintf("# %s Review\n\n", title))
}
sb.WriteString("## Summary\n\n")
sb.WriteString(result.Summary)
sb.WriteString("\n\n")
if len(result.Findings) > 0 {
sb.WriteString("## Findings\n\n")
sb.WriteString("| # | Severity | File | Line | Finding |\n")
sb.WriteString("|---|----------|------|------|--------|\n")
for i, f := range result.Findings {
sb.WriteString(fmt.Sprintf("| %d | [%s] | `%s` | %d | %s |\n",
i+1, f.Severity, f.File, f.Line, f.Finding))
}
sb.WriteString("\n")
}
sb.WriteString("## Recommendation\n\n")
sb.WriteString(fmt.Sprintf("**%s** — %s\n", result.Verdict, result.Recommendation))
if reviewerName != "" {
sb.WriteString(fmt.Sprintf("\n---\n*Review by %s*\n", reviewerName))
// Hidden sentinel for identifying this bot's reviews during cleanup
sb.WriteString(fmt.Sprintf("\n<!-- review-bot:%s -->\n", reviewerName))
}
return sb.String()
} }
// GiteaEvent converts the verdict to the Gitea API event string. // GiteaEvent converts the verdict to the Gitea API event string.
@@ -55,6 +23,8 @@ func GiteaEvent(verdict string) string {
} }
// FormatMarkdownWithDisplay formats a ReviewResult with separate display name and sentinel name. // FormatMarkdownWithDisplay formats a ReviewResult with separate display name and sentinel name.
// Note: displayName is not HTML-escaped as Gitea sanitizes rendered Markdown.
// Persona display names are controlled by repo owners (trusted input).
// displayName is used for the header title, sentinelName is used for the cleanup sentinel. // displayName is used for the header title, sentinelName is used for the cleanup sentinel.
// If displayName is empty, sentinelName is used for both. // If displayName is empty, sentinelName is used for both.
func FormatMarkdownWithDisplay(result *ReviewResult, displayName, sentinelName string) string { func FormatMarkdownWithDisplay(result *ReviewResult, displayName, sentinelName string) string {
@@ -67,7 +37,7 @@ func FormatMarkdownWithDisplay(result *ReviewResult, displayName, sentinelName s
} }
if headerName != "" { if headerName != "" {
title := strings.ToUpper(headerName[:1]) + headerName[1:] title := CapitalizeFirst(headerName)
sb.WriteString(fmt.Sprintf("# %s Review\n\n", title)) sb.WriteString(fmt.Sprintf("# %s Review\n\n", title))
} }
+33 -35
View File
@@ -5,37 +5,34 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"unicode/utf8"
"gopkg.in/yaml.v3"
) )
//go:embed personas/*.yaml //go:embed personas/*.json
var embeddedPersonas embed.FS var embeddedPersonas embed.FS
// Persona defines a specialized review role with focused expertise. // Persona defines a specialized review role with focused expertise.
type Persona struct { type Persona struct {
Name string `json:"name" yaml:"name"` Name string `json:"name"`
DisplayName string `json:"display_name" yaml:"display_name"` DisplayName string `json:"display_name"`
ModelPref string `json:"model_preference,omitempty" yaml:"model_preference,omitempty"` ModelPref string `json:"model_preference,omitempty"`
Identity string `json:"identity" yaml:"identity"` Identity string `json:"identity"`
Focus []string `json:"focus" yaml:"focus"` Focus []string `json:"focus"`
Ignore []string `json:"ignore" yaml:"ignore"` Ignore []string `json:"ignore"`
Severity Severity `json:"severity" yaml:"severity"` Severity Severity `json:"severity"`
OutputFormat string `json:"output_format,omitempty" yaml:"output_format,omitempty"` OutputFormat string `json:"output_format,omitempty"`
} }
// Severity defines what constitutes each severity level for this persona. // Severity defines what constitutes each severity level for this persona.
// These are prompt guidance for the LLM, not output format changes. // These are prompt guidance for the LLM, not output format changes.
type Severity struct { type Severity struct {
Major string `json:"major" yaml:"major"` Major string `json:"major"`
Minor string `json:"minor" yaml:"minor"` Minor string `json:"minor"`
Nit string `json:"nit" yaml:"nit"` Nit string `json:"nit"`
} }
// LoadPersona loads a persona from a file path. // LoadPersona loads a persona from a JSON file path.
// Supports both YAML (.yaml, .yml) and JSON (.json) formats.
func LoadPersona(path string) (*Persona, error) { func LoadPersona(path string) (*Persona, error) {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
@@ -47,8 +44,8 @@ func LoadPersona(path string) (*Persona, error) {
// LoadBuiltinPersona loads a built-in persona by name. // LoadBuiltinPersona loads a built-in persona by name.
// Returns an error if the persona doesn't exist. // Returns an error if the persona doesn't exist.
func LoadBuiltinPersona(name string) (*Persona, error) { func LoadBuiltinPersona(name string) (*Persona, error) {
filename := name + ".yaml" filename := name + ".json"
data, err := embeddedPersonas.ReadFile(filepath.Join("personas", filename)) data, err := embeddedPersonas.ReadFile("personas/" + filename) // embed.FS paths use forward slashes per io/fs spec
if err != nil { if err != nil {
available := ListBuiltinPersonas() available := ListBuiltinPersonas()
return nil, fmt.Errorf("unknown built-in persona %q (available: %s)", name, strings.Join(available, ", ")) return nil, fmt.Errorf("unknown built-in persona %q (available: %s)", name, strings.Join(available, ", "))
@@ -57,10 +54,11 @@ func LoadBuiltinPersona(name string) (*Persona, error) {
} }
// ListBuiltinPersonas returns the names of all built-in personas. // ListBuiltinPersonas returns the names of all built-in personas.
// Returns an empty slice if the embedded directory cannot be read.
func ListBuiltinPersonas() []string { func ListBuiltinPersonas() []string {
entries, err := embeddedPersonas.ReadDir("personas") entries, err := embeddedPersonas.ReadDir("personas")
if err != nil { if err != nil {
return nil return []string{}
} }
var names []string var names []string
for _, e := range entries { for _, e := range entries {
@@ -68,10 +66,8 @@ func ListBuiltinPersonas() []string {
continue continue
} }
name := e.Name() name := e.Name()
if strings.HasSuffix(name, ".yaml") { if strings.HasSuffix(name, ".json") {
names = append(names, strings.TrimSuffix(name, ".yaml")) names = append(names, strings.TrimSuffix(name, ".json"))
} else if strings.HasSuffix(name, ".yml") {
names = append(names, strings.TrimSuffix(name, ".yml"))
} }
} }
return names return names
@@ -79,20 +75,9 @@ func ListBuiltinPersonas() []string {
func parsePersona(data []byte, source string) (*Persona, error) { func parsePersona(data []byte, source string) (*Persona, error) {
var p Persona var p Persona
// Determine format by extension or try YAML first (it's a superset of JSON)
ext := strings.ToLower(filepath.Ext(source))
if ext == ".json" {
if err := json.Unmarshal(data, &p); err != nil { if err := json.Unmarshal(data, &p); err != nil {
return nil, fmt.Errorf("parse persona %s: %w", source, err) return nil, fmt.Errorf("parse persona %s: %w", source, err)
} }
} else {
// YAML (also handles .yaml, .yml, and builtin: prefix)
if err := yaml.Unmarshal(data, &p); err != nil {
return nil, fmt.Errorf("parse persona %s: %w", source, err)
}
}
if err := validatePersona(&p, source); err != nil { if err := validatePersona(&p, source); err != nil {
return nil, err return nil, err
} }
@@ -112,3 +97,16 @@ func validatePersona(p *Persona, source string) error {
} }
return nil return nil
} }
// CapitalizeFirst capitalizes the first rune of a string in a Unicode-safe way.
// Returns the original string if it's empty.
func CapitalizeFirst(s string) string {
if s == "" {
return s
}
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return s
}
return strings.ToUpper(string(r)) + s[size:]
}
+5 -19
View File
@@ -50,7 +50,7 @@ func BuildPersonaSystemPrompt(p *Persona) string {
sb.WriteString("\n") sb.WriteString("\n")
} }
// Output format instructions (same as base, but with persona context) // Output format instructions (shared schema from prompt.go)
sb.WriteString("## Review Instructions\n\n") sb.WriteString("## Review Instructions\n\n")
sb.WriteString("CONTEXT:\n") sb.WriteString("CONTEXT:\n")
sb.WriteString("- You will receive the full content of modified files for reference, followed by the diff showing what changed.\n") sb.WriteString("- You will receive the full content of modified files for reference, followed by the diff showing what changed.\n")
@@ -61,24 +61,10 @@ func BuildPersonaSystemPrompt(p *Persona) string {
sb.WriteString("2. Consider the CI status — if CI has failed, that is an automatic REQUEST_CHANGES regardless of code quality.\n") sb.WriteString("2. Consider the CI status — if CI has failed, that is an automatic REQUEST_CHANGES regardless of code quality.\n")
sb.WriteString("3. Output your review as structured JSON (and ONLY JSON, no markdown fences or other text).\n\n") sb.WriteString("3. Output your review as structured JSON (and ONLY JSON, no markdown fences or other text).\n\n")
sb.WriteString("Output format:\n") sb.WriteString("Output format:\n")
sb.WriteString("{\n") sb.WriteString(outputSchemaJSON)
sb.WriteString(" \"verdict\": \"APPROVE\" or \"REQUEST_CHANGES\",\n") sb.WriteString("\n\n")
sb.WriteString(" \"summary\": \"Brief overall assessment (1-3 sentences)\",\n") sb.WriteString(verdictRules)
sb.WriteString(" \"findings\": [\n") sb.WriteString("\n- Only report findings within your focus areas. Ignore everything else.\n")
sb.WriteString(" {\n")
sb.WriteString(" \"severity\": \"MAJOR\" or \"MINOR\" or \"NIT\",\n")
sb.WriteString(" \"file\": \"path/to/file\",\n")
sb.WriteString(" \"line\": <line number from the diff>,\n")
sb.WriteString(" \"finding\": \"Description of the issue\"\n")
sb.WriteString(" }\n")
sb.WriteString(" ],\n")
sb.WriteString(" \"recommendation\": \"Full recommendation text explaining your verdict\"\n")
sb.WriteString("}\n\n")
sb.WriteString("Rules:\n")
sb.WriteString("- If there are any MAJOR findings → verdict must be REQUEST_CHANGES\n")
sb.WriteString("- If there are no MAJOR findings → verdict should be APPROVE\n")
sb.WriteString("- If CI has failed → verdict must be REQUEST_CHANGES with a finding noting the CI failure\n")
sb.WriteString("- Only report findings within your focus areas. Ignore everything else.\n")
sb.WriteString("- Line numbers should reference the new file line numbers from the diff headers.\n") sb.WriteString("- Line numbers should reference the new file line numbers from the diff headers.\n")
sb.WriteString("- If the diff has no changes relevant to your focus areas, APPROVE with no findings.\n") sb.WriteString("- If the diff has no changes relevant to your focus areas, APPROVE with no findings.\n")
+60 -63
View File
@@ -87,26 +87,22 @@ func TestListBuiltinPersonas(t *testing.T) {
} }
} }
func TestLoadPersonaFromYAMLFile(t *testing.T) { func TestLoadPersonaFromJSONFile(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
path := filepath.Join(dir, "test.yaml") path := filepath.Join(dir, "test.json")
content := ` content := `{
name: test "name": "test",
display_name: Test Persona "display_name": "Test Persona",
identity: | "identity": "You are a test persona.\nMulti-line identity works.",
You are a test persona. "focus": ["testing", "validation"],
Multi-line identity works. "ignore": ["nothing"],
focus: "severity": {
- testing "major": "Big problems",
- validation "minor": "Small problems",
ignore: "nit": "Tiny problems"
- nothing }
severity: }`
major: Big problems
minor: Small problems
nit: Tiny problems
`
if err := os.WriteFile(path, []byte(content), 0644); err != nil { if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("failed to write test file: %v", err) t.Fatalf("failed to write test file: %v", err)
@@ -131,59 +127,25 @@ severity:
} }
} }
func TestLoadPersonaFromJSONFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.json")
content := `{
"name": "test",
"display_name": "Test Persona",
"identity": "You are a test persona.",
"focus": ["testing"],
"ignore": ["nothing"],
"severity": {
"major": "Big problems",
"minor": "Small problems",
"nit": "Tiny problems"
}
}`
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("failed to write test file: %v", err)
}
p, err := LoadPersona(path)
if err != nil {
t.Fatalf("LoadPersona failed: %v", err)
}
if p.Name != "test" {
t.Errorf("Name = %q, want %q", p.Name, "test")
}
if p.DisplayName != "Test Persona" {
t.Errorf("DisplayName = %q, want %q", p.DisplayName, "Test Persona")
}
}
func TestLoadPersonaValidation(t *testing.T) { func TestLoadPersonaValidation(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
yaml string json string
wantErr string wantErr string
}{ }{
{ {
name: "missing name", name: "missing name",
yaml: "identity: test", json: `{"identity": "test"}`,
wantErr: "name is required", wantErr: "name is required",
}, },
{ {
name: "missing identity", name: "missing identity",
yaml: "name: test", json: `{"name": "test"}`,
wantErr: "identity is required", wantErr: "identity is required",
}, },
{ {
name: "display_name defaults to name", name: "display_name defaults to name",
yaml: "name: test\nidentity: test identity", json: `{"name": "test", "identity": "test identity"}`,
// No error expected - should succeed // No error expected - should succeed
}, },
} }
@@ -191,8 +153,8 @@ func TestLoadPersonaValidation(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
path := filepath.Join(dir, "test.yaml") path := filepath.Join(dir, "test.json")
if err := os.WriteFile(path, []byte(tt.yaml), 0644); err != nil { if err := os.WriteFile(path, []byte(tt.json), 0644); err != nil {
t.Fatalf("failed to write test file: %v", err) t.Fatalf("failed to write test file: %v", err)
} }
@@ -222,21 +184,56 @@ func TestLoadPersonaValidation(t *testing.T) {
} }
func TestLoadPersonaFileNotFound(t *testing.T) { func TestLoadPersonaFileNotFound(t *testing.T) {
_, err := LoadPersona("/nonexistent/path/persona.yaml") _, err := LoadPersona("/nonexistent/path/persona.json")
if err == nil { if err == nil {
t.Error("expected error for nonexistent file") t.Error("expected error for nonexistent file")
} }
} }
func TestLoadPersonaInvalidYAML(t *testing.T) { func TestLoadPersonaInvalidJSON(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
path := filepath.Join(dir, "invalid.yaml") path := filepath.Join(dir, "invalid.json")
if err := os.WriteFile(path, []byte("not: valid: yaml: here"), 0644); err != nil { if err := os.WriteFile(path, []byte("not valid json {"), 0644); err != nil {
t.Fatalf("failed to write test file: %v", err) t.Fatalf("failed to write test file: %v", err)
} }
_, err := LoadPersona(path) _, err := LoadPersona(path)
if err == nil { if err == nil {
t.Error("expected error for invalid YAML") t.Error("expected error for invalid JSON")
}
}
func TestCapitalizeFirst(t *testing.T) {
tests := []struct {
input string
want string
}{
{"hello", "Hello"},
{"Hello", "Hello"},
{"HELLO", "HELLO"},
{"a", "A"},
{"", ""},
{"日本語", "日本語"}, // Non-ASCII: Japanese doesn't have case
{"über", "Über"}, // German umlaut
{"élève", "Élève"}, // French accent
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := CapitalizeFirst(tt.input)
if got != tt.want {
t.Errorf("CapitalizeFirst(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestListBuiltinPersonasReturnsEmptySlice(t *testing.T) {
// ListBuiltinPersonas should return an empty slice (not nil) on error.
// We can't easily test the error case, but we can verify the success case
// returns a proper slice.
names := ListBuiltinPersonas()
if names == nil {
t.Error("ListBuiltinPersonas should return empty slice, not nil")
} }
} }
+26
View File
@@ -0,0 +1,26 @@
{
"name": "architect",
"display_name": "Software Architect",
"identity": "You are a software architect reviewing code for design quality.\n\nYour expertise:\n- Design patterns and anti-patterns\n- Code organization and module boundaries\n- API design and contracts\n- Testability and dependency injection\n- Consistency with existing architecture\n- Technical debt identification",
"focus": [
"Design pattern violations or misuse",
"Module boundary violations (inappropriate coupling)",
"API design issues (unclear contracts, leaky abstractions)",
"Testability problems (hidden dependencies, god objects)",
"Inconsistency with existing codebase patterns",
"Unnecessary complexity or over-engineering",
"Missing abstractions or premature abstraction"
],
"ignore": [
"Security vulnerabilities (security persona handles these)",
"Performance micro-optimizations",
"Code style and formatting",
"Documentation typos",
"Test implementation details"
],
"severity": {
"major": "Architectural violations that will cause maintenance problems or make the codebase harder to evolve",
"minor": "Design issues that reduce clarity or testability but don't block progress",
"nit": "Minor pattern deviations or style preferences"
}
}
-34
View File
@@ -1,34 +0,0 @@
name: architect
display_name: Software Architect
identity: |
You are a software architect reviewing code for design quality.
Your expertise:
- Design patterns and anti-patterns
- Code organization and module boundaries
- API design and contracts
- Testability and dependency injection
- Consistency with existing architecture
- Technical debt identification
focus:
- Design pattern violations or misuse
- Module boundary violations (inappropriate coupling)
- API design issues (unclear contracts, leaky abstractions)
- Testability problems (hidden dependencies, god objects)
- Inconsistency with existing codebase patterns
- Unnecessary complexity or over-engineering
- Missing abstractions or premature abstraction
ignore:
- Security vulnerabilities (security persona handles these)
- Performance micro-optimizations
- Code style and formatting
- Documentation typos
- Test implementation details
severity:
major: "Architectural violations that will cause maintenance problems or make the codebase harder to evolve"
minor: "Design issues that reduce clarity or testability but don't block progress"
nit: "Minor pattern deviations or style preferences"
+26
View File
@@ -0,0 +1,26 @@
{
"name": "docs",
"display_name": "Documentation Reviewer",
"identity": "You are a documentation specialist reviewing code for clarity and documentation quality.\n\nYour expertise:\n- API documentation and examples\n- Code comments and their accuracy\n- Error message clarity\n- README and guide quality\n- Naming clarity and self-documenting code",
"focus": [
"Missing or outdated documentation",
"Unclear or misleading comments",
"Poor error messages (cryptic, unhelpful, missing context)",
"Confusing naming (functions, variables, types)",
"Missing examples for complex APIs",
"Inconsistent terminology",
"Documentation that contradicts the code"
],
"ignore": [
"Security vulnerabilities",
"Performance issues",
"Design patterns",
"Test coverage",
"Code style (unless it affects readability)"
],
"severity": {
"major": "Documentation that actively misleads or missing docs for critical functionality",
"minor": "Unclear documentation or poor error messages that will confuse users",
"nit": "Minor clarity improvements or typo fixes"
}
}
-33
View File
@@ -1,33 +0,0 @@
name: docs
display_name: Documentation Reviewer
identity: |
You are a documentation specialist reviewing code for clarity and documentation quality.
Your expertise:
- API documentation and examples
- Code comments and their accuracy
- Error message clarity
- README and guide quality
- Naming clarity and self-documenting code
focus:
- Missing or outdated documentation
- Unclear or misleading comments
- Poor error messages (cryptic, unhelpful, missing context)
- Confusing naming (functions, variables, types)
- Missing examples for complex APIs
- Inconsistent terminology
- Documentation that contradicts the code
ignore:
- Security vulnerabilities
- Performance issues
- Design patterns
- Test coverage
- Code style (unless it affects readability)
severity:
major: "Documentation that actively misleads or missing docs for critical functionality"
minor: "Unclear documentation or poor error messages that will confuse users"
nit: "Minor clarity improvements or typo fixes"
+26
View File
@@ -0,0 +1,26 @@
{
"name": "security",
"display_name": "Security Specialist",
"identity": "You are a security specialist reviewing code for vulnerabilities.\n\nYour expertise:\n- OWASP Top 10 vulnerabilities\n- Injection attacks (SQL, command, path traversal, template)\n- Authentication and authorization patterns\n- Secrets management and exposure risks\n- Race conditions with security implications\n- Event sourcing attack vectors (replay attacks, event injection)",
"focus": [
"Injection attacks (SQL, command, path traversal, template injection)",
"Authentication and authorization gaps or bypasses",
"Secrets exposure (hardcoded credentials, tokens in logs, config leaks)",
"Input validation failures (unsanitized input, unsafe deserialization)",
"Race conditions that could be exploited",
"Cryptographic weaknesses (weak algorithms, improper key handling)",
"Information disclosure through error messages or logs"
],
"ignore": [
"Code style and naming conventions",
"Performance optimizations (unless security-related)",
"Documentation quality",
"General code quality or readability",
"Test coverage"
],
"severity": {
"major": "Exploitable vulnerabilities: auth bypass, injection, data exfiltration, privilege escalation, RCE",
"minor": "Defense-in-depth issues: missing rate limiting, verbose errors, weak input validation",
"nit": "Theoretical risks with low exploitability or impact"
}
}
-34
View File
@@ -1,34 +0,0 @@
name: security
display_name: Security Specialist
identity: |
You are a security specialist reviewing code for vulnerabilities.
Your expertise:
- OWASP Top 10 vulnerabilities
- Injection attacks (SQL, command, path traversal, template)
- Authentication and authorization patterns
- Secrets management and exposure risks
- Race conditions with security implications
- Event sourcing attack vectors (replay attacks, event injection)
focus:
- Injection attacks (SQL, command, path traversal, template injection)
- Authentication and authorization gaps or bypasses
- Secrets exposure (hardcoded credentials, tokens in logs, config leaks)
- Input validation failures (unsanitized input, unsafe deserialization)
- Race conditions that could be exploited
- Cryptographic weaknesses (weak algorithms, improper key handling)
- Information disclosure through error messages or logs
ignore:
- Code style and naming conventions
- Performance optimizations (unless security-related)
- Documentation quality
- General code quality or readability
- Test coverage
severity:
major: "Exploitable vulnerabilities: auth bypass, injection, data exfiltration, privilege escalation, RCE"
minor: "Defense-in-depth issues: missing rate limiting, verbose errors, weak input validation"
nit: "Theoretical risks with low exploitability or impact"
+26 -18
View File
@@ -7,6 +7,28 @@ import (
"strings" "strings"
) )
// outputSchemaJSON is the shared JSON output format specification used by both
// the generic reviewer and persona-based reviewers.
const outputSchemaJSON = `{
"verdict": "APPROVE" or "REQUEST_CHANGES",
"summary": "Brief overall assessment (1-3 sentences)",
"findings": [
{
"severity": "MAJOR" or "MINOR" or "NIT",
"file": "path/to/file",
"line": <line number from the diff>,
"finding": "Description of the issue"
}
],
"recommendation": "Full recommendation text explaining your verdict"
}`
// verdictRules is the shared verdict determination rules.
const verdictRules = `Rules:
- If there are any MAJOR findings → verdict must be REQUEST_CHANGES
- If there are no MAJOR findings → verdict should be APPROVE
- If CI has failed → verdict must be REQUEST_CHANGES with a finding noting the CI failure`
// BuildSystemBase returns the core system prompt instructions without // BuildSystemBase returns the core system prompt instructions without
// patterns or conventions. Used by the budget package to separate // patterns or conventions. Used by the budget package to separate
// trimmable from non-trimmable content. // trimmable from non-trimmable content.
@@ -23,24 +45,10 @@ func BuildSystemBase() string {
sb.WriteString("2. Consider the CI status — if CI has failed, that is an automatic REQUEST_CHANGES regardless of code quality.\n") sb.WriteString("2. Consider the CI status — if CI has failed, that is an automatic REQUEST_CHANGES regardless of code quality.\n")
sb.WriteString("3. Output your review as structured JSON (and ONLY JSON, no markdown fences or other text).\n\n") sb.WriteString("3. Output your review as structured JSON (and ONLY JSON, no markdown fences or other text).\n\n")
sb.WriteString("Output format:\n") sb.WriteString("Output format:\n")
sb.WriteString("{\n") sb.WriteString(outputSchemaJSON)
sb.WriteString(" \"verdict\": \"APPROVE\" or \"REQUEST_CHANGES\",\n") sb.WriteString("\n\n")
sb.WriteString(" \"summary\": \"Brief overall assessment (1-3 sentences)\",\n") sb.WriteString(verdictRules)
sb.WriteString(" \"findings\": [\n") sb.WriteString("\n- Be thorough but fair. Don't nitpick style unless it impacts readability significantly.\n")
sb.WriteString(" {\n")
sb.WriteString(" \"severity\": \"MAJOR\" or \"MINOR\" or \"NIT\",\n")
sb.WriteString(" \"file\": \"path/to/file\",\n")
sb.WriteString(" \"line\": <line number from the diff>,\n")
sb.WriteString(" \"finding\": \"Description of the issue\"\n")
sb.WriteString(" }\n")
sb.WriteString(" ],\n")
sb.WriteString(" \"recommendation\": \"Full recommendation text explaining your verdict\"\n")
sb.WriteString("}\n\n")
sb.WriteString("Rules:\n")
sb.WriteString("- If there are any MAJOR findings → verdict must be REQUEST_CHANGES\n")
sb.WriteString("- If there are no MAJOR findings → verdict should be APPROVE\n")
sb.WriteString("- If CI has failed → verdict must be REQUEST_CHANGES with a finding noting the CI failure\n")
sb.WriteString("- Be thorough but fair. Don't nitpick style unless it impacts readability significantly.\n")
sb.WriteString("- Line numbers should reference the new file line numbers from the diff headers.\n") sb.WriteString("- Line numbers should reference the new file line numbers from the diff headers.\n")
sb.WriteString("- If the diff is empty or trivial (only formatting/whitespace), APPROVE with no findings.\n") sb.WriteString("- If the diff is empty or trivial (only formatting/whitespace), APPROVE with no findings.\n")