-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: fire notification hooks for approvals #2284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| from tenacity import RetryCallState, retry_if_exception, stop_after_attempt, wait_exponential_jitter | ||
|
|
||
| from kimi_cli.approval_runtime import ( | ||
| ApprovalRuntimeEvent, | ||
| ApprovalSource, | ||
| get_current_approval_source_or_none, | ||
| reset_current_approval_source, | ||
|
|
@@ -209,8 +210,13 @@ def __init__( | |
| ] | ||
| self._hook_engine: HookEngine = HookEngine() | ||
| self._stop_hook_active: bool = False | ||
| self._approval_hook_subscription: str | None = None | ||
| if self.is_root: | ||
| self._runtime.notifications.ack_ids("llm", extract_notification_ids(context.history)) | ||
| if self._runtime.approval_runtime is not None: | ||
| self._approval_hook_subscription = self._runtime.approval_runtime.subscribe( | ||
| self._on_approval_runtime_event | ||
| ) | ||
|
Comment on lines
+213
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Approval runtime subscription is never unsubscribed, causing resource leak The subscription token is stored in The concrete impact is in Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| # Bind plan mode state to tools that support it | ||
| self._bind_plan_mode_tools() | ||
|
|
@@ -276,6 +282,40 @@ def set_hook_engine(self, engine: HookEngine) -> None: | |
| if isinstance(self._agent.toolset, KimiToolset): | ||
| self._agent.toolset.set_hook_engine(engine) | ||
|
|
||
| def _on_approval_runtime_event(self, event: ApprovalRuntimeEvent) -> None: | ||
| if event.kind != "request_created": | ||
| return | ||
|
|
||
| from kimi_cli.hooks import events | ||
|
|
||
| request = event.request | ||
| input_data = { | ||
| **events.notification( | ||
| session_id=self._runtime.session.id, | ||
| cwd=str(Path.cwd()), | ||
| sink="user", | ||
| notification_type="permission_prompt", | ||
| title=f"Approval required: {request.sender}", | ||
| body=request.description, | ||
| severity="warning", | ||
| ), | ||
| "request_id": request.id, | ||
| "tool_call_id": request.tool_call_id, | ||
| "sender": request.sender, | ||
| "action": request.action, | ||
| "description": request.description, | ||
| "source_kind": request.source.kind, | ||
| "source_id": request.source.id, | ||
| } | ||
| try: | ||
| self._hook_engine.fire_and_forget_trigger( | ||
| "Notification", | ||
| matcher_value="permission_prompt", | ||
| input_data=input_data, | ||
| ) | ||
| except Exception: | ||
| logger.exception("Failed to trigger approval notification hook") | ||
|
|
||
| def add_injection_provider(self, provider: DynamicInjectionProvider) -> None: | ||
| """Register an additional dynamic injection provider.""" | ||
| self._injection_providers.append(provider) | ||
|
|
@@ -507,6 +547,13 @@ def runtime(self) -> Runtime: | |
| def context(self) -> Context: | ||
| return self._context | ||
|
|
||
| def close(self) -> None: | ||
| if self._approval_hook_subscription is None: | ||
| return | ||
| if self._runtime.approval_runtime is not None: | ||
| self._runtime.approval_runtime.unsubscribe(self._approval_hook_subscription) | ||
| self._approval_hook_subscription = None | ||
|
|
||
| @property | ||
| def _context_usage(self) -> float: | ||
| if self._runtime.llm is not None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid registering a permanent approval-runtime subscriber in
KimiSoul.__init__without removing it later.ApprovalRuntime.subscribe()stores the bound callback strongly, and this class currently has no correspondingunsubscribe(), so temporary root souls (for example/init, which createsKimiSoul(soul.agent, ...)insrc/kimi_cli/soul/slash.py) accumulate listeners. After each/init, every new approval request will fire theNotificationhook multiple times and keep old soul instances alive in memory.Useful? React with 👍 / 👎.