Repository navigation
doc reader: read the transaction layout, send controls as headers - #2295
Conversation
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
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
left a comment
There was a problem hiding this comment.
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.
| // 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?; |
There was a problem hiding this comment.
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_ingestisNone(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.
There was a problem hiding this comment.
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.
| // 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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 writesmemo-1.jsonld,memo-2.jsonld, … in place ofmemo.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_readplans the calls first (plan_document, no request), so the refusal costs nothing. The message names-o FILE.jsonld,--format trig, anddoc 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Nit, test gaps:
- After the re-read, also assert
annotation(therdf: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.jsonldfixture 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.
There was a problem hiding this comment.
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 intxn-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) commitsinvoice-answer.jsonldthe waydoc ingestdoes and inserts it the waydoc read -owrites it, and finds thexsd:decimaltotal, the claim reifying it, the dated mention value and the run. The transactor took the annotated typed literal fine both ways.
…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
|
Thanks @bplatz! I folded everything inline in here rather than leaving follow-ups, and answered each in its thread:
I also merged 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
|
Merged |
The HTTP reader behind
[doc.reader](fluree doc read,fluree doc ingest, and Python'sfluree.doc.read) was written against an answer that echoed the document graph next to a reading indoc: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
X-Fluree-Output: fluree-transaction,X-Fluree-Includeand, for a text read in windows,X-Fluree-Window. The body'sxt:requestgraph is gone. Sending a control both ways is a 400 on the service's side, so this isn't optional.--includeis new ondoc readanddoc ingest(include=in Python):claims,mentions(the default),claims,mentionsornone.--includeasks for. A changed--includetherefore reads again instead of reusing a stale answer.What it does with the answer
reader::reading(rewritten) checks that the commit metadata nameshttps://ns.flur.ee/extract/response/2and 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 ingestcommits each call's transaction as it is, one commit per call, into the document's reading graph<doc>/reading(named on the document node asdoc: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 sendingX-Fluree-Graph, since the service refusesurn:fluree:graph names and our default document IRIs areurn:fluree:doc:….doc readprints the transaction as the reader wrote it (sofluree insert -ftakes it).trigputs the run in<#txn-meta>with each claim as a reifier, andturtleprints the statements without the run.X-Fluree-Release,X-Fluree-RunandX-Fluree-Request-IDare kept per call: a line per call ondoc read/doc ingest,Reading.runsin 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-Afterrule is unchanged.Breaking, for anyone using the reader today: the vocabulary-profile table and the
doc:Relationre-mapping are removed (the reading stays in the terms the reader wrote it in),--relations reified|offis refused when a reader is used, and Python'sReadinglosesnodesandrelation_countin favor oftransactions,runs,mentions()/claims()rows,to_jsonld(),to_trig()andto_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) influree-db-doc/src/reader/client.rs,fluree-db-cli/src/commands/doc_reader.rsanddocs/cli/doc.md, and it's clean againstmainon its own. I'd suggest #2260 → #2261 → this, and I'll mergemainin after each one lands rather than rebasing.Testing:
cargo fmt --all --checkis clean andcargo clippy -p fluree-db-doc -p fluree-db-cli -p fluree-db-python --all-targetshas no warnings.cargo test -p fluree-db-docpasses 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-clipasses 543/543, includingdocs_coverageand the reworkeddoc_*stub-reader tests: facts only in the reading graph, the claim query overrdf:reifies <<( )>>, thetxn-metajoin, re-read replacement and refusal of the old layout.pytest fluree-db-python/tests/test_doc_read.pypasses 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:
Link: <…/response/2>; rel="profile"header although the contract says it should, so the reader detects the layout from the body instead."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.xt:textSha256covers 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.u16, so a very large corpus would eventually hit that. I left the shared-graph design influree-loading.mdfor 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