Skip to content

Commit 5be27cb

Browse files
feat(#132): Add attempts flag for AI refactoring attempts configuration (#134)
1 parent ccbdb53 commit 5be27cb

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewRootCmd(out, _ io.Writer) *cobra.Command {
3838
root.PersistentFlags().StringVar(&params.Soutput, "stats-output", "stats", "Output path for statistics")
3939
root.PersistentFlags().BoolVar(&params.Colorless, "no-colors", false, "Disable colored output")
4040
root.PersistentFlags().StringVarP(&params.Model, "model", "m", "", "Model to use (if supported by the AI provider)")
41+
root.PersistentFlags().IntVar(&params.Attempts, "attempts", 3, "How many attempts the AI has to make a valid refactoring")
4142
root.AddCommand(
4243
newRefactorCmd(&params),
4344
newStartCmd(),

internal/client/params.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Params struct {
1919
Checks []string
2020
Colorless bool
2121
Model string
22+
Attempts int
2223
}
2324

2425
// NewMockParams creates a new Params object with mock settings.
@@ -39,5 +40,6 @@ func NewMockParams() *Params {
3940
Checks: []string{"mvn clean test"},
4041
Colorless: false,
4142
Model: "gpt-3.5-turbo",
43+
Attempts: 3,
4244
}
4345
}

internal/client/refrax_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (c *RefraxClient) Refactor(proj domain.Project) (domain.Project, error) {
160160
if err != nil {
161161
return nil, fmt.Errorf("failed to find free port for facilitator: %w", err)
162162
}
163-
fclttor := facilitator.NewFacilitator(facilitatorBrain, ctc, fxr, rvwr, facilitatorPort, c.params.Colorless)
163+
fclttor := facilitator.NewFacilitator(facilitatorBrain, ctc, fxr, rvwr, facilitatorPort, c.params.Colorless, c.params.Attempts)
164164
fclttor.Handler(countStats(facilitatorStats))
165165

166166
go func() {

internal/facilitator/agent.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type agent struct {
2121
fixer domain.Fixer
2222
reviewer domain.Reviewer
2323
frounds int
24+
attempts int
2425
}
2526

2627
type fix struct {
@@ -45,7 +46,14 @@ func (a *agent) Refactor(job *domain.Job) (*domain.Artifacts, error) {
4546
}
4647
diff := 0
4748
result := make([]domain.Class, 0)
48-
for diff < size {
49+
attempts := a.attempts
50+
if attempts <= 0 {
51+
a.log.Info("Number of attempts less or equal zero (%d), skipping refactoring", attempts)
52+
} else {
53+
a.log.Info("Starting refactoring with max-size=%d and attempts=%d", size, attempts)
54+
}
55+
for diff < size && attempts > 0 {
56+
a.log.Info("Refactoring attempt %d/%d, current diff %d/%d", a.attempts-attempts+1, a.attempts, diff, size)
4957
c, err := a.criticizeAll(job.Classes, size)
5058
if err != nil {
5159
return nil, fmt.Errorf("failed to criticize classes: %w", err)
@@ -81,6 +89,7 @@ func (a *agent) Refactor(job *domain.Job) (*domain.Artifacts, error) {
8189
if err != nil {
8290
return nil, fmt.Errorf("failed to stabilize refactored classes: %w", err)
8391
}
92+
attempts--
8493
}
8594
res := &domain.Artifacts{
8695
Descr: &domain.Description{Text: "refactored classes"},

internal/facilitator/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type A2AFacilitator struct {
2020
}
2121

2222
// NewFacilitator creates a new instance of Facilitator to manage communication between agents.
23-
func NewFacilitator(ai brain.Brain, critic domain.Critic, fixer domain.Fixer, reviewer domain.Reviewer, port int, colorless bool) *A2AFacilitator {
23+
func NewFacilitator(ai brain.Brain, critic domain.Critic, fixer domain.Fixer, reviewer domain.Reviewer, port int, colorless bool, attempts int) *A2AFacilitator {
2424
logger := log.New("facilitator", log.Yellow, colorless)
2525
logger.Debug("preparing server on port %d with ai provider %s", port, ai)
2626
server := protocol.NewServer(agentCard(port), port)
@@ -35,6 +35,7 @@ func NewFacilitator(ai brain.Brain, critic domain.Critic, fixer domain.Fixer, re
3535
fixer: fixer,
3636
reviewer: reviewer,
3737
frounds: 3,
38+
attempts: attempts,
3839
},
3940
}
4041
server.MsgHandler(facilitator.think)

0 commit comments

Comments
 (0)