~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(metrics): keep token writes with the research owner (#7052)
LearningCircuit36bec2fa43bd
diff --git a/changelog.d/5794.bugfix.md b/changelog.d/5794.bugfix.mdnew file mode 100644index 0000000000..209d342348--- /dev/null+++ b/changelog.d/5794.bugfix.md@@ -0,0 +1 @@+Keep synchronous token metrics and query details with the account that owns the research, including callbacks without an HTTP request.diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.pyindex e7b7c7d707..caf2f95752 100644--- a/src/local_deep_research/metrics/token_counter.py+++ b/src/local_deep_research/metrics/token_counter.py@@ -900,12 +900,20 @@ class TokenCountingCallback(BaseCallbackHandler):try:from ..database.session_context import get_user_db_session- username = get_current_username()+ # The query metadata belongs to the research, even if a different+ # account's request is active when its callback runs.+ research_username = self.research_context.get("username")+ username = research_username or get_current_username()if not username:logger.debug("No user session, skipping token metrics save")return- with get_user_db_session(username) as session:+ password = (+ self.research_context.get("user_password")+ if research_username+ else None+ )+ with get_user_db_session(username, password=password) as session:# Phase 1 Enhancement: Prepare additional contextresearch_query = self.research_context.get("research_query")research_mode = self.research_context.get("research_mode")diff --git a/tests/metrics/test_token_counting_attribution.py b/tests/metrics/test_token_counting_attribution.pyindex 83892eb439..326520a988 100644--- a/tests/metrics/test_token_counting_attribution.py+++ b/tests/metrics/test_token_counting_attribution.py@@ -307,25 +307,32 @@ def test_background_thread_attributes_tokens_to_research_context_owner(assert other_rows[0].research_id == "mallory-research"- strict=True,- reason=(- "CROSS-USER MISATTRIBUTION. token_counter._save_to_db MainThread "- "branch resolves the target database with get_current_username() "- "(the request contextvar) and ignores research_context['username'] "- "entirely -- unlike the background-thread branch a few lines above, "- "which uses the research context. When a request from user B is in "- "flight while user A's research emits an LLM call on the main "- "thread, A's TokenUsage row -- research_query text included -- is "- "written into B's encrypted database."- ),-)-def test_mainthread_tokens_must_not_land_in_another_users_database(user_dbs):[email protected]("request_owner", ["bob", "alice", None])+def test_mainthread_tokens_must_not_land_in_another_users_database(+ user_dbs, monkeypatch, request_owner+):+ import local_deep_research.database.session_context as session_context+seed_other_user(user_dbs)- callback = make_callback("research-alice", "alice")+ callback = make_callback(+ "research-alice", "alice", password="synthetic-alice-database-key"+ )+ opened = []- # alice owns the research; bob owns the ambient HTTP request context.- with request_user("bob"):+ @contextmanager+ def owner_session(username, password=None):+ opened.append((username, password))+ assert (username, password) == (+ "alice",+ "synthetic-alice-database-key",+ )+ with user_dbs.read(username) as session:+ yield session++ monkeypatch.setattr(session_context, "get_user_db_session", owner_session)++ # A different, matching, or absent request cannot change the research owner.+ with request_user(request_owner):callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r1")callback.on_llm_end(StubLLMResult(100, 50), run_id="r1")@@ -338,21 +345,72 @@ def test_mainthread_tokens_must_not_land_in_another_users_database(user_dbs):assert len(alice_rows) == 1assert alice_rows[0].total_tokens == 150assert alice_rows[0].research_query == "query owned by alice"+ assert opened == [("alice", "synthetic-alice-database-key")]+++def test_mainthread_ownerless_callback_retains_request_scoping(+ user_dbs, monkeypatch+):+ import local_deep_research.database.session_context as session_context++ opened = []+ existing_session = session_context.get_user_db_session++ @contextmanager+ def request_session(username, password=None):+ opened.append((username, password))+ assert username == "bob" and password is None+ with existing_session(username, password) as session:+ yield session++ monkeypatch.setattr(session_context, "get_user_db_session", request_session)+ callback = TokenCountingCallback(+ research_id="request-owned-research",+ research_context={+ "research_query": "request-owned query",+ "user_password": "unowned-snapshot-password",+ },+ )+ callback.preset_provider = "openai"+ callback.preset_model = "gpt-4o"+ with request_user("bob"):+ callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r2")+ callback.on_llm_end(StubLLMResult(25, 50), run_id="r2")++ assert user_dbs.rows("alice", TokenUsage) == []+ rows = user_dbs.rows("bob", TokenUsage)+ assert len(rows) == 1+ assert rows[0].total_tokens == 75+ assert rows[0].research_query == "request-owned query"+ assert opened == [("bob", None)]+++def test_mainthread_failed_owner_open_cannot_fall_back_to_request_user(+ user_dbs, monkeypatch+):+ import local_deep_research.database.session_context as session_context++ opened = []++ def unavailable_owner(username, password=None):+ opened.append((username, password))+ raise session_context.DatabaseSessionError("Owner database unavailable")++ monkeypatch.setattr(+ session_context, "get_user_db_session", unavailable_owner+ )+ callback = make_callback(+ "research-alice", "alice", password="synthetic-unavailable-owner-key"+ )+ with request_user("bob"):+ callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r3")+ callback.on_llm_end(StubLLMResult(100, 50), run_id="r3")++ assert opened == [("alice", "synthetic-unavailable-owner-key")]+ assert user_dbs.rows("alice", TokenUsage) == []+ assert user_dbs.rows("bob", TokenUsage) == []- strict=True,- reason=(- "SCHEDULED-JOB METRICS ARE DROPPED. Same root cause as above: the "- "MainThread branch of _save_to_db requires get_current_username(). "- "A scheduler job that carries full credentials in its research "- "context but runs with no request contextvar (e.g. "- "scheduler/background.py:848 wraps _reconcile_unindexed_documents "- "via _wrap_job() WITHOUT username=, so get_current_username() is "- "None inside it) silently records nothing -- logged at debug level "- "as 'No user session, skipping token metrics save'."- ),-)def test_scheduled_job_tokens_are_recorded_for_the_context_owner(user_dbs):seed_other_user(user_dbs)callback = make_callback("scheduled-research", "alice")