Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions internal/llmadapters/subprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (a *SubprocessAdapter) startClaudeBG(ctx context.Context, req Request, resu
_ = cleanup()
return nil, err
}
if err := writeClaudeBGPromptFile(req.Prompt, scratch); err != nil {
if err := writeClaudeBGPromptFile(req.Prompt, scratch, req.ReviewerWorkspace); err != nil {
_ = cleanup()
return nil, err
}
Expand All @@ -281,16 +281,14 @@ func (a *SubprocessAdapter) startClaudeBG(ctx context.Context, req Request, resu
}

execArgs := append(append([]string(nil), a.commandArgsPrefix...), args...)
launchDir := workDir
if req.ReviewerWorkspace != nil {
launchDir = req.ReviewerWorkspace.RepoDir
}
// Never launch inside the reviewer checkout: Claude would load the PR's
// .claude/ project settings. The checkout is reachable through --add-dir.
env, err := a.processEnv(req, scratch)
if err != nil {
_ = cleanup()
return nil, err
}
process, err := launchProcess(ctx, a.command, execArgs, launchDir, env, a.timeout, req.LogPath, cleanup, false)
process, err := launchProcess(ctx, a.command, execArgs, workDir, env, a.timeout, req.LogPath, cleanup, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -506,7 +504,7 @@ func (a *SubprocessAdapter) startClaudeForeground(ctx context.Context, req Reque
_ = cleanup()
return nil, err
}
if err := writeClaudeBGPromptFile(req.Prompt, scratch); err != nil {
if err := writeClaudeBGPromptFile(req.Prompt, scratch, req.ReviewerWorkspace); err != nil {
_ = cleanup()
return nil, err
}
Expand Down Expand Up @@ -1974,15 +1972,22 @@ func claudeBGSprintedValue(value any) string {
return stringValue
}

func writeClaudeBGPromptFile(prompt string, scratch string) error {
func writeClaudeBGPromptFile(prompt string, scratch string, workspace *ReviewerWorkspaceRequest) error {
promptPath := filepath.Join(scratch, claudeBGPromptFilename)
resultPath := filepath.Join(scratch, claudeBGResultFilename)
if workspace != nil {
prompt = claudeReviewerWorkspaceNote(workspace.RepoDir) + "\n\n" + prompt
}
if err := os.WriteFile(promptPath, []byte(wrapClaudeBGPrompt(prompt, resultPath)), 0o600); err != nil {
return fmt.Errorf("llm subprocess: writing Claude bg prompt file: %w", err)
}
return nil
}

func claudeReviewerWorkspaceNote(repoDir string) string {
return fmt.Sprintf("The pull request checkout is at %s. Repository-relative paths in these instructions are relative to it. Your shell does not start there: run commands with `cd %q && ...` or `git -C %q`.", repoDir, repoDir, repoDir)
}

func claudeBGPositionalPrompt(scratch string) string {
promptPath := filepath.Join(scratch, claudeBGPromptFilename)
resultPath := filepath.Join(scratch, claudeBGResultFilename)
Expand Down
8 changes: 6 additions & 2 deletions internal/llmadapters/subprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,12 @@ func TestSubprocessClaudeReviewerWorkspaceLaunch(t *testing.T) {
if !containsSamePath(addDirs, record.AddDir) || !containsSamePath(addDirs, repoRoot) {
t.Fatalf("--add-dir values = %#v, want scratch %q and repo %q", addDirs, record.AddDir, repoRoot)
}
if !samePath(t, record.Cwd, repoRoot) {
t.Fatalf("cwd = %q, want reviewer workspace repo %q", record.Cwd, repoRoot)
// Launching inside the PR checkout would load its .claude/ project settings.
if wantWorkDir := filepath.Join(filepath.Dir(recordPath), "claude-bg-workdir"); !samePath(t, record.Cwd, wantWorkDir) {
t.Fatalf("cwd = %q, want Claude bg workdir %q outside the reviewer checkout", record.Cwd, wantWorkDir)
}
if !strings.Contains(record.PromptFile, repoRoot) {
t.Fatalf("prompt file = %q, want reviewer checkout path %q", record.PromptFile, repoRoot)
}
if tools := flagValue(record.AdapterArgs, "--tools"); tools != "Read,Write,Bash" {
t.Fatalf("--tools = %q, want reviewer workspace tools", tools)
Expand Down
Loading