ensure provider working directories exist

This commit is contained in:
Haitao Pan 2026-04-22 13:51:26 +08:00
parent f7b8076a84
commit 5bb9afe422
2 changed files with 55 additions and 0 deletions

View File

@ -30,6 +30,9 @@ func RunProviderCommand(
if command == "" {
return "", fmt.Errorf("unsupported provider: %s", provider)
}
if err := ensureWorkingDirectoryExists(workingDirectory); err != nil {
return "", err
}
cmd := exec.CommandContext(ctx, command, args...)
if strings.TrimSpace(workingDirectory) != "" {
cmd.Dir = strings.TrimSpace(workingDirectory)
@ -59,6 +62,30 @@ func RunProviderCommand(
return output, nil
}
func ensureWorkingDirectoryExists(workingDirectory string) error {
workingDirectory = strings.TrimSpace(workingDirectory)
if workingDirectory == "" {
return nil
}
if info, err := os.Stat(workingDirectory); err == nil {
if info.IsDir() {
return nil
}
return fmt.Errorf("working directory is not a directory: %s", workingDirectory)
}
if err := os.MkdirAll(workingDirectory, 0o755); err != nil {
return fmt.Errorf("ensure working directory %s: %w", workingDirectory, err)
}
info, err := os.Stat(workingDirectory)
if err != nil {
return fmt.Errorf("verify working directory %s: %w", workingDirectory, err)
}
if !info.IsDir() {
return fmt.Errorf("working directory is not a directory: %s", workingDirectory)
}
return nil
}
func NormalizeProviderWorkingDirectory(provider, requested string) (string, string) {
requested = strings.TrimSpace(requested)
if requested == "" {

View File

@ -1,6 +1,8 @@
package shared
import (
"context"
"os"
"path/filepath"
"testing"
)
@ -52,3 +54,29 @@ func TestResolveProviderCommandSupportsHermes(t *testing.T) {
t.Fatalf("unexpected hermes args: %#v", args)
}
}
func TestRunProviderCommandCreatesMissingWorkingDirectory(t *testing.T) {
workspaceRoot := filepath.Join(t.TempDir(), "owners", "local", "user", "thread-1")
scriptPath := filepath.Join(t.TempDir(), "hermes.sh")
if err := os.WriteFile(scriptPath, []byte("#!/bin/sh\necho ok\n"), 0o755); err != nil {
t.Fatalf("write script: %v", err)
}
t.Setenv("ACP_HERMES_BIN", scriptPath)
output, err := RunProviderCommand(
context.Background(),
"hermes",
"sonnet",
"hello world",
workspaceRoot,
)
if err != nil {
t.Fatalf("RunProviderCommand() error = %v", err)
}
if output != "ok" {
t.Fatalf("RunProviderCommand() output = %q, want %q", output, "ok")
}
if info, err := os.Stat(workspaceRoot); err != nil || !info.IsDir() {
t.Fatalf("expected working directory to be created, stat err=%v info=%v", err, info)
}
}