Skip to content

fix(transact): read a JSON-LD transaction's keys as the expander resolves them - #2414

Merged
bplatz merged 8 commits into
mainfrom
fix/jsonld-checks-read-expansion
Nov 19, 2026
Merged

bplatz merged 8 commits into
mainfrom
fix/jsonld-checks-read-expansion

Conversation

@bplatz

@bplatz bplatz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Some checks a JSON-LD transaction gets before it is parsed resolved its compact keys with a copy of the context rules of their own. That copy applied a node's @context and nothing else, and read the top-level context without the parser's rule that a lone @base is also the @vocab. For some documents it named a key differently from the parser.

Fix: the checks read the expanded document instead, where every key is the IRI the parser writes, resolved by the expander itself. A context rule the expander applies (a property's or a type's scoped context, a node's context) is then applied to the check by construction. This ports next's 16a635b8e to main.

  • An ordinary transaction is checked on the expansion the parser already makes, so it does no extra work.
  • A document with edge annotations is checked as written, on one more expansion. The check runs after the lowering, so a document the lowering refuses keeps its reason. That expansion costs an annotated write about 11% per insert and 19% per bulk import (parse_transaction +34% to +47%); next removes it by lowering on the expansion (ef33f6cac), which stays on next.
  • An update's where and values go to the query parser, not the expander, and get the check a query's clauses get. An IRI in a values cell is also read as the parser resolves it.
  • Bulk import refuses a reserved predicate in a chunk the lowering didn't touch as each statement reaches the import sink, by its predicate's Sid: no pass of its own. A lowered chunk is checked as written; a Turtle import still carries f:reifies* as exported.
  • The expander keeps an @annotation (or @edge) on a value object, expanded as a node, as it does on a node reference. It was dropped.

No change to what a valid document writes.

Also in this PR

  • An annotation on a @vocab key links to its edge. The lowering wrote the edge's key as written into the reifier's f:reifiesPredicate, which resolves as an @id, against @base. Under @vocab, "knows" wrote the edge as http://example.org/knows and the link as the relative knows, so @reifies did not find the annotation and a delete of the annotated edge left it. The lowering now resolves the key as the expander resolves a property key. This predates this PR; next fixes it as part of ef33f6cac.
  • A refused value object says where it is. The expander's refusals of an array or map @value without @json, and of an @id-typed value that is not a string, now lead with the value object's place in the document (["@graph",1,"ex:p"]), so a bulk import's error points at the node.

Tests

  • parse::jsonld::tests::checks_read_keys_as_the_expander_resolves_them: reserved keys under a lone @base, a node's context, a property's and a type's scoped context, an annotation's body, an update's values, and an update's where (directly and through a term). Each is refused with the reserved-predicate message.
  • it_import::import_refuses_a_reserved_predicate_however_it_is_spelled: a bulk import refuses a reserved predicate assembled from a prefix, and one an NDJSON file's shared context defines, in files without the text reifies. It fails with the sink's check off.
  • it_edge_annotations::an_annotation_on_a_vocab_key_links_to_its_edge: under @vocab, an annotation-rooted query finds the edge, and a delete retracts the annotation. It fails with the key written as written.
  • expand::tests::a_refused_value_object_names_where_it_is.

When merging into next

next has 16a635b8e, which this ports, and ef33f6cac, which lowers annotations on the expansion. Take next's side in import.rs, import_sink.rs, parse/edge_annotations.rs and parse/jsonld.rs. Check two things survive:

  • the sink-side reserved-predicate check for un-lowered import chunks, if next's import still walks each expansion;
  • JsonLdError::InvalidValueObject and its location in expand.rs.

…lves them

Some checks a JSON-LD transaction gets before it is parsed resolved its
compact keys with a copy of the context rules of their own. That copy
applied a node's @context and nothing else, and read the top-level context
without the parser's rule that a lone @base is also the @vocab, so for some
documents it named a key differently from the parser.

The checks now read the expanded document, where every key is the IRI the
parser writes, resolved by the expander itself. An ordinary transaction is
checked on the expansion the parser already makes, so it does no extra
work. A document with edge annotations is checked as written, on one more
expansion, after the lowering, so a document the lowering refuses keeps its
reason. An IRI in an update's values is read the same way, and bulk import
checks every expansion it makes.

The expander keeps an @annotation (or @edge) on a value object, expanded as
a node, as it does on a node reference; it was dropped.
@bplatz bplatz added the bug Something isn't working as expected label Nov 16, 2026
@bplatz
bplatz requested a review from a team November 16, 2026 01:56

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bplatz yes to reading the expansion: checking the IRIs the expander actually resolves closes the reserved-key gaps the old copy of the context rules left open, under a lone @base, scoped contexts and prefix-assembled imports. The one thing I'd hold the merge for is that the port leaves out 16a635b8e's where check: the old whole-document walk covered an update's where, so after this PR an update can read f:reifies* there, which main refused before. There's also a 3% cost on ordinary bulk imports with a cheap fix, below.

Full review below:


✅ Approving to unblock: please get the where/values check and the import fast path in before merging. Reading the expansion is the right fix: the checks now see a reserved key however the context spells it, which the old copy of the rules didn't under a lone @base, a scoped context or a prefix-assembled import. The two musts are the where/values check this port left behind from 16a635b8e, which main had, and a 3% import cost with a cheap way out.

Before merging:

  1. fluree-db-transact/src/parse/jsonld.rs:146: run the query side's check over an update's where and values, as next's 16a635b8e does. Without it, an update's where can now read f:reifies*.
  2. fluree-db-transact/src/import.rs:1603: move the ordinary-import check from its own walk into ImportSink::emit_triple. The walk costs every ordinary JSON-LD bulk import about 3%.

Notes:

  • Performance, every row Claude measured (release builds, quiet box, three alternating rounds, per-round medians): parse_transaction on 2,000 ordinary nodes −8.1%, on 2,000 annotated edges +33.6%, on 2,000 nodes with one annotated edge +47.4%; an end-to-end insert of 2,000 ordinary nodes −2.0% (one round a tie, so noise), of 2,000 annotated edges +11.4%; a serial bulk import of 100k ordinary NDJSON lines +3.0%, of 100k annotated lines +19.3%. The ordinary-import row is must 2. The annotated rows are the second expansion, which ef33f6cac removes, and I'd like them stated in the body.
  • This PR is the class fix for "a key's IRI resolved by a second copy of the context rules", applied to the checks. Two instances stay outside it on main, neither from this PR: the lowering's own copy (the @vocab comment above, which ef33f6cac fixes on next), and a lone @base in a bulk import, which writes the relative name where a transaction writes http://example.org/name. Not this PR; the point fixes are right here.
  • Documents that changed side: newly refused are the reserved-key spellings the old copy missed, plus an update whose values binds a reserved IRI used only as an object. SPARQL refuses that VALUES too, so that's the right side. Newly accepted are a reserved key inside a @json term's payload, or with a null or [] value (Claude confirmed none writes a reserved flake), and the where case, which is the must.
  • The body doesn't say this ports next's 16a635b8e, and merge-tree against next conflicts in all four code files. next has that commit plus ef33f6cac, so the merger takes next's side throughout. A "Merging into next" section like #2408's would save whoever does it a read.
  • Merge order: this merges cleanly onto current main (with my #2227, which touched import.rs and parse/jsonld.rs) and alongside #2408 (both edit parse_value_object) and #2425. Claude built all three merged on a box, and this PR's test, grp_import and the fluree-graph-json-ld unit tests pass there.
Adherence: Patterns ⚠️ · Performance ✖ · Deployment targets ✔ · Testing ⚠️ · Conventions ⚠️ · verification
  • Patterns/abstractions: ⚠️ The checks now read the parser's own expansion, which is the shared mechanism, instead of a copy of its rules. The port leaves out the where check that 16a635b8e routes through the query side's existing scan.
  • Performance (speed first, memory second): ✖ Ordinary bulk import is +3.0% from the always-on walk (must 2, recoverable in the sink). Annotated writes are +11.4% per insert and +19.3% per import, from the second expansion, which ef33f6cac recovers. Ordinary transactions got faster. Measured, every row in Notes.
  • Deployment targets: ✔ n/a: parse-time code that runs the same in every host, with no I/O, threads or clocks.
  • Testing: ⚠️ The unit test runs in CI and fails under each of three mutations (the walk made a no-op, the values checks removed, the expander hunk reverted). The import checks and an update's where have no test.
  • Conventions: ⚠️ Multi-line commit, CI green. The comments at jsonld.rs:136-141 and :156-157 describe the old order, and the body doesn't name the port or the cost.

Verified at f0f8c6e72: CI green on this SHA (test 16,198 passed, clippy, fmt, testsuite-sparql, wasm32); on a box Claude ran 48 transaction, update, SPARQL-update, import and query probes at the base and this head, mutation-checked the test three ways, timed release builds of both trees, and built main with #2408, #2414 and #2425 merged.

Approving so you can merge once the where check and the import fast path are in.

// parser resolves: an ordinary document on the expansion the parser makes
// anyway, an annotated one as written, before the lowering adds
// statements of its own.
let annotated = super::edge_annotations::document_has_annotation_keys(json);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: this port drops 16a635b8e's where/values check, which main had through the old whole-document walk.

The old firewall walked the whole update document, so a where that named a reserved predicate was refused with the rest. This port replaces it with checks on what the parser expands: the data clauses, the templates and the values cells. An update's where never goes through the expander (it's parsed by the query parser), so nothing checks it now. next's 16a635b8e, which this ports, covers it: it adds fluree_db_query::parse::reject_reserved_iris_in_clause and runs it over an update's where and values in parse_rooted. That hunk didn't come across.

Claude ran this on a box, against a ledger holding one annotated edge:

{"@context": {"ex": "http://example.org/"},
 "where": {"@id": "?r", "https://ns.flur.ee/db#reifiesSubject": {"@id": "?s"}},
 "insert": {"@id": "ex:x", "ex:saw": {"@id": "?s"}}}

On the base it's refused ("resolves to a system-controlled predicate"). At this head, and on main with this PR merged, it's accepted and writes ex:x ex:saw ex:a, the reifier bundle's subject copied into an ordinary triple. The compact f:reifiesSubject spelling behaves the same. A JSON-LD query refuses that where (fluree-db-query/src/parse/mod.rs:106), and SPARQL refuses naming f:reifies* in a pattern, so this would leave an update's where as the one surface that reads the bundle.

The fix is next's: the reject_reserved_iris_in_clause wrapper in fluree-db-query/src/parse/mod.rs, its call for where and values when txn_type is Update, and next's test case ("where": {"@id": "?s", "x": "?o"} under a term naming f:reifiesSubject). Your values-cell checks can stay or go once that's in. The txn-meta half of 16a635b8e isn't needed on main: txn-meta already refuses an f: predicate other than f:message and f:author (Claude checked the sidecar and the envelope forms).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in c23afa7

Comment thread fluree-db-transact/src/import.rs Outdated
)?;
Ok(())
} else {
crate::parse::edge_annotations::refuse_reserved_iris(expanded)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking (performance): the always-on walk costs every ordinary JSON-LD bulk import about 3%.

For a chunk with no annotation, refuse_reserved now walks every expansion the import makes. On a 20,000-node expansion that walk takes 5.5–5.6% of the expansion's own time. The base skipped any chunk whose text lacked reifies. Some check has to run, since a prefix-assembled IRI defeats that text gate, but it doesn't need a pass of its own.

Claude timed release builds of the base and this head on a quiet box, in three alternating rounds. A serial bulk import of 100k ordinary NDJSON lines is +3.4% / +3.0% / +2.5% (mean +3.0%), and the per-round minimums agree (+3.7% / +2.9% / +2.6%). Ordinary transactions got faster: parse_transaction −8.1%, end-to-end insert −2.0%.

Every triple already reaches ImportSink::emit_triple, so a reserved-predicate test on the predicate's Sid there (is_reserved_reifies_predicate, which fails on the namespace code for nearly every triple) should do the same job without the walk. It wants a flag set only for a JSON-LD chunk the lowering didn't touch: a lowered chunk writes the bundle's f:reifies* slots through that sink and keeps its as-written check, and a Turtle import stays permissive for export round-trips. Commenting here because import_sink.rs is not in this diff.

The annotated rows (+33.6% / +47.4% parse_transaction, +11.4% per insert, +19.3% per import) come from the second expansion. I think they're a fair price for closing these gaps, but the body's "one more expansion" should state them. Is next's ef33f6cac, which removes them (and fixes the @vocab lowering issue below), meant for main too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8b13cab. The PR body now states the annotated-write cost; ef33f6cac stays on next.

if needs_lowering {
let top_ctx = crate::parse::edge_annotations::top_level_context(&doc)?;
crate::parse::edge_annotations::run_user_authored_reifies_firewall(&doc, &top_ctx)?;
let refused = refuse_reserved_in_chunk(&doc, shared.as_ref());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a suggestion than a must. The import side is where the old check was narrowest: it ran only on a chunk whose text contained reifies, so a reserved IRI assembled from a prefix, or a term an NDJSON line's shared context scopes to one, wasn't checked. At this head both are refused (Claude checked each on a box). But the PR's test drives parse_transaction only, so nothing would catch refuse_reserved_in_chunk or the per-expansion check regressing. A grp_import test with one of each would pin them. It's minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8af329e

Comment thread fluree-db-transact/src/parse/jsonld.rs Outdated
@@ -139,8 +139,11 @@ fn parse_rooted(
// clone, and only when an `@annotation` / `@edge` / `@reifies` block is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So minor, but the comment just above the new one (:136-141) still says "The firewall always runs on the original document … it must not be gated", and :156-157 and the name lower_edge_annotations_after_firewall describe the old order. Worth a pass while you're here, since the next reader will trust the old paragraph.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8645cf2

Comment thread fluree-db-transact/src/parse/jsonld.rs Outdated
if update {
super::edge_annotations::lower_delete_annotation_blocks(&mut lowered)?;
}
super::edge_annotations::lower_edge_annotations_after_firewall(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code didn't introduce this, but it's the same shape this PR fixes, one step over. The lowering still names the edge's predicate by its own reading of the context: it writes the key as written into f:reifiesPredicate. Under {"@vocab": "http://example.org/"}, "knows": {"@id": "http://example.org/b", "@annotation": {"http://example.org/conf": 0.9}} writes the base edge as http://example.org/knows but the reifier's f:reifiesPredicate as the relative IRI knows, so @reifies can't find the annotation. Claude reproduced that on the base and at this head; with a prefix, a term, a full IRI or a lone @base it attaches. next's ef33f6cac fixes exactly this by lowering on the expansion. Is that one meant to come to main as well? If not, a @vocab-term annotation is worth a refusal on main rather than a link that silently points nowhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 3d347a3. ef33f6cac stays on next; on main the lowering now resolves the key as the expander resolves a property key, so a @vocab key links (an annotation-rooted query finds it, and a delete retracts it).

@bplatz

bplatz commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

For whichever of this PR and #2408 lands second: #2408 adds a refusal in parse_value_object for an array or map @value with no @json type, or a map typed @vector, and leaves a value object typed @id to the existing "a value object typed @id names an IRI, so its @value must be a string". Neither message says where the value object was, so in an NDJSON import the error names no line or node. This PR threads path into parse_value_object; both messages should carry it.

The checks this branch moved onto the expansion read what the parser
expands: an insert, delete or upsert's data and an update's templates.
An update's `where` and `values` are parsed by the query parser
instead, so nothing checked them for reserved predicates any more. The
compact-form walk they replaced had covered the whole document, so an
update could again name `f:reifiesSubject` in its `where` and copy a
reifier bundle's subject into an ordinary triple, which a JSON-LD
query and SPARQL both refuse.

`fluree_db_query::parse::reject_reserved_iris_in_clause` exposes the
scan a query's clauses get, and parsing an update runs it over `where`
and `values` against the document's context. The test refuses a
`where` naming the reserved IRI directly and through a term.
A bulk import refused the `f:reifies*` predicates in a JSON-LD chunk no
annotation lowering touched by walking every expansion it made, a pass
of its own over each document that cost an ordinary import about 3%.

The import sink now does it: for such a chunk it tests each
statement's predicate by its Sid, which fails on the namespace code
for nearly every one, and keeps the first refusal as it does any other
the sink makes. Every statement already reaches the sink, in the
default graph, a named graph or a list, so the check sees what the
walk saw. A lowered chunk keeps its check of the document as written,
since the lowering writes those predicates itself, and a Turtle import
still carries them as exported.
A bulk import's check used to run only on a chunk whose text held
`reifies`, so a reserved IRI assembled from a prefix, or a term an
NDJSON file's shared context defines, slipped past it. Both are now
refused by the IRI the expander resolves, and nothing pinned that on
the import path, where the check now runs in the sink.

The new test imports one file of each kind, neither containing the
text `reifies`, and expects the refusal. With the sink's check off,
both imports go through and write the reserved predicate.
This branch moved the reserved-predicate check to read the document as
written after the annotation lowering has run, so a document the
lowering refuses keeps its reason. Several comments still described the
old order, the check running first on the original document, and
`lower_edge_annotations_after_firewall` was named for it.

The lowering without the check is now `lower_edge_annotations_unchecked`,
documented for a caller that checks the document as written itself.
The parser's comments, the module's, and the lowering's notes on its
synthesized `f:reifies*` IRIs say what holds now, and a wrapper's
unparseable `@context` is said to be refused where it is read.
The expander refuses a value object whose `@value` is an array or a
map without `@type: @json` (or `@vector` for an array), and one typed
`@id` whose `@value` is not a string. Neither message said where the
value object was, so in a bulk import's chunk, which expands many
nodes as one document, the error named no node.

Both now come as `JsonLdError::InvalidValueObject`, whose message
leads with the value object's place in the document: the keys, as
written, and array indexes from the root, as a JSON array, such as
`["@graph",1,"ex:p"]`. The reasons are unchanged.
…er does

The annotation lowering wrote the edge's key as written into the
reifier's `f:reifiesPredicate`, as an `@id`. An `@id` resolves against
`@base`, while the edge's own key resolves through a term, a prefix,
then `@vocab`. Under `@vocab`, `"knows"` wrote the edge under
`http://example.org/knows` but named the relative `knows` in the
reifier, so the annotation linked to no edge: an annotation-rooted
query did not find it, and a delete of the annotated edge did not
retract it.

The lowering now resolves a forward key, and a key in a `@reverse`
map, as the expander resolves a property key, against the node's own
context, in an insert and in a delete. A key none of those rules
resolves is left as written, so `@base` resolves it as before. A term
defined with `@reverse` already names its resolved IRI.
@bplatz

bplatz commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

run the query side's check over an update's where and values, as next's 16a635b8e does

Addressed in c23afa7.

move the ordinary-import check from its own walk into ImportSink::emit_triple

Addressed in 8b13cab (the check is in the sink's push_triple, which every statement goes through), with the import tests in 8af329e.

The annotated rows are the second expansion, which ef33f6cac removes, and I'd like them stated in the body.

Stated in the PR body. ef33f6cac stays on next; the @vocab link it also fixes is fixed on main in 3d347a3.

The body doesn't say this ports next's 16a635b8e … A "Merging into next" section like #2408's would save whoever does it a read.

Both are in the PR body now.

a lone @base in a bulk import, which writes the relative name where a transaction writes http://example.org/name

Left on main, as you suggested. next has it from f4eb039ee: @base resolves references only, as JSON-LD defines, so a key under a lone @base is a plain name in a transaction and an import alike.

@bplatz
bplatz merged commit 57b8875 into main Nov 19, 2026
21 checks passed
@bplatz
bplatz deleted the fix/jsonld-checks-read-expansion branch November 19, 2026 01:37
bplatz added a commit that referenced this pull request Nov 19, 2026
Brings #2414 and #2415.

#2414 ported next's `16a635b8e` to main, and next also has
`ef33f6cac`, which lowers annotations on the expansion. next's side is
kept in `parse/jsonld.rs` and `parse/edge_annotations.rs`; main's
duplicate `reject_reserved_iris_in_clause` and its `values`-cell checks
are dropped (next's `values` already get the query side's check, and on
next `refuse_reserved_predicate` covers `rdf:reifies` too). What main
adds on top comes across:

- Bulk import: next still walked every expansion for reserved
  predicates. A chunk with no lowering is now checked by the import
  sink, by each statement's predicate Sid, with no pass of its own; a
  lowered chunk keeps the walk before its lowering. On next the sink
  refuses `rdf:reifies` as well as the `f:reifies*` slots
  (`is_annotation_predicate`), as next's walk did, and the import test
  gains an `rdf:reifies` case. main's `refuse_reserved_in_chunk` is
  unused here and dropped.
- The expander's value-object refusals carry their location
  (`InvalidValueObject`), with next's reason text.
- main's `@vocab` annotation test passes on next, whose lowering reads
  the expansion; its genesis is awaited.

#2415: the per-row store ids join next's `term_only` scan state, and
next's independent-count call of `make_right_scan` passes none. On
next the fused chain answers `DESCRIBE ?x WHERE { ex:a ex:knows ?x }`,
so that check asserts the fused chain's stamp, not per-row probes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants