Skip to content

fix(hooks): recognize run_in_terminal for VS Code Copilot Chat - #1800

Closed
devmasa wants to merge 5 commits into
rtk-ai:developfrom
devmasa:fix/run-in-terminal-copilot-chat
Closed

devmasa wants to merge 5 commits into
rtk-ai:developfrom
devmasa:fix/run-in-terminal-copilot-chat

Conversation

@devmasa

@devmasa devmasa commented May 8, 2026 •

Copy link
Copy Markdown

Summary

What changed

The hook payload format depends entirely on the event name casing in the hook config:

Hook config Payload format Working response
"preToolUse" (camelCase) toolName/toolArgs (JSON string) modifiedArgs
"PreToolUse" (PascalCase) tool_name/tool_input hookSpecificOutput.updatedInput

Both paths verified on Copilot CLI 1.0.60 and VS Code 1.123.0 / Copilot Chat 0.51.0.

HookFormat variants renamed to reflect payload format rather than host:

Variant Detects Returns
SnakeCaseCmd tool_name: "bash" or "run_in_terminal" hookSpecificOutput.updatedInput
CamelCaseCmd toolName: "bash" + toolArgs modifiedArgs

SnakeCaseCmd covers both VS Code Copilot Chat (always snake_case) and Copilot CLI with "PreToolUse" config.

Bugs fixed in .github/hooks/rtk-rewrite.json

  • "PreToolUse" → "preToolUse" (PascalCase caused modifiedArgs to be silently ignored)
  • "rtk hook" → "rtk hook copilot" (missing subcommand made the hook a no-op)

Notable findings

  • updatedInput works correctly in VS Code 1.123.0 / Copilot Chat 0.51.0 — no infinite loop
  • modifiedArgs only works with camelCase "preToolUse" config; hookSpecificOutput.updatedInput only works with PascalCase "PreToolUse" — the two are not interchangeable
  • VS Code always sends snake_case payloads regardless of event name casing

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test — all passed
  • Copilot CLI ("preToolUse" config): git status → modifiedArgs → rtk git status executed ✓
  • Copilot CLI ("PreToolUse" config): git status → hookSpecificOutput.updatedInput → rtk git status executed ✓
  • VS Code Copilot Chat: git status → hookSpecificOutput.updatedInput → rtk git status executed, no loop ✓

@CLAassistant

CLAassistant commented May 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@TeKGameR950

Copy link
Copy Markdown

Any update ?

@AbdAllahAbdElFattah13

Copy link
Copy Markdown

Can we expect this anytime soon? Anything we can help with?

@andreas-stremayr-ivii

Copy link
Copy Markdown

Also waiting for this. Using RTK only with agent instructions is strictly inferior to the command hook.

@AbdAllahAbdElFattah13

Copy link
Copy Markdown

Still waiting on this one. Meanwhile, after a suggestion by anasawad, we can do a manual hook until this is merged. A gist can be found here.

@aeppling

aeppling commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Hello @devmasa and everyone

Copilot CLI has been udpated on latest release 0.42.3 , now use latest docs from copilot CLI for a correct usage.
Please rebase on develop and ensure no conflicts with this.

I'll keep an eye on this PR so we can merge if it still add value to the copilot integration

@aeppling

aeppling commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Thanks @devmasa. I reviewed this against the current develop, which has since landed the Copilot CLI transparent-rewrite work (#1839). Most of this PR now overlaps with what is already there, so it needs a cleanup pass before it can move forward.

Where things stand vs develop:

  1. Recognizing run_in_terminal for VS Code Copilot Chat ([Bug] rtk hook copilot does not trigger for VS Code Copilot Chat — "run_in_terminal" not recognized in detect_format() #1425) is the one genuinely net-new fix. develop's detect_format() matches runTerminalCommand | Bash | bash but not run_in_terminal, so the hook never fires in current VS Code Copilot Chat. We want this.

  2. The Copilot CLI rewrite via modifiedArgs (github copilot (v1.0.24) now support modifiedArgs #1839) is already implemented on develop — but via a camelCase toolName + toolArgs envelope, whereas this PR assumes snake-case tool_name: "bash" + tool_input.command. The same payload is interpreted oppositely by the two. Copilot CLI is now on 0.42.3 (past the v1.0.24 this targets), so this needs to be re-verified against a real 0.42.3 hook payload, and likely support both envelopes rather than swap one out.

  3. Switching VS Code to deny + suggestion is not something we want. Transparent rewrite is a hard requirement (updatedInput for VS Code / Claude Code, modifiedArgs for Copilot CLI). If updatedInput is being ignored on a given version, the fix is to find the correct transparent mechanism for that version, not to fall back to deny.

Please rebase on develop, drop everything that already exists there, remove the deny path, and confirm against Copilot CLI 0.42.3 that what remains is still needed. As it stands the only part still missing from develop is the run_in_terminal recognition.

devmasa and others added 2 commits June 8, 2026 22:02
VS Code Copilot Chat sends tool_name: "run_in_terminal" in PreToolUse
payloads, but detect_format() only matched "runTerminalCommand", "Bash",
and "bash". This caused all commands from VS Code Copilot Chat to fall
through to HookFormat::PassThrough (no output), silently disabling the
hook.

Changes:
- Add "run_in_terminal" to the match arm in detect_format()
- Merge handle_vscode/handle_copilot_cli into handle_copilot_rewrite()
  to unify the rewrite logic and reduce duplication
- Add CopilotHookOutput enum to separate output format from logic
- Add build_copilot_deny_output() helper to centralize deny JSON
- Switch VS Code output to deny+suggestion (avoids the updatedInput
  infinite-loop bug in Copilot Chat; see issue body for details)
- Add tests: test_detect_vscode_run_in_terminal,
  test_copilot_deny_output_vscode_format,
  test_copilot_deny_output_cli_format

Fixes: rtk-ai#1425

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
… for Copilot CLI

Separate VS Code Copilot Chat and Copilot CLI into distinct HookFormat variants.
Copilot CLI v1.0.24+ now gets transparent rewrite via modifiedArgs.
VS Code keeps deny+suggestion to avoid the updatedInput infinite-loop bug.

Closes rtk-ai#1839

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@devmasa
devmasa force-pushed the fix/run-in-terminal-copilot-chat branch from 6561990 to d435ead Compare June 8, 2026 14:25
- VS Code Copilot Chat: revert deny-with-suggestion to updatedInput
  transparent rewrite (verified working on VS Code 1.123.0 / Copilot
  Chat 0.51.0 — no infinite loop observed)
- detect_format: narrow VS Code detection to "run_in_terminal" only,
  confirmed against live payloads (Copilot CLI 1.0.60 uses "bash",
  VS Code uses "run_in_terminal"; "Bash" belongs to run_claude path)
- Copilot CLI: replace bare Deny check with decide_hook_action so
  unattestable constructs ($(...), backticks, file redirects) are
  guarded consistently with the VS Code path
- Fix .github/hooks/rtk-rewrite.json: "rtk hook" → "rtk hook copilot"

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@devmasa
devmasa force-pushed the fix/run-in-terminal-copilot-chat branch from d435ead to 0eee969 Compare June 8, 2026 14:27
devmasa and others added 2 commits June 9, 2026 01:11
Copilot CLI sends camelCase payloads (toolName/toolArgs as JSON string)
only when the hook config uses "preToolUse" (camelCase event name).
"PreToolUse" (PascalCase) sends snake_case and modifiedArgs is ignored.

- detect_format: detect toolName/toolArgs (camelCase) for Copilot CLI;
  tool_name/tool_input (snake_case) remains for VS Code Copilot Chat
- process_copilot_payload: parse toolArgs JSON string, return
  modifiedArgs with permissionDecision:allow for transparent rewrite
- .github/hooks/rtk-rewrite.json: switch to "preToolUse" (required for
  Copilot CLI) and fix "rtk hook" → "rtk hook copilot" (was broken)
- Tests updated to camelCase fixture format; verified working on
  Copilot CLI 1.0.60 and VS Code 1.123.0 / Copilot Chat 0.51.0

VS Code Copilot Chat always sends snake_case regardless of event name
casing, so existing snake_case detection path is unaffected.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- VsCode → SnakeCaseCmd: handles snake_case payloads (tool_name/tool_input)
  Used by VS Code Copilot Chat (always) and Copilot CLI with "PreToolUse" config
  Returns hookSpecificOutput.updatedInput — verified working for both hosts
- CopilotCli → CamelCaseCmd: handles camelCase payloads (toolName/toolArgs)
  Used by Copilot CLI with "preToolUse" config (official/recommended)
  Returns modifiedArgs
- detect_format: add "bash" to snake_case branch so Copilot CLI users
  with "PreToolUse" config also get transparent rewrite via updatedInput

Copilot CLI supports both formats; VS Code supports SnakeCaseCmd only.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@devmasa

devmasa commented Jun 8, 2026

Copy link
Copy Markdown
Author

Thanks @AbdAllahAbdElFattah13 and @anasawad for the manual workaround gist — really helpful for people waiting on this.


Thanks for the detailed review, @aeppling. I've rebased on develop and spent significant time verifying hook behavior on real hardware. There's quite a bit to unpack, so I'll be thorough.

1. run_in_terminal recognition

Confirmed still missing from develop. This remains the net-new fix in this PR.

2. Copilot CLI payload format — the tricky part

The payload format that Copilot CLI sends depends entirely on the event name casing in the hook config:

Hook config Payload format modifiedArgs hookSpecificOutput.updatedInput
"preToolUse" (camelCase) toolName/toolArgs (JSON string) ✅ works ❌ silently ignored
"PreToolUse" (PascalCase) tool_name/tool_input ❌ silently ignored ✅ works

Both paths were verified on Copilot CLI 1.0.60 in this session.

The original .github/hooks/rtk-rewrite.json used "PreToolUse", so modifiedArgs never fired. Additionally "rtk hook" was missing the copilot subcommand. Both bugs are fixed in this PR.

modifiedArgs verified working (confirmed via postToolUse event showing the rewritten command actually executed):

// Hook receives (camelCase format):
{"toolName": "bash", "toolArgs": "{\"command\":\"git status\",\"description\":\"...\"}"}

// Hook returns:
{"permissionDecision": "allow", "modifiedArgs": {"command": "rtk git status", "description": "..."}}

// postToolUse confirms:
{"toolName": "bash", "toolArgs": "{\"command\":\"rtk git status\",\"description\":\"...\"}"}

develop's toolName/toolArgs detection was correct in principle; the problem was the hook config using "PreToolUse" which prevented it from ever being reached.

3. PreToolUse + modifiedArgs — a Copilot CLI bug (reporting for visibility)

With "PreToolUse" (PascalCase), modifiedArgs is silently ignored but hookSpecificOutput.updatedInput works. The fix in 1.0.24 (copilot-cli#2013) appears to have only covered modifiedArgs for the camelCase preToolUse path.

Since both paths now work (camelCase via modifiedArgs, PascalCase via updatedInput), this PR supports both: Copilot CLI users can use either "preToolUse" or "PreToolUse" and get transparent rewrite. VS Code Copilot Chat always sends snake_case so "PreToolUse" is the right choice there per official docs.

4. VS Code Copilot Chat: event name casing doesn't matter for payload format

VS Code always sends snake_case payloads regardless of event name casing. The VS Code docs note it converts camelCase event names to PascalCase internally, and all examples use "PreToolUse". Global VS Code hooks (~/.github/hooks/) should stay with PascalCase to follow the official format.

5. updatedInput vs deny

Tested updatedInput against VS Code 1.123.0 / Copilot Chat 0.51.0 — no infinite loop, command rewritten and executed exactly once. The deny-with-suggestion approach is reverted; updatedInput is back.

6. Security parity: unattestable construct guard for Copilot CLI (bonus — happy to revert if out of scope)

develop's Copilot CLI handler only checked for explicit Deny rules, skipping contains_unattestable_construct which blocks $(...), backticks, and file redirects. Both paths now go through decide_hook_action(cmd, Host::Claude) for consistent security. Drop it if you want to keep the PR minimal.

Summary of changes

  • run_in_terminal added to detect_format() — the net-new fix
  • HookFormat variants renamed to SnakeCaseCmd / CamelCaseCmd to reflect the actual payload format rather than the host
  • SnakeCaseCmd now covers both run_in_terminal (VS Code) and bash (Copilot CLI with "PreToolUse" config) — both return hookSpecificOutput.updatedInput
  • CamelCaseCmd covers Copilot CLI with "preToolUse" config — returns modifiedArgs
  • .github/hooks/rtk-rewrite.json: "PreToolUse" → "preToolUse", "rtk hook" → "rtk hook copilot" (both were bugs preventing the hook from working at all)
  • updatedInput transparent rewrite restored for VS Code
  • Both transparent rewrite paths verified working on Copilot CLI 1.0.60 and VS Code 1.123.0 / Copilot Chat 0.51.0
  • Unattestable construct guard applied to both paths

@devmasa

devmasa commented Jun 8, 2026

Copy link
Copy Markdown
Author

(Also updated the PR description to reflect the current state of the implementation.)

@devmasa

devmasa commented Jun 21, 2026

Copy link
Copy Markdown
Author

Hi @aeppling — just following up on this one.

I believe I've addressed all the points from your review:

run_in_terminal added to detect_format() (the net-new fix)
Rebased on develop, dropped everything already there
deny path removed; updatedInput restored and verified on VS Code 1.123.0
Both Copilot CLI payload envelopes verified on 1.0.60
Happy to make further adjustments if anything is still off. Is there anything blocking this, or anything I can help with to move it forward?

@pidefrem

Copy link
Copy Markdown

I hit the same #1425 gap independently and can confirm this PR closes it: on current develop, detect_format only matches runTerminalCommand on the snake_case tool_name path, so VS Code builds that send run_in_terminal no-op. This PR's match tool_name { "bash" | "run_in_terminal" => … } fixes exactly that.

For what it's worth, if a maintainer wants the minimal change to unblock #1425 independently of the larger format-detection refactor here, the one-liner is just adding run_in_terminal to the existing match:

matches!(tool_name, "runTerminalCommand" | "run_in_terminal" | "Bash" | "bash")

I have that minimal version on my fork (fix/copilot-vscode-run-in-terminal, +1 test test_detect_vscode_run_in_terminal) — but it's strictly a subset of what you do here, so no need for a competing PR. If this lands I'll drop mine. Happy to review/test this one if that helps it move.

(Orthogonal note for anyone triaging the Copilot tool-name issues: #2574 covers the JetBrains host — camelCase toolName: "run_in_terminal" with a deny-with-suggestion path for #2443 — which is a different payload shape from the VS Code snake_case case this PR and #1425 are about. They're complementary, not duplicates.)

@KevinRond

Copy link
Copy Markdown

Hi! I'm running into this issue in RTK 0.43.0 and noticed this PR appears to address it. Is it still being worked on? I'd be happy to help validate the fix if needed. Thanks!

@KuSh

KuSh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the careful write-up of the two payload shapes. Both halves are on develop already: #3093 (merged 2026-07-26) added the Copilot IDE path for toolName run_in_terminal, and #3212 (merged 2026-08-02) made detect_format recognise run_in_terminal in VS Code Copilot Chat's snake_case payloads, which closed #1425, while transparent rewrite through modifiedArgs landed earlier in 4b0bbf2 and shipped in v0.42.3, which closed #1839. Those merges also reshaped HookFormat into VsCode, CopilotCli and CopilotIde, choosing the format from the payload shape rather than the event name casing, so the rename and the tests around it no longer apply to the current code. One leftover would make a good narrow PR: the repository's own .github/hooks/rtk-rewrite.json still says "rtk hook" with no copilot subcommand and lacks the "version": 1 key that init.rs now writes. Please comment or reopen if something here was missed.

@KuSh KuSh closed this Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

9 participants