Skip to content

Commit 131aa51

Browse files
committed
refactor(agentenv): improve analysis workflow and error handling
- Simplify logging and remove verbose output - Enhance error handling in analysis process - Update file references and command arguments - Optimize marker extraction in analysis output
1 parent cc9541b commit 131aa51

File tree

4 files changed

+17
-61
lines changed

4 files changed

+17
-61
lines changed

.gitignore

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,3 @@ dist/
1010

1111
# artifacts
1212
build/
13-
14-
# agentenv
15-
.agentenv/kwb/
16-
.claude/
17-
.mcp.json
18-
CLAUDE.md
19-
.gemini/
20-
GEMINI.md
21-
.crush/
22-
.crush.json
23-
CRUSH.md
24-
.github/copilot-instructions.md
25-
.github/.copilot.mcp.json

pkg/agentenv/agentenv.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ func (s *Service) Analyse(ctx context.Context) error {
7171
return fmt.Errorf("analysis provider is not set")
7272
}
7373

74-
s.logger.Info("Analysing project", "provider", s.settings.AnalysisProvider)
75-
7674
targetDir := filepath.Join(s.settings.OutputPath, agentEnvDir)
7775
analyser := newAnalyser(s.logger, targetDir)
7876

pkg/agentenv/analyser.go

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ const (
2626
modelGemini = "gemini-2.5-pro"
2727

2828
providerCodex = "codex"
29-
modelCodex = "gpt-5-nano"
29+
modelCodex = "gpt-5"
3030
)
3131

32-
//go:embed analyse.md
32+
//go:embed analyser.md
3333
var analysePrompt string
3434

3535
type analyser struct {
@@ -49,14 +49,6 @@ func (a *analyser) Run(ctx context.Context, provider string, model string) error
4949
return fmt.Errorf("provider tool '%s' not found in PATH", provider)
5050
}
5151

52-
outputFile := filepath.Join(a.outputDir, analysisFileName)
53-
stdoutLogFile := filepath.Join(a.outputDir, "analyse.stdout.log")
54-
stderrLogFile := filepath.Join(a.outputDir, "analyse.stderr.log")
55-
56-
if err := os.MkdirAll(filepath.Dir(a.outputDir), 0755); err != nil {
57-
return fmt.Errorf("failed to create output directory: %w", err)
58-
}
59-
6052
var cmd *exec.Cmd
6153

6254
switch provider {
@@ -83,7 +75,11 @@ func (a *analyser) Run(ctx context.Context, provider string, model string) error
8375
return fmt.Errorf("failed to build command for provider: %s", provider)
8476
}
8577

86-
a.logger.Info("Running analysis")
78+
a.logger.Info("Running analysis",
79+
"provider", provider,
80+
"model", model,
81+
"dir", a.outputDir,
82+
)
8783

8884
var stdout, stderr bytes.Buffer
8985
cmd.Stdout = &stdout
@@ -93,44 +89,17 @@ func (a *analyser) Run(ctx context.Context, provider string, model string) error
9389
output := stdout.String()
9490
errorOutput := stderr.String()
9591

96-
// Save stdout log
97-
if err := os.WriteFile(stdoutLogFile, stdout.Bytes(), 0644); err != nil {
98-
a.logger.Warn("Failed to save stdout log", "error", err)
99-
} else {
100-
a.logger.Info("Stdout log saved", "file", stdoutLogFile)
101-
}
102-
103-
// Save stderr log
104-
if err := os.WriteFile(stderrLogFile, stderr.Bytes(), 0644); err != nil {
105-
a.logger.Warn("Failed to save stderr log", "error", err)
106-
} else {
107-
a.logger.Info("Stderr log saved", "file", stderrLogFile)
108-
}
109-
11092
if err != nil {
11193
a.logger.Error("Analysis command failed", "error", err, "stderr", errorOutput)
112-
errorDetails := fmt.Sprintf(
113-
"# Analysis Failed\n\n## Command Error\n%v\n\n## Standard Error Output\n%s\n\n## Standard Output\n%s\n",
114-
err,
115-
errorOutput,
116-
output,
117-
)
118-
if output == "" {
119-
output = errorDetails
120-
} else {
121-
output = errorDetails + "\n\n## Original Output\n" + output
122-
}
123-
}
124-
125-
if output == "" {
126-
output = fmt.Sprintf(
127-
"# Analysis Failed\n\nNo output generated from the analysis command.\nCommand: %s\nError: %v\n",
128-
cmd.String(),
129-
err,
130-
)
94+
return err
13195
}
13296

13397
output = a.extractAnalysis(output)
98+
outputFile := filepath.Join(a.outputDir, analysisFileName)
99+
100+
if err := os.MkdirAll(a.outputDir, 0755); err != nil {
101+
return fmt.Errorf("failed to create output directory: %w", err)
102+
}
134103

135104
if err := os.WriteFile(outputFile, []byte(output), 0644); err != nil {
136105
return fmt.Errorf("failed to write analysis: %w", err)
@@ -144,6 +113,7 @@ func (a *analyser) Run(ctx context.Context, provider string, model string) error
144113
func (a *analyser) buildClaudeCommand(ctx context.Context, model string, prompt string) *exec.Cmd {
145114
args := []string{
146115
"--model", model,
116+
"-p",
147117
prompt,
148118
}
149119
return exec.CommandContext(ctx, "claude", args...)
@@ -159,6 +129,7 @@ func (a *analyser) buildGeminiCommand(ctx context.Context, model string, prompt
159129

160130
func (a *analyser) buildCodexCommand(ctx context.Context, model string, prompt string) *exec.Cmd {
161131
args := []string{
132+
"exec",
162133
"--model", model,
163134
"--full-auto",
164135
prompt,
@@ -173,12 +144,12 @@ func (a *analyser) checkToolAvailable(tool string) bool {
173144

174145
// extractAnalysis extract text between specific markers from the analysis file.
175146
func (a *analyser) extractAnalysis(input string) string {
176-
beginIdx := bytes.Index([]byte(input), []byte(beginMarker))
147+
beginIdx := bytes.LastIndex([]byte(input), []byte(beginMarker))
177148
if beginIdx == -1 {
178149
a.logger.Warn("Begin marker not found in analysis output")
179150
return input
180151
}
181-
endIdx := bytes.Index([]byte(input), []byte(endMarker))
152+
endIdx := bytes.LastIndex([]byte(input), []byte(endMarker))
182153
if endIdx == -1 || endIdx <= beginIdx {
183154
a.logger.Warn("End marker not found or invalid in analysis output")
184155
return input
File renamed without changes.

0 commit comments

Comments
 (0)