Skip to content

doc reader: read the transaction layout, send controls as headers - #2295

Merged
aaj3f merged 10 commits into
mainfrom
feat/doc-reader-transaction-layout
Nov 12, 2026
Merged

aaj3f merged 10 commits into
mainfrom
feat/doc-reader-transaction-layout

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

The HTTP reader behind [doc.reader] (fluree doc read, fluree doc ingest, and Python's fluree.doc.read) was written against an answer that echoed the document graph next to a reading in doc: terms. The service it reads with now answers differently: the answer is a Fluree DB transaction in the extraction vocabulary (xt:, https://ns.flur.ee/extract#, "response layout 2"). The run sits in the commit metadata, the facts in the default graph, each claim behind a fact as an annotation of it, mentions only when they're asked for, and no echo of the document. Against that answer today's reader comes back wrong or empty, so this brings it up to the current contract.

What the reader sends

  • Controls go as headers and never also in the body: X-Fluree-Output: fluree-transaction, X-Fluree-Include and, for a text read in windows, X-Fluree-Window. The body's xt:request graph is gone. Sending a control both ways is a 400 on the service's side, so this isn't optional.
  • --include is new on doc read and doc ingest (include= in Python): claims,mentions (the default), claims, mentions or none.
  • The answer cache is keyed by the control headers too, and the extraction fingerprint by what --include asks for. A changed --include therefore reads again instead of reusing a stale answer.

What it does with the answer

  • reader::reading (rewritten) checks that the commit metadata names https://ns.flur.ee/extract/response/2 and refuses any other layout by name. An answer it can't read is not cached. For a later call of a long document it moves the offsets into the whole document's text.
  • doc ingest commits each call's transaction as it is, one commit per call, into the document's reading graph <doc>/reading (named on the document node as doc:readingGraph). A re-ingest clears that graph first, so a re-read replaces the reading exactly while every earlier run stays in history. The document node is committed last, so a reading that fails to commit is read again on the next run. I move the facts into that graph on our side rather than sending X-Fluree-Graph, since the service refuses urn:fluree: graph names and our default document IRIs are urn:fluree:doc:….
  • doc read prints the transaction as the reader wrote it (so fluree insert -f takes it). trig puts the run in <#txn-meta> with each claim as a reifier, and turtle prints the statements without the run.
  • X-Fluree-Release, X-Fluree-Run and X-Fluree-Request-ID are kept per call: a line per call on doc read/doc ingest, Reading.runs in Python, and errors quote the request ID. A 504 is not resent, and its error says the read may still have run. The 503 + Retry-After rule is unchanged.

Breaking, for anyone using the reader today: the vocabulary-profile table and the doc:Relation re-mapping are removed (the reading stays in the terms the reader wrote it in), --relations reified|off is refused when a reader is used, and Python's Reading loses nodes and relation_count in favor of transactions, runs, mentions()/claims() rows, to_jsonld(), to_trig() and to_turtle(). I don't think anyone depends on the old shape yet (the reader merged on 11-03 and no release has it), but flagging it in case.

Merge order: this conflicts with my #2260 (read through the sign-in) and #2261 (resend a 429 with Retry-After) in fluree-db-doc/src/reader/client.rs, fluree-db-cli/src/commands/doc_reader.rs and docs/cli/doc.md, and it's clean against main on its own. I'd suggest #2260 → #2261 → this, and I'll merge main in after each one lands rather than rebasing.

Testing: cargo fmt --all --check is clean and cargo clippy -p fluree-db-doc -p fluree-db-cli -p fluree-db-python --all-targets has no warnings. cargo test -p fluree-db-doc passes 109/109, including new tests on a recorded answer (fluree-db-doc/tests/fixtures/reader/invoice-answer.jsonld, with IRIs and the release name renamed). cargo test -p fluree-db-cli passes 543/543, including docs_coverage and the reworked doc_* stub-reader tests: facts only in the reading graph, the claim query over rdf:reifies <<( )>>, the txn-meta join, re-read replacement and refusal of the old layout. pytest fluree-db-python/tests/test_doc_read.py passes 18/18 against a locally built wheel. I also checked it against the live service with two small calls on a synthetic invoice, both 200.

A few things fwiw that I couldn't settle from the contract, and none of them block this:

  1. The live answer carried no Link: <…/response/2>; rel="profile" header although the contract says it should, so the reader detects the layout from the body instead.
  2. The JSON-LD answer writes "unit": {"@value": "TWD"} under a context term coerced to @id. Our JSON-LD expander turns that into a relative IRI <TWD>, while the service's own TriG answer says the literal "TWD". It seems to be either an expander bug on our side or a quirk of the service's context. It probably affects any JSON-LD insert of these answers, so it may be worth its own issue once we know which side it's on.
  3. A document over 48,000 characters is read in several runs, and each run's xt:textSha256 covers only its own part while the offsets are moved to whole-document positions. The contract doesn't yet say what one document read across several runs should look like.
  4. One graph per document is exact and simple, but graph ids are u16, so a very large corpus would eventually hit that. I left the shared-graph design in fluree-loading.md for later, since it's a real scope addition rather than something to fold in here.

Docs updated: docs/cli/doc.md, docs/unstructured/cli/read.md, docs/getting-started/python.md, the vocabulary reference and what-gets-built. The ingest test runs the trace query the docs give.

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmNj3BkNuqovk8Bsfcc

The HTTP reader behind `[doc.reader]` (`fluree doc read`, `fluree doc
ingest`, Python `fluree.doc.read`) expected an answer that echoed the
document graph beside a reading in `doc:` terms. Its service now answers
with a Fluree DB transaction in the extraction vocabulary (`xt:`,
response layout 2): the run in the commit metadata, the facts in the
default graph, each claim behind a fact as an annotation of it, the
mentions in `xt:mentions` when asked for, and no echo. Against it the
reader came back wrong or empty.

Request:
- Controls are headers, never also in the body: `X-Fluree-Output:
  fluree-transaction`, `X-Fluree-Include` (new `--include` on `doc read`
  and `doc ingest`, `include=` in Python; default `claims,mentions`) and
  `X-Fluree-Window` for a text read in windows. The body's `xt:request`
  graph is gone.
- The answer cache is keyed by the control headers too; the
  extraction fingerprint by what `--include` asks for.

Answer:
- `reader::reading` reads layout 2: checks the commit metadata names
  `https://ns.flur.ee/extract/response/2` and refuses any other layout
  by name (an answer it can't read is not cached); moves a later call's
  offsets into the whole document's text; reads mentions, claims, facts,
  new entities and linked records out of it for reporting; prints it as
  TriG (the run in `<#txn-meta>`, claims as reifiers) or Turtle.
- The vocabulary-profile table and the `doc:Relation` re-mapping are
  removed: the reading is kept in the terms the reader wrote it in.
- `X-Fluree-Release`, `X-Fluree-Run` and `X-Fluree-Request-ID` are kept
  per call and printed (a line per call on `doc read` and `doc ingest`,
  `Reading.runs` in Python); errors quote the request ID. A 504 is not
  resent and says the read may still have run; the 503 + Retry-After
  rule is unchanged.

Ingest:
- Each call's transaction is committed as it is, one commit per call,
  in the document's reading graph `<doc>/reading` (named on the document
  node as `doc:readingGraph`); a re-ingest clears that graph first, so a
  re-read replaces the reading exactly and every run stays in history.
  The document node is committed last.
- `--relations reified|off` is refused with a reader: its facts always
  come with its reading.

`doc read` prints the transaction as the reader wrote it (`fluree insert
-f` takes it); Python's `Reading` holds `transactions`, mention and claim
rows, `runs`, `to_trig()` and `to_turtle()`.

Tests use a recorded answer (IRIs and release renamed) and stub readers;
docs updated for `doc ingest`, `doc read`, Python and the vocabulary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmNj3BkNuqovk8Bsfcc
aaj3f and others added 2 commits November 10, 2026 22:31
A CDN's web firewall that refuses a request answers with its own HTML
error page, and the reader's error printed that page's markup, cut at
300 characters. An answer that is an HTML document now reads "answered
by a proxy in front of the reader, not the reader", with the page's
title and the request ID such pages print, so the message says where
to look.

For #2295.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`fluree.doc.read(api_key="flk_…")` traded every Fluree AI key at
`/v1/auth/token`. Only a connection's key trades there; a developer key
is refused (401) and calls the service directly. So a 401 from the
exchange now sends the key to the reader as a bearer token, where the
service checks it (a key that is no good gets the reader's 401). The
exchange's other refusals read the service's nested error envelope for
their message instead of falling back to "the API key was refused".

For #2295.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A 504's message said the reader "may still have read it, and billed it".
Whether a call is billed is the service's to say, and a gateway timeout
is not billed where calls are metered (only a completed read is). What
holds everywhere is that the read may still have run, so a resend would
be a second run, and that is what the message and docs now say.

For #2295.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@bplatz bplatz 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.

Approving. The layout-2 parsing, the header controls, the cache keying and the offset shifting all look right. Please look at the inline comments before merging. The first (a reading left behind after a failed ingest) and the second (--format jsonld for a document read in several calls) are the ones I'd most like addressed here or filed as follow-ups.

Comment thread fluree-db-cli/src/commands/doc.rs Outdated
// document now: its graph holds nothing but readings. Cleared,
// not dropped, so it stays queryable at its commits.
if previous.is_some() {
clear_reading_graph(f, &alias, &reading_graph).await?;

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.

The reading graph is cleared only when a document node from a previous ingest exists. That node is retracted in its own commit above, and the new node is committed last. So a failed run can leave content in <doc>/reading that no later run clears:

  • The retract commits, then this clear fails. Next run, previous_ingest is None (the node is gone), so nothing is cleared, and the old reading stays next to the new one.
  • A first ingest commits call 1, then call 2 fails (or Ctrl-C). Next run, still no node, so no clear. If the answer cache misses (a changed ontology, or --no-cache), call 1's leftovers stay next to the new reading.

Suggest clearing whenever a reader is used, not only if previous.is_some(). Clearing a graph that doesn't exist already ends in a tolerated EmptyTransaction. Separately, each ? here aborts the whole batch after the retract has run. Failing just that document (✗ + continue) might be the better default.

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.

You're right on both cases, and both are fixed in cdbfb9d.

The reading graph is now cleared whenever a reader reads the document, as well as whenever an earlier ingest exists (previous.is_some() || reads_with_reader), so whatever a run that failed partway left there goes before the new reading, document node or not. As you said, clearing a graph with nothing in it ends in the tolerated EmptyTransaction, so a first ingest pays for one no-op.

On the ?s: the whole commit step (the retract, the clear, each call's transaction, the document node) moved into commit_document, and a failure anywhere in it now marks that document ✗ (committing: …, with what its read was billed), adds the read to the run's totals, and the run goes on. The document node still goes last, so the next run reads that document again and clears what was left. This is the same rule I applied in #2260 and #2261, so the ingest loop stays coherent: a failure of this document fails it, and only a failure every remaining document is sure to repeat (the sign-in being gone, a reader asking to wait past the cap) ends the run.

doc_ingest_recovers_from_a_reading_a_failed_run_left_behind covers your second case: memo-2.md is read in two calls and its second call's commit is refused (a txn-meta key in f:, which no transaction may set), memo.md after it is still ingested, the first call's claim is left behind with no document node, and the next run clears it and commits both new calls. For the first case (the retract commits, then the clear fails) I couldn't find a clean way to make the clear itself fail in a test, but it lands on the same path: no document node, a reader reading it, so the graph is cleared.

Comment thread fluree-db-cli/src/commands/doc.rs Outdated
// The reading as the reader wrote it: one transaction per call, which
// `fluree insert` takes as it is.
let mut out = match args.format {
DocReadFormat::Jsonld => match read.readings.as_slice() {

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.

For a document read in several calls this prints a JSON array of transactions. Commit metadata is read only from a top-level object (fluree-db-transact/src/parse/txn_meta.rs), so fluree insert -f on this output loses every call's txn-meta (its run, prov:used), and it may not take the array at all. The help text in cli.rs and the example in docs/cli/doc.md both say fluree insert -f takes the output with no condition. Suggest refusing jsonld for a read of more than one call (as Python's to_jsonld() does), writing one file per call, or adding that condition to the help and docs.

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.

Good catch, thank you. Fixed in f535f8a. I went with one file per call, since that's the only form where fluree insert -f keeps each call's run as its commit's metadata:

  • With -o memo.jsonld, a document read in several calls writes memo-1.jsonld, memo-2.jsonld, … in place of memo.jsonld, each a single top-level transaction, and stderr names each file.
  • On stdout it's refused, as Python's to_jsonld() refuses it, but before anything is sent: run_read plans the calls first (plan_document, no request), so the refusal costs nothing. The message names -o FILE.jsonld, --format trig, and doc ingest, which commits each call as it is.
  • A single-call read prints and inserts exactly as before.

The help text in cli.rs (--format and -o), docs/cli/doc.md (the options table, the output paragraph, and a two-call example that loops fluree insert -f over the files) and docs/unstructured/cli/read.md say so now. doc_read_writes_a_reading_of_several_calls_to_a_file_per_call checks that the refusal sends nothing, then inserts both files and finds two runs in txn-meta.

let body: serde_json::Value = response.body_mut().read_json().unwrap_or_default();
// A developer key is refused here (only a connection's key trades for a
// token) and is sent to the reader as it is, which checks it there.
if status == 401 {

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.

Nit: a 401 always returns here, so the 401 | 403 arm in the error branch below can only ever see a 403. Either drop 401 from that arm, or leave a comment there if it's deliberate.

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.

Dropped in 3d29845, with a one-line comment above the arm saying why a 401 never gets there (a developer key returns earlier and is sent to the reader as it is).

assert_eq!(sparql_rows(&tmp, "memos", asserted), 0);
assert_eq!(sparql_rows(&tmp, "memos", edge), 0);
assert_eq!(sparql_rows(&tmp, "memos", &fact), 0);
assert_eq!(sparql_rows(&tmp, "memos", &claim), 0);

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.

Nit, test gaps:

  • After the re-read, also assert annotation (the rdf:reifies <<( … )>> query) returns 0, since that's the shape the clear has to remove.
  • No test reads a document in more than one call (N commits into one reading graph).
  • No test covers recovery after a partial failure (see the comment on doc.rs:983).
  • The recorded invoice-answer.jsonld fixture is parsed but never transacted. It has an annotated typed literal (t:total), which the transactor handles specially, so a test that commits it would catch a mismatch there.

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.

All four are added, with stub readers and no live calls:

  • The annotation query after the re-read is asserted to return 0 now, in doc_ingest_reads_with_an_http_reader_and_a_reread_replaces_the_reading (cdbfb9d).
  • A document read in two calls: doc_ingest_commits_each_call_of_a_reading_into_one_graph (cdbfb9d). Two commits into one reading graph, two runs in txn-meta, one fact with two claims behind it, the second call's mentions moved past the first call's text, and a re-read that replaces both calls' readings.
  • Recovery after a partial failure: doc_ingest_recovers_from_a_reading_a_failed_run_left_behind (cdbfb9d, more in the thread on line 983).
  • The recorded fixture transacted: doc_ingest_and_insert_take_the_recorded_answer (85c8da3) commits invoice-answer.jsonld the way doc ingest does and inserts it the way doc read -o writes it, and finds the xsd:decimal total, the claim reifying it, the dated mention value and the run. The transactor took the annotated typed literal fine both ways.

aaj3f and others added 5 commits November 11, 2026 13:35
…a failed run left

The reading graph was cleared only when a previous ingest's document node
existed. That node is retracted in its own commit and the new one is
committed last, so a run that failed in between left content in
`<doc>/reading` that no later run cleared: the retract committed and the
clear failed (no node next time), or a first ingest committed call 1 and
call 2 failed or was interrupted (no node yet). A later read whose cache
missed then sat beside the leftovers.

The graph is now cleared whenever a reader reads the document, as well as
whenever an earlier ingest exists. Clearing a graph with nothing in it ends
in a tolerated EmptyTransaction.

And a failure in any of the document's commits (the retract, the clear, a
call's transaction, the document node) no longer aborts the run. It marks
that document ✗, counts what its read was billed, and the run goes on: the
same rule as the rest of the loop, where a failure of this document fails
it and only a failure every remaining document would meet ends the run.

Tests: a document read in two calls (two commits, two runs, one reading
graph, offsets moved, both replaced on a re-read); a run whose second
call's commit is refused, the next document still ingested, and the next
run clearing the first call's leftovers; and the annotation query after a
re-read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
For a document read in several calls, `--format jsonld` printed a JSON
array of transactions. Commit metadata is read only from a top-level
object, so `fluree insert -f` on that output lost every call's txn-meta
(its run, `prov:used`), though the help and the docs said it takes the
output as it is.

A transaction per call now goes to a file per call, which `fluree insert
-f` takes with its run as that commit's metadata: `-o memo.jsonld` writes
memo-1.jsonld, memo-2.jsonld, …, in place of memo.jsonld, and stderr names
each. On stdout a reading of several calls is refused before anything is
sent (the call count is planned first), as Python's to_jsonld() refuses
it, naming `-o FILE.jsonld`, `--format trig` and `doc ingest`. A
single-call reading prints and inserts as before. The help text, the
command reference and its example say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
A 401 from the key exchange returns earlier (a developer key, sent to the
reader as it is), so the `401 | 403` arm below it only ever saw a 403.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
The recorded answer (fixtures/reader/invoice-answer.jsonld) was parsed in
unit tests but never transacted. It has an annotated typed literal
(`t:total`, an xsd:decimal with the claim behind it) and typed mention
values, which the transactor handles apart from IRI objects. The test
commits it as `doc ingest` does, into the reading graph, and inserts it as
`doc read -o` writes it with `fluree insert -f`, and finds the total, its
claim, the dated mention value and the run in the commit metadata.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
@aaj3f

aaj3f commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bplatz! I folded everything inline in here rather than leaving follow-ups, and answered each in its thread:

  • a reader now clears what a failed run left in the reading graph, and a failed commit fails only its own document (cdbfb9d)
  • a JSON-LD read of several calls goes to a file per call, and on stdout it's refused before anything is sent (f535f8a)
  • the unreachable 401 arm is gone (3d29845)
  • the four test gaps are covered (cdbfb9d, 85c8da3)

I also merged main in (fd0609c).

The error handling follows one rule I applied across #2260, #2261 and this PR so the ingest loop stays coherent: a failure of this document (a bad answer, a failed clear or commit) marks it ✗ and the run continues, and a failure every remaining document is sure to repeat (the sign-in being gone, a reader asking to wait past the cap) ends the run with one message.

…action-layout

# Conflicts:
#	docs/cli/doc.md
#	fluree-db-cli/src/commands/doc.rs
#	fluree-db-cli/src/commands/doc_reader.rs
#	fluree-db-cli/tests/integration.rs
#	fluree-db-doc/src/reader/client.rs
@aaj3f

aaj3f commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Merged main in now that #2260 and #2261 have landed (9069e84). The conflicts were the five files all three PRs touch (client.rs, doc_reader.rs, doc.rs, integration.rs, docs/cli/doc.md), resolved by keeping both sides: the header controls, the 401 renewal and the Retry-After waits sit together in the client, and the ingest loop keeps the run-ending ReadFailure::Run beside this PR's per-document commit step. There's no behaviour change on any side. The doc and CLI suites (695) and the Python doc tests (19) pass.

@aaj3f
aaj3f merged commit 467f0da into main Nov 12, 2026
19 checks passed
@aaj3f
aaj3f deleted the feat/doc-reader-transaction-layout branch November 12, 2026 15:34
bplatz added a commit that referenced this pull request Nov 13, 2026
Brings the doc-reader work (#2260, #2261, #2295). Conflicts: doc_reader.rs keeps next's project-aware catalog loader under main's account credential and include option; test_doc_read.py compares the query row with fluree.IRI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants