Skip to content

Datalog: a cross-ledger f:rulesSource is read at its head under a pinned query, and drops f:sparql rules silently #2460

Open

Description

@aaj3f

For a rule run someone has to audit later, the claim we want to be able to make is "these rules, as of the rules ledger's t = R, over this data as of t = D", and be able to re-run it and get the same answer. Keeping the rules in their own ledger is the natural layout for that (rules get their own history, approvals and access), but on current main (55ecf81da) a cross-ledger f:rulesSource can't be pinned, and it drops f:sparql rules without saying so. I had Claude go through what's supported today and repro the two problems on the v4.2.6 CLI (the cross-ledger code hasn't changed since).

What pins today

Where the rules live Pinned with a @t: data query?
The data ledger's own default graph, or a local f:rulesSource graph Yes. load_rule_set reads the rules graph at the query's t (fluree-db-query/src/datalog_rules/execute.rs:76-85)
Query-time rules The caller supplies the text, so yes, but they're admin-only: under a non-root view policy they're stripped with a tracing::debug! and nothing in the response (fluree-db-api/src/view/query.rs:1786-1796)
Another ledger, via f:rulesSource + f:ledger No. The rules ledger resolves at its head for the request (fluree-db-api/src/cross_ledger/resolver.rs:144-157), and f:atT is refused as Phase 3 (resolver.rs:81-87, docs/security/cross-ledger-policy.md:609-616)

So the only pinned two-ledger setup today is a runner that reads the rule text out of the rules ledger at @t:R itself and passes it as query-time rules against data@t:D, as root.

The silent part: cross-ledger f:sparql rules

materialize_rules keeps only FlakeValue::Json values of f:rule (fluree-db-api/src/cross_ledger/rules_materializer.rs:91-98, and the module doc at :12-15 says it mirrors the local extractor). The local extractor has since learned f:sparql and refuses any other datatype by name (fluree-db-query/src/datalog_rules/mod.rs:250-281), so the two have drifted: the same SPARQL rule runs when stored in the data ledger and disappears when stored in a rules ledger. The cross-ledger docs (cross-ledger-policy.md:602-607) say a bad cross-ledger rule fails the query because "silently weakening the configured reasoning model is the worst failure mode", which I agree with, and which is what this does.

Repro

Data ledger d2: ex:alice ex:score 5, ex:bob ex:score 50, both ex:Account. Its #config points f:rulesSource at graph ex:rules of ledger rules:main (the TriG from cross-ledger-policy.md:576-590). The rules graph holds ex:r1 (@json: score > 10 → ex:Candidate) and ex:r2 (f:sparql: CONSTRUCT { ?a a ex:SparqlCandidate } WHERE { ?a ex:score ?s }).

  1. {"from": "d2:main@t:2", "select": ["?a","?c"], "where": {"@id":"?a","@type":"?c"}, "reasoning": "datalog"} returns bob as ex:Candidate, and no ex:SparqlCandidate at all.
  2. Add ex:r3 (@json: score > 1 → ex:Candidate) to the rules ledger. The same query, still pinned at d2:main@t:2, now returns alice as a candidate too.
  3. The same ex:r2 stored in a data ledger's own default graph derives ex:SparqlCandidate for both accounts.

What I think we want, roughly in order

  1. Cross-ledger f:sparql rules travel, or fail. Query-time rules already accept an f:sparql-typed value (fluree-db-query/src/datalog_rules/parse.rs:63-81), so carrying the datatype across the wire may be all it takes; failing the query like a malformed JSON rule would also be fine. This one's small and it's the silent one.
  2. A pinned rules read. Either f:atT for f:rulesSource (Phase 3), or, perhaps more useful for a runner, a per-query way to name the rules source and its t next to a pinned from, e.g. "rulesFrom": "rules:main@t:12", under the same admin gate query-time rules have. I don't have a strong opinion on which; the second doesn't need a config write per run.
  3. Say what ran. The tracked reasoning block (ReasoningTally, fluree-db-core/src/tracking.rs:644-668) carries the evaluation time but not which rules source, at which t, or a rule-set hash. The cache key already hashes the rule set (rule_set.content_hash(), fluree-db-query/src/execute/reasoning_prep.rs:171-186), so surfacing it (plus the resolved t of a cross-ledger source, which resolver.rs already has as head.t) would let a caller record "rules@R over data@D" without trusting its own bookkeeping.
  4. More of a question than a suggestion: should stripping query-time rules under a non-root policy be a 4xx (or at least a tracked warning) rather than a debug log? A query that reads a derived predicate just comes back empty today, and nothing tells the caller why.

Happy to talk through any of these, and to split 1 out on its own if that's easier to land.

Activity

  1. added
    bug
    enhancement
    area:datalog
    on Nov 19, 2026
  2. aaj3f commented on Nov 19, 2026

    @aaj3f
    ContributorAuthor

    Picking this up. Items 1 and 2 seem unambiguous to me, so they're in progress now and a PR will link back here: cross-ledger f:sparql rules will travel (the cross-ledger materializer and the local extractor will share one classifier for an f:rule value, so they can't drift again, and a value that's neither @json nor f:sparql fails the query naming the rule, as it does locally), and the tracked reasoning block will carry the rule set's hash plus, for a cross-ledger source, its ledger and the t it resolved to. Cross-ledger rules also lose their @id on the wire today (they run as _:query_rule_N), so errors and the tally will name them by their IRI.

    Items 3 and 4 read to me as decisions more than fixes, so I'd like @bplatz's read before anything gets built. Claude laid out the options; pasting its notes, lightly edited:

    3. Pinning the rules read. Two shapes, and they aren't mutually exclusive:

    • f:atT on f:rulesSource (the Phase 3 field cross_ledger/resolver.rs:81-87 refuses today). The pin lives in D's config, so "this ledger reasons with rules@R" is a config fact with its own history, and every query uses it. The cost is a config write each time the pin moves, plus what Phase 3 always needed: a check that the rules ledger still holds t (so a pin past retention fails rather than quietly reading the head), and the per-request head cache (ResolveCtx::resolved_heads) keyed by (ledger, t) instead of ledger alone. Since policy and schema sources share the resolver, it would land for them too, which is either a feature or scope creep depending on how you look at it.
    • A per-query rulesFrom, e.g. "rulesFrom": "rules:main@t:12#http://example.org/rules", under the same admin gate query-time rules have (an unrestricted view, and f:allowQueryTimeRules not turned off). The address grammar already parses that shape (LedgerRef::parse, fluree-db-core/src/dataset_ref.rs), so there's no new syntax to invent, just a new request key (and a PRAGMA for SPARQL). It would replace the configured rules for that query rather than add to them, which seems like the auditable reading of "these rules, as of R".

    Claude's recommendation, fwiw, is rulesFrom first: it's what a runner needs to re-run "rules@R over data@D" without a config write per run, and once item 2 lands, the tally reports the rules ledger and t an unpinned run used, so any run can be re-run pinned afterwards. f:atT could follow whenever someone needs the pin to be the ledger's standing configuration.

    4. Query-time rules that are dropped without a word. Under a non-root policy they're stripped with a tracing::debug! (view/query.rs:1793-1796, and view/dataset_query.rs:637-643 for the history branch), so a query that reads a derived predicate just comes back empty. There's a sibling a few lines above (view/query.rs:1776-1784): when the ledger's config sets f:allowQueryTimeRules false under an override control that denies the query, rules is cleared the same quiet way. Two options:

    • Refuse with a 400, saying query-time rules need an unrestricted view (or that the ledger's config doesn't allow them). That matches how explain already refuses a restricted view (view/explain.rs:67-71), and the rest of the reasoning surface, which has been moving towards refusing rather than answering from less than was asked for. The risk is a client that sends the same rules whatever the identity and is happy to have them ignored; I'd guess that's rare, but I don't actually know.
    • Keep answering, and say so in the tracked response (an ignored_rules count on the reasoning block, say). Non-breaking, but only a tracked request ever sees it.

    Claude leans towards refusing, at least for the policy case. It changes a response a client could depend on, though, so I'd rather we agree on it before it's built. Happy to talk through any of these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:datalogbugenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions