Skip to content

~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.

fix(metrics): apply research_mode filter to context-overflow panel aggregation (#3887)

LearningCircuit5ad978f7c7a3
diff --git a/changelog.d/+context-overflow-research-mode-filter.bugfix.md b/changelog.d/+context-overflow-research-mode-filter.bugfix.md
new file mode 100644
index 0000000000..3dab84e592
--- /dev/null
+++ b/changelog.d/+context-overflow-research-mode-filter.bugfix.md
@@ -0,0 +1 @@
+The Context Overflow summary panel on `/metrics` now honors the dashboard's research-mode filter (Quick / Detailed / All) the same way the rest of the panels do.
diff --git a/src/local_deep_research/metrics/query_utils.py b/src/local_deep_research/metrics/query_utils.py
index a748c58672..7b0e914be2 100644
--- a/src/local_deep_research/metrics/query_utils.py
+++ b/src/local_deep_research/metrics/query_utils.py
@@ -67,7 +67,7 @@ class TruncationSummary(TypedDict):
def get_context_overflow_truncation_summary(
- session: Session, period: str
+ session: Session, period: str, research_mode: str = "all"
) -> TruncationSummary:
"""Single-source aggregation of truncation + token-summary stats.
@@ -78,14 +78,23 @@ def get_context_overflow_truncation_summary(
Truncation fields and token-summary fields are computed in one merged
query (CASE-based AVG isolates truncated rows; SQL AVG ignores NULLs).
Returns raw values — callers round/cast for their own display contract.
+
+ research_mode defaults to "all" (no mode filter). Pass "quick" or
+ "detailed" to scope to that mode — matches the rest of api_metrics()
+ so the dashboard's mode toggle stays in sync with this panel.
"""
from ..database.models import TokenUsage
time_condition = get_time_filter_condition(period, TokenUsage.timestamp)
+ mode_condition = get_research_mode_condition(
+ research_mode, TokenUsage.research_mode
+ )
base = session.query(TokenUsage)
if time_condition is not None:
base = base.filter(time_condition)
+ if mode_condition is not None:
+ base = base.filter(mode_condition)
row = base.with_entities(
func.count(TokenUsage.id).label("total_requests"),
diff --git a/src/local_deep_research/web/routes/metrics_routes.py b/src/local_deep_research/web/routes/metrics_routes.py
index cd90d2de6e..b63ebc3608 100644
--- a/src/local_deep_research/web/routes/metrics_routes.py
+++ b/src/local_deep_research/web/routes/metrics_routes.py
@@ -900,8 +900,11 @@ def api_metrics():
}
try:
with get_user_db_session(username) as session:
+ # Honor the dashboard's research_mode filter the same way the
+ # rest of api_metrics() does (token_metrics, search_metrics,
+ # etc.). Without this the panel ignores mode toggles.
summary = get_context_overflow_truncation_summary(
- session, period
+ session, period, research_mode=research_mode
)
context_overflow_data = {
"truncation_rate": round(summary["truncation_rate"], 1),
diff --git a/tests/metrics/test_query_utils_truncation_summary.py b/tests/metrics/test_query_utils_truncation_summary.py
index 6e61486873..1dcb23cf14 100644
--- a/tests/metrics/test_query_utils_truncation_summary.py
+++ b/tests/metrics/test_query_utils_truncation_summary.py
@@ -40,9 +40,10 @@ def _make_usage(
context_truncated: bool = False,
tokens_truncated: int | None = None,
prompt_tokens: int = 100,
+ research_mode: str | None = None,
) -> TokenUsage:
return TokenUsage(
- research_id=f"r-{minutes_ago}-{context_truncated}",
+ research_id=f"r-{minutes_ago}-{context_truncated}-{research_mode}",
model_provider="openai",
model_name="gpt-4",
prompt_tokens=prompt_tokens,
@@ -52,6 +53,7 @@ def _make_usage(
context_limit=context_limit,
context_truncated=context_truncated,
tokens_truncated=tokens_truncated,
+ research_mode=research_mode,
)
@@ -197,3 +199,64 @@ class TestDivisionByZeroGuard:
assert result["total_requests"] == 2
assert result["requests_with_context"] == 0
assert result["truncation_rate"] == 0.0
+
+
+class TestResearchModeFilter:
+ def test_default_includes_all_modes(self, session):
+ session.add_all(
+ [
+ _make_usage(research_mode="quick"),
+ _make_usage(research_mode="detailed"),
+ _make_usage(research_mode=None),
+ ]
+ )
+ session.commit()
+
+ # Default research_mode is "all" — no mode filter applied
+ result = get_context_overflow_truncation_summary(session, "30d")
+ assert result["total_requests"] == 3
+
+ def test_quick_excludes_detailed(self, session):
+ session.add_all(
+ [
+ _make_usage(research_mode="quick"),
+ _make_usage(research_mode="detailed"),
+ _make_usage(research_mode="detailed"),
+ ]
+ )
+ session.commit()
+
+ result = get_context_overflow_truncation_summary(
+ session, "30d", research_mode="quick"
+ )
+ assert result["total_requests"] == 1
+
+ def test_detailed_excludes_quick(self, session):
+ session.add_all(
+ [
+ _make_usage(research_mode="quick"),
+ _make_usage(research_mode="detailed"),
+ _make_usage(research_mode="detailed"),
+ ]
+ )
+ session.commit()
+
+ result = get_context_overflow_truncation_summary(
+ session, "30d", research_mode="detailed"
+ )
+ assert result["total_requests"] == 2
+
+ def test_all_mode_string_includes_everything(self, session):
+ session.add_all(
+ [
+ _make_usage(research_mode="quick"),
+ _make_usage(research_mode="detailed"),
+ ]
+ )
+ session.commit()
+
+ # Explicit "all" matches the default (no filter)
+ result = get_context_overflow_truncation_summary(
+ session, "30d", research_mode="all"
+ )
+ assert result["total_requests"] == 2