Skip to content

feat(doc): read through the machine's Fluree AI sign-in ([doc.reader] account = true) - #2260

Merged
aaj3f merged 19 commits into
mainfrom
feat/doc-reader-account
Nov 12, 2026
Merged

aaj3f merged 19 commits into
mainfrom
feat/doc-reader-account

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What

fluree doc can now read through the machine's Fluree AI sign-in (fluree login, #2205) instead of a key in config:

[doc.reader]
account = true
models = ["reader-small"]

With account = true (or FLUREE_DOC_READER_ACCOUNT=true):

  • No key in config. The credential is the sign-in's access token, sent as Authorization: Bearer.
  • A default url. It defaults to the account's own reader at <api base>/grounding, read from the sign-in. An explicit url on the same scheme, host and port still wins; one elsewhere is refused before anything is sent, so a project's .fluree/config.toml can't send the sign-in's token to another server.
  • Long runs keep going. doc ingest renews the token before each document (a refresh only when it's about to expire), so a run longer than the hourly token doesn't fail partway.
  • Configuration changes don't break a run. On a 401 from the account's reader, the token is renewed once with a forced refresh and that call alone is resent; calls already answered aren't sent (or billed) again. The common case is config_stale, after the account's configuration changed. A second 401 fails with a hint to run fluree login again.
  • No url needed in the file. [doc.reader] account = true with no url loads (it used to fail with "missing field url").
  • Not signed in says so: "the reader uses this machine's sign-in, and it isn't signed in: run fluree login".

How

  • ReaderEndpoint.account (serde-defaulted to false, so existing configs are unchanged). is_usable() accepts an account slot with no url.
  • doc_reader::resolve picks the URL and credential (and refuses a url off the account's origin), and renew refreshes the sign-in between documents.
  • ReaderClient::with_renewal(Renewal): on a 401 the client runs the hook once, swaps in the credential and resends that one request. The CLI's hook forces a refresh on the reading's blocking thread through the runtime handle. read()'s signature is unchanged.
  • ReaderClient's credential sits behind an RwLock so a run can swap in the renewed token (set_credential).
  • The sign-in hint matches DocError::Endpoint { status: 401, .. }, not message text.

Also: an expired device code during fluree login now says to run the same login command again, not fluree auth login.

Tests

  • doc_read_through_the_signed_in_account: reading through the sign-in, plus the not-signed-in error.
  • doc_read_through_the_account_renews_a_stale_token_once: a stub answers 401 config_stale, then the refresh, then the reading. The test checks the order of calls, that the retry carries the new token, and that account.toml now holds it.
  • After review: a_refused_credential_is_renewed_once_and_the_call_resent (client); doc_ingest_through_the_account_resends_only_the_refused_call (a two-call document refused on its second call: grounding, grounding, exchange, grounding, and 2 call(s) reported); doc_read_through_the_account_refused_twice_says_to_sign_in_again; doc_read_with_its_own_key_is_not_renewed_on_a_401; doc_read_through_the_account_gives_the_sign_in_hint_only_for_a_401; doc_ingest_through_the_account_renews_the_token_before_each_document; doc_read_through_the_account_refuses_a_url_off_the_accounts_api.
  • cargo test -p fluree-db-doc -p fluree-db-cli green; clippy clean for fluree-db-doc, fluree-db-cli and fluree-db-python --features extension; cargo fmt --all --check clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmSfiNVsRZmcpMicDfq

aaj3f and others added 8 commits October 30, 2026 09:55
…= true)

With `account = true` the reader's credential is this machine's
`fluree login` (a Bearer token, renewed before each document so a run
longer than the hourly token keeps going), and `url` defaults to the
account's own reader at `<api base>/grounding`. Not signed in, the run
says to `fluree login`; a 401 says to sign in again.
FLUREE_DOC_READER_ACCOUNT sets it from the environment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmSGfnxzJgMSzBESNee
# Conflicts:
#	fluree-db-cli/src/commands/doc.rs
#	fluree-db-cli/tests/integration.rs
A 401 from the account's reader (config_stale after the account's
configuration changed, most often) renews the token with
account::session(force) and sends the document again, once. Calls the
first try answered come from the reading cache when there is one.
Also follows #2205's session(http, force) signature.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmSfiNVsRZmcpMicDfq
@aaj3f aaj3f added enhancement New feature or request area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows labels Nov 4, 2026
@aaj3f aaj3f self-assigned this Nov 4, 2026
@aaj3f
aaj3f requested a review from bplatz November 4, 2026 16:31

@jdorety jdorety left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review of 5817b63. I reproduced every finding below with temporary integration tests (none committed).

🔴 Blocking

1. A 401 retry re-reads the whole document, so calls already answered are sent and billed again (doc_reader.rs:115-129)
After a 401, read_document calls session.read again from the first part. Parts that were already answered are only skipped if the reading cache has them.

  • Without a cache (--no-cache, no project or global dir, or a failed store_reading): with a 2-part doc where the stub answers 200 then 401, the reader received grounding ×2, exchange, grounding ×2. Part 1 was billed twice. This breaks the rule in docs/cli/doc.md that nothing else is resent because a failure "may follow a read that ran and was billed".
  • Reported usage is wrong with or without a cache. Without a cache: 2 call(s), 2,064 tokens, but 3 calls succeeded. With a cache: 1 from cache, 1,032 tokens, but 2 calls were billed.
  • Per-document renew doesn't prevent this. A long multi-call document, with up to 5 minutes of 503 waits per call, can outlast the token, and config_stale can arrive between any two calls.

Fix: retry only the refused call. For example, give ReaderClient::read a renewal callback that it runs once on a 401 before resending that one request.

2. A project's config can send the machine-wide account token to any URL (doc_reader.rs:28-40)
With account = true the token comes from the global fluree login, but url is used as written. [doc.reader] is read from the project's .fluree/config.toml, which is found by walking up from cwd. I reproduced it: a project config with account = true and a foreign url (the account's api_base_url on a different host, no env vars set) sent Authorization: Bearer <account token> to that URL. A cloned repo could do the same. Before this PR, a project file could only route credentials that the same file or the user's env supplied.

Fix: in account mode, reject a url whose scheme/host/port differs from api_base_url, or accept the override only from env or the global config.

🟡 Should fix in this PR

3. The new code paths have no test. I made these three changes together, and all 27 doc_ integration tests and all 276 CLI unit tests still passed:

  • deleting doc_reader::renew(&reader).await? (doc.rs:812);
  • removing the if setup.account guard on the 401 retry, so a reader using its own key would attempt an account token exchange;
  • removing the "run fluree login again" hint.

Suggested tests:

  • an ingest where the token is close to expiry: an exchange must come before the second document;
  • a non-account reader that gets a 401: one request, no /oauth/;
  • an account reader refused twice: grounding, exchange, grounding, then the hint.

🟢 Non-blocking

4. The "sign-in refused" hint is chosen by matching text, so it appears on errors that aren't 401s (doc_reader.rs:130-135)
error_message joins error and message with ": ". A 502 whose body is {"error":"upstream_error","message":"401 Unauthorized from model host"} printed hint: the sign-in was refused; run fluree login again, though the sign-in was fine. Match on DocError::Endpoint { status: 401, .. } instead.

Checked, no issue

  • Handle::current().block_on inside read_document is safe. Both callers run it inside spawn_blocking on the multi-threaded runtime.
  • The Python bindings don't read DocConfig, so an empty url in account mode doesn't reach them.

aaj3f and others added 2 commits November 10, 2026 22:14
… the account's API

A 401 used to re-read the whole document, so calls already answered were
sent and billed again when no reading cache held them, and the run's usage
report was wrong either way. The renewal now lives in ReaderClient: a client
given a Renewal answers a 401 by renewing once and resending that call
alone; a second 401 is the caller's error. read()'s signature is unchanged.

With account = true, a [doc.reader] url on another scheme, host or port
than the sign-in's api_base_url is refused before anything is sent, since
the section may come from a project's .fluree/config.toml.

The "sign-in refused" hint now matches DocError::Endpoint { status: 401 }
instead of the message text, and a failed renewal surfaces its own error.

Tests: the client renews once and resends one call; a two-call document
refused on its second call resends only that call and reports 2 calls; an
account refused twice gets the hint after one renewal; a reader with its
own key that gets a 401 sends one request and no exchange; a 502 that
mentions 401 gets no hint; an ingest renews the token before each
document; a url off the account's API is refused.

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

aaj3f commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, @jdorety, this is a really careful review, and the repros made each of these easy to confirm. All four are right, and they're fixed in a1cd4e2 (with origin/main merged in first, 692308a).

🔴1, the 401 retry re-reading the whole document. You're right that the cache was carrying the correctness here, and only when there was one. The renewal now lives in the client instead of around Session::read: ReaderClient::with_renewal(Renewal) takes a hook, and on a 401 ReaderClient::read runs it once, swaps the credential, and resends that call alone; a second 401 comes back to the caller as before. The CLI hands it a hook that forces account::session(true) on the reading's blocking thread, and read_document no longer retries anything. So a two-call document refused on its second call sends grounding, grounding, exchange, grounding, and the run reports 2 call(s), 2,064 tokens. doc_ingest_through_the_account_resends_only_the_refused_call is your exact repro (no cache, 200 then 401) as a test. read()'s signature didn't change, which matters because #2295 adds an argument to it. The module doc now gives the reason the 401 resend is safe: a request whose credential was refused wasn't read for, or billed to, that credential. The per-document renew stays as the cheap proactive refresh; the per-call hook is what covers a token running out partway through a long document, or a config_stale landing between two calls.

🔴2, a project config sending the account token anywhere. I went with your first option. With account = true, resolve now refuses a url whose scheme, host or port differs from the sign-in's api_base_url, before anything is sent, and the hint says to drop url or give the reader its own api_key. A different path on the same origin is still accepted. I preferred this to "only from env or global config" because it holds whichever layer the url came from, and nobody has to remember which layers are trusted. doc_read_through_the_account_refuses_a_url_off_the_accounts_api puts a foreign url in the project's .fluree/config.toml: refused, and neither stub sees a request. Then <api base>/v1/reader on the account's origin is used.

🟡3, tests. All three of yours are in, plus the two above and one for 🟢4:

  • doc_ingest_through_the_account_renews_the_token_before_each_document: every token the exchange hands out is inside the refresh margin, and each document's call has to carry a token exchanged just before it (two documents, two different tokens);
  • doc_read_with_its_own_key_is_not_renewed_on_a_401: a reader with its own key, on a machine that is signed in, gets one request, no exchange, and no fluree login hint;
  • doc_read_through_the_account_refused_twice_says_to_sign_in_again: grounding, exchange, grounding, then the hint;
  • a_refused_credential_is_renewed_once_and_the_call_resent in reader::client, for the client on its own.

I also re-ran your three mutations, plus removing the origin check and going back to text matching for the hint. Each one now fails a test.

🟢4, the hint matched on text. It now matches DocError::Endpoint { status: 401, .. }. Your 502 body ("401 Unauthorized from model host") is doc_read_through_the_account_gives_the_sign_in_hint_only_for_a_401. Separately, if the renewal itself fails (a revoked sign-in, say), that error now comes through whole, with its own fluree login hint, instead of being wrapped as an endpoint error.

cargo fmt --all --check, clippy on fluree-db-doc/fluree-db-cli/fluree-db-python with --all-targets, and cargo test -p fluree-db-doc -p fluree-db-cli are clean locally. Happy to talk any of these through if you'd have gone a different way, especially on 🔴2.

aaj3f and others added 4 commits November 10, 2026 22:55
The docs show `[doc.reader]` with `account = true` and no `url` (the
account's API supplies it), but `url` was a required field, so that
config file failed to load: "[doc] section: missing field `url` in
`reader`". The environment variables never hit it, which is what the
tests used. `url` now defaults to empty, and an empty one isn't written
back.

For #2260.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…th login`

The device-code flow is shared by `fluree auth login` (a remote) and
`fluree login` (the account), and its expiry message named the first.
It now says to run the same login command again.

For #2260.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…'s reader

The project-config case of the url rule, through the config file rather
than the environment (which never hit the missing-field error): with
`account = true` and no `url`, the call goes to `<api base>/grounding`.

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

Any 401 from the account's reader renews the sign-in once and resends
only that call. When the renewal is itself refused (invalid_grant), the
run fails with the sign-in's own message and its `fluree login --url`
hint, not an endpoint error.

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

aaj3f commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Two more fixes for this PR, both found in end-to-end testing after the review round:

  • 423e817: [doc.reader] account = true with no url, which is how the docs show it, failed to load with "missing field url". The env-var tests never hit that path. url is now optional, and an empty one isn't written back. With no url, account mode reads at <api base>/grounding, same as before, and the origin check from 🔴2 only applies when a url is given. ace9967 adds that config-file case to doc_read_through_the_account_refuses_a_url_off_the_accounts_api.
  • d9bc202: when the device code expired during fluree login, the message told people to run fluree auth login. It now says to run the same login command again.

Also a402561, a test for two more cases. A token_invalid 401 renews once and resends only that call, same as config_stale. And when the renewal itself is refused (a revoked refresh token), the run fails with the sign-in's own "your sign-in is no longer valid … hint: fluree login --url …" message.

@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 per-call 401 resend and the origin check look right, and the tests pin both. Please look at the three inline comments before merging. The first one (ingest keeps going after the sign-in is gone) is the one I'd most like addressed here or filed as a follow-up.

Comment thread fluree-db-cli/src/commands/doc.rs Outdated
}
} else {
// A signed-in account's token, renewed before the call.
doc_reader::renew(&reader).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.

If a call is refused and the forced refresh also fails (a revoked refresh token, say), read_document returns the renewal error, and this loop prints ✗ and continues. The stored access token still looks unexpired locally, so the next document's renew doesn't refresh. Each remaining document then sends a grounding call (401) plus a failing exchange. Neither is billed, but a large folder becomes 2N pointless requests and N ✗ lines. Suggest ending the run when renewal_failed is set.

The opposite case is odd too: a transient exchange error in this renew(..).await? aborts the whole run. Worth deciding deliberately which way each one should go.

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, and the stored token still looking unexpired is exactly why it kept going. Fixed in 61d6f84.

read_document now returns a ReadFailure, either Document or Run, and a refused sign-in whose forced renewal also fails is Run. The loop prints one ✗ for that document, stops there, still indexes and totals what it already committed, and then ends with one error that names where it stopped and what it left, e.g. the run stopped at memo-2.md, leaving 1 document(s) unread that would fail the same way: the reader refused the sign-in, and renewing it failed: your sign-in is no longer valid (…), with the fluree login --url … hint underneath. doc_ingest_through_the_account_stops_when_the_sign_in_is_gone pins it: two documents, a 401 and a revoked refresh, and only /v1/grounding + /v1/oauth/exchange are ever sent.

On the opposite case (a transient exchange error in the renew(..).await? before a document), I did think about which way it should go, and I went with ending the run there too, on purpose. That renewal only runs when the token is inside its refresh margin, so every document after it needs the same renewal first. If the sign-in is revoked, ✗-and-continue is the same N failing exchanges. If it's a passing outage at the exchange, what was committed before it is kept and a rerun skips those documents as unchanged, so stopping costs very little. It now goes through the same ReadFailure::Run path (renewing the sign-in: …) rather than a bare ?, so it gets the same message and the run is still indexed and totalled. doc_ingest_through_the_account_stops_when_renewing_before_a_document_fails covers that one: the first document is read, the second document's renewal is refused, and the run ends with 1 ingested.

This is one rule I'm keeping across all three reader PRs so the ingest loop stays coherent: a failure of this document (a bad answer, a refused call, a failed commit) marks it ✗ and the run goes on, and a failure every remaining document is sure to repeat ends the run with one message. Here that's the sign-in being gone; in #2261 it's a reader asking to wait past the cap, and #2295 applies the first half to the commit step.

/// another scheme, host or port is refused, since `[doc.reader]` may come
/// from a project's `.fluree/config.toml` that a cloned repository carries.
pub async fn resolve(endpoint: &ReaderEndpoint) -> CliResult<(String, Option<Credential>)> {
if !endpoint.account {

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.

With account = true, an api_key on the same slot is silently ignored. Having both set is most likely a mistake, so I'd refuse it or at least warn.

@aaj3f aaj3f Nov 11, 2026 •

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.

Agreed. I went with refusing rather than warning, since either way one of the two is the wrong credential for someone. Fixed in 18b09e2: resolve refuses a reader with account = true and a non-empty api_key (from the file or from FLUREE_DOC_READER_API_KEY) before anything is sent, with [doc.reader] has both `account = true` and an `api_key` (or FLUREE_DOC_READER_API_KEY): pick one and a hint for each way out. doc_read_refuses_an_account_reader_with_a_key covers it, and docs/cli/doc.md now says a reader takes account or api_key, not both.

Comment thread fluree-db-doc/src/config.rs Outdated
self.cache = Some(v);
}
if let Some(v) = var("ACCOUNT") {
self.account = matches!(v.trim().to_ascii_lowercase().as_str(), "1" | "true" | "yes");

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.

Any value other than 1/true/yes (on, a typo) silently means false, and that also overrides an account = true from the file. Suggest erroring on a value it doesn't recognize.

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, and it's worse for being the value that overrides the file. Fixed in 3a19d98: FLUREE_DOC_READER_ACCOUNT takes 1/true/yes or 0/false/no (any case, trimmed), and anything else is an error that names the variable and what it takes, e.g. FLUREE_DOC_READER_ACCOUNT="on": expected 1, true or yes, or 0, false or no. That meant DocConfig::with_env returns a Result now; its four callers in doc.rs pass the error on. I also left an empty value as an error rather than a quiet no, so an export FLUREE_DOC_READER_ACCOUNT= doesn't turn off the file's account = true either. a_flag_variable_is_yes_no_or_an_error covers the parsing, and doc_read_refuses_an_account_variable_it_cannot_read the command end to end.

aaj3f and others added 4 commits November 11, 2026 13:25
Any value other than 1/true/yes read as false, and a value overrides the
file, so a typo (or `on`) quietly turned off `account = true`. It now takes
1/true/yes or 0/false/no in any case, and anything else is an error that
names the variable and what it takes. DocConfig::with_env returns a Result
to carry it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
With `account = true` the slot's `api_key` (or FLUREE_DOC_READER_API_KEY)
was quietly ignored. Both set is most likely a mistake, and either way one
of them would be the wrong credential for someone, so the reader is now
refused before anything is sent, with a hint to remove one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018gr44NC1vnaK3VWs79jCxm
When a call was refused and the forced renewal failed too (a revoked
refresh token), the document was marked ✗ and the run went on. The stored
token still looked valid, so each later document sent a refused call and a
failing renewal of its own: 2N requests and N ✗ lines for nothing.

One rule now covers every failure in the ingest loop. A failure of this
document marks it ✗ and the next is read. A failure every remaining
document is sure to meet too ends the run with one error, after what was
already done is indexed and totalled. read_document says which with
ReadFailure::{Document, Run}.

A renewal that fails before a document is read ends the run too, by the
same rule: the token is about to expire, so every later document would
need the same renewal first. Documents read before the stop are kept, and
the error names the document and how many were left unread.

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 all three inline notes in here rather than leaving any as follow-ups, and answered each in its thread:

  • ingest now stops, with one error, when a refused sign-in can't be renewed (61d6f84)
  • account = true beside an api_key is refused (18b09e2)
  • FLUREE_DOC_READER_ACCOUNT is an error on a value it doesn't recognize, and takes explicit falses (3a19d98)

I also merged main in (e54812b).

On the first note, I took your "worth deciding deliberately which way each one should go" as a rule for the whole ingest loop, and applied the same rule in #2261 and #2295 so the three stay coherent: a failure of this document marks it ✗ and the run goes on; a failure every remaining document is sure to repeat ends the run with one message, after it indexes and totals what it already did. Both renewal failures (the one after a refused call and the one before a document) fall in the second group.

@aaj3f
aaj3f merged commit 0bf1144 into main Nov 12, 2026
19 checks passed
@aaj3f
aaj3f deleted the feat/doc-reader-account branch November 12, 2026 13:27
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

area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants