Skip to content

fix: apply configured permission rules when an agent starts - #4757

Merged
7Sageer merged 1 commit into
mainfrom
fix/load-permission-rules
Nov 9, 2026
Merged

7Sageer merged 1 commit into
mainfrom
fix/load-permission-rules

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Users who wrote [[permission.rules]] in ~/.kimi-code/config.toml expected those rules to gate tool calls. The file loaded, but deny, ask, and allow rules never ran, in print mode and in the interactive TUI, including subagents. This PR makes a newly created or restored agent apply the rules already parsed from config.

Bug Reproduction Steps

  1. Put a deny rule in ~/.kimi-code/config.toml:
[[permission.rules]]
decision = "deny"
pattern = "Read(/path/to/secret.txt)"
  1. Start a new session, or run kimi -p, and ask the agent to read that file.
  2. Before this change the Read proceeds with no denial and no prompt. After this change the matching tool call is denied by the user-configured rule.

Root Cause

[[permission.rules]] is parsed into the config section, and the permission policy chain already consults IAgentPermissionRulesService.rules. Nothing copied the parsed rules into that list. addRules() had no production caller, so the list stayed empty and every user-configured policy missed. This is a fundamental fix: agent creation now copies the parsed rules into the list the policies already read.

Code Changes

Agent creation already copies default_permission_mode into the agent after wire restore. The same step now copies permission.rules.

 bindBootstrap
   bind profile
   apply default permission mode unless the session already chose one
+  copy permission.rules from config into the agent rule list

Subagents, forks, and /reload all go through AgentLifecycleService.create, so they pick up the same copy. Rules are not written to the wire log, so a restored session reads the current config instead of replaying a stale copy. An already-open session does not see a config edit until it is created again (/reload, or a new kimi -p run).

Start at packages/agent-core-v2/src/session/agentLifecycle/agentLifecycleService.ts. The policy chain and the pattern matcher are unchanged.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
[[permission.rules]] deny / ask / allow on tool calls Parsed, then ignored. Print mode and the TUI both fall through to the mode default. Applied when the agent is created or restored, including subagents. Users who wrote rules and observed that they did nothing. The documented behavior was that the rules apply. Remove the rules from config.toml.
Open session after editing config.toml Rules still ignored. Rules still unchanged until the session is created again. Nobody, the rules were already inert. /reload, or start a new session.

Affected modules: agent creation in agent-core-v2, consumed by the CLI (TUI and kimi -p), desktop, web, and ACP through the same server. Tests cover a new agent, a subagent, and a restored agent loading the current rules. The existing policy-chain tests already deny, ask, and allow once the rule list is populated.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

config.toml rules were parsed and then never copied into the rule list
the permission policies read, so deny, ask, and allow never ran.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 09:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@7Hanrui

7Hanrui commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T09:38:56.050870Z b8a4c67 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8a4c67421

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +434 to +436
const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules;
if (rules === undefined || rules.length === 0) return;
handle.accessor.get(IAgentPermissionRulesService).addRules(rules);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Honor session-runtime rules loaded from config

When a configured rule uses the documented scope = "session-runtime", this call adds it to the rule service, but evaluateUserConfiguredRule filters rules to turn-override, project, and user (permissionPolicy/policies/user-configured-rule.ts:14-18), while the session-history policy reads only separately recorded approval patterns. Consequently, a configured deny or ask rule with this accepted scope is silently ignored; either reject this scope in the config schema or include it when evaluating configured rules.

AGENTS.md reference: AGENTS.md:L129-L131

Useful? React with 👍 / 👎.

Comment on lines +434 to +436
const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules;
if (rules === undefined || rules.length === 0) return;
handle.accessor.get(IAgentPermissionRulesService).addRules(rules);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve first-match ordering for configured rules

Loading the whole ordered config array into the existing policy chain does not provide the documented “first matching rule takes effect” behavior: permissionPolicyService.ts:39-49 evaluates every matching deny before every ask and allow, regardless of array position. For overlapping rules such as an early allow Bash followed by ask Bash, users get a prompt instead of the promised allow, so the rules need a unified ordered evaluator or the configuration contract must explicitly describe and validate decision precedence.

AGENTS.md reference: AGENTS.md:L129-L131

Useful? React with 👍 / 👎.

Comment on lines +434 to +436
const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules;
if (rules === undefined || rules.length === 0) return;
handle.accessor.get(IAgentPermissionRulesService).addRules(rules);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Let ask rules override auto-mode approval

When default_permission_mode = "auto" and the loaded config contains a matching ask rule, the rule still never gates the call: permissionPolicyService.ts:39-48 evaluates the unconditional AutoModeApprovePermissionPolicyService before UserConfiguredAskPermissionPolicyService, and the auto policy approves every tool. TUI and print-mode users therefore silently approve calls they explicitly configured to require confirmation, so configured ask rules must run before auto-mode approval while retaining deny precedence.

AGENTS.md reference: AGENTS.md:L129-L131

Useful? React with 👍 / 👎.

Comment on lines +434 to +436
const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules;
if (rules === undefined || rules.length === 0) return;
handle.accessor.get(IAgentPermissionRulesService).addRules(rules);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reapply configured rules after undo restores state

Configured rules are added through the live-only PermissionRulesAdd event, and the existing replay test confirms that restoring state rebuilds rules as empty; conversation undo then calls dispatcher.restore() in undoService.ts:128-136, whose rerun path resets every replayable state key before replaying durable records (eventDispatcherService.ts:783-795,861-865). After the first /undo, all configured deny, ask, and allow rules therefore silently stop applying until the agent is recreated, so these rules must be reapplied after every replay or stored outside replay-reset state.

AGENTS.md reference: AGENTS.md:L129-L131

Useful? React with 👍 / 👎.

@changeset-bot

changeset-bot Bot commented Nov 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b8a4c67

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

This PR includes changesets to release 1 package
Name Type
@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

@7Sageer
7Sageer merged commit c4f81f8 into main Nov 9, 2026
17 checks passed
@7Sageer
7Sageer deleted the fix/load-permission-rules branch November 9, 2026 12:27
@AgenticThinkingUK

Copy link
Copy Markdown

Thanks for fixing this. We reported this behaviour to Moonshot on 9 October and again as GHSA-cmmx-h9w3-462x on 8 November. Our retest confirms that deny, ask and write rules are now enforced (fixed in 2.6.0, tested on 2.6.2).

Could you set 2.6.0 as the patched version, request a CVE through GitHub and publish the advisory, so people still on 2.5.2 or earlier know they need to upgrade?

Leo Ruocco, Agentic Thinking (agenticbench.org)

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.

5 participants