Skip to content

fix: use correct workDir for agentic mode with file inputs#197

Merged
kris-hansen merged 1 commit intomainfrom
fix/agentic-file-input-workdir
Mar 28, 2026
Merged

fix: use correct workDir for agentic mode with file inputs#197
kris-hansen merged 1 commit intomainfrom
fix/agentic-file-input-workdir

Conversation

@kris-hansen
Copy link
Copy Markdown
Owner

@kris-hansen kris-hansen commented Mar 28, 2026

PR Type:

Bug fix


PR Description:

  • Corrects the working directory used in agentic mode when file inputs are involved.
  • Ensures that the agentic mode reads file content and uses SendPromptAgentic instead of SendPromptWithFile.
  • Implements a fallback to SendPromptWithFile for non-agentic mode.
  • Adds debug logging for better visibility and troubleshooting.

PR Main Files Walkthrough:

files:
  • utils/processor/action_handler.go: - Modified the logic to handle file inputs differently based on whether the mode is agentic or not.
  • In agentic mode, the file content is read and combined with the action prompt, and SendPromptAgentic is used to ensure the correct working directory.
  • Added debug logging to provide visibility into the process, including setting up a debug file and using native worktree support if available.
  • Retained the original behavior of using SendPromptWithFile for non-agentic mode.

User Description:

Problem

PR #196 fixed the default runtimeDir when not specified, but the issue persisted. Found the real culprit:

When agentic loops have file inputs (like STDIN content written to a temp file), the code path was:

  1. STDIN content → temp file at /var/folders/.../comanda-stdin-*.txt
  2. File passed as FileInput
  3. Code falls through to SendPromptWithFile (line ~196)
  4. SendPromptWithFile uses filepath.Dir(file.Path) as workDir → temp directory

Debug showed:

[DEBUG][ClaudeCode] Preparing to send prompt with file to Claude Code
[DEBUG][ClaudeCode] Starting claude command in /var/folders/jj/.../T

Note: "with file" not "agentic prompt" — it was using the wrong code path!

Solution

When in agentic mode with file inputs:

  • Read the file content manually
  • Combine with the action prompt
  • Call SendPromptAgentic (which uses p.getEffectiveWorkDir())
  • Fall back to SendPromptWithFile only for non-agentic mode

This ensures Claude Code runs in the project directory where allowed_paths resolve correctly.

When agentic loops have file inputs (e.g., from STDIN temp files), the
code was falling through to SendPromptWithFile which uses the file's
directory as the working directory. For temp files, this meant Claude
Code ran in /var/folders/... instead of the project directory.

Fix: When in agentic mode with file inputs, read the file content and
use SendPromptAgentic with the correct working directory from
getEffectiveWorkDir().
@preston-ai
Copy link
Copy Markdown

preston-ai bot commented Mar 28, 2026

PR Analysis

  • 🎯 Main theme: Corrects the working directory used in agentic mode when file inputs are involved.
  • 📝 PR summary: This PR addresses a bug where the incorrect working directory was used in agentic mode with file inputs. It ensures that file content is read and combined with the action prompt, using SendPromptAgentic for agentic mode, and falls back to SendPromptWithFile for non-agentic mode. Debug logging has been added for better visibility.
  • 📌 Type of PR: Bug fix
  • 🧪 Relevant tests added: No
  • Focused PR: yes, because the changes are specifically targeted at fixing the working directory issue in agentic mode with file inputs.
  • ⏱️ Estimated effort to review [1-5]: 2, because the changes are straightforward and focused on a specific bug fix, but require understanding of the agentic mode logic.
  • 🔒 Security concerns: No security concerns found

PR Feedback

  • 💡 General suggestions: The PR effectively addresses the issue with the working directory in agentic mode. Consider adding tests to ensure that the new logic behaves as expected in different scenarios, which would help prevent regressions in the future.

How to use

Instructions

To invoke the Preston AI, add a comment using one of the following commands:
/review: Request a review of your Pull Request.
/describe: Update the PR title and description based on the contents of the PR.
/improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
/ask <QUESTION>: Ask a question about the PR.
/add_docs: Generate docstring for new components introduced in the PR.
/generate_labels: Generate labels for the PR based on the PR's contents.

// In agentic mode, read the file content and use SendPromptAgentic
// This ensures the correct working directory is used instead of the file's temp directory
if isAgenticMode {
if claudeCode, ok := configuredProvider.(*models.ClaudeCodeProvider); ok {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling the case where configuredProvider is not of type *models.ClaudeCodeProvider more explicitly, perhaps by logging a warning or error message. This would improve the robustness of the code by making it clear when the expected provider type is not being used. [medium]

claudeCode.SetDebugFile(debugPath)
if p.streamLog != nil {
debugWatcher = NewDebugWatcher(debugPath, p.streamLog)
debugWatcher.Start()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the debugWatcher is properly stopped in all code paths, including error handling. This can be done by using a defer statement right after its creation. [important]

claudeCode.SetWorktree(worktreeName)
defer claudeCode.ClearWorktree()
}
result, err := claudeCode.SendPromptAgentic(modelName, combinedPrompt,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name than result for the output of SendPromptAgentic to improve code readability and maintainability. [medium]

return nil, fmt.Errorf("failed to read file %s: %w", fileInputs[0].Path, err)
}
// Combine file content with action
combinedPrompt := fmt.Sprintf("File: %s\n\n```\n%s\n```\n\nTask: %s",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When constructing combinedPrompt, consider using a template or a dedicated function to handle the formatting. This would make the code cleaner and easier to modify if the prompt format needs to change. [medium]

@kris-hansen kris-hansen merged commit 9bee54f into main Mar 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant