Skip to content

Commit f8d707e

Browse files
authored
Merge pull request #1731 from dgageot/daily-fixes
Daily fixes
2 parents 8d89956 + 62bafde commit f8d707e

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

cmd/root/root.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,26 @@ func (e RuntimeError) Unwrap() error {
204204
return e.Err
205205
}
206206

207-
// isFirstRun checks if this is the first time cagent is being run
208-
// It creates a marker file in the user's config directory
207+
// isFirstRun checks if this is the first time cagent is being run.
208+
// It atomically creates a marker file in the user's config directory
209+
// using os.O_EXCL to avoid a race condition when multiple processes
210+
// start concurrently.
209211
func isFirstRun() bool {
210212
configDir := paths.GetConfigDir()
211213
markerFile := filepath.Join(configDir, ".cagent_first_run")
212214

213-
// Check if marker file exists
214-
if _, err := os.Stat(markerFile); err == nil {
215-
return false // File exists, not first run
216-
}
217-
218-
// Create marker file to indicate this run has happened
215+
// Ensure the config directory exists before trying to create the marker file
219216
if err := os.MkdirAll(configDir, 0o755); err != nil {
220-
return false // Can't create config dir, assume not first run
217+
slog.Warn("Failed to create config directory for first run marker", "error", err)
218+
return false
221219
}
222220

223-
if err := os.WriteFile(markerFile, []byte(""), 0o644); err != nil {
224-
return false // Can't create marker file, assume not first run
221+
// Atomically create the marker file. If it already exists, OpenFile returns an error.
222+
f, err := os.OpenFile(markerFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o644)
223+
if err != nil {
224+
return false // File already exists or other error, not first run
225225
}
226+
f.Close()
226227

227-
return true // Successfully created marker, this is first run
228+
return true
228229
}

pkg/app/app.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,18 @@ func (a *App) RunWithMessage(ctx context.Context, cancel context.CancelFunc, msg
446446
}
447447

448448
func (a *App) RunBangCommand(ctx context.Context, command string) {
449-
out, _ := exec.CommandContext(ctx, "/bin/sh", "-c", command).CombinedOutput()
450-
a.events <- runtime.ShellOutput("$ " + command + "\n" + string(out))
449+
command = strings.TrimSpace(command)
450+
if command == "" {
451+
a.events <- runtime.ShellOutput("Error: empty command")
452+
return
453+
}
454+
455+
out, err := exec.CommandContext(ctx, "/bin/sh", "-c", command).CombinedOutput()
456+
output := "$ " + command + "\n" + string(out)
457+
if err != nil && len(out) == 0 {
458+
output = "$ " + command + "\nError: " + err.Error()
459+
}
460+
a.events <- runtime.ShellOutput(output)
451461
}
452462

453463
func (a *App) Subscribe(ctx context.Context, program *tea.Program) {

pkg/tools/builtin/shell.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"errors"
88
"fmt"
9+
"log/slog"
910
"os"
1011
"os/exec"
1112
"runtime"
@@ -137,6 +138,10 @@ func statusToString(status int32) string {
137138
}
138139

139140
func (h *shellHandler) RunShell(ctx context.Context, params RunShellArgs) (*tools.ToolCallResult, error) {
141+
if strings.TrimSpace(params.Cmd) == "" {
142+
return tools.ResultError("Error: empty command"), nil
143+
}
144+
140145
timeout := h.timeout
141146
if params.Timeout > 0 {
142147
timeout = time.Duration(params.Timeout) * time.Second
@@ -152,6 +157,8 @@ func (h *shellHandler) RunShell(ctx context.Context, params RunShellArgs) (*tool
152157
return h.sandbox.runCommand(timeoutCtx, ctx, params.Cmd, cwd, timeout), nil
153158
}
154159

160+
slog.Debug("Executing native shell command", "command", params.Cmd, "cwd", cwd)
161+
155162
return h.runNativeCommand(timeoutCtx, ctx, params.Cmd, cwd, timeout), nil
156163
}
157164

0 commit comments

Comments
 (0)