shelley: fix git repo detection using wrong directory

Philip Zeyliger and Claude Opus 4.6 created

Prompt: evaluate and fix https://github.com/boldsoftware/shelley/issues/71 . The agent has been given a working directory; maybe we're checking the wrong directory to see if it's a git repo?

collectGitInfo() was running `git rev-parse --show-toplevel` without
setting cmd.Dir, so it checked the server process's cwd instead of the
user's working directory. This caused the system prompt to incorrectly
say "Not in a git repository" for VMs where the user's project was in
a git repo but the server process ran from a different directory.

Fixes https://github.com/boldsoftware/shelley/issues/71

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Change summary

server/system_prompt.go      |  9 +++++--
server/system_prompt_test.go | 42 +++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 3 deletions(-)

Detailed changes

server/system_prompt.go 🔗

@@ -80,7 +80,7 @@ func collectSystemData(workingDir string) (*SystemPromptData, error) {
 	}
 
 	// Try to collect git info
-	gitInfo, err := collectGitInfo()
+	gitInfo, err := collectGitInfo(wd)
 	if err == nil {
 		data.GitInfo = gitInfo
 	}
@@ -132,9 +132,12 @@ func collectSystemData(workingDir string) (*SystemPromptData, error) {
 	return data, nil
 }
 
-func collectGitInfo() (*GitInfo, error) {
+func collectGitInfo(dir string) (*GitInfo, error) {
 	// Find git root
 	rootCmd := exec.Command("git", "rev-parse", "--show-toplevel")
+	if dir != "" {
+		rootCmd.Dir = dir
+	}
 	rootOutput, err := rootCmd.Output()
 	if err != nil {
 		return nil, err
@@ -350,7 +353,7 @@ func GenerateSubagentSystemPrompt(workingDir string) (string, error) {
 	}
 
 	// Try to collect git info
-	gitInfo, err := collectGitInfo()
+	gitInfo, err := collectGitInfo(wd)
 	if err == nil {
 		data.GitInfo = gitInfo
 	}

server/system_prompt_test.go 🔗

@@ -2,6 +2,7 @@ package server
 
 import (
 	"os"
+	"os/exec"
 	"path/filepath"
 	"strings"
 	"testing"
@@ -64,6 +65,47 @@ func TestSystemPromptEmptyCwdFallsBackToCurrentDir(t *testing.T) {
 	}
 }
 
+// TestSystemPromptDetectsGitInWorkingDir verifies that the system prompt
+// correctly detects a git repo in the specified working directory, not the
+// process's cwd. Regression test for https://github.com/boldsoftware/shelley/issues/71
+func TestSystemPromptDetectsGitInWorkingDir(t *testing.T) {
+	// Create a temp dir with a git repo
+	tmpDir, err := os.MkdirTemp("", "shelley_git_test")
+	if err != nil {
+		t.Fatalf("failed to create temp dir: %v", err)
+	}
+	defer os.RemoveAll(tmpDir)
+
+	// Initialize a git repo in the temp dir
+	cmd := exec.Command("git", "init")
+	cmd.Dir = tmpDir
+	if out, err := cmd.CombinedOutput(); err != nil {
+		t.Fatalf("git init failed: %v\n%s", err, out)
+	}
+	cmd = exec.Command("git", "commit", "--allow-empty", "-m", "initial")
+	cmd.Dir = tmpDir
+	if out, err := cmd.CombinedOutput(); err != nil {
+		t.Fatalf("git commit failed: %v\n%s", err, out)
+	}
+
+	// Generate system prompt for the git repo directory
+	prompt, err := GenerateSystemPrompt(tmpDir)
+	if err != nil {
+		t.Fatalf("GenerateSystemPrompt failed: %v", err)
+	}
+
+	// The prompt should say "Git repository root:" not "Not in a git repository"
+	if strings.Contains(prompt, "Not in a git repository") {
+		t.Errorf("system prompt incorrectly says 'Not in a git repository' for a directory that is a git repo")
+	}
+	if !strings.Contains(prompt, "Git repository root:") {
+		t.Errorf("system prompt should contain 'Git repository root:' for a git repo directory")
+	}
+	if !strings.Contains(prompt, tmpDir) {
+		t.Errorf("system prompt should reference the git root directory %s", tmpDir)
+	}
+}
+
 func min(a, b int) int {
 	if a < b {
 		return a