ConnectOnionConnectOnion
DocsUseful Pluginsprefer_write_tool

prefer_write_tool

Block bash file creation, soft-remind for file reading

Problem

AI models often use bash commands for file operations:

File Creation

code
1cat <<EOF > /tmp/my_script.py 2import os 3print("hello") 4EOF

File Reading

code
1cat config.json 2head -n 10 README.md

File creation via bash bypasses tool UI/diffs/approval flow and has escaping issues. File reading via bash works but misses line numbers, formatting, and control that read_file provides.

Solution

This plugin uses two strategies:

File Creation

Hard block — raises ValueError, agent must use Write or Edit tool

File Reading

Soft reminder — command runs, system reminder appended suggesting read_file

main.py
1from connectonion import Agent 2from connectonion.useful_plugins import prefer_write_tool 3 4agent = Agent( 5 "assistant", 6 tools=[bash, read_file, write, edit], 7 plugins=[prefer_write_tool] 8)

Detected Patterns

File Creation (hard blocked)

cat <<EOF > file.py - heredoc redirection
echo "..." > file.py - output redirection
printf "..." > file.py - printf redirection
cmd > ./file - output redirection to path
cmd >> ./file - append redirection to path
tee file.py - tee command

File Reading (soft reminder)

cat file.txt - standalone cat (not piped)
head file.txt - read first lines
tail file.log - read last lines
less file.txt - page through file
more file.txt - page through file

Note: cat file | grep pattern (piped cat) is not detected — piping is legitimate bash usage.

What Happens

For file creation (blocked)

Bash file creation blocked.

<system-reminder>
You tried to create a file using bash. This is blocked.

Use the Write tool instead:
  Write(file_path="/path/to/file.py", content="...")

For editing existing files:
  Edit(file_path="/path/to/file.py", old_string="...", new_string="...")
</system-reminder>

For file reading (soft reminder)

[actual command output here]

<system-reminder>
You used bash to read a file. Consider using the read_file tool instead:
  read_file(file_path="/path/to/file.txt")

Why: read_file provides line numbers, proper formatting, and better control.
</system-reminder>

Result

File creation is stopped before execution. File reading runs normally with a gentle nudge toward read_file.

How It Works

The plugin exports two event handlers:

block_bash_file_creation (before_each_tool) — detects file creation patterns and raises ValueError. For file reading, sets a session flag.

remind_read_file (after_each_tool) — if the flag is set, appends a system reminder to the tool result message.

Combining with tool_approval

You can use both plugins together:

main.py
1from connectonion.useful_plugins import tool_approval, prefer_write_tool 2 3agent = Agent( 4 "assistant", 5 tools=[bash, read_file, write, edit], 6 plugins=[prefer_write_tool, tool_approval] # prefer_write_tool first 7)

Order Matters

prefer_write_tool should come first to block file creation before tool_approval prompts for approval.

See Also

Enjoying ConnectOnion?

⭐ Star us on GitHub = ☕ Coffee chat with our founder. We love meeting builders.