Documentation
Table of Contents

Class RuleCompileHandler

Namespace
IntentSharp.Guardrails
Assembly
IntentSharp.Guardrails.dll

Compiles rules written as intent into a compiled companion: when a target's schema changes, or the intent: text of a rule file changes, the rule sets written for that target are compiled again by the model, through the client's maintenance role, into fixed rules of the kinds the client knows, and the result is submitted as rules/<name>.rules.compiled.json through the client's edit seam, so it takes effect by default. No model decides anything when an operation is checked: the gate evaluates the compiled rules like the authored ones. Compilation is off until the developer turns it on: register the handler with WithRuleCompilation(IntentClientBuilder, IDocumentStore, Action<RuleCompileOptions>?) on the builder, or with AddRuleCompilation(IServiceCollection, Action<RuleCompileOptions>?) beside AddGuardrails(IServiceCollection, IDocumentStore, params string[]) for a client built by AddIntentSharp.

[Experimental("INTENTSHARP001")]
public sealed class RuleCompileHandler : IEnvironmentChangeHandler
Inheritance
object
RuleCompileHandler
Implements
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.ReferenceEquals(object, object)
object.ToString()
Extension Methods

Remarks

What compiles: of the rule files the manifest lists, each set with an intent: and a target:. On a TargetSchema change of that target, a set whose compile lists target-change compiles when its companion's fingerprint differs from the target's new one, its source.intentHash differs from the hash of the current intent text, or it has no companion; a set whose compile lists intent-change compiles when the intent hash differs or it has no companion. On a Document change of a rule file, its set compiles when its compile lists intent-change and the intent hash differs, against the description a target runtime of the client received last; when no runtime of the client has described the target in this process, as at a start after the rule file was edited, the client raises the target's first description as a TargetSchema change (with equal fingerprints when the schema did not change), and the set compiles then. A change of a compiled companion never compiles anything, so a companion, written by this handler or by hand, cannot start a loop. A provider options change compiles nothing in this version: no shipped kind names a model.

Each compile takes one recompile under environment.maxRecompilesPerHour for its target and set first, and runs within environment.handlerTimeout. Its model call is checked at the Request and Reply checkpoints under the client's default sets and the set being compiled. The compile intent is the running version of defaults/intentsharp.guardrails.compile.json in the store when one loads (a developer or a model may replace its wording and output rules), else the one this package ships. The reply must pass the intent's output rules and the handler's own check, in library code because only it knows the current description: one kind the client knows per rule, a subject declared at the set's checkpoint, a pattern the non-backtracking engine accepts (a compiled pattern runs on it, so it matches in time linear in the value; a lookaround, a backreference, an atomic group or a conditional is refused), and on data.entities no allowed name the description does not list. "*" and "@described" pass: they are applied, and the rule file validator flags the companion that carries them as widening security, which a writer of your own may hold. The companion's rules run after the set's authored rules, and a deny always wins.

When a compile fails, is capped, or is held, rejected or refused by the edit seam, the version that ran before keeps running, a new entity stays denied, and the set is reported with ReportPending(string, string?), so environment.pendingRecompile counts it, at the change and at every later operation that names the set until a compile of it applies, and the operation's Maintenance names it. By default a later operation over the target tries such a compile again, within environment.maxRecompilesPerHour; with the setting environment.recompileRetry set to nextChange it runs again only when the target or the intent changes again, or at the next start, and with off only when the target or the intent changes again. A set name two listed rule files define compiles only when the rule file source serves it (rules.ambiguousSet). The model calls are reported with ReportMaintenance(AIResultBase), apart from the operation's own. The compile prompt renders the description's entities and fields, and only the properties the target lists for the model; a description never carries connection data (the target's rule). Calls are not recorded in the client's memory.

Experimental (INTENTSHARP001): the compile output and the companion format, rules-compiled/1, may change in a later version. Safe to share between threads.

Constructors

RuleCompileHandler(IDocumentStore, RuleCompileOptions?, ILogger<RuleCompileHandler>?)

Creates the handler over the rule files store holds.

Methods

HandleAsync(EnvironmentChange, EnvironmentChangeContext, CancellationToken)

Compiles the rule sets change concerns; see the class remarks.