Skip to content

Fix detection of shell built-in commands (e.g. pwd) in interactive CLI#1252

Merged
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
aksKrIITK:fix-shell-command-detection
May 3, 2026
Merged

Fix detection of shell built-in commands (e.g. pwd) in interactive CLI#1252
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
aksKrIITK:fix-shell-command-detection

Conversation

@aksKrIITK
Copy link
Copy Markdown
Contributor

Fixes #

Describe the changes you have made in this PR -

This PR fixes detection of direct shell commands (e.g., pwd) in the interactive CLI.

Previously, _looks_like_direct_shell_command relied on shutil.which, which only detects executables available in the system PATH. However, many common shell commands like pwd and cd are shell built-ins and are not discoverable via shutil.which, especially on Windows.

As a result, direct commands such as "pwd" were not recognized as valid shell actions, causing plan_terminal_tasks("pwd") to return an empty list instead of ["shell"].


Demo/Screenshot for feature changes and bug fixes -

Before fix:

plan_terminal_tasks("pwd") → []

After fix:

plan_terminal_tasks("pwd") → ["shell"]

All related CLI tests now pass:

22 passed in 1.30s

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

  • Problem: Direct shell commands like "pwd" were not detected because shutil.which does not recognize shell built-ins.

  • Approach:
    I extended _looks_like_direct_shell_command by adding a fallback check for common shell built-in commands (e.g., pwd, cd, ls).

  • Alternative considered:
    Modifying _plan_clause_actions or _extract_shell_command, but that would break abstraction and duplicate logic.
    The correct layer to fix was _looks_like_direct_shell_command.

  • Why this approach:

    • Minimal and localized change
    • Preserves existing behavior
    • Improves cross-platform compatibility (Windows/Linux)
    • Aligns with current architecture
  • Key change:
    Added a safe fallback for known shell commands before shutil.which check.


Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • My code follows the project's style guidelines and conventions

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 3, 2026

Greptile Summary

This PR adds a COMMON_SHELL_COMMANDS fast-path set to _looks_like_direct_shell_command so that genuine shell built-ins like pwd and cd are recognised without relying on shutil.which. The core fix is sound, but previous review threads have flagged that the set includes non-built-in external executables (kubectl, docker, git) that cause false positives on machines where those tools are absent, and that the constant diverges from the file's frozenset/underscore-prefix naming convention and is placed after the function that uses it.

Confidence Score: 4/5

The fix is functionally correct for the core built-ins case, but the inclusion of non-built-in tools warrants addressing before merge.

A P1 regression (non-built-in tools always matching even when not installed) was identified in a previous review thread and has not been addressed in the current diff; this caps the score at 4.

app/cli/interactive_shell/agent_actions.py — specifically the contents and placement of COMMON_SHELL_COMMANDS.

Important Files Changed

Filename Overview
app/cli/interactive_shell/agent_actions.py Adds a COMMON_SHELL_COMMANDS set as a fast-path check in _looks_like_direct_shell_command; the set is defined after the function that uses it, uses a mutable set instead of the file's established frozenset convention, lacks the module-private underscore prefix, and includes external executables (kubectl, docker, git) that can produce false positives on systems where those tools are not installed.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[User input text] --> B[_looks_like_direct_shell_command]
    B --> C{first token is None?}
    C -->|Yes| D[return False]
    C -->|No| E{in NON_COMMAND_STARTS?}
    E -->|Yes| D
    E -->|No| F{in COMMON_SHELL_COMMANDS? NEW}
    F -->|Yes| G[return True - dispatch as shell action]
    F -->|No| H{starts with ./ ../ /}
    H -->|Yes| I{Path.exists?}
    I -->|Yes| G
    I -->|No| D
    H -->|No| J{shutil.which finds it?}
    J -->|Found| G
    J -->|Not Found| D
Loading

Reviews (2): Last reviewed commit: "Fix detection of shell built-in commands..." | Re-trigger Greptile

Comment on lines +214 to +218
COMMON_SHELL_COMMANDS = {
"pwd", "ls", "cd", "echo", "cat", "touch",
"mkdir", "rm", "cp", "mv", "grep", "find",
"kubectl", "docker", "git"
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Non-built-ins in COMMON_SHELL_COMMANDS cause false positives

kubectl, docker, and git are external executables, not shell built-ins. They are already discoverable via shutil.which on systems where they are installed, so listing them here is redundant there. On systems where they are not installed, this change causes a regression: any input whose first token is "docker", "kubectl", or "git" will now always be classified as a shell command, bypassing the LLM fallback. For example, "git is a version control tool" typed on a machine without Git installed would be incorrectly dispatched as a shell action instead of being sent to the LLM.

The fix should be limited to genuine shell built-ins that shutil.which cannot discover (e.g. pwd, cd, echo in some shells).

Suggested change
COMMON_SHELL_COMMANDS = {
"pwd", "ls", "cd", "echo", "cat", "touch",
"mkdir", "rm", "cp", "mv", "grep", "find",
"kubectl", "docker", "git"
}
COMMON_SHELL_BUILTINS: frozenset[str] = frozenset(
{
"pwd", "cd", "echo", "export", "unset",
"alias", "type", "source", "exit", "history",
}
)

@muddlebee
Copy link
Copy Markdown
Collaborator

pls open a github issue and describe the scenario/context for this

@muddlebee muddlebee closed this May 3, 2026
@muddlebee muddlebee reopened this May 3, 2026
@muddlebee muddlebee merged commit 90b1d37 into Tracer-Cloud:main May 3, 2026
1 check passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

🧑‍💻 @aksKrIITK has entered the contributor hall of fame. Merged. Done. Shipped. Go touch grass (then come back with another PR). 🌱


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants