Skip to content

Fix: agent hooks usage example for blocking dangerous commands not working#9675

Open
painfulexistence wants to merge 1 commit intomicrosoft:mainfrom
painfulexistence:agent-hooks-usage-example
Open

Fix: agent hooks usage example for blocking dangerous commands not working#9675
painfulexistence wants to merge 1 commit intomicrosoft:mainfrom
painfulexistence:agent-hooks-usage-example

Conversation

@painfulexistence
Copy link
Copy Markdown

The block-dangerous-commands usage example doesn't work because the tool_name check in the script doesn't match the actual tool name, which should be "run_in_terminal".

VSCode version: 1.116.0
OS: Darwin arm64 24.6.0

Steps to reproduce:

  1. Copy the block-dangerous-commands example code from the Usage Scenarios section
  2. Ask the agent to run rm -rf ${some_test_file}
  3. Agent hook doesn't block the command

Copy link
Copy Markdown
Collaborator

@ntrogh ntrogh left a comment

Choose a reason for hiding this comment

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

@painfulexistence Thanks for the PR. Can you revert the exit code?

if echo "$COMMAND" | grep -qE '(rm\s+-rf|DROP\s+TABLE|DELETE\s+FROM)'; then
echo '{"hookSpecificOutput":{"permissionDecision":"deny","permissionDecisionReason":"Destructive command blocked by security policy"}}'
exit 0
exit 2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The user would get a generic "unexpected error" with exit code 2. With the exit code 0, the command is denied and the provided reason is shown. Can you revert back to exit code 0?

Copy link
Copy Markdown
Author

@painfulexistence painfulexistence Apr 21, 2026

Choose a reason for hiding this comment

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

Hi @ntrogh, thanks for the review!

I did try exit code 0 and it didn't work. The command did not be blocked when exit code is either 0 or 1.
(I've double checked one minutes ago)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@painfulexistence Ok, let me check if this is a bug. Thanks for looking into this!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@painfulexistence I can't repro with exit code 0. The hook blocks the command from executing. I do notice that the regex in the sample only blocks rm -rf and not plan rm. Can you check in the agent debug logs (... > Show Agent Debug Logs) which command is being run by the LLM?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually I tested it with git add command.
FYI, I ran in autopilot mode. I can still reproduce this issue with exit code 0. Could you try it with autopilot mode?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, this might actually be a side-effect of Autopilot, which auto-approves all tool calls.
I'll create an issue in the vscode repo.

Can you confirm the hook works with exit code 0 when you use default permissions?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Okay, I tried Default Approvals mode, but exit code 0 still did not block specified commands. Only exit code 2 works for me.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@painfulexistence This seems to be a product issue. Can you log an issue in the microsoft/vscode repo and provide the repro steps for the scenario that doesn't work with exit code 2?

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.

2 participants