Repository navigation
Expand file tree
/
Copy patharchitect.go
More file actions
489 lines (428 loc) · 15.2 KB
/
Copy patharchitect.go
File metadata and controls
489 lines (428 loc) · 15.2 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package engine
import (
"context"
"fmt"
"strings"
"github.com/GrayCodeAI/reflect/internal/planning"
"github.com/GrayCodeAI/reflect/internal/provider/routing"
"github.com/GrayCodeAI/reflect/internal/types"
)
// ArchitectConfig configures the two-model architect/editor pipeline.
// A cheap model (architect) plans the changes, then an expensive model (editor)
// executes them precisely. This is a cost-saving pattern: the architect uses
// fewer tokens to plan, allowing the editor to focus on precise implementation.
type ArchitectConfig struct {
ArchitectModel string // cheap/fast model for planning, e.g., "haiku"
EditorModel string // expensive/precise model for edits, e.g., "sonnet"
PlanTokenBudget int // max tokens for architect's plan, default 4096
Enabled bool
// BeamSearch enables tree-search refinement of the plan (ToT/LATS) using
// the model as expander/scorer. Off by default: it costs extra model calls.
BeamSearch bool
}
// ArchitectPlan represents the structured output from the architect model.
type ArchitectPlan struct {
Goal string
Steps []PlanStep
FilesToModify []string
EstimatedComplexity string // "trivial", "simple", "moderate", "complex"
RawPlan string
}
// PlanStep is a single step in the architect's plan.
type PlanStep struct {
Description string
File string
Action string // "create", "modify", "delete"
Details string
}
// Message is a lightweight chat message used by the architect pipeline.
// This avoids coupling to external message types from relay.
type ArchitectMessage struct {
Role string // "system", "user", "assistant"
Content string
}
// Architect coordinates the two-model pipeline: a cheap model plans,
// then an expensive model executes each step.
type Architect struct {
Config ArchitectConfig
ChatFn func(ctx context.Context, model string, messages []ArchitectMessage) (string, error)
}
const architectSystemPrompt = `You are a software architect. Given a coding task, produce a precise implementation plan.
Output format:
GOAL: <one-line summary>
COMPLEXITY: <trivial|simple|moderate|complex>
FILES: <comma-separated list of files to modify/create>
STEPS:
1. [file.go] MODIFY: <what to change and why>
2. [new_file.go] CREATE: <what this file should contain>
...
Be specific about WHAT to change in each file. The editor will implement your plan exactly.`
// Plan sends the goal and repo context to the architect model and returns a structured plan.
func (a *Architect) Plan(ctx context.Context, goal string, repoContext string) (*ArchitectPlan, error) {
if a.ChatFn == nil {
return nil, fmt.Errorf("architect: ChatFn is not configured")
}
userContent := goal
if repoContext != "" {
userContent = fmt.Sprintf("Repository context:\n%s\n\nTask:\n%s", repoContext, goal)
}
messages := []ArchitectMessage{
{Role: "system", Content: architectSystemPrompt},
{Role: "user", Content: userContent},
}
model := a.Config.ArchitectModel
if model == "" {
provider := "anthropic"
if info, ok := routing.Find(a.Config.EditorModel); ok && info.Provider != "" {
provider = info.Provider
}
model = routing.PreferredModelForTier(provider, routing.TierHaiku, "")
}
response, err := a.ChatFn(ctx, model, messages)
if err != nil {
return nil, fmt.Errorf("architect: planning failed: %w", err)
}
plan, err := ParsePlan(response)
if err != nil {
return nil, fmt.Errorf("architect: failed to parse plan: %w", err)
}
// Optional tree-search refinement (ToT/LATS): explore plan candidates with
// the model as expander/scorer and keep the best reachable plan.
if a.Config.BeamSearch {
refined, rerr := planning.PlanWithBeamSearch(
&architectProvider{a: a},
model,
plan.RawPlan,
"Suggest the next implementation step for this plan, one per line.",
"Rate this plan step 0..1 for correctness and completeness.",
3, 4,
)
if rerr == nil && refined != "" && refined != plan.RawPlan {
if refinedPlan, perr := ParsePlan(refined); perr == nil {
refinedPlan.RawPlan = refined
plan = refinedPlan
}
}
}
plan.RawPlan = response
return plan, nil
}
// architectProvider adapts the Architect's ChatFn to the planning package's
// types.ChatProvider so BeamSearch can drive the live architect model.
type architectProvider struct {
a *Architect
}
func (p *architectProvider) Chat(ctx context.Context, messages []types.RelayMessage, opts types.ChatOptions) (*types.RelayResponse, error) {
archMsgs := make([]ArchitectMessage, len(messages))
for i, m := range messages {
archMsgs[i] = ArchitectMessage{Role: m.Role, Content: m.Content}
}
model := p.a.Config.ArchitectModel
if opts.Model != "" {
model = opts.Model
}
out, err := p.a.ChatFn(ctx, model, archMsgs)
if err != nil {
return nil, err
}
return &types.RelayResponse{Content: out}, nil
}
func (p *architectProvider) StreamChat(ctx context.Context, messages []types.RelayMessage, opts types.ChatOptions) (*types.StreamResult, error) {
return nil, nil
}
func (p *architectProvider) Ping(ctx context.Context) error { return nil }
func (p *architectProvider) Name() string { return "architect" }
// ParsePlan extracts GOAL, COMPLEXITY, FILES, and STEPS from the architect's response.
// It handles variations in formatting gracefully.
func ParsePlan(response string) (*ArchitectPlan, error) {
if strings.TrimSpace(response) == "" {
return nil, fmt.Errorf("empty plan response")
}
plan := &ArchitectPlan{
RawPlan: response,
}
lines := strings.Split(response, "\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
upper := strings.ToUpper(trimmed)
if strings.HasPrefix(upper, "GOAL:") {
plan.Goal = strings.TrimSpace(trimmed[len("GOAL:"):])
} else if strings.HasPrefix(upper, "COMPLEXITY:") {
complexity := strings.TrimSpace(trimmed[len("COMPLEXITY:"):])
complexity = strings.ToLower(complexity)
// Normalize to valid values
switch complexity {
case "trivial", "simple", "moderate", "complex":
plan.EstimatedComplexity = complexity
default:
// Try to match partial/variant forms
if strings.Contains(complexity, "trivial") {
plan.EstimatedComplexity = "trivial"
} else if strings.Contains(complexity, "simple") {
plan.EstimatedComplexity = "simple"
} else if strings.Contains(complexity, "complex") {
plan.EstimatedComplexity = "complex"
} else if strings.Contains(complexity, "moderate") || strings.Contains(complexity, "medium") {
plan.EstimatedComplexity = "moderate"
} else {
plan.EstimatedComplexity = "moderate" // default
}
}
} else if strings.HasPrefix(upper, "FILES:") {
filesStr := strings.TrimSpace(trimmed[len("FILES:"):])
if filesStr != "" {
parts := strings.Split(filesStr, ",")
for _, p := range parts {
f := strings.TrimSpace(p)
if f != "" {
plan.FilesToModify = append(plan.FilesToModify, f)
}
}
}
}
}
// Parse STEPS section
plan.Steps = parseSteps(lines)
// If no goal was explicitly found, use the first non-empty line as a fallback
if plan.Goal == "" && len(lines) > 0 {
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed != "" {
plan.Goal = trimmed
break
}
}
}
// If no complexity was set, default to moderate
if plan.EstimatedComplexity == "" {
plan.EstimatedComplexity = "moderate"
}
return plan, nil
}
// parseSteps extracts step entries from lines. It looks for numbered steps
// that follow patterns like:
//
// 1. [file.go] ACTION: description
// 2. [file.go] ACTION: description
//
// It also handles variants without brackets or with different numbering.
func parseSteps(lines []string) []PlanStep {
var steps []PlanStep
inSteps := false
for _, line := range lines {
trimmed := strings.TrimSpace(line)
upper := strings.ToUpper(trimmed)
// Detect start of STEPS section
if strings.HasPrefix(upper, "STEPS:") || strings.HasPrefix(upper, "STEPS") && strings.HasSuffix(upper, ":") {
inSteps = true
continue
}
if !inSteps {
continue
}
// Skip empty lines within steps
if trimmed == "" {
continue
}
// Try to parse a step line
step, ok := parseStepLine(trimmed)
if ok {
steps = append(steps, step)
}
}
return steps
}
// parseStepLine attempts to parse a single step line.
// Supported formats:
// - "1. [file.go] MODIFY: description"
// - "- [file.go] CREATE: description"
// - "1. file.go - MODIFY: description"
// - "1. MODIFY file.go: description"
func parseStepLine(line string) (PlanStep, bool) {
// Strip leading number/bullet: "1. ", "- ", "* "
stripped := line
for i, ch := range stripped {
if ch == '.' || ch == ')' {
stripped = strings.TrimSpace(stripped[i+1:])
break
}
if ch == '-' || ch == '*' {
if i == 0 {
stripped = strings.TrimSpace(stripped[1:])
break
}
}
if ch < '0' || ch > '9' {
break
}
}
step := PlanStep{}
// Try format: [file.go] ACTION: description
if strings.HasPrefix(stripped, "[") {
endBracket := strings.Index(stripped, "]")
if endBracket > 1 {
step.File = strings.TrimSpace(stripped[1:endBracket])
rest := strings.TrimSpace(stripped[endBracket+1:])
action, details := extractActionAndDetails(rest)
step.Action = action
if details != "" {
step.Details = details
step.Description = fmt.Sprintf("%s %s: %s", step.Action, step.File, details)
} else {
step.Description = fmt.Sprintf("%s %s", step.Action, step.File)
}
return step, true
}
}
// Try format: ACTION file.go: description or ACTION: file.go - description
upperStripped := strings.ToUpper(stripped)
for _, action := range []string{"CREATE", "MODIFY", "DELETE"} {
if strings.HasPrefix(upperStripped, action) {
rest := strings.TrimSpace(stripped[len(action):])
// Remove leading colon or dash
rest = strings.TrimLeft(rest, ":- ")
rest = strings.TrimSpace(rest)
// Next token is likely the filename
parts := strings.SplitN(rest, " ", 2)
if len(parts) >= 1 {
// Clean file name (strip trailing colon/dash)
step.File = strings.TrimRight(parts[0], ":-")
step.Action = strings.ToLower(action)
if len(parts) == 2 {
step.Details = strings.TrimLeft(parts[1], ":- ")
}
step.Description = fmt.Sprintf("%s %s", step.Action, step.File)
if step.Details != "" {
step.Description += ": " + step.Details
}
return step, true
}
}
}
// Fallback: treat the whole line as a step with unknown action
if stripped != "" {
step.Description = stripped
step.Action = "modify"
// Try to extract a filename (anything ending in common extensions)
words := strings.Fields(stripped)
for _, w := range words {
clean := strings.Trim(w, "[](),:")
if looksLikeFile(clean) {
step.File = clean
break
}
}
return step, true
}
return PlanStep{}, false
}
// looksLikeFile returns true if the string looks like a filename.
func looksLikeFile(s string) bool {
extensions := []string{
".go", ".js", ".ts", ".py", ".rs", ".java", ".c", ".h",
".cpp", ".rb", ".md", ".yaml", ".yml", ".json", ".toml", ".sql", ".sh",
}
lower := strings.ToLower(s)
for _, ext := range extensions {
if strings.HasSuffix(lower, ext) {
return true
}
}
return false
}
// extractActionAndDetails splits "ACTION: details" into the action and details.
func extractActionAndDetails(s string) (string, string) {
s = strings.TrimSpace(s)
// Check for known actions
upper := strings.ToUpper(s)
for _, action := range []string{"CREATE", "MODIFY", "DELETE"} {
if strings.HasPrefix(upper, action) {
rest := strings.TrimSpace(s[len(action):])
rest = strings.TrimLeft(rest, ":- ")
return strings.ToLower(action), strings.TrimSpace(rest)
}
}
// No recognized action; default to "modify"
return "modify", s
}
// ShouldUseArchitect applies heuristics to decide whether the architect/editor
// pipeline should be used for this request. It returns true for complex tasks
// that benefit from planning.
func ShouldUseArchitect(prompt string, messageCount int) bool {
lower := strings.ToLower(prompt)
// Don't use for explicit speed requests
if strings.Contains(lower, "quick") || strings.Contains(lower, "fast") ||
strings.Contains(lower, "just do it") || strings.Contains(lower, "hurry") {
return false
}
// Don't use for simple questions
if strings.HasPrefix(lower, "what ") || strings.HasPrefix(lower, "how ") ||
strings.HasPrefix(lower, "why ") || strings.HasPrefix(lower, "explain ") {
words := strings.Fields(prompt)
if len(words) <= 15 {
return false
}
}
// Use for multi-file references
if strings.Contains(lower, "files") || strings.Contains(lower, "across") ||
strings.Contains(lower, "multiple") || strings.Contains(lower, "all of") {
return true
}
// Use for complex task indicators
if strings.Contains(lower, "refactor") || strings.Contains(lower, "redesign") ||
strings.Contains(lower, "implement") || strings.Contains(lower, "architecture") ||
strings.Contains(lower, "migrate") || strings.Contains(lower, "restructure") {
return true
}
// Word count heuristic: complex prompts tend to be longer
words := strings.Fields(prompt)
if len(words) > 50 {
return true
}
// Early in conversation and long prompt suggests initial complex task
if messageCount <= 2 && len(words) > 30 {
return true
}
return false
}
// BuildEditorPrompt formats a focused prompt for the editor model to implement
// a specific step from the architect's plan.
func BuildEditorPrompt(plan *ArchitectPlan, step PlanStep) string {
var b strings.Builder
b.WriteString("Implement this specific step from the plan:\n\n")
b.WriteString(fmt.Sprintf("Overall goal: %s\n\n", plan.Goal))
b.WriteString(fmt.Sprintf("Your task: %s\n", step.Description))
b.WriteString(fmt.Sprintf("File: %s\n", step.File))
b.WriteString(fmt.Sprintf("Action: %s\n", step.Action))
if step.Details != "" {
b.WriteString(fmt.Sprintf("Details: %s\n", step.Details))
}
b.WriteString("\nImplement this precisely. Do not deviate from the plan.")
return b.String()
}
// EstimateSavings calculates the estimated cost savings of using the architect/editor
// pipeline versus using the expensive model for everything. The architectCost and
// editorCost are per-million-token input prices.
func EstimateSavings(plan *ArchitectPlan, architectCost, editorCost float64) string {
if plan == nil || len(plan.Steps) == 0 {
return "No savings estimate available (empty plan)."
}
// Estimate tokens: architect plan ~1000 tokens, each editor step ~2000 tokens
planTokens := 1000.0
editorTokensPerStep := 2000.0
totalEditorTokens := float64(len(plan.Steps)) * editorTokensPerStep
// Cost with architect pipeline
architectPipelineCost := (planTokens * architectCost / 1_000_000) +
(totalEditorTokens * editorCost / 1_000_000)
// Cost without pipeline (all tokens at editor price)
totalTokensWithout := planTokens + totalEditorTokens
withoutPipelineCost := totalTokensWithout * editorCost / 1_000_000
savings := withoutPipelineCost - architectPipelineCost
if savings <= 0 {
return fmt.Sprintf("No cost savings (architect pipeline costs the same or more). Pipeline: $%.6f, Direct: $%.6f",
architectPipelineCost, withoutPipelineCost)
}
percentSaved := (savings / withoutPipelineCost) * 100
return fmt.Sprintf("Estimated savings: $%.6f (%.1f%% reduction). Pipeline: $%.6f vs Direct: $%.6f",
savings, percentSaved, architectPipelineCost, withoutPipelineCost)
}