Skip to content

fix(core): explain unanalyzable shell commands - #54218

Closed
kitlangton wants to merge 1 commit into
devfrom
fix/shell-scanner-substitution
Closed

kitlangton wants to merge 1 commit into
devfrom
fix/shell-scanner-substitution

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

Why

When the portable shell scanner can't analyze a command, the shell tool refuses it with only a reason code:

Portable shell scanner cannot analyze command: command-substitution

Agents most often hit this when they put Markdown in a double-quoted argument, for example note add --body "uses `vec3(1.0)` here" Title. Bash treats those backticks as command substitution and would try to run vec3(1.0) as a command. Refusing is correct, but the error doesn't explain what went wrong or how to fix it, so agents keep retrying variations or give up.

What Changes

The scanner still refuses commands it can't analyze, and they still never run. The error now says the command was not run, keeps the reason code, and explains how to rewrite the command.

Before

Portable shell scanner cannot analyze command: command-substitution

After

Command was not run: the shell permission scanner cannot analyze it (command-substitution). A command substitution or nested expansion is incomplete or cannot be checked. Inside double quotes, the shell runs text between backticks as a command. For literal backticks, such as Markdown code spans, use single quotes, escape each backtick as \`, or write the text to a file and pass "$(cat file)". Rewrite the command and try again.

Every OpaqueReason now has its own hint, such as "A quote is not closed." or "A redirection is missing its target or is not allowed here."

Command Result
note add --body "uses `vec3(1.0)` here" T Not run. The error explains how to pass literal backticks
note add --body 'uses `vec3(1.0)` here' T Runs. The backticks stay literal
note add --body "$(cat notes.md)" T Runs. note add … and cat notes.md are checked as separate resources
note add --body "$(cat notes.md)" T with cat * denied Blocked by the deny rule

Permission Safety

I chose not to fall back to asking for approval. The default policy allows every shell resource, so a whole-command fallback resource would be auto-allowed. That would let commands inside an unanalyzed substitution get past deny rules like cat * or rm *. The permissions docs also say tree-sitter is not a fallback and that there is no extra approval mode. This PR leaves those rules unchanged and updates the docs to describe the clearer error and how substitutions are checked.

Scope

This PR changes only the scanner's error messages, adds tests and updates the docs. Adding a forced-approval fallback for unanalyzable commands would need a new Permission mode that ignores allow rules. That belongs in its own PR if we want it.

Verification

cd packages/core
bun run test test/shell-parse.test.ts test/shell-parse-parity.test.ts
bun run test test/tool-shell.test.ts
bun run test test/shell-scan test/shell-parse-compatibility.test.ts test/shell-parse-syntax.test.ts test/permission.test.ts
bun run typecheck
  • Shell parse and parity tests: 62 passed.
  • tool-shell.test.ts: 93 passed, 19 skipped (Windows and PowerShell). New end-to-end tests use the real Permission service and a real sh:
    • printf '%s' "$(cat body.md)" reproduces a Markdown file containing backticks byte for byte.
    • A cat * deny rule blocks the same command, and its marker file is never written.
    • A double-quoted `vec3(1.0)` returns the new message without running or asking for permission. The single-quoted rewrite runs.
  • Scanner, compatibility, syntax and permission tests: 3415 passed, 5 skipped.
  • Typecheck passed.

@opencode-agent opencode-agent Bot left a comment

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.

Looks good. I reproduced the bare command-substitution error on the base with the portable scanner. With this PR, the same command is still refused, and the error now explains how to rewrite it. The single-quoted and "$(cat file)" forms scan the same way before and after, and PowerShell never gets the bash-specific backtick hint. The shell-parse, parity and tool-shell tests pass (132 passed, 42 skipped), and the packages/core typecheck passes.

Public API: unchanged, apart from the new error text and the internal ShellParse.opaqueMessage export.

One small point: in tool-shell.test.ts, the first two new tests (substitution passthrough and the cat * deny rule) cover behaviour that already worked on the base. The backtick test is the one that checks this change, so the other two can be dropped to keep the change minimal.

@kitlangton
kitlangton force-pushed the fix/shell-scanner-substitution branch from 1ba28e7 to 983c71a Compare October 25, 2026 22:47

@opencode-agent opencode-agent Bot left a comment

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.

The only change since my last review is a rebase onto the current base. It also updates one existing shell-parse.test.ts assertion that checked the old error text. Nothing new to raise. The shell-parse, parity and tool-shell tests pass in packages/core, and tsc -b passes. The package's typecheck script fails on the base as well, because the installed Bun has no bun check.

Public API: unchanged.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant