Class DocumentGuardrailSource
- Namespace
- IntentSharp.Guardrails
- Assembly
- IntentSharp.Guardrails.dll
The guardrail source over rule files in an IDocumentStore: the rule files a manifest document lists, each one rule set, with the rules of its compiled companion after its own. Pass it to WithGuardrails(IGuardrailSource, params string[]), or let WithGuardrailFiles(IntentClientBuilder, IDocumentStore, params string[]) or AddGuardrails(IServiceCollection, IDocumentStore, params string[]) create it. Every document is versioned by the store, so an edit applies to the next check and a revert brings the previous rules back.
public sealed class DocumentGuardrailSource : IGuardrailSource, IGuardrailShadowSource, IDisposable
- Inheritance
-
objectDocumentGuardrailSource
- Implements
-
IDisposable
- Inherited Members
-
object.Equals(object)object.Equals(object, object)object.GetHashCode()object.GetType()object.ReferenceEquals(object, object)object.ToString()
- Extension Methods
Remarks
The manifest (Manifest, intentsharp.guardrails.json by default, or its
YAML spelling) is the exact list: { "intentsharp": "guardrails/1", "documents": ["rules/orders.rules.yaml"] }.
No folder is ever scanned. A rule file's companion is found from its path (rules/orders.rules.yaml has
rules/orders.rules.compiled.json); a companion that names another set or another rule file is ignored.
Loading never throws for content and keeps last good versions (SI-6): a listed document that is no longer in the
store, cannot be read, does not parse or does not bind keeps the rules of its last good version, with the problem in
Problems and the log; at the first load such a document adds no set. So does the manifest, whose
last good list stays when a new version does not load. With no manifest at the first load there are no sets and one
problem. A set name defined by two listed documents is served as the setting rules.ambiguousSet says: by
neither (serveNeither, its fail-closed value), with a problem that names both, so every check that names the
set is refused until one definition is removed; or by the one listed first (firstWins), the later one being a
problem. The source reads that setting from the settings document in its store (intentsharp.settings.json, or
its YAML spelling), else from CodeSettings; a rule file cannot set it. Every set
carries its document, the store's version sequence and the content hash, which every decision stamps.
Current reads the store at its first use, blocking on the store's calls (an InMemoryDocumentStore completes synchronously and a FolderDocumentStore reads local files), and again on the first use after the store reports a change. Nothing reloads in the background and the source holds no callback on the store, so a source nobody uses any more reads nothing and is collected with the client that held it. A pinned document (ExpectedHashes) whose hash differs makes Current throw a GuardrailLoadException naming both hashes, so every check that names a set is refused rather than run the change. Safe to share between threads.
It is an IGuardrailShadowSource, so a client can run other versions of the listed rule files side by
side (Shadows): a version is read from the store by its sequence and bound as the
running version is, with the running compiled companion only when that companion was compiled from this version
(else with its authored rules alone). A version of a document the manifest does not list, or one that is not in the
store or does not load, compares nothing and is logged at Warning once. Each compared check is counted in the state
value trial/<document path>/<sequence>:
{ "checks": n, "differing": n, "byRule": { "<set>/<rule id>": n }, "since": time, "last": time },
which names no subject value and is advisory.
Constructors
- DocumentGuardrailSource(IDocumentStore, GuardrailSourceOptions?, ILogger<DocumentGuardrailSource>?)
Creates the source over the rule files
storeholds. Nothing is read until Current is first used.
Properties
- Current
The rule sets as they are now. The first use loads, blocking on the store; a later use returns the published snapshot, reading the store again only after it reported a change.
- Problems
The problems of the latest load: documents that did not load and keep their last good version, a missing or duplicated manifest, set names defined twice, companions that were ignored, and pinned documents whose hash differs. Refreshed as Current is; empty when everything loaded.
Methods
- Dispose()
Stops reading the store. Current keeps returning the last snapshot without reading the store again, and ReloadAsync(CancellationToken) throws ObjectDisposedException. The source holds no callback on the store, so a source that is never disposed leaves nothing behind either. The store is not disposed: it is the caller's. Calling it more than once is safe.
- GetChangeToken()
A token that fires once, after a load publishes a different snapshot: a use of Current or Problems after the store reported a change, or ReloadAsync(CancellationToken). Nothing reloads in the background, so a store change alone does not fire it; to be told of one without a check, follow the store's own change token and call ReloadAsync(CancellationToken). Take a new token after it fires.
- ReloadAsync(CancellationToken)
Reads the manifest and every listed document from the store now, whether or not the store reported a change, and returns the snapshot.