Skip to content

fix(agent-core-v2): load config permission rules into agents at bootstrap - #2970

Open
N123999 wants to merge 3 commits into
MoonshotAI:mainfrom
N123999:fix/v2-load-config-permission-rules
Open

fix(agent-core-v2): load config permission rules into agents at bootstrap#2970
N123999 wants to merge 3 commits into
MoonshotAI:mainfrom
N123999:fix/v2-load-config-permission-rules

Conversation

@N123999

@N123999 N123999 commented Aug 16, 2026

Copy link
Copy Markdown

Related Issue

Closes #2964

Problem

Since 0.33.0 the interactive TUI, kimi -p, and kimi acp run on the agent-core-v2 engine by default. Under v2, [[permission.rules]] in ~/.kimi-code/config.toml (e.g. { decision = "allow", pattern = "Bash(ls*)" }) has no effect at all — every matching Bash call still prompts for approval. Root cause, in three parts:

  1. The v2 permission config section has a complete schema and TOML transforms and self-registers at module load (packages/agent-core-v2/src/agent/permissionRules/configSection.ts), but after registration it has no consumer anywhere in the repo.
  2. IAgentPermissionRulesService.addRules has no production call site (only tests call it). Agent materialization replays only persisted records via wire.restore(), and permission.rules.add is a transient op, so after restore the rules model is always empty.
  3. With the rules model empty, the user-configured-allow/deny/ask policies can never match, and unmatched calls fall through to fallback-ask at the end of the policy chain.

v1 had this wiring: session create/resume passed config.permission?.rules into the PermissionManager. The v2 port dropped it.

What changed

One change point: AgentLifecycleService.bindBootstrap (packages/agent-core-v2/src/session/agentLifecycle/agentLifecycleService.ts). After wire.restore(), it now reads the permission section through IConfigService and applies rules via IAgentPermissionRulesService.addRules, mirroring the existing defaultPermissionMode block in the same function.

Why the engine-side bootstrap point rather than host-side injection:

  1. Single point, full coverage. TUI / kimi -p / kimi acp / kimi web all run the engine in-process, and session create/resume converges on IAgentLifecycleService.createbindBootstrap; subagents (Agent tool), btw forks, and session forks are materialized through the same entry. Host-side injection would need touch points in node-sdk, acp-server, and kap-server, and would still miss agents created inside the engine.
  2. Fits the v2 config model. Each config section is read by its consumer through IConfigService, never passed around as a config bag via options; defaultPermissionMode and defaultPlanMode are existing precedents of the engine applying a config section at bootstrap.
  3. Resume semantics match v1. permission.rules.add is a transient op, so the rules model starts empty on restore and bindBootstrap re-reads the current config.toml — rule edits between runs take effect on the next resume, injection happens exactly once per materialization, and the approval memory replayed from persisted permission.record_approval_result records is untouched.
  4. Subagents inherit automatically, since they go through the same createbindBootstrap path — the net effect matches v1, where subagents shared the parent's PermissionManager.

Tests: extended packages/agent-core-v2/test/session/agentLifecycle/agentLifecycle.test.ts — rules from config are loaded into the agent on create; addRules is not called when the section is missing or the list is empty; on resume the config rules are re-injected while the restored session-approval patterns are preserved (and nothing is re-persisted); and a config-sourced deny rule actually blocks a matching tool call through the real policy chain (user-configured-deny).

Screenshots

Before (0.36.1): ls prompts for approval despite [[permission.rules]] allowing Bash(ls*). After (this branch): the same prompt runs ls directly, no approval dialog.

Before (0.36.1) After (this branch)
kimi-code-approval-before-fix kimi-code-approval-after-fix

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update. (No doc update needed — the fix restores the already-documented [[permission.rules]] behavior.)

@changeset-bot

changeset-bot Bot commented Aug 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7c51047

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

@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: 3db334f17b

ℹ️ 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 thread packages/agent-core-v2/test/session/agentLifecycle/agentLifecycle.test.ts Outdated
@N123999
N123999 force-pushed the fix/v2-load-config-permission-rules branch 2 times, most recently from 1548319 to 34c35b5 Compare August 16, 2026 13:26
@N123999

N123999 commented Aug 16, 2026

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 34c35b5088

ℹ️ 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".

…trap

The `permission` config section (`[[permission.rules]]`) had a schema and
TOML transforms but no consumer: nothing ever called
`IAgentPermissionRulesService.addRules` in production, and since
`permission.rules.add` is a transient op, a restored agent's rules model
always started empty. Every Bash call therefore fell through to the
`fallback-ask` policy even when the user had configured allow/deny rules.

Apply the configured rules in `AgentLifecycleService.bindBootstrap`, right
after `wire.restore()`, mirroring the existing `defaultPermissionMode`
handling. Every v2 agent creation path (main agent, subagent, btw fork,
session fork; TUI / kimi -p / kimi acp / kimi web) converges on this point,
so config rules are injected exactly once per materialization and re-read
from config.toml on resume, without touching the restored session-approval
memory carried by persisted `permission.record_approval_result` records.

Closes MoonshotAI#2964
@N123999
N123999 force-pushed the fix/v2-load-config-permission-rules branch from 34c35b5 to 7c51047 Compare August 17, 2026 11:50
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.

kimicode权限规则失效

1 participant