C API
Policy guide
Use the C manifests API: ownership, explanations and complete examples. Use the C rulesets API: ownership, explanations and complete examples.The generated Policy reference contains the complete declarations and individual API cards. This guide keeps the usage explanations and examples.
The opaque loader returns a maelys_datalog_policy_t *. It can parse one
inline source or a governed manifest containing several policies. This page
covers the stable calls in <maelys/datalog.h>. The manifest concept
explains why a policy set exists.
A ruleset is the logical program that the solver evaluates. The stable C API
does not expose a ruleset_t, parsed rule array, or mutable symbol table.
It loads source into an opaque maelys_datalog_policy_t * and prepares one
selected policy in an opaque session. This page maps the ruleset concept
to those public operations.
Types
Defined in the reference
| Type | Purpose |
|---|---|
policy_t | Opaque handle for the loaded policy set. Input handle for counting, identifying and releasing policies. |
policy_bundle_entry_t | Policy ID and source bytes supplied with an in-memory manifest. |
| policy_stat_t | Select a compiled count to read. |
Used from other pages
These types are defined on the linked pages; this page uses them in the roles below.
| Type | Use on this page | Defined in |
|---|---|---|
diagnostic_t | Optional output argument receiving loading details. | Errors |
Functions
| Function | Purpose |
|---|---|
diagnostic_initializer() | Create the optional diagnostic value before calling a loader. |
policy_load_inline() | Load one policy from source bytes. |
policy_load_manifest() | Load a verified policy set from files. |
policy_load_manifest_buffer() | Load the same manifest contract from supplied JSON and source bytes. |
policy_count() | Find the valid policy indices. |
policy_stat_get() | Read one policy's compiled predicate, fact or normalized-rule count. |
policy_fingerprint() | Identify the loaded set independently of request facts. |
policy_free() | Release the loaded set after preparing the sessions that need it. |
Initialize diagnostic_t with diagnostic_initializer() before passing its address to a loader. It sets the required size and ABI version; {0} alone does not. Pass NULL instead if no detailed error information is needed. The diagnostic is separate from the loaded policy handle.
Since 0.15.0, <maelys/datalog.h> also provides the in-memory buffer loader. The current header additionally declares policy_load_manifest_text(), with its historical default_profile=MAELYS-DATALOG-v2 contract. The v0.16 loading architecture records the earlier header placement.
Policy-set handle
Initialize the output pointer to NULL before either loader. On success it
owns one policy or a manifest set; release it with
policy_free()
when no new session needs it. Each loader returns a
status_t:
only STATUS_OK makes the output handle usable. On failure
there is no partial policy; inspect the status and, if supplied, the
diagnostic instead.
Manifest bundle entry
See the policy_bundle_entry_t reference.
Choose a loading path
The stable header offers four loading calls: inline source, a file manifest, a buffer manifest and manifest text with its historical profile contract. The other installed SDK headers include the same core declarations, so a check in both columns below denotes the same call, not a second implementation. The v0.16 loading architecture remains available as historical documentation.
| Path | Variant | C API | Extension API |
|---|---|---|---|
| Manifest | files on disk | ✓ | ✓ |
| Manifest | JSON and source in memory | ✓ | ✓ |
| Inline | source bytes; registered domain | ✓ | ✓ |
| Inline | register domain + load source in one call | ✕ | ✕ |
A green check means the installed SDK exposes that path; a red cross means it does not. In the last row, the crosses refer only to the missing combined call: a C application can already supply a static predicate table and source bytes in memory, using registration followed by inline loading. The two stable calls are illustrated below:
- Verified filespolicy_load_manifest
- Policy source bytespolicy_load_inline
The manifest-file path reads JSON metadata and policy files from disk; the manifest-buffer path borrows JSON and sources from the caller. Both verify the declared SHA-256 digests and apply the manifest's Public Query Whitelist. The inline-source path takes one policy's source bytes in memory without a manifest or whitelist. Both require a previously registered domain: the inline call names it directly; the manifest names it in JSON. Neither gives the caller a mutable parser or ruleset.
Register a domain once when several policies share its vocabulary. Registration copies the declarations, including policy-source atoms; using a static table does not mean loading without a copy. Inline loading reads the supplied source bytes with no filesystem access. The common JavaScript/TypeScript API uses this same sequence for inline policies: registerDomain(), then loadInlineRuleset().
Registration and loading are separate operations, not one transaction. If registration succeeds but loading fails, the domain remains registered and no new policy is returned. The v0.16 loading architecture documents the historical combined call.
If the manifest JSON and policy sources are already in memory, use the stable buffer call below. It retains the manifest checks and is distinct from raw inline source. policy_load_manifest_text() is also declared in <maelys/datalog.h>; unlike the buffer call, it retains default_profile=MAELYS-DATALOG-v2. The v0.16 manifest-text variants describe their historical placement.
Loading a policy
Use inline loading when the application already has one policy's source bytes. No manifest is read, so there is no digest or whitelist metadata on this path.
See the policy_load_inline() reference.
Use manifest loading when files, digests and an explicit public-query whitelist are part of the deployment contract. A single manifest can name several policies.
See the policy_load_manifest() reference.
For in-memory JSON and source bytes, the stable loader uses the same schema,
permissions, default_profile=enforce, SHA-256 and public-query whitelist as
the file loader. Required policy file fields remain metadata: no files are read.
Sources are matched by policy_id(); missing enabled sources fail with
NOT_FOUND. All byte buffers are borrowed for the call only.
See the policy_load_manifest_buffer() reference.
Use policy_count() before selecting an index from a manifest
that contains multiple enabled policies.
Example calls
Choose one loading path for a given handle. For source bytes already in memory:
maelys_datalog_policy_t *policy = NULL;
maelys_datalog_diagnostic_t diagnostic =
maelys_datalog_diagnostic_initializer();
maelys_datalog_status_t status = maelys_datalog_policy_load_inline(
"documents", "documents.main", source, strlen(source),
&policy, &diagnostic);For a governed set stored as files, use this instead:
maelys_datalog_policy_t *policy = NULL;
maelys_datalog_diagnostic_t diagnostic =
maelys_datalog_diagnostic_initializer();
maelys_datalog_status_t status = maelys_datalog_policy_load_manifest(
"policies/manifest.json", MAELYS_DATALOG_PUBLIC_ALLOW_NONE,
&policy, &diagnostic);If you need both loaded sets simultaneously, give each its own output variable and later free each handle.
Loading permissions
The flags argument of policy_load_manifest() is a mask of independent permissions, not a choice between global loading modes. PUBLIC_ALLOW_NONE has the permanent value 0u and means that no optional permission is granted.
| C flags | Permission granted |
|---|---|
PUBLIC_ALLOW_NONE | None: require declared policy constants and reject enabled test-only policies. |
PUBLIC_ALLOW_TEST_ONLY | Admit enabled entries whose manifest mode is test_only. |
PUBLIC_ALLOW_UNDECLARED_POLICY_ATOMS | Admit policy-local string constants absent from the domain's atoms. |
Both bits combined with | | Grant both permissions independently. |
Test-only is a loading guard, not a simulation. An enabled entry marked "mode": "test_only" is rejected with STATUS_FORBIDDEN unless the caller grants the permission. Once loaded, it evaluates normally and can produce real decisions. The engine does not detect a production environment: the application must keep this permission out of production configuration. Granting it does not bypass SHA-256 checks or authorize undeclared atoms.
Policy atoms belong to the registered domain, not the manifest. For example, blocked("mallory"). in policy source requires "mallory" in domain_t — atoms by default. Registering the string permits its use; it does not create a blocked-user fact. A literal used as a predicate argument inside a rule has the same requirement, including for EDB predicates. Special standard-filter pattern arguments have their own validation; they are not policy atoms.
By contrast, "mallory" supplied by an EDB insertion is runtime data and does not need an atom declaration. Variables such as User and integer values are not string atoms either. ALLOW_UNDECLARED_POLICY_ATOMS relaxes only the source-constant check: predicates still need declaration, capacities remain bounded, and the permission does not modify the global domain or change request-data handling.
Inline loading guarantees declared policy constants. policy_load_inline() has no flags parameter: its built-in Datalog path always checks source constants against the registered domain and exposes no opt-out. It reads no manifest metadata such as test_only. Among these two public loaders, only the manifest loader exposes the permissions above.
Unknown bits fail explicitly. The public manifest loader rejects any unsupported bit with STATUS_INVALID_ARGUMENT; it does not ignore it. A program compiled with a future nonzero permission bit therefore cannot silently lose that permission when used with an older library. Do not confuse this permission mask with the unrelated predicate-kind flags or the numeric policy index used to create a session.
See the document-access comparison for the same blocked-user fact supplied as EDB data versus written as a policy fact.
Example — load a document-access policy
After registering the seven-predicate documents domain on
registries, load
the same policy used by the quickstart. The inline path returns a
one-policy handle; its index is 0.
static const char source[] =
"can_read(User, Doc) :-\n"
" owns(User, Doc) or delegated(User, Doc),\n"
" not(blocked(User)).\n"
"has_any_document(User) :- owns(User, _).\n"
"allow(User, Doc) :- user(User), can_read(User, Doc).\n";The loader receives these source bytes and an initialized diagnostic for optional error details.
maelys_datalog_policy_t *policy = NULL;
maelys_datalog_diagnostic_t diagnostic =
maelys_datalog_diagnostic_initializer();
maelys_datalog_status_t status = maelys_datalog_policy_load_inline(
"documents", "documents.main", source, sizeof(source) - 1u,
&policy, &diagnostic);
if (status != MAELYS_DATALOG_STATUS_OK) {
/* Inspect diagnostic; do not create a session. */
}For a file manifest, the domain field selects the same registered
documents vocabulary and the policy file contains these same source
bytes. The manifest adds policy-set metadata and can select more than one
policy; it does not change the meaning of the rules. This policy contains
no quoted source atoms, so the domain's atom list remains empty.
Load an opaque policy set
Choose inline source or a manifest after
registering the domain. Both loaders return the same opaque policy-handle
type. Inline source produces one policy, so index 0 is valid. A manifest
may contain multiple enabled policies: count them before selecting one.
See the policy_count() reference.
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_policy_count(policy, &count);policy_count() does not expose the rules of a policy or combine the policies.
It only gives the number of valid indices. session_create(policy, index, &session) selects exactly one of them. Two sessions can select different
indices from the same handle, and each evaluates only its selected policy.
See solving for their lifetime and result leases.
Compiled program counts
See the policy_stat_t reference.
See the policy_stat_get() reference.
Predicates include unused declarations from the domain registry. Facts count
compiled policy facts, excluding runtime EDB inputs. Rules count normalized
rules, including expanded or alternatives. These counts differ from build
ceilings, effective session quotas and current result occupancy.
Example — select a document-access policy
The inline load of documents.main on the manifest page
contains one ruleset. Count before selecting its index; the handle does not
expose or permit editing the parsed rules.
size_t count = 0u;
maelys_datalog_status_t status = maelys_datalog_policy_count(policy, &count);
if (status != MAELYS_DATALOG_STATUS_OK || count != 1u) {
/* Loading failed or the expected policy is absent: deny. */
}
maelys_datalog_session_t *session = NULL;
status = maelys_datalog_session_create(policy, 0u, &session);Index 0 selects documents.main from this one-policy inline handle,
not the first predicate or the first rule. The same selected policy accepts
the seven request facts on Runtime EDB.
Identity and ownership
A fingerprint answers whether two loaded policy sets have the same canonical identity; it does not include request facts.
See the policy_fingerprint() reference.
char fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES];
maelys_datalog_status_t status = maelys_datalog_policy_fingerprint(
policy, fingerprint);The loaded-set fingerprint and a session's fingerprint answer different
questions: the former identifies the loaded policy authority, while
session_fingerprint() identifies the prepared selection.
session_execution_fingerprint() additionally binds the
backend, execution options, and size profile; runtime request facts are not
part of that identity. Use the returned identity that matches the decision
you need to attest.
After successful session creation, that session owns its prepared state. Release the loaded set when no new session needs it:
See the policy_free() reference.
maelys_datalog_status_t status = maelys_datalog_policy_free(policy);
policy = NULL;Free each result before its session. The opaque handles do not permit direct modification of parsed rules after loading.
Loading failure is not a partial ruleset
Loader errors can fill diagnostic_t with a source
location and a corrective hint. They publish no partial policy handle. A
malformed source, undeclared predicate, forbidden source atom, or invalid
manifest must be fixed and loaded again; the caller cannot repair the
opaque handle in place. See stable errors.