|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// maxPromptArgLen is a conservative limit for passing prompts as |
| 15 | +// CLI arguments. macOS ARG_MAX is ~1 MB; we leave headroom for |
| 16 | +// the command name, flags, and environment. |
| 17 | +const maxPromptArgLen = 512 * 1024 |
| 18 | + |
| 19 | +// stripKiroOutput removes Kiro's UI chrome (logo, tip box, model line, timing footer) |
| 20 | +// and terminal control sequences, returning only the review text. |
| 21 | +func stripKiroOutput(raw string) string { |
| 22 | + text, _ := stripKiroReview(raw) |
| 23 | + return text |
| 24 | +} |
| 25 | + |
| 26 | +// stripKiroReview strips Kiro chrome and returns the cleaned text |
| 27 | +// plus a bool indicating whether a "> " review marker was found. |
| 28 | +// When no marker is found the full ANSI-stripped text is returned |
| 29 | +// (hasMarker == false), which may be non-review noise. |
| 30 | +func stripKiroReview(raw string) (string, bool) { |
| 31 | + s := stripTerminalControls(raw) |
| 32 | + |
| 33 | + // Kiro prepends a splash screen and tip box before the response. |
| 34 | + // The "> " prompt marker appears near the top; limit the search |
| 35 | + // to avoid mistaking markdown blockquotes for the start marker. |
| 36 | + lines := strings.Split(s, "\n") |
| 37 | + limit := min(30, len(lines)) |
| 38 | + start := -1 |
| 39 | + for i, line := range lines[:limit] { |
| 40 | + if strings.HasPrefix(line, "> ") || line == ">" { |
| 41 | + start = i |
| 42 | + break |
| 43 | + } |
| 44 | + } |
| 45 | + if start == -1 { |
| 46 | + return strings.TrimSpace(s), false |
| 47 | + } |
| 48 | + |
| 49 | + // Strip the prompt marker from the first content line. |
| 50 | + // A bare ">" (no trailing content) is skipped entirely. |
| 51 | + if lines[start] == ">" { |
| 52 | + start++ |
| 53 | + if start >= len(lines) { |
| 54 | + return "", true |
| 55 | + } |
| 56 | + } else { |
| 57 | + lines[start] = strings.TrimPrefix(lines[start], "> ") |
| 58 | + } |
| 59 | + |
| 60 | + // Drop the timing footer ("▸ Time: Xs") and anything after it. |
| 61 | + // Trim trailing blank lines first so they don't push the real |
| 62 | + // footer outside the scan window, then scan the last 5 non-blank |
| 63 | + // lines to avoid truncating review content that happens to |
| 64 | + // contain "▸ Time:" in a code snippet. |
| 65 | + end := len(lines) |
| 66 | + for end > start && strings.TrimSpace(lines[end-1]) == "" { |
| 67 | + end-- |
| 68 | + } |
| 69 | + scanFrom := max(start, end-5) |
| 70 | + for i := scanFrom; i < end; i++ { |
| 71 | + if strings.HasPrefix(strings.TrimSpace(lines[i]), "▸ Time:") { |
| 72 | + end = i |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return strings.TrimSpace(strings.Join(lines[start:end], "\n")), true |
| 78 | +} |
| 79 | + |
| 80 | +// KiroAgent runs code reviews using the Kiro CLI (kiro-cli) |
| 81 | +type KiroAgent struct { |
| 82 | + Command string // The kiro-cli command to run (default: "kiro-cli") |
| 83 | + Reasoning ReasoningLevel // Reasoning level (stored; kiro-cli has no reasoning flag) |
| 84 | + Agentic bool // Whether agentic mode is enabled (uses --trust-all-tools) |
| 85 | +} |
| 86 | + |
| 87 | +// NewKiroAgent creates a new Kiro agent with standard reasoning |
| 88 | +func NewKiroAgent(command string) *KiroAgent { |
| 89 | + if command == "" { |
| 90 | + command = "kiro-cli" |
| 91 | + } |
| 92 | + return &KiroAgent{Command: command, Reasoning: ReasoningStandard} |
| 93 | +} |
| 94 | + |
| 95 | +// WithReasoning returns a copy with the reasoning level stored. |
| 96 | +// kiro-cli has no reasoning flag; callers can map reasoning to agent selection instead. |
| 97 | +func (a *KiroAgent) WithReasoning(level ReasoningLevel) Agent { |
| 98 | + return &KiroAgent{Command: a.Command, Reasoning: level, Agentic: a.Agentic} |
| 99 | +} |
| 100 | + |
| 101 | +// WithAgentic returns a copy of the agent configured for agentic mode. |
| 102 | +// In agentic mode, --trust-all-tools is passed so kiro can use tools without confirmation. |
| 103 | +func (a *KiroAgent) WithAgentic(agentic bool) Agent { |
| 104 | + return &KiroAgent{Command: a.Command, Reasoning: a.Reasoning, Agentic: agentic} |
| 105 | +} |
| 106 | + |
| 107 | +// WithModel returns the agent unchanged; kiro-cli does not expose a --model CLI flag. |
| 108 | +func (a *KiroAgent) WithModel(model string) Agent { |
| 109 | + return a |
| 110 | +} |
| 111 | + |
| 112 | +func (a *KiroAgent) Name() string { |
| 113 | + return "kiro" |
| 114 | +} |
| 115 | + |
| 116 | +func (a *KiroAgent) CommandName() string { |
| 117 | + return a.Command |
| 118 | +} |
| 119 | + |
| 120 | +func (a *KiroAgent) buildArgs(agenticMode bool) []string { |
| 121 | + args := []string{"chat", "--no-interactive"} |
| 122 | + if agenticMode { |
| 123 | + args = append(args, "--trust-all-tools") |
| 124 | + } |
| 125 | + return args |
| 126 | +} |
| 127 | + |
| 128 | +func (a *KiroAgent) CommandLine() string { |
| 129 | + agenticMode := a.Agentic || AllowUnsafeAgents() |
| 130 | + args := a.buildArgs(agenticMode) |
| 131 | + return a.Command + " " + strings.Join(args, " ") + " -- <prompt>" |
| 132 | +} |
| 133 | + |
| 134 | +func (a *KiroAgent) Review(ctx context.Context, repoPath, commitSHA, prompt string, output io.Writer) (string, error) { |
| 135 | + if len(prompt) > maxPromptArgLen { |
| 136 | + return "", fmt.Errorf( |
| 137 | + "prompt too large for kiro-cli argv (%d bytes, max %d)", |
| 138 | + len(prompt), maxPromptArgLen, |
| 139 | + ) |
| 140 | + } |
| 141 | + |
| 142 | + agenticMode := a.Agentic || AllowUnsafeAgents() |
| 143 | + |
| 144 | + // kiro-cli chat --no-interactive [--trust-all-tools] <prompt> |
| 145 | + // The prompt is passed as a positional argument |
| 146 | + // (kiro-cli does not support stdin). |
| 147 | + args := a.buildArgs(agenticMode) |
| 148 | + args = append(args, "--", prompt) |
| 149 | + |
| 150 | + cmd := exec.CommandContext(ctx, a.Command, args...) |
| 151 | + cmd.Dir = repoPath |
| 152 | + cmd.Env = os.Environ() |
| 153 | + cmd.WaitDelay = 5 * time.Second |
| 154 | + |
| 155 | + // kiro-cli emits ANSI terminal escape codes that are not |
| 156 | + // suitable for streaming. Capture and return stripped text. |
| 157 | + var stdout, stderr bytes.Buffer |
| 158 | + cmd.Stdout = &stdout |
| 159 | + cmd.Stderr = &stderr |
| 160 | + |
| 161 | + if err := cmd.Run(); err != nil { |
| 162 | + return "", fmt.Errorf( |
| 163 | + "kiro failed: %w\nstderr: %s", |
| 164 | + err, stderr.String(), |
| 165 | + ) |
| 166 | + } |
| 167 | + |
| 168 | + // Prefer the stream that contains a "> " review marker. |
| 169 | + // - stdout with marker and content → use stdout |
| 170 | + // - stdout empty or marker-only → try stderr |
| 171 | + // - stdout has content but no marker → use stderr only |
| 172 | + // if stderr has a marker (otherwise keep stdout) |
| 173 | + result, stdoutMarker := stripKiroReview(stdout.String()) |
| 174 | + if !stdoutMarker || len(result) == 0 { |
| 175 | + alt, stderrMarker := stripKiroReview(stderr.String()) |
| 176 | + if len(alt) > 0 && (len(result) == 0 || stderrMarker) { |
| 177 | + result = alt |
| 178 | + } |
| 179 | + } |
| 180 | + if len(result) == 0 { |
| 181 | + return "No review output generated", nil |
| 182 | + } |
| 183 | + if sw := newSyncWriter(output); sw != nil { |
| 184 | + _, _ = sw.Write([]byte(result + "\n")) |
| 185 | + } |
| 186 | + return result, nil |
| 187 | +} |
| 188 | + |
| 189 | +func init() { |
| 190 | + Register(NewKiroAgent("")) |
| 191 | +} |
0 commit comments