send_ability_alert( $name, sprintf( /* translators: %s: ability name. */ __( 'A new ability "%s" was registered on your site. If you did not just install or update a plugin that provides it, investigate before agents can use it.', 'agentic-daisy-ai-agent-firewall' ), $name ) ); } /** * Alert on a changed ability definition (rug-pull signal). * * @param string $name Ability name. */ public function notify_ability_changed( string $name ): void { $this->send_ability_alert( $name, sprintf( /* translators: %s: ability name. */ __( 'The definition of ability "%s" changed. A benign tool being silently replaced is a known attack (tool poisoning); verify the change is expected.', 'agentic-daisy-ai-agent-firewall' ), $name ) ); } /** * Shared throttled sender for ability-drift alerts. * * @param string $name Ability name (throttle key). * @param string $body Email body. */ private function send_ability_alert( string $name, string $body ): void { if ( '1' !== (string) get_option( self::OPTION_EMAIL, '1' ) ) { return; } $throttle_key = 'adaf_anomaly_tool_' . substr( md5( $name ), 0, 20 ); if ( false !== get_transient( $throttle_key ) ) { return; } set_transient( $throttle_key, 1, self::THROTTLE ); wp_mail( (string) get_option( 'admin_email' ), __( '[Agent Firewall] Ability registry changed', 'agentic-daisy-ai-agent-firewall' ), $body ); } /** * Send the (throttled) alert email. * * @param int $agent_id Agent that exceeded its limit. * @param array $rule The matched limit rule. */ public function notify( int $agent_id, array $rule ): void { if ( '1' !== (string) get_option( self::OPTION_EMAIL, '1' ) ) { return; } $throttle_key = 'adaf_anomaly_mail_' . $agent_id; if ( false !== get_transient( $throttle_key ) ) { return; } set_transient( $throttle_key, 1, self::THROTTLE ); $limit = isset( $rule['limit'] ) && is_array( $rule['limit'] ) ? $rule['limit'] : array(); $writes = isset( $limit['writes'] ) ? (int) $limit['writes'] : 0; $window = isset( $limit['window'] ) ? (int) $limit['window'] : 0; $subject = sprintf( /* translators: %d: agent id. */ __( '[Agent Firewall] Rate limit exceeded by Agent #%d', 'agentic-daisy-ai-agent-firewall' ), $agent_id ); $body = sprintf( /* translators: 1: agent id, 2: allowed writes, 3: window in seconds. */ __( 'Agent #%1$d exceeded its write rate limit (%2$d writes per %3$d seconds) and further writes were denied. Review the Agent Firewall ledger; a runaway loop or a hijacked agent may be at work. Further alerts for this agent are muted for one hour.', 'agentic-daisy-ai-agent-firewall' ), $agent_id, $writes, $window ); wp_mail( (string) get_option( 'admin_email' ), $subject, $body ); } }