agentleFS
Microsoft AzureKnown publisherAzure/azure-api-management-policy-toolkit/.github/skills/policy-compilation/SKILL.md

policy-compilation

Skill95 starsChanged 12 months agoMITView on GitHub

Conventions and patterns for creating policy compilers in the Azure API Management policy toolkit. Use this skill when creating or modifying compiler classes in src/Core/Compiling/Policy/.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this for policies that take a config object parameter.

    line 14
  2. Very specific:

    Use this for policies with 1–2 simple parameters and no optional fields (e.g., SetMethod, FindAndReplace).

    line 67
  3. Very specific:

    All utility methods live in src/Core/Compiling/CompilerUtils.cs and src/Core/Compiling/SyntaxExtensions.cs.

    line 104
  4. Distinctive:

    If a specific policy requires namespace prefixes or explicit schema references, this must be specified during authoring Phase 1.

    line 187

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Policy Compilation Patterns

This skill describes how to implement compilers that convert authoring models to XML in the Azure API Management policy toolkit.

Compiler Class — src/Core/Compiling/Policy/{PolicyName}Compiler.cs

Config-Based Compiler Template (Most Common)

Use this for policies that take a config object parameter.

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Xml.Linq;

using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring;
using Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class {PolicyName}Compiler : IMethodPolicyHandler
{
    public string MethodName => nameof(I{Section}Context.{MethodName});

    public void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node)
    {
        if (!node.TryExtractingConfigParameter<{PolicyName}Config>(context, "{xml-element-name}", out var values))
        {
            return;
        }

        var element = new XElement("{xml-element-name}");

        // Required attributes — report diagnostic and return on failure
        if (!element.AddAttribute(values, nameof({PolicyName}Config.{RequiredProp}), "{xml-attr-name}"))
        {
            context.Report(Diagnostic.Create(
                CompilationErrors.RequiredParameterNotDefined,
                node.GetLocation(),
                "{xml-element-name}",
                nameof({PolicyName}Config.{RequiredProp})
            ));
            return;
        }

        // Optional attributes — no error reporting needed
        element.AddAttribute(values, nameof({PolicyName}Config.{OptionalProp}), "{xml-attr-name}");

        // Child elements — iterate sub-configs if applicable
        // (see "Handling Child Elements" below)

        context.AddPolicy(element);
    }
}

Direct-Parameter Compiler Template

Use this for policies with 1–2 simple parameters and no optional fields (e.g., SetMethod, FindAndReplace).

public class {PolicyName}Compiler : IMethodPolicyHandler
{
    public string MethodName => nameof(I{Section}Context.{MethodName});

    public void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node)
    {
        if (node.ArgumentList.Arguments.Count != {expectedCount})
        {
            context.Report(Diagnostic.Create(
                CompilationErrors.ArgumentCountMissMatchForPolicy,
                node.ArgumentList.GetLocation(),
                "{xml-element-name}"));
            return;
        }

        var value = node.ArgumentList.Arguments[0].Expression.ProcessParameter(context);
        context.AddPolicy(new XElement("{xml-element-name}", value));
    }
}

Auto-Registration

Compiler classes do not need manual DI registration. The CompilerModule.AddMethodPolicyHandlers() method in src/Core/IoC/CompilerModule.cs uses reflection to find all types matching:

  • IsClass: true
  • IsAbstract: false
  • IsPublic: true
  • Namespace: "Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy"
  • Implements IMethodPolicyHandler

Just ensure your compiler class meets all these criteria and it will be auto-registered.

Key Utility Methods

All utility methods live in src/Core/Compiling/CompilerUtils.cs and src/Core/Compiling/SyntaxExtensions.cs. You do not need to modify these files — just use their APIs.

TryExtractingConfigParameter<T>

Extracts the config object from the method invocation. Returns a dictionary of property name → InitializerValue. Returns false and reports diagnostics if extraction fails.

AddAttribute

Extension method on XElement. Adds an XML attribute from the values dictionary:

  • Property name to XML attribute conversion: C# property names are converted to kebab-case XML attribute names automatically. For example:
    • RenewalPeriod → renewal-period
    • RetryAfterHeaderName → retry-after-header-name
    • The conversion happens in the AddAttribute helper; you supply the kebab-case name in the compiler call.
  • Expression handling: Automatically wraps expression values in @(...) when the value comes from an expression method.
  • Return value: true if the attribute was added, false if the key was not present (property not set).

GenericCompiler.HandleList

Utility in src/Core/Compiling/Policy/GenericCompiler.cs for compiling string arrays into repeated child elements:

GenericCompiler.HandleList(element, values, nameof(Config.Claims), "required-claims", "claim");
// Produces: <required-claims><claim>value1</claim><claim>value2</claim></required-claims>

Used by ValidateJwtCompiler and ValidateAzureAdTokenCompiler.

ProcessParameter

Extension method on ExpressionSyntax. Processes a raw argument into its string value, handling both constants and expressions. Used in direct-parameter compilers.

Handling Child Elements

Typed Sub-Config Arrays

For child elements backed by sub-config records (e.g., ApiRateLimit[]):

if (values.TryGetValue(nameof({PolicyName}Config.{ChildArray}), out var items))
{
    foreach (var item in items.UnnamedValues!)
    {
        var childElement = new XElement("{child-element-name}");
        var childValues = item.NamedValues!;

        childElement.AddAttribute(childValues, nameof({SubConfig}.{Prop}), "{attr-name}");
        // ... more attributes

        element.Add(childElement);
    }
}

String Arrays

For simple string arrays (e.g., IP addresses):

if (values.TryGetValue(nameof({PolicyName}Config.{StringArray}), out var items))
{
    foreach (var item in items.UnnamedValues!)
    {
        element.Add(new XElement("{child-element-name}", item.Value!));
    }
}

Diagnostics

Error reporting uses descriptors from src/Core/Compiling/Diagnostics/CompilationErrors.cs. Common ones:

DescriptorWhen to Use
RequiredParameterNotDefinedA required attribute/property is missing
RequiredParameterIsEmptyA required array is present but empty
AtLeastOneOfTwoShouldBeDefinedAt least one of two mutually supportive properties must be set
OnlyOneOfTwoShouldBeDefinedExactly one of two mutually exclusive properties must be set
PolicyArgumentIsNotOfRequiredTypeA child element has the wrong type
ArgumentCountMissMatchForPolicyWrong number of arguments in direct-parameter compilers

XML Namespaces

  • By default, compiled XML policies do not include xmlns attributes. XML elements use the element name directly (e.g., <rate-limit />), and the namespace context is managed at the gateway level.
  • If a specific policy requires namespace prefixes or explicit schema references, this must be specified during authoring Phase 1. Handle with care and test thoroughly.

IReturnValueMethodPolicyHandler

A second compiler interface exists for policies that return a value assigned to a variable (e.g., AuthenticationManagedIdentity returning a token). This interface is not auto-registered by CompilerModule and the syntax compiler for it (LocalDeclarationStatementCompiler) is currently commented out in CompilerModule.cs. If you encounter a policy that assigns its result to a variable, flag this to the user as a special case requiring manual wiring.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.