Skip to content

fix(agent-core): keep session approval for commands with special chars (#325)#496

Open
fancive wants to merge 1 commit into
MoonshotAI:mainfrom
fancive:fix/325-session-approval-special-chars
Open

fix(agent-core): keep session approval for commands with special chars (#325)#496
fancive wants to merge 1 commit into
MoonshotAI:mainfrom
fancive:fix/325-session-approval-special-chars

Conversation

@fancive
Copy link
Copy Markdown

@fancive fancive commented Jun 6, 2026

Summary

Fixes #325. Session-level "Approve for this session" stopped working for Bash commands containing quotes, parentheses, or pipes — the user was prompted to approve the identical command on every call.

Root cause

When a command is approved for the session, the rule subject is stored with glob metacharacters backslash-escaped via escapeRuleSubjectLiteral(). On the next call, matchesGlobRuleSubject() re-matches the stored pattern through picomatch (globMatch). picomatch drops the double quotes adjacent to the escaped parens, so the stored rule

Bash(python -c "print\('1'\)")

never matches the command it was created from

python -c "print('1')"

and the session approval is effectively ignored.

Fix

Add an exact-literal fast path in matchesGlobRuleSubject(): if the stored rule subject unescapes back to a literal that is byte-for-byte equal to the command, treat it as a match before handing the string to picomatch. Genuine glob rules (which contain no escaped metacharacters) are unchanged and still fall through to globMatch, so wildcard rules keep working.

unescapeRuleSubjectLiteral(pattern) === value || globMatch(value, pattern)

Tests

Added regression coverage in packages/agent-core/test/agent/permission.test.ts for Bash commands containing quotes + parentheses and quotes + pipes, asserting that:

  • the second identical call reuses the session approval (no re-prompt), and
  • a changed command still prompts.

Verified locally: @moonshot-ai/agent-core suite 2495 passed / 0 failed, full monorepo pnpm test 5995 passed / 0 failed, typecheck and lint clean.

MoonshotAI#325)

Session-level "Approved for this session" stopped working for Bash commands
containing quotes/parens/pipes: the command would be re-prompted every time.

Root cause: escapeRuleSubjectLiteral() backslash-escapes glob metacharacters
when storing the approval rule, but matchesGlobRuleSubject() re-matches the
stored pattern through picomatch (globMatch). picomatch drops the double quotes
adjacent to the escaped parens, so e.g. the stored rule
  Bash(python -c "print\('1'\)")
never matches the command it was created from
  python -c "print('1')"
and the user is asked to approve the identical command again.

Fix: add an exact-literal fast path in matchesGlobRuleSubject() — if the stored
rule subject unescapes back to a literal byte-for-byte equal to the command,
treat it as a match before handing the string to picomatch. Genuine glob rules
(no escaped metacharacters) are unchanged and still fall through to globMatch.

Adds regression tests covering quoted parentheses and quoted pipelines that
assert the second identical call reuses the session approval (no re-prompt) and
that a changed command still prompts.

Change-Id: I04846520621185bc0848cc92b3d01a1979de37a6
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 6, 2026

🦋 Changeset detected

Latest commit: a7f4f22

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@moonshot-ai/agent-core Patch
@moonshot-ai/kimi-code-sdk Patch
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

审批模式 Approved for session 失效

1 participant