summaryrefslogtreecommitdiff
path: root/internal/api/elaborate.go
blob: e8930a66594dfcb6fb49c3f891316f323f998c6c (plain)
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
package api

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"os/exec"
	"strings"
	"time"
)

const elaborateTimeout = 30 * time.Second

// claudeJSONResult is the top-level object returned by `claude --output-format json`.
type claudeJSONResult struct {
	Result  string `json:"result"`
	IsError bool   `json:"is_error"`
}

// geminiJSONResult is the top-level object returned by `gemini --output-format json`.
type geminiJSONResult struct {
	Response string `json:"response"`
}

// extractJSON returns the first top-level JSON object found in s, stripping
// surrounding prose or markdown code fences the model may have added.
func extractJSON(s string) string {
	start := strings.Index(s, "{")
	end := strings.LastIndex(s, "}")
	if start == -1 || end == -1 || end < start {
		return s
	}
	return s[start : end+1]
}

func (s *Server) claudeBinaryPath() string {
	if s.elaborateCmdPath != "" {
		return s.elaborateCmdPath
	}
	if s.claudeBinPath != "" {
		return s.claudeBinPath
	}
	return "claude"
}

func (s *Server) geminiBinaryPath() string {
	if s.geminiBinPath != "" {
		return s.geminiBinPath
	}
	return "gemini"
}

// elaboratedStorySubtask is a leaf unit within a story task.
type elaboratedStorySubtask struct {
	Name         string `json:"name"`
	Instructions string `json:"instructions"`
}

// elaboratedStoryTask is one independently-deployable unit in a story plan.
type elaboratedStoryTask struct {
	Name               string                   `json:"name"`
	Instructions       string                   `json:"instructions"`
	AcceptanceCriteria string                   `json:"acceptance_criteria"`
	Subtasks           []elaboratedStorySubtask `json:"subtasks"`
}

// elaboratedStoryValidation describes how to verify the story was successful.
type elaboratedStoryValidation struct {
	Type            string   `json:"type"`
	Steps           []string `json:"steps"`
	SuccessCriteria string   `json:"success_criteria"`
}

// elaboratedStory is the full implementation plan produced by story elaboration.
type elaboratedStory struct {
	Name       string                    `json:"name"`
	BranchName string                    `json:"branch_name"`
	Tasks      []elaboratedStoryTask     `json:"tasks"`
	Validation elaboratedStoryValidation `json:"validation"`
}

func buildStoryElaboratePrompt() string {
	return `You are a software architect. Given a goal, analyze the codebase at /workspace and produce a structured implementation plan as JSON.

Output ONLY valid JSON matching this schema:
{
  "name": "story name",
  "branch_name": "story/kebab-case-name",
  "tasks": [
    {
      "name": "task name",
      "instructions": "detailed instructions including file paths and what to change",
      "acceptance_criteria": "specific, verifiable conditions a separate reviewer can check — e.g. 'run go test ./... and verify all pass; confirm GET /api/foo returns 200 with expected JSON shape'",
      "subtasks": [
        { "name": "subtask name", "instructions": "..." }
      ]
    }
  ],
  "validation": {
    "type": "build|test|smoke",
    "steps": ["step1", "step2"],
    "success_criteria": "what success looks like"
  }
}

Rules:
- Tasks must be independently buildable (each can be deployed alone)
- Subtasks within a task are order-dependent and run sequentially
- Instructions must include specific file paths, function names, and exact changes
- Instructions must end with: git add -A && git commit -m "..." && git push origin <branch>
- Validation should match the scope: small change = build check; new feature = smoke test
- acceptance_criteria must be concrete and verifiable by a separate agent — no vague assertions like "code looks good"`
}

func (s *Server) elaborateStoryWithClaude(ctx context.Context, workDir, goal string) (*elaboratedStory, error) {
	cmd := exec.CommandContext(ctx, s.claudeBinaryPath(),
		"-p", goal,
		"--system-prompt", buildStoryElaboratePrompt(),
		"--output-format", "json",
		"--model", "haiku",
	)
	if workDir != "" {
		cmd.Dir = workDir
	}

	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	err := cmd.Run()

	output := stdout.Bytes()
	if len(output) == 0 {
		if err != nil {
			return nil, fmt.Errorf("claude failed: %w (stderr: %s)", err, stderr.String())
		}
		return nil, fmt.Errorf("claude returned no output")
	}

	var wrapper claudeJSONResult
	if jerr := json.Unmarshal(output, &wrapper); jerr != nil {
		return nil, fmt.Errorf("failed to parse claude JSON wrapper: %w (output: %s)", jerr, string(output))
	}
	if wrapper.IsError {
		return nil, fmt.Errorf("claude error: %s", wrapper.Result)
	}

	var result elaboratedStory
	if jerr := json.Unmarshal([]byte(extractJSON(wrapper.Result)), &result); jerr != nil {
		return nil, fmt.Errorf("failed to parse elaborated story JSON: %w (result: %s)", jerr, wrapper.Result)
	}
	return &result, nil
}

func (s *Server) elaborateStoryWithGemini(ctx context.Context, workDir, goal string) (*elaboratedStory, error) {
	combinedPrompt := fmt.Sprintf("%s\n\n%s", buildStoryElaboratePrompt(), goal)
	cmd := exec.CommandContext(ctx, s.geminiBinaryPath(),
		"-p", combinedPrompt,
		"--output-format", "json",
		"--model", "gemini-2.5-flash-lite",
	)
	if workDir != "" {
		cmd.Dir = workDir
	}

	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	if err := cmd.Run(); err != nil {
		return nil, fmt.Errorf("gemini failed: %w (stderr: %s)", err, stderr.String())
	}

	var wrapper geminiJSONResult
	if err := json.Unmarshal(stdout.Bytes(), &wrapper); err != nil {
		return nil, fmt.Errorf("failed to parse gemini JSON wrapper: %w (output: %s)", err, stdout.String())
	}

	var result elaboratedStory
	if err := json.Unmarshal([]byte(extractJSON(wrapper.Response)), &result); err != nil {
		return nil, fmt.Errorf("failed to parse elaborated story JSON: %w (response: %s)", err, wrapper.Response)
	}
	return &result, nil
}

func (s *Server) handleElaborateStory(w http.ResponseWriter, r *http.Request) {
	var input struct {
		Goal      string `json:"goal"`
		ProjectID string `json:"project_id"`
	}
	if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()})
		return
	}
	if input.Goal == "" {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "goal is required"})
		return
	}
	if input.ProjectID == "" {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "project_id is required"})
		return
	}

	proj, err := s.store.GetProject(input.ProjectID)
	if err != nil {
		writeJSON(w, http.StatusNotFound, map[string]string{"error": "project not found"})
		return
	}

	// Update git refs without modifying the working tree.
	if proj.LocalPath != "" {
		gitCmd := exec.Command("git", "-C", proj.LocalPath, "fetch", "origin")
		if err := gitCmd.Run(); err != nil {
			s.logger.Warn("story elaborate: git fetch failed", "error", err, "path", proj.LocalPath)
		}
	}

	ctx, cancel := context.WithTimeout(r.Context(), elaborateTimeout)
	defer cancel()

	result, err := s.elaborateStoryWithClaude(ctx, proj.LocalPath, input.Goal)
	if err != nil {
		s.logger.Warn("story elaborate: claude failed, falling back to gemini", "error", err)
		result, err = s.elaborateStoryWithGemini(ctx, proj.LocalPath, input.Goal)
		if err != nil {
			s.logger.Error("story elaborate: fallback gemini also failed", "error", err)
			writeJSON(w, http.StatusBadGateway, map[string]string{
				"error": fmt.Sprintf("elaboration failed: %v", err),
			})
			return
		}
	}

	if result.Name == "" {
		writeJSON(w, http.StatusBadGateway, map[string]string{
			"error": "elaboration failed: missing required fields in response",
		})
		return
	}

	writeJSON(w, http.StatusOK, result)
}