policy-codebase-reference
Skill95 starsChanged 12 months agoMITView on GitHub
Reference guide for the Azure API Management policy toolkit codebase structure. Use this skill when you need to find existing policies, infrastructure, or naming conventions.
In this repository6 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- policy-authoringSkill
- policy-compilationSkill
- policy-emulatorSkill
- policy-emulator-testingSkill
- policy-testingSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 2 are distinctive, and 1 is 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.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 79
Every new .cs file must start with:
- Distinctive: line 88
When implementing a new policy, choose the closest structural match as your reference:
- Distinctive: line 159
When implementing a new emulator handler, choose the closest structural match:
≈ “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.
Files
Policy Toolkit Codebase Reference
This skill provides an inventory of the codebase structure, shared infrastructure, reference policy selection, and naming conventions for the Azure API Management policy toolkit.
File Path Conventions
Authoring & Compilation
| Artifact | Location | Example |
| Config record | src/Authoring/Configs/{PolicyName}Config.cs | RateLimitConfig.cs |
| Document attribute | src/Authoring/Attributes/DocumentAttribute.cs | — |
| Expression attribute | src/Authoring/Attributes/ExpressionAttribute.cs | — |
| Expression-allowed attribute | src/Authoring/Attributes/ExpressionAllowedAttribute.cs | — |
| Section interfaces | src/Authoring/I{Section}Context.cs | IInboundContext.cs |
| Compiler class | src/Core/Compiling/Policy/{PolicyName}Compiler.cs | RateLimitCompiler.cs |
| Compiler test class | test/Test.Core/Compiling/{PolicyName}Tests.cs | RateLimitTests.cs |
| Available policies doc | docs/AvailablePolicies.md | — |
| Compiler utilities | src/Core/Compiling/CompilerUtils.cs | — |
| Syntax extensions | src/Core/Compiling/SyntaxExtensions.cs | — |
| Diagnostics | src/Core/Compiling/Diagnostics/CompilationErrors.cs | — |
| IoC / auto-registration | src/Core/IoC/CompilerModule.cs | — |
| Test initialization | test/Test.Core/CompilerTestInitialize.cs | — |
| Assertion helpers | test/Test.Core/Assertions/ | CompilationResultAssertion.cs |
| Generic compiler utilities | src/Core/Compiling/Policy/GenericCompiler.cs | — |
| Add-policy guide | docs/AddPolicyGuide.md | — |
Gateway Emulator
| Artifact | Location | Example |
| Emulator handler class | src/Testing/Emulator/Policies/{PolicyName}Handler.cs | SetHeaderHandler.cs |
| Handler base classes | src/Testing/Emulator/Policies/PolicyHandler.cs | — |
| IPolicyHandler interface | src/Testing/Emulator/IPolicyHandler.cs | — |
| Section attribute | src/Testing/Emulator/Policies/SectionAttribute.cs | — |
| Argument extensions | src/Testing/Emulator/Policies/ArgumentsExtensions.cs | — |
| Policy exception | src/Testing/Emulator/PolicyExeption.cs | Note: filename has typo |
| SectionContextProxy | src/Testing/Emulator/SectionContextProxy.cs | — |
| GatewayContext | src/Testing/GatewayContext.cs | — |
| TestDocument | src/Testing/TestDocument.cs | — |
| Emulator test class | test/Test.Testing/Emulator/Policies/{PolicyName}Tests.cs | SetHeaderTests.cs |
| Emulator policy checklist | docs/EmulatorPolicyChecklist.md | — |
Naming Conventions
Authoring & Compilation
| Artifact | Convention | Example |
| Config record | {PolicyName}Config | RateLimitConfig |
| Sub-config record | {DescriptiveName} | ApiRateLimit, AddressRange |
| Compiler class | {PolicyName}Compiler | RateLimitCompiler |
| Compiler test class | {PolicyName}Tests | RateLimitTests |
| Context method | {PolicyName} | RateLimit |
| XML element name | kebab-case | rate-limit |
| Config namespace | Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring | — |
| Compiler namespace | Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy | — |
| Compiler test namespace | Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling | — |
Gateway Emulator
| Artifact | Convention | Example |
| Handler class | {PolicyName}Handler | SetHeaderHandler |
| Emulator test class | {PolicyName}Tests | SetHeaderTests |
| PolicyName property | PascalCase method name from section interface | SetHeader |
| Handler namespace | Microsoft.Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies | — |
| Emulator test namespace | Test.Emulator.Emulator.Policies | — |
Note:
JsonToXmlHandle.csis a naming inconsistency in the source (missing therinHandler). Do not rename it.
Copyright Header
Every new .cs file must start with:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.Reference Policy Selection Guide
When implementing a new policy, choose the closest structural match as your reference:
| Policy Structure | Reference Config | Reference Compiler | Reference Tests |
| Simple attributes only | RateLimitByKeyConfig | RateLimitByKeyCompiler | RateLimitByKeyTests |
| Attributes + child elements | RateLimitConfig | RateLimitCompiler | RateLimitTests |
| Attributes + typed child arrays | IpFilterConfig | IpFilterCompiler | IpFilterTests |
| String-list child elements | ValidateJwtConfig | ValidateJwtCompiler | ValidateJwtTests |
| Complex nested configs | CorsConfig | CorsCompiler | CorsTests |
| Direct parameters (no config) | — | SetMethodCompiler | SetMethodTests |
Shared Infrastructure Inventory
Expression Support
[Document]attribute (src/Authoring/Attributes/DocumentAttribute.cs) — Marks a class as a policy document. Optionalnameparameter;ScopeandTypeproperties control document scope and type.[Expression]attribute (src/Authoring/Attributes/ExpressionAttribute.cs) — Marks a method as a policy expression helper. Captures source file path via[CallerFilePath].[ExpressionAllowed]attribute (src/Authoring/Attributes/ExpressionAllowedAttribute.cs) — Marks properties or parameters that accept policy expressions.Expression<T>delegate (src/Authoring/Expression.cs) —delegate T Expression<out T>(IExpressionContext context).IHaveExpressionContextinterface (src/Authoring/IHaveExpressionContext.cs) — Base interface for all section contexts, providingIExpressionContext ExpressionContext { get; }.
Compiler Infrastructure
IMethodPolicyHandlerinterface (src/Core/Compiling/IMethodPolicyHandler.cs) — The primary compiler interface. Properties:string MethodName { get; }. Method:void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node).IReturnValueMethodPolicyHandlerinterface (src/Core/Compiling/IReturnValueMethodPolicyHandler.cs) — For policies returning a value. Currently disabled (theLocalDeclarationStatementCompileris commented out inCompilerModule.cs).- Auto-registration (
src/Core/IoC/CompilerModule.cs) — Uses reflection to find and register all public, non-abstract classes in theMicrosoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policynamespace that implementIMethodPolicyHandler. No manual DI wiring needed for standard compilers. CompilerUtils(src/Core/Compiling/CompilerUtils.cs) — Static utility class with methods for parameter extraction (ProcessParameter,TryExtractingConfig), expression processing, and syntax normalization. Also definesInitializerValuefor property name → value mapping.SyntaxExtensions(src/Core/Compiling/SyntaxExtensions.cs) — Extension methods for Roslyn syntax trees: attribute detection (ContainsAttributeOfType<T>), document class discovery (GetDocumentAttributedClasses), and document metadata extraction.GenericCompiler.HandleList(src/Core/Compiling/Policy/GenericCompiler.cs) — Utility for compiling string arrays into repeated child XML elements.CompilationErrors(src/Core/Compiling/Diagnostics/CompilationErrors.cs) — Static class withDiagnosticDescriptorfields for all compilation error types.
Compiler Test Infrastructure
CompilerTestInitialize(test/Test.Core/CompilerTestInitialize.cs) — Assembly-level MSTest setup. Creates Roslyn compilation with references toSystem,System.Xml.Linq, and the Authoring assembly. Provides theCompileDocument()extension method.CompilationResultAssertion(test/Test.Core/Assertions/CompilationResultAssertion.cs) — FluentAssertions extension providingBeSuccessful()andDocumentEquivalentTo(string expectedXml).Usings.cs(test/Test.Core/Usings.cs) — Global usings:FluentAssertions,Microsoft.VisualStudio.TestTools.UnitTesting, assertion helpers, test extensions.
Emulator Infrastructure
IPolicyHandlerinterface (src/Testing/Emulator/IPolicyHandler.cs) — The handler interface. Properties:string PolicyName { get; }. Method:object? Handle(GatewayContext context, object?[]? args).PolicyHandler<T>base classes (src/Testing/Emulator/Policies/PolicyHandler.cs) — Three generic variants: single config, optional config, and two-parameter. All includeCallbackSetupfor test mocking.SectionContextProxy<T>(src/Testing/Emulator/SectionContextProxy.cs) — UsesSystem.Reflection.DispatchProxyto dynamically route method calls to handlers. Discovers handlers via reflection on namespace +[Section]attribute.[Section]attribute (src/Testing/Emulator/Policies/SectionAttribute.cs) — Constructor takesstring scope(usenameof(IInboundContext), etc.). Multiple attributes can be stacked.ArgumentsExtensions(src/Testing/Emulator/Policies/ArgumentsExtensions.cs) — Static utility for safe argument extraction fromobject?[]?:ExtractArgument<T>()(required single arg),ExtractOptionalArgument<T>()(nullable), andExtractArguments<T1, T2>()(two-param handlers).PolicyException(src/Testing/Emulator/PolicyExeption.cs) — Exception thrown when a handler fails. CarriesPolicy,Section, andPolicyArgscontext. Note: filename has a typo (Exeption) — do not rename.GatewayContext(src/Testing/GatewayContext.cs) — Central mock object with section proxies, mock stores (CertificateStore, CacheStore, ResponseExampleStore, LoggerStore), and expression context.TestDocument(src/Testing/TestDocument.cs) — Orchestrates test execution across policy sections. WrapsIDocumentand calls section methods through proxies.FinishSectionProcessingException— Thrown by handlers (e.g., MockResponse, ReturnResponse) to terminate section processing early.
Emulator Test Infrastructure
.AsTestDocument()extension — CreatesTestDocumentfromIDocumentfor fluent test setup.SetupInbound()/SetupOutbound()/etc. — ReturnsMockPoliciesProvider<TSection>for mock configuration.Usings.cs(test/Test.Testing/Usings.cs) — Global usings forFluentAssertionsand MSTest.
Section Interfaces
Policies can be available in one or more pipeline sections:
| Section | Interface File | XML Section |
| Inbound | src/Authoring/IInboundContext.cs | <inbound> |
| Outbound | src/Authoring/IOutboundContext.cs | <outbound> |
| Backend | src/Authoring/IBackendContext.cs | <backend> |
| On-Error | src/Authoring/IOnErrorContext.cs | <on-error> |
| Fragment | src/Authoring/IFragmentContext.cs | N/A (reusable fragment) |
Note: IFragmentContext duplicates method signatures from other interfaces (see //TODO at top of file). Copy signatures verbatim when adding policies there.
Emulator Reference Handler Selection Guide
When implementing a new emulator handler, choose the closest structural match:
| Policy Structure | Reference Handler | Reference Test |
| Single config, simple state change | SetStatusHandler | SetStatusTests |
| Two parameters (name + values) | SetHeaderHandler | SetHeaderTests |
| Config with callback hooks only (no-op) | RateLimitHandler | RateLimitTests |
| Optional config | MockResponseHandler | MockResponseTests |
| Validation + short-circuit | CheckHeaderHandler | CheckHeaderTests |
| Authentication + token | AuthenticationManagedIdentityHandler | AuthenticationManagedIdentityHandlerTests |
| Cache interaction | CacheLookupValueHandler | CacheLookupValueTests |
| Logging/store interaction | LogToEventHubHandler | LogToEventHubTests |
| No-config (no-arg method) | BaseHandler | BaseTests |
| Custom flow control / wrapper | ReturnResponseHandler | — |
| AzureOpenAi variant (inherits Llm) | AzureOpenAiEmitTokenMetricHandler | — |
Common Pitfalls
| Pitfall | Rule |
Modifying .csproj files | Do not change Compiling.csproj or Testing.csproj unless explicitly required. Reformatting, reordering, or encoding changes (BOM, line endings) cause noisy diffs and will be rejected in review. |
Missing IFragmentContext entry | Every method added to a section interface (IInboundContext, IOutboundContext, etc.) must also be added to IFragmentContext.cs. See the authoring skill for details. |
| Missing expression tests | Every property marked [ExpressionAllowed] in a config must have a corresponding expression test [DataRow]. See the testing skill section 3 for the pattern. |
Build and Test Commands
# Build the entire solution
dotnet build
# Run all compiler tests
dotnet test --project test/Test.Core
# Run all emulator tests
dotnet test --project test/Test.Testing
# Run tests for a specific policy
dotnet test --filter "FullyQualifiedName~{PolicyName}Tests"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
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.

