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: canonicalize URLs when deduping Sources section (#3482)

LearningCircuite8be26d8900f
diff --git a/src/local_deep_research/utilities/search_utilities.py b/src/local_deep_research/utilities/search_utilities.py
index 9ec3224272..65627288ea 100644
--- a/src/local_deep_research/utilities/search_utilities.py
+++ b/src/local_deep_research/utilities/search_utilities.py
@@ -3,6 +3,8 @@ from typing import Dict, List
from loguru import logger
+from .url_utils import canonical_url_key
+
LANGUAGE_CODE_MAP = {
"english": "en",
@@ -68,37 +70,38 @@ def format_links_to_markdown(all_links: List[Dict]) -> str:
logger.info(f"Formatting {len(all_links)} links to markdown...")
if all_links:
- # Group links by URL and collect all their indices
+ # Group links by canonical URL (collapses trailing slash, utm
+ # params, fragments, default ports, scheme/host case, userinfo).
+ # The canonical form is also what gets displayed so the Sources
+ # section stays clean — no utm_*/fbclid clutter, no embedded
+ # credentials, no scheme/host casing noise. Click-through is
+ # unaffected (tracking params carry no content).
url_to_indices: dict[str, list] = {}
+ canon_to_title: dict[str, str] = {}
for link in all_links:
- url = link.get("url")
- if url is None:
- url = link.get("link")
- index = link.get("index", "")
- if url:
- if url not in url_to_indices:
- url_to_indices[url] = []
- url_to_indices[url].append(index)
-
- # Format each unique URL with all its indices
- seen_urls: set[str] = set()
+ raw = link.get("url") or link.get("link") or ""
+ canon = canonical_url_key(raw)
+ if not canon:
+ continue
+ url_to_indices.setdefault(canon, []).append(link.get("index", ""))
+ canon_to_title.setdefault(canon, link.get("title", "Untitled"))
+
+ # Emit each unique source once, in first-seen order.
+ seen: set[str] = set()
for link in all_links:
- url = link.get("url")
- if url is None:
- url = link.get("link")
- title = link.get("title", "Untitled")
- if url and url not in seen_urls:
- # Get all indices for this URL
- indices = sorted(
- set(url_to_indices[url])
- ) # Sort for consistent ordering
- # Format as [1, 3, 5] if multiple indices, or just [1] if single
- indices_str = f"[{', '.join(map(str, indices))}]"
- # Add (source nr) as a visible fallback for humans
- parts.append(
- f"{indices_str} {title} (source nr: {', '.join(map(str, indices))})\n URL: {url}\n\n"
- )
- seen_urls.add(url)
+ raw = link.get("url") or link.get("link") or ""
+ canon = canonical_url_key(raw)
+ if not canon or canon in seen:
+ continue
+ title = canon_to_title[canon]
+ indices = sorted(set(url_to_indices[canon]))
+ indices_str = f"[{', '.join(map(str, indices))}]"
+ parts.append(
+ f"{indices_str} {title} "
+ f"(source nr: {', '.join(map(str, indices))})\n"
+ f" URL: {canon}\n\n"
+ )
+ seen.add(canon)
parts.append("\n")
diff --git a/src/local_deep_research/utilities/url_utils.py b/src/local_deep_research/utilities/url_utils.py
index 3be377d03a..b4bed91f7b 100644
--- a/src/local_deep_research/utilities/url_utils.py
+++ b/src/local_deep_research/utilities/url_utils.py
@@ -1,9 +1,32 @@
"""URL utility functions for the local deep research application."""
+from functools import lru_cache
+from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
+
from ..security.network_utils import is_private_ip
# Re-export for backwards compatibility
-__all__ = ["normalize_url", "is_private_ip"]
+__all__ = ["normalize_url", "is_private_ip", "canonical_url_key"]
+
+# Tracking query parameter keys (matched lowercased).
+_TRACKING_PARAMS = frozenset(
+ {
+ "fbclid",
+ "gclid",
+ "msclkid",
+ "yclid",
+ "dclid",
+ "gad_source",
+ "mc_eid",
+ "mc_cid",
+ "ref_src",
+ "igshid",
+ "_ga",
+ "_gl",
+ }
+)
+# Tracking param name prefixes (matched lowercased).
+_TRACKING_PREFIXES = ("utm_",)
def normalize_url(raw_url: str) -> str:
@@ -60,3 +83,83 @@ def normalize_url(raw_url: str) -> str:
scheme = "http" if is_private_ip(hostname) else "https"
return f"{scheme}://{raw_url}"
+
+
+@lru_cache(maxsize=1024)
+def canonical_url_key(url: str) -> str:
+ """Return a canonical form of ``url`` suitable for deduplication and
+ display in a Sources / citations listing.
+
+ The canonical form:
+ - lowercases scheme and host (paths stay case-sensitive),
+ - strips userinfo (``user:pass@`` — never leak creds),
+ - strips default ports (80/http, 443/https),
+ - strips fragments,
+ - drops tracking query params (``utm_*``, ``fbclid``, ``gclid``,
+ ``msclkid``, ``yclid``, ``dclid``, ``gad_source``, ``mc_eid``,
+ ``mc_cid``, ``ref_src``, ``igshid``, ``_ga``, ``_gl``),
+ - trims a trailing ``/`` from non-root paths.
+
+ Click-through behavior is preserved — tracking params carry no
+ content, and mainstream browsers already strip them automatically.
+ Percent-encoding is not normalized; query param order is preserved
+ as-is.
+
+ Falls back to ``url.strip()`` when the input is not a recognizable
+ absolute URL (e.g. ``mailto:``, ``data:``, or protocol-relative
+ ``//host/p``), since canonicalization would be ambiguous.
+ """
+ if not url:
+ return ""
+ try:
+ parsed = urlsplit(url)
+ except Exception:
+ return url.strip()
+ # Require both a scheme and a netloc; otherwise canonicalization is
+ # ambiguous (mailto:, data:, protocol-relative, etc.).
+ if not parsed.scheme or not parsed.netloc:
+ return url.strip()
+
+ scheme = parsed.scheme.lower()
+
+ # Strip userinfo (user:pass@host) from netloc.
+ netloc = parsed.netloc.rsplit("@", 1)[-1]
+
+ # Split host/port carefully so IPv6 literals survive.
+ if netloc.startswith("["):
+ end = netloc.find("]")
+ host = netloc[: end + 1]
+ rest = netloc[end + 1 :]
+ port = rest[1:] if rest.startswith(":") else ""
+ elif ":" in netloc:
+ host, _, port = netloc.rpartition(":")
+ host = host.lower()
+ else:
+ host, port = netloc.lower(), ""
+
+ if (scheme == "https" and port == "443") or (
+ scheme == "http" and port == "80"
+ ):
+ port = ""
+ netloc = f"{host}:{port}" if port else host
+
+ # Filter query params case-insensitively on key; preserve order/values.
+ if parsed.query:
+ pairs = parse_qsl(parsed.query, keep_blank_values=True)
+ kept = [
+ (k, v)
+ for k, v in pairs
+ if not (
+ k.lower() in _TRACKING_PARAMS
+ or any(k.lower().startswith(p) for p in _TRACKING_PREFIXES)
+ )
+ ]
+ query_str = urlencode(kept, doseq=True) if kept else ""
+ else:
+ query_str = ""
+
+ path = parsed.path
+ if path and path != "/" and path.endswith("/"):
+ path = path.rstrip("/")
+
+ return urlunsplit((scheme, netloc, path, query_str, ""))
diff --git a/tests/utilities/test_search_utilities_formatting.py b/tests/utilities/test_search_utilities_formatting.py
index b6119213ad..1c60f914b8 100644
--- a/tests/utilities/test_search_utilities_formatting.py
+++ b/tests/utilities/test_search_utilities_formatting.py
@@ -87,7 +87,11 @@ class TestFormatLinksToMarkdownSpecialChars:
assert "https://example.com/search?q=hello+world&lang=en" in result
def test_url_with_fragment(self):
- """URLs with fragments are preserved."""
+ """URLs with fragments are stripped in the Sources display.
+
+ Fragments are anchor-only and have no effect on click-through
+ landing pages; the canonical form drops them for a cleaner UI.
+ """
from local_deep_research.utilities.search_utilities import (
format_links_to_markdown,
)
@@ -101,7 +105,8 @@ class TestFormatLinksToMarkdownSpecialChars:
]
result = format_links_to_markdown(links)
- assert "https://example.com/page#section-2" in result
+ assert "URL: https://example.com/page" in result
+ assert "#section-2" not in result
def test_title_with_special_markdown_chars(self):
"""Titles with markdown-special chars are included."""
@@ -287,3 +292,159 @@ class TestFormatFindingsMalformedStructures:
# Should not crash
result = format_findings(findings, "Summary", {})
assert "Content" in result
+
+
+# ---------------------------------------------------------------------------
+# format_links_to_markdown — dedup via canonical URL key
+# ---------------------------------------------------------------------------
+
+
+class TestFormatLinksDedupCanonical:
+ """Canonicalized dedup: slight URL variants collapse to one Sources entry."""
+
+ def setup_method(self):
+ # canonical_url_key is cached across tests; clear so each test starts
+ # from a clean slate (otherwise earlier test inputs can pollute the
+ # expected-output behavior if we ever depend on cache-miss timing).
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ canonical_url_key.cache_clear()
+
+ def test_dedup_trailing_slash(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {"title": "Page", "url": "https://x.com/p", "index": "1"},
+ {"title": "Page", "url": "https://x.com/p/", "index": "2"},
+ ]
+ result = format_links_to_markdown(links)
+ # One entry, two indices.
+ assert result.count("URL: https://x.com/p") == 1
+ assert "[1, 2]" in result
+
+ def test_dedup_tracking_params(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {
+ "title": "Vox",
+ "url": "https://www.vox.com/a?utm_source=g&utm_medium=s",
+ "index": "1",
+ },
+ {"title": "Vox", "url": "https://www.vox.com/a", "index": "2"},
+ ]
+ result = format_links_to_markdown(links)
+ assert "[1, 2]" in result
+ # Canonical (clean) URL is displayed — tracking params stripped.
+ assert "utm_source" not in result
+ assert "utm_medium" not in result
+ assert "URL: https://www.vox.com/a" in result
+
+ def test_dedup_fragment_variants(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {"title": "Wiki", "url": "https://x.com/p", "index": "1"},
+ {
+ "title": "Wiki",
+ "url": "https://x.com/p#section",
+ "index": "2",
+ },
+ ]
+ result = format_links_to_markdown(links)
+ assert "[1, 2]" in result
+
+ def test_dedup_default_port(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {"title": "Site", "url": "https://x.com/p", "index": "1"},
+ {"title": "Site", "url": "https://x.com:443/p", "index": "2"},
+ ]
+ result = format_links_to_markdown(links)
+ assert "[1, 2]" in result
+
+ def test_distinct_urls_stay_distinct(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ links = [
+ {"title": "A", "url": "https://x.com/a", "index": "1"},
+ {"title": "B", "url": "https://x.com/b", "index": "2"},
+ ]
+ result = format_links_to_markdown(links)
+ assert "https://x.com/a" in result
+ assert "https://x.com/b" in result
+ assert "[1, 2]" not in result
+
+ def test_display_shows_canonical_url_not_raw(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ # Even when the utm-bearing variant comes first, the Sources
+ # section shows the canonical (tracking-stripped) URL — cleaner
+ # UI, identical click-through behavior.
+ links = [
+ {
+ "title": "Article",
+ "url": "https://x.com/a?utm_source=g",
+ "index": "1",
+ },
+ {"title": "Article", "url": "https://x.com/a", "index": "2"},
+ ]
+ result = format_links_to_markdown(links)
+ assert "URL: https://x.com/a\n" in result
+ assert "utm_source" not in result
+
+ def test_display_strips_userinfo(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ # Userinfo in a URL would leak credentials if displayed. Canonical
+ # form drops it, so the Sources section is always safe.
+ links = [
+ {
+ "title": "Internal",
+ "url": "https://user:[email protected]/p",
+ "index": "1",
+ }
+ ]
+ result = format_links_to_markdown(links)
+ assert "secret" not in result
+ assert "user:" not in result
+ assert "URL: https://internal.example.com/p" in result
+
+ def test_ref_param_not_stripped(self):
+ from local_deep_research.utilities.search_utilities import (
+ format_links_to_markdown,
+ )
+
+ # ?ref= on GitHub is a branch selector, not a tracker.
+ links = [
+ {
+ "title": "Repo on main",
+ "url": "https://github.com/o/r?ref=main",
+ "index": "1",
+ },
+ {
+ "title": "Repo on dev",
+ "url": "https://github.com/o/r?ref=dev",
+ "index": "2",
+ },
+ ]
+ result = format_links_to_markdown(links)
+ # Must remain two separate entries.
+ assert "ref=main" in result
+ assert "ref=dev" in result
+ assert "[1, 2]" not in result
diff --git a/tests/utilities/test_url_utils.py b/tests/utilities/test_url_utils.py
index ae73ae9318..d8cd5b2d65 100644
--- a/tests/utilities/test_url_utils.py
+++ b/tests/utilities/test_url_utils.py
@@ -121,3 +121,142 @@ class TestNormalizeUrl:
result = normalize_url("http://example.com?q=test&page=1")
assert result == "http://example.com?q=test&page=1"
+
+
+class TestCanonicalUrlKey:
+ """Tests for canonical_url_key — dedup key generation, NOT for display."""
+
+ def setup_method(self):
+ # lru_cache persists across tests; clear to keep each assertion
+ # independent of prior test inputs.
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ canonical_url_key.cache_clear()
+
+ def test_empty_returns_empty(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert canonical_url_key("") == ""
+
+ def test_lowercases_scheme_and_host_preserves_path_case(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("HTTPS://EXAMPLE.COM/Foo/Bar")
+ == "https://example.com/Foo/Bar"
+ )
+
+ def test_strips_fragment(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("https://example.com/page#section")
+ == "https://example.com/page"
+ )
+
+ def test_trailing_slash_normalized(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert canonical_url_key("https://example.com/p") == canonical_url_key(
+ "https://example.com/p/"
+ )
+ # Root path '/' is preserved (not stripped to empty).
+ assert canonical_url_key("https://example.com/").endswith("/")
+
+ def test_strips_utm_and_common_trackers(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ url = (
+ "https://example.com/p?"
+ "utm_source=x&UTM_Medium=y&utm_campaign=z&"
+ "fbclid=a&gclid=b&msclkid=c&yclid=d&dclid=e&gad_source=f&"
+ "mc_eid=g&mc_cid=h&ref_src=i&igshid=j&_ga=k&_gl=l"
+ )
+ assert canonical_url_key(url) == "https://example.com/p"
+
+ def test_keeps_non_tracking_query_params(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ # q, ref (GitHub branch), v (YouTube id), page, id are content-bearing.
+ assert (
+ canonical_url_key("https://github.com/o/r?ref=main&utm_source=x")
+ == "https://github.com/o/r?ref=main"
+ )
+ assert (
+ canonical_url_key(
+ "https://www.youtube.com/watch?v=abc123&utm_source=x"
+ )
+ == "https://www.youtube.com/watch?v=abc123"
+ )
+ assert (
+ canonical_url_key("https://example.com/p?q=hello&page=2")
+ == "https://example.com/p?q=hello&page=2"
+ )
+
+ def test_strips_userinfo(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("https://user:[email protected]/p")
+ == "https://example.com/p"
+ )
+ # Userinfo without colon.
+ assert (
+ canonical_url_key("https://[email protected]/p")
+ == "https://example.com/p"
+ )
+
+ def test_strips_default_ports(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("https://example.com:443/p")
+ == "https://example.com/p"
+ )
+ assert (
+ canonical_url_key("http://example.com:80/p")
+ == "http://example.com/p"
+ )
+
+ def test_preserves_nondefault_port(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("https://example.com:8443/p")
+ == "https://example.com:8443/p"
+ )
+
+ def test_ipv6_host_preserved(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key("https://[::1]:8443/page")
+ == "https://[::1]:8443/page"
+ )
+
+ def test_invalid_url_returned_stripped(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert canonical_url_key(" not a url ") == "not a url"
+
+ def test_mailto_returns_stripped(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert canonical_url_key("mailto:[email protected]") == "mailto:[email protected]"
+
+ def test_protocol_relative_returns_stripped(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ # Protocol-relative URL has no scheme — canonicalization is ambiguous,
+ # so we fall back to the stripped input.
+ assert canonical_url_key("//example.com/path") == "//example.com/path"
+
+ def test_combined_normalization(self):
+ from local_deep_research.utilities.url_utils import canonical_url_key
+
+ assert (
+ canonical_url_key(
+ "HTTPS://User:[email protected]:443/Path/?utm_source=x&q=1#frag"
+ )
+ == "https://example.com/Path?q=1"
+ )