microsoft / microsoft/vscode

Terminal auto-approve: a partially-failed tree-sitter parse can still auto-approve

Open
#328,108 0 comments 0 reactions 1 assignee Claimed by @anthonykim1 View on GitHub
bug terminal
Dominant language
TypeScript
Stars
193k
Forks
42.4k
PR merge metrics
PR metrics pending

Description

Follow up to #328052 and #328107. Workbench `runInTerminal` path, live on `main`.

## Problem

When tree-sitter cannot fully parse a command line it still produces a partial tree, and `extractSubCommands` happily returns the captures it found. Those partial captures can omit executable parts of the line, so the sub-commands that *were* captured can all match allow rules and the line auto-approves — while the parts the parser dropped still run.

## Evidence

Measured against the real PowerShell grammar:

```
'ls; ./x.sh' hasError=true captures = ["ls"] <- "./x.sh" dropped
'ls; npm run test -- --foo' hasError=true captures = ["ls"] <- rest dropped
'echo hi; $x=' hasError=true captures = ["echo hi"] <- rest dropped
```

`ls` matches the default allow rule, so the line auto-approves and `./x.sh` executes without confirmation.

Both triggers are common in agent-emitted commands:

- a bare `--` separator (`npm run test -- --grep x`)
- `./path` at command position (`./scripts/test.sh`)

## Existing precedent

The Agent Host auto-approver already guards this — `commandAutoApprover.ts` returns `undefined` (→ requires confirmation) when `tree.rootNode.hasError` is set for a PowerShell parse, with the rationale that an erroring parse can produce truncated captures that hide part of the command line from rule matching.

The workbench has no equivalent gate: `commandLineAutoApproveAnalyzer.ts` only bails when `!subCommands?.length`, i.e. when the parse produced *nothing*. A partial parse proceeds normally.

## Suggested fix

Treat an errored parse as unanalyzable and require confirmation, matching the Agent Host.

Two consequences worth designing for:

1. Roughly **8% of realistic agent-emitted commands** fail to parse under the PowerShell grammar (measured 3/36 on a sample of plausible commands), so this will add prompts. Tracked separately as #328110.
2. Users need an escape hatch for those commands. See the companion issue about honouring explicit `matchCommandLine` rules before parsing.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.